resource_loader.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/os/thread.h"
  33. #include "core/resource.h"
  34. class ResourceInteractiveLoader : public Reference {
  35. GDCLASS(ResourceInteractiveLoader, Reference);
  36. friend class ResourceLoader;
  37. String path_loading;
  38. Thread::ID path_loading_thread;
  39. protected:
  40. bool no_subresource_cache = false;
  41. static void _bind_methods();
  42. public:
  43. virtual void set_local_path(const String &p_local_path) = 0;
  44. virtual Ref<Resource> get_resource() = 0;
  45. virtual Error poll() = 0;
  46. virtual int get_stage() const = 0;
  47. virtual int get_stage_count() const = 0;
  48. virtual void set_translation_remapped(bool p_remapped) = 0;
  49. virtual Error wait();
  50. virtual void set_no_subresource_cache(bool p_no_subresource_cache);
  51. virtual bool get_no_subresource_cache();
  52. ResourceInteractiveLoader() {}
  53. ~ResourceInteractiveLoader();
  54. };
  55. class ResourceFormatLoader : public Reference {
  56. GDCLASS(ResourceFormatLoader, Reference);
  57. protected:
  58. static void _bind_methods();
  59. public:
  60. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_no_subresource_cache = false);
  61. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_no_subresource_cache = false);
  62. virtual bool exists(const String &p_path) const;
  63. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  64. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  65. virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
  66. virtual bool handles_type(const String &p_type) const;
  67. virtual String get_resource_type(const String &p_path) const;
  68. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  69. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  70. virtual bool is_import_valid(const String &p_path) const { return true; }
  71. virtual bool is_imported(const String &p_path) const { return false; }
  72. virtual int get_import_order(const String &p_path) const { return 0; }
  73. virtual String get_import_group_file(const String &p_path) const { return ""; } //no group
  74. virtual ~ResourceFormatLoader() {}
  75. };
  76. typedef void (*ResourceLoadErrorNotify)(void *p_ud, const String &p_text);
  77. typedef void (*DependencyErrorNotify)(void *p_ud, const String &p_loading, const String &p_which, const String &p_type);
  78. typedef Error (*ResourceLoaderImport)(const String &p_path);
  79. typedef void (*ResourceLoadedCallback)(RES p_resource, const String &p_path);
  80. class ResourceLoader {
  81. enum {
  82. MAX_LOADERS = 64
  83. };
  84. static Ref<ResourceFormatLoader> loader[MAX_LOADERS];
  85. static int loader_count;
  86. static bool timestamp_on_load;
  87. static void *err_notify_ud;
  88. static ResourceLoadErrorNotify err_notify;
  89. static void *dep_err_notify_ud;
  90. static DependencyErrorNotify dep_err_notify;
  91. static bool abort_on_missing_resource;
  92. static HashMap<String, Vector<String>> translation_remaps;
  93. static HashMap<String, String> path_remaps;
  94. static String _path_remap(const String &p_path, bool *r_translation_remapped = nullptr);
  95. friend class Resource;
  96. static SelfList<Resource>::List remapped_list;
  97. friend class ResourceFormatImporter;
  98. friend class ResourceInteractiveLoader;
  99. //internal load function
  100. static RES _load(const String &p_path, const String &p_original_path, const String &p_type_hint, bool p_no_cache, Error *r_error);
  101. static ResourceLoadedCallback _loaded_callback;
  102. static Ref<ResourceFormatLoader> _find_custom_resource_format_loader(String path);
  103. static Mutex loading_map_mutex;
  104. //used to track paths being loaded in a thread, avoids cyclic recursion
  105. struct LoadingMapKey {
  106. String path;
  107. Thread::ID thread;
  108. bool operator==(const LoadingMapKey &p_key) const {
  109. return (thread == p_key.thread && path == p_key.path);
  110. }
  111. };
  112. struct LoadingMapKeyHasher {
  113. static _FORCE_INLINE_ uint32_t hash(const LoadingMapKey &p_key) { return p_key.path.hash() + HashMapHasherDefault::hash(p_key.thread); }
  114. };
  115. static HashMap<LoadingMapKey, int, LoadingMapKeyHasher> loading_map;
  116. static bool _add_to_loading_map(const String &p_path);
  117. static void _remove_from_loading_map(const String &p_path);
  118. static void _remove_from_loading_map_and_thread(const String &p_path, Thread::ID p_thread);
  119. public:
  120. static Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = nullptr);
  121. static RES load(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = nullptr);
  122. static bool exists(const String &p_path, const String &p_type_hint = "");
  123. static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions);
  124. static void add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front = false);
  125. static void remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader);
  126. static String get_resource_type(const String &p_path);
  127. static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  128. static Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  129. static bool is_import_valid(const String &p_path);
  130. static String get_import_group_file(const String &p_path);
  131. static bool is_imported(const String &p_path);
  132. static int get_import_order(const String &p_path);
  133. static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; }
  134. static bool get_timestamp_on_load() { return timestamp_on_load; }
  135. static void notify_load_error(const String &p_err) {
  136. if (err_notify) {
  137. err_notify(err_notify_ud, p_err);
  138. }
  139. }
  140. static void set_error_notify_func(void *p_ud, ResourceLoadErrorNotify p_err_notify) {
  141. err_notify = p_err_notify;
  142. err_notify_ud = p_ud;
  143. }
  144. static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) {
  145. if (dep_err_notify) {
  146. dep_err_notify(dep_err_notify_ud, p_path, p_dependency, p_type);
  147. }
  148. }
  149. static void set_dependency_error_notify_func(void *p_ud, DependencyErrorNotify p_err_notify) {
  150. dep_err_notify = p_err_notify;
  151. dep_err_notify_ud = p_ud;
  152. }
  153. static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource = p_abort; }
  154. static bool get_abort_on_missing_resources() { return abort_on_missing_resource; }
  155. static String path_remap(const String &p_path);
  156. static String import_remap(const String &p_path);
  157. static void load_path_remaps();
  158. static void clear_path_remaps();
  159. static void reload_translation_remaps();
  160. static void load_translation_remaps();
  161. static void clear_translation_remaps();
  162. static void set_load_callback(ResourceLoadedCallback p_callback);
  163. static ResourceLoaderImport import;
  164. static bool add_custom_resource_format_loader(String script_path);
  165. static void remove_custom_resource_format_loader(String script_path);
  166. static void add_custom_loaders();
  167. static void remove_custom_loaders();
  168. static void finalize();
  169. };
  170. #endif // RESOURCE_LOADER_H