Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: warlord-fix

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