Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/qt_gui/src/lib/util/loading/resource_manager.cc @ 7640

Last change on this file since 7640 was 7616, checked in by bensch, 19 years ago

more file stuff in File-class

File size: 26.4 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, fullName);
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, imgName);
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  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            PRINTF(2)("unable to unload %s because there are still %d references to it\n",
574                      this->resourceList[index]->name.c_str(), this->resourceList[index]->count);
575          removeCount++;
576        }
577      }
578      index--;
579    }
580    if (removeCount == 0) break;
581  }
582}
583
584
585/**
586 * @brief Searches for a Resource by some information
587 * @param fileName: The name to look for
588 * @param type the Type of resource to locate.
589 * @param param0: an additional option to parse (see the constuctors for more help)
590 * @param param1: an additional option to parse (see the constuctors for more help)
591 * @param param2: an additional option to parse (see the constuctors for more help)
592 * @returns a Pointer to the Resource if found, NULL otherwise.
593*/
594Resource* ResourceManager::locateResourceByInfo(const std::string& fileName, ResourceType type,
595    const MultiType& param0, const MultiType& param1, const MultiType& param2) const
596{
597  std::vector<Resource*>::const_iterator resource;
598  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
599  {
600    if ((*resource)->type == type && fileName == (*resource)->name)
601    {
602      bool match = false;
603      switch (type)
604      {
605#ifndef NO_MODEL
606        case PRIM:
607        case OBJ:
608          if (param0 == MT_NULL)
609          {
610            if ((*resource)->param[0] == 1.0f)
611              match = true;
612          }
613          else if ((*resource)->param[0] == param0.getFloat())
614            match = true;
615          break;
616        case MD2:
617          if (param0 == MT_NULL && ((*resource)->param[0] == "") && param1 == MT_NULL && ((*resource)->param[0] == 1.0f))
618              match = true;
619          else if ((*resource)->param[0] == ((MultiType)param0).getString() && (*resource)->param[1] == ((MultiType)param1).getFloat())
620            match = true;
621          break;
622#endif /* NO_MODEL */
623#ifndef NO_TEXT
624        case TTF:
625          if (param0 == MT_NULL)
626          {
627            if ((*resource)->param[0] == FONT_DEFAULT_RENDER_SIZE)
628              match = true;
629          }
630          else if ((*resource)->param[0] == param0.getInt())
631            match = true;
632          break;
633#endif /* NO_TEXT */
634#ifndef NO_SHADERS
635        case SHADER:
636          if (param0 == MT_NULL)
637          {
638            if ((*resource)->param[0] == "")
639              match = true;
640          }
641          else if ((*resource)->param[0] == ((MultiType)param0).getString())
642            match = true;
643          break;
644#endif /* NO_SHADERS */
645#ifndef NO_TEXTURES
646        case IMAGE:
647          if (param0 == MT_NULL)
648          {
649            if ((*resource)->param[0] == GL_TEXTURE_2D)
650              match = true;
651          }
652          else if ((*resource)->param[0] ==  param0.getInt())
653            match = true;
654          break;
655#endif /* NO_TEXTURES */
656        default:
657          match = true;
658          break;
659      }
660      if (match)
661      {
662        return (*resource);
663      }
664    }
665  }
666  return NULL;
667}
668
669/**
670 * @brief Searches for a Resource by Pointer
671 * @param pointer the Pointer to search for
672 * @returns a Pointer to the Resource if found, NULL otherwise.
673 */
674Resource* ResourceManager::locateResourceByPointer(const void* pointer) const
675{
676  //  Resource* enumRes = resourceList->enumerate();
677  std::vector<Resource*>::const_iterator resource;
678  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
679    if (pointer == (*resource)->pointer)
680      return (*resource);
681  return NULL;
682}
683
684std::string ResourceManager::toResourcableString(unsigned int i)
685{
686/*  int len = strlen(ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
687  len += this->resourceList[i]->name.size();
688  if (this->resourceList[i]->param[0].getCString()) len += strlen(this->resourceList[i]->param[0].getCString()) +1;
689  if (this->resourceList[i]->param[1].getCString()) len += strlen(this->resourceList[i]->param[1].getCString()) +1;
690  if (this->resourceList[i]->param[2].getCString()) len += strlen(this->resourceList[i]->param[2].getCString()) +1;
691  len += 10;
692  std::string tmp = new char[len];
693  tmp[0] = '\0';
694  strcat(tmp, ResourceManager::ResourceTypeToChar(this->resourceList[i]->type));
695  strcat(tmp,",");
696  strcat (tmp, this->resourceList[i]->name);
697  if (this->resourceList[i]->param[0].getCString() && this->resourceList[i]->param[0].getCString() != '\0')
698  {
699    strcat(tmp,",");
700    strcat( tmp, this->resourceList[i]->param[0].getCString());
701  }
702  if (this->resourceList[i]->param[1].getCString() && this->resourceList[i]->param[1].getCString() != '\0')
703  {
704    strcat(tmp,",");
705    strcat( tmp, this->resourceList[i]->param[1].getCString());
706  }
707  if (this->resourceList[i]->param[2].getCString() && this->resourceList[i]->param[2].getCString() != '\0')
708  {
709    strcat(tmp,",");
710    strcat( tmp, this->resourceList[i]->param[2].getCString());
711  }
712  return tmp;*/
713}
714
715/**
716 * @brief caches a Resource from a ResourceableString created with the toResourcableString-function
717 * @param resourceableString the String to cache the resource from.
718 */
719bool ResourceManager::fromResourceableString(const std::string& resourceableString)
720{
721/*  SubString splits(resourceableString, ',');
722  splits.debug();
723  if (splits.getCount() == 2)
724    this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
725                RP_LEVEL);
726  else if (splits.getCount() == 3)
727    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
728                RP_LEVEL, splits[2]);
729  else if (splits.getCount() == 4)
730    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
731                RP_LEVEL, splits[2], splits[3]);
732  else if (splits.getCount() == 5)
733    return this->cache(splits[1], ResourceManager::stringToResourceType(splits[0]),
734                RP_LEVEL, splits[2], splits[3], splits[4]);*/
735}
736
737
738/**
739 * @param fileName the Name of the File to check
740 * @returns The full name of the file, including the DataDir, and NULL if the file does not exist
741 * !!IMPORTANT: this has to be deleted from the outside!!
742*/
743std::string ResourceManager::getFullName(const std::string& fileName)
744{
745  if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty())
746    return "";
747
748  std::string retName = ResourceManager::getInstance()->getDataDir() +fileName;
749  if (File(retName).isFile() || File(retName).isDirectory())
750    return retName;
751  else
752    return "";
753}
754
755
756/**
757 * @brief checks wether a file is in the DataDir.
758 * @param fileName the File to check if it is in the Data-Dir structure.
759 * @returns true if the file exists, false otherwise
760 */
761bool ResourceManager::isInDataDir(const std::string& fileName)
762{
763  if (fileName.empty() || ResourceManager::getInstance()->getDataDir().empty())
764    return false;
765
766  bool retVal = false;
767  std::string checkFile = ResourceManager::getInstance()->getDataDir() + fileName;
768
769  if (File(checkFile).exists())
770    retVal = true;
771  else
772    retVal = false;
773  return retVal;
774}
775
776
777/**
778 * @brief outputs debug information about the ResourceManager
779 */
780void ResourceManager::debug() const
781{
782  PRINT(0)("=RM===================================\n");
783  PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
784  PRINT(0)("======================================\n");
785  // if it is not initialized
786  PRINT(0)(" Reference is: %p\n", ResourceManager::singletonRef);
787  PRINT(0)(" Data-Directory is: %s\n", this->dataDir.c_str());
788  PRINT(0)(" List of Image-Directories: ");
789  std::vector<std::string>::const_iterator imageDir;
790  for (imageDir = this->imageDirs.begin(); imageDir != this->imageDirs.end(); imageDir++)
791    PRINT(0)("%s ", (*imageDir).c_str());
792  PRINT(0)("\n");
793
794  PRINT(0)("List of all stored Resources:\n");
795  std::vector<Resource*>::const_iterator resource;
796  for (resource = this->resourceList.begin(); resource != this->resourceList.end(); resource++)
797
798  {
799    PRINT(0)("-----------------------------------------\n");
800    PRINT(0)("Name: %s; References: %d; Type: %s ", (*resource)->name.c_str(), (*resource)->count, ResourceManager::ResourceTypeToChar((*resource)->type));
801
802    PRINT(0)("gets deleted at ");
803    switch((*resource)->prio)
804    {
805      default:
806      case RP_NO:
807        PRINT(0)("first posibility (0)\n");
808        break;
809      case RP_LEVEL:
810        PRINT(0)("the end of the Level (1)\n");
811        break;
812      case RP_CAMPAIGN:
813        PRINT(0)("the end of the campaign (2)\n");
814        break;
815      case RP_GAME:
816        PRINT(0)("when leaving the game (3)\n");
817        break;
818    }
819  }
820
821
822
823  PRINT(0)("==================================RM==\n");
824}
825
826
827/**
828 * @brief converts a ResourceType into the corresponding String
829 * @param type the ResourceType to translate
830 * @returns the converted String.
831 */
832const char* ResourceManager::ResourceTypeToChar(ResourceType type)
833{
834  return ResourceManager::resourceNames[type];
835}
836
837/**
838 * @brief converts a String into a ResourceType (good for loading)
839 * @param resourceType the name of the Type
840 * @returns the Number of the Type, or 0 (defautl) if not found.
841 */
842ResourceType ResourceManager::stringToResourceType(const std::string& resourceType)
843{
844  for (unsigned int i = 0; i < RESOURCE_TYPE_SIZE; i++)
845    if (resourceType == ResourceManager::resourceNames[i])
846      return (ResourceType)i;
847  return (ResourceType)0;
848}
849
850/**
851 * The Names of the ResourceTypes
852 */
853const char* ResourceManager::resourceNames[] =
854  {
855#ifndef NO_MODEL
856    "ObjectModel",
857    "PrimitiveModel",
858    "MD2-Data",
859#endif
860#ifndef NO_TEXT
861    "Font",
862#endif
863#ifndef NO_AUDIO
864    "Wav",
865    "mp3",
866    "ogg",
867#endif
868#ifndef NO_TEXTURES
869    "Texture",
870#endif
871#ifndef NO_SHADERS
872    "Shader",
873#endif
874
875  };
Note: See TracBrowser for help on using the repository browser.