Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5303 was 5303, checked in by bensch, 20 years ago

orxonox/trunk: less output on quit… still some strange effecs caused by unloading textures

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