Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: ResourceManager now has Vectors instead of Lists

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