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