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