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