[4597] | 1 | /* |
---|
[1853] | 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 | |
---|
[6648] | 20 | #include "substring.h" |
---|
[4642] | 21 | #include "debug.h" |
---|
| 22 | |
---|
[6222] | 23 | #include <algorithm> |
---|
[6655] | 24 | #include <assert.h> |
---|
[6222] | 25 | |
---|
[3655] | 26 | // different resource Types |
---|
[4534] | 27 | #ifndef NO_MODEL |
---|
[3655] | 28 | #include "objModel.h" |
---|
[3657] | 29 | #include "primitive_model.h" |
---|
[4462] | 30 | #include "md2Model.h" |
---|
[4534] | 31 | #endif /* NO_MODEL */ |
---|
| 32 | #ifndef NO_TEXTURES |
---|
[3655] | 33 | #include "texture.h" |
---|
[4534] | 34 | #endif /* NO_TEXTURES */ |
---|
| 35 | #ifndef NO_TEXT |
---|
[5344] | 36 | #include "font.h" |
---|
[4534] | 37 | #endif /* NO_TEXT */ |
---|
| 38 | #ifndef NO_AUDIO |
---|
[5930] | 39 | #include "sound_buffer.h" |
---|
[4961] | 40 | #include "ogg_player.h" |
---|
[4534] | 41 | #endif /* NO_AUDIO */ |
---|
[5323] | 42 | #ifndef NO_SHADERS |
---|
| 43 | #include "shader.h" |
---|
| 44 | #endif /* NO_SHADERS */ |
---|
[1853] | 45 | |
---|
[3655] | 46 | // File Handling Includes |
---|
| 47 | #include <sys/types.h> |
---|
| 48 | #include <sys/stat.h> |
---|
| 49 | #include <unistd.h> |
---|
| 50 | |
---|
[1856] | 51 | using namespace std; |
---|
[1853] | 52 | |
---|
[3245] | 53 | /** |
---|
[6647] | 54 | * @brief standard constructor |
---|
[3245] | 55 | */ |
---|
[4597] | 56 | ResourceManager::ResourceManager () |
---|
[3365] | 57 | { |
---|
[4597] | 58 | this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager"); |
---|
| 59 | this->setName("ResourceManager"); |
---|
| 60 | |
---|
[5423] | 61 | this->dataDir = new char[3]; |
---|
| 62 | strcpy(this->dataDir, "./"); |
---|
[5480] | 63 | this->tryDataDir("./data"); |
---|
[3365] | 64 | } |
---|
[1853] | 65 | |
---|
[3658] | 66 | //! Singleton Reference to the ResourceManager |
---|
[3655] | 67 | ResourceManager* ResourceManager::singletonRef = NULL; |
---|
| 68 | |
---|
[3245] | 69 | /** |
---|
[6647] | 70 | * @brief standard destructor |
---|
[3655] | 71 | */ |
---|
[4746] | 72 | ResourceManager::~ResourceManager () |
---|
[3655] | 73 | { |
---|
[3660] | 74 | // deleting the Resources-List |
---|
[3672] | 75 | this->unloadAllByPriority(RP_GAME); |
---|
[5303] | 76 | |
---|
[6222] | 77 | if (!this->resourceList.empty()) |
---|
| 78 | PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size()); |
---|
[5303] | 79 | |
---|
[3660] | 80 | // deleting the Directorie Lists |
---|
[6222] | 81 | while (!this->imageDirs.empty()) |
---|
[6640] | 82 | { |
---|
[6642] | 83 | delete[] this->imageDirs.back(); |
---|
| 84 | this->imageDirs.pop_back(); |
---|
[6640] | 85 | } |
---|
[3672] | 86 | |
---|
[5211] | 87 | delete[] this->dataDir; |
---|
| 88 | |
---|
[3655] | 89 | ResourceManager::singletonRef = NULL; |
---|
| 90 | } |
---|
[1853] | 91 | |
---|
[3655] | 92 | /** |
---|
[6647] | 93 | * @brief sets the data main directory |
---|
[4836] | 94 | * @param dataDir the DataDirectory. |
---|
[5480] | 95 | */ |
---|
[3883] | 96 | bool ResourceManager::setDataDir(const char* dataDir) |
---|
[3543] | 97 | { |
---|
[4341] | 98 | char* realDir = ResourceManager::homeDirCheck(dataDir); |
---|
| 99 | if (isDir(realDir)) |
---|
[5480] | 100 | { |
---|
| 101 | delete[] this->dataDir; |
---|
| 102 | if (dataDir[strlen(dataDir)-1] == '/' || dataDir[strlen(dataDir)-1] == '\\') |
---|
[3655] | 103 | { |
---|
[5480] | 104 | this->dataDir = new char[strlen(realDir)+1]; |
---|
| 105 | strcpy(this->dataDir, realDir); |
---|
[3655] | 106 | } |
---|
[5480] | 107 | else |
---|
| 108 | { |
---|
| 109 | this->dataDir = new char[strlen(realDir)+2]; |
---|
| 110 | strcpy(this->dataDir, realDir); |
---|
| 111 | this->dataDir[strlen(realDir)] = '/'; |
---|
| 112 | this->dataDir[strlen(realDir)+1] = '\0'; |
---|
| 113 | } |
---|
| 114 | delete[] realDir; |
---|
| 115 | return true; |
---|
| 116 | } |
---|
[3655] | 117 | else |
---|
[5480] | 118 | { |
---|
| 119 | PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", realDir, this->dataDir); |
---|
| 120 | delete[] realDir; |
---|
| 121 | return false; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
[6647] | 126 | * @brief sets the data main directory |
---|
[5480] | 127 | * @param dataDir the DataDirectory. |
---|
| 128 | * |
---|
| 129 | * this is essentially the same as setDataDir, but it ommits the error-message |
---|
| 130 | */ |
---|
| 131 | bool ResourceManager::tryDataDir(const char* dataDir) |
---|
| 132 | { |
---|
| 133 | char* realDir = ResourceManager::homeDirCheck(dataDir); |
---|
| 134 | if (isDir(realDir)) |
---|
| 135 | { |
---|
| 136 | delete[] this->dataDir; |
---|
| 137 | if (dataDir[strlen(dataDir)-1] == '/' || dataDir[strlen(dataDir)-1] == '\\') |
---|
[3655] | 138 | { |
---|
[5480] | 139 | this->dataDir = new char[strlen(realDir)+1]; |
---|
| 140 | strcpy(this->dataDir, realDir); |
---|
[3655] | 141 | } |
---|
[5480] | 142 | else |
---|
| 143 | { |
---|
| 144 | this->dataDir = new char[strlen(realDir)+2]; |
---|
| 145 | strcpy(this->dataDir, realDir); |
---|
| 146 | this->dataDir[strlen(realDir)] = '/'; |
---|
| 147 | this->dataDir[strlen(realDir)+1] = '\0'; |
---|
| 148 | } |
---|
| 149 | delete[] realDir; |
---|
| 150 | return true; |
---|
| 151 | } |
---|
[5483] | 152 | delete[] realDir; |
---|
[5482] | 153 | return false; |
---|
[3543] | 154 | } |
---|
[1853] | 155 | |
---|
[5480] | 156 | |
---|
[3660] | 157 | /** |
---|
[6647] | 158 | * @brief checks for the DataDirectory, by looking if |
---|
[5480] | 159 | * @param fileInside is iniside of the given directory. |
---|
[4091] | 160 | */ |
---|
[5480] | 161 | bool ResourceManager::verifyDataDir(const char* fileInside) |
---|
[4091] | 162 | { |
---|
| 163 | bool retVal; |
---|
| 164 | if (!isDir(this->dataDir)) |
---|
[6640] | 165 | { |
---|
| 166 | PRINTF(1)("%s is not a directory\n", this->dataDir); |
---|
| 167 | return false; |
---|
| 168 | } |
---|
[4597] | 169 | |
---|
[4091] | 170 | char* testFile = new char[strlen(this->dataDir)+strlen(fileInside)+1]; |
---|
| 171 | sprintf(testFile, "%s%s", this->dataDir, fileInside); |
---|
| 172 | retVal = isFile(testFile); |
---|
[5208] | 173 | delete[] testFile; |
---|
[4091] | 174 | return retVal; |
---|
| 175 | } |
---|
| 176 | |
---|
[4653] | 177 | #ifndef NO_TEXTURES |
---|
[4091] | 178 | /** |
---|
[6647] | 179 | * @brief adds a new Path for Images |
---|
[4836] | 180 | * @param imageDir The path to insert |
---|
| 181 | * @returns true, if the Path was well and injected (or already existent within the list) |
---|
[3660] | 182 | false otherwise |
---|
| 183 | */ |
---|
[4370] | 184 | bool ResourceManager::addImageDir(const char* imageDir) |
---|
[3658] | 185 | { |
---|
[5335] | 186 | if (imageDir == NULL) |
---|
| 187 | return false; |
---|
| 188 | |
---|
| 189 | char* newDir; |
---|
| 190 | if (imageDir[strlen(imageDir)-1] == '/' || imageDir[strlen(imageDir)-1] == '\\') |
---|
| 191 | { |
---|
| 192 | newDir = new char[strlen(imageDir)+1]; |
---|
| 193 | strcpy(newDir, imageDir); |
---|
| 194 | } |
---|
| 195 | else |
---|
| 196 | { |
---|
| 197 | newDir = new char[strlen(imageDir)+2]; |
---|
| 198 | strcpy(newDir, imageDir); |
---|
| 199 | newDir[strlen(imageDir)] = '/'; |
---|
| 200 | newDir[strlen(imageDir)+1] = '\0'; |
---|
| 201 | } |
---|
[3660] | 202 | // check if the param is a Directory |
---|
[5335] | 203 | if (isDir(newDir)) |
---|
[6640] | 204 | { |
---|
| 205 | // check if the Directory has been added before |
---|
[6642] | 206 | std::vector<char*>::const_iterator imageDir; |
---|
[6640] | 207 | for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) |
---|
[3658] | 208 | { |
---|
[6640] | 209 | if (!strcmp(*imageDir, newDir)) |
---|
[6222] | 210 | { |
---|
[6640] | 211 | PRINTF(3)("Path %s already loaded\n", newDir); |
---|
| 212 | delete[] newDir; |
---|
| 213 | return true; |
---|
[6222] | 214 | } |
---|
[3658] | 215 | } |
---|
[6640] | 216 | // adding the directory to the List |
---|
| 217 | this->imageDirs.push_back(newDir); |
---|
| 218 | return true; |
---|
| 219 | } |
---|
[3658] | 220 | else |
---|
[6640] | 221 | { |
---|
| 222 | PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir); |
---|
| 223 | delete[] newDir; |
---|
| 224 | return false; |
---|
| 225 | } |
---|
[3658] | 226 | } |
---|
[4534] | 227 | #endif /* NO_TEXTURES */ |
---|
[3658] | 228 | |
---|
[3245] | 229 | /** |
---|
[6647] | 230 | * @brief loads resources |
---|
[4836] | 231 | * @param fileName: The fileName of the resource to load |
---|
| 232 | * @param prio: The ResourcePriority of this resource (will only be increased) |
---|
[6645] | 233 | * @param param0: an additional option to parse (see the constuctors for more help) |
---|
[4836] | 234 | * @param param1: an additional option to parse (see the constuctors for more help) |
---|
| 235 | * @param param2: an additional option to parse (see the constuctors for more help) |
---|
| 236 | * @returns a pointer to a desired Resource. |
---|
[3655] | 237 | */ |
---|
[6645] | 238 | BaseObject* ResourceManager::load(const char* fileName, ResourcePriority prio, |
---|
[6648] | 239 | const MultiType& param0, const MultiType& param1, const MultiType& param2) |
---|
[3655] | 240 | { |
---|
[5366] | 241 | if (fileName == NULL) |
---|
| 242 | return NULL; |
---|
[3655] | 243 | ResourceType tmpType; |
---|
[4534] | 244 | #ifndef NO_MODEL |
---|
[4637] | 245 | #define __IF_OK |
---|
[5323] | 246 | if (!strncasecmp(fileName+(strlen(fileName)-4), ".obj", 4)) |
---|
[3655] | 247 | tmpType = OBJ; |
---|
[4534] | 248 | else if (!strncmp(fileName+(strlen(fileName)-4), ".md2", 4)) |
---|
[4462] | 249 | tmpType = MD2; |
---|
[5323] | 250 | else if (!strcasecmp(fileName, "cube") || |
---|
| 251 | !strcasecmp(fileName, "sphere") || |
---|
| 252 | !strcasecmp(fileName, "plane") || |
---|
| 253 | !strcasecmp(fileName, "cylinder") || |
---|
| 254 | !strcasecmp(fileName, "cone")) |
---|
[4534] | 255 | tmpType = PRIM; |
---|
| 256 | #endif /* NO_MODEL */ |
---|
| 257 | #ifndef NO_AUDIO |
---|
[4637] | 258 | #ifdef __IF_OK |
---|
| 259 | else |
---|
| 260 | #endif |
---|
| 261 | #define __IF_OK |
---|
[6640] | 262 | if (!strncasecmp(fileName+(strlen(fileName)-4), ".wav", 4)) |
---|
| 263 | tmpType = WAV; |
---|
| 264 | else if (!strncasecmp(fileName+(strlen(fileName)-4), ".mp3", 4)) |
---|
| 265 | tmpType = MP3; |
---|
| 266 | else if (!strncasecmp(fileName+(strlen(fileName)-4), ".ogg", 4)) |
---|
| 267 | tmpType = OGG; |
---|
[4534] | 268 | #endif /* NO_AUDIO */ |
---|
| 269 | #ifndef NO_TEXT |
---|
[4637] | 270 | #ifdef __IF_OK |
---|
[6640] | 271 | else |
---|
[4637] | 272 | #endif |
---|
| 273 | #define __IF_OK |
---|
[6640] | 274 | if (!strncasecmp(fileName+(strlen(fileName)-4), ".ttf", 4)) |
---|
| 275 | tmpType = TTF; |
---|
[4534] | 276 | #endif /* NO_TEXT */ |
---|
[5323] | 277 | #ifndef NO_SHADERS |
---|
| 278 | #ifdef __IF_OK |
---|
[6640] | 279 | else |
---|
[5323] | 280 | #endif |
---|
| 281 | #define __IF_OK |
---|
[6640] | 282 | if (!strncasecmp(fileName+(strlen(fileName)-5), ".vert", 5)) |
---|
| 283 | tmpType = SHADER; |
---|
[5323] | 284 | #endif /* NO_SHADERS */ |
---|
[4534] | 285 | #ifndef NO_TEXTURES |
---|
[4637] | 286 | #ifdef __IF_OK |
---|
[6640] | 287 | else |
---|
[4637] | 288 | #else |
---|
| 289 | if |
---|
| 290 | #endif |
---|
[6640] | 291 | tmpType = IMAGE; |
---|
[4534] | 292 | #endif /* NO_TEXTURES */ |
---|
[4653] | 293 | #undef __IF_OK |
---|
[6645] | 294 | return this->load(fileName, tmpType, prio, param0, param1, param2); |
---|
[3655] | 295 | } |
---|
| 296 | |
---|
| 297 | /** |
---|
[6647] | 298 | * @brief caches a Resource |
---|
[6641] | 299 | * |
---|
| 300 | * @see load; |
---|
| 301 | * |
---|
[6650] | 302 | * @brief returns true if ok, false otherwise. |
---|
[6641] | 303 | * This function loads a Resource without applying it to an Object. |
---|
| 304 | * This is for loading purposes, e.g, when the user is loading a Resource |
---|
| 305 | * during the initialisation instead of at Runtime. |
---|
| 306 | */ |
---|
[6650] | 307 | bool ResourceManager::cache(const char* fileName, ResourceType type, ResourcePriority prio, |
---|
[6645] | 308 | const MultiType& param0, const MultiType& param1, const MultiType& param2) |
---|
[6641] | 309 | { |
---|
| 310 | assert(fileName != NULL); |
---|
| 311 | |
---|
| 312 | // searching if the resource was loaded before. |
---|
| 313 | Resource* tmpResource; |
---|
| 314 | // check if we already loaded this Resource |
---|
[6645] | 315 | tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2); |
---|
[6641] | 316 | // otherwise load it |
---|
| 317 | if (tmpResource == NULL) |
---|
[6645] | 318 | tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2); |
---|
[6641] | 319 | // return cached pointer. |
---|
| 320 | if (tmpResource != NULL) // if the resource was loaded before. |
---|
[6650] | 321 | { |
---|
[6641] | 322 | if(tmpResource->prio < prio) |
---|
| 323 | tmpResource->prio = prio; |
---|
[6650] | 324 | return true; |
---|
| 325 | } |
---|
| 326 | else |
---|
| 327 | return false; |
---|
[6641] | 328 | } |
---|
| 329 | |
---|
[6651] | 330 | /** |
---|
| 331 | * tells the ResourceManager to generate a Copy of the Resource. |
---|
| 332 | * @brief resourcePointer: The Pointer to the resource to copy |
---|
| 333 | * @returns the Resource pointed to resourcePointer. |
---|
| 334 | */ |
---|
| 335 | BaseObject* ResourceManager::copy(BaseObject* resourcePointer) |
---|
| 336 | { |
---|
| 337 | Resource* tmp = locateResourceByPointer(resourcePointer); |
---|
| 338 | if (tmp!=NULL) |
---|
| 339 | { |
---|
| 340 | tmp->count++; |
---|
| 341 | return tmp->pointer; |
---|
| 342 | } |
---|
| 343 | else |
---|
| 344 | return NULL; |
---|
| 345 | } |
---|
[6641] | 346 | |
---|
[6651] | 347 | |
---|
[6641] | 348 | /** |
---|
[6647] | 349 | * @brief loads resources |
---|
[4836] | 350 | * @param fileName: The fileName of the resource to load |
---|
[5306] | 351 | * @param type: The Type of Resource to load. |
---|
[4836] | 352 | * @param prio: The ResourcePriority of this resource (will only be increased) |
---|
[6645] | 353 | * @param param0: an additional option to parse (see the constuctors for more help) |
---|
[4836] | 354 | * @param param1: an additional option to parse (see the constuctors for more help) |
---|
| 355 | * @param param2: an additional option to parse (see the constuctors for more help) |
---|
| 356 | * @returns a pointer to a desired Resource. |
---|
[3245] | 357 | */ |
---|
[5304] | 358 | BaseObject* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio, |
---|
[6645] | 359 | const MultiType& param0, const MultiType& param1, const MultiType& param2) |
---|
[3655] | 360 | { |
---|
[6640] | 361 | assert(fileName != NULL); |
---|
[5366] | 362 | |
---|
[3658] | 363 | // searching if the resource was loaded before. |
---|
[6640] | 364 | Resource* tmpResource; |
---|
| 365 | // check if we already loaded this Resource |
---|
[6645] | 366 | tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2); |
---|
[6640] | 367 | // otherwise load it |
---|
| 368 | if (tmpResource == NULL) |
---|
[6648] | 369 | { |
---|
[6645] | 370 | tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2); |
---|
[6648] | 371 | } |
---|
[6640] | 372 | // return cached pointer. |
---|
[5306] | 373 | if (tmpResource != NULL) // if the resource was loaded before. |
---|
[6640] | 374 | { |
---|
| 375 | tmpResource->count++; |
---|
| 376 | if(tmpResource->prio < prio) |
---|
| 377 | tmpResource->prio = prio; |
---|
[6648] | 378 | |
---|
[6640] | 379 | return tmpResource->pointer; |
---|
| 380 | } |
---|
[3677] | 381 | else |
---|
[6640] | 382 | return NULL; |
---|
| 383 | } |
---|
[3658] | 384 | |
---|
[6640] | 385 | |
---|
| 386 | /** |
---|
[6647] | 387 | * @brief loads resources for internal purposes |
---|
[6640] | 388 | * @param fileName: The fileName of the resource to load |
---|
| 389 | * @param type: The Type of Resource to load. |
---|
| 390 | * @param prio: The ResourcePriority of this resource (will only be increased) |
---|
[6645] | 391 | * @param param0: an additional option to parse (see the constuctors for more help) |
---|
[6640] | 392 | * @param param1: an additional option to parse (see the constuctors for more help) |
---|
| 393 | * @param param2: an additional option to parse (see the constuctors for more help) |
---|
| 394 | * @returns a pointer to a desired Resource. |
---|
| 395 | */ |
---|
| 396 | Resource* ResourceManager::loadResource(const char* fileName, ResourceType type, ResourcePriority prio, |
---|
[6645] | 397 | const MultiType& param0, const MultiType& param1, const MultiType& param2) |
---|
[6640] | 398 | { |
---|
| 399 | // Setting up the new Resource |
---|
| 400 | Resource* tmpResource = new Resource; |
---|
| 401 | tmpResource->count = 0; |
---|
| 402 | tmpResource->type = type; |
---|
| 403 | tmpResource->prio = prio; |
---|
| 404 | tmpResource->pointer = NULL; |
---|
| 405 | tmpResource->name = new char[strlen(fileName)+1]; |
---|
| 406 | strcpy(tmpResource->name, fileName); |
---|
| 407 | |
---|
| 408 | // creating the full name. (directoryName + FileName) |
---|
| 409 | char* fullName = ResourceManager::getFullName(fileName); |
---|
| 410 | // Checking for the type of resource \see ResourceType |
---|
| 411 | switch(type) |
---|
| 412 | { |
---|
[4534] | 413 | #ifndef NO_MODEL |
---|
[6648] | 414 | case OBJ: |
---|
| 415 | if (param0.getType() != MT_NULL) |
---|
| 416 | tmpResource->param[0] = param0; |
---|
| 417 | else |
---|
| 418 | tmpResource->param[0] = 1.0f; |
---|
[3790] | 419 | |
---|
[6648] | 420 | if(ResourceManager::isFile(fullName)) |
---|
| 421 | tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat()); |
---|
| 422 | else |
---|
| 423 | { |
---|
| 424 | PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName, dataDir); |
---|
| 425 | tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat()); |
---|
| 426 | } |
---|
| 427 | break; |
---|
| 428 | case PRIM: |
---|
| 429 | if (param0 != MT_NULL) |
---|
| 430 | tmpResource->param[0] = param0; |
---|
| 431 | else |
---|
| 432 | tmpResource->param[0] = 1.0f; |
---|
[3790] | 433 | |
---|
[6648] | 434 | if (!strcmp(tmpResource->name, "cube")) |
---|
| 435 | tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat()); |
---|
| 436 | else if (!strcmp(tmpResource->name, "sphere")) |
---|
| 437 | tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat()); |
---|
| 438 | else if (!strcmp(tmpResource->name, "plane")) |
---|
| 439 | tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat()); |
---|
| 440 | else if (!strcmp(tmpResource->name, "cylinder")) |
---|
| 441 | tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat()); |
---|
| 442 | else if (!strcmp(tmpResource->name, "cone")) |
---|
| 443 | tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat()); |
---|
| 444 | break; |
---|
| 445 | case MD2: |
---|
| 446 | if(ResourceManager::isFile(fullName)) |
---|
| 447 | { |
---|
| 448 | tmpResource->param[0] = param0; |
---|
| 449 | tmpResource->pointer = new MD2Data(fullName, tmpResource->param[0].getString()); |
---|
| 450 | // tmpResource->pointer = new MD2Model(fullName, tmpResource->secFileName); |
---|
[6222] | 451 | |
---|
[6648] | 452 | } |
---|
| 453 | break; |
---|
[4534] | 454 | #endif /* NO_MODEL */ |
---|
| 455 | #ifndef NO_TEXT |
---|
[6648] | 456 | case TTF: |
---|
| 457 | if (param0 != MT_NULL) |
---|
| 458 | { |
---|
| 459 | assert(param0.getInt() >= 0); |
---|
| 460 | tmpResource->param[0] = param0; |
---|
| 461 | } |
---|
| 462 | else |
---|
| 463 | tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE; |
---|
[4597] | 464 | |
---|
[6648] | 465 | if(isFile(fullName)) |
---|
| 466 | tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt()); |
---|
| 467 | else |
---|
| 468 | PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir); |
---|
| 469 | break; |
---|
[4534] | 470 | #endif /* NO_TEXT */ |
---|
| 471 | #ifndef NO_AUDIO |
---|
[6648] | 472 | case WAV: |
---|
| 473 | if(isFile(fullName)) |
---|
| 474 | tmpResource->pointer = new SoundBuffer(fullName); |
---|
| 475 | break; |
---|
| 476 | case OGG: |
---|
| 477 | if (isFile(fullName)) |
---|
| 478 | tmpResource->pointer = new OggPlayer(fullName); |
---|
| 479 | break; |
---|
[4534] | 480 | #endif /* NO_AUDIO */ |
---|
| 481 | #ifndef NO_TEXTURES |
---|
[6648] | 482 | case IMAGE: |
---|
| 483 | if (param0 != MT_NULL) |
---|
| 484 | tmpResource->param[0] = param0; |
---|
| 485 | else |
---|
| 486 | tmpResource->param[0] = GL_TEXTURE_2D; |
---|
| 487 | if(isFile(fullName)) |
---|
[6640] | 488 | { |
---|
[6648] | 489 | PRINTF(4)("Image %s resides to %s\n", fileName, fullName); |
---|
| 490 | tmpResource->pointer = new Texture(fullName); |
---|
| 491 | } |
---|
| 492 | else |
---|
| 493 | { |
---|
| 494 | std::vector<char*>::iterator imageDir; |
---|
| 495 | for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) |
---|
[6640] | 496 | { |
---|
[6648] | 497 | char* imgName = new char[strlen(*imageDir)+strlen(fileName)+1]; |
---|
| 498 | sprintf(imgName, "%s%s", *imageDir, fileName); |
---|
| 499 | if(isFile(imgName)) |
---|
| 500 | { |
---|
| 501 | PRINTF(4)("Image %s resides to %s\n", fileName, imgName); |
---|
| 502 | tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt()); |
---|
| 503 | delete[] imgName; |
---|
| 504 | break; |
---|
| 505 | } |
---|
[6640] | 506 | delete[] imgName; |
---|
| 507 | } |
---|
| 508 | } |
---|
[6648] | 509 | if(!tmpResource) |
---|
| 510 | PRINTF(2)("!!Image %s not Found!!\n", fileName); |
---|
| 511 | break; |
---|
[4534] | 512 | #endif /* NO_TEXTURES */ |
---|
[5323] | 513 | #ifndef NO_SHADERS |
---|
[6648] | 514 | case SHADER: |
---|
| 515 | if(ResourceManager::isFile(fullName)) |
---|
[6640] | 516 | { |
---|
[6648] | 517 | if (param0 != MT_NULL) |
---|
[6640] | 518 | { |
---|
[6648] | 519 | MultiType param = param0; /// HACK |
---|
| 520 | char* secFullName = ResourceManager::getFullName(param.getString()); |
---|
| 521 | if (ResourceManager::isFile(secFullName)) |
---|
| 522 | { |
---|
| 523 | tmpResource->param[0] = secFullName; |
---|
| 524 | tmpResource->pointer = new Shader(fullName, secFullName); |
---|
| 525 | } |
---|
| 526 | delete[] secFullName; |
---|
[4597] | 527 | } |
---|
[6648] | 528 | else |
---|
| 529 | { |
---|
| 530 | tmpResource->param[0] = param0; |
---|
| 531 | tmpResource->pointer = new Shader(fullName, NULL); |
---|
| 532 | } |
---|
[6640] | 533 | } |
---|
[6648] | 534 | break; |
---|
[6640] | 535 | #endif /* NO_SHADERS */ |
---|
[6648] | 536 | default: |
---|
| 537 | tmpResource->pointer = NULL; |
---|
| 538 | PRINTF(1)("No type found for %s.\n !!This should not happen unless the Type is not supported yet. JUST DO IT!!\n", tmpResource->name); |
---|
| 539 | break; |
---|
[6640] | 540 | } |
---|
[5216] | 541 | if (tmpResource->pointer != NULL) |
---|
[6640] | 542 | this->resourceList.push_back(tmpResource); |
---|
| 543 | delete[] fullName; |
---|
| 544 | |
---|
| 545 | |
---|
| 546 | if (tmpResource->pointer != NULL) |
---|
| 547 | return tmpResource; |
---|
[4597] | 548 | else |
---|
[6640] | 549 | { |
---|
| 550 | PRINTF(2)("Resource %s could not be loaded\n", fileName); |
---|
| 551 | delete[] tmpResource->name; |
---|
| 552 | delete tmpResource; |
---|
| 553 | return NULL; |
---|
| 554 | } |
---|
[3658] | 555 | } |
---|
| 556 | |
---|
| 557 | /** |
---|
[6647] | 558 | * @brief unloads a Resource |
---|
[4836] | 559 | * @param pointer: The pointer to free |
---|
| 560 | * @param prio: the PriorityLevel to unload this resource |
---|
| 561 | * @returns true if successful (pointer found, and deleted), false otherwise |
---|
[3658] | 562 | */ |
---|
[6651] | 563 | bool ResourceManager::unload(BaseObject* pointer, ResourcePriority prio) |
---|
[3658] | 564 | { |
---|
[5366] | 565 | if (pointer == NULL) |
---|
| 566 | return false; |
---|
[3658] | 567 | // if pointer is existent. and only one resource of this type exists. |
---|
[3672] | 568 | Resource* tmpResource = this->locateResourceByPointer(pointer); |
---|
[5366] | 569 | if (tmpResource != NULL) |
---|
| 570 | return unload(tmpResource, prio); |
---|
[3660] | 571 | else |
---|
[5366] | 572 | { |
---|
| 573 | PRINTF(2)("Resource not Found %p\n", pointer); |
---|
| 574 | return false; |
---|
| 575 | } |
---|
[3660] | 576 | } |
---|
| 577 | |
---|
[4465] | 578 | /** |
---|
[6647] | 579 | * @brief unloads a Resource |
---|
[4836] | 580 | * @param resource: The resource to unloade |
---|
| 581 | * @param prio the PriorityLevel to unload this resource |
---|
[5308] | 582 | * @returns true on success, false otherwise. |
---|
[4465] | 583 | */ |
---|
[3660] | 584 | bool ResourceManager::unload(Resource* resource, ResourcePriority prio) |
---|
| 585 | { |
---|
[5306] | 586 | if (resource == NULL) |
---|
| 587 | return false; |
---|
[3665] | 588 | if (resource->count > 0) |
---|
| 589 | resource->count--; |
---|
[5306] | 590 | |
---|
[3660] | 591 | if (resource->prio <= prio) |
---|
[6640] | 592 | { |
---|
| 593 | if (resource->count == 0) |
---|
[3658] | 594 | { |
---|
[6640] | 595 | // deleting the Resource |
---|
| 596 | switch(resource->type) |
---|
| 597 | { |
---|
[4534] | 598 | #ifndef NO_MODEL |
---|
[6648] | 599 | case OBJ: |
---|
| 600 | case PRIM: |
---|
| 601 | delete (Model*)resource->pointer; |
---|
| 602 | break; |
---|
| 603 | case MD2: |
---|
| 604 | delete (MD2Data*)resource->pointer; |
---|
| 605 | break; |
---|
[4534] | 606 | #endif /* NO_MODEL */ |
---|
| 607 | #ifndef NO_AUDIO |
---|
[6648] | 608 | case WAV: |
---|
| 609 | delete (SoundBuffer*)resource->pointer; |
---|
| 610 | break; |
---|
| 611 | case OGG: |
---|
| 612 | delete (OggPlayer*)resource->pointer; |
---|
| 613 | break; |
---|
[4534] | 614 | #endif /* NO_AUDIO */ |
---|
| 615 | #ifndef NO_TEXT |
---|
[6648] | 616 | case TTF: |
---|
| 617 | delete (Font*)resource->pointer; |
---|
| 618 | break; |
---|
[4534] | 619 | #endif /* NO_TEXT */ |
---|
| 620 | #ifndef NO_TEXTURES |
---|
[6648] | 621 | case IMAGE: |
---|
| 622 | delete (Texture*)resource->pointer; |
---|
| 623 | break; |
---|
[4534] | 624 | #endif /* NO_TEXTURES */ |
---|
[5323] | 625 | #ifndef NO_SHADERS |
---|
[6648] | 626 | case SHADER: |
---|
| 627 | delete (Shader*)resource->pointer; |
---|
| 628 | break; |
---|
[5323] | 629 | #endif /* NO_SHADERS */ |
---|
[6648] | 630 | default: |
---|
| 631 | PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n"); |
---|
| 632 | return false; |
---|
| 633 | break; |
---|
[6640] | 634 | } |
---|
| 635 | // deleting the List Entry: |
---|
| 636 | PRINTF(4)("Resource %s safely removed.\n", resource->name); |
---|
| 637 | delete[] resource->name; |
---|
[6642] | 638 | std::vector<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource); |
---|
[6640] | 639 | this->resourceList.erase(resourceIT); |
---|
| 640 | delete resource; |
---|
[3658] | 641 | } |
---|
[6640] | 642 | else |
---|
| 643 | PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count); |
---|
| 644 | } |
---|
[3658] | 645 | else |
---|
[3660] | 646 | PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name); |
---|
[3658] | 647 | return true; |
---|
[3655] | 648 | } |
---|
| 649 | |
---|
[3660] | 650 | |
---|
[3655] | 651 | /** |
---|
[6647] | 652 | * @brief unloads all alocated Memory of Resources with a pririty lower than prio |
---|
[4836] | 653 | * @param prio The priority to delete |
---|
[3660] | 654 | */ |
---|
| 655 | bool ResourceManager::unloadAllByPriority(ResourcePriority prio) |
---|
| 656 | { |
---|
[6222] | 657 | unsigned int removeCount; |
---|
| 658 | for (unsigned int round = 0; round < 3; round++) |
---|
| 659 | { |
---|
[6642] | 660 | int index = this->resourceList.size() - 1; |
---|
[6222] | 661 | removeCount = 0; |
---|
[6642] | 662 | while (index >= 0) |
---|
[3660] | 663 | { |
---|
[6642] | 664 | if (this->resourceList[index]->prio <= prio) |
---|
[6640] | 665 | { |
---|
[6642] | 666 | if (this->resourceList[index]->count == 0) |
---|
| 667 | unload(this->resourceList[index], prio); |
---|
[6640] | 668 | else |
---|
[6222] | 669 | { |
---|
[6640] | 670 | PRINTF(2)("unable to unload %s because there are still %d references to it\n", |
---|
[6642] | 671 | this->resourceList[index]->name, this->resourceList[index]->count); |
---|
[6640] | 672 | removeCount++; |
---|
[6222] | 673 | } |
---|
[6640] | 674 | } |
---|
[6642] | 675 | index--; |
---|
[6640] | 676 | } |
---|
| 677 | if (removeCount == 0) break; |
---|
[6222] | 678 | } |
---|
[3660] | 679 | } |
---|
| 680 | |
---|
[5994] | 681 | |
---|
[3660] | 682 | /** |
---|
[6647] | 683 | * @brief Searches for a Resource by some information |
---|
[4836] | 684 | * @param fileName: The name to look for |
---|
| 685 | * @param type the Type of resource to locate. |
---|
[6645] | 686 | * @param param0: an additional option to parse (see the constuctors for more help) |
---|
[4836] | 687 | * @param param1: an additional option to parse (see the constuctors for more help) |
---|
| 688 | * @param param2: an additional option to parse (see the constuctors for more help) |
---|
| 689 | * @returns a Pointer to the Resource if found, NULL otherwise. |
---|
[3658] | 690 | */ |
---|
[4462] | 691 | Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type, |
---|
[6648] | 692 | const MultiType& param0, const MultiType& param1, const MultiType& param2) const |
---|
[3658] | 693 | { |
---|
[6642] | 694 | std::vector<Resource*>::const_iterator resource; |
---|
[6222] | 695 | for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++) |
---|
| 696 | { |
---|
| 697 | if ((*resource)->type == type && !strcmp(fileName, (*resource)->name)) |
---|
[6640] | 698 | { |
---|
| 699 | bool match = false; |
---|
| 700 | switch (type) |
---|
| 701 | { |
---|
[4534] | 702 | #ifndef NO_MODEL |
---|
[6648] | 703 | case PRIM: |
---|
| 704 | case OBJ: |
---|
| 705 | if (param0 == MT_NULL) |
---|
| 706 | { |
---|
| 707 | if ((*resource)->param[0] == 1.0f) |
---|
| 708 | match = true; |
---|
| 709 | } |
---|
| 710 | else if ((*resource)->param[0] == param0.getFloat()) |
---|
[6640] | 711 | match = true; |
---|
[6648] | 712 | break; |
---|
| 713 | case MD2: |
---|
| 714 | if (param0 == MT_NULL) |
---|
| 715 | { |
---|
| 716 | if ((*resource)->param[0] == "") |
---|
| 717 | match = true; |
---|
| 718 | } |
---|
| 719 | else if ((*resource)->param[0] == ((MultiType)param0).getString()) |
---|
[6640] | 720 | match = true; |
---|
[6648] | 721 | break; |
---|
[4534] | 722 | #endif /* NO_MODEL */ |
---|
| 723 | #ifndef NO_TEXT |
---|
[6648] | 724 | case TTF: |
---|
| 725 | if (param0 == MT_NULL) |
---|
| 726 | { |
---|
| 727 | if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE) |
---|
| 728 | match = true; |
---|
| 729 | } |
---|
| 730 | else if ((*resource)->param[0] == param0.getInt()) |
---|
[6640] | 731 | match = true; |
---|
[6648] | 732 | break; |
---|
[4534] | 733 | #endif /* NO_TEXT */ |
---|
[5323] | 734 | #ifndef NO_SHADERS |
---|
[6648] | 735 | case SHADER: |
---|
| 736 | if (param0 == MT_NULL) |
---|
| 737 | { |
---|
| 738 | if ((*resource)->param[0] == "") |
---|
| 739 | match = true; |
---|
| 740 | } |
---|
| 741 | else if ((*resource)->param[0] == ((MultiType)param0).getString()) |
---|
[6640] | 742 | match = true; |
---|
[5323] | 743 | #endif /* NO_SHADERS */ |
---|
[6467] | 744 | #ifndef NO_TEXTURES |
---|
[6648] | 745 | case IMAGE: |
---|
| 746 | if (param0 == MT_NULL) |
---|
| 747 | { |
---|
| 748 | if ((*resource)->param[0] == GL_TEXTURE_2D) |
---|
| 749 | match = true; |
---|
| 750 | } |
---|
| 751 | else if ((*resource)->param[0] == param0.getInt()) |
---|
[6640] | 752 | match = true; |
---|
[6648] | 753 | #endif /* NO_TEXTURES */ |
---|
| 754 | default: |
---|
[6640] | 755 | match = true; |
---|
[6648] | 756 | break; |
---|
[6640] | 757 | } |
---|
| 758 | if (match) |
---|
| 759 | { |
---|
| 760 | return (*resource); |
---|
| 761 | } |
---|
[3658] | 762 | } |
---|
[6640] | 763 | } |
---|
[3658] | 764 | return NULL; |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | /** |
---|
[6647] | 768 | * @brief Searches for a Resource by Pointer |
---|
[4836] | 769 | * @param pointer the Pointer to search for |
---|
| 770 | * @returns a Pointer to the Resource if found, NULL otherwise. |
---|
[6647] | 771 | */ |
---|
[5994] | 772 | Resource* ResourceManager::locateResourceByPointer(const void* pointer) const |
---|
[3658] | 773 | { |
---|
[3667] | 774 | // Resource* enumRes = resourceList->enumerate(); |
---|
[6642] | 775 | std::vector<Resource*>::const_iterator resource; |
---|
[6222] | 776 | for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++) |
---|
| 777 | if (pointer == (*resource)->pointer) |
---|
[6640] | 778 | return (*resource); |
---|
[3658] | 779 | return NULL; |
---|
| 780 | } |
---|
| 781 | |
---|
[6648] | 782 | char* ResourceManager::toResourcableString(unsigned int i) |
---|
| 783 | { |
---|
| 784 | int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type)); |
---|
| 785 | len += strlen(this->resourceList[i]->name); |
---|
| 786 | if (this->resourceList[i]->param[0].getString()) len += strlen(this->resourceList[i]->param[0].getString()) +1; |
---|
| 787 | if (this->resourceList[i]->param[1].getString()) len += strlen(this->resourceList[i]->param[1].getString()) +1; |
---|
| 788 | if (this->resourceList[i]->param[2].getString()) len += strlen(this->resourceList[i]->param[2].getString()) +1; |
---|
| 789 | len += 10; |
---|
| 790 | char* tmp = new char[len]; |
---|
| 791 | tmp[0] = '\0'; |
---|
| 792 | strcat( tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type)); |
---|
| 793 | strcat(tmp,","); |
---|
| 794 | strcat (tmp, this->resourceList[i]->name); |
---|
| 795 | if (this->resourceList[i]->param[0].getString() && this->resourceList[i]->param[0].getString() != '\0') |
---|
| 796 | { |
---|
| 797 | strcat(tmp,","); |
---|
| 798 | strcat( tmp, this->resourceList[i]->param[0].getString()); |
---|
| 799 | } |
---|
| 800 | if (this->resourceList[i]->param[1].getString() && this->resourceList[i]->param[1].getString() != '\0') |
---|
| 801 | { |
---|
| 802 | strcat(tmp,","); |
---|
| 803 | strcat( tmp, this->resourceList[i]->param[1].getString()); |
---|
| 804 | } |
---|
| 805 | if (this->resourceList[i]->param[2].getString() && this->resourceList[i]->param[2].getString() != '\0') |
---|
| 806 | { |
---|
| 807 | strcat(tmp,","); |
---|
| 808 | strcat( tmp, this->resourceList[i]->param[2].getString()); |
---|
| 809 | } |
---|
| 810 | return tmp; |
---|
| 811 | } |
---|
| 812 | |
---|
[3658] | 813 | /** |
---|
[6648] | 814 | * @brief caches a Resource from a ResourceableString created with the toResourcableString-function |
---|
| 815 | * @param resourceableString the String to cache the resource from. |
---|
| 816 | */ |
---|
| 817 | bool ResourceManager::fromResourceableString(const char* resourceableString) |
---|
| 818 | { |
---|
| 819 | SubString splits(resourceableString, ','); |
---|
| 820 | splits.debug(); |
---|
| 821 | if (splits.getCount() == 2) |
---|
| 822 | this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), |
---|
| 823 | RP_LEVEL); |
---|
| 824 | else if (splits.getCount() == 3) |
---|
[6650] | 825 | return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), |
---|
[6648] | 826 | RP_LEVEL, splits[2]); |
---|
| 827 | else if (splits.getCount() == 4) |
---|
[6650] | 828 | return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), |
---|
[6648] | 829 | RP_LEVEL, splits[2], splits[3]); |
---|
| 830 | else if (splits.getCount() == 5) |
---|
[6650] | 831 | return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]), |
---|
[6648] | 832 | RP_LEVEL, splits[2], splits[3], splits[4]); |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | |
---|
| 836 | /** |
---|
[6647] | 837 | * @brief Checks if it is a Directory |
---|
[4836] | 838 | * @param directoryName the Directory to check for |
---|
| 839 | * @returns true if it is a directory/symlink false otherwise |
---|
[3655] | 840 | */ |
---|
| 841 | bool ResourceManager::isDir(const char* directoryName) |
---|
| 842 | { |
---|
[4462] | 843 | if (directoryName == NULL) |
---|
| 844 | return false; |
---|
| 845 | |
---|
[3883] | 846 | char* tmpDirName = NULL; |
---|
[3655] | 847 | struct stat status; |
---|
[3883] | 848 | |
---|
| 849 | // checking for the termination of the string given. If there is a "/" at the end cut it away |
---|
[5113] | 850 | if (directoryName[strlen(directoryName)-1] == '/' || |
---|
| 851 | directoryName[strlen(directoryName)-1] == '\\') |
---|
[6640] | 852 | { |
---|
| 853 | tmpDirName = new char[strlen(directoryName)]; |
---|
| 854 | strncpy(tmpDirName, directoryName, strlen(directoryName)-1); |
---|
| 855 | tmpDirName[strlen(directoryName)-1] = '\0'; |
---|
| 856 | } |
---|
[3883] | 857 | else |
---|
[6640] | 858 | { |
---|
| 859 | tmpDirName = new char[strlen(directoryName)+1]; |
---|
| 860 | strcpy(tmpDirName, directoryName); |
---|
| 861 | } |
---|
[3883] | 862 | |
---|
[4032] | 863 | if(!stat(tmpDirName, &status)) |
---|
[6640] | 864 | { |
---|
| 865 | if (status.st_mode & (S_IFDIR |
---|
[3790] | 866 | #ifndef __WIN32__ |
---|
[6640] | 867 | | S_IFLNK |
---|
[3790] | 868 | #endif |
---|
[6640] | 869 | )) |
---|
| 870 | { |
---|
| 871 | delete[] tmpDirName; |
---|
| 872 | return true; |
---|
[3883] | 873 | } |
---|
[6640] | 874 | else |
---|
| 875 | { |
---|
| 876 | delete[] tmpDirName; |
---|
| 877 | return false; |
---|
| 878 | } |
---|
| 879 | } |
---|
[3658] | 880 | else |
---|
[5211] | 881 | { |
---|
| 882 | delete[] tmpDirName; |
---|
[4032] | 883 | return false; |
---|
[5211] | 884 | } |
---|
[3655] | 885 | } |
---|
| 886 | |
---|
| 887 | /** |
---|
[6647] | 888 | * @brief Checks if the file is either a Regular file or a Symlink |
---|
[4836] | 889 | * @param fileName the File to check for |
---|
| 890 | * @returns true if it is a regular file/symlink, false otherwise |
---|
[3655] | 891 | */ |
---|
| 892 | bool ResourceManager::isFile(const char* fileName) |
---|
| 893 | { |
---|
[4462] | 894 | if (fileName == NULL) |
---|
| 895 | return false; |
---|
[4032] | 896 | char* tmpFileName = ResourceManager::homeDirCheck(fileName); |
---|
| 897 | // actually checks the File |
---|
[3655] | 898 | struct stat status; |
---|
[4032] | 899 | if (!stat(tmpFileName, &status)) |
---|
[6640] | 900 | { |
---|
| 901 | if (status.st_mode & (S_IFREG |
---|
[3790] | 902 | #ifndef __WIN32__ |
---|
[6640] | 903 | | S_IFLNK |
---|
[3790] | 904 | #endif |
---|
[6640] | 905 | )) |
---|
| 906 | { |
---|
| 907 | delete[] tmpFileName; |
---|
| 908 | return true; |
---|
[4032] | 909 | } |
---|
[6640] | 910 | else |
---|
[4032] | 911 | { |
---|
[5208] | 912 | delete[] tmpFileName; |
---|
[4032] | 913 | return false; |
---|
| 914 | } |
---|
[6640] | 915 | } |
---|
| 916 | else |
---|
| 917 | { |
---|
| 918 | delete[] tmpFileName; |
---|
| 919 | return false; |
---|
| 920 | } |
---|
[4032] | 921 | } |
---|
| 922 | |
---|
[4166] | 923 | /** |
---|
[6647] | 924 | * @brief touches a File on the disk (thereby creating it) |
---|
[4836] | 925 | * @param fileName The file to touch |
---|
[4166] | 926 | */ |
---|
[4032] | 927 | bool ResourceManager::touchFile(const char* fileName) |
---|
| 928 | { |
---|
| 929 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
[4462] | 930 | if (tmpName == NULL) |
---|
| 931 | return false; |
---|
[4032] | 932 | FILE* stream; |
---|
| 933 | if( (stream = fopen (tmpName, "w")) == NULL) |
---|
[6640] | 934 | { |
---|
| 935 | PRINTF(1)("could not open %s fro writing\n", fileName); |
---|
| 936 | delete[] tmpName; |
---|
| 937 | return false; |
---|
| 938 | } |
---|
[4033] | 939 | fclose(stream); |
---|
[4597] | 940 | |
---|
[5208] | 941 | delete[] tmpName; |
---|
[4032] | 942 | } |
---|
| 943 | |
---|
[4166] | 944 | /** |
---|
[6647] | 945 | * @brief deletes a File from disk |
---|
[4836] | 946 | * @param fileName the File to delete |
---|
[4166] | 947 | */ |
---|
[4032] | 948 | bool ResourceManager::deleteFile(const char* fileName) |
---|
| 949 | { |
---|
[4462] | 950 | if (fileName == NULL) |
---|
| 951 | return false; |
---|
[4032] | 952 | char* tmpName = ResourceManager::homeDirCheck(fileName); |
---|
| 953 | unlink(tmpName); |
---|
[5208] | 954 | delete[] tmpName; |
---|
[4032] | 955 | } |
---|
| 956 | |
---|
[4597] | 957 | /** |
---|
[4961] | 958 | * @param name the Name of the file to check |
---|
| 959 | * @returns The name of the file, including the HomeDir |
---|
| 960 | * IMPORTANT: this has to be deleted from the outside |
---|
| 961 | */ |
---|
[4032] | 962 | char* ResourceManager::homeDirCheck(const char* name) |
---|
| 963 | { |
---|
[4462] | 964 | if (name == NULL) |
---|
| 965 | return NULL; |
---|
[4032] | 966 | char* retName; |
---|
| 967 | if (!strncmp(name, "~/", 2)) |
---|
[6640] | 968 | { |
---|
| 969 | char tmpFileName[500]; |
---|
[4032] | 970 | #ifdef __WIN32__ |
---|
[6640] | 971 | strcpy(tmpFileName, getenv("USERPROFILE")); |
---|
[4032] | 972 | #else |
---|
[6640] | 973 | strcpy(tmpFileName, getenv("HOME")); |
---|
[4032] | 974 | #endif |
---|
[6640] | 975 | retName = new char[strlen(tmpFileName)+strlen(name)]; |
---|
| 976 | sprintf(retName, "%s%s", tmpFileName, name+1); |
---|
| 977 | } |
---|
[3655] | 978 | else |
---|
[6640] | 979 | { |
---|
| 980 | retName = new char[strlen(name)+1]; |
---|
| 981 | strcpy(retName, name); |
---|
| 982 | } |
---|
[4032] | 983 | return retName; |
---|
[3655] | 984 | } |
---|
[3676] | 985 | |
---|
[4597] | 986 | /** |
---|
[4961] | 987 | * @param fileName the Name of the File to check |
---|
| 988 | * @returns The full name of the file, including the DataDir, and NULL if the file does not exist |
---|
[5219] | 989 | * !!IMPORTANT: this has to be deleted from the outside!! |
---|
[4166] | 990 | */ |
---|
| 991 | char* ResourceManager::getFullName(const char* fileName) |
---|
| 992 | { |
---|
[5335] | 993 | if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL) |
---|
[4462] | 994 | return NULL; |
---|
| 995 | |
---|
[4216] | 996 | char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir()) |
---|
[4597] | 997 | + strlen(fileName) + 1]; |
---|
[4166] | 998 | sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); |
---|
[4462] | 999 | if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName)) |
---|
[4167] | 1000 | return retName; |
---|
| 1001 | else |
---|
[6640] | 1002 | { |
---|
| 1003 | delete[] retName; |
---|
| 1004 | return NULL; |
---|
| 1005 | } |
---|
[4166] | 1006 | } |
---|
[4032] | 1007 | |
---|
| 1008 | |
---|
[3676] | 1009 | /** |
---|
[6647] | 1010 | * @brief checks wether a file is in the DataDir. |
---|
[5335] | 1011 | * @param fileName the File to check if it is in the Data-Dir structure. |
---|
| 1012 | * @returns true if the file exists, false otherwise |
---|
| 1013 | */ |
---|
| 1014 | bool ResourceManager::isInDataDir(const char* fileName) |
---|
| 1015 | { |
---|
| 1016 | if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL) |
---|
| 1017 | return false; |
---|
| 1018 | |
---|
| 1019 | bool retVal = false; |
---|
| 1020 | char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir()) |
---|
[6640] | 1021 | + strlen(fileName) + 1]; |
---|
[5335] | 1022 | sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName); |
---|
| 1023 | |
---|
| 1024 | if (ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile)) |
---|
| 1025 | retVal = true; |
---|
| 1026 | else |
---|
| 1027 | retVal = false; |
---|
| 1028 | delete[] checkFile; |
---|
| 1029 | return retVal; |
---|
| 1030 | } |
---|
| 1031 | |
---|
| 1032 | |
---|
| 1033 | /** |
---|
[6647] | 1034 | * @brief outputs debug information about the ResourceManager |
---|
| 1035 | */ |
---|
[4746] | 1036 | void ResourceManager::debug() const |
---|
[3676] | 1037 | { |
---|
| 1038 | PRINT(0)("=RM===================================\n"); |
---|
| 1039 | PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n"); |
---|
| 1040 | PRINT(0)("======================================\n"); |
---|
| 1041 | // if it is not initialized |
---|
| 1042 | PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef); |
---|
| 1043 | PRINT(0)(" Data-Directory is: %s\n", this->dataDir); |
---|
| 1044 | PRINT(0)(" List of Image-Directories: "); |
---|
[6642] | 1045 | std::vector<char*>::const_iterator imageDir; |
---|
[6222] | 1046 | for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++) |
---|
[6640] | 1047 | PRINT(0)("%s ", (*imageDir)); |
---|
[3676] | 1048 | PRINT(0)("\n"); |
---|
| 1049 | |
---|
| 1050 | PRINT(0)("List of all stored Resources:\n"); |
---|
[6642] | 1051 | std::vector<Resource*>::const_iterator resource; |
---|
[6222] | 1052 | for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++) |
---|
| 1053 | |
---|
[6640] | 1054 | { |
---|
| 1055 | PRINT(0)("-----------------------------------------\n"); |
---|
| 1056 | PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name, (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type)); |
---|
| 1057 | |
---|
| 1058 | PRINT(0)("gets deleted at "); |
---|
| 1059 | switch((*resource)->prio) |
---|
[3676] | 1060 | { |
---|
[6648] | 1061 | default: |
---|
| 1062 | case RP_NO: |
---|
| 1063 | PRINT(0)("first posibility (0)\n"); |
---|
| 1064 | break; |
---|
| 1065 | case RP_LEVEL: |
---|
| 1066 | PRINT(0)("the end of the Level (1)\n"); |
---|
| 1067 | break; |
---|
| 1068 | case RP_CAMPAIGN: |
---|
| 1069 | PRINT(0)("the end of the campaign (2)\n"); |
---|
| 1070 | break; |
---|
| 1071 | case RP_GAME: |
---|
| 1072 | PRINT(0)("when leaving the game (3)\n"); |
---|
| 1073 | break; |
---|
[3676] | 1074 | } |
---|
[6640] | 1075 | } |
---|
[3676] | 1076 | |
---|
| 1077 | |
---|
| 1078 | |
---|
| 1079 | PRINT(0)("==================================RM==\n"); |
---|
| 1080 | } |
---|
[5306] | 1081 | |
---|
| 1082 | |
---|
| 1083 | /** |
---|
[6647] | 1084 | * @brief converts a ResourceType into the corresponding String |
---|
[5306] | 1085 | * @param type the ResourceType to translate |
---|
| 1086 | * @returns the converted String. |
---|
| 1087 | */ |
---|
| 1088 | const char* ResourceManager::ResourceTypeToChar(ResourceType type) |
---|
| 1089 | { |
---|
[6646] | 1090 | return ResourceManager::resourceNames[type]; |
---|
| 1091 | } |
---|
| 1092 | |
---|
| 1093 | /** |
---|
| 1094 | * @brief converts a String into a ResourceType (good for loading) |
---|
| 1095 | * @param resourceType the name of the Type |
---|
| 1096 | * @returns the Number of the Type, or 0 (defautl) if not found. |
---|
| 1097 | */ |
---|
| 1098 | ResourceType ResourceManager::stringToResourceType(const char* resourceType) |
---|
| 1099 | { |
---|
| 1100 | assert(resourceType != NULL); |
---|
| 1101 | for (unsigned int i = 0; i < RESOURCE_TYPE_SIZE; i++) |
---|
| 1102 | if (!strcmp(resourceType, ResourceManager::resourceNames[i])) |
---|
| 1103 | return (ResourceType)i; |
---|
| 1104 | return (ResourceType)0; |
---|
| 1105 | } |
---|
| 1106 | |
---|
| 1107 | /** |
---|
| 1108 | * The Names of the ResourceTypes |
---|
| 1109 | */ |
---|
| 1110 | const char* ResourceManager::resourceNames[] = |
---|
[6648] | 1111 | { |
---|
[5306] | 1112 | #ifndef NO_MODEL |
---|
[6648] | 1113 | "ObjectModel", |
---|
| 1114 | "PrimitiveModel", |
---|
| 1115 | "MD2-Data", |
---|
[5306] | 1116 | #endif |
---|
[6648] | 1117 | #ifndef NO_TEXT |
---|
| 1118 | "Font", |
---|
[5306] | 1119 | #endif |
---|
| 1120 | #ifndef NO_AUDIO |
---|
[6648] | 1121 | "Wav", |
---|
| 1122 | "mp3", |
---|
| 1123 | "ogg", |
---|
[5306] | 1124 | #endif |
---|
[6648] | 1125 | #ifndef NO_TEXTURES |
---|
| 1126 | "Texture", |
---|
[5306] | 1127 | #endif |
---|
[5323] | 1128 | #ifndef NO_SHADERS |
---|
[6648] | 1129 | "Shader", |
---|
[5323] | 1130 | #endif |
---|
[6648] | 1131 | |
---|
| 1132 | }; |
---|