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