resource_loader.h 8.8 KB

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