Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

moved the File-Classes

File size: 26.5 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 "util/loading/resource_manager.h"
19#include "substring.h"
20#include "debug.h"
21
22#include <algorithm>
23#include <assert.h>
24
25// different resource Types
26#ifndef NO_MODEL
27#include "objModel.h"
28#include "primitive_model.h"
29#include "md2Model.h"
30#endif /* NO_MODEL */
31#ifndef NO_TEXTURES
32#include "texture.h"
33#endif /* NO_TEXTURES */
34#ifndef NO_TEXT
35#include "font.h"
36#endif /* NO_TEXT */
37#ifndef NO_AUDIO
38#include "sound_buffer.h"
39#include "ogg_player.h"
40#endif /* NO_AUDIO */
41#ifndef NO_SHADERS
42#include "shader.h"
43#endif /* NO_SHADERS */
44
45// File Handling Includes
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <unistd.h>
49
50using namespace std;
51
52/**
53 * @brief standard constructor
54*/
55ResourceManager::ResourceManager ()
56{
57  this->setClassID(CL_RESOURCE_MANAGER, "ResourceManager");
58  this->setName("ResourceManager");
59
60  this->dataDir = "./";
61  this->tryDataDir("./data");
62}
63
64//! Singleton Reference to the ResourceManager
65ResourceManager* ResourceManager::singletonRef = NULL;
66
67/**
68 * @brief 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  ResourceManager::singletonRef = NULL;
79}
80
81/**
82 * @brief sets the data main directory
83 * @param dataDir the DataDirectory.
84 */
85bool ResourceManager::setDataDir(const std::string& dataDir)
86{
87  File dataDirectory(dataDir);
88  if (dataDirectory.isDirectory())
89  {
90    this->dataDir = dataDirectory.name();
91
92    if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\')
93    {
94      this->dataDir += '/';
95    }
96    return true;
97  }
98  else
99  {
100    PRINTF(1)("%s is not a Directory, and can not be the Data Directory, leaving as %s \n", dataDir.c_str(), this->dataDir.c_str());
101    return false;
102  }
103}
104
105/**
106 * @brief sets the data main directory
107 * @param dataDir the DataDirectory.
108 *
109 * this is essentially the same as setDataDir, but it ommits the error-message
110 */
111bool ResourceManager::tryDataDir(const std::string& dataDir)
112{
113  File dataDirectory(dataDir);
114  if (dataDirectory.isDirectory())
115  {
116    this->dataDir = dataDirectory.name();
117
118    if (dataDir[dataDir.size()-1] != '/' && dataDir[dataDir.size()-1] != '\\')
119    {
120      this->dataDir += '/';
121    }
122    return true;
123  }
124  return false;
125}
126
127
128/**
129 * @brief checks for the DataDirectory, by looking if
130 * @param fileInside is iniside of the given directory.
131*/
132bool ResourceManager::verifyDataDir(const std::string& fileInside)
133{
134  File dataDirectory(this->dataDir);
135  if (!dataDirectory.isDirectory())
136  {
137    PRINTF(1)("'%s' is not a directory\n", this->dataDir.c_str());
138    return false;
139  }
140
141  File testFile(this->dataDir + fileInside);
142  return testFile.isFile();
143}
144
145#ifndef NO_TEXTURES
146/**
147 * @brief adds a new Path for Images
148 * @param imageDir The path to insert
149 * @returns true, if the Path was well and injected (or already existent within the list)
150   false otherwise
151*/
152bool ResourceManager::addImageDir(const std::string& imageDir)
153{
154  std::string newDir = imageDir;
155  if (imageDir[imageDir.size()-1] != '/' && imageDir[imageDir.size()-1] != '\\')
156  {
157    newDir += '/';
158  }
159  // check if the param is a Directory
160  if (File(newDir).isDirectory())
161  {
162    // check if the Directory has been added before
163    std::vector<std::string>::const_iterator imageDir;
164    for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
165    {
166      if (*imageDir == newDir)
167      {
168        PRINTF(3)("Path %s already loaded\n", newDir.c_str());
169        return true;
170      }
171    }
172    // adding the directory to the List
173    this->imageDirs.push_back(newDir);
174    return true;
175  }
176  else
177  {
178    PRINTF(1)("%s is not a Directory, and can not be added to the Paths of Images\n", newDir.c_str());
179    return false;
180  }
181}
182#endif /* NO_TEXTURES */
183
184/**
185 * @brief loads resources
186 * @param fileName: The fileName of the resource to load
187 * @param prio: The ResourcePriority of this resource (will only be increased)
188 * @param param0: an additional option to parse (see the constuctors for more help)
189 * @param param1: an additional option to parse (see the constuctors for more help)
190 * @param param2: an additional option to parse (see the constuctors for more help)
191 * @returns a pointer to a desired Resource.
192*/
193BaseObject* ResourceManager::load(const std::string& fileName, ResourcePriority prio,
194                                  const MultiType& param0, const MultiType& param1, const MultiType& param2)
195{
196  ResourceType tmpType;
197#ifndef NO_MODEL
198#define __IF_OK
199  if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".obj", 4))
200    tmpType = OBJ;
201  else if (!strncmp(fileName.c_str()+(fileName.size()-4), ".md2", 4))
202    tmpType = MD2;
203  else if (!strcasecmp(fileName.c_str(), "cube") ||
204            !strcasecmp(fileName.c_str(), "sphere") ||
205            !strcasecmp(fileName.c_str(), "plane") ||
206            !strcasecmp(fileName.c_str(), "cylinder") ||
207            !strcasecmp(fileName.c_str(), "cone"))
208    tmpType = PRIM;
209#endif /* NO_MODEL */
210#ifndef NO_AUDIO
211#ifdef __IF_OK
212  else
213#endif
214#define __IF_OK
215    if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".wav", 4))
216      tmpType = WAV;
217    else if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".mp3", 4))
218      tmpType = MP3;
219    else if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".ogg", 4))
220      tmpType = OGG;
221#endif /* NO_AUDIO */
222#ifndef NO_TEXT
223#ifdef __IF_OK
224    else
225#endif
226#define __IF_OK
227      if (!strncasecmp(fileName.c_str()+(fileName.size()-4), ".ttf", 4))
228        tmpType = TTF;
229#endif /* NO_TEXT */
230#ifndef NO_SHADERS
231#ifdef __IF_OK
232      else
233#endif
234#define __IF_OK
235        if (!strncasecmp(fileName.c_str()+(fileName.size()-5), ".vert", 5))
236          tmpType = SHADER;
237#endif /* NO_SHADERS */
238#ifndef NO_TEXTURES
239#ifdef __IF_OK
240        else
241#else
242  if
243#endif
244          tmpType = IMAGE;
245#endif /* NO_TEXTURES */
246#undef __IF_OK
247  return this->load(fileName, tmpType, prio, param0, param1, param2);
248}
249
250/**
251 * @brief caches a Resource
252 *
253 * @see load;
254 *
255 * @brief returns true if ok, false otherwise.
256 * This function loads a Resource without applying it to an Object.
257 * This is for loading purposes, e.g, when the user is loading a Resource
258 * during the initialisation instead of at Runtime.
259 */
260bool ResourceManager::cache(const std::string& fileName, ResourceType type, ResourcePriority prio,
261                            const MultiType& param0, const MultiType& param1, const MultiType& param2)
262{
263  // searching if the resource was loaded before.
264  Resource* tmpResource;
265  // check if we already loaded this Resource
266  tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
267  // otherwise load it
268  if (tmpResource == NULL)
269    tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
270  // return cached pointer.
271  if (tmpResource != NULL) // if the resource was loaded before.
272  {
273    if(tmpResource->prio < prio)
274      tmpResource->prio = prio;
275    return true;
276  }
277  else
278    return false;
279}
280
281/**
282 * tells the ResourceManager to generate a Copy of the Resource.
283 * @brief resourcePointer: The Pointer to the resource to copy
284 * @returns the Resource pointed to resourcePointer.
285 */
286BaseObject* ResourceManager::copy(BaseObject* resourcePointer)
287{
288  Resource* tmp = locateResourceByPointer(resourcePointer);
289  if (tmp!=NULL)
290  {
291    tmp->count++;
292    return tmp->pointer;
293  }
294  else
295    return NULL;
296}
297
298
299/**
300 * @brief loads resources
301 * @param fileName: The fileName of the resource to load
302 * @param type: The Type of Resource to load.
303 * @param prio: The ResourcePriority of this resource (will only be increased)
304 * @param param0: an additional option to parse (see the constuctors for more help)
305 * @param param1: an additional option to parse (see the constuctors for more help)
306 * @param param2: an additional option to parse (see the constuctors for more help)
307 * @returns a pointer to a desired Resource.
308*/
309BaseObject* ResourceManager::load(const std::string& fileName, ResourceType type, ResourcePriority prio,
310                                  const MultiType& param0, const MultiType& param1, const MultiType& param2)
311{
312
313  // searching if the resource was loaded before.
314  Resource* tmpResource;
315  // check if we already loaded this Resource
316  tmpResource = this->locateResourceByInfo(fileName, type, param0, param1, param2);
317  // otherwise load it
318  if (tmpResource == NULL)
319  {
320    tmpResource = this->loadResource(fileName, type, prio, param0, param1, param2);
321  }
322  // return cached pointer.
323  if (tmpResource != NULL) // if the resource was loaded before.
324  {
325    tmpResource->count++;
326    if(tmpResource->prio < prio)
327      tmpResource->prio = prio;
328
329    return tmpResource->pointer;
330  }
331  else
332    return NULL;
333}
334
335
336/**
337 * @brief loads resources for internal purposes
338 * @param fileName: The fileName of the resource to load
339 * @param type: The Type of Resource to load.
340 * @param prio: The ResourcePriority of this resource (will only be increased)
341 * @param param0: an additional option to parse (see the constuctors for more help)
342 * @param param1: an additional option to parse (see the constuctors for more help)
343 * @param param2: an additional option to parse (see the constuctors for more help)
344 * @returns a pointer to a desired Resource.
345 */
346Resource* ResourceManager::loadResource(const std::string& fileName, ResourceType type, ResourcePriority prio,
347                                        const MultiType& param0, const MultiType& param1, const MultiType& param2)
348{
349  // Setting up the new Resource
350  Resource* tmpResource = new Resource;
351  tmpResource->count = 0;
352  tmpResource->type = type;
353  tmpResource->prio = prio;
354  tmpResource->pointer = NULL;
355  tmpResource->name = fileName;
356
357  // creating the full name. (directoryName + FileName)
358  std::string fullName = ResourceManager::getFullName(fileName);
359  // Checking for the type of resource \see ResourceType
360  switch(type)
361  {
362#ifndef NO_MODEL
363    case OBJ:
364      if (param0.getType() != MT_NULL)
365        tmpResource->param[0] = param0;
366      else
367        tmpResource->param[0] = 1.0f;
368
369      if(File(fullName).isFile())
370        tmpResource->pointer = new OBJModel(fullName, tmpResource->param[0].getFloat());
371      else
372      {
373        PRINTF(2)("File %s in %s does not exist. Loading a cube-Model instead\n", fileName.c_str(), dataDir.c_str());
374        tmpResource->pointer = ResourceManager::load("cube", PRIM, prio, tmpResource->param[0].getFloat());
375      }
376      break;
377    case PRIM:
378      if (param0 != MT_NULL)
379        tmpResource->param[0] = param0;
380      else
381        tmpResource->param[0] = 1.0f;
382
383      if (tmpResource->name == "cube")
384        tmpResource->pointer = new PrimitiveModel(PRIM_CUBE, tmpResource->param[0].getFloat());
385      else if (tmpResource->name == "sphere")
386        tmpResource->pointer = new PrimitiveModel(PRIM_SPHERE, tmpResource->param[0].getFloat());
387      else if (tmpResource->name == "plane")
388        tmpResource->pointer = new PrimitiveModel(PRIM_PLANE, tmpResource->param[0].getFloat());
389      else if (tmpResource->name == "cylinder")
390        tmpResource->pointer = new PrimitiveModel(PRIM_CYLINDER, tmpResource->param[0].getFloat());
391      else if (tmpResource->name == "cone")
392        tmpResource->pointer = new PrimitiveModel(PRIM_CONE, tmpResource->param[0].getFloat());
393      break;
394    case MD2:
395      if(File(fullName).isFile())
396      {
397        tmpResource->param[0] = param0;
398        tmpResource->param[1] = param1;
399        tmpResource->pointer = new MD2Data(fullName, tmpResource->param[0].getCString(), tmpResource->param[1].getFloat());
400      }
401      break;
402#endif /* NO_MODEL */
403#ifndef NO_TEXT
404    case TTF:
405      if (param0 != MT_NULL)
406      {
407        assert(param0.getInt() >= 0);
408        tmpResource->param[0] = param0;
409      }
410      else
411        tmpResource->param[0] = FONT_DEFAULT_RENDER_SIZE;
412
413      if(File(fullName).isFile())
414        tmpResource->pointer = new Font(fullName, (unsigned int) tmpResource->param[0].getInt());
415      else
416        PRINTF(2)("%s does not exist in %s. Not loading Font\n", fileName.c_str(), this->dataDir.c_str());
417      break;
418#endif /* NO_TEXT */
419#ifndef NO_AUDIO
420    case WAV:
421      if(File(fullName).isFile())
422        tmpResource->pointer = new OrxSound::SoundBuffer(fullName);
423      break;
424    case OGG:
425      if (File(fullName).isFile())
426        tmpResource->pointer = new OrxSound::OggPlayer(fullName);
427      break;
428#endif /* NO_AUDIO */
429#ifndef NO_TEXTURES
430    case IMAGE:
431      if (param0 != MT_NULL)
432        tmpResource->param[0] = param0;
433      else
434        tmpResource->param[0] = GL_TEXTURE_2D;
435      if(File(fullName).isFile())
436      {
437        PRINTF(4)("Image %s resides to %s\n", fileName.c_str(), fullName.c_str());
438        tmpResource->pointer = new Texture(fullName, tmpResource->param[0].getInt());
439      }
440      else
441      {
442        std::vector<std::string>::iterator imageDir;
443        for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
444        {
445          std::string imgName = *imageDir + fileName;
446          if(File(imgName).isFile())
447          {
448            PRINTF(4)("Image %s resides to %s\n", fileName.c_str(), imgName.c_str());
449            tmpResource->pointer = new Texture(imgName, tmpResource->param[0].getInt());
450            break;
451          }
452        }
453      }
454      if(!tmpResource)
455        PRINTF(2)("!!Image %s not Found!!\n", fileName.c_str());
456      break;
457#endif /* NO_TEXTURES */
458#ifndef NO_SHADERS
459    case SHADER:
460      if(File(fullName).isFile())
461      {
462        if (param0 != MT_NULL)
463        {
464          MultiType param = param0; /// HACK
465          std::string secFullName = ResourceManager::getFullName(param.getCString());
466          if (File(secFullName).isFile())
467          {
468            tmpResource->param[0] = secFullName;
469            tmpResource->pointer = new Shader(fullName, secFullName);
470          }
471        }
472        else
473        {
474          tmpResource->param[0] = param0;
475          tmpResource->pointer = new Shader(fullName, "");
476        }
477      }
478      break;
479#endif /* NO_SHADERS */
480    default:
481      tmpResource->pointer = NULL;
482      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.c_str());
483      break;
484  }
485  if (tmpResource->pointer != NULL)
486    this->resourceList.push_back(tmpResource);
487
488  if (tmpResource->pointer != NULL)
489    return tmpResource;
490  else
491  {
492    PRINTF(2)("Resource %s could not be loaded\n", fileName.c_str());
493    delete tmpResource;
494    return NULL;
495  }
496}
497
498/**
499 * @brief unloads a Resource
500 * @param pointer: The pointer to free
501 * @param prio: the PriorityLevel to unload this resource
502 * @returns true if successful (pointer found, and deleted), false otherwise
503*/
504bool ResourceManager::unload(BaseObject* pointer, ResourcePriority prio)
505{
506  if (pointer == NULL)
507    return false;
508  // if pointer is existent. and only one resource of this type exists.
509  Resource* tmpResource = this->locateResourceByPointer(pointer);
510  if (tmpResource != NULL)
511    return unload(tmpResource, prio);
512  else
513  {
514    PRINTF(2)("Resource not Found %p\n", pointer);
515    return false;
516  }
517}
518
519/**
520 * @brief unloads a Resource
521 * @param resource: The resource to unloade
522 * @param prio the PriorityLevel to unload this resource
523 * @returns true on success, false otherwise.
524*/
525bool ResourceManager::unload(Resource* resource, ResourcePriority prio)
526{
527  if (resource == NULL)
528    return false;
529  if (resource->count > 0)
530    resource->count--;
531
532  if (resource->prio <= prio)
533  {
534    if (resource->count == 0)
535    {
536      delete resource->pointer;
537      // deleting the List Entry:
538      PRINTF(4)("Resource %s safely removed.\n", resource->name.c_str());
539      std::vector<Resource*>::iterator resourceIT = std::find(this->resourceList.begin(), this->resourceList.end(), resource);
540      this->resourceList.erase(resourceIT);
541      delete resource;
542    }
543    else
544      PRINTF(4)("Resource %s not removed, because there are still %d References to it.\n", resource->name.c_str(), resource->count);
545  }
546  else
547    PRINTF(4)("not deleting resource %s because DeleteLevel to high\n", resource->name.c_str());
548  return true;
549}
550
551
552/**
553 * @brief unloads all alocated Memory of Resources with a pririty lower than prio
554 * @param prio The priority to delete
555*/
556bool ResourceManager::unloadAllByPriority(ResourcePriority prio)
557{
558  bool removedAll = true;
559  unsigned int removeCount;
560  for (unsigned int round = 0; round < 3; round++)
561  {
562    int index = this->resourceList.size() - 1;
563    removeCount = 0;
564    while (index >= 0)
565    {
566      if (this->resourceList[index]->prio <= prio)
567      {
568        if (this->resourceList[index]->count == 0)
569          unload(this->resourceList[index], prio);
570        else
571        {
572          if (round == 3)
573          {
574            PRINTF(2)("unable to unload %s because there are still %d references to it\n",
575                      this->resourceList[index]->name.c_str(), this->resourceList[index]->count);
576            removedAll = false;
577          }
578          removeCount++;
579        }
580      }
581      index--;
582    }
583    if (removeCount == 0) break;
584  }
585  return removedAll;
586}
587
588
589/**
590 * @brief Searches for a Resource by some information
591 * @param fileName: The name to look for
592 * @param type the Type of resource to locate.
593 * @param param0: an additional option to parse (see the constuctors for more help)
594 * @param param1: an additional option to parse (see the constuctors for more help)
595 * @param param2: an additional option to parse (see the constuctors for more help)
596 * @returns a Pointer to the Resource if found, NULL otherwise.
597*/
598Resource* ResourceManager::locateResourceByInfo(const std::string& fileName, ResourceType type,
599    const MultiType& param0, const MultiType& param1, const MultiType& param2) const
600{
601  std::vector<Resource*>::const_iterator resource;
602  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
603  {
604    if ((*resource)->type == type && fileName == (*resource)->name)
605    {
606      bool match = false;
607      switch (type)
608      {
609#ifndef NO_MODEL
610        case PRIM:
611        case OBJ:
612          if (param0 == MT_NULL)
613          {
614            if ((*resource)->param[0] == 1.0f)
615              match = true;
616          }
617          else if ((*resource)->param[0] == param0.getFloat())
618            match = true;
619          break;
620        case MD2:
621          if (param0 == MT_NULL && ((*resource)->param[0] == "") && param1 == MT_NULL && ((*resource)->param[0] == 1.0f))
622              match = true;
623          else if ((*resource)->param[0] == ((MultiType)param0).getString() && (*resource)->param[1] == ((MultiType)param1).getFloat())
624            match = true;
625          break;
626#endif /* NO_MODEL */
627#ifndef NO_TEXT
628        case TTF:
629          if (param0 == MT_NULL)
630          {
631            if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE)
632              match = true;
633          }
634          else if ((*resource)->param[0] == param0.getInt())
635            match = true;
636          break;
637#endif /* NO_TEXT */
638#ifndef NO_SHADERS
639        case SHADER:
640          if (param0 == MT_NULL)
641          {
642            if ((*resource)->param[0] == "")
643              match = true;
644          }
645          else if ((*resource)->param[0] == ((MultiType)param0).getString())
646            match = true;
647          break;
648#endif /* NO_SHADERS */
649#ifndef NO_TEXTURES
650        case IMAGE:
651          if (param0 == MT_NULL)
652          {
653            if ((*resource)->param[0] == GL_TEXTURE_2D)
654              match = true;
655          }
656          else if ((*resource)->param[0] ==  param0.getInt())
657            match = true;
658          break;
659#endif /* NO_TEXTURES */
660        default:
661          match = true;
662          break;
663      }
664      if (match)
665      {
666        return (*resource);
667      }
668    }
669  }
670  return NULL;
671}
672
673/**
674 * @brief Searches for a Resource by Pointer
675 * @param pointer the Pointer to search for
676 * @returns a Pointer to the Resource if found, NULL otherwise.
677 */
678Resource* ResourceManager::locateResourceByPointer(const void* pointer) const
679{
680  //  Resource* enumRes = resourceList->enumerate();
681  std::vector<Resource*>::const_iterator resource;
682  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
683    if (pointer == (*resource)->pointer)
684      return (*resource);
685  return NULL;
686}
687
688std::string ResourceManager::toResourcableString(unsigned int i)
689{
690/*  int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
691  len += this->resourceList[i]->name.size();
692  if (this->resourceList[i]->param[0].getCString()) len += strlen(this->resourceList[i]->param[0].getCString()) +1;
693  if (this->resourceList[i]->param[1].getCString()) len += strlen(this->resourceList[i]->param[1].getCString()) +1;
694  if (this->resourceList[i]->param[2].getCString()) len += strlen(this->resourceList[i]->param[2].getCString()) +1;
695  len += 10;
696  std::string tmp = new char[len];
697  tmp[0] = '\0';
698  strcat(tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
699  strcat(tmp,",");
700  strcat (tmp, this->resourceList[i]->name);
701  if (this->resourceList[i]->param[0].getCString() && this->resourceList[i]->param[0].getCString() != '\0')
702  {
703    strcat(tmp,",");
704    strcat( tmp, this->resourceList[i]->param[0].getCString());
705  }
706  if (this->resourceList[i]->param[1].getCString() && this->resourceList[i]->param[1].getCString() != '\0')
707  {
708    strcat(tmp,",");
709    strcat( tmp, this->resourceList[i]->param[1].getCString());
710  }
711  if (this->resourceList[i]->param[2].getCString() && this->resourceList[i]->param[2].getCString() != '\0')
712  {
713    strcat(tmp,",");
714    strcat( tmp, this->resourceList[i]->param[2].getCString());
715  }
716  return tmp;*/
717  return "";
718}
719
720/**
721 * @brief caches a Resource from a ResourceableString created with the toResourcableString-function
722 * @param resourceableString the String to cache the resource from.
723 */
724bool ResourceManager::fromResourceableString(const std::string& resourceableString)
725{
726/*  SubString splits(resourceableString, ',');
727  splits.debug();
728  if (splits.getCount() == 2)
729    this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
730                RP_LEVEL);
731  else if (splits.getCount() == 3)
732    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
733                RP_LEVEL, splits[2]);
734  else if (splits.getCount() == 4)
735    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
736                RP_LEVEL, splits[2], splits[3]);
737  else if (splits.getCount() == 5)
738    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
739                RP_LEVEL, splits[2], splits[3], splits[4]);*/
740  return false;
741}
742
743
744/**
745 * @param fileName the Name of the File to check
746 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist
747 * !!IMPORTANT: this has to be deleted from the outside!!
748*/
749std::string ResourceManager::getFullName(const std::string& fileName)
750{
751  if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty())
752    return "";
753
754  std::string retName = ResourceManager::getInstance()->getDataDir() +fileName;
755  if (File(retName).isFile() || File(retName).isDirectory())
756    return retName;
757  else
758    return "";
759}
760
761
762/**
763 * @brief checks wether a file is in the DataDir.
764 * @param fileName the File to check if it is in the Data-Dir structure.
765 * @returns true if the file exists, false otherwise
766 */
767bool ResourceManager::isInDataDir(const std::string& fileName)
768{
769  if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty())
770    return false;
771
772  bool retVal = false;
773  std::string checkFile = ResourceManager::getInstance()->getDataDir() + fileName;
774
775  if (File(checkFile).exists())
776    retVal = true;
777  else
778    retVal = false;
779  return retVal;
780}
781
782
783/**
784 * @brief outputs debug information about the ResourceManager
785 */
786void ResourceManager::debug() const
787{
788  PRINT(0)("=RM===================================\n");
789  PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
790  PRINT(0)("======================================\n");
791  // if it is not initialized
792  PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef);
793  PRINT(0)(" Data-Directory is: %s\n", this->dataDir.c_str());
794  PRINT(0)(" List of Image-Directories: ");
795  std::vector<std::string>::const_iterator imageDir;
796  for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
797    PRINT(0)("%s ", (*imageDir).c_str());
798  PRINT(0)("\n");
799
800  PRINT(0)("List of all stored Resources:\n");
801  std::vector<Resource*>::const_iterator resource;
802  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
803
804  {
805    PRINT(0)("-----------------------------------------\n");
806    PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name.c_str(), (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));
807
808    PRINT(0)("gets deleted at ");
809    switch((*resource)->prio)
810    {
811      default:
812      case RP_NO:
813        PRINT(0)("first posibility (0)\n");
814        break;
815      case RP_LEVEL:
816        PRINT(0)("the end of the Level (1)\n");
817        break;
818      case RP_CAMPAIGN:
819        PRINT(0)("the end of the campaign (2)\n");
820        break;
821      case RP_GAME:
822        PRINT(0)("when leaving the game (3)\n");
823        break;
824    }
825  }
826
827
828
829  PRINT(0)("==================================RM==\n");
830}
831
832
833/**
834 * @brief converts a ResourceType into the corresponding String
835 * @param type the ResourceType to translate
836 * @returns the converted String.
837 */
838const char* ResourceManager::ResourceTypeToChar(ResourceType type)
839{
840  return ResourceManager::resourceNames[type];
841}
842
843/**
844 * @brief converts a String into a ResourceType (good for loading)
845 * @param resourceType the name of the Type
846 * @returns the Number of the Type, or 0 (defautl) if not found.
847 */
848ResourceType ResourceManager::stringToResourceType(const std::string& resourceType)
849{
850  for (unsigned int i = 0; i < RESOURCE_TYPE_SIZE; i++)
851    if (resourceType == ResourceManager::resourceNames[i])
852      return (ResourceType)i;
853  return (ResourceType)0;
854}
855
856/**
857 * The Names of the ResourceTypes
858 */
859const char* ResourceManager::resourceNames[] =
860  {
861#ifndef NO_MODEL
862    "ObjectModel",
863    "PrimitiveModel",
864    "MD2-Data",
865#endif
866#ifndef NO_TEXT
867    "Font",
868#endif
869#ifndef NO_AUDIO
870    "Wav",
871    "mp3",
872    "ogg",
873#endif
874#ifndef NO_TEXTURES
875    "Texture",
876#endif
877#ifndef NO_SHADERS
878    "Shader",
879#endif
880
881  };
Note: See TracBrowser for help on using the repository browser.