resource_loader.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*************************************************************************/
  2. /* resource_loader.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef RESOURCE_LOADER_H
  30. #define RESOURCE_LOADER_H
  31. #include "resource.h"
  32. /**
  33. @author Juan Linietsky <[email protected]>
  34. */
  35. class ResourceInteractiveLoader : public Reference {
  36. GDCLASS(ResourceInteractiveLoader,Reference);
  37. protected:
  38. static void _bind_methods();
  39. public:
  40. virtual void set_local_path(const String& p_local_path)=0;
  41. virtual Ref<Resource> get_resource()=0;
  42. virtual Error poll()=0;
  43. virtual int get_stage() const=0;
  44. virtual int get_stage_count() const=0;
  45. virtual Error wait();
  46. ResourceInteractiveLoader() {}
  47. };
  48. class ResourceFormatLoader {
  49. public:
  50. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path,Error *r_error=NULL);
  51. virtual RES load(const String &p_path,const String& p_original_path="",Error *r_error=NULL);
  52. virtual void get_recognized_extensions(List<String> *p_extensions) const=0;
  53. virtual void get_recognized_extensions_for_type(const String& p_type,List<String> *p_extensions) const;
  54. bool recognize(const String& p_extension) const;
  55. virtual bool handles_type(const String& p_type) const=0;
  56. virtual String get_resource_type(const String &p_path) const=0;
  57. virtual void get_dependencies(const String& p_path,List<String> *p_dependencies,bool p_add_types=false);
  58. virtual Error load_import_metadata(const String &p_path, Ref<ResourceImportMetadata>& r_var) const { return ERR_UNAVAILABLE; }
  59. virtual Error rename_dependencies(const String &p_path,const Map<String,String>& p_map) { return OK; }
  60. virtual ~ResourceFormatLoader() {}
  61. };
  62. typedef void (*ResourceLoadErrorNotify)(void *p_ud,const String& p_text);
  63. typedef void (*DependencyErrorNotify)(void *p_ud,const String& p_loading,const String& p_which,const String& p_type);
  64. class ResourceLoader {
  65. enum {
  66. MAX_LOADERS=64
  67. };
  68. static ResourceFormatLoader *loader[MAX_LOADERS];
  69. static int loader_count;
  70. static bool timestamp_on_load;
  71. static void* err_notify_ud;
  72. static ResourceLoadErrorNotify err_notify;
  73. static void* dep_err_notify_ud;
  74. static DependencyErrorNotify dep_err_notify;
  75. static bool abort_on_missing_resource;
  76. static String find_complete_path(const String& p_path,const String& p_type);
  77. public:
  78. static Ref<ResourceInteractiveLoader> load_interactive(const String &p_path,const String& p_type_hint="",bool p_no_cache=false,Error *r_error=NULL);
  79. static RES load(const String &p_path,const String& p_type_hint="",bool p_no_cache=false,Error *r_error=NULL);
  80. static Ref<ResourceImportMetadata> load_import_metadata(const String &p_path);
  81. static void get_recognized_extensions_for_type(const String& p_type,List<String> *p_extensions);
  82. static void add_resource_format_loader(ResourceFormatLoader *p_format_loader,bool p_at_front=false);
  83. static String get_resource_type(const String &p_path);
  84. static void get_dependencies(const String& p_path,List<String> *p_dependencies,bool p_add_types=false);
  85. static Error rename_dependencies(const String &p_path,const Map<String,String>& p_map);
  86. static String guess_full_filename(const String &p_path,const String& p_type);
  87. static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load=p_timestamp; }
  88. static void notify_load_error(const String& p_err) { if (err_notify) err_notify(err_notify_ud,p_err); }
  89. static void set_error_notify_func(void* p_ud,ResourceLoadErrorNotify p_err_notify) { err_notify=p_err_notify; err_notify_ud=p_ud;}
  90. static void notify_dependency_error(const String& p_path,const String& p_dependency,const String& p_type) { if (dep_err_notify) dep_err_notify(dep_err_notify_ud,p_path,p_dependency,p_type); }
  91. static void set_dependency_error_notify_func(void* p_ud,DependencyErrorNotify p_err_notify) { dep_err_notify=p_err_notify; dep_err_notify_ud=p_ud;}
  92. static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource=p_abort; }
  93. static bool get_abort_on_missing_resources() { return abort_on_missing_resource; }
  94. };
  95. #endif