/
Resources.cpp
332 lines (293 loc) · 10.9 KB
/
Resources.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/***************************************************************************
* Copyright (C) 2010 Alexey Aksenov, Alexx Fomichew *
* Alexey Aksenov (ezh at ezh.msk.ru) software, firmware *
* Alexx Fomichew (axx at fomichi.ru) hardware *
* *
* This file is part of ENikiBENiki *
* *
* ENikiBENiki is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* ENikiBENiki is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with ENikiBENiki. If not, see <http://www.gnu.org/licenses/>. *
* *
***************************************************************************/
/*
* idea of wrapper based on source code of Matthias Braun <matze@braunis.de>
*/
#include <ptlib.h>
#include "physfs.h"
#include "Resources.h"
#define new PNEW
Resources::Resources(PString & _resourceExt) : resourceExt(_resourceExt) {
}
Resources::~Resources() {
}
bool Resources::Open(PString & argv0, PString & application) {
if(!PHYSFS_init(argv0.GetPointer())) {
PError << "failure while initialising physfs: " << PHYSFS_getLastError() << endl;
return PFalse;
} else {
PTRACE(5, "successful initialize physfs");
}
const char* basedir = PHYSFS_getBaseDir();
const char* userdir = PHYSFS_getUserDir();
const char* dirsep = PHYSFS_getDirSeparator();
char* writedir = new char[strlen(userdir) + application.GetLength() + 2];
sprintf(writedir, "%s.%s", userdir, application.GetPointer());
PTRACE(5, "physfs base directory: " << basedir);
PTRACE(5, "physfs user directory: " << userdir);
PTRACE(5, "physfs write directory: " << writedir);
if(!PHYSFS_setWriteDir(writedir)) {
// try to create the directory...
char* mkdir = new char[application.GetLength()+2];
sprintf(mkdir, ".%s", application.GetPointer());
if(!PHYSFS_setWriteDir(userdir) || ! PHYSFS_mkdir(mkdir)) {
delete[] writedir;
delete[] mkdir;
PError << "failed creating configuration directory: '" << writedir << "': " << PHYSFS_getLastError() << endl;
return PFalse;
}
delete[] mkdir;
if (!PHYSFS_setWriteDir(writedir)) {
PError << "couldn't set configuration directory to '" << writedir << "': " << PHYSFS_getLastError() << endl;
return PFalse;
}
}
PHYSFS_addToSearchPath(writedir, 0);
PHYSFS_addToSearchPath(basedir, 1);
delete[] writedir;
/* Root out archives, and add them to search path... */
if (resourceExt != NULL) {
char **rc = PHYSFS_enumerateFiles("/");
char **i;
size_t extlen = strlen(resourceExt);
char *ext;
for (i = rc; *i != NULL; i++) {
size_t l = strlen(*i);
if ((l > extlen) && ((*i)[l - extlen - 1] == '.')) {
ext = (*i) + (l - extlen);
if (strcasecmp(ext, resourceExt) == 0) {
PTRACE(5, "Add resource '" << *i << "' to search path");
const char *d = PHYSFS_getRealDir(*i);
char* str = new char[strlen(d) + strlen(dirsep) + l + 1];
sprintf(str, "%s%s%s", d, dirsep, *i);
addToSearchPath(str, 1);
delete[] str;
};
};
};
PHYSFS_freeList(rc);
}
return PTrue;
}
bool Resources::Close() {
PHYSFS_deinit();
return PTrue;
}
bool Resources::addToSearchPath(const char* directory, bool append) {
if(!PHYSFS_addToSearchPath(directory, append)) {
PError << "Couldn't add '" << directory << "' to searchpath: " << PHYSFS_getLastError() << endl;
return PFalse;
};
return PTrue;
}
bool Resources::removeFromSearchPath(const char* directory) {
if(!PHYSFS_removeFromSearchPath(directory)) {
PError << "Couldn't remove '" << directory << "' from searchpath: " << PHYSFS_getLastError() << endl;
return PFalse;
};
return PTrue;
}
const char* Resources::getRealDir(const char* filename) {
return PHYSFS_getRealDir(filename);
}
std::string Resources::getRealName(const char* filename) {
const char* dir = PHYSFS_getRealDir(filename);
if (dir == 0) {
PError << "no such path '" << filename << "'" << endl;
return NULL;
};
std::string realname = dir;
realname += PHYSFS_getDirSeparator();
realname += filename;
return realname;
}
std::string Resources::getRealWriteName(const char* filename) {
const char* dir = PHYSFS_getWriteDir();
if (dir == 0) {
PError << "no writedir defined" << endl;
return NULL;
}
std::string realname = dir;
realname += PHYSFS_getDirSeparator();
realname += filename;
return realname;
}
char** Resources::enumerateFiles(const char* directory) {
return PHYSFS_enumerateFiles(directory);
}
void Resources::freeList(char** list) {
PHYSFS_freeList(list);
}
ResourceWO * Resources::RetrieveWrite(PString & filename) {
PHYSFS_file* file = PHYSFS_openWrite(filename);
if(!file) {
PError << "couldn't open file '" << filename << "' for writing: " << PHYSFS_getLastError() << endl;
return NULL;
};
return new ResourceWO(file);
}
ResourceRO * Resources::RetrieveRead(PString & filename) {
PHYSFS_file* file = PHYSFS_openRead(filename);
if(!file) {
const int fn_length = filename.GetLength()+1;
char fn[4096];
memcpy(fn, filename.GetPointer(), fn_length); // includes \0;
char * folder_sep = strrchr(fn, '/');
char * fn_start = fn;
char ** filelist = 0;
if ( folder_sep ) {
*folder_sep = 0;
filelist = enumerateFiles(fn);
*folder_sep = '/';
fn_start = folder_sep+1;
} else {
filelist = enumerateFiles(".");
folder_sep = fn;
};
if (filelist) {
for(char** curfile = filelist; *curfile != 0; curfile++) {
if ( strcasecmp(*curfile, fn_start) == 0 ) {
memcpy(fn_start, *curfile, fn_length-(folder_sep-fn));
file = PHYSFS_openRead(fn);
break;
};
};
freeList(filelist);
};
if (!file) {
PError << "couldn't open file '" << filename << "' for reading: " << PHYSFS_getLastError() << endl;
return NULL;
};
};
return new ResourceRO(file);
}
ResourceWO * Resources::RetrieveAppend(PString & filename) {
PHYSFS_file* file = PHYSFS_openAppend(filename);
if(!file) {
PError << "couldn't open file '" << filename << "' for writing(append): %s" << PHYSFS_getLastError() << endl;
return NULL;
};
return new ResourceWO(file);
}
bool Resources::mkdir(const char* directory) {
if(!PHYSFS_mkdir(directory)) {
PError << "couldn't create directory '" << directory << "': " << PHYSFS_getLastError() << endl;
return PFalse;
};
return PTrue;
}
bool Resources::remove(const char* filename) {
if(!PHYSFS_delete(filename)) {
PError << "couldn't remove file '" << filename << "': " << PHYSFS_getLastError() << endl;
return PFalse;
};
return PTrue;
}
bool Resources::exists(const char* filename) {
return PHYSFS_exists(filename);
}
bool Resources::isDirectory(const char* filename) {
return PHYSFS_isDirectory(filename);
}
bool Resources::isSymbolicLink(const char* filename) {
return PHYSFS_isSymbolicLink(filename);
}
int64_t Resources::getLastModTime(const char* filename) {
int64_t modtime = PHYSFS_getLastModTime(filename);
if (modtime < 0) {
PError << "couldn't determine modification time of '" << filename << "': " << PHYSFS_getLastError() << endl;
return NULL;
};
return modtime;
}
SDL_Surface * Resources::LoadImage(PString & imageName) {
PTRACE(4, "load image '" << imageName << "' from resources");
SDL_Surface * imageSurface = 0;
ResourceRO * imageFile = RetrieveRead(imageName);
if (!imageFile)
return NULL;
SDL_RWops * rw = imageFile->GetSDLRWOps();
if (!rw)
return NULL;
imageSurface = SDL_LoadBMP_RW(rw, 0);
PTRACE(4, "image width: " << imageSurface->w << ", image height: " << imageSurface->h);
return imageSurface;
}
SDL_Surface * Resources::LoadImageOptimized(PString & imageName) {
SDL_Surface * imageSurface = 0;
SDL_Surface * optimizedSurface = 0;
imageSurface = LoadImage(imageName);
if (!imageSurface)
return NULL;
//Create an optimized image
optimizedSurface = SDL_DisplayFormat(imageSurface);
//Free the old surface
SDL_FreeSurface(imageSurface);
return optimizedSurface;
}
bool Resources::LoadTextFile(PString & fileName, PStringArray & textBuffer) {
PTRACE(4, "load textfile '" << fileName << "' from resources");
bool result;
ResourceRO * textFile = RetrieveRead(fileName);
result = textFile->readText(textBuffer);
delete textFile;
return result;
}
//int * ResFile::loadFile(char *fileName, int *fileSize) {
/* PHYSFS_file *compFile;
PHYSFS_sint64 fileLength;
int *levelInMem = NULL;
char tempFilename[MAX_STRING_SIZE];
if (NO == fileSystemReady) {
SDL_SetError("PHYSFS system has not been initialised.");
return NULL;
}
// Get a handle to the file
compFile = PHYSFS_openRead(fileName);
if (NULL == compFile) {
strcpy(tempFilename, "");
strcpy(tempFilename, dataDirectory);
strcat(tempFilename, fileName);
compFile = PHYSFS_openRead(fileName);
if (NULL == compFile)
return NULL;
}
// Get it's size
fileLength = PHYSFS_fileLength(compFile);
if (-1 == fileLength) {
//CON_Out (mainConsole, "Unable to determine file length for [ %s ]", fileName);
return NULL;
}
// Pass the filesize back
*fileSize = (int)fileLength;
// conPrint (true, "Size of [ %s ] is [ %i ]", fileName, fileLength);
if (NULL != levelInMem)
free(levelInMem);
levelInMem = (int *)malloc((int)fileLength);
int returnCode = (int)PHYSFS_read(compFile, (void *)levelInMem, (PHYSFS_uint32)fileLength, 1);
if (-1 == returnCode) {
return NULL;
};
PHYSFS_close(compFile);
return (int *)levelInMem;*/
// return 0;
//}