Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: better algorithm for loading the Shader through the ResourceManager
if a fragment-Shader was supplied, but the file did not exist, the Shader gets rejected

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