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 | // tmpResource->pointer = new MD2Model(fullName, tmpResource->secFileName); |
---|
393 | |
---|
394 | } |
---|
395 | break; |
---|
396 | #endif /* NO_MODEL */ |
---|
397 | #ifndef NO_TEXT |
---|
398 | case TTF: |
---|
399 | if (param1 != NULL) |
---|
400 | tmpResource->ttfSize = *(unsigned int*)param1; |
---|
401 | else |
---|
402 | tmpResource->ttfSize = FONT_DEFAULT_RENDER_SIZE; |
---|
403 | |
---|
404 | if(isFile(fullName)) |
---|
405 | tmpResource->pointer = new Font(fullName, tmpResource->ttfSize); |
---|
406 | else |
---|
407 | PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir); |
---|
408 | break; |
---|
409 | #endif /* NO_TEXT */ |
---|
410 | #ifndef NO_AUDIO |
---|
411 | case WAV: |
---|
412 | if(isFile(fullName)) |
---|
413 | tmpResource->pointer = new SoundBuffer(fullName); |
---|
414 | break; |
---|
415 | case OGG: |
---|
416 | if (isFile(fullName)) |
---|
417 | tmpResource->pointer = new OggPlayer(fullName); |
---|
418 | break; |
---|
419 | #endif /* NO_AUDIO */ |
---|
420 | #ifndef NO_TEXTURES |
---|
421 | case IMAGE: |
---|
422 | if(isFile(fullName)) |
---|
423 | { |
---|
424 | PRINTF(4)("Image %s resides to %s\n", fileName, fullName); |
---|
425 | tmpResource->pointer = new Texture(fullName); |
---|
426 | } |
---|
427 | else |
---|
428 | { |
---|
429 | char* tmpDir; |
---|
430 | tIterator<char>* iterator = imageDirs->getIterator(); |
---|
431 | tmpDir = iterator->firstElement(); |
---|
432 | while(tmpDir) |
---|
433 | { |
---|
434 | char* imgName = new char[strlen(tmpDir)+strlen(fileName)+1]; |
---|
435 | sprintf(imgName, "%s%s", tmpDir, fileName); |
---|
436 | if(isFile(imgName)) |
---|
437 | { |
---|
438 | PRINTF(4)("Image %s resides to %s\n", fileName, imgName); |
---|
439 | tmpResource->pointer = new Texture(imgName); |
---|
440 | delete[] imgName; |
---|
441 | break; |
---|
442 | } |
---|
443 | delete[] imgName; |
---|
444 | tmpDir = iterator->nextElement(); |
---|
445 | } |
---|
446 | delete iterator; |
---|
447 | } |
---|
448 | if(!tmpResource) |
---|
449 | PRINTF(2)("!!Image %s not Found!!\n", fileName); |
---|
450 | break; |
---|
451 | #endif /* NO_TEXTURES */ |
---|
452 | #ifndef NO_SHADERS |
---|
453 | case SHADER: |
---|
454 | if(ResourceManager::isFile(fullName)) |
---|
455 | { |
---|
456 | if (param1 != NULL) |
---|
457 | { |
---|
458 | char* secFullName = ResourceManager::getFullName((const char*)param1); |
---|
459 | if (ResourceManager::isFile(secFullName)) |
---|
460 | { |
---|
461 | tmpResource->secFileName = new char[strlen((const char*)param1)+1]; |
---|
462 | strcpy(tmpResource->secFileName, (const char*) param1); |
---|
463 | tmpResource->pointer = new Shader(fullName, secFullName); |
---|
464 | } |
---|
465 | delete[] secFullName; |
---|
466 | } |
---|
467 | else |
---|
468 | { |
---|
469 | tmpResource->secFileName = NULL; |
---|
470 | tmpResource->pointer = new Shader(fullName, NULL); |
---|
471 | } |
---|
472 | } |
---|
473 | break; |
---|
474 | #endif /* NO_SHADERS */ |
---|
475 | default: |
---|
476 | tmpResource->pointer = NULL; |
---|
477 | 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); |
---|
478 | break; |
---|
479 | } |
---|
480 | if (tmpResource->pointer != NULL) |
---|
481 | this->resourceList->add(tmpResource); |
---|
482 | delete[] fullName; |
---|
483 | } |
---|
484 | if (tmpResource->pointer != NULL) |
---|
485 | return tmpResource->pointer; |
---|
486 | else |
---|
487 | { |
---|
488 | PRINTF(2)("Resource %s could not be loaded\n", fileName); |
---|
489 | delete[] tmpResource->name; |
---|
490 | delete tmpResource; |
---|
491 | return NULL; |
---|
492 | } |
---|
493 | } |
---|
494 | |
---|
495 | /** |
---|
496 | * unloads a Resource |
---|
497 | * @param pointer: The pointer to free |
---|
498 | * @param prio: the PriorityLevel to unload this resource |
---|
499 | * @returns true if successful (pointer found, and deleted), false otherwise |
---|
500 | */ |
---|
501 | bool ResourceManager::unload(void* pointer, ResourcePriority prio) |
---|
502 | { |
---|
503 | if (pointer == NULL) |
---|
504 | return false; |
---|
505 | // if pointer is existent. and only one resource of this type exists. |
---|
506 | Resource* tmpResource = this->locateResourceByPointer(pointer); |
---|
507 | if (tmpResource != NULL) |
---|
508 | return unload(tmpResource, prio); |
---|
509 | else |
---|
510 | { |
---|
511 | PRINTF(2)("Resource not Found %p\n", pointer); |
---|
512 | return false; |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | /** |
---|
517 | * unloads a Resource |
---|
518 | * @param resource: The resource to unloade |
---|
519 | * @param prio the PriorityLevel to unload this resource |
---|
520 | * @returns true on success, false otherwise. |
---|
521 | */ |
---|
522 | bool ResourceManager::unload(Resource* resource, ResourcePriority prio) |
---|
523 | { |
---|
524 | if (resource == NULL) |
---|
525 | return false; |
---|
526 | if (resource->count > 0) |
---|
527 | resource->count--; |
---|
528 | |
---|
529 | if (resource->prio <= prio) |
---|
530 | { |
---|
531 | if (resource->count == 0) |
---|
532 | { |
---|
533 | // deleting the Resource |
---|
534 | switch(resource->type) |
---|
535 | { |
---|
536 | #ifndef NO_MODEL |
---|
537 | case OBJ: |
---|
538 | case PRIM: |
---|
539 | delete (Model*)resource->pointer; |
---|
540 | break; |
---|
541 | case MD2: |
---|
542 | delete (MD2Data*)resource->pointer; |
---|
543 | break; |
---|
544 | #endif /* NO_MODEL */ |
---|
545 | #ifndef NO_AUDIO |
---|
546 | case WAV: |
---|
547 | delete (SoundBuffer*)resource->pointer; |
---|
548 | break; |
---|
549 | case OGG: |
---|
550 | delete (OggPlayer*)resource->pointer; |
---|
551 | break; |
---|
552 | #endif /* NO_AUDIO */ |
---|
553 | #ifndef NO_TEXT |
---|
554 | case TTF: |
---|
555 | delete (Font*)resource->pointer; |
---|
556 | break; |
---|
557 | #endif /* NO_TEXT */ |
---|
558 | #ifndef NO_TEXTURES |
---|
559 | case IMAGE: |
---|
560 | delete (Texture*)resource->pointer; |
---|
561 | break; |
---|
562 | #endif /* NO_TEXTURES */ |
---|
563 | #ifndef NO_SHADERS |
---|
564 | case SHADER: |
---|
565 | delete (Shader*)resource->pointer; |
---|
566 | break; |
---|
567 | #endif /* NO_SHADERS */ |
---|
568 | default: |
---|
569 | PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n"); |
---|
570 | return false; |
---|
571 | break; |
---|
572 | } |
---|
573 | // deleting the List Entry: |
---|
574 | PRINTF(4)("Resource %s safely removed.\n", resource->name); |
---|
575 | delete[] resource->name; |
---|
576 | this->resourceList->remove(resource); |
---|
577 | delete resource; |
---|
578 | } |
---|
579 | else |
---|
580 | PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count); |
---|
581 | } |
---|
582 | else |
---|
583 | PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name); |
---|
584 | return true; |
---|
585 | } |
---|
586 | |
---|
587 | |
---|
588 | /** |
---|
589 | * unloads all alocated Memory of Resources with a pririty lower than prio |
---|
590 | * @param prio The priority to delete |
---|
591 | */ |
---|
592 | bool ResourceManager::unloadAllByPriority(ResourcePriority prio) |
---|
593 | { |
---|
594 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
595 | Resource* enumRes = iterator->lastElement(); |
---|
596 | while (enumRes) |
---|
597 | { |
---|
598 | if (enumRes->prio <= prio) |
---|
599 | if (enumRes->count == 0) |
---|
600 | unload(enumRes, prio); |
---|
601 | else |
---|
602 | PRINTF(2)("unable to unload %s because there are still %d references to it\n", |
---|
603 | enumRes->name, enumRes->count); |
---|
604 | //enumRes = resourceList->nextElement(); |
---|
605 | enumRes = iterator->prevElement(); |
---|
606 | } |
---|
607 | delete iterator; |
---|
608 | } |
---|
609 | |
---|
610 | |
---|
611 | /** |
---|
612 | * Searches for a Resource by some information |
---|
613 | * @param fileName: The name to look for |
---|
614 | * @param type the Type of resource to locate. |
---|
615 | * @param param1: an additional option to parse (see the constuctors for more help) |
---|
616 | * @param param2: an additional option to parse (see the constuctors for more help) |
---|
617 | * @param param3: an additional option to parse (see the constuctors for more help) |
---|
618 | * @returns a Pointer to the Resource if found, NULL otherwise. |
---|
619 | */ |
---|
620 | Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type, |
---|
621 | void* param1, void* param2, void* param3) const |
---|
622 | { |
---|
623 | // Resource* enumRes = resourceList->enumerate(); |
---|
624 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
625 | Resource* enumRes = iterator->firstElement(); |
---|
626 | while (enumRes) |
---|
627 | { |
---|
628 | if (enumRes->type == type && !strcmp(fileName, enumRes->name)) |
---|
629 | { |
---|
630 | bool match = false; |
---|
631 | |
---|
632 | switch (type) |
---|
633 | { |
---|
634 | #ifndef NO_MODEL |
---|
635 | case PRIM: |
---|
636 | case OBJ: |
---|
637 | if (!param1) |
---|
638 | { |
---|
639 | if (enumRes->modelSize == 1.0) |
---|
640 | match = true; |
---|
641 | } |
---|
642 | else if (enumRes->modelSize == *(float*)param1) |
---|
643 | match = true; |
---|
644 | break; |
---|
645 | case MD2: |
---|
646 | if (!param1) |
---|
647 | { |
---|
648 | if (enumRes->secFileName == NULL) |
---|
649 | match = true; |
---|
650 | } |
---|
651 | else if (!strcmp(enumRes->secFileName, (const char*)param1)) |
---|
652 | match = true; |
---|
653 | break; |
---|
654 | #endif /* NO_MODEL */ |
---|
655 | #ifndef NO_TEXT |
---|
656 | case TTF: |
---|
657 | if (param1 == NULL) |
---|
658 | { |
---|
659 | if (enumRes->ttfSize == FONT_DEFAULT_RENDER_SIZE) |
---|
660 | match = true; |
---|
661 | } |
---|
662 | else if (enumRes->ttfSize == *(unsigned int*)param1) |
---|
663 | match = true; |
---|
664 | break; |
---|
665 | #endif /* NO_TEXT */ |
---|
666 | #ifndef NO_SHADERS |
---|
667 | case SHADER: |
---|
668 | if (!param1) |
---|
669 | { |
---|
670 | if (enumRes->secFileName == NULL) |
---|
671 | match = true; |
---|
672 | } |
---|
673 | else if (!strcmp(enumRes->secFileName, (const char*)param1)) |
---|
674 | match = true; |
---|
675 | #endif /* NO_SHADERS */ |
---|
676 | default: |
---|
677 | match = true; |
---|
678 | break; |
---|
679 | } |
---|
680 | if (match) |
---|
681 | { |
---|
682 | delete iterator; |
---|
683 | return enumRes; |
---|
684 | } |
---|
685 | } |
---|
686 | enumRes = iterator->nextElement(); |
---|
687 | } |
---|
688 | delete iterator; |
---|
689 | return NULL; |
---|
690 | } |
---|
691 | |
---|
692 | /** |
---|
693 | * Searches for a Resource by Pointer |
---|
694 | * @param pointer the Pointer to search for |
---|
695 | * @returns a Pointer to the Resource if found, NULL otherwise. |
---|
696 | */ |
---|
697 | Resource* ResourceManager::locateResourceByPointer(const void* pointer) const |
---|
698 | { |
---|
699 | // Resource* enumRes = resourceList->enumerate(); |
---|
700 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
701 | Resource* enumRes = iterator->firstElement(); |
---|
702 | while (enumRes) |
---|
703 | { |
---|
704 | if (pointer == enumRes->pointer) |
---|
705 | { |
---|
706 | delete iterator; |
---|
707 | return enumRes; |
---|
708 | } |
---|
709 | enumRes = iterator->nextElement(); |
---|
710 | } |
---|
711 | delete iterator; |
---|
712 | return NULL; |
---|
713 | } |
---|
714 | |
---|
715 | /** |
---|
716 | * Checks if it is a Directory |
---|
717 | * @param directoryName the Directory to check for |
---|
718 | * @returns true if it is a directory/symlink false otherwise |
---|
719 | */ |
---|
720 | bool ResourceManager::isDir(const char* directoryName) |
---|
721 | { |
---|
722 | if (directoryName == NULL) |
---|
723 | return false; |
---|
724 | |
---|
725 | char* tmpDirName = NULL; |
---|
726 | struct stat status; |
---|
727 | |
---|
728 | // checking for the termination of the string given. If there is a "/" at the end cut it away |
---|
729 | if (directoryName[strlen(directoryName)-1] == '/' || |
---|
730 | directoryName[strlen(directoryName)-1] == '\\') |
---|
731 | { |
---|
732 | tmpDirName = new char[strlen(directoryName)]; |
---|
733 | strncpy(tmpDirName, directoryName, strlen(directoryName)-1); |
---|
734 | tmpDirName[strlen(directoryName)-1] = '\0'; |
---|
735 | } |
---|
736 | else |
---|
737 | { |
---|
738 | tmpDirName = new char[strlen(directoryName)+1]; |
---|
739 | strcpy(tmpDirName, directoryName); |
---|
740 | } |
---|
741 | |
---|
742 | if(!stat(tmpDirName, &status)) |
---|
743 | { |
---|
744 | if (status.st_mode & (S_IFDIR |
---|
745 | #ifndef __WIN32__ |
---|
746 | | S_IFLNK |
---|
747 | #endif |
---|
748 | )) |
---|
749 | { |
---|
750 | delete[] tmpDirName; |
---|
751 | return true; |
---|
752 | } |
---|
753 | else |
---|
754 | { |
---|
755 | delete[] tmpDirName; |
---|
756 | return false; |
---|
757 | } |
---|
758 | } |
---|
759 | else |
---|
760 | { |
---|
761 | delete[] tmpDirName; |
---|
762 | return false; |
---|
763 | } |
---|
764 | } |
---|
765 | |
---|
766 | /** |
---|
767 | * Checks if the file is either a Regular file or a Symlink |
---|
768 | * @param fileName the File to check for |
---|
769 | * @returns true if it is a regular file/symlink, false otherwise |
---|
770 | */ |
---|
771 | bool ResourceManager::isFile(const char* fileName) |
---|
772 | { |
---|
773 | if (fileName == NULL) |
---|
774 | return false; |
---|
775 | char* tmpFileName = ResourceManager::homeDirCheck(fileName); |
---|
776 | // actually checks the File |
---|
777 | struct stat status; |
---|
778 | if (!stat(tmpFileName, &status)) |
---|
779 | { |
---|
780 | if (status.st_mode & (S_IFREG |
---|
781 | #ifndef __WIN32__ |
---|
782 | | S_IFLNK |
---|
783 | #endif |
---|
784 | )) |
---|
785 | { |
---|
786 | delete[] tmpFileName; |
---|
787 | return true; |
---|
788 | } |
---|
789 | else |
---|
790 | { |
---|
791 | delete[] tmpFileName; |
---|
792 | return false; |
---|
793 | } |
---|
794 | } |
---|
795 | else |
---|
796 | { |
---|
797 | delete[] tmpFileName; |
---|
798 | return false; |
---|
799 | } |
---|
800 | } |
---|
801 | |
---|
802 | /** |
---|
803 | * touches a File on the disk (thereby creating it) |
---|
804 | * @param fileName The file to touch |
---|
805 | */ |
---|
806 | bool ResourceManager::touchFile(const char* fileName) |
---|
807 | { |
---|
808 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
809 | if (tmpName == NULL) |
---|
810 | return false; |
---|
811 | FILE* stream; |
---|
812 | if( (stream = fopen (tmpName, "w")) == NULL) |
---|
813 | { |
---|
814 | PRINTF(1)("could not open %s fro writing\n", fileName); |
---|
815 | delete[] tmpName; |
---|
816 | return false; |
---|
817 | } |
---|
818 | fclose(stream); |
---|
819 | |
---|
820 | delete[] tmpName; |
---|
821 | } |
---|
822 | |
---|
823 | /** |
---|
824 | * deletes a File from disk |
---|
825 | * @param fileName the File to delete |
---|
826 | */ |
---|
827 | bool ResourceManager::deleteFile(const char* fileName) |
---|
828 | { |
---|
829 | if (fileName == NULL) |
---|
830 | return false; |
---|
831 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
832 | unlink(tmpName); |
---|
833 | delete[] tmpName; |
---|
834 | } |
---|
835 | |
---|
836 | /** |
---|
837 | * @param name the Name of the file to check |
---|
838 | * @returns The name of the file, including the HomeDir |
---|
839 | * IMPORTANT: this has to be deleted from the outside |
---|
840 | */ |
---|
841 | char* ResourceManager::homeDirCheck(const char* name) |
---|
842 | { |
---|
843 | if (name == NULL) |
---|
844 | return NULL; |
---|
845 | char* retName; |
---|
846 | if (!strncmp(name, "~/", 2)) |
---|
847 | { |
---|
848 | char tmpFileName[500]; |
---|
849 | #ifdef __WIN32__ |
---|
850 | strcpy(tmpFileName, getenv("USERPROFILE")); |
---|
851 | #else |
---|
852 | strcpy(tmpFileName, getenv("HOME")); |
---|
853 | #endif |
---|
854 | retName = new char[strlen(tmpFileName)+strlen(name)]; |
---|
855 | sprintf(retName, "%s%s", tmpFileName, name+1); |
---|
856 | } |
---|
857 | else |
---|
858 | { |
---|
859 | retName = new char[strlen(name)+1]; |
---|
860 | strcpy(retName, name); |
---|
861 | } |
---|
862 | return retName; |
---|
863 | } |
---|
864 | |
---|
865 | /** |
---|
866 | * @param fileName the Name of the File to check |
---|
867 | * @returns The full name of the file, including the DataDir, and NULL if the file does not exist |
---|
868 | * !!IMPORTANT: this has to be deleted from the outside!! |
---|
869 | */ |
---|
870 | char* ResourceManager::getFullName(const char* fileName) |
---|
871 | { |
---|
872 | if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL) |
---|
873 | return NULL; |
---|
874 | |
---|
875 | char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir()) |
---|
876 | + strlen(fileName) + 1]; |
---|
877 | sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); |
---|
878 | if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName)) |
---|
879 | return retName; |
---|
880 | else |
---|
881 | { |
---|
882 | delete[] retName; |
---|
883 | return NULL; |
---|
884 | } |
---|
885 | } |
---|
886 | |
---|
887 | |
---|
888 | /** |
---|
889 | * checks wether a file is in the DataDir. |
---|
890 | * @param fileName the File to check if it is in the Data-Dir structure. |
---|
891 | * @returns true if the file exists, false otherwise |
---|
892 | */ |
---|
893 | bool ResourceManager::isInDataDir(const char* fileName) |
---|
894 | { |
---|
895 | if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL) |
---|
896 | return false; |
---|
897 | |
---|
898 | bool retVal = false; |
---|
899 | char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir()) |
---|
900 | + strlen(fileName) + 1]; |
---|
901 | sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); |
---|
902 | |
---|
903 | if (ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile)) |
---|
904 | retVal = true; |
---|
905 | else |
---|
906 | retVal = false; |
---|
907 | delete[] checkFile; |
---|
908 | return retVal; |
---|
909 | } |
---|
910 | |
---|
911 | |
---|
912 | /** |
---|
913 | * outputs debug information about the ResourceManager |
---|
914 | */ |
---|
915 | void ResourceManager::debug() const |
---|
916 | { |
---|
917 | PRINT(0)("=RM===================================\n"); |
---|
918 | PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n"); |
---|
919 | PRINT(0)("======================================\n"); |
---|
920 | // if it is not initialized |
---|
921 | PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef); |
---|
922 | PRINT(0)(" Data-Directory is: %s\n", this->dataDir); |
---|
923 | PRINT(0)(" List of Image-Directories: "); |
---|
924 | tIterator<char>* tmpIt = imageDirs->getIterator(); |
---|
925 | char* tmpDir = tmpIt->firstElement(); |
---|
926 | while(tmpDir) |
---|
927 | { |
---|
928 | PRINT(0)("%s ",tmpDir); |
---|
929 | tmpDir = tmpIt->nextElement(); |
---|
930 | } |
---|
931 | delete tmpIt; |
---|
932 | PRINT(0)("\n"); |
---|
933 | |
---|
934 | PRINT(0)("List of all stored Resources:\n"); |
---|
935 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
936 | Resource* enumRes = iterator->firstElement(); |
---|
937 | while (enumRes) |
---|
938 | { |
---|
939 | PRINT(0)("-----------------------------------------\n"); |
---|
940 | PRINT(0)("Name: %s; References: %d; Type: %s ", enumRes->name, enumRes->count, ResourceManager::ResourceTypeToChar(enumRes->type)); |
---|
941 | |
---|
942 | PRINT(0)("gets deleted at "); |
---|
943 | switch(enumRes->prio) |
---|
944 | { |
---|
945 | default: |
---|
946 | case RP_NO: |
---|
947 | PRINT(0)("first posibility (0)\n"); |
---|
948 | break; |
---|
949 | case RP_LEVEL: |
---|
950 | PRINT(0)("the end of the Level (1)\n"); |
---|
951 | break; |
---|
952 | case RP_CAMPAIGN: |
---|
953 | PRINT(0)("the end of the campaign (2)\n"); |
---|
954 | break; |
---|
955 | case RP_GAME: |
---|
956 | PRINT(0)("when leaving the game (3)\n"); |
---|
957 | break; |
---|
958 | } |
---|
959 | enumRes = iterator->nextElement(); |
---|
960 | } |
---|
961 | delete iterator; |
---|
962 | |
---|
963 | |
---|
964 | |
---|
965 | PRINT(0)("==================================RM==\n"); |
---|
966 | } |
---|
967 | |
---|
968 | |
---|
969 | /** |
---|
970 | * converts a ResourceType into the corresponding String |
---|
971 | * @param type the ResourceType to translate |
---|
972 | * @returns the converted String. |
---|
973 | */ |
---|
974 | const char* ResourceManager::ResourceTypeToChar(ResourceType type) |
---|
975 | { |
---|
976 | switch (type) |
---|
977 | { |
---|
978 | #ifndef NO_MODEL |
---|
979 | case OBJ: |
---|
980 | return "ObjectModel"; |
---|
981 | break; |
---|
982 | case PRIM: |
---|
983 | return "PrimitiveModel"; |
---|
984 | break; |
---|
985 | case MD2: |
---|
986 | return "MD2-Data"; |
---|
987 | break; |
---|
988 | #endif |
---|
989 | #ifndef NO_TEXTURES |
---|
990 | case IMAGE: |
---|
991 | return "ImageFile (Texture)"; |
---|
992 | break; |
---|
993 | #endif |
---|
994 | #ifndef NO_AUDIO |
---|
995 | case WAV: |
---|
996 | return "SoundFile"; |
---|
997 | break; |
---|
998 | case OGG: |
---|
999 | return "MusicFile"; |
---|
1000 | break; |
---|
1001 | #endif |
---|
1002 | #ifndef NO_TEXT |
---|
1003 | case TTF: |
---|
1004 | return "Font (TTF)"; |
---|
1005 | break; |
---|
1006 | #endif |
---|
1007 | #ifndef NO_SHADERS |
---|
1008 | case SHADER: |
---|
1009 | return "Shader"; |
---|
1010 | break; |
---|
1011 | #endif |
---|
1012 | default: |
---|
1013 | return "unknown Format"; |
---|
1014 | break; |
---|
1015 | } |
---|
1016 | } |
---|