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_buffer.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 | /** |
---|
610 | * Searches for a Resource by some information |
---|
611 | * @param fileName: The name to look for |
---|
612 | * @param type the Type of resource to locate. |
---|
613 | * @param param1: an additional option to parse (see the constuctors for more help) |
---|
614 | * @param param2: an additional option to parse (see the constuctors for more help) |
---|
615 | * @param param3: an additional option to parse (see the constuctors for more help) |
---|
616 | * @returns a Pointer to the Resource if found, NULL otherwise. |
---|
617 | */ |
---|
618 | Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type, |
---|
619 | void* param1, void* param2, void* param3) const |
---|
620 | { |
---|
621 | // Resource* enumRes = resourceList->enumerate(); |
---|
622 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
623 | Resource* enumRes = iterator->firstElement(); |
---|
624 | while (enumRes) |
---|
625 | { |
---|
626 | if (enumRes->type == type && !strcmp(fileName, enumRes->name)) |
---|
627 | { |
---|
628 | bool match = false; |
---|
629 | |
---|
630 | switch (type) |
---|
631 | { |
---|
632 | #ifndef NO_MODEL |
---|
633 | case PRIM: |
---|
634 | case OBJ: |
---|
635 | if (!param1) |
---|
636 | { |
---|
637 | if (enumRes->modelSize == 1.0) |
---|
638 | match = true; |
---|
639 | } |
---|
640 | else if (enumRes->modelSize == *(float*)param1) |
---|
641 | match = true; |
---|
642 | break; |
---|
643 | case MD2: |
---|
644 | if (!param1) |
---|
645 | { |
---|
646 | if (enumRes->secFileName == NULL) |
---|
647 | match = true; |
---|
648 | } |
---|
649 | else if (!strcmp(enumRes->secFileName, (const char*)param1)) |
---|
650 | match = true; |
---|
651 | break; |
---|
652 | #endif /* NO_MODEL */ |
---|
653 | #ifndef NO_TEXT |
---|
654 | case TTF: |
---|
655 | if (param1 == NULL) |
---|
656 | { |
---|
657 | if (enumRes->ttfSize == FONT_DEFAULT_RENDER_SIZE) |
---|
658 | match = true; |
---|
659 | } |
---|
660 | else if (enumRes->ttfSize == *(unsigned int*)param1) |
---|
661 | match = true; |
---|
662 | break; |
---|
663 | #endif /* NO_TEXT */ |
---|
664 | #ifndef NO_SHADERS |
---|
665 | case SHADER: |
---|
666 | if (!param1) |
---|
667 | { |
---|
668 | if (enumRes->secFileName == NULL) |
---|
669 | match = true; |
---|
670 | } |
---|
671 | else if (!strcmp(enumRes->secFileName, (const char*)param1)) |
---|
672 | match = true; |
---|
673 | #endif /* NO_SHADERS */ |
---|
674 | default: |
---|
675 | match = true; |
---|
676 | break; |
---|
677 | } |
---|
678 | if (match) |
---|
679 | { |
---|
680 | delete iterator; |
---|
681 | return enumRes; |
---|
682 | } |
---|
683 | } |
---|
684 | enumRes = iterator->nextElement(); |
---|
685 | } |
---|
686 | delete iterator; |
---|
687 | return NULL; |
---|
688 | } |
---|
689 | |
---|
690 | /** |
---|
691 | * Searches for a Resource by Pointer |
---|
692 | * @param pointer the Pointer to search for |
---|
693 | * @returns a Pointer to the Resource if found, NULL otherwise. |
---|
694 | */ |
---|
695 | Resource* ResourceManager::locateResourceByPointer(const void* pointer) const |
---|
696 | { |
---|
697 | // Resource* enumRes = resourceList->enumerate(); |
---|
698 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
699 | Resource* enumRes = iterator->firstElement(); |
---|
700 | while (enumRes) |
---|
701 | { |
---|
702 | if (pointer == enumRes->pointer) |
---|
703 | { |
---|
704 | delete iterator; |
---|
705 | return enumRes; |
---|
706 | } |
---|
707 | enumRes = iterator->nextElement(); |
---|
708 | } |
---|
709 | delete iterator; |
---|
710 | return NULL; |
---|
711 | } |
---|
712 | |
---|
713 | /** |
---|
714 | * Checks if it is a Directory |
---|
715 | * @param directoryName the Directory to check for |
---|
716 | * @returns true if it is a directory/symlink false otherwise |
---|
717 | */ |
---|
718 | bool ResourceManager::isDir(const char* directoryName) |
---|
719 | { |
---|
720 | if (directoryName == NULL) |
---|
721 | return false; |
---|
722 | |
---|
723 | char* tmpDirName = NULL; |
---|
724 | struct stat status; |
---|
725 | |
---|
726 | // checking for the termination of the string given. If there is a "/" at the end cut it away |
---|
727 | if (directoryName[strlen(directoryName)-1] == '/' || |
---|
728 | directoryName[strlen(directoryName)-1] == '\\') |
---|
729 | { |
---|
730 | tmpDirName = new char[strlen(directoryName)]; |
---|
731 | strncpy(tmpDirName, directoryName, strlen(directoryName)-1); |
---|
732 | tmpDirName[strlen(directoryName)-1] = '\0'; |
---|
733 | } |
---|
734 | else |
---|
735 | { |
---|
736 | tmpDirName = new char[strlen(directoryName)+1]; |
---|
737 | strcpy(tmpDirName, directoryName); |
---|
738 | } |
---|
739 | |
---|
740 | if(!stat(tmpDirName, &status)) |
---|
741 | { |
---|
742 | if (status.st_mode & (S_IFDIR |
---|
743 | #ifndef __WIN32__ |
---|
744 | | S_IFLNK |
---|
745 | #endif |
---|
746 | )) |
---|
747 | { |
---|
748 | delete[] tmpDirName; |
---|
749 | return true; |
---|
750 | } |
---|
751 | else |
---|
752 | { |
---|
753 | delete[] tmpDirName; |
---|
754 | return false; |
---|
755 | } |
---|
756 | } |
---|
757 | else |
---|
758 | { |
---|
759 | delete[] tmpDirName; |
---|
760 | return false; |
---|
761 | } |
---|
762 | } |
---|
763 | |
---|
764 | /** |
---|
765 | * Checks if the file is either a Regular file or a Symlink |
---|
766 | * @param fileName the File to check for |
---|
767 | * @returns true if it is a regular file/symlink, false otherwise |
---|
768 | */ |
---|
769 | bool ResourceManager::isFile(const char* fileName) |
---|
770 | { |
---|
771 | if (fileName == NULL) |
---|
772 | return false; |
---|
773 | char* tmpFileName = ResourceManager::homeDirCheck(fileName); |
---|
774 | // actually checks the File |
---|
775 | struct stat status; |
---|
776 | if (!stat(tmpFileName, &status)) |
---|
777 | { |
---|
778 | if (status.st_mode & (S_IFREG |
---|
779 | #ifndef __WIN32__ |
---|
780 | | S_IFLNK |
---|
781 | #endif |
---|
782 | )) |
---|
783 | { |
---|
784 | delete[] tmpFileName; |
---|
785 | return true; |
---|
786 | } |
---|
787 | else |
---|
788 | { |
---|
789 | delete[] tmpFileName; |
---|
790 | return false; |
---|
791 | } |
---|
792 | } |
---|
793 | else |
---|
794 | { |
---|
795 | delete[] tmpFileName; |
---|
796 | return false; |
---|
797 | } |
---|
798 | } |
---|
799 | |
---|
800 | /** |
---|
801 | * touches a File on the disk (thereby creating it) |
---|
802 | * @param fileName The file to touch |
---|
803 | */ |
---|
804 | bool ResourceManager::touchFile(const char* fileName) |
---|
805 | { |
---|
806 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
807 | if (tmpName == NULL) |
---|
808 | return false; |
---|
809 | FILE* stream; |
---|
810 | if( (stream = fopen (tmpName, "w")) == NULL) |
---|
811 | { |
---|
812 | PRINTF(1)("could not open %s fro writing\n", fileName); |
---|
813 | delete[] tmpName; |
---|
814 | return false; |
---|
815 | } |
---|
816 | fclose(stream); |
---|
817 | |
---|
818 | delete[] tmpName; |
---|
819 | } |
---|
820 | |
---|
821 | /** |
---|
822 | * deletes a File from disk |
---|
823 | * @param fileName the File to delete |
---|
824 | */ |
---|
825 | bool ResourceManager::deleteFile(const char* fileName) |
---|
826 | { |
---|
827 | if (fileName == NULL) |
---|
828 | return false; |
---|
829 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
830 | unlink(tmpName); |
---|
831 | delete[] tmpName; |
---|
832 | } |
---|
833 | |
---|
834 | /** |
---|
835 | * @param name the Name of the file to check |
---|
836 | * @returns The name of the file, including the HomeDir |
---|
837 | * IMPORTANT: this has to be deleted from the outside |
---|
838 | */ |
---|
839 | char* ResourceManager::homeDirCheck(const char* name) |
---|
840 | { |
---|
841 | if (name == NULL) |
---|
842 | return NULL; |
---|
843 | char* retName; |
---|
844 | if (!strncmp(name, "~/", 2)) |
---|
845 | { |
---|
846 | char tmpFileName[500]; |
---|
847 | #ifdef __WIN32__ |
---|
848 | strcpy(tmpFileName, getenv("USERPROFILE")); |
---|
849 | #else |
---|
850 | strcpy(tmpFileName, getenv("HOME")); |
---|
851 | #endif |
---|
852 | retName = new char[strlen(tmpFileName)+strlen(name)]; |
---|
853 | sprintf(retName, "%s%s", tmpFileName, name+1); |
---|
854 | } |
---|
855 | else |
---|
856 | { |
---|
857 | retName = new char[strlen(name)+1]; |
---|
858 | strcpy(retName, name); |
---|
859 | } |
---|
860 | return retName; |
---|
861 | } |
---|
862 | |
---|
863 | /** |
---|
864 | * @param fileName the Name of the File to check |
---|
865 | * @returns The full name of the file, including the DataDir, and NULL if the file does not exist |
---|
866 | * !!IMPORTANT: this has to be deleted from the outside!! |
---|
867 | */ |
---|
868 | char* ResourceManager::getFullName(const char* fileName) |
---|
869 | { |
---|
870 | if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL) |
---|
871 | return NULL; |
---|
872 | |
---|
873 | char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir()) |
---|
874 | + strlen(fileName) + 1]; |
---|
875 | sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); |
---|
876 | if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName)) |
---|
877 | return retName; |
---|
878 | else |
---|
879 | { |
---|
880 | delete[] retName; |
---|
881 | return NULL; |
---|
882 | } |
---|
883 | } |
---|
884 | |
---|
885 | |
---|
886 | /** |
---|
887 | * checks wether a file is in the DataDir. |
---|
888 | * @param fileName the File to check if it is in the Data-Dir structure. |
---|
889 | * @returns true if the file exists, false otherwise |
---|
890 | */ |
---|
891 | bool ResourceManager::isInDataDir(const char* fileName) |
---|
892 | { |
---|
893 | if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL) |
---|
894 | return false; |
---|
895 | |
---|
896 | bool retVal = false; |
---|
897 | char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir()) |
---|
898 | + strlen(fileName) + 1]; |
---|
899 | sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); |
---|
900 | |
---|
901 | if (ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile)) |
---|
902 | retVal = true; |
---|
903 | else |
---|
904 | retVal = false; |
---|
905 | delete[] checkFile; |
---|
906 | return retVal; |
---|
907 | } |
---|
908 | |
---|
909 | |
---|
910 | /** |
---|
911 | * outputs debug information about the ResourceManager |
---|
912 | */ |
---|
913 | void ResourceManager::debug() const |
---|
914 | { |
---|
915 | PRINT(0)("=RM===================================\n"); |
---|
916 | PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n"); |
---|
917 | PRINT(0)("======================================\n"); |
---|
918 | // if it is not initialized |
---|
919 | PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef); |
---|
920 | PRINT(0)(" Data-Directory is: %s\n", this->dataDir); |
---|
921 | PRINT(0)(" List of Image-Directories: "); |
---|
922 | tIterator<char>* tmpIt = imageDirs->getIterator(); |
---|
923 | char* tmpDir = tmpIt->firstElement(); |
---|
924 | while(tmpDir) |
---|
925 | { |
---|
926 | PRINT(0)("%s ",tmpDir); |
---|
927 | tmpDir = tmpIt->nextElement(); |
---|
928 | } |
---|
929 | delete tmpIt; |
---|
930 | PRINT(0)("\n"); |
---|
931 | |
---|
932 | PRINT(0)("List of all stored Resources:\n"); |
---|
933 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
934 | Resource* enumRes = iterator->firstElement(); |
---|
935 | while (enumRes) |
---|
936 | { |
---|
937 | PRINT(0)("-----------------------------------------\n"); |
---|
938 | PRINT(0)("Name: %s; References: %d; Type: %s ", enumRes->name, enumRes->count, ResourceManager::ResourceTypeToChar(enumRes->type)); |
---|
939 | |
---|
940 | PRINT(0)("gets deleted at "); |
---|
941 | switch(enumRes->prio) |
---|
942 | { |
---|
943 | default: |
---|
944 | case RP_NO: |
---|
945 | PRINT(0)("first posibility (0)\n"); |
---|
946 | break; |
---|
947 | case RP_LEVEL: |
---|
948 | PRINT(0)("the end of the Level (1)\n"); |
---|
949 | break; |
---|
950 | case RP_CAMPAIGN: |
---|
951 | PRINT(0)("the end of the campaign (2)\n"); |
---|
952 | break; |
---|
953 | case RP_GAME: |
---|
954 | PRINT(0)("when leaving the game (3)\n"); |
---|
955 | break; |
---|
956 | } |
---|
957 | enumRes = iterator->nextElement(); |
---|
958 | } |
---|
959 | delete iterator; |
---|
960 | |
---|
961 | |
---|
962 | |
---|
963 | PRINT(0)("==================================RM==\n"); |
---|
964 | } |
---|
965 | |
---|
966 | |
---|
967 | /** |
---|
968 | * converts a ResourceType into the corresponding String |
---|
969 | * @param type the ResourceType to translate |
---|
970 | * @returns the converted String. |
---|
971 | */ |
---|
972 | const char* ResourceManager::ResourceTypeToChar(ResourceType type) |
---|
973 | { |
---|
974 | switch (type) |
---|
975 | { |
---|
976 | #ifndef NO_MODEL |
---|
977 | case OBJ: |
---|
978 | return "ObjectModel"; |
---|
979 | break; |
---|
980 | case PRIM: |
---|
981 | return "PrimitiveModel"; |
---|
982 | break; |
---|
983 | case MD2: |
---|
984 | return "MD2-Data"; |
---|
985 | break; |
---|
986 | #endif |
---|
987 | #ifndef NO_TEXTURES |
---|
988 | case IMAGE: |
---|
989 | return "ImageFile (Texture)"; |
---|
990 | break; |
---|
991 | #endif |
---|
992 | #ifndef NO_AUDIO |
---|
993 | case WAV: |
---|
994 | return "SoundFile"; |
---|
995 | break; |
---|
996 | case OGG: |
---|
997 | return "MusicFile"; |
---|
998 | break; |
---|
999 | #endif |
---|
1000 | #ifndef NO_TEXT |
---|
1001 | case TTF: |
---|
1002 | return "Font (TTF)"; |
---|
1003 | break; |
---|
1004 | #endif |
---|
1005 | #ifndef NO_SHADERS |
---|
1006 | case SHADER: |
---|
1007 | return "Shader"; |
---|
1008 | break; |
---|
1009 | #endif |
---|
1010 | default: |
---|
1011 | return "unknown Format"; |
---|
1012 | break; |
---|
1013 | } |
---|
1014 | } |
---|