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