PolyResourceManager.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #pragma once
  20. #include "polycode/core/PolyGlobals.h"
  21. #include "polycode/core/PolyEventDispatcher.h"
  22. #include <vector>
  23. #define RESOURCE_CHECK_INTERVAL 2000
  24. namespace Polycode {
  25. class Resource;
  26. class PolycodeShaderModule;
  27. class String;
  28. class ResourceLoader;
  29. class _PolyExport ResourcePool : public EventDispatcher {
  30. public:
  31. ResourcePool(const String &name, ResourcePool *fallbackPool);
  32. ~ResourcePool();
  33. void setFallbackPool(ResourcePool *pool);
  34. void addResource(Resource *resource);
  35. void removeResource(Resource *resource);
  36. bool hasResource(Resource *resource);
  37. void loadResourcesFromFolder(const String &folder, bool recursive);
  38. Resource *getResource(int resourceType, const String& resourceName) const;
  39. String getName();
  40. void setName(const String &name);
  41. Resource *getResourceByPath(const String& resourcePath) const;
  42. void Update(int elapsed);
  43. std::vector<Resource *> getResources(int resourceType);
  44. void checkForChangedFiles();
  45. bool reloadResourcesOnModify;
  46. bool dispatchChangeEvents;
  47. int resourceSubscribers;
  48. bool deleteOnUnsubscribe;
  49. static bool defaultReloadResourcesOnModify;
  50. private:
  51. void loadResourcesFromFolderWithLoader(const String &folder, bool recursive, ResourceLoader *loader, const String &containingFolder);
  52. ResourcePool *fallbackPool;
  53. String name;
  54. int ticksSinceCheck;
  55. std::vector <Resource*> resources;
  56. };
  57. class _PolyExport ResourceLoader {
  58. public:
  59. virtual ~ResourceLoader() {}
  60. bool canHandleExtension(const String &extension);
  61. virtual Resource *loadResource(const String &path, ResourcePool *targetPool) = 0;
  62. std::vector<String> extensions;
  63. };
  64. class _PolyExport TextureResourceLoader : public ResourceLoader {
  65. public:
  66. TextureResourceLoader();
  67. Resource *loadResource(const String &path, ResourcePool *targetPool);
  68. };
  69. class _PolyExport ProgramResourceLoader : public ResourceLoader {
  70. public:
  71. ProgramResourceLoader();
  72. Resource *loadResource(const String &path, ResourcePool *targetPool);
  73. };
  74. class _PolyExport MaterialResourceLoader : public ResourceLoader {
  75. public:
  76. MaterialResourceLoader();
  77. Resource *loadResource(const String &path, ResourcePool *targetPool);
  78. };
  79. class _PolyExport FontResourceLoader : public ResourceLoader {
  80. public:
  81. FontResourceLoader();
  82. Resource *loadResource(const String &path, ResourcePool *targetPool);
  83. };
  84. /**
  85. * Manages loading and unloading of resources from directories and archives. Should only be accessed via the CoreServices singleton.
  86. */
  87. class _PolyExport ResourceManager : public EventDispatcher {
  88. public:
  89. ResourceManager();
  90. ~ResourceManager();
  91. ResourcePool *getGlobalPool();
  92. ResourcePool *getResourcePoolByName(const String &name);
  93. void addResourceLoader(ResourceLoader *loader);
  94. ResourceLoader *getResourceLoaderForExtension(const String &extension);
  95. void removeResourceLoader(ResourceLoader *loader);
  96. unsigned int getNumResourceLoaders();
  97. ResourceLoader *getResourceLoaderAtIndex(unsigned int index);
  98. void addResourcePool(ResourcePool *pool);
  99. void removeResourcePool(ResourcePool *pool);
  100. std::vector<Resource*> getResources(int resourceType);
  101. void removeResource(Resource *resource);
  102. void subscribeToResourcePool(ResourcePool *pool);
  103. void unsubscibeFromResourcePool(ResourcePool *pool);
  104. void Update(int elapsed);
  105. void handleEvent(Event *event);
  106. private:
  107. std::vector<ResourceLoader*> resourceLoaders;
  108. ResourcePool *globalPool;
  109. std::vector <ResourcePool*> pools;
  110. };
  111. }