Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/loading/resource_manager.cc @ 8336

Last change on this file since 8336 was 8330, checked in by bensch, 18 years ago

moved the File-Classes

File size: 26.5 KB
RevLine 
[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
[7193]18#include "util/loading/resource_manager.h"
[6648]19#include "substring.h"
[4642]20#include "debug.h"
21
[6222]22#include <algorithm>
[6655]23#include <assert.h>
[6222]24
[3655]25// different resource Types
[4534]26#ifndef NO_MODEL
[3655]27#include "objModel.h"
[3657]28#include "primitive_model.h"
[4462]29#include "md2Model.h"
[4534]30#endif /* NO_MODEL */
31#ifndef NO_TEXTURES
[3655]32#include "texture.h"
[4534]33#endif /* NO_TEXTURES */
34#ifndef NO_TEXT
[5344]35#include "font.h"
[4534]36#endif /* NO_TEXT */
37#ifndef NO_AUDIO
[5930]38#include "sound_buffer.h"
[4961]39#include "ogg_player.h"
[4534]40#endif /* NO_AUDIO */
[5323]41#ifndef NO_SHADERS
42#include "shader.h"
43#endif /* NO_SHADERS */
[1853]44
[3655]45// File Handling Includes
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <unistd.h>
49
[1856]50using namespace std;
[1853]51
[3245]52/**
[6647]53 * @brief standard constructor
[3245]54*/
[4597]55ResourceManager::ResourceManager ()
[3365]56{
[4597]57  this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager");
58  this->setName("ResourceManager");
59
[7221]60  this->dataDir = "./";
[5480]61  this->tryDataDir("./data");
[3365]62}
[1853]63
[3658]64//! Singleton Reference to the ResourceManager
[3655]65ResourceManager* ResourceManager::singletonRef = NULL;
66
[3245]67/**
[6647]68 * @brief standard destructor
[3655]69*/
[4746]70ResourceManager::~ResourceManager ()
[3655]71{
[3660]72  // deleting the Resources-List
[3672]73  this->unloadAllByPriority(RP_GAME);
[5303]74
[6222]75  if (!this->resourceList.empty())
76    PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size());
[5303]77
[3655]78  ResourceManager::singletonRef = NULL;
79}
[1853]80
[3655]81/**
[6647]82 * @brief sets the data main directory
[4836]83 * @param dataDir the DataDirectory.
[5480]84 */
[7221]85bool ResourceManager::setDataDir(const std::string& dataDir)
[3543]86{
[7661]87  File dataDirectory(dataDir);
88  if (dataDirectory.isDirectory())
[5480]89  {
[7661]90    this->dataDir = dataDirectory.name();
91
92    if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\')
[3655]93    {
[7221]94      this->dataDir += '/';
[5480]95    }
96    return true;
97  }
[3655]98  else
[5480]99  {
[7661]100    PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", dataDir.c_str(), this->dataDir.c_str());
[5480]101    return false;
102  }
103}
104
105/**
[6647]106 * @brief sets the data main directory
[5480]107 * @param dataDir the DataDirectory.
108 *
109 * this is essentially the same as setDataDir, but it ommits the error-message
110 */
[7221]111bool ResourceManager::tryDataDir(const std::string& dataDir)
[5480]112{
[7661]113  File dataDirectory(dataDir);
114  if (dataDirectory.isDirectory())
[5480]115  {
[7661]116    this->dataDir = dataDirectory.name();
117
118    if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\')
[3655]119    {
[7221]120      this->dataDir += '/';
[5480]121    }
122    return true;
123  }
[5482]124  return false;
[3543]125}
[1853]126
[5480]127
[3660]128/**
[6647]129 * @brief checks for the DataDirectory, by looking if
[5480]130 * @param fileInside is iniside of the given directory.
[4091]131*/
[7221]132bool ResourceManager::verifyDataDir(const std::string& fileInside)
[4091]133{
[7661]134  File dataDirectory(this->dataDir);
135  if (!dataDirectory.isDirectory())
[6640]136  {
[7661]137    PRINTF(1)("'%s' is not a directory\n", this->dataDir.c_str());
[6640]138    return false;
139  }
[4597]140
[7661]141  File testFile(this->dataDir + fileInside);
142  return testFile.isFile();
[4091]143}
144
[4653]145#ifndef NO_TEXTURES
[4091]146/**
[6647]147 * @brief adds a new Path for Images
[4836]148 * @param imageDir The path to insert
149 * @returns true, if the Path was well and injected (or already existent within the list)
[3660]150   false otherwise
151*/
[7221]152bool ResourceManager::addImageDir(const std::string& imageDir)
[3658]153{
[7661]154  std::string newDir = imageDir;
155  if (imageDir[imageDir.size()-1] != '/' && imageDir[imageDir.size()-1] != '\\')
[5335]156  {
[7221]157    newDir += '/';
[5335]158  }
[3660]159  // check if the param is a Directory
[7661]160  if (File(newDir).isDirectory())
[6640]161  {
162    // check if the Directory has been added before
[7221]163    std::vector<std::string>::const_iterator imageDir;
[6640]164    for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
[3658]165    {
[7221]166      if (*imageDir == newDir)
[6222]167      {
[7221]168        PRINTF(3)("Path %s already loaded\n", newDir.c_str());
[6640]169        return true;
[6222]170      }
[3658]171    }
[6640]172    // adding the directory to the List
173    this->imageDirs.push_back(newDir);
174    return true;
175  }
[3658]176  else
[6640]177  {
[7221]178    PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir.c_str());
[6640]179    return false;
180  }
[3658]181}
[4534]182#endif /* NO_TEXTURES */
[3658]183
[3245]184/**
[6647]185 * @brief loads resources
[4836]186 * @param fileName: The fileName of the resource to load
187 * @param prio: The ResourcePriority of this resource (will only be increased)
[6645]188 * @param param0: an additional option to parse (see the constuctors for more help)
[4836]189 * @param param1: an additional option to parse (see the constuctors for more help)
190 * @param param2: an additional option to parse (see the constuctors for more help)
191 * @returns a pointer to a desired Resource.
[3655]192*/
[7221]193BaseObject* ResourceManager::load(const std::string& fileName, ResourcePriority prio,
[6648]194                                  const MultiType& param0, const MultiType& param1, const MultiType& param2)
[3655]195{
196  ResourceType tmpType;
[4534]197#ifndef NO_MODEL
[4637]198#define __IF_OK
[7221]199  if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".obj", 4))
[3655]200    tmpType = OBJ;
[7221]201  else if (!strncmp(fileName.c_str()+(fileName.size()-4), ".md2", 4))
[4462]202    tmpType = MD2;
[7221]203  else if (!strcasecmp(fileName.c_str(), "cube") ||
204            !strcasecmp(fileName.c_str(), "sphere") ||
205            !strcasecmp(fileName.c_str(), "plane") ||
206            !strcasecmp(fileName.c_str(), "cylinder") ||
207            !strcasecmp(fileName.c_str(), "cone"))
[4534]208    tmpType = PRIM;
209#endif /* NO_MODEL */
210#ifndef NO_AUDIO
[4637]211#ifdef __IF_OK
212  else
213#endif
214#define __IF_OK
[7221]215    if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".wav", 4))
[6640]216      tmpType = WAV;
[7221]217    else if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".mp3", 4))
[6640]218      tmpType = MP3;
[7221]219    else if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".ogg", 4))
[6640]220      tmpType = OGG;
[4534]221#endif /* NO_AUDIO */
222#ifndef NO_TEXT
[4637]223#ifdef __IF_OK
[6640]224    else
[4637]225#endif
226#define __IF_OK
[7221]227      if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".ttf", 4))
[6640]228        tmpType = TTF;
[4534]229#endif /* NO_TEXT */
[5323]230#ifndef NO_SHADERS
231#ifdef __IF_OK
[6640]232      else
[5323]233#endif
234#define __IF_OK
[7221]235        if (!strncasecmp(fileName.c_str()+(fileName.size()-5), ".vert", 5))
[6640]236          tmpType = SHADER;
[5323]237#endif /* NO_SHADERS */
[4534]238#ifndef NO_TEXTURES
[4637]239#ifdef __IF_OK
[6640]240        else
[4637]241#else
242  if
243#endif
[6640]244          tmpType = IMAGE;
[4534]245#endif /* NO_TEXTURES */
[4653]246#undef __IF_OK
[6645]247  return this->load(fileName, tmpType, prio, param0, param1, param2);
[3655]248}
249
250/**
[6647]251 * @brief caches a Resource
[6641]252 *
253 * @see load;
254 *
[6650]255 * @brief returns true if ok, false otherwise.
[6641]256 * This function loads a Resource without applying it to an Object.
257 * This is for loading purposes, e.g, when the user is loading a Resource
258 * during the initialisation instead of at Runtime.
259 */
[7221]260bool ResourceManager::cache(const std::string& fileName, ResourceType type, ResourcePriority prio,
[6645]261                            const MultiType& param0, const MultiType& param1, const MultiType& param2)
[6641]262{
263  // searching if the resource was loaded before.
264  Resource* tmpResource;
265  // check if we already loaded this Resource
[6645]266  tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
[6641]267  // otherwise load it
268  if (tmpResource == NULL)
[6645]269    tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
[6641]270  // return cached pointer.
271  if (tmpResource != NULL) // if the resource was loaded before.
[6650]272  {
[6641]273    if(tmpResource->prio < prio)
274      tmpResource->prio = prio;
[6650]275    return true;
276  }
277  else
278    return false;
[6641]279}
280
[6651]281/**
282 * tells the ResourceManager to generate a Copy of the Resource.
283 * @brief resourcePointer: The Pointer to the resource to copy
284 * @returns the Resource pointed to resourcePointer.
285 */
286BaseObject* ResourceManager::copy(BaseObject* resourcePointer)
287{
288  Resource* tmp = locateResourceByPointer(resourcePointer);
289  if (tmp!=NULL)
290  {
291    tmp->count++;
292    return tmp->pointer;
293  }
294  else
295    return NULL;
296}
[6641]297
[6651]298
[6641]299/**
[6647]300 * @brief loads resources
[4836]301 * @param fileName: The fileName of the resource to load
[5306]302 * @param type: The Type of Resource to load.
[4836]303 * @param prio: The ResourcePriority of this resource (will only be increased)
[6645]304 * @param param0: an additional option to parse (see the constuctors for more help)
[4836]305 * @param param1: an additional option to parse (see the constuctors for more help)
306 * @param param2: an additional option to parse (see the constuctors for more help)
307 * @returns a pointer to a desired Resource.
[3245]308*/
[7221]309BaseObject* ResourceManager::load(const std::string& fileName, ResourceType type, ResourcePriority prio,
[6645]310                                  const MultiType& param0, const MultiType& param1, const MultiType& param2)
[3655]311{
[5366]312
[3658]313  // searching if the resource was loaded before.
[6640]314  Resource* tmpResource;
315  // check if we already loaded this Resource
[6645]316  tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
[6640]317  // otherwise load it
318  if (tmpResource == NULL)
[6648]319  {
[6645]320    tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
[6648]321  }
[6640]322  // return cached pointer.
[5306]323  if (tmpResource != NULL) // if the resource was loaded before.
[6640]324  {
325    tmpResource->count++;
326    if(tmpResource->prio < prio)
327      tmpResource->prio = prio;
[6648]328
[6640]329    return tmpResource->pointer;
330  }
[3677]331  else
[6640]332    return NULL;
333}
[3658]334
[6640]335
336/**
[6647]337 * @brief loads resources for internal purposes
[6640]338 * @param fileName: The fileName of the resource to load
339 * @param type: The Type of Resource to load.
340 * @param prio: The ResourcePriority of this resource (will only be increased)
[6645]341 * @param param0: an additional option to parse (see the constuctors for more help)
[6640]342 * @param param1: an additional option to parse (see the constuctors for more help)
343 * @param param2: an additional option to parse (see the constuctors for more help)
344 * @returns a pointer to a desired Resource.
345 */
[7221]346Resource* ResourceManager::loadResource(const std::string& fileName, ResourceType type, ResourcePriority prio,
[6645]347                                        const MultiType& param0, const MultiType& param1, const MultiType& param2)
[6640]348{
349  // Setting up the new Resource
350  Resource* tmpResource = new Resource;
351  tmpResource->count = 0;
352  tmpResource->type = type;
353  tmpResource->prio = prio;
354  tmpResource->pointer = NULL;
[7221]355  tmpResource->name = fileName;
[6640]356
357  // creating the full name. (directoryName + FileName)
[7221]358  std::string fullName = ResourceManager::getFullName(fileName);
[6640]359  // Checking for the type of resource \see ResourceType
360  switch(type)
361  {
[4534]362#ifndef NO_MODEL
[6648]363    case OBJ:
364      if (param0.getType() != MT_NULL)
365        tmpResource->param[0] = param0;
366      else
367        tmpResource->param[0] = 1.0f;
[3790]368
[7661]369      if(File(fullName).isFile())
[6648]370        tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat());
371      else
372      {
[7221]373        PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName.c_str(), dataDir.c_str());
[6648]374        tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat());
375      }
376      break;
377    case PRIM:
378      if (param0 != MT_NULL)
379        tmpResource->param[0] = param0;
380      else
381        tmpResource->param[0] = 1.0f;
[3790]382
[7221]383      if (tmpResource->name == "cube")
[6648]384        tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat());
[7221]385      else if (tmpResource->name == "sphere")
[6648]386        tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat());
[7221]387      else if (tmpResource->name == "plane")
[6648]388        tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat());
[7221]389      else if (tmpResource->name == "cylinder")
[6648]390        tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat());
[7221]391      else if (tmpResource->name == "cone")
[6648]392        tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat());
393      break;
394    case MD2:
[7661]395      if(File(fullName).isFile())
[6648]396      {
397        tmpResource->param[0] = param0;
[7059]398        tmpResource->param[1] = param1;
[7199]399        tmpResource->pointer = new MD2Data(fullName, tmpResource->param[0].getCString(), tmpResource->param[1].getFloat());
[6648]400      }
401      break;
[4534]402#endif /* NO_MODEL */
403#ifndef NO_TEXT
[6648]404    case TTF:
405      if (param0 != MT_NULL)
406      {
407        assert(param0.getInt() >= 0);
408        tmpResource->param[0] = param0;
409      }
410      else
411        tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE;
[4597]412
[7661]413      if(File(fullName).isFile())
[6648]414        tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt());
415      else
[7221]416        PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName.c_str(), this->dataDir.c_str());
[6648]417      break;
[4534]418#endif /* NO_TEXT */
419#ifndef NO_AUDIO
[6648]420    case WAV:
[7661]421      if(File(fullName).isFile())
[7460]422        tmpResource->pointer = new OrxSound::SoundBuffer(fullName);
[6648]423      break;
424    case OGG:
[7661]425      if (File(fullName).isFile())
[7460]426        tmpResource->pointer = new OrxSound::OggPlayer(fullName);
[6648]427      break;
[4534]428#endif /* NO_AUDIO */
429#ifndef NO_TEXTURES
[6648]430    case IMAGE:
431      if (param0 != MT_NULL)
432        tmpResource->param[0] = param0;
433      else
434        tmpResource->param[0] = GL_TEXTURE_2D;
[7661]435      if(File(fullName).isFile())
[6640]436      {
[7676]437        PRINTF(4)("Image %s resides to %s\n", fileName.c_str(), fullName.c_str());
[6859]438        tmpResource->pointer = new Texture(fullName, tmpResource->param[0].getInt());
[6648]439      }
440      else
441      {
[7221]442        std::vector<std::string>::iterator imageDir;
[6648]443        for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
[6640]444        {
[7221]445          std::string imgName = *imageDir + fileName;
[7661]446          if(File(imgName).isFile())
[6648]447          {
[7676]448            PRINTF(4)("Image %s resides to %s\n", fileName.c_str(), imgName.c_str());
[6648]449            tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt());
450            break;
451          }
[6640]452        }
453      }
[6648]454      if(!tmpResource)
[7221]455        PRINTF(2)("!!Image %s not Found!!\n", fileName.c_str());
[6648]456      break;
[4534]457#endif /* NO_TEXTURES */
[5323]458#ifndef NO_SHADERS
[6648]459    case SHADER:
[7661]460      if(File(fullName).isFile())
[6640]461      {
[6648]462        if (param0 != MT_NULL)
[6640]463        {
[6648]464          MultiType param = param0; /// HACK
[7221]465          std::string secFullName = ResourceManager::getFullName(param.getCString());
[7661]466          if (File(secFullName).isFile())
[6648]467          {
468            tmpResource->param[0] = secFullName;
469            tmpResource->pointer = new Shader(fullName, secFullName);
470          }
[4597]471        }
[6648]472        else
473        {
474          tmpResource->param[0] = param0;
[7221]475          tmpResource->pointer = new Shader(fullName, "");
[6648]476        }
[6640]477      }
[6648]478      break;
[6640]479#endif /* NO_SHADERS */
[6648]480    default:
481      tmpResource->pointer = NULL;
[7221]482      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.c_str());
[6648]483      break;
[6640]484  }
[5216]485  if (tmpResource->pointer != NULL)
[6640]486    this->resourceList.push_back(tmpResource);
487
488  if (tmpResource->pointer != NULL)
489    return tmpResource;
[4597]490  else
[6640]491  {
[7221]492    PRINTF(2)("Resource %s could not be loaded\n", fileName.c_str());
[6640]493    delete tmpResource;
494    return NULL;
495  }
[3658]496}
497
498/**
[6647]499 * @brief unloads a Resource
[4836]500 * @param pointer: The pointer to free
501 * @param prio: the PriorityLevel to unload this resource
502 * @returns true if successful (pointer found, and deleted), false otherwise
[3658]503*/
[6651]504bool ResourceManager::unload(BaseObject* pointer, ResourcePriority prio)
[3658]505{
[5366]506  if (pointer == NULL)
507    return false;
[3658]508  // if pointer is existent. and only one resource of this type exists.
[3672]509  Resource* tmpResource = this->locateResourceByPointer(pointer);
[5366]510  if (tmpResource != NULL)
511    return unload(tmpResource, prio);
[3660]512  else
[5366]513  {
514    PRINTF(2)("Resource not Found %p\n", pointer);
515    return false;
516  }
[3660]517}
518
[4465]519/**
[6647]520 * @brief unloads a Resource
[4836]521 * @param resource: The resource to unloade
522 * @param prio the PriorityLevel to unload this resource
[5308]523 * @returns true on success, false otherwise.
[4465]524*/
[3660]525bool ResourceManager::unload(Resource* resource, ResourcePriority prio)
526{
[5306]527  if (resource == NULL)
528    return false;
[3665]529  if (resource->count > 0)
530    resource->count--;
[5306]531
[3660]532  if (resource->prio <= prio)
[6640]533  {
534    if (resource->count == 0)
[3658]535    {
[7193]536      delete resource->pointer;
[6640]537      // deleting the List Entry:
[7221]538      PRINTF(4)("Resource %s safely removed.\n", resource->name.c_str());
[6642]539      std::vector<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource);
[6640]540      this->resourceList.erase(resourceIT);
541      delete resource;
[3658]542    }
[6640]543    else
[7221]544      PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name.c_str(), resource->count);
[6640]545  }
[3658]546  else
[7221]547    PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name.c_str());
[3658]548  return true;
[3655]549}
550
[3660]551
[3655]552/**
[6647]553 * @brief unloads all alocated Memory of Resources with a pririty lower than prio
[4836]554 * @param prio The priority to delete
[3660]555*/
556bool ResourceManager::unloadAllByPriority(ResourcePriority prio)
557{
[8316]558  bool removedAll = true;
[6222]559  unsigned int removeCount;
560  for (unsigned int round = 0; round < 3; round++)
561  {
[6642]562    int index = this->resourceList.size() - 1;
[6222]563    removeCount = 0;
[6642]564    while (index >= 0)
[3660]565    {
[6642]566      if (this->resourceList[index]->prio <= prio)
[6640]567      {
[6642]568        if (this->resourceList[index]->count == 0)
569          unload(this->resourceList[index], prio);
[6640]570        else
[6222]571        {
[6664]572          if (round == 3)
[8316]573          {
[6664]574            PRINTF(2)("unable to unload %s because there are still %d references to it\n",
[7221]575                      this->resourceList[index]->name.c_str(), this->resourceList[index]->count);
[8316]576            removedAll = false;
577          }
[6640]578          removeCount++;
[6222]579        }
[6640]580      }
[6642]581      index--;
[6640]582    }
583    if (removeCount == 0) break;
[6222]584  }
[8316]585  return removedAll;
[3660]586}
587
[5994]588
[3660]589/**
[6647]590 * @brief Searches for a Resource by some information
[4836]591 * @param fileName: The name to look for
592 * @param type the Type of resource to locate.
[6645]593 * @param param0: an additional option to parse (see the constuctors for more help)
[4836]594 * @param param1: an additional option to parse (see the constuctors for more help)
595 * @param param2: an additional option to parse (see the constuctors for more help)
596 * @returns a Pointer to the Resource if found, NULL otherwise.
[3658]597*/
[7221]598Resource* ResourceManager::locateResourceByInfo(const std::string& fileName, ResourceType type,
[6648]599    const MultiType& param0, const MultiType& param1, const MultiType& param2) const
[3658]600{
[6642]601  std::vector<Resource*>::const_iterator resource;
[6222]602  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
603  {
[7221]604    if ((*resource)->type == type && fileName == (*resource)->name)
[6640]605    {
606      bool match = false;
607      switch (type)
608      {
[4534]609#ifndef NO_MODEL
[6648]610        case PRIM:
611        case OBJ:
612          if (param0 == MT_NULL)
613          {
614            if ((*resource)->param[0] == 1.0f)
615              match = true;
616          }
617          else if ((*resource)->param[0] == param0.getFloat())
[6640]618            match = true;
[6648]619          break;
620        case MD2:
[7059]621          if (param0 == MT_NULL && ((*resource)->param[0] == "") && param1 == MT_NULL && ((*resource)->param[0] == 1.0f))
[6648]622              match = true;
[7059]623          else if ((*resource)->param[0] == ((MultiType)param0).getString() && (*resource)->param[1] == ((MultiType)param1).getFloat())
[6640]624            match = true;
[6648]625          break;
[4534]626#endif /* NO_MODEL */
627#ifndef NO_TEXT
[6648]628        case TTF:
629          if (param0 == MT_NULL)
630          {
631            if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE)
632              match = true;
633          }
634          else if ((*resource)->param[0] == param0.getInt())
[6640]635            match = true;
[6648]636          break;
[4534]637#endif /* NO_TEXT */
[5323]638#ifndef NO_SHADERS
[6648]639        case SHADER:
640          if (param0 == MT_NULL)
641          {
642            if ((*resource)->param[0] == "")
643              match = true;
644          }
645          else if ((*resource)->param[0] == ((MultiType)param0).getString())
[6640]646            match = true;
[6859]647          break;
[5323]648#endif /* NO_SHADERS */
[6467]649#ifndef NO_TEXTURES
[6648]650        case IMAGE:
651          if (param0 == MT_NULL)
652          {
653            if ((*resource)->param[0] == GL_TEXTURE_2D)
654              match = true;
655          }
656          else if ((*resource)->param[0] ==  param0.getInt())
[6640]657            match = true;
[6859]658          break;
[6648]659#endif /* NO_TEXTURES */
660        default:
[6640]661          match = true;
[6648]662          break;
[6640]663      }
664      if (match)
665      {
666        return (*resource);
667      }
[3658]668    }
[6640]669  }
[3658]670  return NULL;
671}
672
673/**
[6647]674 * @brief Searches for a Resource by Pointer
[4836]675 * @param pointer the Pointer to search for
676 * @returns a Pointer to the Resource if found, NULL otherwise.
[6647]677 */
[5994]678Resource* ResourceManager::locateResourceByPointer(const void* pointer) const
[3658]679{
[3667]680  //  Resource* enumRes = resourceList->enumerate();
[6642]681  std::vector<Resource*>::const_iterator resource;
[6222]682  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
683    if (pointer == (*resource)->pointer)
[6640]684      return (*resource);
[3658]685  return NULL;
686}
687
[7221]688std::string ResourceManager::toResourcableString(unsigned int i)
[6648]689{
[7221]690/*  int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
691  len += this->resourceList[i]->name.size();
[7199]692  if (this->resourceList[i]->param[0].getCString()) len += strlen(this->resourceList[i]->param[0].getCString()) +1;
693  if (this->resourceList[i]->param[1].getCString()) len += strlen(this->resourceList[i]->param[1].getCString()) +1;
694  if (this->resourceList[i]->param[2].getCString()) len += strlen(this->resourceList[i]->param[2].getCString()) +1;
[6648]695  len += 10;
[7221]696  std::string tmp = new char[len];
[6648]697  tmp[0] = '\0';
[7221]698  strcat(tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
[6648]699  strcat(tmp,",");
700  strcat (tmp, this->resourceList[i]->name);
[7199]701  if (this->resourceList[i]->param[0].getCString() && this->resourceList[i]->param[0].getCString() != '\0')
[6648]702  {
703    strcat(tmp,",");
[7199]704    strcat( tmp, this->resourceList[i]->param[0].getCString());
[6648]705  }
[7199]706  if (this->resourceList[i]->param[1].getCString() && this->resourceList[i]->param[1].getCString() != '\0')
[6648]707  {
708    strcat(tmp,",");
[7199]709    strcat( tmp, this->resourceList[i]->param[1].getCString());
[6648]710  }
[7199]711  if (this->resourceList[i]->param[2].getCString() && this->resourceList[i]->param[2].getCString() != '\0')
[6648]712  {
713    strcat(tmp,",");
[7199]714    strcat( tmp, this->resourceList[i]->param[2].getCString());
[6648]715  }
[7221]716  return tmp;*/
[8316]717  return "";
[6648]718}
719
[3658]720/**
[6648]721 * @brief caches a Resource from a ResourceableString created with the toResourcableString-function
722 * @param resourceableString the String to cache the resource from.
723 */
[7221]724bool ResourceManager::fromResourceableString(const std::string& resourceableString)
[6648]725{
[7221]726/*  SubString splits(resourceableString, ',');
[6648]727  splits.debug();
728  if (splits.getCount() == 2)
729    this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
730                RP_LEVEL);
731  else if (splits.getCount() == 3)
[6650]732    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
[6648]733                RP_LEVEL, splits[2]);
734  else if (splits.getCount() == 4)
[6650]735    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
[6648]736                RP_LEVEL, splits[2], splits[3]);
737  else if (splits.getCount() == 5)
[6650]738    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
[7221]739                RP_LEVEL, splits[2], splits[3], splits[4]);*/
[8316]740  return false;
[6648]741}
742
743
744/**
[4961]745 * @param fileName the Name of the File to check
746 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist
[5219]747 * !!IMPORTANT: this has to be deleted from the outside!!
[4166]748*/
[7221]749std::string ResourceManager::getFullName(const std::string& fileName)
[4166]750{
[7221]751  if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty())
752    return "";
[4462]753
[7221]754  std::string retName = ResourceManager::getInstance()->getDataDir() +fileName;
[7661]755  if (File(retName).isFile() || File(retName).isDirectory())
[4167]756    return retName;
757  else
[7221]758    return "";
[4166]759}
[4032]760
761
[3676]762/**
[6647]763 * @brief checks wether a file is in the DataDir.
[5335]764 * @param fileName the File to check if it is in the Data-Dir structure.
765 * @returns true if the file exists, false otherwise
766 */
[7221]767bool ResourceManager::isInDataDir(const std::string& fileName)
[5335]768{
[7221]769  if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty())
[5335]770    return false;
771
772  bool retVal = false;
[7221]773  std::string checkFile = ResourceManager::getInstance()->getDataDir() + fileName;
[5335]774
[7661]775  if (File(checkFile).exists())
[5335]776    retVal = true;
777  else
778    retVal = false;
779  return retVal;
780}
781
782
783/**
[6647]784 * @brief outputs debug information about the ResourceManager
785 */
[4746]786void ResourceManager::debug() const
[3676]787{
788  PRINT(0)("=RM===================================\n");
789  PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
790  PRINT(0)("======================================\n");
791  // if it is not initialized
792  PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef);
[7221]793  PRINT(0)(" Data-Directory is: %s\n", this->dataDir.c_str());
[3676]794  PRINT(0)(" List of Image-Directories: ");
[7221]795  std::vector<std::string>::const_iterator imageDir;
[6222]796  for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
[7221]797    PRINT(0)("%s ", (*imageDir).c_str());
[3676]798  PRINT(0)("\n");
799
800  PRINT(0)("List of all stored Resources:\n");
[6642]801  std::vector<Resource*>::const_iterator resource;
[6222]802  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
803
[6640]804  {
805    PRINT(0)("-----------------------------------------\n");
[7221]806    PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name.c_str(), (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));
[6640]807
808    PRINT(0)("gets deleted at ");
809    switch((*resource)->prio)
[3676]810    {
[6648]811      default:
812      case RP_NO:
813        PRINT(0)("first posibility (0)\n");
814        break;
815      case RP_LEVEL:
816        PRINT(0)("the end of the Level (1)\n");
817        break;
818      case RP_CAMPAIGN:
819        PRINT(0)("the end of the campaign (2)\n");
820        break;
821      case RP_GAME:
822        PRINT(0)("when leaving the game (3)\n");
823        break;
[3676]824    }
[6640]825  }
[3676]826
827
828
829  PRINT(0)("==================================RM==\n");
830}
[5306]831
832
833/**
[6647]834 * @brief converts a ResourceType into the corresponding String
[5306]835 * @param type the ResourceType to translate
836 * @returns the converted String.
837 */
838const char* ResourceManager::ResourceTypeToChar(ResourceType type)
839{
[6646]840  return ResourceManager::resourceNames[type];
841}
842
843/**
844 * @brief converts a String into a ResourceType (good for loading)
845 * @param resourceType the name of the Type
846 * @returns the Number of the Type, or 0 (defautl) if not found.
847 */
[7225]848ResourceType ResourceManager::stringToResourceType(const std::string& resourceType)
[6646]849{
850  for (unsigned int i = 0; i < RESOURCE_TYPE_SIZE; i++)
[7225]851    if (resourceType == ResourceManager::resourceNames[i])
[6646]852      return (ResourceType)i;
853  return (ResourceType)0;
854}
855
856/**
857 * The Names of the ResourceTypes
858 */
859const char* ResourceManager::resourceNames[] =
[6648]860  {
[5306]861#ifndef NO_MODEL
[6648]862    "ObjectModel",
863    "PrimitiveModel",
864    "MD2-Data",
[5306]865#endif
[6648]866#ifndef NO_TEXT
867    "Font",
[5306]868#endif
869#ifndef NO_AUDIO
[6648]870    "Wav",
871    "mp3",
872    "ogg",
[5306]873#endif
[6648]874#ifndef NO_TEXTURES
875    "Texture",
[5306]876#endif
[5323]877#ifndef NO_SHADERS
[6648]878    "Shader",
[5323]879#endif
[6648]880
881  };
Note: See TracBrowser for help on using the repository browser.