Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/util/loading/resource_manager.cc @ 9848

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

new_class_id: dynamic loading works completely

try the shell with:
ResourceManger load Texture some-pic-from-data

File size: 6.1 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: Patrick Boenzli
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD
17
18#include "resource_manager.h"
19#include "debug.h"
20
21#include <algorithm>
22#include <cassert>
23
24
25namespace Resources
26{
27  ObjectListDefinition(ResourceManager);
28  //! Singleton Reference to the ResourceManager
29  ResourceManager* ResourceManager::_singletonRef = NULL;
30
31
32  /**
33   * @brief standard constructor
34  */
35  ResourceManager::ResourceManager ()
36  {
37    this->registerObject(this, ResourceManager::_objectList);
38    this->setName("ResourceManager");
39    this->_mainGlobalPath = Directory("./");
40  }
41
42
43  /**
44   * @brief standard destructor
45  */
46  ResourceManager::~ResourceManager ()
47  {
48    // deleting the Resources-List
49    //this->unloadAllByPriority(RP_GAME);
50
51    //   if (!this->resourceList.empty())
52    //     PRINTF(1)("Not removed all Resources, since there are still %d resources registered\n", this->resourceList.size());
53
54    ResourceManager::_singletonRef = NULL;
55  }
56
57
58
59
60  void ResourceManager::registerType(Resources::Type* type)
61  {
62    this->_resourceTypes.push_back(type);
63    PRINTF(5)("ResourceType '%s' added\n", type->storedClassName().c_str());
64  }
65
66  void ResourceManager::unregisterType(Resources::Type* type)
67  {
68    std::vector<Resources::Type*>::iterator it = std::find (this->_resourceTypes.begin(), this->_resourceTypes.end(), type);
69    if (it != this->_resourceTypes.end())
70    {
71      this->_resourceTypes.erase(it);
72      PRINTF(5)("ResourceType '%s' removed\n", type->storedClassName().c_str());
73    }
74  }
75
76
77  void ResourceManager::setMainGlobalPath(const Directory& directory)
78  {
79    this->_mainGlobalPath = directory;
80    this->_mainGlobalPath.open();
81  }
82
83  void ResourceManager::addGlobalPath(const Directory& directory)
84  {
85    std::vector<Directory>::const_iterator it = std::find(this->_globalPaths.begin(), this->_globalPaths.end(), directory);
86    if (it == this->_globalPaths.end())
87      this->_globalPaths.push_back(directory);
88  }
89
90
91  bool ResourceManager::addResourcePath(const std::string& resourceName, const std::string& pathName)
92  {
93    std::vector<Resources::Type*>::iterator it;
94    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
95      if (*(*it) == resourceName)
96        return (*it)->addResourcePath(pathName);
97    PRINTF(2)("ResourcePath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
98    return false;
99  }
100
101  bool ResourceManager::addResourceSubPath(const std::string& resourceName, const std::string& pathName)
102  {
103    std::vector<Resources::Type*>::iterator it;
104    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
105      if (*(*it) == resourceName)
106        return (*it)->addResourceSubPath(pathName);
107    PRINTF(2)("ResourceSubPath %s could not be added to the ResourceType %s\n", pathName.c_str(), resourceName.c_str());
108    return false;
109  }
110
111
112
113  bool ResourceManager::checkFileInMainPath(const File& fileInside)
114  {
115    return (this->_mainGlobalPath + fileInside).exists();
116  }
117
118  std::string ResourceManager::prependAbsoluteMainPath(const std::string& fileName)
119  {
120    return (this->_mainGlobalPath + File(fileName)).name();
121  }
122
123
124  unsigned int ResourceManager::addKeepLevelName(const std::string& keepLevelName)
125  {
126    this->_keepLevelNames.push_back(keepLevelName);
127    return _keepLevelNames.size()-1;
128  }
129
130  unsigned int ResourceManager::getKeepLevelID(const std::string& keepLevelName) const
131  {
132    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
133      if (this->_keepLevelNames[i] == keepLevelName)
134        return i;
135
136    PRINTF(2)("KeepLevel '%s' not found. Using 0 instead\n", keepLevelName.c_str());
137    return 0;
138  }
139
140  const std::string& ResourceManager::getKeepLevelName(unsigned int keepLevelID) const
141  {
142    assert(keepLevelID < this->_keepLevelNames.size());
143    return this->_keepLevelNames[keepLevelID];
144  }
145
146  void ResourceManager::loadFromLoadString(const std::string& resourceTypeName, const std::string& loadString)
147  {
148    std::vector<Resources::Type*>::const_iterator it;
149    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
150    {
151      if (*(*it) == resourceTypeName)
152      {
153        (*it)->createFromString(loadString);
154        /// TODO check if the resource was allocated!!
155        return ;
156      }
157    }
158    return ;
159  }
160
161
162  void ResourceManager::unloadAllBelowKeepLevel(const Resources::KeepLevel& keepLevel)
163  {
164    std::vector<Resources::Type*>::const_iterator it;
165    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
166    {
167      (*it)->unloadAllBelowKeepLevel(keepLevel);
168    }
169  }
170
171
172  /**
173   * @brief outputs debug information about the ResourceManager
174   */
175  void ResourceManager::debug() const
176  {
177    PRINT(0)("=RM===================================\n");
178    PRINT(0)("= RESOURCE-MANAGER DEBUG INFORMATION =\n");
179    PRINT(0)("======================================\n");
180    PRINT(0)(" MainGlobal search path is %s\n", this->_mainGlobalPath.name().c_str());
181    if(!this->_globalPaths.empty())
182    {
183      PRINT(0)(" Additional Global search Paths are: ");
184      for (unsigned int i = 0; i < this->_globalPaths.size(); ++i)
185        PRINT(0)("'%s' ", this->_globalPaths[i].name().c_str());
186      PRINT(0)("\n");
187    }
188    PRINT(0)(" Listing %d Types: \n", this->_resourceTypes.size());
189    std::vector<Resources::Type*>::const_iterator it;
190    for (it = this->_resourceTypes.begin(); it != this->_resourceTypes.end(); ++it)
191    {
192      (*it)->debug();
193      if (it != --this->_resourceTypes.end())
194        PRINT(0)(" ------------------------------------\n ");
195    }
196
197    PRINT(0)("KeepLevels are: ");
198    for (unsigned int i = 0; i < this->_keepLevelNames.size(); ++i)
199      PRINT(0)("%d:'%s'  ", i, this->_keepLevelNames[i].c_str());
200    PRINT(0)("\n");
201    PRINT(0)("==================================RM==\n");
202  }
203}
Note: See TracBrowser for help on using the repository browser.