resource_format_xml.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*************************************************************************/
  2. /* resource_format_xml.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_FORMAT_XML_H
  31. #define RESOURCE_FORMAT_XML_H
  32. #include "io/resource_loader.h"
  33. #include "io/resource_saver.h"
  34. #include "os/file_access.h"
  35. class ResourceInteractiveLoaderXML : public ResourceInteractiveLoader {
  36. String local_path;
  37. String res_path;
  38. FileAccess *f;
  39. struct Tag {
  40. String name;
  41. HashMap<String, String> args;
  42. };
  43. _FORCE_INLINE_ Error _parse_array_element(Vector<char> &buff, bool p_number_only, FileAccess *f, bool *end);
  44. struct ExtResource {
  45. String path;
  46. String type;
  47. };
  48. Map<String, String> remaps;
  49. Map<int, ExtResource> ext_resources;
  50. int resources_total;
  51. int resource_current;
  52. String resource_type;
  53. mutable int lines;
  54. uint8_t get_char() const;
  55. int get_current_line() const;
  56. friend class ResourceFormatLoaderXML;
  57. List<Tag> tag_stack;
  58. List<RES> resource_cache;
  59. Tag *parse_tag(bool *r_exit = NULL, bool p_printerr = true, List<String> *r_order = NULL);
  60. Error close_tag(const String &p_name);
  61. _FORCE_INLINE_ void unquote(String &p_str);
  62. Error goto_end_of_tag();
  63. Error parse_property_data(String &r_data);
  64. Error parse_property(Variant &r_v, String &r_name, bool p_for_export_data = false);
  65. Error error;
  66. RES resource;
  67. public:
  68. virtual void set_local_path(const String &p_local_path);
  69. virtual Ref<Resource> get_resource();
  70. virtual Error poll();
  71. virtual int get_stage() const;
  72. virtual int get_stage_count() const;
  73. void open(FileAccess *p_f);
  74. String recognize(FileAccess *p_f);
  75. void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
  76. Error rename_dependencies(FileAccess *p_f, const String &p_path, const Map<String, String> &p_map);
  77. Error get_export_data(FileAccess *p_f, ExportData &r_export_data);
  78. ~ResourceInteractiveLoaderXML();
  79. };
  80. class ResourceFormatLoaderXML : public ResourceFormatLoader {
  81. public:
  82. static ResourceFormatLoaderXML *singleton;
  83. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, Error *r_error = NULL);
  84. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  85. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  86. virtual bool handles_type(const String &p_type) const;
  87. virtual String get_resource_type(const String &p_path) const;
  88. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  89. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  90. virtual Error get_export_data(const String &p_path, ExportData &r_export_data);
  91. ResourceFormatLoaderXML() { singleton = this; }
  92. };
  93. ////////////////////////////////////////////////////////////////////////////////////////////
  94. class ResourceFormatSaverXMLInstance {
  95. String local_path;
  96. bool takeover_paths;
  97. bool relative_paths;
  98. bool bundle_resources;
  99. bool skip_editor;
  100. FileAccess *f;
  101. int depth;
  102. Set<RES> resource_set;
  103. List<RES> saved_resources;
  104. Map<RES, int> external_resources;
  105. void enter_tag(const char *p_tag, const String &p_args = String());
  106. void exit_tag(const char *p_tag);
  107. void _find_resources(const Variant &p_variant, bool p_main = false);
  108. void write_property(const String &p_name, const Variant &p_property, bool *r_ok = NULL);
  109. void escape(String &p_str);
  110. void write_tabs(int p_diff = 0);
  111. void write_string(String p_str, bool p_escape = true);
  112. public:
  113. Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  114. };
  115. class ResourceFormatSaverXML : public ResourceFormatSaver {
  116. public:
  117. static ResourceFormatSaverXML *singleton;
  118. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  119. virtual bool recognize(const RES &p_resource) const;
  120. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  121. ResourceFormatSaverXML();
  122. };
  123. #endif // RESOURCE_FORMAT_XML_H