| 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: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD | 
|---|
| 17 |  | 
|---|
| 18 | #include "resource.h" | 
|---|
| 19 | #include "resource_manager.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "debug.h" | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 | namespace Resources | 
|---|
| 25 | { | 
|---|
| 26 |   ObjectListDefinition(NewResource); | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 |   /** | 
|---|
| 30 |    * standard constructor | 
|---|
| 31 |   */ | 
|---|
| 32 |   NewResource::NewResource (Type* type) | 
|---|
| 33 |       : _pointer(NULL), _type(type) | 
|---|
| 34 |   { | 
|---|
| 35 |     this->registerObject(this, NewResource::_objectList); | 
|---|
| 36 |   } | 
|---|
| 37 |  | 
|---|
| 38 |   /** | 
|---|
| 39 |    * standard deconstructor | 
|---|
| 40 |    */ | 
|---|
| 41 |   NewResource::~NewResource () | 
|---|
| 42 |   { | 
|---|
| 43 |     // delete what has to be deleted here | 
|---|
| 44 |   } | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 |   std::string NewResource::locateFile(const std::string& fileName) const | 
|---|
| 48 |   { | 
|---|
| 49 |     if ((ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).exists() ) | 
|---|
| 50 |       return (ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name(); | 
|---|
| 51 |  | 
|---|
| 52 |     std::string locatedFile; | 
|---|
| 53 |     locatedFile = locateFileInSubDir(ResourceManager::getInstance()->mainGlobalPath(), fileName); | 
|---|
| 54 |     if (!locatedFile.empty()) | 
|---|
| 55 |     { | 
|---|
| 56 |       return locatedFile; | 
|---|
| 57 |     } | 
|---|
| 58 |  | 
|---|
| 59 |     if (File(fileName).exists()) | 
|---|
| 60 |       return fileName; | 
|---|
| 61 |  | 
|---|
| 62 |     return (ResourceManager::getInstance()->mainGlobalPath() + File(fileName)).name(); | 
|---|
| 63 |   } | 
|---|
| 64 |  | 
|---|
| 65 |   /** | 
|---|
| 66 |    * @brief tests in all the SubDirectories defined in Resource under Directory if the fileName exists. | 
|---|
| 67 |    * @param directory the directory to in what to search for all subdirectories for. | 
|---|
| 68 |    * @param fileName the Name of the File to query for | 
|---|
| 69 |    * @return true on success. | 
|---|
| 70 |    */ | 
|---|
| 71 |   std::string NewResource::locateFileInSubDir(const Directory& directory, const std::string& fileName) const | 
|---|
| 72 |   { | 
|---|
| 73 |     std::vector<Directory>::const_iterator it; | 
|---|
| 74 |     for (it = this->_type->resourceSubPaths().begin(); it != this->_type->resourceSubPaths().end(); ++it) | 
|---|
| 75 |     { | 
|---|
| 76 |       Directory dir = directory + (*it); | 
|---|
| 77 |       File file = dir + File(fileName); | 
|---|
| 78 |       if ((dir+ File(fileName)).exists()) | 
|---|
| 79 |         return (dir+File(fileName)).name(); | 
|---|
| 80 |     } | 
|---|
| 81 |     return ""; | 
|---|
| 82 |   } | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 |  | 
|---|
| 86 |   StorePointer* NewResource::acquireResource(const std::string& loadString) | 
|---|
| 87 |   { | 
|---|
| 88 |     //const Type* const type = _resourceTypes[this->_type->id()]; | 
|---|
| 89 |  | 
|---|
| 90 |     for (unsigned int i = 0; i < _type->storedResources().size(); ++i) | 
|---|
| 91 |     { | 
|---|
| 92 |       if (_type->storedResources()[i]->loadString() == loadString) | 
|---|
| 93 |         return _type->storedResources()[i]; | 
|---|
| 94 |     } | 
|---|
| 95 |  | 
|---|
| 96 |     return NULL; | 
|---|
| 97 |   } | 
|---|
| 98 |  | 
|---|
| 99 |  | 
|---|
| 100 |   void NewResource::addResource(StorePointer* pointer) | 
|---|
| 101 |   { | 
|---|
| 102 |     this->_type->addResource(pointer); | 
|---|
| 103 |   } | 
|---|
| 104 |  | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 |   /////////////////// | 
|---|
| 111 |   //// KEEPLEVEL //// | 
|---|
| 112 |   /////////////////// | 
|---|
| 113 |   KeepLevel::KeepLevel(const std::string& keepLevelName) | 
|---|
| 114 |   { | 
|---|
| 115 |     this->_keepLevel = ResourceManager::getInstance()->getKeepLevelID(keepLevelName); | 
|---|
| 116 |   } | 
|---|
| 117 |  | 
|---|
| 118 |   const std::string& KeepLevel::name() const | 
|---|
| 119 |   { | 
|---|
| 120 |     return ResourceManager::getInstance()->getKeepLevelName(this->_keepLevel); | 
|---|
| 121 |   } | 
|---|
| 122 |  | 
|---|
| 123 |  | 
|---|
| 124 |  | 
|---|
| 125 |   /////////////////////// | 
|---|
| 126 |   //// STORE POINTER //// | 
|---|
| 127 |   /////////////////////// | 
|---|
| 128 |   StorePointer::StorePointer(const std::string& loadString, const KeepLevel& keeplevel) | 
|---|
| 129 |       : _loadString(loadString), _keepLevel(keeplevel) | 
|---|
| 130 |   {} | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 |  | 
|---|
| 135 |   ////////////// | 
|---|
| 136 |   //// TYPE //// | 
|---|
| 137 |   ////////////// | 
|---|
| 138 |   Type::Type(const std::string& typeName) | 
|---|
| 139 |       : _id(-1), _typeName(typeName) | 
|---|
| 140 |   { | 
|---|
| 141 |     ResourceManager::getInstance()->registerType(this); | 
|---|
| 142 |     PRINTF(4)("Created ResourceType '%s'\n", typeName.c_str()); | 
|---|
| 143 |   } | 
|---|
| 144 |  | 
|---|
| 145 |   Type::~Type() | 
|---|
| 146 |   { | 
|---|
| 147 |     ResourceManager::getInstance()->unregisterType(this); | 
|---|
| 148 |   } | 
|---|
| 149 |  | 
|---|
| 150 |   void Type::addResource(StorePointer* resource) | 
|---|
| 151 |   { | 
|---|
| 152 |     this->_storedResources.push_back(resource); | 
|---|
| 153 |  | 
|---|
| 154 |   } | 
|---|
| 155 |  | 
|---|
| 156 |   bool Type::addResourcePath(const std::string& path) | 
|---|
| 157 |   { | 
|---|
| 158 |     std::vector<Directory>::const_iterator it; | 
|---|
| 159 |     for (it = this->_resourcePaths.begin(); it != this->_resourcePaths.end(); ++it) | 
|---|
| 160 |       if ((*it) == path) | 
|---|
| 161 |         return false; | 
|---|
| 162 |     this->_resourcePaths.push_back(path); | 
|---|
| 163 |     return true; | 
|---|
| 164 |  | 
|---|
| 165 |   } | 
|---|
| 166 |  | 
|---|
| 167 |   bool Type::addResourceSubPath(const std::string& subPath) | 
|---|
| 168 |   { | 
|---|
| 169 |     std::vector<Directory>::const_iterator it; | 
|---|
| 170 |     for (it = this->_resourceSubPaths.begin(); it != this->_resourceSubPaths.end(); ++it) | 
|---|
| 171 |       if ((*it) == subPath) | 
|---|
| 172 |         return false; | 
|---|
| 173 |     this->_resourceSubPaths.push_back(subPath); | 
|---|
| 174 |     return true; | 
|---|
| 175 |   } | 
|---|
| 176 |  | 
|---|
| 177 |  | 
|---|
| 178 |   void Type::setID(int id) | 
|---|
| 179 |   { | 
|---|
| 180 |     this->_id = id; | 
|---|
| 181 |   } | 
|---|
| 182 |  | 
|---|
| 183 |  | 
|---|
| 184 |   void Type::debug() const | 
|---|
| 185 |   { | 
|---|
| 186 |     PRINT(0)(" ResourceType '%s' with ID %d stores %d Resources\n", this->_typeName.c_str(), this->_id, this->_storedResources.size()); | 
|---|
| 187 |     PRINT(0)("  Paths:\n"); | 
|---|
| 188 |     for (unsigned int i = 0; i < this->_resourcePaths.size(); ++i) | 
|---|
| 189 |       PRINT(0)("    %s\n", this->_resourcePaths[i].name().c_str()); | 
|---|
| 190 |     PRINT(0)("  Sub-Paths:"); | 
|---|
| 191 |     for (unsigned int i = 0; i < this->_resourceSubPaths.size(); ++i) | 
|---|
| 192 |       PRINT(0)(" '%s'", this->_resourceSubPaths[i].name().c_str()); | 
|---|
| 193 |     PRINT(0)("\n"); | 
|---|
| 194 |  | 
|---|
| 195 |     PRINT(0)("  Loaded Resources:\n"); | 
|---|
| 196 |     std::vector<Resources::StorePointer*>::const_iterator it; | 
|---|
| 197 |     for (it = this->_storedResources.begin(); it != this->_storedResources.end(); ++it) | 
|---|
| 198 |       PRINT(0)("    '%s' : KeepLevel '%s'\n", (*it)->loadString().c_str(), (*it)->keepLevel().name().c_str()); | 
|---|
| 199 |   } | 
|---|
| 200 | } | 
|---|