Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6350 was 6222, checked in by bensch, 19 years ago

orxonox/trunk: merged the christmas branche to the trunk
merged with command:
svn merge -r6165:HEAD christmas_branche/ ../trunk/
no conflicts

File size: 27.8 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.end();
580  unsigned int removeCount;
581  for (unsigned int round = 0; round < 3; round++)
582  {
583    removeCount = 0;
584    while (pre != resourceList.end())
585    {
586      resource = pre;
587      pre--;
588        if ((*resource)->prio <= prio)
589        {
590          if ((*resource)->count == 0)
591            unload((*resource), prio);
592          else
593          {
594            PRINTF(2)("unable to unload %s because there are still %d references to it\n",
595                     (*resource)->name, (*resource)->count);
596            removeCount++;
597          }
598        }
599     }
600     if (removeCount == 0) break;
601  }
602}
603
604
605/**
606 * Searches for a Resource by some information
607 * @param fileName: The name to look for
608 * @param type the Type of resource to locate.
609 * @param param1: an additional option to parse (see the constuctors for more help)
610 * @param param2: an additional option to parse (see the constuctors for more help)
611 * @param param3: an additional option to parse (see the constuctors for more help)
612 * @returns a Pointer to the Resource if found, NULL otherwise.
613*/
614Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type,
615                                                void* param1, void* param2, void* param3) const
616{
617  std::list<Resource*>::const_iterator resource;
618  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
619  {
620    if ((*resource)->type == type && !strcmp(fileName, (*resource)->name))
621        {
622          bool match = false;
623
624          switch (type)
625            {
626#ifndef NO_MODEL
627            case PRIM:
628            case OBJ:
629              if (!param1)
630                {
631                  if ((*resource)->modelSize == 1.0)
632                    match = true;
633                }
634              else if ((*resource)->modelSize == *(float*)param1)
635                match = true;
636              break;
637            case MD2:
638              if (!param1)
639                {
640                  if ((*resource)->secFileName == NULL)
641                    match = true;
642                }
643              else if (!strcmp((*resource)->secFileName, (const char*)param1))
644                match = true;
645              break;
646#endif /* NO_MODEL */
647#ifndef NO_TEXT
648            case TTF:
649              if (param1 == NULL)
650                {
651                  if ((*resource)->ttfSize == FONT_DEFAULT_RENDER_SIZE)
652                    match = true;
653                }
654              else if ((*resource)->ttfSize == *(unsigned int*)param1)
655                match = true;
656              break;
657#endif /* NO_TEXT */
658#ifndef NO_SHADERS
659              case SHADER:
660                if (!param1)
661                {
662                  if ((*resource)->secFileName == NULL)
663                    match = true;
664                }
665                else if (!strcmp((*resource)->secFileName, (const char*)param1))
666                  match = true;
667#endif /* NO_SHADERS */
668            default:
669              match = true;
670              break;
671            }
672          if (match)
673            {
674              return (*resource);
675            }
676        }
677    }
678  return NULL;
679}
680
681/**
682 * Searches for a Resource by Pointer
683 * @param pointer the Pointer to search for
684 * @returns a Pointer to the Resource if found, NULL otherwise.
685*/
686Resource* ResourceManager::locateResourceByPointer(const void* pointer) const
687{
688  //  Resource* enumRes = resourceList->enumerate();
689  std::list<Resource*>::const_iterator resource;
690  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
691    if (pointer == (*resource)->pointer)
692        return (*resource);
693  return NULL;
694}
695
696/**
697 * Checks if it is a Directory
698 * @param directoryName the Directory to check for
699 * @returns true if it is a directory/symlink false otherwise
700*/
701bool ResourceManager::isDir(const char* directoryName)
702{
703  if (directoryName == NULL)
704    return false;
705
706  char* tmpDirName = NULL;
707  struct stat status;
708
709  // checking for the termination of the string given. If there is a "/" at the end cut it away
710  if (directoryName[strlen(directoryName)-1] == '/' ||
711      directoryName[strlen(directoryName)-1] == '\\')
712    {
713      tmpDirName = new char[strlen(directoryName)];
714      strncpy(tmpDirName, directoryName, strlen(directoryName)-1);
715      tmpDirName[strlen(directoryName)-1] = '\0';
716    }
717  else
718    {
719      tmpDirName = new char[strlen(directoryName)+1];
720      strcpy(tmpDirName, directoryName);
721    }
722
723  if(!stat(tmpDirName, &status))
724    {
725      if (status.st_mode & (S_IFDIR
726#ifndef __WIN32__
727                            | S_IFLNK
728#endif
729                            ))
730        {
731          delete[] tmpDirName;
732          return true;
733        }
734      else
735        {
736          delete[] tmpDirName;
737          return false;
738        }
739    }
740  else
741  {
742    delete[] tmpDirName;
743    return false;
744  }
745}
746
747/**
748 * Checks if the file is either a Regular file or a Symlink
749 * @param fileName the File to check for
750 * @returns true if it is a regular file/symlink, false otherwise
751*/
752bool ResourceManager::isFile(const char* fileName)
753{
754  if (fileName == NULL)
755    return false;
756  char* tmpFileName = ResourceManager::homeDirCheck(fileName);
757  // actually checks the File
758  struct stat status;
759  if (!stat(tmpFileName, &status))
760    {
761      if (status.st_mode & (S_IFREG
762#ifndef __WIN32__
763                            | S_IFLNK
764#endif
765                            ))
766        {
767          delete[] tmpFileName;
768          return true;
769        }
770      else
771        {
772          delete[] tmpFileName;
773          return false;
774        }
775    }
776  else
777    {
778      delete[] tmpFileName;
779      return false;
780    }
781}
782
783/**
784 * touches a File on the disk (thereby creating it)
785 * @param fileName The file to touch
786*/
787bool ResourceManager::touchFile(const char* fileName)
788{
789  char* tmpName = ResourceManager::homeDirCheck(fileName);
790  if (tmpName == NULL)
791    return false;
792  FILE* stream;
793  if( (stream = fopen (tmpName, "w")) == NULL)
794    {
795      PRINTF(1)("could not open %s fro writing\n", fileName);
796      delete[] tmpName;
797      return false;
798    }
799  fclose(stream);
800
801  delete[] tmpName;
802}
803
804/**
805 * deletes a File from disk
806 * @param fileName the File to delete
807*/
808bool ResourceManager::deleteFile(const char* fileName)
809{
810  if (fileName == NULL)
811    return false;
812  char* tmpName = ResourceManager::homeDirCheck(fileName);
813  unlink(tmpName);
814  delete[] tmpName;
815}
816
817/**
818 * @param name the Name of the file to check
819 * @returns The name of the file, including the HomeDir
820 * IMPORTANT: this has to be deleted from the outside
821 */
822char* ResourceManager::homeDirCheck(const char* name)
823{
824  if (name == NULL)
825    return NULL;
826  char* retName;
827  if (!strncmp(name, "~/", 2))
828    {
829      char tmpFileName[500];
830#ifdef __WIN32__
831      strcpy(tmpFileName, getenv("USERPROFILE"));
832#else
833      strcpy(tmpFileName, getenv("HOME"));
834#endif
835      retName = new char[strlen(tmpFileName)+strlen(name)];
836      sprintf(retName, "%s%s", tmpFileName, name+1);
837    }
838  else
839    {
840      retName = new char[strlen(name)+1];
841      strcpy(retName, name);
842    }
843  return retName;
844}
845
846/**
847 * @param fileName the Name of the File to check
848 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist
849 * !!IMPORTANT: this has to be deleted from the outside!!
850*/
851char* ResourceManager::getFullName(const char* fileName)
852{
853  if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL)
854    return NULL;
855
856  char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir())
857                           + strlen(fileName) + 1];
858  sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName);
859  if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName))
860    return retName;
861  else
862    {
863      delete[] retName;
864      return NULL;
865    }
866}
867
868
869/**
870 * checks wether a file is in the DataDir.
871 * @param fileName the File to check if it is in the Data-Dir structure.
872 * @returns true if the file exists, false otherwise
873 */
874bool ResourceManager::isInDataDir(const char* fileName)
875{
876  if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL)
877    return false;
878
879  bool retVal = false;
880  char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir())
881      + strlen(fileName) + 1];
882  sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName);
883
884  if (ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile))
885    retVal = true;
886  else
887    retVal = false;
888  delete[] checkFile;
889  return retVal;
890}
891
892
893/**
894 * outputs debug information about the ResourceManager
895*/
896void ResourceManager::debug() const
897{
898  PRINT(0)("=RM===================================\n");
899  PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
900  PRINT(0)("======================================\n");
901  // if it is not initialized
902  PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef);
903  PRINT(0)(" Data-Directory is: %s\n", this->dataDir);
904  PRINT(0)(" List of Image-Directories: ");
905  std::list<char*>::const_iterator imageDir;
906  for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
907      PRINT(0)("%s ", (*imageDir));
908  PRINT(0)("\n");
909
910  PRINT(0)("List of all stored Resources:\n");
911  std::list<Resource*>::const_iterator resource;
912  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
913
914    {
915      PRINT(0)("-----------------------------------------\n");
916      PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name, (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));
917
918      PRINT(0)("gets deleted at ");
919      switch((*resource)->prio)
920        {
921        default:
922        case RP_NO:
923          PRINT(0)("first posibility (0)\n");
924          break;
925        case RP_LEVEL:
926          PRINT(0)("the end of the Level (1)\n");
927          break;
928        case RP_CAMPAIGN:
929          PRINT(0)("the end of the campaign (2)\n");
930          break;
931        case RP_GAME:
932          PRINT(0)("when leaving the game (3)\n");
933          break;
934        }
935    }
936
937
938
939  PRINT(0)("==================================RM==\n");
940}
941
942
943/**
944 * converts a ResourceType into the corresponding String
945 * @param type the ResourceType to translate
946 * @returns the converted String.
947 */
948const char* ResourceManager::ResourceTypeToChar(ResourceType type)
949{
950  switch (type)
951  {
952#ifndef NO_MODEL
953    case OBJ:
954      return "ObjectModel";
955      break;
956    case PRIM:
957      return "PrimitiveModel";
958      break;
959    case MD2:
960      return "MD2-Data";
961      break;
962#endif
963#ifndef NO_TEXTURES
964    case IMAGE:
965      return "ImageFile (Texture)";
966      break;
967#endif
968#ifndef NO_AUDIO
969    case WAV:
970      return "SoundFile";
971      break;
972    case OGG:
973      return "MusicFile";
974      break;
975#endif
976#ifndef NO_TEXT
977    case TTF:
978      return "Font (TTF)";
979      break;
980#endif
981#ifndef NO_SHADERS
982    case SHADER:
983      return "Shader";
984      break;
985#endif
986    default:
987      return "unknown Format";
988      break;
989  }
990}
Note: See TracBrowser for help on using the repository browser.