Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: better check for data-dir

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