resource_loader.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*************************************************************************/
  2. /* resource_loader.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef RESOURCE_LOADER_H
  31. #define RESOURCE_LOADER_H
  32. #include "core/io/resource.h"
  33. #include "core/os/semaphore.h"
  34. #include "core/os/thread.h"
  35. class ResourceFormatLoader : public Reference {
  36. GDCLASS(ResourceFormatLoader, Reference);
  37. public:
  38. enum CacheMode {
  39. CACHE_MODE_IGNORE, //resource and subresources do not use path cache, no path is set into resource.
  40. CACHE_MODE_REUSE, //resource and subresources use patch cache, reuse existing loaded resources instead of loading from disk when available
  41. CACHE_MODE_REPLACE, //resource and and subresource use path cache, but replace existing loaded resources when available with information from disk
  42. };
  43. protected:
  44. static void _bind_methods();
  45. public:
  46. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
  47. virtual bool exists(const String &p_path) const;
  48. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  49. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  50. virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
  51. virtual bool handles_type(const String &p_type) const;
  52. virtual String get_resource_type(const String &p_path) const;
  53. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  54. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  55. virtual bool is_import_valid(const String &p_path) const { return true; }
  56. virtual bool is_imported(const String &p_path) const { return false; }
  57. virtual int get_import_order(const String &p_path) const { return 0; }
  58. virtual String get_import_group_file(const String &p_path) const { return ""; } //no group
  59. virtual ~ResourceFormatLoader() {}
  60. };
  61. VARIANT_ENUM_CAST(ResourceFormatLoader::CacheMode)
  62. typedef void (*ResourceLoadErrorNotify)(void *p_ud, const String &p_text);
  63. typedef void (*DependencyErrorNotify)(void *p_ud, const String &p_loading, const String &p_which, const String &p_type);
  64. typedef Error (*ResourceLoaderImport)(const String &p_path);
  65. typedef void (*ResourceLoadedCallback)(RES p_resource, const String &p_path);
  66. class ResourceLoader {
  67. enum {
  68. MAX_LOADERS = 64
  69. };
  70. public:
  71. enum ThreadLoadStatus {
  72. THREAD_LOAD_INVALID_RESOURCE,
  73. THREAD_LOAD_IN_PROGRESS,
  74. THREAD_LOAD_FAILED,
  75. THREAD_LOAD_LOADED
  76. };
  77. private:
  78. static Ref<ResourceFormatLoader> loader[MAX_LOADERS];
  79. static int loader_count;
  80. static bool timestamp_on_load;
  81. static void *err_notify_ud;
  82. static ResourceLoadErrorNotify err_notify;
  83. static void *dep_err_notify_ud;
  84. static DependencyErrorNotify dep_err_notify;
  85. static bool abort_on_missing_resource;
  86. static HashMap<String, Vector<String>> translation_remaps;
  87. static HashMap<String, String> path_remaps;
  88. static String _path_remap(const String &p_path, bool *r_translation_remapped = nullptr);
  89. friend class Resource;
  90. static SelfList<Resource>::List remapped_list;
  91. friend class ResourceFormatImporter;
  92. friend class ResourceInteractiveLoader;
  93. //internal load function
  94. static RES _load(const String &p_path, const String &p_original_path, const String &p_type_hint, ResourceFormatLoader::CacheMode p_cache_mode, Error *r_error, bool p_use_sub_threads, float *r_progress);
  95. static ResourceLoadedCallback _loaded_callback;
  96. static Ref<ResourceFormatLoader> _find_custom_resource_format_loader(String path);
  97. struct ThreadLoadTask {
  98. Thread *thread = nullptr;
  99. Thread::ID loader_id = 0;
  100. Semaphore *semaphore = nullptr;
  101. String local_path;
  102. String remapped_path;
  103. String type_hint;
  104. float progress = 0.0;
  105. ThreadLoadStatus status = THREAD_LOAD_IN_PROGRESS;
  106. ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE;
  107. Error error = OK;
  108. RES resource;
  109. bool xl_remapped = false;
  110. bool use_sub_threads = false;
  111. bool start_next = true;
  112. int requests = 0;
  113. int poll_requests = 0;
  114. Set<String> sub_tasks;
  115. };
  116. static void _thread_load_function(void *p_userdata);
  117. static Mutex *thread_load_mutex;
  118. static HashMap<String, ThreadLoadTask> thread_load_tasks;
  119. static Semaphore *thread_load_semaphore;
  120. static int thread_waiting_count;
  121. static int thread_loading_count;
  122. static int thread_suspended_count;
  123. static int thread_load_max;
  124. static float _dependency_get_progress(const String &p_path);
  125. public:
  126. static Error load_threaded_request(const String &p_path, const String &p_type_hint = "", bool p_use_sub_threads = false, ResourceFormatLoader::CacheMode p_cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE, const String &p_source_resource = String());
  127. static ThreadLoadStatus load_threaded_get_status(const String &p_path, float *r_progress = nullptr);
  128. static RES load_threaded_get(const String &p_path, Error *r_error = nullptr);
  129. static RES load(const String &p_path, const String &p_type_hint = "", ResourceFormatLoader::CacheMode p_cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE, Error *r_error = nullptr);
  130. static bool exists(const String &p_path, const String &p_type_hint = "");
  131. static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions);
  132. static void add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front = false);
  133. static void remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader);
  134. static String get_resource_type(const String &p_path);
  135. static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  136. static Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  137. static bool is_import_valid(const String &p_path);
  138. static String get_import_group_file(const String &p_path);
  139. static bool is_imported(const String &p_path);
  140. static int get_import_order(const String &p_path);
  141. static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; }
  142. static bool get_timestamp_on_load() { return timestamp_on_load; }
  143. static void notify_load_error(const String &p_err) {
  144. if (err_notify) {
  145. err_notify(err_notify_ud, p_err);
  146. }
  147. }
  148. static void set_error_notify_func(void *p_ud, ResourceLoadErrorNotify p_err_notify) {
  149. err_notify = p_err_notify;
  150. err_notify_ud = p_ud;
  151. }
  152. static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) {
  153. if (dep_err_notify) {
  154. dep_err_notify(dep_err_notify_ud, p_path, p_dependency, p_type);
  155. }
  156. }
  157. static void set_dependency_error_notify_func(void *p_ud, DependencyErrorNotify p_err_notify) {
  158. dep_err_notify = p_err_notify;
  159. dep_err_notify_ud = p_ud;
  160. }
  161. static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource = p_abort; }
  162. static bool get_abort_on_missing_resources() { return abort_on_missing_resource; }
  163. static String path_remap(const String &p_path);
  164. static String import_remap(const String &p_path);
  165. static void load_path_remaps();
  166. static void clear_path_remaps();
  167. static void reload_translation_remaps();
  168. static void load_translation_remaps();
  169. static void clear_translation_remaps();
  170. static void set_load_callback(ResourceLoadedCallback p_callback);
  171. static ResourceLoaderImport import;
  172. static bool add_custom_resource_format_loader(String script_path);
  173. static void remove_custom_resource_format_loader(String script_path);
  174. static void add_custom_loaders();
  175. static void remove_custom_loaders();
  176. static void initialize();
  177. static void finalize();
  178. };
  179. #endif // RESOURCE_LOADER_H