[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 |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[3655] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD |
---|
[1853] | 17 | |
---|
[3655] | 18 | #include "resource_manager.h" |
---|
[1853] | 19 | |
---|
[3655] | 20 | // different resource Types |
---|
| 21 | #include "objModel.h" |
---|
[3657] | 22 | #include "primitive_model.h" |
---|
[3655] | 23 | #include "texture.h" |
---|
[1853] | 24 | |
---|
[3658] | 25 | #include "list.h" |
---|
| 26 | |
---|
[3655] | 27 | // File Handling Includes |
---|
| 28 | #include <sys/types.h> |
---|
| 29 | #include <sys/stat.h> |
---|
| 30 | #include <unistd.h> |
---|
| 31 | |
---|
[1856] | 32 | using namespace std; |
---|
[1853] | 33 | |
---|
[3245] | 34 | /** |
---|
| 35 | \brief standard constructor |
---|
| 36 | */ |
---|
[3655] | 37 | ResourceManager::ResourceManager () |
---|
[3365] | 38 | { |
---|
[3655] | 39 | this->setClassName ("ResourceManager"); |
---|
[3658] | 40 | dataDir = NULL; |
---|
| 41 | imageDirs = new tList<char>(); |
---|
| 42 | resourceList = new tList<Resource>(); |
---|
[3365] | 43 | } |
---|
[1853] | 44 | |
---|
[3660] | 45 | /** |
---|
| 46 | \returns the Instance to this ResourceManager |
---|
| 47 | */ |
---|
[3655] | 48 | ResourceManager* ResourceManager::getInstance(void) |
---|
| 49 | { |
---|
| 50 | if (!ResourceManager::singletonRef) |
---|
| 51 | ResourceManager::singletonRef = new ResourceManager(); |
---|
| 52 | return ResourceManager::singletonRef; |
---|
| 53 | } |
---|
[1853] | 54 | |
---|
[3658] | 55 | //! Singleton Reference to the ResourceManager |
---|
[3655] | 56 | ResourceManager* ResourceManager::singletonRef = NULL; |
---|
[3658] | 57 | //! The List of Resources, that has already been loaded. |
---|
| 58 | tList<Resource>* ResourceManager::resourceList = NULL; |
---|
| 59 | //! The Data Directory, where all relevant Data is stored. |
---|
| 60 | char* ResourceManager::dataDir = NULL; |
---|
| 61 | //! A list of directories in which images are stored. |
---|
| 62 | tList<char>* ResourceManager::imageDirs = NULL; |
---|
[3655] | 63 | |
---|
[3245] | 64 | /** |
---|
[3660] | 65 | \brief standard destructor |
---|
[3655] | 66 | */ |
---|
| 67 | ResourceManager::~ResourceManager (void) |
---|
| 68 | { |
---|
[3660] | 69 | // deleting the Resources-List |
---|
| 70 | unloadAllByPriority(RP_GAME); |
---|
[3658] | 71 | delete resourceList; |
---|
| 72 | resourceList = NULL; |
---|
[3660] | 73 | // deleting the Directorie Lists |
---|
| 74 | char* tmpDir = imageDirs->enumerate(); |
---|
| 75 | while(tmpDir) |
---|
| 76 | { |
---|
| 77 | delete []tmpDir; |
---|
| 78 | tmpDir = imageDirs->nextElement(); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | delete imageDirs; |
---|
| 82 | imageDirs = NULL; |
---|
[3655] | 83 | ResourceManager::singletonRef = NULL; |
---|
| 84 | } |
---|
[1853] | 85 | |
---|
[3655] | 86 | /** |
---|
| 87 | \brief sets the data main directory |
---|
| 88 | \param dataDir the DataDirectory. |
---|
[3245] | 89 | */ |
---|
[3655] | 90 | bool ResourceManager::setDataDir(char* dataDir) |
---|
[3543] | 91 | { |
---|
[3655] | 92 | if (isDir(dataDir)) |
---|
| 93 | { |
---|
[3658] | 94 | ResourceManager::dataDir = new char[strlen(dataDir)+1]; |
---|
| 95 | strcpy(ResourceManager::dataDir, dataDir); |
---|
[3655] | 96 | } |
---|
| 97 | else |
---|
| 98 | { |
---|
| 99 | PRINTF(1)("%s is not a Directory, and can not be the Data Directory\n", dataDir); |
---|
| 100 | } |
---|
[3543] | 101 | } |
---|
[1853] | 102 | |
---|
[3660] | 103 | /** |
---|
| 104 | \brief adds a new Path for Images |
---|
| 105 | \param imageDir The path to insert |
---|
| 106 | \returns true, if the Path was well and injected (or already existent within the list) |
---|
| 107 | false otherwise |
---|
| 108 | */ |
---|
[3658] | 109 | bool ResourceManager::addImageDir(char* imageDir) |
---|
| 110 | { |
---|
[3660] | 111 | // check if the param is a Directory |
---|
[3658] | 112 | if (isDir(imageDir)) |
---|
| 113 | { |
---|
[3660] | 114 | // check if the Directory has been added before |
---|
| 115 | char* tmpDir = imageDirs->enumerate(); |
---|
| 116 | while(tmpDir) |
---|
| 117 | { |
---|
| 118 | if (!strcmp(tmpDir, imageDir)) |
---|
| 119 | { |
---|
| 120 | PRINTF(4)("Path %s already loaded\n", imageDir); |
---|
| 121 | return true; |
---|
| 122 | } |
---|
| 123 | tmpDir = imageDirs->nextElement(); |
---|
| 124 | } |
---|
| 125 | // adding the directory to the List |
---|
| 126 | tmpDir = new char[strlen(imageDir)+1]; |
---|
[3658] | 127 | strcpy(tmpDir, imageDir); |
---|
| 128 | imageDirs->add(tmpDir); |
---|
[3660] | 129 | return true; |
---|
[3658] | 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", dataDir); |
---|
[3660] | 134 | return false; |
---|
[3658] | 135 | } |
---|
| 136 | } |
---|
| 137 | |
---|
[3245] | 138 | /** |
---|
[3655] | 139 | \brief loads resources |
---|
| 140 | \param fileName The fileName of the resource to load |
---|
[3660] | 141 | \param prio The ResourcePriority of this resource (will only be increased) |
---|
[3655] | 142 | \returns a pointer to a desired Resource. |
---|
| 143 | */ |
---|
[3660] | 144 | void* ResourceManager::load(const char* fileName, ResourcePriority prio) |
---|
[3655] | 145 | { |
---|
| 146 | ResourceType tmpType; |
---|
| 147 | if (!strncmp(fileName+(strlen(fileName)-4), ".obj", 4)) |
---|
| 148 | tmpType = OBJ; |
---|
[3658] | 149 | else if (!strncmp(fileName+(strlen(fileName)-4), ".wav", 4)) |
---|
| 150 | tmpType = WAV; |
---|
| 151 | else if (!strncmp(fileName+(strlen(fileName)-4), ".mp3", 4)) |
---|
| 152 | tmpType = MP3; |
---|
| 153 | else if (!strncmp(fileName+(strlen(fileName)-4), ".ogg", 4)) |
---|
| 154 | tmpType = OGG; |
---|
| 155 | else if (!strcmp(fileName, "cube") || |
---|
| 156 | !strcmp(fileName, "sphere") || |
---|
| 157 | !strcmp(fileName, "plane") || |
---|
| 158 | !strcmp(fileName, "cylinder") || |
---|
| 159 | !strcmp(fileName, "cone")) |
---|
| 160 | tmpType = PRIM; |
---|
| 161 | else |
---|
| 162 | tmpType = IMAGE; |
---|
[3245] | 163 | |
---|
[3660] | 164 | return ResourceManager::load(fileName, tmpType, prio); |
---|
[3655] | 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | \brief loads resources |
---|
| 169 | \param fileName The fileName of the resource to load |
---|
| 170 | \param type The Type of Resource to load (\see ResourceType) |
---|
[3660] | 171 | \param prio The ResourcePriority of this resource (will only be increased) |
---|
[3655] | 172 | \returns a pointer to a desired Resource. |
---|
[3245] | 173 | */ |
---|
[3660] | 174 | void* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio) |
---|
[3655] | 175 | { |
---|
[3658] | 176 | // searching if the resource was loaded before. |
---|
| 177 | Resource* tmpResource = ResourceManager::locateResourceByName(fileName); |
---|
| 178 | if (!tmpResource) // if the resource was not loaded before. |
---|
[3655] | 179 | { |
---|
[3660] | 180 | char* tmpDir; |
---|
[3658] | 181 | // Setting up the new Resource |
---|
| 182 | tmpResource = new Resource; |
---|
| 183 | tmpResource->count = 1; |
---|
| 184 | tmpResource->type = type; |
---|
[3660] | 185 | tmpResource->prio = prio; |
---|
[3658] | 186 | tmpResource->name = new char[strlen(fileName)+1]; |
---|
| 187 | strcpy(tmpResource->name, fileName); |
---|
| 188 | |
---|
| 189 | // creating the full name. (directoryName + FileName) |
---|
| 190 | char* fullName = new char[strlen(dataDir)+strlen(fileName)+1]; |
---|
| 191 | sprintf(fullName, "%s%s", dataDir, fileName); |
---|
| 192 | |
---|
| 193 | // Checking for the type of resource \see ResourceType |
---|
| 194 | switch(type) |
---|
[3655] | 195 | { |
---|
[3658] | 196 | case OBJ: |
---|
| 197 | if(isFile(fullName)) |
---|
| 198 | tmpResource->pointer = new OBJModel(fullName); |
---|
| 199 | else |
---|
| 200 | { |
---|
| 201 | PRINTF(2)("Sorry, %s does not exist. Loading a cube-Model instead\n", fullName); |
---|
| 202 | tmpResource->pointer = ResourceManager::load("cube", PRIM); |
---|
| 203 | } |
---|
| 204 | break; |
---|
| 205 | case PRIM: |
---|
| 206 | if (!strcmp(tmpResource->name, "cube")) |
---|
| 207 | tmpResource->pointer = new PrimitiveModel(CUBE); |
---|
| 208 | else if (!strcmp(tmpResource->name, "sphere")) |
---|
| 209 | tmpResource->pointer = new PrimitiveModel(SPHERE); |
---|
| 210 | else if (!strcmp(tmpResource->name, "plane")) |
---|
| 211 | tmpResource->pointer = new PrimitiveModel(PLANE); |
---|
| 212 | else if (!strcmp(tmpResource->name, "cylinder")) |
---|
| 213 | tmpResource->pointer = new PrimitiveModel(CYLINDER); |
---|
| 214 | else if (!strcmp(tmpResource->name, "cone")) |
---|
| 215 | tmpResource->pointer = new PrimitiveModel(CONE); |
---|
| 216 | break; |
---|
| 217 | case IMAGE: |
---|
| 218 | if(isFile(fullName)) |
---|
| 219 | { |
---|
| 220 | tmpResource->pointer = new Texture(fullName); |
---|
| 221 | } |
---|
| 222 | else |
---|
| 223 | { |
---|
| 224 | tmpDir = imageDirs->enumerate(); |
---|
| 225 | while(tmpDir) |
---|
| 226 | { |
---|
| 227 | char* imgName = new char[strlen(tmpDir)+strlen(fileName)+1]; |
---|
| 228 | sprintf(imgName, "%s%s", tmpDir, fileName); |
---|
| 229 | if(isFile(imgName)) |
---|
| 230 | tmpResource->pointer = new Texture(imgName); |
---|
| 231 | delete []imgName; |
---|
| 232 | tmpDir = imageDirs->nextElement(); |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | if(!tmpResource) |
---|
| 236 | PRINTF(2)("!!Image %s not Found!!\n", fileName); |
---|
| 237 | break; |
---|
| 238 | default: |
---|
| 239 | tmpResource->pointer = NULL; |
---|
| 240 | PRINTF(1)("No type found for %s.\n !!This should not happen unless the Type is not supported yet.!!\n", tmpResource->name); |
---|
| 241 | break; |
---|
[3655] | 242 | } |
---|
[3658] | 243 | |
---|
| 244 | // checking if the File really exists. |
---|
| 245 | if(!isFile(fullName)) |
---|
[3655] | 246 | { |
---|
[3658] | 247 | PRINTF(2)("Sorry, %s is not a regular file.\n", fullName); |
---|
| 248 | tmpResource->pointer = NULL; |
---|
[3655] | 249 | } |
---|
[3658] | 250 | resourceList->add(tmpResource); |
---|
| 251 | delete []fullName; |
---|
[3655] | 252 | } |
---|
[3658] | 253 | else |
---|
| 254 | { |
---|
[3665] | 255 | PRINTF(4)("not loading cached resource %s\n", tmpResource->name); |
---|
[3658] | 256 | tmpResource->count++; |
---|
[3660] | 257 | if(tmpResource->prio < prio) |
---|
| 258 | tmpResource->prio = prio; |
---|
[3658] | 259 | } |
---|
[3655] | 260 | |
---|
[3658] | 261 | return tmpResource->pointer; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | \brief unloads a Resource |
---|
| 266 | \param pointer The pointer to free |
---|
| 267 | \returns true if successful (pointer found, and deleted), false otherwise |
---|
| 268 | |
---|
| 269 | */ |
---|
[3660] | 270 | bool ResourceManager::unload(void* pointer, ResourcePriority prio) |
---|
[3658] | 271 | { |
---|
| 272 | // if pointer is existent. and only one resource of this type exists. |
---|
[3660] | 273 | Resource* tmpResource =ResourceManager::locateResourceByPointer(pointer); |
---|
[3658] | 274 | if (!tmpResource) |
---|
[3655] | 275 | { |
---|
[3658] | 276 | PRINTF(2)("Resource not Found %p\n", pointer); |
---|
| 277 | return false; |
---|
[3655] | 278 | } |
---|
[3660] | 279 | else |
---|
| 280 | unload(tmpResource, prio); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | bool ResourceManager::unload(Resource* resource, ResourcePriority prio) |
---|
| 284 | { |
---|
[3665] | 285 | if (resource->count > 0) |
---|
| 286 | resource->count--; |
---|
[3660] | 287 | if (resource->prio <= prio) |
---|
[3658] | 288 | { |
---|
[3660] | 289 | if (resource->count <= 0) |
---|
[3658] | 290 | { |
---|
[3660] | 291 | // deleting the Resource |
---|
| 292 | switch(resource->type) |
---|
| 293 | { |
---|
| 294 | case OBJ: |
---|
| 295 | case PRIM: |
---|
| 296 | delete (Model*)resource->pointer; |
---|
| 297 | break; |
---|
| 298 | case IMAGE: |
---|
| 299 | delete (Texture*)resource->pointer; |
---|
| 300 | break; |
---|
| 301 | default: |
---|
| 302 | PRINTF(1)("NOT YET IMPLEMENTED !!FIX FIX!!\n"); |
---|
| 303 | return false; |
---|
| 304 | break; |
---|
| 305 | } |
---|
| 306 | // deleting the List Entry: |
---|
| 307 | PRINTF(4)("Resource %s safely removed.\n", resource->name); |
---|
| 308 | delete []resource->name; |
---|
| 309 | resourceList->remove(resource); |
---|
[3658] | 310 | } |
---|
[3660] | 311 | else |
---|
| 312 | PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count); |
---|
[3658] | 313 | } |
---|
| 314 | else |
---|
[3660] | 315 | PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name); |
---|
[3658] | 316 | return true; |
---|
[3655] | 317 | } |
---|
| 318 | |
---|
[3660] | 319 | |
---|
[3655] | 320 | /** |
---|
[3660] | 321 | \brief unloads all alocated Memory of Resources with a pririty lower than prio |
---|
| 322 | \param prio The priority to delete |
---|
| 323 | */ |
---|
| 324 | bool ResourceManager::unloadAllByPriority(ResourcePriority prio) |
---|
| 325 | { |
---|
| 326 | Resource* enumRes = resourceList->enumerate(); |
---|
| 327 | while (enumRes) |
---|
| 328 | { |
---|
| 329 | if (enumRes->prio <= prio) |
---|
| 330 | unload(enumRes, prio); |
---|
| 331 | enumRes = resourceList->nextElement(); |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | /** |
---|
[3658] | 336 | \brief Searches for a Resource by Name |
---|
| 337 | \param fileName The name to look for |
---|
| 338 | \returns a Pointer to the Resource if found, NULL otherwise. |
---|
| 339 | */ |
---|
| 340 | Resource* ResourceManager::locateResourceByName(const char* fileName) |
---|
| 341 | { |
---|
| 342 | Resource* enumRes = resourceList->enumerate(); |
---|
| 343 | while (enumRes) |
---|
| 344 | { |
---|
| 345 | if (!strcmp(fileName, enumRes->name)) |
---|
| 346 | return enumRes; |
---|
| 347 | enumRes = resourceList->nextElement(); |
---|
| 348 | } |
---|
| 349 | return NULL; |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | /** |
---|
| 353 | \brief Searches for a Resource by Pointer |
---|
| 354 | \param pointer the Pointer to search for |
---|
| 355 | \returns a Pointer to the Resource if found, NULL otherwise. |
---|
| 356 | */ |
---|
| 357 | Resource* ResourceManager::locateResourceByPointer(const void* pointer) |
---|
| 358 | { |
---|
| 359 | Resource* enumRes = resourceList->enumerate(); |
---|
| 360 | while (enumRes) |
---|
| 361 | { |
---|
[3665] | 362 | if (pointer == enumRes->pointer) |
---|
[3658] | 363 | return enumRes; |
---|
| 364 | enumRes = resourceList->nextElement(); |
---|
| 365 | } |
---|
| 366 | return NULL; |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | /** |
---|
[3655] | 370 | \brief Checks if it is a Directory |
---|
| 371 | \param directoryName the Directory to check for |
---|
| 372 | \returns true if it is a directory/symlink false otherwise |
---|
| 373 | */ |
---|
| 374 | bool ResourceManager::isDir(const char* directoryName) |
---|
| 375 | { |
---|
| 376 | struct stat status; |
---|
| 377 | stat(directoryName, &status); |
---|
| 378 | if (status.st_mode & (S_IFDIR | S_IFLNK)) |
---|
| 379 | return true; |
---|
[3658] | 380 | else |
---|
[3655] | 381 | return false; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /** |
---|
| 385 | \brief Checks if the file is either a Regular file or a Symlink |
---|
| 386 | \param fileName the File to check for |
---|
| 387 | \returns true if it is a regular file/symlink, false otherwise |
---|
| 388 | */ |
---|
| 389 | bool ResourceManager::isFile(const char* fileName) |
---|
| 390 | { |
---|
| 391 | struct stat status; |
---|
| 392 | stat(fileName, &status); |
---|
| 393 | if (status.st_mode & (S_IFREG | S_IFLNK)) |
---|
| 394 | return true; |
---|
| 395 | else |
---|
| 396 | return false; |
---|
| 397 | } |
---|