1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: Patrick Boenzli |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
17 | |
---|
18 | #include "resource_manager.h" |
---|
19 | |
---|
20 | #include "debug.h" |
---|
21 | |
---|
22 | // different resource Types |
---|
23 | #ifndef NO_MODEL |
---|
24 | #include "objModel.h" |
---|
25 | #include "primitive_model.h" |
---|
26 | #include "md2Model.h" |
---|
27 | #endif /* NO_MODEL */ |
---|
28 | #ifndef NO_TEXTURES |
---|
29 | #include "texture.h" |
---|
30 | #endif /* NO_TEXTURES */ |
---|
31 | #ifndef NO_TEXT |
---|
32 | #include "font.h" |
---|
33 | #endif /* NO_TEXT */ |
---|
34 | #ifndef NO_AUDIO |
---|
35 | #include "sound_engine.h" |
---|
36 | #include "ogg_player.h" |
---|
37 | #endif /* NO_AUDIO */ |
---|
38 | #ifndef NO_SHADERS |
---|
39 | #include "shader.h" |
---|
40 | #endif /* NO_SHADERS */ |
---|
41 | |
---|
42 | #include "list.h" |
---|
43 | #include "sdlincl.h" |
---|
44 | |
---|
45 | // File Handling Includes |
---|
46 | #include <sys/types.h> |
---|
47 | #include <sys/stat.h> |
---|
48 | #include <unistd.h> |
---|
49 | |
---|
50 | using namespace std; |
---|
51 | |
---|
52 | /** |
---|
53 | * standard constructor |
---|
54 | */ |
---|
55 | ResourceManager::ResourceManager () |
---|
56 | { |
---|
57 | this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager"); |
---|
58 | this->setName("ResourceManager"); |
---|
59 | |
---|
60 | this->dataDir = new char[3]; |
---|
61 | strcpy(this->dataDir, "./"); |
---|
62 | this->tryDataDir("./data"); |
---|
63 | this->imageDirs = new tList<char>; |
---|
64 | this->resourceList = new tList<Resource>; |
---|
65 | } |
---|
66 | |
---|
67 | //! Singleton Reference to the ResourceManager |
---|
68 | ResourceManager* ResourceManager::singletonRef = NULL; |
---|
69 | |
---|
70 | /** |
---|
71 | * standard destructor |
---|
72 | */ |
---|
73 | ResourceManager::~ResourceManager () |
---|
74 | { |
---|
75 | // deleting the Resources-List |
---|
76 | this->unloadAllByPriority(RP_GAME); |
---|
77 | |
---|
78 | if (this->resourceList->getSize() > 0) |
---|
79 | PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList->getSize()); |
---|
80 | |
---|
81 | delete this->resourceList; |
---|
82 | // deleting the Directorie Lists |
---|
83 | tIterator<char>* tmpIt = imageDirs->getIterator(); |
---|
84 | char* tmpDir = tmpIt->firstElement(); |
---|
85 | while(tmpDir) |
---|
86 | { |
---|
87 | delete[] tmpDir; |
---|
88 | tmpDir = tmpIt->nextElement(); |
---|
89 | } |
---|
90 | delete tmpIt; |
---|
91 | delete this->imageDirs; |
---|
92 | |
---|
93 | delete[] this->dataDir; |
---|
94 | |
---|
95 | ResourceManager::singletonRef = NULL; |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * sets the data main directory |
---|
100 | * @param dataDir the DataDirectory. |
---|
101 | */ |
---|
102 | bool ResourceManager::setDataDir(const char* dataDir) |
---|
103 | { |
---|
104 | char* realDir = ResourceManager::homeDirCheck(dataDir); |
---|
105 | if (isDir(realDir)) |
---|
106 | { |
---|
107 | delete[] this->dataDir; |
---|
108 | if (dataDir[strlen(dataDir)-1] == '/' || dataDir[strlen(dataDir)-1] == '\\') |
---|
109 | { |
---|
110 | this->dataDir = new char[strlen(realDir)+1]; |
---|
111 | strcpy(this->dataDir, realDir); |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | this->dataDir = new char[strlen(realDir)+2]; |
---|
116 | strcpy(this->dataDir, realDir); |
---|
117 | this->dataDir[strlen(realDir)] = '/'; |
---|
118 | this->dataDir[strlen(realDir)+1] = '\0'; |
---|
119 | } |
---|
120 | delete[] realDir; |
---|
121 | return true; |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", realDir, this->dataDir); |
---|
126 | delete[] realDir; |
---|
127 | return false; |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * sets the data main directory |
---|
133 | * @param dataDir the DataDirectory. |
---|
134 | * |
---|
135 | * this is essentially the same as setDataDir, but it ommits the error-message |
---|
136 | */ |
---|
137 | bool ResourceManager::tryDataDir(const char* dataDir) |
---|
138 | { |
---|
139 | char* realDir = ResourceManager::homeDirCheck(dataDir); |
---|
140 | if (isDir(realDir)) |
---|
141 | { |
---|
142 | delete[] this->dataDir; |
---|
143 | if (dataDir[strlen(dataDir)-1] == '/' || dataDir[strlen(dataDir)-1] == '\\') |
---|
144 | { |
---|
145 | this->dataDir = new char[strlen(realDir)+1]; |
---|
146 | strcpy(this->dataDir, realDir); |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | this->dataDir = new char[strlen(realDir)+2]; |
---|
151 | strcpy(this->dataDir, realDir); |
---|
152 | this->dataDir[strlen(realDir)] = '/'; |
---|
153 | this->dataDir[strlen(realDir)+1] = '\0'; |
---|
154 | } |
---|
155 | delete[] realDir; |
---|
156 | return true; |
---|
157 | } |
---|
158 | delete[] realDir; |
---|
159 | return false; |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | /** |
---|
165 | * checks for the DataDirectory, by looking if |
---|
166 | * @param fileInside is iniside of the given directory. |
---|
167 | */ |
---|
168 | bool ResourceManager::verifyDataDir(const char* fileInside) |
---|
169 | { |
---|
170 | bool retVal; |
---|
171 | if (!isDir(this->dataDir)) |
---|
172 | { |
---|
173 | PRINTF(1)("%s is not a directory\n", this->dataDir); |
---|
174 | return false; |
---|
175 | } |
---|
176 | |
---|
177 | char* testFile = new char[strlen(this->dataDir)+strlen(fileInside)+1]; |
---|
178 | sprintf(testFile, "%s%s", this->dataDir, fileInside); |
---|
179 | retVal = isFile(testFile); |
---|
180 | delete[] testFile; |
---|
181 | return retVal; |
---|
182 | } |
---|
183 | |
---|
184 | #ifndef NO_TEXTURES |
---|
185 | /** |
---|
186 | * adds a new Path for Images |
---|
187 | * @param imageDir The path to insert |
---|
188 | * @returns true, if the Path was well and injected (or already existent within the list) |
---|
189 | false otherwise |
---|
190 | */ |
---|
191 | bool ResourceManager::addImageDir(const char* imageDir) |
---|
192 | { |
---|
193 | if (imageDir == NULL) |
---|
194 | return false; |
---|
195 | |
---|
196 | char* newDir; |
---|
197 | if (imageDir[strlen(imageDir)-1] == '/' || imageDir[strlen(imageDir)-1] == '\\') |
---|
198 | { |
---|
199 | newDir = new char[strlen(imageDir)+1]; |
---|
200 | strcpy(newDir, imageDir); |
---|
201 | } |
---|
202 | else |
---|
203 | { |
---|
204 | newDir = new char[strlen(imageDir)+2]; |
---|
205 | strcpy(newDir, imageDir); |
---|
206 | newDir[strlen(imageDir)] = '/'; |
---|
207 | newDir[strlen(imageDir)+1] = '\0'; |
---|
208 | } |
---|
209 | // check if the param is a Directory |
---|
210 | if (isDir(newDir)) |
---|
211 | { |
---|
212 | // check if the Directory has been added before |
---|
213 | tIterator<char>* tmpImageDirs = imageDirs->getIterator(); |
---|
214 | char* tmpDir = tmpImageDirs->firstElement(); |
---|
215 | while(tmpDir) |
---|
216 | { |
---|
217 | if (!strcmp(tmpDir, newDir)) |
---|
218 | { |
---|
219 | PRINTF(3)("Path %s already loaded\n", newDir); |
---|
220 | delete[] newDir; |
---|
221 | delete tmpImageDirs; |
---|
222 | return true; |
---|
223 | } |
---|
224 | tmpDir = tmpImageDirs->nextElement(); |
---|
225 | } |
---|
226 | delete tmpImageDirs; |
---|
227 | |
---|
228 | // adding the directory to the List |
---|
229 | this->imageDirs->add(newDir); |
---|
230 | return true; |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir); |
---|
235 | delete[] newDir; |
---|
236 | return false; |
---|
237 | } |
---|
238 | } |
---|
239 | #endif /* NO_TEXTURES */ |
---|
240 | |
---|
241 | /** |
---|
242 | * loads resources |
---|
243 | * @param fileName: The fileName of the resource to load |
---|
244 | * @param prio: The ResourcePriority of this resource (will only be increased) |
---|
245 | * @param param1: an additional option to parse (see the constuctors for more help) |
---|
246 | * @param param2: an additional option to parse (see the constuctors for more help) |
---|
247 | * @param param3: an additional option to parse (see the constuctors for more help) |
---|
248 | * @returns a pointer to a desired Resource. |
---|
249 | */ |
---|
250 | BaseObject* ResourceManager::load(const char* fileName, ResourcePriority prio, void* param1, void* param2, void* param3) |
---|
251 | { |
---|
252 | if (fileName == NULL) |
---|
253 | return NULL; |
---|
254 | ResourceType tmpType; |
---|
255 | #ifndef NO_MODEL |
---|
256 | #define __IF_OK |
---|
257 | if (!strncasecmp(fileName+(strlen(fileName)-4), ".obj", 4)) |
---|
258 | tmpType = OBJ; |
---|
259 | else if (!strncmp(fileName+(strlen(fileName)-4), ".md2", 4)) |
---|
260 | tmpType = MD2; |
---|
261 | else if (!strcasecmp(fileName, "cube") || |
---|
262 | !strcasecmp(fileName, "sphere") || |
---|
263 | !strcasecmp(fileName, "plane") || |
---|
264 | !strcasecmp(fileName, "cylinder") || |
---|
265 | !strcasecmp(fileName, "cone")) |
---|
266 | tmpType = PRIM; |
---|
267 | #endif /* NO_MODEL */ |
---|
268 | #ifndef NO_AUDIO |
---|
269 | #ifdef __IF_OK |
---|
270 | else |
---|
271 | #endif |
---|
272 | #define __IF_OK |
---|
273 | if (!strncasecmp(fileName+(strlen(fileName)-4), ".wav", 4)) |
---|
274 | tmpType = WAV; |
---|
275 | else if (!strncasecmp(fileName+(strlen(fileName)-4), ".mp3", 4)) |
---|
276 | tmpType = MP3; |
---|
277 | else if (!strncasecmp(fileName+(strlen(fileName)-4), ".ogg", 4)) |
---|
278 | tmpType = OGG; |
---|
279 | #endif /* NO_AUDIO */ |
---|
280 | #ifndef NO_TEXT |
---|
281 | #ifdef __IF_OK |
---|
282 | else |
---|
283 | #endif |
---|
284 | #define __IF_OK |
---|
285 | if (!strncasecmp(fileName+(strlen(fileName)-4), ".ttf", 4)) |
---|
286 | tmpType = TTF; |
---|
287 | #endif /* NO_TEXT */ |
---|
288 | #ifndef NO_SHADERS |
---|
289 | #ifdef __IF_OK |
---|
290 | else |
---|
291 | #endif |
---|
292 | #define __IF_OK |
---|
293 | if (!strncasecmp(fileName+(strlen(fileName)-5), ".vert", 5)) |
---|
294 | tmpType = SHADER; |
---|
295 | #endif /* NO_SHADERS */ |
---|
296 | #ifndef NO_TEXTURES |
---|
297 | #ifdef __IF_OK |
---|
298 | else |
---|
299 | #else |
---|
300 | if |
---|
301 | #endif |
---|
302 | tmpType = IMAGE; |
---|
303 | #endif /* NO_TEXTURES */ |
---|
304 | #undef __IF_OK |
---|
305 | return this->load(fileName, tmpType, prio, param1, param2, param3); |
---|
306 | } |
---|
307 | |
---|
308 | /** |
---|
309 | * loads resources |
---|
310 | * @param fileName: The fileName of the resource to load |
---|
311 | * @param type: The Type of Resource to load. |
---|
312 | * @param prio: The ResourcePriority of this resource (will only be increased) |
---|
313 | * @param param1: an additional option to parse (see the constuctors for more help) |
---|
314 | * @param param2: an additional option to parse (see the constuctors for more help) |
---|
315 | * @param param3: an additional option to parse (see the constuctors for more help) |
---|
316 | * @returns a pointer to a desired Resource. |
---|
317 | */ |
---|
318 | BaseObject* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio, |
---|
319 | void* param1, void* param2, void* param3) |
---|
320 | { |
---|
321 | if (fileName == NULL) |
---|
322 | return NULL; |
---|
323 | |
---|
324 | // searching if the resource was loaded before. |
---|
325 | Resource* tmpResource = this->locateResourceByInfo(fileName, type, param1, param2, param3); |
---|
326 | if (tmpResource != NULL) // if the resource was loaded before. |
---|
327 | { |
---|
328 | PRINTF(4)("not loading cached resource %s\n", tmpResource->name); |
---|
329 | tmpResource->count++; |
---|
330 | if(tmpResource->prio < prio) |
---|
331 | tmpResource->prio = prio; |
---|
332 | } |
---|
333 | else |
---|
334 | { |
---|
335 | // Setting up the new Resource |
---|
336 | tmpResource = new Resource; |
---|
337 | tmpResource->count = 1; |
---|
338 | tmpResource->type = type; |
---|
339 | tmpResource->prio = prio; |
---|
340 | tmpResource->pointer = NULL; |
---|
341 | tmpResource->name = new char[strlen(fileName)+1]; |
---|
342 | strcpy(tmpResource->name, fileName); |
---|
343 | |
---|
344 | // creating the full name. (directoryName + FileName) |
---|
345 | char* fullName = ResourceManager::getFullName(fileName); |
---|
346 | // Checking for the type of resource \see ResourceType |
---|
347 | switch(type) |
---|
348 | { |
---|
349 | #ifndef NO_MODEL |
---|
350 | case OBJ: |
---|
351 | if (param1) |
---|
352 | tmpResource->modelSize = *(float*)param1; |
---|
353 | else |
---|
354 | tmpResource->modelSize = 1.0; |
---|
355 | |
---|
356 | if(ResourceManager::isFile(fullName)) |
---|
357 | tmpResource->pointer = new OBJModel(fullName, tmpResource->modelSize); |
---|
358 | else |
---|
359 | { |
---|
360 | PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName, dataDir); |
---|
361 | tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, &tmpResource->modelSize); |
---|
362 | } |
---|
363 | break; |
---|
364 | case PRIM: |
---|
365 | if (param1) |
---|
366 | tmpResource->modelSize = *(float*)param1; |
---|
367 | else |
---|
368 | tmpResource->modelSize = 1.0; |
---|
369 | |
---|
370 | if (!strcmp(tmpResource->name, "cube")) |
---|
371 | tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->modelSize); |
---|
372 | else if (!strcmp(tmpResource->name, "sphere")) |
---|
373 | tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->modelSize); |
---|
374 | else if (!strcmp(tmpResource->name, "plane")) |
---|
375 | tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->modelSize); |
---|
376 | else if (!strcmp(tmpResource->name, "cylinder")) |
---|
377 | tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->modelSize); |
---|
378 | else if (!strcmp(tmpResource->name, "cone")) |
---|
379 | tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->modelSize); |
---|
380 | break; |
---|
381 | case MD2: |
---|
382 | if(ResourceManager::isFile(fullName)) |
---|
383 | { |
---|
384 | if (param1 != NULL) |
---|
385 | { |
---|
386 | tmpResource->secFileName = new char[strlen((const char*)param1)+1]; |
---|
387 | strcpy(tmpResource->secFileName, (const char*) param1); |
---|
388 | } |
---|
389 | else |
---|
390 | tmpResource->secFileName = NULL; |
---|
391 | tmpResource->pointer = new MD2Data(fullName, tmpResource->secFileName); |
---|
392 | } |
---|
393 | break; |
---|
394 | #endif /* NO_MODEL */ |
---|
395 | #ifndef NO_TEXT |
---|
396 | case TTF: |
---|
397 | if (param1 != NULL) |
---|
398 | tmpResource->ttfSize = *(unsigned int*)param1; |
---|
399 | else |
---|
400 | tmpResource->ttfSize = FONT_DEFAULT_RENDER_SIZE; |
---|
401 | |
---|
402 | if(isFile(fullName)) |
---|
403 | tmpResource->pointer = new Font(fullName, tmpResource->ttfSize); |
---|
404 | else |
---|
405 | PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir); |
---|
406 | break; |
---|
407 | #endif /* NO_TEXT */ |
---|
408 | #ifndef NO_AUDIO |
---|
409 | case WAV: |
---|
410 | if(isFile(fullName)) |
---|
411 | tmpResource->pointer = new SoundBuffer(fullName); |
---|
412 | break; |
---|
413 | case OGG: |
---|
414 | if (isFile(fullName)) |
---|
415 | tmpResource->pointer = new OggPlayer(fullName); |
---|
416 | break; |
---|
417 | #endif /* NO_AUDIO */ |
---|
418 | #ifndef NO_TEXTURES |
---|
419 | case IMAGE: |
---|
420 | if(isFile(fullName)) |
---|
421 | { |
---|
422 | PRINTF(4)("Image %s resides to %s\n", fileName, fullName); |
---|
423 | tmpResource->pointer = new Texture(fullName); |
---|
424 | } |
---|
425 | else |
---|
426 | { |
---|
427 | char* tmpDir; |
---|
428 | tIterator<char>* iterator = imageDirs->getIterator(); |
---|
429 | tmpDir = iterator->firstElement(); |
---|
430 | while(tmpDir) |
---|
431 | { |
---|
432 | char* imgName = new char[strlen(tmpDir)+strlen(fileName)+1]; |
---|
433 | sprintf(imgName, "%s%s", tmpDir, fileName); |
---|
434 | if(isFile(imgName)) |
---|
435 | { |
---|
436 | PRINTF(4)("Image %s resides to %s\n", fileName, imgName); |
---|
437 | tmpResource->pointer = new Texture(imgName); |
---|
438 | delete[] imgName; |
---|
439 | break; |
---|
440 | } |
---|
441 | delete[] imgName; |
---|
442 | tmpDir = iterator->nextElement(); |
---|
443 | } |
---|
444 | delete iterator; |
---|
445 | } |
---|
446 | if(!tmpResource) |
---|
447 | PRINTF(2)("!!Image %s not Found!!\n", fileName); |
---|
448 | break; |
---|
449 | #endif /* NO_TEXTURES */ |
---|
450 | #ifndef NO_SHADERS |
---|
451 | case SHADER: |
---|
452 | if(ResourceManager::isFile(fullName)) |
---|
453 | { |
---|
454 | if (param1 != NULL) |
---|
455 | { |
---|
456 | char* secFullName = ResourceManager::getFullName((const char*)param1); |
---|
457 | if (ResourceManager::isFile(secFullName)) |
---|
458 | { |
---|
459 | tmpResource->secFileName = new char[strlen((const char*)param1)+1]; |
---|
460 | strcpy(tmpResource->secFileName, (const char*) param1); |
---|
461 | tmpResource->pointer = new Shader(fullName, secFullName); |
---|
462 | } |
---|
463 | delete[] secFullName; |
---|
464 | } |
---|
465 | else |
---|
466 | { |
---|
467 | tmpResource->secFileName = NULL; |
---|
468 | tmpResource->pointer = new Shader(fullName, NULL); |
---|
469 | } |
---|
470 | } |
---|
471 | break; |
---|
472 | #endif /* NO_SHADERS */ |
---|
473 | default: |
---|
474 | tmpResource->pointer = NULL; |
---|
475 | PRINTF(1)("No type found for %s.\n !!This should not happen unless the Type is not supported yet. JUST DO IT!!\n", tmpResource->name); |
---|
476 | break; |
---|
477 | } |
---|
478 | if (tmpResource->pointer != NULL) |
---|
479 | this->resourceList->add(tmpResource); |
---|
480 | delete[] fullName; |
---|
481 | } |
---|
482 | if (tmpResource->pointer != NULL) |
---|
483 | return tmpResource->pointer; |
---|
484 | else |
---|
485 | { |
---|
486 | PRINTF(2)("Resource %s could not be loaded\n", fileName); |
---|
487 | delete[] tmpResource->name; |
---|
488 | delete tmpResource; |
---|
489 | return NULL; |
---|
490 | } |
---|
491 | } |
---|
492 | |
---|
493 | /** |
---|
494 | * unloads a Resource |
---|
495 | * @param pointer: The pointer to free |
---|
496 | * @param prio: the PriorityLevel to unload this resource |
---|
497 | * @returns true if successful (pointer found, and deleted), false otherwise |
---|
498 | */ |
---|
499 | bool ResourceManager::unload(void* pointer, ResourcePriority prio) |
---|
500 | { |
---|
501 | if (pointer == NULL) |
---|
502 | return false; |
---|
503 | // if pointer is existent. and only one resource of this type exists. |
---|
504 | Resource* tmpResource = this->locateResourceByPointer(pointer); |
---|
505 | if (tmpResource != NULL) |
---|
506 | return unload(tmpResource, prio); |
---|
507 | else |
---|
508 | { |
---|
509 | PRINTF(2)("Resource not Found %p\n", pointer); |
---|
510 | return false; |
---|
511 | } |
---|
512 | } |
---|
513 | |
---|
514 | /** |
---|
515 | * unloads a Resource |
---|
516 | * @param resource: The resource to unloade |
---|
517 | * @param prio the PriorityLevel to unload this resource |
---|
518 | * @returns true on success, false otherwise. |
---|
519 | */ |
---|
520 | bool ResourceManager::unload(Resource* resource, ResourcePriority prio) |
---|
521 | { |
---|
522 | if (resource == NULL) |
---|
523 | return false; |
---|
524 | if (resource->count > 0) |
---|
525 | resource->count--; |
---|
526 | |
---|
527 | if (resource->prio <= prio) |
---|
528 | { |
---|
529 | if (resource->count == 0) |
---|
530 | { |
---|
531 | // deleting the Resource |
---|
532 | switch(resource->type) |
---|
533 | { |
---|
534 | #ifndef NO_MODEL |
---|
535 | case OBJ: |
---|
536 | case PRIM: |
---|
537 | delete (Model*)resource->pointer; |
---|
538 | break; |
---|
539 | case MD2: |
---|
540 | delete (MD2Data*)resource->pointer; |
---|
541 | break; |
---|
542 | #endif /* NO_MODEL */ |
---|
543 | #ifndef NO_AUDIO |
---|
544 | case WAV: |
---|
545 | delete (SoundBuffer*)resource->pointer; |
---|
546 | break; |
---|
547 | case OGG: |
---|
548 | delete (OggPlayer*)resource->pointer; |
---|
549 | break; |
---|
550 | #endif /* NO_AUDIO */ |
---|
551 | #ifndef NO_TEXT |
---|
552 | case TTF: |
---|
553 | delete (Font*)resource->pointer; |
---|
554 | break; |
---|
555 | #endif /* NO_TEXT */ |
---|
556 | #ifndef NO_TEXTURES |
---|
557 | case IMAGE: |
---|
558 | delete (Texture*)resource->pointer; |
---|
559 | break; |
---|
560 | #endif /* NO_TEXTURES */ |
---|
561 | #ifndef NO_SHADERS |
---|
562 | case SHADER: |
---|
563 | delete (Shader*)resource->pointer; |
---|
564 | break; |
---|
565 | #endif /* NO_SHADERS */ |
---|
566 | default: |
---|
567 | PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n"); |
---|
568 | return false; |
---|
569 | break; |
---|
570 | } |
---|
571 | // deleting the List Entry: |
---|
572 | PRINTF(4)("Resource %s safely removed.\n", resource->name); |
---|
573 | delete[] resource->name; |
---|
574 | this->resourceList->remove(resource); |
---|
575 | delete resource; |
---|
576 | } |
---|
577 | else |
---|
578 | PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count); |
---|
579 | } |
---|
580 | else |
---|
581 | PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name); |
---|
582 | return true; |
---|
583 | } |
---|
584 | |
---|
585 | |
---|
586 | /** |
---|
587 | * unloads all alocated Memory of Resources with a pririty lower than prio |
---|
588 | * @param prio The priority to delete |
---|
589 | */ |
---|
590 | bool ResourceManager::unloadAllByPriority(ResourcePriority prio) |
---|
591 | { |
---|
592 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
593 | Resource* enumRes = iterator->lastElement(); |
---|
594 | while (enumRes) |
---|
595 | { |
---|
596 | if (enumRes->prio <= prio) |
---|
597 | if (enumRes->count == 0) |
---|
598 | unload(enumRes, prio); |
---|
599 | else |
---|
600 | PRINTF(2)("unable to unload %s because there are still %d references to it\n", |
---|
601 | enumRes->name, enumRes->count); |
---|
602 | //enumRes = resourceList->nextElement(); |
---|
603 | enumRes = iterator->prevElement(); |
---|
604 | } |
---|
605 | delete iterator; |
---|
606 | } |
---|
607 | |
---|
608 | /** |
---|
609 | * Searches for a Resource by some information |
---|
610 | * @param fileName: The name to look for |
---|
611 | * @param type the Type of resource to locate. |
---|
612 | * @param param1: an additional option to parse (see the constuctors for more help) |
---|
613 | * @param param2: an additional option to parse (see the constuctors for more help) |
---|
614 | * @param param3: an additional option to parse (see the constuctors for more help) |
---|
615 | * @returns a Pointer to the Resource if found, NULL otherwise. |
---|
616 | */ |
---|
617 | Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type, |
---|
618 | void* param1, void* param2, void* param3) |
---|
619 | { |
---|
620 | // Resource* enumRes = resourceList->enumerate(); |
---|
621 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
622 | Resource* enumRes = iterator->firstElement(); |
---|
623 | while (enumRes) |
---|
624 | { |
---|
625 | if (enumRes->type == type && !strcmp(fileName, enumRes->name)) |
---|
626 | { |
---|
627 | bool match = false; |
---|
628 | |
---|
629 | switch (type) |
---|
630 | { |
---|
631 | #ifndef NO_MODEL |
---|
632 | case PRIM: |
---|
633 | case OBJ: |
---|
634 | if (!param1) |
---|
635 | { |
---|
636 | if (enumRes->modelSize == 1.0) |
---|
637 | match = true; |
---|
638 | } |
---|
639 | else if (enumRes->modelSize == *(float*)param1) |
---|
640 | match = true; |
---|
641 | break; |
---|
642 | case MD2: |
---|
643 | if (!param1) |
---|
644 | { |
---|
645 | if (enumRes->secFileName == NULL) |
---|
646 | match = true; |
---|
647 | } |
---|
648 | else if (!strcmp(enumRes->secFileName, (const char*)param1)) |
---|
649 | match = true; |
---|
650 | break; |
---|
651 | #endif /* NO_MODEL */ |
---|
652 | #ifndef NO_TEXT |
---|
653 | case TTF: |
---|
654 | if (param1 == NULL) |
---|
655 | { |
---|
656 | if (enumRes->ttfSize == FONT_DEFAULT_RENDER_SIZE) |
---|
657 | match = true; |
---|
658 | } |
---|
659 | else if (enumRes->ttfSize == *(unsigned int*)param1) |
---|
660 | match = true; |
---|
661 | break; |
---|
662 | #endif /* NO_TEXT */ |
---|
663 | #ifndef NO_SHADERS |
---|
664 | case SHADER: |
---|
665 | if (!param1) |
---|
666 | { |
---|
667 | if (enumRes->secFileName == NULL) |
---|
668 | match = true; |
---|
669 | } |
---|
670 | else if (!strcmp(enumRes->secFileName, (const char*)param1)) |
---|
671 | match = true; |
---|
672 | #endif /* NO_SHADERS */ |
---|
673 | default: |
---|
674 | match = true; |
---|
675 | break; |
---|
676 | } |
---|
677 | if (match) |
---|
678 | { |
---|
679 | delete iterator; |
---|
680 | return enumRes; |
---|
681 | } |
---|
682 | } |
---|
683 | enumRes = iterator->nextElement(); |
---|
684 | } |
---|
685 | delete iterator; |
---|
686 | return NULL; |
---|
687 | } |
---|
688 | |
---|
689 | /** |
---|
690 | * Searches for a Resource by Pointer |
---|
691 | * @param pointer the Pointer to search for |
---|
692 | * @returns a Pointer to the Resource if found, NULL otherwise. |
---|
693 | */ |
---|
694 | Resource* ResourceManager::locateResourceByPointer(const void* pointer) |
---|
695 | { |
---|
696 | // Resource* enumRes = resourceList->enumerate(); |
---|
697 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
698 | Resource* enumRes = iterator->firstElement(); |
---|
699 | while (enumRes) |
---|
700 | { |
---|
701 | if (pointer == enumRes->pointer) |
---|
702 | { |
---|
703 | delete iterator; |
---|
704 | return enumRes; |
---|
705 | } |
---|
706 | enumRes = iterator->nextElement(); |
---|
707 | } |
---|
708 | delete iterator; |
---|
709 | return NULL; |
---|
710 | } |
---|
711 | |
---|
712 | /** |
---|
713 | * Checks if it is a Directory |
---|
714 | * @param directoryName the Directory to check for |
---|
715 | * @returns true if it is a directory/symlink false otherwise |
---|
716 | */ |
---|
717 | bool ResourceManager::isDir(const char* directoryName) |
---|
718 | { |
---|
719 | if (directoryName == NULL) |
---|
720 | return false; |
---|
721 | |
---|
722 | char* tmpDirName = NULL; |
---|
723 | struct stat status; |
---|
724 | |
---|
725 | // checking for the termination of the string given. If there is a "/" at the end cut it away |
---|
726 | if (directoryName[strlen(directoryName)-1] == '/' || |
---|
727 | directoryName[strlen(directoryName)-1] == '\\') |
---|
728 | { |
---|
729 | tmpDirName = new char[strlen(directoryName)]; |
---|
730 | strncpy(tmpDirName, directoryName, strlen(directoryName)-1); |
---|
731 | tmpDirName[strlen(directoryName)-1] = '\0'; |
---|
732 | } |
---|
733 | else |
---|
734 | { |
---|
735 | tmpDirName = new char[strlen(directoryName)+1]; |
---|
736 | strcpy(tmpDirName, directoryName); |
---|
737 | } |
---|
738 | |
---|
739 | if(!stat(tmpDirName, &status)) |
---|
740 | { |
---|
741 | if (status.st_mode & (S_IFDIR |
---|
742 | #ifndef __WIN32__ |
---|
743 | | S_IFLNK |
---|
744 | #endif |
---|
745 | )) |
---|
746 | { |
---|
747 | delete[] tmpDirName; |
---|
748 | return true; |
---|
749 | } |
---|
750 | else |
---|
751 | { |
---|
752 | delete[] tmpDirName; |
---|
753 | return false; |
---|
754 | } |
---|
755 | } |
---|
756 | else |
---|
757 | { |
---|
758 | delete[] tmpDirName; |
---|
759 | return false; |
---|
760 | } |
---|
761 | } |
---|
762 | |
---|
763 | /** |
---|
764 | * Checks if the file is either a Regular file or a Symlink |
---|
765 | * @param fileName the File to check for |
---|
766 | * @returns true if it is a regular file/symlink, false otherwise |
---|
767 | */ |
---|
768 | bool ResourceManager::isFile(const char* fileName) |
---|
769 | { |
---|
770 | if (fileName == NULL) |
---|
771 | return false; |
---|
772 | char* tmpFileName = ResourceManager::homeDirCheck(fileName); |
---|
773 | // actually checks the File |
---|
774 | struct stat status; |
---|
775 | if (!stat(tmpFileName, &status)) |
---|
776 | { |
---|
777 | if (status.st_mode & (S_IFREG |
---|
778 | #ifndef __WIN32__ |
---|
779 | | S_IFLNK |
---|
780 | #endif |
---|
781 | )) |
---|
782 | { |
---|
783 | delete[] tmpFileName; |
---|
784 | return true; |
---|
785 | } |
---|
786 | else |
---|
787 | { |
---|
788 | delete[] tmpFileName; |
---|
789 | return false; |
---|
790 | } |
---|
791 | } |
---|
792 | else |
---|
793 | { |
---|
794 | delete[] tmpFileName; |
---|
795 | return false; |
---|
796 | } |
---|
797 | } |
---|
798 | |
---|
799 | /** |
---|
800 | * touches a File on the disk (thereby creating it) |
---|
801 | * @param fileName The file to touch |
---|
802 | */ |
---|
803 | bool ResourceManager::touchFile(const char* fileName) |
---|
804 | { |
---|
805 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
806 | if (tmpName == NULL) |
---|
807 | return false; |
---|
808 | FILE* stream; |
---|
809 | if( (stream = fopen (tmpName, "w")) == NULL) |
---|
810 | { |
---|
811 | PRINTF(1)("could not open %s fro writing\n", fileName); |
---|
812 | delete[] tmpName; |
---|
813 | return false; |
---|
814 | } |
---|
815 | fclose(stream); |
---|
816 | |
---|
817 | delete[] tmpName; |
---|
818 | } |
---|
819 | |
---|
820 | /** |
---|
821 | * deletes a File from disk |
---|
822 | * @param fileName the File to delete |
---|
823 | */ |
---|
824 | bool ResourceManager::deleteFile(const char* fileName) |
---|
825 | { |
---|
826 | if (fileName == NULL) |
---|
827 | return false; |
---|
828 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
829 | unlink(tmpName); |
---|
830 | delete[] tmpName; |
---|
831 | } |
---|
832 | |
---|
833 | /** |
---|
834 | * @param name the Name of the file to check |
---|
835 | * @returns The name of the file, including the HomeDir |
---|
836 | * IMPORTANT: this has to be deleted from the outside |
---|
837 | */ |
---|
838 | char* ResourceManager::homeDirCheck(const char* name) |
---|
839 | { |
---|
840 | if (name == NULL) |
---|
841 | return NULL; |
---|
842 | char* retName; |
---|
843 | if (!strncmp(name, "~/", 2)) |
---|
844 | { |
---|
845 | char tmpFileName[500]; |
---|
846 | #ifdef __WIN32__ |
---|
847 | strcpy(tmpFileName, getenv("USERPROFILE")); |
---|
848 | #else |
---|
849 | strcpy(tmpFileName, getenv("HOME")); |
---|
850 | #endif |
---|
851 | retName = new char[strlen(tmpFileName)+strlen(name)]; |
---|
852 | sprintf(retName, "%s%s", tmpFileName, name+1); |
---|
853 | } |
---|
854 | else |
---|
855 | { |
---|
856 | retName = new char[strlen(name)+1]; |
---|
857 | strcpy(retName, name); |
---|
858 | } |
---|
859 | return retName; |
---|
860 | } |
---|
861 | |
---|
862 | /** |
---|
863 | * @param fileName the Name of the File to check |
---|
864 | * @returns The full name of the file, including the DataDir, and NULL if the file does not exist |
---|
865 | * !!IMPORTANT: this has to be deleted from the outside!! |
---|
866 | */ |
---|
867 | char* ResourceManager::getFullName(const char* fileName) |
---|
868 | { |
---|
869 | if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL) |
---|
870 | return NULL; |
---|
871 | |
---|
872 | char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir()) |
---|
873 | + strlen(fileName) + 1]; |
---|
874 | sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); |
---|
875 | if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName)) |
---|
876 | return retName; |
---|
877 | else |
---|
878 | { |
---|
879 | delete[] retName; |
---|
880 | return NULL; |
---|
881 | } |
---|
882 | } |
---|
883 | |
---|
884 | |
---|
885 | /** |
---|
886 | * checks wether a file is in the DataDir. |
---|
887 | * @param fileName the File to check if it is in the Data-Dir structure. |
---|
888 | * @returns true if the file exists, false otherwise |
---|
889 | */ |
---|
890 | bool ResourceManager::isInDataDir(const char* fileName) |
---|
891 | { |
---|
892 | if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL) |
---|
893 | return false; |
---|
894 | |
---|
895 | bool retVal = false; |
---|
896 | char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir()) |
---|
897 | + strlen(fileName) + 1]; |
---|
898 | sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); |
---|
899 | |
---|
900 | if (ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile)) |
---|
901 | retVal = true; |
---|
902 | else |
---|
903 | retVal = false; |
---|
904 | delete[] checkFile; |
---|
905 | return retVal; |
---|
906 | } |
---|
907 | |
---|
908 | |
---|
909 | /** |
---|
910 | * outputs debug information about the ResourceManager |
---|
911 | */ |
---|
912 | void ResourceManager::debug() const |
---|
913 | { |
---|
914 | PRINT(0)("=RM===================================\n"); |
---|
915 | PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n"); |
---|
916 | PRINT(0)("======================================\n"); |
---|
917 | // if it is not initialized |
---|
918 | PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef); |
---|
919 | PRINT(0)(" Data-Directory is: %s\n", this->dataDir); |
---|
920 | PRINT(0)(" List of Image-Directories: "); |
---|
921 | tIterator<char>* tmpIt = imageDirs->getIterator(); |
---|
922 | char* tmpDir = tmpIt->firstElement(); |
---|
923 | while(tmpDir) |
---|
924 | { |
---|
925 | PRINT(0)("%s ",tmpDir); |
---|
926 | tmpDir = tmpIt->nextElement(); |
---|
927 | } |
---|
928 | delete tmpIt; |
---|
929 | PRINT(0)("\n"); |
---|
930 | |
---|
931 | PRINT(0)("List of all stored Resources:\n"); |
---|
932 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
933 | Resource* enumRes = iterator->firstElement(); |
---|
934 | while (enumRes) |
---|
935 | { |
---|
936 | PRINT(0)("-----------------------------------------\n"); |
---|
937 | PRINT(0)("Name: %s; References: %d; Type: %s ", enumRes->name, enumRes->count, ResourceManager::ResourceTypeToChar(enumRes->type)); |
---|
938 | |
---|
939 | PRINT(0)("gets deleted at "); |
---|
940 | switch(enumRes->prio) |
---|
941 | { |
---|
942 | default: |
---|
943 | case RP_NO: |
---|
944 | PRINT(0)("first posibility (0)\n"); |
---|
945 | break; |
---|
946 | case RP_LEVEL: |
---|
947 | PRINT(0)("the end of the Level (1)\n"); |
---|
948 | break; |
---|
949 | case RP_CAMPAIGN: |
---|
950 | PRINT(0)("the end of the campaign (2)\n"); |
---|
951 | break; |
---|
952 | case RP_GAME: |
---|
953 | PRINT(0)("when leaving the game (3)\n"); |
---|
954 | break; |
---|
955 | } |
---|
956 | enumRes = iterator->nextElement(); |
---|
957 | } |
---|
958 | delete iterator; |
---|
959 | |
---|
960 | |
---|
961 | |
---|
962 | PRINT(0)("==================================RM==\n"); |
---|
963 | } |
---|
964 | |
---|
965 | |
---|
966 | /** |
---|
967 | * converts a ResourceType into the corresponding String |
---|
968 | * @param type the ResourceType to translate |
---|
969 | * @returns the converted String. |
---|
970 | */ |
---|
971 | const char* ResourceManager::ResourceTypeToChar(ResourceType type) |
---|
972 | { |
---|
973 | switch (type) |
---|
974 | { |
---|
975 | #ifndef NO_MODEL |
---|
976 | case OBJ: |
---|
977 | return "ObjectModel"; |
---|
978 | break; |
---|
979 | case PRIM: |
---|
980 | return "PrimitiveModel"; |
---|
981 | break; |
---|
982 | case MD2: |
---|
983 | return "MD2-Data"; |
---|
984 | break; |
---|
985 | #endif |
---|
986 | #ifndef NO_TEXTURES |
---|
987 | case IMAGE: |
---|
988 | return "ImageFile (Texture)"; |
---|
989 | break; |
---|
990 | #endif |
---|
991 | #ifndef NO_AUDIO |
---|
992 | case WAV: |
---|
993 | return "SoundFile"; |
---|
994 | break; |
---|
995 | case OGG: |
---|
996 | return "MusicFile"; |
---|
997 | break; |
---|
998 | #endif |
---|
999 | #ifndef NO_TEXT |
---|
1000 | case TTF: |
---|
1001 | return "Font (TTF)"; |
---|
1002 | break; |
---|
1003 | #endif |
---|
1004 | #ifndef NO_SHADERS |
---|
1005 | case SHADER: |
---|
1006 | return "Shader"; |
---|
1007 | break; |
---|
1008 | #endif |
---|
1009 | default: |
---|
1010 | return "unknown Format"; |
---|
1011 | break; |
---|
1012 | } |
---|
1013 | } |
---|