2
0

resource_loader.h 8.9 KB

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