Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/util/loading/resource_manager.cc @ 6726

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

trunk: small fix in resource/ammos

File size: 32.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 "substring.h"
21#include "debug.h"
22
23#include <algorithm>
24#include <assert.h>
25
26// different resource Types
27#ifndef NO_MODEL
28#include "objModel.h"
29#include "primitive_model.h"
30#include "md2Model.h"
31#endif /* NO_MODEL */
32#ifndef NO_TEXTURES
33#include "texture.h"
34#endif /* NO_TEXTURES */
35#ifndef NO_TEXT
36#include "font.h"
37#endif /* NO_TEXT */
38#ifndef NO_AUDIO
39#include "sound_buffer.h"
40#include "ogg_player.h"
41#endif /* NO_AUDIO */
42#ifndef NO_SHADERS
43#include "shader.h"
44#endif /* NO_SHADERS */
45
46// File Handling Includes
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <unistd.h>
50
51using namespace std;
52
53/**
54 * @brief standard constructor
55*/
56ResourceManager::ResourceManager ()
57{
58  this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager");
59  this->setName("ResourceManager");
60
61  this->dataDir = new char[3];
62  strcpy(this->dataDir, "./");
63  this->tryDataDir("./data");
64}
65
66//! Singleton Reference to the ResourceManager
67ResourceManager* ResourceManager::singletonRef = NULL;
68
69/**
70 * @brief standard destructor
71*/
72ResourceManager::~ResourceManager ()
73{
74  // deleting the Resources-List
75  this->unloadAllByPriority(RP_GAME);
76
77  if (!this->resourceList.empty())
78    PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size());
79
80  // deleting the Directorie Lists
81  while (!this->imageDirs.empty())
82  {
83    delete[] this->imageDirs.back();
84    this->imageDirs.pop_back();
85  }
86
87  delete[] this->dataDir;
88
89  ResourceManager::singletonRef = NULL;
90}
91
92/**
93 * @brief sets the data main directory
94 * @param dataDir the DataDirectory.
95 */
96bool ResourceManager::setDataDir(const char* dataDir)
97{
98  char* realDir = ResourceManager::homeDirCheck(dataDir);
99  if (isDir(realDir))
100  {
101    delete[] this->dataDir;
102    if (dataDir[strlen(dataDir)-1] == '/' || dataDir[strlen(dataDir)-1] == '\\')
103    {
104      this->dataDir = new char[strlen(realDir)+1];
105      strcpy(this->dataDir, realDir);
106    }
107    else
108    {
109      this->dataDir = new char[strlen(realDir)+2];
110      strcpy(this->dataDir, realDir);
111      this->dataDir[strlen(realDir)] = '/';
112      this->dataDir[strlen(realDir)+1] = '\0';
113    }
114    delete[] realDir;
115    return true;
116  }
117  else
118  {
119    PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", realDir, this->dataDir);
120    delete[] realDir;
121    return false;
122  }
123}
124
125/**
126 * @brief sets the data main directory
127 * @param dataDir the DataDirectory.
128 *
129 * this is essentially the same as setDataDir, but it ommits the error-message
130 */
131bool ResourceManager::tryDataDir(const char* dataDir)
132{
133  char* realDir = ResourceManager::homeDirCheck(dataDir);
134  if (isDir(realDir))
135  {
136    delete[] this->dataDir;
137    if (dataDir[strlen(dataDir)-1] == '/' || dataDir[strlen(dataDir)-1] == '\\')
138    {
139      this->dataDir = new char[strlen(realDir)+1];
140      strcpy(this->dataDir, realDir);
141    }
142    else
143    {
144      this->dataDir = new char[strlen(realDir)+2];
145      strcpy(this->dataDir, realDir);
146      this->dataDir[strlen(realDir)] = '/';
147      this->dataDir[strlen(realDir)+1] = '\0';
148    }
149    delete[] realDir;
150    return true;
151  }
152  delete[] realDir;
153  return false;
154}
155
156
157/**
158 * @brief checks for the DataDirectory, by looking if
159 * @param fileInside is iniside of the given directory.
160*/
161bool ResourceManager::verifyDataDir(const char* fileInside)
162{
163  bool retVal;
164  if (!isDir(this->dataDir))
165  {
166    PRINTF(1)("%s is not a directory\n", this->dataDir);
167    return false;
168  }
169
170  char* testFile = new char[strlen(this->dataDir)+strlen(fileInside)+1];
171  sprintf(testFile, "%s%s", this->dataDir, fileInside);
172  retVal = isFile(testFile);
173  delete[] testFile;
174  return retVal;
175}
176
177#ifndef NO_TEXTURES
178/**
179 * @brief adds a new Path for Images
180 * @param imageDir The path to insert
181 * @returns true, if the Path was well and injected (or already existent within the list)
182   false otherwise
183*/
184bool ResourceManager::addImageDir(const char* imageDir)
185{
186  if (imageDir == NULL)
187    return false;
188
189  char* newDir;
190  if (imageDir[strlen(imageDir)-1] == '/' || imageDir[strlen(imageDir)-1] == '\\')
191  {
192    newDir = new char[strlen(imageDir)+1];
193    strcpy(newDir, imageDir);
194  }
195  else
196  {
197    newDir = new char[strlen(imageDir)+2];
198    strcpy(newDir, imageDir);
199    newDir[strlen(imageDir)] = '/';
200    newDir[strlen(imageDir)+1] = '\0';
201  }
202  // check if the param is a Directory
203  if (isDir(newDir))
204  {
205    // check if the Directory has been added before
206    std::vector<char*>::const_iterator imageDir;
207    for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
208    {
209      if (!strcmp(*imageDir, newDir))
210      {
211        PRINTF(3)("Path %s already loaded\n", newDir);
212        delete[] newDir;
213        return true;
214      }
215    }
216    // adding the directory to the List
217    this->imageDirs.push_back(newDir);
218    return true;
219  }
220  else
221  {
222    PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir);
223    delete[] newDir;
224    return false;
225  }
226}
227#endif /* NO_TEXTURES */
228
229/**
230 * @brief loads resources
231 * @param fileName: The fileName of the resource to load
232 * @param prio: The ResourcePriority of this resource (will only be increased)
233 * @param param0: an additional option to parse (see the constuctors for more help)
234 * @param param1: an additional option to parse (see the constuctors for more help)
235 * @param param2: an additional option to parse (see the constuctors for more help)
236 * @returns a pointer to a desired Resource.
237*/
238BaseObject* ResourceManager::load(const char* fileName, ResourcePriority prio,
239                                  const MultiType& param0, const MultiType& param1, const MultiType& param2)
240{
241  if (fileName == NULL)
242    return NULL;
243  ResourceType tmpType;
244#ifndef NO_MODEL
245#define __IF_OK
246  if (!strncasecmp(fileName+(strlen(fileName)-4), ".obj", 4))
247    tmpType = OBJ;
248  else if (!strncmp(fileName+(strlen(fileName)-4), ".md2", 4))
249    tmpType = MD2;
250  else if (!strcasecmp(fileName, "cube") ||
251           !strcasecmp(fileName, "sphere") ||
252           !strcasecmp(fileName, "plane") ||
253           !strcasecmp(fileName, "cylinder") ||
254           !strcasecmp(fileName, "cone"))
255    tmpType = PRIM;
256#endif /* NO_MODEL */
257#ifndef NO_AUDIO
258#ifdef __IF_OK
259  else
260#endif
261#define __IF_OK
262    if (!strncasecmp(fileName+(strlen(fileName)-4), ".wav", 4))
263      tmpType = WAV;
264    else if (!strncasecmp(fileName+(strlen(fileName)-4), ".mp3", 4))
265      tmpType = MP3;
266    else if (!strncasecmp(fileName+(strlen(fileName)-4), ".ogg", 4))
267      tmpType = OGG;
268#endif /* NO_AUDIO */
269#ifndef NO_TEXT
270#ifdef __IF_OK
271    else
272#endif
273#define __IF_OK
274      if (!strncasecmp(fileName+(strlen(fileName)-4), ".ttf", 4))
275        tmpType = TTF;
276#endif /* NO_TEXT */
277#ifndef NO_SHADERS
278#ifdef __IF_OK
279      else
280#endif
281#define __IF_OK
282        if (!strncasecmp(fileName+(strlen(fileName)-5), ".vert", 5))
283          tmpType = SHADER;
284#endif /* NO_SHADERS */
285#ifndef NO_TEXTURES
286#ifdef __IF_OK
287        else
288#else
289  if
290#endif
291          tmpType = IMAGE;
292#endif /* NO_TEXTURES */
293#undef __IF_OK
294  return this->load(fileName, tmpType, prio, param0, param1, param2);
295}
296
297/**
298 * @brief caches a Resource
299 *
300 * @see load;
301 *
302 * @brief returns true if ok, false otherwise.
303 * This function loads a Resource without applying it to an Object.
304 * This is for loading purposes, e.g, when the user is loading a Resource
305 * during the initialisation instead of at Runtime.
306 */
307bool ResourceManager::cache(const char* fileName, ResourceType type, ResourcePriority prio,
308                            const MultiType& param0, const MultiType& param1, const MultiType& param2)
309{
310  assert(fileName != NULL);
311
312  // searching if the resource was loaded before.
313  Resource* tmpResource;
314  // check if we already loaded this Resource
315  tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
316  // otherwise load it
317  if (tmpResource == NULL)
318    tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
319  // return cached pointer.
320  if (tmpResource != NULL) // if the resource was loaded before.
321  {
322    if(tmpResource->prio < prio)
323      tmpResource->prio = prio;
324    return true;
325  }
326  else
327    return false;
328}
329
330/**
331 * tells the ResourceManager to generate a Copy of the Resource.
332 * @brief resourcePointer: The Pointer to the resource to copy
333 * @returns the Resource pointed to resourcePointer.
334 */
335BaseObject* ResourceManager::copy(BaseObject* resourcePointer)
336{
337  Resource* tmp = locateResourceByPointer(resourcePointer);
338  if (tmp!=NULL)
339  {
340    tmp->count++;
341    return tmp->pointer;
342  }
343  else
344    return NULL;
345}
346
347
348/**
349 * @brief loads resources
350 * @param fileName: The fileName of the resource to load
351 * @param type: The Type of Resource to load.
352 * @param prio: The ResourcePriority of this resource (will only be increased)
353 * @param param0: an additional option to parse (see the constuctors for more help)
354 * @param param1: an additional option to parse (see the constuctors for more help)
355 * @param param2: an additional option to parse (see the constuctors for more help)
356 * @returns a pointer to a desired Resource.
357*/
358BaseObject* ResourceManager::load(const char* fileName, ResourceType type, ResourcePriority prio,
359                                  const MultiType& param0, const MultiType& param1, const MultiType& param2)
360{
361  assert(fileName != NULL);
362
363  // searching if the resource was loaded before.
364  Resource* tmpResource;
365  // check if we already loaded this Resource
366  tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
367  // otherwise load it
368  if (tmpResource == NULL)
369  {
370    tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
371  }
372  // return cached pointer.
373  if (tmpResource != NULL) // if the resource was loaded before.
374  {
375    tmpResource->count++;
376    if(tmpResource->prio < prio)
377      tmpResource->prio = prio;
378
379    return tmpResource->pointer;
380  }
381  else
382    return NULL;
383}
384
385
386/**
387 * @brief loads resources for internal purposes
388 * @param fileName: The fileName of the resource to load
389 * @param type: The Type of Resource to load.
390 * @param prio: The ResourcePriority of this resource (will only be increased)
391 * @param param0: an additional option to parse (see the constuctors for more help)
392 * @param param1: an additional option to parse (see the constuctors for more help)
393 * @param param2: an additional option to parse (see the constuctors for more help)
394 * @returns a pointer to a desired Resource.
395 */
396Resource* ResourceManager::loadResource(const char* fileName, ResourceType type, ResourcePriority prio,
397                                        const MultiType& param0, const MultiType& param1, const MultiType& param2)
398{
399  // Setting up the new Resource
400  Resource* tmpResource = new Resource;
401  tmpResource->count = 0;
402  tmpResource->type = type;
403  tmpResource->prio = prio;
404  tmpResource->pointer = NULL;
405  tmpResource->name = new char[strlen(fileName)+1];
406  strcpy(tmpResource->name, fileName);
407
408  // creating the full name. (directoryName + FileName)
409  char* fullName = ResourceManager::getFullName(fileName);
410  // Checking for the type of resource \see ResourceType
411  switch(type)
412  {
413#ifndef NO_MODEL
414    case OBJ:
415      if (param0.getType() != MT_NULL)
416        tmpResource->param[0] = param0;
417      else
418        tmpResource->param[0] = 1.0f;
419
420      if(ResourceManager::isFile(fullName))
421        tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat());
422      else
423      {
424        PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName, dataDir);
425        tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat());
426      }
427      break;
428    case PRIM:
429      if (param0 != MT_NULL)
430        tmpResource->param[0] = param0;
431      else
432        tmpResource->param[0] = 1.0f;
433
434      if (!strcmp(tmpResource->name, "cube"))
435        tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat());
436      else if (!strcmp(tmpResource->name, "sphere"))
437        tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat());
438      else if (!strcmp(tmpResource->name, "plane"))
439        tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat());
440      else if (!strcmp(tmpResource->name, "cylinder"))
441        tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat());
442      else if (!strcmp(tmpResource->name, "cone"))
443        tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat());
444      break;
445    case MD2:
446      if(ResourceManager::isFile(fullName))
447      {
448        tmpResource->param[0] = param0;
449        tmpResource->pointer = new MD2Data(fullName, tmpResource->param[0].getString());
450        //               tmpResource->pointer = new MD2Model(fullName, tmpResource->secFileName);
451
452      }
453      break;
454#endif /* NO_MODEL */
455#ifndef NO_TEXT
456    case TTF:
457      if (param0 != MT_NULL)
458      {
459        assert(param0.getInt() >= 0);
460        tmpResource->param[0] = param0;
461      }
462      else
463        tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE;
464
465      if(isFile(fullName))
466        tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt());
467      else
468        PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName, this->dataDir);
469      break;
470#endif /* NO_TEXT */
471#ifndef NO_AUDIO
472    case WAV:
473      if(isFile(fullName))
474        tmpResource->pointer = new SoundBuffer(fullName);
475      break;
476    case OGG:
477      if (isFile(fullName))
478        tmpResource->pointer = new OggPlayer(fullName);
479      break;
480#endif /* NO_AUDIO */
481#ifndef NO_TEXTURES
482    case IMAGE:
483      if (param0 != MT_NULL)
484        tmpResource->param[0] = param0;
485      else
486        tmpResource->param[0] = GL_TEXTURE_2D;
487      if(isFile(fullName))
488      {
489        PRINTF(4)("Image %s resides to %s\n", fileName, fullName);
490        tmpResource->pointer = new Texture(fullName);
491      }
492      else
493      {
494        std::vector<char*>::iterator imageDir;
495        for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
496        {
497          char* imgName = new char[strlen(*imageDir)+strlen(fileName)+1];
498          sprintf(imgName, "%s%s", *imageDir, fileName);
499          if(isFile(imgName))
500          {
501            PRINTF(4)("Image %s resides to %s\n", fileName, imgName);
502            tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt());
503            delete[] imgName;
504            break;
505          }
506          delete[] imgName;
507        }
508      }
509      if(!tmpResource)
510        PRINTF(2)("!!Image %s not Found!!\n", fileName);
511      break;
512#endif /* NO_TEXTURES */
513#ifndef NO_SHADERS
514    case SHADER:
515      if(ResourceManager::isFile(fullName))
516      {
517        if (param0 != MT_NULL)
518        {
519          MultiType param = param0; /// HACK
520          char* secFullName = ResourceManager::getFullName(param.getString());
521          if (ResourceManager::isFile(secFullName))
522          {
523            tmpResource->param[0] = secFullName;
524            tmpResource->pointer = new Shader(fullName, secFullName);
525          }
526          delete[] secFullName;
527        }
528        else
529        {
530          tmpResource->param[0] = param0;
531          tmpResource->pointer = new Shader(fullName, NULL);
532        }
533      }
534      break;
535#endif /* NO_SHADERS */
536    default:
537      tmpResource->pointer = NULL;
538      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);
539      break;
540  }
541  if (tmpResource->pointer != NULL)
542    this->resourceList.push_back(tmpResource);
543  delete[] fullName;
544
545
546  if (tmpResource->pointer != NULL)
547    return tmpResource;
548  else
549  {
550    PRINTF(2)("Resource %s could not be loaded\n", fileName);
551    delete[] tmpResource->name;
552    delete tmpResource;
553    return NULL;
554  }
555}
556
557/**
558 * @brief unloads a Resource
559 * @param pointer: The pointer to free
560 * @param prio: the PriorityLevel to unload this resource
561 * @returns true if successful (pointer found, and deleted), false otherwise
562*/
563bool ResourceManager::unload(BaseObject* pointer, ResourcePriority prio)
564{
565  if (pointer == NULL)
566    return false;
567  // if pointer is existent. and only one resource of this type exists.
568  Resource* tmpResource = this->locateResourceByPointer(pointer);
569  if (tmpResource != NULL)
570    return unload(tmpResource, prio);
571  else
572  {
573    PRINTF(2)("Resource not Found %p\n", pointer);
574    return false;
575  }
576}
577
578/**
579 * @brief unloads a Resource
580 * @param resource: The resource to unloade
581 * @param prio the PriorityLevel to unload this resource
582 * @returns true on success, false otherwise.
583*/
584bool ResourceManager::unload(Resource* resource, ResourcePriority prio)
585{
586  if (resource == NULL)
587    return false;
588  if (resource->count > 0)
589    resource->count--;
590
591  if (resource->prio <= prio)
592  {
593    if (resource->count == 0)
594    {
595      // deleting the Resource
596      switch(resource->type)
597      {
598#ifndef NO_MODEL
599        case OBJ:
600        case PRIM:
601          delete (Model*)resource->pointer;
602          break;
603        case MD2:
604          delete (MD2Data*)resource->pointer;
605          break;
606#endif /* NO_MODEL */
607#ifndef NO_AUDIO
608        case WAV:
609          delete (SoundBuffer*)resource->pointer;
610          break;
611        case OGG:
612          delete (OggPlayer*)resource->pointer;
613          break;
614#endif /* NO_AUDIO */
615#ifndef NO_TEXT
616        case TTF:
617          delete (Font*)resource->pointer;
618          break;
619#endif /* NO_TEXT */
620#ifndef NO_TEXTURES
621        case IMAGE:
622          delete (Texture*)resource->pointer;
623          break;
624#endif /* NO_TEXTURES */
625#ifndef NO_SHADERS
626        case SHADER:
627          delete (Shader*)resource->pointer;
628          break;
629#endif /* NO_SHADERS */
630        default:
631          PRINTF(2)("NOT YET IMPLEMENTED !!FIX FIX!!\n");
632          return false;
633          break;
634      }
635      // deleting the List Entry:
636      PRINTF(4)("Resource %s safely removed.\n", resource->name);
637      delete[] resource->name;
638      std::vector<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource);
639      this->resourceList.erase(resourceIT);
640      delete resource;
641    }
642    else
643      PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name, resource->count);
644  }
645  else
646    PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name);
647  return true;
648}
649
650
651/**
652 * @brief unloads all alocated Memory of Resources with a pririty lower than prio
653 * @param prio The priority to delete
654*/
655bool ResourceManager::unloadAllByPriority(ResourcePriority prio)
656{
657  unsigned int removeCount;
658  for (unsigned int round = 0; round < 3; round++)
659  {
660    int index = this->resourceList.size() - 1;
661    removeCount = 0;
662    while (index >= 0)
663    {
664      if (this->resourceList[index]->prio <= prio)
665      {
666        if (this->resourceList[index]->count == 0)
667          unload(this->resourceList[index], prio);
668        else
669        {
670          if (round == 3)
671            PRINTF(2)("unable to unload %s because there are still %d references to it\n",
672                      this->resourceList[index]->name, this->resourceList[index]->count);
673          removeCount++;
674        }
675      }
676      index--;
677    }
678    if (removeCount == 0) break;
679  }
680}
681
682
683/**
684 * @brief Searches for a Resource by some information
685 * @param fileName: The name to look for
686 * @param type the Type of resource to locate.
687 * @param param0: an additional option to parse (see the constuctors for more help)
688 * @param param1: an additional option to parse (see the constuctors for more help)
689 * @param param2: an additional option to parse (see the constuctors for more help)
690 * @returns a Pointer to the Resource if found, NULL otherwise.
691*/
692Resource* ResourceManager::locateResourceByInfo(const char* fileName, ResourceType type,
693    const MultiType& param0, const MultiType& param1, const MultiType& param2) const
694{
695  std::vector<Resource*>::const_iterator resource;
696  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
697  {
698    if ((*resource)->type == type && !strcmp(fileName, (*resource)->name))
699    {
700      bool match = false;
701      switch (type)
702      {
703#ifndef NO_MODEL
704        case PRIM:
705        case OBJ:
706          if (param0 == MT_NULL)
707          {
708            if ((*resource)->param[0] == 1.0f)
709              match = true;
710          }
711          else if ((*resource)->param[0] == param0.getFloat())
712            match = true;
713          break;
714        case MD2:
715          if (param0 == MT_NULL)
716          {
717            if ((*resource)->param[0] == "")
718              match = true;
719          }
720          else if ((*resource)->param[0] == ((MultiType)param0).getString())
721            match = true;
722          break;
723#endif /* NO_MODEL */
724#ifndef NO_TEXT
725        case TTF:
726          if (param0 == MT_NULL)
727          {
728            if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE)
729              match = true;
730          }
731          else if ((*resource)->param[0] == param0.getInt())
732            match = true;
733          break;
734#endif /* NO_TEXT */
735#ifndef NO_SHADERS
736        case SHADER:
737          if (param0 == MT_NULL)
738          {
739            if ((*resource)->param[0] == "")
740              match = true;
741          }
742          else if ((*resource)->param[0] == ((MultiType)param0).getString())
743            match = true;
744#endif /* NO_SHADERS */
745#ifndef NO_TEXTURES
746        case IMAGE:
747          if (param0 == MT_NULL)
748          {
749            if ((*resource)->param[0] == GL_TEXTURE_2D)
750              match = true;
751          }
752          else if ((*resource)->param[0] ==  param0.getInt())
753            match = true;
754#endif /* NO_TEXTURES */
755        default:
756          match = true;
757          break;
758      }
759      if (match)
760      {
761        return (*resource);
762      }
763    }
764  }
765  return NULL;
766}
767
768/**
769 * @brief Searches for a Resource by Pointer
770 * @param pointer the Pointer to search for
771 * @returns a Pointer to the Resource if found, NULL otherwise.
772 */
773Resource* ResourceManager::locateResourceByPointer(const void* pointer) const
774{
775  //  Resource* enumRes = resourceList->enumerate();
776  std::vector<Resource*>::const_iterator resource;
777  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
778    if (pointer == (*resource)->pointer)
779      return (*resource);
780  return NULL;
781}
782
783char* ResourceManager::toResourcableString(unsigned int i)
784{
785  int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
786  len += strlen(this->resourceList[i]->name);
787  if (this->resourceList[i]->param[0].getString()) len += strlen(this->resourceList[i]->param[0].getString()) +1;
788  if (this->resourceList[i]->param[1].getString()) len += strlen(this->resourceList[i]->param[1].getString()) +1;
789  if (this->resourceList[i]->param[2].getString()) len += strlen(this->resourceList[i]->param[2].getString()) +1;
790  len += 10;
791  char* tmp = new char[len];
792  tmp[0] = '\0';
793  strcat( tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
794  strcat(tmp,",");
795  strcat (tmp, this->resourceList[i]->name);
796  if (this->resourceList[i]->param[0].getString() && this->resourceList[i]->param[0].getString() != '\0')
797  {
798    strcat(tmp,",");
799    strcat( tmp, this->resourceList[i]->param[0].getString());
800  }
801  if (this->resourceList[i]->param[1].getString() && this->resourceList[i]->param[1].getString() != '\0')
802  {
803    strcat(tmp,",");
804    strcat( tmp, this->resourceList[i]->param[1].getString());
805  }
806  if (this->resourceList[i]->param[2].getString() && this->resourceList[i]->param[2].getString() != '\0')
807  {
808    strcat(tmp,",");
809    strcat( tmp, this->resourceList[i]->param[2].getString());
810  }
811  return tmp;
812}
813
814/**
815 * @brief caches a Resource from a ResourceableString created with the toResourcableString-function
816 * @param resourceableString the String to cache the resource from.
817 */
818bool ResourceManager::fromResourceableString(const char* resourceableString)
819{
820  SubString splits(resourceableString, ',');
821  splits.debug();
822  if (splits.getCount() == 2)
823    this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
824                RP_LEVEL);
825  else if (splits.getCount() == 3)
826    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
827                RP_LEVEL, splits[2]);
828  else if (splits.getCount() == 4)
829    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
830                RP_LEVEL, splits[2], splits[3]);
831  else if (splits.getCount() == 5)
832    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
833                RP_LEVEL, splits[2], splits[3], splits[4]);
834}
835
836
837/**
838 * @brief Checks if it is a Directory
839 * @param directoryName the Directory to check for
840 * @returns true if it is a directory/symlink false otherwise
841*/
842bool ResourceManager::isDir(const char* directoryName)
843{
844  if (directoryName == NULL)
845    return false;
846
847  char* tmpDirName = NULL;
848  struct stat status;
849
850  // checking for the termination of the string given. If there is a "/" at the end cut it away
851  if (directoryName[strlen(directoryName)-1] == '/' ||
852      directoryName[strlen(directoryName)-1] == '\\')
853  {
854    tmpDirName = new char[strlen(directoryName)];
855    strncpy(tmpDirName, directoryName, strlen(directoryName)-1);
856    tmpDirName[strlen(directoryName)-1] = '\0';
857  }
858  else
859  {
860    tmpDirName = new char[strlen(directoryName)+1];
861    strcpy(tmpDirName, directoryName);
862  }
863
864  if(!stat(tmpDirName, &status))
865  {
866    if (status.st_mode & (S_IFDIR
867#ifndef __WIN32__
868                          | S_IFLNK
869#endif
870                         ))
871    {
872      delete[] tmpDirName;
873      return true;
874    }
875    else
876    {
877      delete[] tmpDirName;
878      return false;
879    }
880  }
881  else
882  {
883    delete[] tmpDirName;
884    return false;
885  }
886}
887
888/**
889 * @brief Checks if the file is either a Regular file or a Symlink
890 * @param fileName the File to check for
891 * @returns true if it is a regular file/symlink, false otherwise
892*/
893bool ResourceManager::isFile(const char* fileName)
894{
895  if (fileName == NULL)
896    return false;
897  char* tmpFileName = ResourceManager::homeDirCheck(fileName);
898  // actually checks the File
899  struct stat status;
900  if (!stat(tmpFileName, &status))
901  {
902    if (status.st_mode & (S_IFREG
903#ifndef __WIN32__
904                          | S_IFLNK
905#endif
906                         ))
907    {
908      delete[] tmpFileName;
909      return true;
910    }
911    else
912    {
913      delete[] tmpFileName;
914      return false;
915    }
916  }
917  else
918  {
919    delete[] tmpFileName;
920    return false;
921  }
922}
923
924/**
925 * @brief touches a File on the disk (thereby creating it)
926 * @param fileName The file to touch
927*/
928bool ResourceManager::touchFile(const char* fileName)
929{
930  char* tmpName = ResourceManager::homeDirCheck(fileName);
931  if (tmpName == NULL)
932    return false;
933  FILE* stream;
934  if( (stream = fopen (tmpName, "w")) == NULL)
935  {
936    PRINTF(1)("could not open %s fro writing\n", fileName);
937    delete[] tmpName;
938    return false;
939  }
940  fclose(stream);
941
942  delete[] tmpName;
943}
944
945/**
946 * @brief deletes a File from disk
947 * @param fileName the File to delete
948*/
949bool ResourceManager::deleteFile(const char* fileName)
950{
951  if (fileName == NULL)
952    return false;
953  char* tmpName = ResourceManager::homeDirCheck(fileName);
954  unlink(tmpName);
955  delete[] tmpName;
956}
957
958/**
959 * @param name the Name of the file to check
960 * @returns The name of the file, including the HomeDir
961 * IMPORTANT: this has to be deleted from the outside
962 */
963char* ResourceManager::homeDirCheck(const char* name)
964{
965  if (name == NULL)
966    return NULL;
967  char* retName;
968  if (!strncmp(name, "~/", 2))
969  {
970    char tmpFileName[500];
971#ifdef __WIN32__
972    strcpy(tmpFileName, getenv("USERPROFILE"));
973#else
974    strcpy(tmpFileName, getenv("HOME"));
975#endif
976    retName = new char[strlen(tmpFileName)+strlen(name)];
977    sprintf(retName, "%s%s", tmpFileName, name+1);
978  }
979  else
980  {
981    retName = new char[strlen(name)+1];
982    strcpy(retName, name);
983  }
984  return retName;
985}
986
987/**
988 * @param fileName the Name of the File to check
989 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist
990 * !!IMPORTANT: this has to be deleted from the outside!!
991*/
992char* ResourceManager::getFullName(const char* fileName)
993{
994  if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL)
995    return NULL;
996
997  char* retName = new char[strlen(ResourceManager::getInstance()->getDataDir())
998                           + strlen(fileName) + 1];
999  sprintf(retName, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName);
1000  if (ResourceManager::isFile(retName) || ResourceManager::isDir(retName))
1001    return retName;
1002  else
1003  {
1004    delete[] retName;
1005    return NULL;
1006  }
1007}
1008
1009
1010/**
1011 * @brief checks wether a file is in the DataDir.
1012 * @param fileName the File to check if it is in the Data-Dir structure.
1013 * @returns true if the file exists, false otherwise
1014 */
1015bool ResourceManager::isInDataDir(const char* fileName)
1016{
1017  if (fileName == NULL || ResourceManager::getInstance()->getDataDir() == NULL)
1018    return false;
1019
1020  bool retVal = false;
1021  char* checkFile = new char[strlen(ResourceManager::getInstance()->getDataDir())
1022                             + strlen(fileName) + 1];
1023  sprintf(checkFile, "%s%s", ResourceManager::getInstance()->getDataDir(), fileName);
1024
1025  if (ResourceManager::isFile(checkFile) || ResourceManager::isDir(checkFile))
1026    retVal = true;
1027  else
1028    retVal = false;
1029  delete[] checkFile;
1030  return retVal;
1031}
1032
1033
1034/**
1035 * @brief outputs debug information about the ResourceManager
1036 */
1037void ResourceManager::debug() const
1038{
1039  PRINT(0)("=RM===================================\n");
1040  PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
1041  PRINT(0)("======================================\n");
1042  // if it is not initialized
1043  PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef);
1044  PRINT(0)(" Data-Directory is: %s\n", this->dataDir);
1045  PRINT(0)(" List of Image-Directories: ");
1046  std::vector<char*>::const_iterator imageDir;
1047  for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
1048    PRINT(0)("%s ", (*imageDir));
1049  PRINT(0)("\n");
1050
1051  PRINT(0)("List of all stored Resources:\n");
1052  std::vector<Resource*>::const_iterator resource;
1053  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
1054
1055  {
1056    PRINT(0)("-----------------------------------------\n");
1057    PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name, (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));
1058
1059    PRINT(0)("gets deleted at ");
1060    switch((*resource)->prio)
1061    {
1062      default:
1063      case RP_NO:
1064        PRINT(0)("first posibility (0)\n");
1065        break;
1066      case RP_LEVEL:
1067        PRINT(0)("the end of the Level (1)\n");
1068        break;
1069      case RP_CAMPAIGN:
1070        PRINT(0)("the end of the campaign (2)\n");
1071        break;
1072      case RP_GAME:
1073        PRINT(0)("when leaving the game (3)\n");
1074        break;
1075    }
1076  }
1077
1078
1079
1080  PRINT(0)("==================================RM==\n");
1081}
1082
1083
1084/**
1085 * @brief converts a ResourceType into the corresponding String
1086 * @param type the ResourceType to translate
1087 * @returns the converted String.
1088 */
1089const char* ResourceManager::ResourceTypeToChar(ResourceType type)
1090{
1091  return ResourceManager::resourceNames[type];
1092}
1093
1094/**
1095 * @brief converts a String into a ResourceType (good for loading)
1096 * @param resourceType the name of the Type
1097 * @returns the Number of the Type, or 0 (defautl) if not found.
1098 */
1099ResourceType ResourceManager::stringToResourceType(const char* resourceType)
1100{
1101  assert(resourceType != NULL);
1102  for (unsigned int i = 0; i < RESOURCE_TYPE_SIZE; i++)
1103    if (!strcmp(resourceType, ResourceManager::resourceNames[i]))
1104      return (ResourceType)i;
1105  return (ResourceType)0;
1106}
1107
1108/**
1109 * The Names of the ResourceTypes
1110 */
1111const char* ResourceManager::resourceNames[] =
1112  {
1113#ifndef NO_MODEL
1114    "ObjectModel",
1115    "PrimitiveModel",
1116    "MD2-Data",
1117#endif
1118#ifndef NO_TEXT
1119    "Font",
1120#endif
1121#ifndef NO_AUDIO
1122    "Wav",
1123    "mp3",
1124    "ogg",
1125#endif
1126#ifndef NO_TEXTURES
1127    "Texture",
1128#endif
1129#ifndef NO_SHADERS
1130    "Shader",
1131#endif
1132
1133  };
Note: See TracBrowser for help on using the repository browser.