Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: VERY simplistic Banking of the Player

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