resource_loader.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************/
  2. /* resource_loader.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "export_data.h"
  33. #include "resource.h"
  34. /**
  35. @author Juan Linietsky <reduzio@gmail.com>
  36. */
  37. class ResourceInteractiveLoader : public Reference {
  38. OBJ_TYPE(ResourceInteractiveLoader, Reference);
  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 Error wait();
  48. ResourceInteractiveLoader() {}
  49. };
  50. class ResourceFormatLoader {
  51. public:
  52. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_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. bool recognize(const String &p_extension) 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 load_import_metadata(const String &p_path, Ref<ResourceImportMetadata> &r_var) const { return ERR_UNAVAILABLE; }
  61. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map) { return OK; }
  62. virtual Error get_export_data(const String &p_path, ExportData &r_export_data) { return ERR_UNAVAILABLE; }
  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 String find_complete_path(const String &p_path, const String &p_type);
  80. public:
  81. static Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL);
  82. static RES load(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL);
  83. static Ref<ResourceImportMetadata> load_import_metadata(const String &p_path);
  84. static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions);
  85. static void add_resource_format_loader(ResourceFormatLoader *p_format_loader, bool p_at_front = false);
  86. static String get_resource_type(const String &p_path);
  87. static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  88. static Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  89. static Error get_export_data(const String &p_path, ExportData &r_export_data);
  90. static String guess_full_filename(const String &p_path, const String &p_type);
  91. static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; }
  92. static void notify_load_error(const String &p_err) {
  93. if (err_notify) err_notify(err_notify_ud, p_err);
  94. }
  95. static void set_error_notify_func(void *p_ud, ResourceLoadErrorNotify p_err_notify) {
  96. err_notify = p_err_notify;
  97. err_notify_ud = p_ud;
  98. }
  99. static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) {
  100. if (dep_err_notify) dep_err_notify(dep_err_notify_ud, p_path, p_dependency, p_type);
  101. }
  102. static void set_dependency_error_notify_func(void *p_ud, DependencyErrorNotify p_err_notify) {
  103. dep_err_notify = p_err_notify;
  104. dep_err_notify_ud = p_ud;
  105. }
  106. static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource = p_abort; }
  107. static bool get_abort_on_missing_resources() { return abort_on_missing_resource; }
  108. };
  109. #endif