Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: nicer rendering of the Shell

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