Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: better banking of the player, and memory leak fix

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