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