Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/christmas_branche/src/util/loading/resource_manager.cc @ 6186

Last change on this file since 6186 was 6186, checked in by bensch, 18 years ago

christmas less include

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