resource_loader.h 11 KB

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