resource_loader.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*************************************************************************/
  2. /* resource_loader.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/object/gdvirtual.gen.inc"
  34. #include "core/object/script_language.h"
  35. #include "core/os/semaphore.h"
  36. #include "core/os/thread.h"
  37. class ResourceFormatLoader : public RefCounted {
  38. GDCLASS(ResourceFormatLoader, RefCounted);
  39. public:
  40. enum CacheMode {
  41. CACHE_MODE_IGNORE, // Resource and subresources do not use path cache, no path is set into resource.
  42. CACHE_MODE_REUSE, // Resource and subresources use patch cache, reuse existing loaded resources instead of loading from disk when available.
  43. CACHE_MODE_REPLACE, // Resource and subresource use path cache, but replace existing loaded resources when available with information from disk.
  44. };
  45. protected:
  46. static void _bind_methods();
  47. GDVIRTUAL0RC(Vector<String>, _get_recognized_extensions)
  48. GDVIRTUAL1RC(bool, _handles_type, StringName)
  49. GDVIRTUAL1RC(String, _get_resource_type, String)
  50. GDVIRTUAL1RC(ResourceUID::ID, _get_resource_uid, String)
  51. GDVIRTUAL2RC(Vector<String>, _get_dependencies, String, bool)
  52. GDVIRTUAL2RC(int64_t, _rename_dependencies, String, Dictionary)
  53. GDVIRTUAL1RC(bool, _exists, String)
  54. GDVIRTUAL4RC(Variant, _load, String, String, bool, int)
  55. public:
  56. virtual Ref<Resource> 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);
  57. virtual bool exists(const String &p_path) const;
  58. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  59. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  60. virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
  61. virtual bool handles_type(const String &p_type) const;
  62. virtual String get_resource_type(const String &p_path) const;
  63. virtual ResourceUID::ID get_resource_uid(const String &p_path) const;
  64. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  65. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  66. virtual bool is_import_valid(const String &p_path) const { return true; }
  67. virtual bool is_imported(const String &p_path) const { return false; }
  68. virtual int get_import_order(const String &p_path) const { return 0; }
  69. virtual String get_import_group_file(const String &p_path) const { return ""; } //no group
  70. virtual ~ResourceFormatLoader() {}
  71. };
  72. VARIANT_ENUM_CAST(ResourceFormatLoader::CacheMode)
  73. typedef void (*ResourceLoadErrorNotify)(void *p_ud, const String &p_text);
  74. typedef void (*DependencyErrorNotify)(void *p_ud, const String &p_loading, const String &p_which, const String &p_type);
  75. typedef Error (*ResourceLoaderImport)(const String &p_path);
  76. typedef void (*ResourceLoadedCallback)(Ref<Resource> p_resource, const String &p_path);
  77. class ResourceLoader {
  78. enum {
  79. MAX_LOADERS = 64
  80. };
  81. public:
  82. enum ThreadLoadStatus {
  83. THREAD_LOAD_INVALID_RESOURCE,
  84. THREAD_LOAD_IN_PROGRESS,
  85. THREAD_LOAD_FAILED,
  86. THREAD_LOAD_LOADED
  87. };
  88. private:
  89. static Ref<ResourceFormatLoader> loader[MAX_LOADERS];
  90. static int loader_count;
  91. static bool timestamp_on_load;
  92. static void *err_notify_ud;
  93. static ResourceLoadErrorNotify err_notify;
  94. static void *dep_err_notify_ud;
  95. static DependencyErrorNotify dep_err_notify;
  96. static bool abort_on_missing_resource;
  97. static HashMap<String, Vector<String>> translation_remaps;
  98. static HashMap<String, String> path_remaps;
  99. static String _path_remap(const String &p_path, bool *r_translation_remapped = nullptr);
  100. friend class Resource;
  101. static SelfList<Resource>::List remapped_list;
  102. friend class ResourceFormatImporter;
  103. friend class ResourceInteractiveLoader;
  104. // Internal load function.
  105. static Ref<Resource> _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);
  106. static ResourceLoadedCallback _loaded_callback;
  107. static Ref<ResourceFormatLoader> _find_custom_resource_format_loader(String path);
  108. struct ThreadLoadTask {
  109. Thread *thread = nullptr;
  110. Thread::ID loader_id = 0;
  111. Semaphore *semaphore = nullptr;
  112. String local_path;
  113. String remapped_path;
  114. String type_hint;
  115. float progress = 0.0;
  116. ThreadLoadStatus status = THREAD_LOAD_IN_PROGRESS;
  117. ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE;
  118. Error error = OK;
  119. Ref<Resource> resource;
  120. bool xl_remapped = false;
  121. bool use_sub_threads = false;
  122. bool start_next = true;
  123. int requests = 0;
  124. int poll_requests = 0;
  125. Set<String> sub_tasks;
  126. };
  127. static void _thread_load_function(void *p_userdata);
  128. static Mutex *thread_load_mutex;
  129. static HashMap<String, ThreadLoadTask> thread_load_tasks;
  130. static Semaphore *thread_load_semaphore;
  131. static int thread_waiting_count;
  132. static int thread_loading_count;
  133. static int thread_suspended_count;
  134. static int thread_load_max;
  135. static float _dependency_get_progress(const String &p_path);
  136. public:
  137. 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());
  138. static ThreadLoadStatus load_threaded_get_status(const String &p_path, float *r_progress = nullptr);
  139. static Ref<Resource> load_threaded_get(const String &p_path, Error *r_error = nullptr);
  140. static Ref<Resource> load(const String &p_path, const String &p_type_hint = "", ResourceFormatLoader::CacheMode p_cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE, Error *r_error = nullptr);
  141. static bool exists(const String &p_path, const String &p_type_hint = "");
  142. static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions);
  143. static void add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front = false);
  144. static void remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader);
  145. static String get_resource_type(const String &p_path);
  146. static ResourceUID::ID get_resource_uid(const String &p_path);
  147. static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  148. static Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  149. static bool is_import_valid(const String &p_path);
  150. static String get_import_group_file(const String &p_path);
  151. static bool is_imported(const String &p_path);
  152. static int get_import_order(const String &p_path);
  153. static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; }
  154. static bool get_timestamp_on_load() { return timestamp_on_load; }
  155. static void notify_load_error(const String &p_err) {
  156. if (err_notify) {
  157. err_notify(err_notify_ud, p_err);
  158. }
  159. }
  160. static void set_error_notify_func(void *p_ud, ResourceLoadErrorNotify p_err_notify) {
  161. err_notify = p_err_notify;
  162. err_notify_ud = p_ud;
  163. }
  164. static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) {
  165. if (dep_err_notify) {
  166. dep_err_notify(dep_err_notify_ud, p_path, p_dependency, p_type);
  167. }
  168. }
  169. static void set_dependency_error_notify_func(void *p_ud, DependencyErrorNotify p_err_notify) {
  170. dep_err_notify = p_err_notify;
  171. dep_err_notify_ud = p_ud;
  172. }
  173. static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource = p_abort; }
  174. static bool get_abort_on_missing_resources() { return abort_on_missing_resource; }
  175. static String path_remap(const String &p_path);
  176. static String import_remap(const String &p_path);
  177. static void load_path_remaps();
  178. static void clear_path_remaps();
  179. static void reload_translation_remaps();
  180. static void load_translation_remaps();
  181. static void clear_translation_remaps();
  182. static void set_load_callback(ResourceLoadedCallback p_callback);
  183. static ResourceLoaderImport import;
  184. static bool add_custom_resource_format_loader(String script_path);
  185. static void remove_custom_resource_format_loader(String script_path);
  186. static void add_custom_loaders();
  187. static void remove_custom_loaders();
  188. static void initialize();
  189. static void finalize();
  190. };
  191. #endif // RESOURCE_LOADER_H