Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: much cleaner Model Loading unloading, model is now private to WorldEntity (not protected)

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
[5930]35#include "sound_buffer.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
[5994]608
[3660]609/**
[4961]610 * Searches for a Resource by some information
[4836]611 * @param fileName: The name to look for
612 * @param type the Type of resource to locate.
613 * @param param1: an additional option to parse (see the constuctors for more help)
614 * @param param2: an additional option to parse (see the constuctors for more help)
615 * @param param3: an additional option to parse (see the constuctors for more help)
616 * @returns a Pointer to the Resource if found, NULL otherwise.
[3658]617*/
[4462]618Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type,
[5994]619                                                void* param1, void* param2, void* param3) const
[3658]620{
[3667]621  //  Resource* enumRes = resourceList->enumerate();
622  tIterator<Resource>* iterator = resourceList->getIterator();
[5115]623  Resource* enumRes = iterator->firstElement();
[3658]624  while (enumRes)
625    {
[3790]626      if (enumRes->type == type && !strcmp(fileName, enumRes->name))
[4597]627        {
628          bool match = false;
[4462]629
[4597]630          switch (type)
631            {
[4534]632#ifndef NO_MODEL
[4597]633            case PRIM:
634            case OBJ:
635              if (!param1)
636                {
637                  if (enumRes->modelSize == 1.0)
638                    match = true;
639                }
640              else if (enumRes->modelSize == *(float*)param1)
641                match = true;
642              break;
643            case MD2:
644              if (!param1)
645                {
[5323]646                  if (enumRes->secFileName == NULL)
[4597]647                    match = true;
648                }
[5323]649              else if (!strcmp(enumRes->secFileName, (const char*)param1))
[4597]650                match = true;
651              break;
[4534]652#endif /* NO_MODEL */
653#ifndef NO_TEXT
[4597]654            case TTF:
[5307]655              if (param1 == NULL)
[4597]656                {
[5372]657                  if (enumRes->ttfSize == FONT_DEFAULT_RENDER_SIZE)
[5307]658                    match = true;
[4597]659                }
[5306]660              else if (enumRes->ttfSize == *(unsigned int*)param1)
[5307]661                match = true;
[4597]662              break;
[4534]663#endif /* NO_TEXT */
[5323]664#ifndef NO_SHADERS
665              case SHADER:
666                if (!param1)
667                {
668                  if (enumRes->secFileName == NULL)
669                    match = true;
670                }
671                else if (!strcmp(enumRes->secFileName, (const char*)param1))
672                  match = true;
673#endif /* NO_SHADERS */
[4597]674            default:
675              match = true;
676              break;
677            }
678          if (match)
679            {
680              delete iterator;
681              return enumRes;
682            }
683        }
[3667]684      enumRes = iterator->nextElement();
[3658]685    }
[3667]686  delete iterator;
[3658]687  return NULL;
688}
689
690/**
[4961]691 * Searches for a Resource by Pointer
[4836]692 * @param pointer the Pointer to search for
693 * @returns a Pointer to the Resource if found, NULL otherwise.
[3658]694*/
[5994]695Resource* ResourceManager::locateResourceByPointer(const void* pointer) const
[3658]696{
[3667]697  //  Resource* enumRes = resourceList->enumerate();
698  tIterator<Resource>* iterator = resourceList->getIterator();
[5115]699  Resource* enumRes = iterator->firstElement();
[3658]700  while (enumRes)
701    {
[3665]702      if (pointer == enumRes->pointer)
[4597]703        {
704          delete iterator;
705          return enumRes;
706        }
[3667]707      enumRes = iterator->nextElement();
[3658]708    }
[3667]709  delete iterator;
[3658]710  return NULL;
711}
712
713/**
[4961]714 * Checks if it is a Directory
[4836]715 * @param directoryName the Directory to check for
716 * @returns true if it is a directory/symlink false otherwise
[3655]717*/
718bool ResourceManager::isDir(const char* directoryName)
719{
[4462]720  if (directoryName == NULL)
721    return false;
722
[3883]723  char* tmpDirName = NULL;
[3655]724  struct stat status;
[3883]725
726  // checking for the termination of the string given. If there is a "/" at the end cut it away
[5113]727  if (directoryName[strlen(directoryName)-1] == '/' ||
728      directoryName[strlen(directoryName)-1] == '\\')
[3883]729    {
[5335]730      tmpDirName = new char[strlen(directoryName)];
[3883]731      strncpy(tmpDirName, directoryName, strlen(directoryName)-1);
732      tmpDirName[strlen(directoryName)-1] = '\0';
733    }
734  else
735    {
736      tmpDirName = new char[strlen(directoryName)+1];
737      strcpy(tmpDirName, directoryName);
738    }
739
[4032]740  if(!stat(tmpDirName, &status))
741    {
742      if (status.st_mode & (S_IFDIR
[3790]743#ifndef __WIN32__
[4597]744                            | S_IFLNK
[3790]745#endif
[4597]746                            ))
747        {
[5113]748          delete[] tmpDirName;
[4597]749          return true;
750        }
[4032]751      else
[4597]752        {
[5208]753          delete[] tmpDirName;
[4597]754          return false;
755        }
[3883]756    }
[3658]757  else
[5211]758  {
759    delete[] tmpDirName;
[4032]760    return false;
[5211]761  }
[3655]762}
763
764/**
[4961]765 * Checks if the file is either a Regular file or a Symlink
[4836]766 * @param fileName the File to check for
767 * @returns true if it is a regular file/symlink, false otherwise
[3655]768*/
769bool ResourceManager::isFile(const char* fileName)
770{
[4462]771  if (fileName == NULL)
772    return false;
[4032]773  char* tmpFileName = ResourceManager::homeDirCheck(fileName);
774  // actually checks the File
[3655]775  struct stat status;
[4032]776  if (!stat(tmpFileName, &status))
777    {
[4597]778      if (status.st_mode & (S_IFREG
[3790]779#ifndef __WIN32__
[4597]780                            | S_IFLNK
[3790]781#endif
[4597]782                            ))
783        {
[5208]784          delete[] tmpFileName;
[4597]785          return true;
786        }
[4032]787      else
[4597]788        {
[5208]789          delete[] tmpFileName;
[4597]790          return false;
791        }
[4032]792    }
[4597]793  else
[4032]794    {
[5208]795      delete[] tmpFileName;
[4032]796      return false;
797    }
798}
799
[4166]800/**
[4961]801 * touches a File on the disk (thereby creating it)
[4836]802 * @param fileName The file to touch
[4166]803*/
[4032]804bool ResourceManager::touchFile(const char* fileName)
805{
806  char* tmpName = ResourceManager::homeDirCheck(fileName);
[4462]807  if (tmpName == NULL)
808    return false;
[4032]809  FILE* stream;
810  if( (stream = fopen (tmpName, "w")) == NULL)
811    {
812      PRINTF(1)("could not open %s fro writing\n", fileName);
[5208]813      delete[] tmpName;
[4032]814      return false;
815    }
[4033]816  fclose(stream);
[4597]817
[5208]818  delete[] tmpName;
[4032]819}
820
[4166]821/**
[4961]822 * deletes a File from disk
[4836]823 * @param fileName the File to delete
[4166]824*/
[4032]825bool ResourceManager::deleteFile(const char* fileName)
826{
[4462]827  if (fileName == NULL)
828    return false;
[4032]829  char* tmpName = ResourceManager::homeDirCheck(fileName);
830  unlink(tmpName);
[5208]831  delete[] tmpName;
[4032]832}
833
[4597]834/**
[4961]835 * @param name the Name of the file to check
836 * @returns The name of the file, including the HomeDir
837 * IMPORTANT: this has to be deleted from the outside
838 */
[4032]839char* ResourceManager::homeDirCheck(const char* name)
840{
[4462]841  if (name == NULL)
842    return NULL;
[4032]843  char* retName;
844  if (!strncmp(name, "~/", 2))
845    {
846      char tmpFileName[500];
847#ifdef __WIN32__
848      strcpy(tmpFileName, getenv("USERPROFILE"));
849#else
850      strcpy(tmpFileName, getenv("HOME"));
851#endif
852      retName = new char[strlen(tmpFileName)+strlen(name)];
853      sprintf(retName, "%s%s", tmpFileName, name+1);
854    }
[3655]855  else
[4032]856    {
857      retName = new char[strlen(name)+1];
858      strcpy(retName, name);
859    }
860  return retName;
[3655]861}
[3676]862
[4597]863/**
[4961]864 * @param fileName the Name of the File to check
865 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist
[5219]866 * !!IMPORTANT: this has to be deleted from the outside!!
[4166]867*/
868char* ResourceManager::getFullName(const char* fileName)
869{
[5335]870  if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL)
[4462]871    return NULL;
872
[4216]873  char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir())
[4597]874                           + strlen(fileName) + 1];
[4166]875  sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName);
[4462]876  if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName))
[4167]877    return retName;
878  else
879    {
[5208]880      delete[] retName;
[4167]881      return NULL;
882    }
[4166]883}
[4032]884
885
[3676]886/**
[5335]887 * checks wether a file is in the DataDir.
888 * @param fileName the File to check if it is in the Data-Dir structure.
889 * @returns true if the file exists, false otherwise
890 */
891bool ResourceManager::isInDataDir(const char* fileName)
892{
893  if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL)
894    return false;
895
896  bool retVal = false;
897  char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir())
898      + strlen(fileName) + 1];
899  sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName);
900
901  if (ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile))
902    retVal = true;
903  else
904    retVal = false;
905  delete[] checkFile;
906  return retVal;
907}
908
909
910/**
[4961]911 * outputs debug information about the ResourceManager
[3676]912*/
[4746]913void ResourceManager::debug() const
[3676]914{
915  PRINT(0)("=RM===================================\n");
916  PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
917  PRINT(0)("======================================\n");
918  // if it is not initialized
919  PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef);
920  PRINT(0)(" Data-Directory is: %s\n", this->dataDir);
921  PRINT(0)(" List of Image-Directories: ");
922  tIterator<char>* tmpIt = imageDirs->getIterator();
[5115]923  char* tmpDir = tmpIt->firstElement();
[3676]924  while(tmpDir)
925    {
926      PRINT(0)("%s ",tmpDir);
927      tmpDir = tmpIt->nextElement();
928    }
929  delete tmpIt;
930  PRINT(0)("\n");
931
932  PRINT(0)("List of all stored Resources:\n");
933  tIterator<Resource>* iterator = resourceList->getIterator();
[5115]934  Resource* enumRes = iterator->firstElement();
[3676]935  while (enumRes)
936    {
937      PRINT(0)("-----------------------------------------\n");
[5306]938      PRINT(0)("Name: %s; References: %d; Type: %s ", enumRes->name, enumRes->count, ResourceManager::ResourceTypeToChar(enumRes->type));
939
[3676]940      PRINT(0)("gets deleted at ");
941      switch(enumRes->prio)
[4597]942        {
943        default:
944        case RP_NO:
945          PRINT(0)("first posibility (0)\n");
946          break;
947        case RP_LEVEL:
948          PRINT(0)("the end of the Level (1)\n");
949          break;
950        case RP_CAMPAIGN:
951          PRINT(0)("the end of the campaign (2)\n");
952          break;
953        case RP_GAME:
954          PRINT(0)("when leaving the game (3)\n");
955          break;
956        }
[3676]957      enumRes = iterator->nextElement();
958    }
959  delete iterator;
960
961
962
963  PRINT(0)("==================================RM==\n");
964}
[5306]965
966
967/**
968 * converts a ResourceType into the corresponding String
969 * @param type the ResourceType to translate
970 * @returns the converted String.
971 */
972const char* ResourceManager::ResourceTypeToChar(ResourceType type)
973{
974  switch (type)
975  {
976#ifndef NO_MODEL
977    case OBJ:
978      return "ObjectModel";
979      break;
980    case PRIM:
981      return "PrimitiveModel";
982      break;
983    case MD2:
984      return "MD2-Data";
985      break;
986#endif
987#ifndef NO_TEXTURES
988    case IMAGE:
989      return "ImageFile (Texture)";
990      break;
991#endif
992#ifndef NO_AUDIO
993    case WAV:
994      return "SoundFile";
995      break;
996    case OGG:
997      return "MusicFile";
998      break;
999#endif
1000#ifndef NO_TEXT
1001    case TTF:
1002      return "Font (TTF)";
1003      break;
1004#endif
[5323]1005#ifndef NO_SHADERS
1006    case SHADER:
1007      return "Shader";
1008      break;
1009#endif
[5306]1010    default:
1011      return "unknown Format";
1012      break;
1013  }
1014}
Note: See TracBrowser for help on using the repository browser.