PolyResourceManager.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyResourceManager.h"
  20. #include "PolyCoreServices.h"
  21. #include "PolyCubemap.h"
  22. #include "PolyMaterialManager.h"
  23. #include "PolyModule.h"
  24. #include "PolyFontManager.h"
  25. #include "PolyLogger.h"
  26. #include "PolyMaterial.h"
  27. #include "PolyShader.h"
  28. #include "PolyTexture.h"
  29. #include "OSBasics.h"
  30. #include "physfs.h"
  31. #include "tinyxml.h"
  32. using std::vector;
  33. using namespace Polycode;
  34. ResourceManager::ResourceManager() {
  35. PHYSFS_init(NULL);
  36. }
  37. ResourceManager::~ResourceManager() {
  38. printf("Shutting down resource manager...\n");
  39. PHYSFS_deinit();
  40. for(int i=0; i < resources.size(); i++) {
  41. delete resources[i];
  42. }
  43. resources.clear();
  44. }
  45. void ResourceManager::parseShaders(const String& dirPath, bool recursive) {
  46. vector<OSFileEntry> resourceDir;
  47. resourceDir = OSBasics::parseFolder(dirPath, false);
  48. for(int i=0; i < resourceDir.size(); i++) {
  49. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  50. if(resourceDir[i].extension == "mat") {
  51. Logger::log("Adding shaders from %s\n", resourceDir[i].nameWithoutExtension.c_str());
  52. TiXmlDocument doc(resourceDir[i].fullPath.c_str());
  53. doc.LoadFile();
  54. if(doc.Error()) {
  55. Logger::log("XML Error: %s\n", doc.ErrorDesc());
  56. } else {
  57. TiXmlElement *mElem = doc.RootElement()->FirstChildElement("shaders");
  58. if(mElem) {
  59. TiXmlNode* pChild;
  60. for (pChild = mElem->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
  61. Shader *newShader = CoreServices::getInstance()->getMaterialManager()->createShaderFromXMLNode(pChild);
  62. if(newShader != NULL) {
  63. Logger::log("Adding shader %s\n", newShader->getName().c_str());
  64. newShader->setResourceName(newShader->getName());
  65. resources.push_back(newShader);
  66. CoreServices::getInstance()->getMaterialManager()->registerShader(newShader);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. } else {
  73. if(recursive)
  74. parseShaders(dirPath+"/"+resourceDir[i].name, true);
  75. }
  76. }
  77. }
  78. void ResourceManager::addShaderModule(PolycodeShaderModule *module) {
  79. shaderModules.push_back(module);
  80. }
  81. void ResourceManager::parsePrograms(const String& dirPath, bool recursive) {
  82. vector<OSFileEntry> resourceDir;
  83. resourceDir = OSBasics::parseFolder(dirPath, false);
  84. for(int i=0; i < resourceDir.size(); i++) {
  85. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  86. for(int m=0; m < shaderModules.size(); m++) {
  87. PolycodeShaderModule *shaderModule = shaderModules[m];
  88. if(shaderModule->acceptsExtension(resourceDir[i].extension)) {
  89. Resource *newProgram = shaderModule->createProgramFromFile(resourceDir[i].extension, resourceDir[i].fullPath);
  90. if(newProgram) {
  91. newProgram->setResourceName(resourceDir[i].name);
  92. newProgram->setResourcePath(resourceDir[i].fullPath);
  93. resources.push_back(newProgram);
  94. }
  95. }
  96. }
  97. } else {
  98. if(recursive)
  99. parsePrograms(dirPath+"/"+resourceDir[i].name, true);
  100. }
  101. }
  102. }
  103. void ResourceManager::parseMaterials(const String& dirPath, bool recursive) {
  104. vector<OSFileEntry> resourceDir;
  105. resourceDir = OSBasics::parseFolder(dirPath, false);
  106. for(int i=0; i < resourceDir.size(); i++) {
  107. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  108. if(resourceDir[i].extension == "mat") {
  109. MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
  110. std::vector<Material*> materials = materialManager->loadMaterialsFromFile(resourceDir[i].fullPath);
  111. for(int m=0; m < materials.size(); m++) {
  112. materials[m]->setResourceName(materials[m]->getName());
  113. resources.push_back(materials[m]);
  114. materialManager->addMaterial(materials[m]);
  115. }
  116. }
  117. } else {
  118. if(recursive)
  119. parseMaterials(dirPath+"/"+resourceDir[i].name, true);
  120. }
  121. }
  122. }
  123. void ResourceManager::parseCubemaps(const String& dirPath, bool recursive) {
  124. vector<OSFileEntry> resourceDir;
  125. resourceDir = OSBasics::parseFolder(dirPath, false);
  126. for(int i=0; i < resourceDir.size(); i++) {
  127. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  128. if(resourceDir[i].extension == "mat") {
  129. Logger::log("Adding cubemaps from %s\n", resourceDir[i].nameWithoutExtension.c_str());
  130. TiXmlDocument doc(resourceDir[i].fullPath.c_str());
  131. doc.LoadFile();
  132. if(doc.Error()) {
  133. Logger::log("XML Error: %s\n", doc.ErrorDesc());
  134. } else {
  135. TiXmlElement *mElem = doc.RootElement()->FirstChildElement("cubemaps");
  136. if(mElem) {
  137. TiXmlNode* pChild;
  138. for (pChild = mElem->FirstChild(); pChild != 0; pChild = pChild->NextSibling()) {
  139. Cubemap *newMat = CoreServices::getInstance()->getMaterialManager()->cubemapFromXMLNode(pChild);
  140. // newMat->setResourceName(newMat->getName());
  141. if(newMat)
  142. resources.push_back(newMat);
  143. }
  144. }
  145. }
  146. }
  147. } else {
  148. if(recursive)
  149. parseCubemaps(dirPath+"/"+resourceDir[i].name, true);
  150. }
  151. }
  152. }
  153. void ResourceManager::addResource(Resource *resource) {
  154. resources.push_back(resource);
  155. }
  156. void ResourceManager::parseTextures(const String& dirPath, bool recursive, const String& basePath) {
  157. vector<OSFileEntry> resourceDir;
  158. resourceDir = OSBasics::parseFolder(dirPath, false);
  159. for(int i=0; i < resourceDir.size(); i++) {
  160. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  161. if(resourceDir[i].extension == "png") {
  162. Logger::log("Adding texture %s\n", resourceDir[i].nameWithoutExtension.c_str());
  163. Texture *t = CoreServices::getInstance()->getMaterialManager()->createTextureFromFile(resourceDir[i].fullPath);
  164. if(t) {
  165. if(basePath == "") {
  166. t->setResourceName(resourceDir[i].name);
  167. } else {
  168. t->setResourceName(basePath+"/"+resourceDir[i].name);
  169. }
  170. resources.push_back(t);
  171. }
  172. }
  173. } else {
  174. if(recursive) {
  175. if(basePath == "") {
  176. parseTextures(dirPath+"/"+resourceDir[i].name, true, resourceDir[i].name);
  177. } else {
  178. parseTextures(dirPath+"/"+resourceDir[i].name, true, basePath+"/"+resourceDir[i].name);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. void ResourceManager::parseOthers(const String& dirPath, bool recursive) {
  185. vector<OSFileEntry> resourceDir;
  186. resourceDir = OSBasics::parseFolder(dirPath, false);
  187. for(int i=0; i < resourceDir.size(); i++) {
  188. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  189. if(resourceDir[i].extension == "ttf") {
  190. Logger::log("Registering font: %s\n", resourceDir[i].nameWithoutExtension.c_str());
  191. CoreServices::getInstance()->getFontManager()->registerFont(resourceDir[i].nameWithoutExtension, resourceDir[i].fullPath);
  192. }
  193. } else {
  194. if(recursive)
  195. parseOthers(dirPath+"/"+resourceDir[i].name, true);
  196. }
  197. }
  198. }
  199. void ResourceManager::addArchive(const String& path) {
  200. if(PHYSFS_addToSearchPath(path.c_str(), 1) == 0) {
  201. Logger::log("Error adding archive to resource manager... %s\n", PHYSFS_getLastError());
  202. } else {
  203. Logger::log("Added archive: %s\n", path.c_str());
  204. }
  205. }
  206. void ResourceManager::removeArchive(const String& path) {
  207. PHYSFS_removeFromSearchPath(path.c_str());
  208. }
  209. void ResourceManager::addDirResource(const String& dirPath, bool recursive) {
  210. parseTextures(dirPath, recursive, "");
  211. parsePrograms(dirPath, recursive);
  212. parseShaders(dirPath, recursive);
  213. parseCubemaps(dirPath, recursive);
  214. parseMaterials(dirPath, recursive);
  215. parseOthers(dirPath, recursive);
  216. }
  217. Resource *ResourceManager::getResource(int resourceType, const String& resourceName) const {
  218. Logger::log("requested %s\n", resourceName.c_str());
  219. for(int i =0; i < resources.size(); i++) {
  220. // Logger::log("is it %s?\n", resources[i]->getResourceName().c_str());
  221. if(resources[i]->getResourceName() == resourceName && resources[i]->getResourceType() == resourceType) {
  222. return resources[i];
  223. }
  224. }
  225. if(resourceType == Resource::RESOURCE_TEXTURE && resourceName != "default/default.png") {
  226. Logger::log("Texture not found, using default\n");
  227. return getResource(Resource::RESOURCE_TEXTURE, "default/default.png");
  228. }
  229. Logger::log("return NULL\n");
  230. // need to add some sort of default resource for each type
  231. return NULL;
  232. }
  233. // Would it make more sense to pass back, like, something like an ObjectEntry here? Lua hates vectors.
  234. vector<Resource *> ResourceManager::getResources(int resourceType) {
  235. vector<Resource *> result;
  236. Logger::log("requested all of type %d\n", resourceType);
  237. for(int i =0; i < resources.size(); i++) {
  238. // Logger::log("is it %s?\n", resources[i]->getResourceName().c_str());
  239. if(resources[i]->getResourceType() == resourceType) {
  240. result.push_back(resources[i]);
  241. }
  242. }
  243. return result;
  244. }