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