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