Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: updated to new autoconf, moved resourcemanager to util/loading

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