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