Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the bsp-model-stuff back here

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