-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleFS.cpp
More file actions
178 lines (139 loc) · 3.93 KB
/
ModuleFS.cpp
File metadata and controls
178 lines (139 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "Application.h"
#include "ModuleFS.h"
#pragma comment( lib, "PhysFS/libx86/physfs.lib" )
#include "PhysFS\include\physfs.h"
#include <string>
ModuleFS::ModuleFS(Application* app, bool start_enabled) : Module(app, start_enabled)
{
base_path = SDL_GetBasePath();
PHYSFS_init(base_path);
AddPath(".");
CreateDir(LIBRARY_BASE_PATH, true);
CreateDir(LIBRARY_MESHES_PATH);
CreateDir(LIBRARY_TEXTURES_PATH);
CreateDir(SETTINGS_BASE_PATH);
}
ModuleFS::~ModuleFS()
{
PHYSFS_deinit();
}
// Add a new zip file or folder
bool ModuleFS::AddPath(const char* path_or_zip, const char* mount_point)
{
bool ret = false;
if(PHYSFS_mount(path_or_zip, mount_point, 1) == 0)
App->gui->app_log.AddLog("File System error while adding a path or zip(%s): %s\n", path_or_zip, PHYSFS_getLastError());
else
ret = true;
return ret;
}
bool ModuleFS::Exists(const char* file) const
{
return PHYSFS_exists(file) != 0;
}
bool ModuleFS::IsDirectory(const char* file) const
{
return PHYSFS_isDirectory(file) != 0;
}
// Read a whole file and put it in a new buffer
char* ModuleFS::Load(const char* file, uint& size) const
{
char* buffer = nullptr;
PHYSFS_file* fs_file = PHYSFS_openRead(file);
if(fs_file != NULL)
{
size = PHYSFS_fileLength(fs_file);
if(size > 0)
{
buffer = new char[size];
PHYSFS_sint64 readed = PHYSFS_read(fs_file, buffer, 1, (PHYSFS_sint32)size);
if(readed != size)
{
App->gui->app_log.AddLog("File System error while reading from file %s: %s\n", file, PHYSFS_getLastError());
if (buffer) {
delete[] buffer;
buffer = nullptr;
}
}
}
if(PHYSFS_close(fs_file) == 0)
App->gui->app_log.AddLog("File System error while closing file %s: %s\n", file, PHYSFS_getLastError());
}
else
App->gui->app_log.AddLog("File System error while opening file %s: %s\n", file, PHYSFS_getLastError());
return buffer;
}
// Read a whole file and put it in a new buffer
SDL_RWops* ModuleFS::Load(const char* file) const
{
uint size = 0;
char* buffer = Load(file, size);
if(size > 0)
{
SDL_RWops* r = SDL_RWFromConstMem(buffer, size);
if(r != NULL)
r->close = close_sdl_rwops;
return r;
}
else
return NULL;
}
int close_sdl_rwops(SDL_RWops *rw)
{
if (rw->hidden.mem.base)
delete rw->hidden.mem.base;
SDL_FreeRW(rw);
return 0;
}
// Save a whole buffer to disk
unsigned int ModuleFS::Save(const char* file_name, const void* data, const char* write_dir, unsigned int size)const
{
unsigned int ret = 0;
if (PHYSFS_setWriteDir(write_dir) != 0) {
PHYSFS_file* fs_file = PHYSFS_openWrite(file_name);
if (fs_file != NULL)
{
PHYSFS_sint64 written = PHYSFS_write(fs_file, data, 1, size);
if (written != size)
App->gui->app_log.AddLog("File System error while writing to file %s: %s\n", file_name, PHYSFS_getLastError());
else
ret = (uint)written;
if (PHYSFS_close(fs_file) == 0)
App->gui->app_log.AddLog("File System error while closing file %s: %s\n", file_name, PHYSFS_getLastError());
}
else
App->gui->app_log.AddLog("File System error while opening file %s: %s\n", file_name, PHYSFS_getLastError());
}
else
App->gui->app_log.AddLog("File System error while setting write dir %s: %s\n", write_dir, PHYSFS_getLastError());
return ret;
}
void ModuleFS::CreateDir(const char* path, bool hidden)
{
DWORD ftyp = GetFileAttributesA(path);
LPCSTR file = path;
if (ftyp == INVALID_FILE_ATTRIBUTES) {
CreateDirectory(file, NULL);
if (hidden) SetFileAttributes(file, FILE_ATTRIBUTE_HIDDEN);
}
if (ftyp & FILE_ATTRIBUTE_DIRECTORY);
//It exists
}
std::vector<std::string> ModuleFS::GetFilesInDir(const char* dir, bool append_root_folder)
{
std::vector<std::string> files;
char** assets = PHYSFS_enumerateFiles(dir);
if (assets != nullptr) {
for (int i = 0; assets[i] != nullptr; i++)
{
std::string path(assets[i]);
if (append_root_folder) {
path.insert(0, "/");
path.insert(0, dir);
}
files.push_back(path);
}
}
PHYSFS_freeList(assets);
return files;
}