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