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