resource_loader.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/resource.h"
  33. /**
  34. @author Juan Linietsky <reduzio@gmail.com>
  35. */
  36. class ResourceInteractiveLoader : public Reference {
  37. GDCLASS(ResourceInteractiveLoader, Reference);
  38. protected:
  39. static void _bind_methods();
  40. public:
  41. virtual void set_local_path(const String &p_local_path) = 0;
  42. virtual Ref<Resource> get_resource() = 0;
  43. virtual Error poll() = 0;
  44. virtual int get_stage() const = 0;
  45. virtual int get_stage_count() const = 0;
  46. virtual void set_translation_remapped(bool p_remapped) = 0;
  47. virtual Error wait();
  48. ResourceInteractiveLoader() {}
  49. };
  50. class ResourceFormatLoader : public Reference {
  51. GDCLASS(ResourceFormatLoader, Reference)
  52. protected:
  53. static void _bind_methods();
  54. public:
  55. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  56. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  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 void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  64. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  65. virtual bool is_import_valid(const String &p_path) const { return true; }
  66. virtual int get_import_order(const String &p_path) const { return 0; }
  67. virtual ~ResourceFormatLoader() {}
  68. };
  69. typedef void (*ResourceLoadErrorNotify)(void *p_ud, const String &p_text);
  70. typedef void (*DependencyErrorNotify)(void *p_ud, const String &p_loading, const String &p_which, const String &p_type);
  71. typedef Error (*ResourceLoaderImport)(const String &p_path);
  72. typedef void (*ResourceLoadedCallback)(RES p_resource, const String &p_path);
  73. class ResourceLoader {
  74. enum {
  75. MAX_LOADERS = 64
  76. };
  77. static Ref<ResourceFormatLoader> loader[MAX_LOADERS];
  78. static int loader_count;
  79. static bool timestamp_on_load;
  80. static void *err_notify_ud;
  81. static ResourceLoadErrorNotify err_notify;
  82. static void *dep_err_notify_ud;
  83. static DependencyErrorNotify dep_err_notify;
  84. static bool abort_on_missing_resource;
  85. static HashMap<String, Vector<String> > translation_remaps;
  86. static HashMap<String, String> path_remaps;
  87. static String _path_remap(const String &p_path, bool *r_translation_remapped = NULL);
  88. friend class Resource;
  89. static SelfList<Resource>::List remapped_list;
  90. friend class ResourceFormatImporter;
  91. //internal load function
  92. static RES _load(const String &p_path, const String &p_original_path, const String &p_type_hint, bool p_no_cache, Error *r_error);
  93. static ResourceLoadedCallback _loaded_callback;
  94. static Ref<ResourceFormatLoader> _find_custom_resource_format_loader(String path);
  95. public:
  96. static Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL);
  97. static RES load(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL);
  98. static bool exists(const String &p_path, const String &p_type_hint = "");
  99. static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions);
  100. static void add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front = false);
  101. static void remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader);
  102. static String get_resource_type(const String &p_path);
  103. static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  104. static Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  105. static bool is_import_valid(const String &p_path);
  106. static int get_import_order(const String &p_path);
  107. static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; }
  108. static bool get_timestamp_on_load() { return timestamp_on_load; }
  109. static void notify_load_error(const String &p_err) {
  110. if (err_notify) err_notify(err_notify_ud, p_err);
  111. }
  112. static void set_error_notify_func(void *p_ud, ResourceLoadErrorNotify p_err_notify) {
  113. err_notify = p_err_notify;
  114. err_notify_ud = p_ud;
  115. }
  116. static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) {
  117. if (dep_err_notify) dep_err_notify(dep_err_notify_ud, p_path, p_dependency, p_type);
  118. }
  119. static void set_dependency_error_notify_func(void *p_ud, DependencyErrorNotify p_err_notify) {
  120. dep_err_notify = p_err_notify;
  121. dep_err_notify_ud = p_ud;
  122. }
  123. static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource = p_abort; }
  124. static bool get_abort_on_missing_resources() { return abort_on_missing_resource; }
  125. static String path_remap(const String &p_path);
  126. static String import_remap(const String &p_path);
  127. static void load_path_remaps();
  128. static void clear_path_remaps();
  129. static void reload_translation_remaps();
  130. static void load_translation_remaps();
  131. static void clear_translation_remaps();
  132. static void set_load_callback(ResourceLoadedCallback p_callback);
  133. static ResourceLoaderImport import;
  134. static bool add_custom_resource_format_loader(String script_path);
  135. static void remove_custom_resource_format_loader(String script_path);
  136. static void add_custom_loaders();
  137. static void remove_custom_loaders();
  138. };
  139. #endif