resource_loader.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*************************************************************************/
  2. /* resource_loader.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "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 {
  51. public:
  52. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  53. virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
  54. virtual void get_recognized_extensions(List<String> *p_extensions) const = 0;
  55. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  56. virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
  57. virtual bool handles_type(const String &p_type) const = 0;
  58. virtual String get_resource_type(const String &p_path) const = 0;
  59. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  60. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map) { return OK; }
  61. virtual bool is_import_valid(const String &p_path) const { return true; }
  62. virtual int get_import_order(const String &p_path) const { return 0; }
  63. virtual ~ResourceFormatLoader() {}
  64. };
  65. typedef void (*ResourceLoadErrorNotify)(void *p_ud, const String &p_text);
  66. typedef void (*DependencyErrorNotify)(void *p_ud, const String &p_loading, const String &p_which, const String &p_type);
  67. class ResourceLoader {
  68. enum {
  69. MAX_LOADERS = 64
  70. };
  71. static ResourceFormatLoader *loader[MAX_LOADERS];
  72. static int loader_count;
  73. static bool timestamp_on_load;
  74. static void *err_notify_ud;
  75. static ResourceLoadErrorNotify err_notify;
  76. static void *dep_err_notify_ud;
  77. static DependencyErrorNotify dep_err_notify;
  78. static bool abort_on_missing_resource;
  79. static HashMap<String, Vector<String> > translation_remaps;
  80. static HashMap<String, String> path_remaps;
  81. static String _path_remap(const String &p_path, bool *r_translation_remapped = NULL);
  82. friend class Resource;
  83. static SelfList<Resource>::List remapped_list;
  84. friend class ResourceFormatImporter;
  85. //internal load function
  86. static RES _load(const String &p_path, const String &p_original_path, const String &p_type_hint, bool p_no_cache, Error *r_error);
  87. public:
  88. static Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL);
  89. static RES load(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL);
  90. static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions);
  91. static void add_resource_format_loader(ResourceFormatLoader *p_format_loader, bool p_at_front = false);
  92. static String get_resource_type(const String &p_path);
  93. static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  94. static Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  95. static bool is_import_valid(const String &p_path);
  96. static int get_import_order(const String &p_path);
  97. static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; }
  98. static void notify_load_error(const String &p_err) {
  99. if (err_notify) err_notify(err_notify_ud, p_err);
  100. }
  101. static void set_error_notify_func(void *p_ud, ResourceLoadErrorNotify p_err_notify) {
  102. err_notify = p_err_notify;
  103. err_notify_ud = p_ud;
  104. }
  105. static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) {
  106. if (dep_err_notify) dep_err_notify(dep_err_notify_ud, p_path, p_dependency, p_type);
  107. }
  108. static void set_dependency_error_notify_func(void *p_ud, DependencyErrorNotify p_err_notify) {
  109. dep_err_notify = p_err_notify;
  110. dep_err_notify_ud = p_ud;
  111. }
  112. static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource = p_abort; }
  113. static bool get_abort_on_missing_resources() { return abort_on_missing_resource; }
  114. static String path_remap(const String &p_path);
  115. static String import_remap(const String &p_path);
  116. static void load_path_remaps();
  117. static void clear_path_remaps();
  118. static void reload_translation_remaps();
  119. static void load_translation_remaps();
  120. static void clear_translation_remaps();
  121. };
  122. #endif