-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleFileSystem.cpp
More file actions
175 lines (143 loc) · 3.97 KB
/
ModuleFileSystem.cpp
File metadata and controls
175 lines (143 loc) · 3.97 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
#include "Application.h"
#include "ModuleFileSystem.h"
#include "PhysFS/include/physfs.h"
#include "SDL/include/SDL.h"
#pragma comment( lib, "PhysFS/libx86/physfs.lib" )
ModuleFileSystem::ModuleFileSystem(Application* app, bool start_enabled) : Module(app, start_enabled)
{
name = "file_system";
// need to be created before Awake so other modules can use it
char* base_path = SDL_GetBasePath();
PHYSFS_init(base_path);
SDL_free(base_path);
// By default we include executable's own directory
// without this we won't be able to find config.xml :-(
AddPath(".");
}
// Destructor
ModuleFileSystem::~ModuleFileSystem()
{
PHYSFS_deinit();
}
// Called before render is available
bool ModuleFileSystem::Awake(pugi::xml_node& config)
{
LOG("Loading File System");
bool ret = true;
// Add all paths in configuration in order
for(pugi::xml_node path = config.child("path"); path; path = path.next_sibling("path"))
{
AddPath(path.child_value());
}
// Ask SDL for a write dir
char* write_path = SDL_GetPrefPath(App->GetOrganization(), App->GetTitle());
if (PHYSFS_setWriteDir(write_path) == 0)
{
LOG("File System error while creating write dir: %s\n", PHYSFS_getLastError());
}
else
{
// We add the writing directory as a reading directory too with speacial mount point
LOG("Writing directory is %s\n", write_path);
AddPath(write_path, GetSaveDirectory());
}
SDL_free(write_path);
return ret;
}
// Called before quitting
bool ModuleFileSystem::CleanUp()
{
//LOG("Freeing File System subsystem");
return true;
}
// Add a new zip file or folder
bool ModuleFileSystem::AddPath(const char* path_or_zip, const char* mount_point)
{
bool ret = false;
if (PHYSFS_mount(path_or_zip, mount_point, 1) == 0)
{
LOG("File System error while adding a path or zip(%s): %s\n", path_or_zip, PHYSFS_getLastError());
}
else
ret = true;
return ret;
}
// Check if a file exists
bool ModuleFileSystem::Exists(const char* file) const
{
return PHYSFS_exists(file) != 0;
}
// Check if a file is a directory
bool ModuleFileSystem::IsDirectory(const char* file) const
{
return PHYSFS_isDirectory(file) != 0;
}
// Read a whole file and put it in a new buffer
unsigned int ModuleFileSystem::Load(const char* file, char** buffer) const
{
unsigned int ret = 0;
PHYSFS_file* fs_file = PHYSFS_openRead(file);
if(fs_file != NULL)
{
PHYSFS_sint64 size = PHYSFS_fileLength(fs_file);
if(size > 0)
{
*buffer = new char[(uint)size];
PHYSFS_sint64 readed = PHYSFS_read(fs_file, *buffer, 1, (PHYSFS_sint32)size);
if(readed != size)
{
LOG("File System error while reading from file %s: %s\n", file, PHYSFS_getLastError());
RELEASE(buffer);
}
else
ret = (uint)readed;
}
if(PHYSFS_close(fs_file) == 0)
LOG("File System error while closing file %s: %s\n", file, PHYSFS_getLastError());
}
else
LOG("File System error while opening file %s: %s\n", file, PHYSFS_getLastError());
return ret;
}
// Read a whole file and put it in a new buffer
SDL_RWops* ModuleFileSystem::Load(const char* file) const
{
char* buffer;
int size = Load(file, &buffer);
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)
{
RELEASE(rw->hidden.mem.base);
SDL_FreeRW(rw);
return 0;
}
// Save a whole buffer to disk
unsigned int ModuleFileSystem::Save(const char* file, const char* buffer, unsigned int size) const
{
unsigned int ret = 0;
PHYSFS_file* fs_file = PHYSFS_openWrite(file);
if(fs_file != NULL)
{
PHYSFS_sint64 written = PHYSFS_write(fs_file, (const void*)buffer, 1, size);
if (written != size)
{
LOG("File System error while writing to file %s: %s\n", file, PHYSFS_getLastError());
}
else
ret = (uint) written;
if(PHYSFS_close(fs_file) == 0)
LOG("File System error while closing file %s: %s\n", file, PHYSFS_getLastError());
}
else
LOG("File System error while opening file %s: %s\n", file, PHYSFS_getLastError());
return ret;
}