Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: better check for data-dir

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