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