Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5482 was 5482, checked in by bensch, 19 years ago

orxonox/trunk: VERY simplistic Banking of the Player

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