Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: ResourceManager: caching should work

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