Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: new Way to load and unload Text

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