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