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