Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: fixed most -Wall warnings… but there are still many missing :/

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