Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the christmas branche to the trunk
merged with command:
svn merge -r6165:HEAD christmas_branche/ ../trunk/
no conflicts

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