Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: better check for the Data-Directory

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