Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more fool-proov loading of important paths (data and image)

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