[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); |
---|
[5306] | 253 | printf("adding %s count: %d\n", tmpResource->pointer->getName(), tmpResource->count); |
---|
[3677] | 254 | tmpResource->count++; |
---|
| 255 | if(tmpResource->prio < prio) |
---|
[4597] | 256 | tmpResource->prio = prio; |
---|
[3677] | 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
[3660] | 260 | char* tmpDir; |
---|
[3658] | 261 | // Setting up the new Resource |
---|
| 262 | tmpResource = new Resource; |
---|
| 263 | tmpResource->count = 1; |
---|
| 264 | tmpResource->type = type; |
---|
[3660] | 265 | tmpResource->prio = prio; |
---|
[4356] | 266 | tmpResource->pointer = NULL; |
---|
[3658] | 267 | tmpResource->name = new char[strlen(fileName)+1]; |
---|
| 268 | strcpy(tmpResource->name, fileName); |
---|
| 269 | |
---|
| 270 | // creating the full name. (directoryName + FileName) |
---|
[4462] | 271 | char* fullName = ResourceManager::getFullName(fileName); |
---|
[3658] | 272 | // Checking for the type of resource \see ResourceType |
---|
| 273 | switch(type) |
---|
[4597] | 274 | { |
---|
[4534] | 275 | #ifndef NO_MODEL |
---|
[4597] | 276 | case OBJ: |
---|
| 277 | if (param1) |
---|
| 278 | tmpResource->modelSize = *(float*)param1; |
---|
| 279 | else |
---|
| 280 | tmpResource->modelSize = 1.0; |
---|
[3790] | 281 | |
---|
[4597] | 282 | if(ResourceManager::isFile(fullName)) |
---|
| 283 | tmpResource->pointer = new OBJModel(fullName, tmpResource->modelSize); |
---|
| 284 | else |
---|
| 285 | { |
---|
| 286 | PRINTF(2)("Sorry, %s does not exist. Loading a cube-Model instead\n", fullName); |
---|
| 287 | tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, &tmpResource->modelSize); |
---|
| 288 | } |
---|
| 289 | break; |
---|
| 290 | case PRIM: |
---|
| 291 | if (param1) |
---|
| 292 | tmpResource->modelSize = *(float*)param1; |
---|
| 293 | else |
---|
| 294 | tmpResource->modelSize = 1.0; |
---|
[3790] | 295 | |
---|
[4597] | 296 | if (!strcmp(tmpResource->name, "cube")) |
---|
| 297 | tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->modelSize); |
---|
| 298 | else if (!strcmp(tmpResource->name, "sphere")) |
---|
| 299 | tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->modelSize); |
---|
| 300 | else if (!strcmp(tmpResource->name, "plane")) |
---|
| 301 | tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->modelSize); |
---|
| 302 | else if (!strcmp(tmpResource->name, "cylinder")) |
---|
| 303 | tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->modelSize); |
---|
| 304 | else if (!strcmp(tmpResource->name, "cone")) |
---|
| 305 | tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->modelSize); |
---|
| 306 | break; |
---|
| 307 | case MD2: |
---|
| 308 | if(ResourceManager::isFile(fullName)) |
---|
| 309 | { |
---|
[5306] | 310 | if (param1 != NULL) |
---|
[4597] | 311 | { |
---|
| 312 | tmpResource->skinFileName = new char[strlen((const char*)param1)+1]; |
---|
| 313 | strcpy(tmpResource->skinFileName, (const char*) param1); |
---|
| 314 | } |
---|
| 315 | else |
---|
| 316 | tmpResource->skinFileName = NULL; |
---|
| 317 | tmpResource->pointer = new MD2Data(fullName, tmpResource->skinFileName); |
---|
| 318 | } |
---|
| 319 | break; |
---|
[4534] | 320 | #endif /* NO_MODEL */ |
---|
| 321 | #ifndef NO_TEXT |
---|
[4597] | 322 | case TTF: |
---|
| 323 | if (param1) |
---|
[5306] | 324 | tmpResource->ttfSize = *(unsigned int*)param1; |
---|
[4597] | 325 | else |
---|
| 326 | tmpResource->ttfSize = FONT_DEFAULT_SIZE; |
---|
| 327 | |
---|
| 328 | if(isFile(fullName)) |
---|
[5306] | 329 | tmpResource->pointer = new Font(fullName, tmpResource->ttfSize); |
---|
[4597] | 330 | else |
---|
| 331 | PRINTF(2)("Sorry, %s does not exist. Not loading Font\n", fullName); |
---|
| 332 | break; |
---|
[4534] | 333 | #endif /* NO_TEXT */ |
---|
| 334 | #ifndef NO_AUDIO |
---|
[4597] | 335 | case WAV: |
---|
| 336 | if(isFile(fullName)) |
---|
| 337 | tmpResource->pointer = new SoundBuffer(fullName); |
---|
| 338 | break; |
---|
[4961] | 339 | case OGG: |
---|
| 340 | if (isFile(fullName)) |
---|
| 341 | tmpResource->pointer = new OggPlayer(fullName); |
---|
| 342 | break; |
---|
[4534] | 343 | #endif /* NO_AUDIO */ |
---|
| 344 | #ifndef NO_TEXTURES |
---|
[4597] | 345 | case IMAGE: |
---|
| 346 | if(isFile(fullName)) |
---|
| 347 | { |
---|
| 348 | PRINTF(4)("Image %s resides to %s\n", fileName, fullName); |
---|
| 349 | tmpResource->pointer = new Texture(fullName); |
---|
| 350 | } |
---|
| 351 | else |
---|
| 352 | { |
---|
| 353 | tIterator<char>* iterator = imageDirs->getIterator(); |
---|
[5115] | 354 | tmpDir = iterator->firstElement(); |
---|
[4597] | 355 | while(tmpDir) |
---|
| 356 | { |
---|
| 357 | char* imgName = new char[strlen(tmpDir)+strlen(fileName)+1]; |
---|
| 358 | sprintf(imgName, "%s%s", tmpDir, fileName); |
---|
| 359 | if(isFile(imgName)) |
---|
| 360 | { |
---|
| 361 | PRINTF(4)("Image %s resides to %s\n", fileName, imgName); |
---|
| 362 | tmpResource->pointer = new Texture(imgName); |
---|
[5211] | 363 | delete[] imgName; |
---|
[4597] | 364 | break; |
---|
| 365 | } |
---|
[5211] | 366 | delete[] imgName; |
---|
[4597] | 367 | tmpDir = iterator->nextElement(); |
---|
| 368 | } |
---|
| 369 | delete iterator; |
---|
| 370 | } |
---|
| 371 | if(!tmpResource) |
---|
| 372 | PRINTF(2)("!!Image %s not Found!!\n", fileName); |
---|
| 373 | break; |
---|
[4534] | 374 | #endif /* NO_TEXTURES */ |
---|
[4597] | 375 | default: |
---|
| 376 | tmpResource->pointer = NULL; |
---|
[5306] | 377 | 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] | 378 | break; |
---|
| 379 | } |
---|
[5216] | 380 | if (tmpResource->pointer != NULL) |
---|
[4597] | 381 | this->resourceList->add(tmpResource); |
---|
[5211] | 382 | delete[] fullName; |
---|
[3655] | 383 | } |
---|
[5216] | 384 | if (tmpResource->pointer != NULL) |
---|
[3790] | 385 | return tmpResource->pointer; |
---|
[4597] | 386 | else |
---|
[3790] | 387 | { |
---|
| 388 | PRINTF(2)("Resource %s could not be loaded\n", fileName); |
---|
[5208] | 389 | delete[] tmpResource->name; |
---|
[3790] | 390 | delete tmpResource; |
---|
| 391 | return NULL; |
---|
| 392 | } |
---|
[3658] | 393 | } |
---|
| 394 | |
---|
| 395 | /** |
---|
[4961] | 396 | * unloads a Resource |
---|
[4836] | 397 | * @param pointer: The pointer to free |
---|
| 398 | * @param prio: the PriorityLevel to unload this resource |
---|
| 399 | * @returns true if successful (pointer found, and deleted), false otherwise |
---|
[3658] | 400 | */ |
---|
[3660] | 401 | bool ResourceManager::unload(void* pointer, ResourcePriority prio) |
---|
[3658] | 402 | { |
---|
| 403 | // if pointer is existent. and only one resource of this type exists. |
---|
[3672] | 404 | Resource* tmpResource = this->locateResourceByPointer(pointer); |
---|
[5302] | 405 | if (tmpResource == NULL) |
---|
[3655] | 406 | { |
---|
[3658] | 407 | PRINTF(2)("Resource not Found %p\n", pointer); |
---|
| 408 | return false; |
---|
[3655] | 409 | } |
---|
[3660] | 410 | else |
---|
| 411 | unload(tmpResource, prio); |
---|
| 412 | } |
---|
| 413 | |
---|
[4465] | 414 | /** |
---|
[4961] | 415 | * unloads a Resource |
---|
[4836] | 416 | * @param resource: The resource to unloade |
---|
| 417 | * @param prio the PriorityLevel to unload this resource |
---|
[4465] | 418 | */ |
---|
[3660] | 419 | bool ResourceManager::unload(Resource* resource, ResourcePriority prio) |
---|
| 420 | { |
---|
[5306] | 421 | if (resource == NULL) |
---|
| 422 | return false; |
---|
| 423 | printf("removing %s count: %d\n", resource->pointer->getName(), resource->count); |
---|
[3665] | 424 | if (resource->count > 0) |
---|
| 425 | resource->count--; |
---|
[5306] | 426 | |
---|
[3660] | 427 | if (resource->prio <= prio) |
---|
[3658] | 428 | { |
---|
[3660] | 429 | if (resource->count <= 0) |
---|
[4597] | 430 | { |
---|
| 431 | // deleting the Resource |
---|
| 432 | switch(resource->type) |
---|
| 433 | { |
---|
[4534] | 434 | #ifndef NO_MODEL |
---|
[4597] | 435 | case OBJ: |
---|
| 436 | case PRIM: |
---|
| 437 | delete (Model*)resource->pointer; |
---|
| 438 | break; |
---|
| 439 | case MD2: |
---|
| 440 | delete (MD2Data*)resource->pointer; |
---|
| 441 | break; |
---|
[4534] | 442 | #endif /* NO_MODEL */ |
---|
| 443 | #ifndef NO_AUDIO |
---|
[4597] | 444 | case WAV: |
---|
| 445 | delete (SoundBuffer*)resource->pointer; |
---|
| 446 | break; |
---|
[4961] | 447 | case OGG: |
---|
| 448 | delete (OggPlayer*)resource->pointer; |
---|
| 449 | break; |
---|
[4534] | 450 | #endif /* NO_AUDIO */ |
---|
| 451 | #ifndef NO_TEXT |
---|
[4597] | 452 | case TTF: |
---|
| 453 | delete (Font*)resource->pointer; |
---|
| 454 | break; |
---|
[4534] | 455 | #endif /* NO_TEXT */ |
---|
| 456 | #ifndef NO_TEXTURES |
---|
[4597] | 457 | case IMAGE: |
---|
| 458 | delete (Texture*)resource->pointer; |
---|
| 459 | break; |
---|
[4534] | 460 | #endif /* NO_TEXTURES */ |
---|
[4597] | 461 | default: |
---|
[4653] | 462 | PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n"); |
---|
[4597] | 463 | return false; |
---|
| 464 | break; |
---|
| 465 | } |
---|
| 466 | // deleting the List Entry: |
---|
| 467 | PRINTF(4)("Resource %s safely removed.\n", resource->name); |
---|
[5208] | 468 | delete[] resource->name; |
---|
[4597] | 469 | this->resourceList->remove(resource); |
---|
[5302] | 470 | delete resource; |
---|
[4597] | 471 | } |
---|
[3660] | 472 | else |
---|
[4597] | 473 | PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count); |
---|
[3658] | 474 | } |
---|
| 475 | else |
---|
[3660] | 476 | PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name); |
---|
[3658] | 477 | return true; |
---|
[3655] | 478 | } |
---|
| 479 | |
---|
[3660] | 480 | |
---|
[3655] | 481 | /** |
---|
[4836] | 482 | * unloads all alocated Memory of Resources with a pririty lower than prio |
---|
| 483 | * @param prio The priority to delete |
---|
[3660] | 484 | */ |
---|
| 485 | bool ResourceManager::unloadAllByPriority(ResourcePriority prio) |
---|
| 486 | { |
---|
[3666] | 487 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
[5115] | 488 | Resource* enumRes = iterator->firstElement(); |
---|
[3660] | 489 | while (enumRes) |
---|
| 490 | { |
---|
| 491 | if (enumRes->prio <= prio) |
---|
[4597] | 492 | if (enumRes->count == 0) |
---|
| 493 | unload(enumRes, prio); |
---|
| 494 | else |
---|
| 495 | PRINTF(2)("unable to unload %s because there are still %d references to it\n", |
---|
| 496 | enumRes->name, enumRes->count); |
---|
[3666] | 497 | //enumRes = resourceList->nextElement(); |
---|
| 498 | enumRes = iterator->nextElement(); |
---|
[3660] | 499 | } |
---|
[3666] | 500 | delete iterator; |
---|
[3660] | 501 | } |
---|
| 502 | |
---|
| 503 | /** |
---|
[4961] | 504 | * Searches for a Resource by some information |
---|
[4836] | 505 | * @param fileName: The name to look for |
---|
| 506 | * @param type the Type of resource to locate. |
---|
| 507 | * @param param1: an additional option to parse (see the constuctors for more help) |
---|
| 508 | * @param param2: an additional option to parse (see the constuctors for more help) |
---|
| 509 | * @param param3: an additional option to parse (see the constuctors for more help) |
---|
| 510 | * @returns a Pointer to the Resource if found, NULL otherwise. |
---|
[3658] | 511 | */ |
---|
[4462] | 512 | Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type, |
---|
[4597] | 513 | void* param1, void* param2, void* param3) |
---|
[3658] | 514 | { |
---|
[3667] | 515 | // Resource* enumRes = resourceList->enumerate(); |
---|
| 516 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
[5115] | 517 | Resource* enumRes = iterator->firstElement(); |
---|
[3658] | 518 | while (enumRes) |
---|
| 519 | { |
---|
[3790] | 520 | if (enumRes->type == type && !strcmp(fileName, enumRes->name)) |
---|
[4597] | 521 | { |
---|
| 522 | bool match = false; |
---|
| 523 | bool subMatch = false; |
---|
[4462] | 524 | |
---|
[4597] | 525 | switch (type) |
---|
| 526 | { |
---|
[4534] | 527 | #ifndef NO_MODEL |
---|
[4597] | 528 | case PRIM: |
---|
| 529 | case OBJ: |
---|
| 530 | if (!param1) |
---|
| 531 | { |
---|
| 532 | if (enumRes->modelSize == 1.0) |
---|
| 533 | match = true; |
---|
| 534 | } |
---|
| 535 | else if (enumRes->modelSize == *(float*)param1) |
---|
| 536 | match = true; |
---|
| 537 | break; |
---|
| 538 | case MD2: |
---|
| 539 | if (!param1) |
---|
| 540 | { |
---|
| 541 | if (enumRes->skinFileName == NULL) |
---|
| 542 | match = true; |
---|
| 543 | } |
---|
[5306] | 544 | else if (enumRes->skinFileName!= NULL && !strcmp(enumRes->skinFileName, (const char*) param1)) |
---|
[4597] | 545 | match = true; |
---|
| 546 | break; |
---|
[4534] | 547 | #endif /* NO_MODEL */ |
---|
| 548 | #ifndef NO_TEXT |
---|
[4597] | 549 | case TTF: |
---|
| 550 | if (!param1) |
---|
| 551 | { |
---|
| 552 | if (enumRes->ttfSize == FONT_DEFAULT_SIZE) |
---|
| 553 | subMatch = true; |
---|
| 554 | } |
---|
[5306] | 555 | else if (enumRes->ttfSize == *(unsigned int*)param1) |
---|
[4597] | 556 | subMatch = true; |
---|
| 557 | break; |
---|
[4534] | 558 | #endif /* NO_TEXT */ |
---|
[4597] | 559 | default: |
---|
| 560 | match = true; |
---|
| 561 | break; |
---|
| 562 | } |
---|
| 563 | if (match) |
---|
| 564 | { |
---|
| 565 | delete iterator; |
---|
| 566 | return enumRes; |
---|
| 567 | } |
---|
| 568 | } |
---|
[3667] | 569 | enumRes = iterator->nextElement(); |
---|
[3658] | 570 | } |
---|
[3667] | 571 | delete iterator; |
---|
[3658] | 572 | return NULL; |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | /** |
---|
[4961] | 576 | * Searches for a Resource by Pointer |
---|
[4836] | 577 | * @param pointer the Pointer to search for |
---|
| 578 | * @returns a Pointer to the Resource if found, NULL otherwise. |
---|
[3658] | 579 | */ |
---|
| 580 | Resource* ResourceManager::locateResourceByPointer(const void* pointer) |
---|
| 581 | { |
---|
[3667] | 582 | // Resource* enumRes = resourceList->enumerate(); |
---|
| 583 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
[5115] | 584 | Resource* enumRes = iterator->firstElement(); |
---|
[3658] | 585 | while (enumRes) |
---|
| 586 | { |
---|
[3665] | 587 | if (pointer == enumRes->pointer) |
---|
[4597] | 588 | { |
---|
| 589 | delete iterator; |
---|
| 590 | return enumRes; |
---|
| 591 | } |
---|
[3667] | 592 | enumRes = iterator->nextElement(); |
---|
[3658] | 593 | } |
---|
[3667] | 594 | delete iterator; |
---|
[3658] | 595 | return NULL; |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | /** |
---|
[4961] | 599 | * Checks if it is a Directory |
---|
[4836] | 600 | * @param directoryName the Directory to check for |
---|
| 601 | * @returns true if it is a directory/symlink false otherwise |
---|
[3655] | 602 | */ |
---|
| 603 | bool ResourceManager::isDir(const char* directoryName) |
---|
| 604 | { |
---|
[4462] | 605 | if (directoryName == NULL) |
---|
| 606 | return false; |
---|
| 607 | |
---|
[3883] | 608 | char* tmpDirName = NULL; |
---|
[3655] | 609 | struct stat status; |
---|
[3883] | 610 | |
---|
| 611 | // checking for the termination of the string given. If there is a "/" at the end cut it away |
---|
[5113] | 612 | if (directoryName[strlen(directoryName)-1] == '/' || |
---|
| 613 | directoryName[strlen(directoryName)-1] == '\\') |
---|
[3883] | 614 | { |
---|
| 615 | tmpDirName = new char[strlen(directoryName)+1]; |
---|
| 616 | strncpy(tmpDirName, directoryName, strlen(directoryName)-1); |
---|
| 617 | tmpDirName[strlen(directoryName)-1] = '\0'; |
---|
| 618 | } |
---|
| 619 | else |
---|
| 620 | { |
---|
| 621 | tmpDirName = new char[strlen(directoryName)+1]; |
---|
| 622 | strcpy(tmpDirName, directoryName); |
---|
| 623 | } |
---|
| 624 | |
---|
[4032] | 625 | if(!stat(tmpDirName, &status)) |
---|
| 626 | { |
---|
| 627 | if (status.st_mode & (S_IFDIR |
---|
[3790] | 628 | #ifndef __WIN32__ |
---|
[4597] | 629 | | S_IFLNK |
---|
[3790] | 630 | #endif |
---|
[4597] | 631 | )) |
---|
| 632 | { |
---|
[5113] | 633 | delete[] tmpDirName; |
---|
[4597] | 634 | return true; |
---|
| 635 | } |
---|
[4032] | 636 | else |
---|
[4597] | 637 | { |
---|
[5208] | 638 | delete[] tmpDirName; |
---|
[4597] | 639 | return false; |
---|
| 640 | } |
---|
[3883] | 641 | } |
---|
[3658] | 642 | else |
---|
[5211] | 643 | { |
---|
| 644 | delete[] tmpDirName; |
---|
[4032] | 645 | return false; |
---|
[5211] | 646 | } |
---|
[3655] | 647 | } |
---|
| 648 | |
---|
| 649 | /** |
---|
[4961] | 650 | * Checks if the file is either a Regular file or a Symlink |
---|
[4836] | 651 | * @param fileName the File to check for |
---|
| 652 | * @returns true if it is a regular file/symlink, false otherwise |
---|
[3655] | 653 | */ |
---|
| 654 | bool ResourceManager::isFile(const char* fileName) |
---|
| 655 | { |
---|
[4462] | 656 | if (fileName == NULL) |
---|
| 657 | return false; |
---|
[4032] | 658 | char* tmpFileName = ResourceManager::homeDirCheck(fileName); |
---|
| 659 | // actually checks the File |
---|
[3655] | 660 | struct stat status; |
---|
[4032] | 661 | if (!stat(tmpFileName, &status)) |
---|
| 662 | { |
---|
[4597] | 663 | if (status.st_mode & (S_IFREG |
---|
[3790] | 664 | #ifndef __WIN32__ |
---|
[4597] | 665 | | S_IFLNK |
---|
[3790] | 666 | #endif |
---|
[4597] | 667 | )) |
---|
| 668 | { |
---|
[5208] | 669 | delete[] tmpFileName; |
---|
[4597] | 670 | return true; |
---|
| 671 | } |
---|
[4032] | 672 | else |
---|
[4597] | 673 | { |
---|
[5208] | 674 | delete[] tmpFileName; |
---|
[4597] | 675 | return false; |
---|
| 676 | } |
---|
[4032] | 677 | } |
---|
[4597] | 678 | else |
---|
[4032] | 679 | { |
---|
[5208] | 680 | delete[] tmpFileName; |
---|
[4032] | 681 | return false; |
---|
| 682 | } |
---|
| 683 | } |
---|
| 684 | |
---|
[4166] | 685 | /** |
---|
[4961] | 686 | * touches a File on the disk (thereby creating it) |
---|
[4836] | 687 | * @param fileName The file to touch |
---|
[4166] | 688 | */ |
---|
[4032] | 689 | bool ResourceManager::touchFile(const char* fileName) |
---|
| 690 | { |
---|
| 691 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
[4462] | 692 | if (tmpName == NULL) |
---|
| 693 | return false; |
---|
[4032] | 694 | FILE* stream; |
---|
| 695 | if( (stream = fopen (tmpName, "w")) == NULL) |
---|
| 696 | { |
---|
| 697 | PRINTF(1)("could not open %s fro writing\n", fileName); |
---|
[5208] | 698 | delete[] tmpName; |
---|
[4032] | 699 | return false; |
---|
| 700 | } |
---|
[4033] | 701 | fclose(stream); |
---|
[4597] | 702 | |
---|
[5208] | 703 | delete[] tmpName; |
---|
[4032] | 704 | } |
---|
| 705 | |
---|
[4166] | 706 | /** |
---|
[4961] | 707 | * deletes a File from disk |
---|
[4836] | 708 | * @param fileName the File to delete |
---|
[4166] | 709 | */ |
---|
[4032] | 710 | bool ResourceManager::deleteFile(const char* fileName) |
---|
| 711 | { |
---|
[4462] | 712 | if (fileName == NULL) |
---|
| 713 | return false; |
---|
[4032] | 714 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
| 715 | unlink(tmpName); |
---|
[5208] | 716 | delete[] tmpName; |
---|
[4032] | 717 | } |
---|
| 718 | |
---|
[4597] | 719 | /** |
---|
[4961] | 720 | * @param name the Name of the file to check |
---|
| 721 | * @returns The name of the file, including the HomeDir |
---|
| 722 | * IMPORTANT: this has to be deleted from the outside |
---|
| 723 | */ |
---|
[4032] | 724 | char* ResourceManager::homeDirCheck(const char* name) |
---|
| 725 | { |
---|
[4462] | 726 | if (name == NULL) |
---|
| 727 | return NULL; |
---|
[4032] | 728 | char* retName; |
---|
| 729 | if (!strncmp(name, "~/", 2)) |
---|
| 730 | { |
---|
| 731 | char tmpFileName[500]; |
---|
| 732 | #ifdef __WIN32__ |
---|
| 733 | strcpy(tmpFileName, getenv("USERPROFILE")); |
---|
| 734 | #else |
---|
| 735 | strcpy(tmpFileName, getenv("HOME")); |
---|
| 736 | #endif |
---|
| 737 | retName = new char[strlen(tmpFileName)+strlen(name)]; |
---|
| 738 | sprintf(retName, "%s%s", tmpFileName, name+1); |
---|
| 739 | } |
---|
[3655] | 740 | else |
---|
[4032] | 741 | { |
---|
| 742 | retName = new char[strlen(name)+1]; |
---|
| 743 | strcpy(retName, name); |
---|
| 744 | } |
---|
| 745 | return retName; |
---|
[3655] | 746 | } |
---|
[3676] | 747 | |
---|
[4597] | 748 | /** |
---|
[4961] | 749 | * @param fileName the Name of the File to check |
---|
| 750 | * @returns The full name of the file, including the DataDir, and NULL if the file does not exist |
---|
[5219] | 751 | * !!IMPORTANT: this has to be deleted from the outside!! |
---|
[4166] | 752 | */ |
---|
| 753 | char* ResourceManager::getFullName(const char* fileName) |
---|
| 754 | { |
---|
[4462] | 755 | if (fileName == NULL) |
---|
| 756 | return NULL; |
---|
| 757 | |
---|
[4216] | 758 | char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir()) |
---|
[4597] | 759 | + strlen(fileName) + 1]; |
---|
[4166] | 760 | sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); |
---|
[4462] | 761 | if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName)) |
---|
[4167] | 762 | return retName; |
---|
| 763 | else |
---|
| 764 | { |
---|
[5208] | 765 | delete[] retName; |
---|
[4167] | 766 | return NULL; |
---|
| 767 | } |
---|
[4166] | 768 | } |
---|
[4032] | 769 | |
---|
| 770 | |
---|
[3676] | 771 | /** |
---|
[4961] | 772 | * outputs debug information about the ResourceManager |
---|
[3676] | 773 | */ |
---|
[4746] | 774 | void ResourceManager::debug() const |
---|
[3676] | 775 | { |
---|
| 776 | PRINT(0)("=RM===================================\n"); |
---|
| 777 | PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n"); |
---|
| 778 | PRINT(0)("======================================\n"); |
---|
| 779 | // if it is not initialized |
---|
| 780 | PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef); |
---|
| 781 | PRINT(0)(" Data-Directory is: %s\n", this->dataDir); |
---|
| 782 | PRINT(0)(" List of Image-Directories: "); |
---|
| 783 | tIterator<char>* tmpIt = imageDirs->getIterator(); |
---|
[5115] | 784 | char* tmpDir = tmpIt->firstElement(); |
---|
[3676] | 785 | while(tmpDir) |
---|
| 786 | { |
---|
| 787 | PRINT(0)("%s ",tmpDir); |
---|
| 788 | tmpDir = tmpIt->nextElement(); |
---|
| 789 | } |
---|
| 790 | delete tmpIt; |
---|
| 791 | PRINT(0)("\n"); |
---|
| 792 | |
---|
| 793 | PRINT(0)("List of all stored Resources:\n"); |
---|
| 794 | tIterator<Resource>* iterator = resourceList->getIterator(); |
---|
[5115] | 795 | Resource* enumRes = iterator->firstElement(); |
---|
[3676] | 796 | while (enumRes) |
---|
| 797 | { |
---|
| 798 | PRINT(0)("-----------------------------------------\n"); |
---|
[5306] | 799 | PRINT(0)("Name: %s; References: %d; Type: %s ", enumRes->name, enumRes->count, ResourceManager::ResourceTypeToChar(enumRes->type)); |
---|
| 800 | |
---|
[3676] | 801 | PRINT(0)("gets deleted at "); |
---|
| 802 | switch(enumRes->prio) |
---|
[4597] | 803 | { |
---|
| 804 | default: |
---|
| 805 | case RP_NO: |
---|
| 806 | PRINT(0)("first posibility (0)\n"); |
---|
| 807 | break; |
---|
| 808 | case RP_LEVEL: |
---|
| 809 | PRINT(0)("the end of the Level (1)\n"); |
---|
| 810 | break; |
---|
| 811 | case RP_CAMPAIGN: |
---|
| 812 | PRINT(0)("the end of the campaign (2)\n"); |
---|
| 813 | break; |
---|
| 814 | case RP_GAME: |
---|
| 815 | PRINT(0)("when leaving the game (3)\n"); |
---|
| 816 | break; |
---|
| 817 | } |
---|
[3676] | 818 | enumRes = iterator->nextElement(); |
---|
| 819 | } |
---|
| 820 | delete iterator; |
---|
| 821 | |
---|
| 822 | |
---|
| 823 | |
---|
| 824 | PRINT(0)("==================================RM==\n"); |
---|
| 825 | } |
---|
[5306] | 826 | |
---|
| 827 | |
---|
| 828 | /** |
---|
| 829 | * converts a ResourceType into the corresponding String |
---|
| 830 | * @param type the ResourceType to translate |
---|
| 831 | * @returns the converted String. |
---|
| 832 | */ |
---|
| 833 | const char* ResourceManager::ResourceTypeToChar(ResourceType type) |
---|
| 834 | { |
---|
| 835 | switch (type) |
---|
| 836 | { |
---|
| 837 | #ifndef NO_MODEL |
---|
| 838 | case OBJ: |
---|
| 839 | return "ObjectModel"; |
---|
| 840 | break; |
---|
| 841 | case PRIM: |
---|
| 842 | return "PrimitiveModel"; |
---|
| 843 | break; |
---|
| 844 | case MD2: |
---|
| 845 | return "MD2-Data"; |
---|
| 846 | break; |
---|
| 847 | #endif |
---|
| 848 | #ifndef NO_TEXTURES |
---|
| 849 | case IMAGE: |
---|
| 850 | return "ImageFile (Texture)"; |
---|
| 851 | break; |
---|
| 852 | #endif |
---|
| 853 | #ifndef NO_AUDIO |
---|
| 854 | case WAV: |
---|
| 855 | return "SoundFile"; |
---|
| 856 | break; |
---|
| 857 | case OGG: |
---|
| 858 | return "MusicFile"; |
---|
| 859 | break; |
---|
| 860 | #endif |
---|
| 861 | #ifndef NO_TEXT |
---|
| 862 | case TTF: |
---|
| 863 | return "Font (TTF)"; |
---|
| 864 | break; |
---|
| 865 | #endif |
---|
| 866 | default: |
---|
| 867 | return "unknown Format"; |
---|
| 868 | break; |
---|
| 869 | } |
---|
| 870 | } |
---|