PolyResourceManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. ResourcePool::ResourcePool(const String &name, ResourcePool *fallbackPool) {
  35. this->name = name;
  36. this->fallbackPool = fallbackPool;
  37. dispatchChangeEvents = false;
  38. reloadResourcesOnModify = false;
  39. ticksSinceCheck = 0;
  40. resourceSubscribers = 0;
  41. deleteOnUnsubscribe = false;
  42. }
  43. ResourcePool::~ResourcePool() {
  44. CoreServices::getInstance()->getResourceManager()->removeResourcePool(this);
  45. for(int i=0; i < resources.size(); i++) {
  46. if(resources[i]->getResourceType() == Resource::RESOURCE_MATERIAL) {
  47. delete resources[i];
  48. }
  49. }
  50. for(int i=0; i < resources.size(); i++) {
  51. if(resources[i]->getResourceType() == Resource::RESOURCE_SHADER) {
  52. delete resources[i];
  53. }
  54. }
  55. for(int i=0; i < resources.size(); i++) {
  56. if(resources[i]->getResourceType() == Resource::RESOURCE_PROGRAM) {
  57. delete resources[i];
  58. }
  59. }
  60. resources.clear();
  61. }
  62. String ResourcePool::getName() {
  63. return name;
  64. }
  65. void ResourcePool::setName(const String &name) {
  66. this->name = name;
  67. }
  68. void ResourcePool::removeResource(Resource *resource) {
  69. for(int i=0;i<resources.size();i++) {
  70. if(resources[i] == resource) {
  71. resources.erase(resources.begin()+i);
  72. return;
  73. }
  74. }
  75. }
  76. bool ResourcePool::hasResource(Resource *resource) {
  77. for(int i=0; i < resources.size(); i++) {
  78. if(resources[i] == resource) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. void ResourcePool::addResource(Resource *resource) {
  85. resource->addEventListener(this, Event::RESOURCE_CHANGE_EVENT);
  86. resources.push_back(resource);
  87. resource->resourceFileTime = OSBasics::getFileTime(resource->getResourcePath());
  88. if(dispatchChangeEvents) {
  89. dispatchEvent(new Event(), Event::CHANGE_EVENT);
  90. }
  91. }
  92. void ResourcePool::setFallbackPool(ResourcePool *pool) {
  93. fallbackPool = pool;
  94. }
  95. void ResourceManager::addDirResource(const String& dirPath, bool recursive) {
  96. parseTexturesIntoPool(globalPool, dirPath, recursive, "");
  97. parseProgramsIntoPool(globalPool, dirPath, recursive);
  98. parseShadersIntoPool(globalPool, dirPath, recursive);
  99. parseCubemapsIntoPool(globalPool, dirPath, recursive);
  100. parseMaterialsIntoPool(globalPool, dirPath, recursive);
  101. parseOtherIntoPool(globalPool, dirPath, recursive);
  102. }
  103. Resource *ResourcePool::getResourceByPath(const String& resourcePath) const {
  104. for(int i =0; i < resources.size(); i++) {
  105. if(resources[i]->getResourcePath() == resourcePath) {
  106. return resources[i];
  107. }
  108. }
  109. if(fallbackPool) {
  110. return fallbackPool->getResourceByPath(resourcePath);
  111. } else {
  112. Logger::log("Could not find resource for path [%s] in pool [%s]\n", resourcePath.c_str(), name.c_str());
  113. return NULL;
  114. }
  115. }
  116. std::vector<Resource*> ResourcePool::getResources(int resourceType) {
  117. std::vector<Resource*> result;
  118. for(int i =0; i < resources.size(); i++) {
  119. if(resources[i]->getResourceType() == resourceType) {
  120. result.push_back(resources[i]);
  121. }
  122. }
  123. return result;
  124. }
  125. Resource *ResourcePool::getResource(int resourceType, const String& resourceName) const {
  126. for(int i =0; i < resources.size(); i++) {
  127. if(resources[i]->getResourceName() == resourceName && resources[i]->getResourceType() == resourceType) {
  128. return resources[i];
  129. }
  130. }
  131. if(resourceType == Resource::RESOURCE_TEXTURE && resourceName != "default/default.png") {
  132. Logger::log("Texture [%s] not found in pool [%s], using default\n", resourceName.c_str(), name.c_str());
  133. return getResource(Resource::RESOURCE_TEXTURE, "default/default.png");
  134. }
  135. if(fallbackPool) {
  136. return fallbackPool->getResource(resourceType, resourceName);
  137. } else {
  138. Logger::log("Could not find resource [%s] in pool [%s]\n", resourceName.c_str(), name.c_str());
  139. return NULL;
  140. }
  141. }
  142. void ResourcePool::checkForChangedFiles() {
  143. for(int i=0; i < resources.size(); i++) {
  144. if(resources[i]->reloadOnFileModify == true) {
  145. time_t newFileTime = OSBasics::getFileTime(resources[i]->getResourcePath());
  146. // printf("%s\n%lld %lld\n", resources[i]->getResourcePath().c_str(), newFileTime, resources[i]->resourceFileTime);
  147. if((newFileTime != resources[i]->resourceFileTime) && newFileTime != 0) {
  148. resources[i]->reloadResource();
  149. resources[i]->resourceFileTime = newFileTime;
  150. }
  151. }
  152. }
  153. }
  154. ResourceManager::ResourceManager() : EventDispatcher() {
  155. PHYSFS_init(NULL);
  156. globalPool = new ResourcePool("Global", NULL);
  157. }
  158. ResourceManager::~ResourceManager() {
  159. printf("Shutting down resource manager...\n");
  160. PHYSFS_deinit();
  161. for(int i=0; i < pools.size(); i++) {
  162. delete pools[i];
  163. }
  164. pools.clear();
  165. }
  166. void ResourcePool::Update(int elapsed) {
  167. if(!reloadResourcesOnModify)
  168. return;
  169. ticksSinceCheck += elapsed;
  170. if(ticksSinceCheck > RESOURCE_CHECK_INTERVAL) {
  171. ticksSinceCheck = 0;
  172. checkForChangedFiles();
  173. }
  174. }
  175. void ResourceManager::parseShadersIntoPool(ResourcePool *pool, const String& dirPath, bool recursive) {
  176. vector<OSFileEntry> resourceDir;
  177. resourceDir = OSBasics::parseFolder(dirPath, false);
  178. for(int i=0; i < resourceDir.size(); i++) {
  179. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  180. if(resourceDir[i].extension == "mat") {
  181. MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
  182. std::vector<Shader*> shaders = materialManager->loadShadersFromFile(pool, resourceDir[i].fullPath);
  183. for(int s=0; s < shaders.size(); s++) {
  184. pool->addResource(shaders[s]);
  185. materialManager->addShader(shaders[s]);
  186. }
  187. }
  188. } else {
  189. if(recursive)
  190. parseShadersIntoPool(pool, dirPath+"/"+resourceDir[i].name, true);
  191. }
  192. }
  193. }
  194. void ResourceManager::parseOtherIntoPool(ResourcePool *pool, const String& dirPath, bool recursive) {
  195. vector<OSFileEntry> resourceDir;
  196. resourceDir = OSBasics::parseFolder(dirPath, false);
  197. for(int i=0; i < resourceDir.size(); i++) {
  198. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  199. if(resourceDir[i].extension == "ttf") {
  200. Logger::log("Registering font: %s\n", resourceDir[i].nameWithoutExtension.c_str());
  201. CoreServices::getInstance()->getFontManager()->registerFont(resourceDir[i].nameWithoutExtension, resourceDir[i].fullPath);
  202. }
  203. } else {
  204. if(recursive)
  205. parseOtherIntoPool(pool, dirPath+"/"+resourceDir[i].name, true);
  206. }
  207. }
  208. }
  209. void ResourceManager::parseProgramsIntoPool(ResourcePool *pool, const String& dirPath, bool recursive) {
  210. vector<OSFileEntry> resourceDir;
  211. resourceDir = OSBasics::parseFolder(dirPath, false);
  212. for(int i=0; i < resourceDir.size(); i++) {
  213. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  214. MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
  215. ShaderProgram *newProgram = materialManager->createProgramFromFile(resourceDir[i].fullPath);
  216. if(newProgram) {
  217. newProgram->setResourceName(resourceDir[i].name);
  218. newProgram->setResourcePath(resourceDir[i].fullPath);
  219. pool->addResource(newProgram);
  220. }
  221. } else {
  222. if(recursive)
  223. parseProgramsIntoPool(pool, dirPath+"/"+resourceDir[i].name, true);
  224. }
  225. }
  226. }
  227. void ResourceManager::parseMaterialsIntoPool(ResourcePool *pool, const String& dirPath, bool recursive) {
  228. vector<OSFileEntry> resourceDir;
  229. resourceDir = OSBasics::parseFolder(dirPath, false);
  230. for(int i=0; i < resourceDir.size(); i++) {
  231. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  232. if(resourceDir[i].extension == "mat") {
  233. MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
  234. std::vector<Material*> materials = materialManager->loadMaterialsFromFile(pool, resourceDir[i].fullPath);
  235. for(int m=0; m < materials.size(); m++) {
  236. materials[m]->setResourceName(materials[m]->getName());
  237. pool->addResource(materials[m]);
  238. materialManager->addMaterial(materials[m]);
  239. }
  240. }
  241. } else {
  242. if(recursive)
  243. parseMaterialsIntoPool(pool, dirPath+"/"+resourceDir[i].name, true);
  244. }
  245. }
  246. }
  247. void ResourceManager::parseCubemapsIntoPool(ResourcePool *pool, const String& dirPath, bool recursive) {
  248. vector<OSFileEntry> resourceDir;
  249. resourceDir = OSBasics::parseFolder(dirPath, false);
  250. for(int i=0; i < resourceDir.size(); i++) {
  251. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  252. if(resourceDir[i].extension == "mat") {
  253. MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
  254. std::vector<Cubemap*> cubemaps = materialManager->loadCubemapsFromFile(resourceDir[i].fullPath);
  255. for(int c=0; c < cubemaps.size(); c++) {
  256. pool->addResource(cubemaps[c]);
  257. }
  258. }
  259. } else {
  260. if(recursive)
  261. parseCubemapsIntoPool(pool, dirPath+"/"+resourceDir[i].name, true);
  262. }
  263. }
  264. }
  265. void ResourceManager::handleEvent(Event *event) {
  266. if(event->getEventCode() == Event::RESOURCE_CHANGE_EVENT) {
  267. dispatchEvent(new Event(), Event::CHANGE_EVENT);
  268. }
  269. }
  270. ResourcePool *ResourceManager::getGlobalPool() {
  271. return globalPool;
  272. }
  273. void ResourceManager::parseTexturesIntoPool(ResourcePool *pool, const String& dirPath, bool recursive, const String& basePath) {
  274. MaterialManager *materialManager = CoreServices::getInstance()->getMaterialManager();
  275. vector<OSFileEntry> resourceDir;
  276. resourceDir = OSBasics::parseFolder(dirPath, false);
  277. for(int i=0; i < resourceDir.size(); i++) {
  278. if(resourceDir[i].type == OSFileEntry::TYPE_FILE) {
  279. if(resourceDir[i].extension == "png") {
  280. Logger::log("Adding texture %s\n", resourceDir[i].nameWithoutExtension.c_str());
  281. Texture *t = materialManager->createTextureFromFile(resourceDir[i].fullPath, materialManager->clampDefault, materialManager->mipmapsDefault);
  282. if(t) {
  283. if(basePath == "") {
  284. t->setResourceName(resourceDir[i].name);
  285. t->setResourcePath(resourceDir[i].fullPath);
  286. } else {
  287. t->setResourceName(basePath+"/"+resourceDir[i].name);
  288. t->setResourcePath(resourceDir[i].fullPath);
  289. }
  290. pool->addResource(t);
  291. }
  292. }
  293. } else {
  294. if(recursive) {
  295. if(basePath == "") {
  296. parseTexturesIntoPool(pool, dirPath+"/"+resourceDir[i].name, true, resourceDir[i].name);
  297. } else {
  298. parseTexturesIntoPool(pool, dirPath+"/"+resourceDir[i].name, true, basePath+"/"+resourceDir[i].name);
  299. }
  300. }
  301. }
  302. }
  303. }
  304. void ResourceManager::addArchive(const String& path) {
  305. if(PHYSFS_addToSearchPath(path.c_str(), 1) == 0) {
  306. Logger::log("Error adding archive to resource manager... %s\n", PHYSFS_getLastError());
  307. } else {
  308. Logger::log("Added archive: %s\n", path.c_str());
  309. }
  310. }
  311. void ResourceManager::removeArchive(const String& path) {
  312. PHYSFS_removeFromSearchPath(path.c_str());
  313. }
  314. void ResourceManager::Update(int elapsed) {
  315. globalPool->Update(elapsed);
  316. for(int i=0; i < pools.size(); i++) {
  317. pools[i]->Update(elapsed);
  318. }
  319. }
  320. void ResourceManager::removeResource(Resource *resource) {
  321. globalPool->removeResource(resource);
  322. for(int i=0; i < pools.size(); i++) {
  323. pools[i]->removeResource(resource);
  324. }
  325. }
  326. void ResourceManager::addResourcePool(ResourcePool *pool) {
  327. pools.push_back(pool);
  328. }
  329. ResourcePool *ResourceManager::getResourcePoolByName(const String &name) {
  330. printf("request resource pool [%s]\n", name.c_str());
  331. for(int i=0; i < pools.size(); i++) {
  332. if(pools[i]->getName() == name) {
  333. return pools[i];
  334. }
  335. }
  336. printf("resource pool not found!\n");
  337. return NULL;
  338. }
  339. void ResourceManager::removeResourcePool(ResourcePool *pool) {
  340. for(int i=0; i < pools.size(); i++) {
  341. if(pools[i] == pool) {
  342. pools.erase(pools.begin()+i);
  343. return;
  344. }
  345. }
  346. }
  347. void ResourceManager::subscribeToResourcePool(ResourcePool *pool) {
  348. pool->resourceSubscribers++;
  349. }
  350. void ResourceManager::unsubscibeFromResourcePool(ResourcePool *pool) {
  351. pool->resourceSubscribers--;
  352. if(pool->deleteOnUnsubscribe && pool->resourceSubscribers < 1) {
  353. delete pool;
  354. }
  355. }
  356. std::vector<Resource*> ResourceManager::getResources(int resourceType) {
  357. std::vector<Resource*> result;
  358. std::vector<Resource*> subresult = globalPool->getResources(resourceType);
  359. result.insert(result.end(), subresult.begin(), subresult.end());
  360. for(int i =0; i < pools.size(); i++) {
  361. subresult = pools[i]->getResources(resourceType);
  362. result.insert(result.end(), subresult.begin(), subresult.end());
  363. }
  364. return result;
  365. }