resource_format_text.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**************************************************************************/
  2. /* resource_format_text.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_TEXT_H
  31. #define RESOURCE_FORMAT_TEXT_H
  32. #include "core/io/resource_loader.h"
  33. #include "core/io/resource_saver.h"
  34. #include "core/os/file_access.h"
  35. #include "core/variant_parser.h"
  36. #include "scene/resources/packed_scene.h"
  37. class ResourceInteractiveLoaderText : public ResourceInteractiveLoader {
  38. bool translation_remapped;
  39. String local_path;
  40. String res_path;
  41. String error_text;
  42. FileAccess *f;
  43. VariantParser::StreamFile stream;
  44. struct ExtResource {
  45. RES cache;
  46. String path;
  47. String type;
  48. };
  49. bool is_scene;
  50. String res_type;
  51. bool ignore_resource_parsing;
  52. //Map<String,String> remaps;
  53. Map<int, ExtResource> ext_resources;
  54. Map<int, RES> int_resources;
  55. int resources_total;
  56. int resource_current;
  57. String resource_type;
  58. VariantParser::Tag next_tag;
  59. mutable int lines;
  60. Map<String, String> remaps;
  61. //void _printerr();
  62. static Error _parse_sub_resources(void *p_self, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) { return reinterpret_cast<ResourceInteractiveLoaderText *>(p_self)->_parse_sub_resource(p_stream, r_res, line, r_err_str); }
  63. static Error _parse_ext_resources(void *p_self, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) { return reinterpret_cast<ResourceInteractiveLoaderText *>(p_self)->_parse_ext_resource(p_stream, r_res, line, r_err_str); }
  64. Error _parse_sub_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  65. Error _parse_ext_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  66. // for converter
  67. class DummyResource : public Resource {
  68. public:
  69. };
  70. struct DummyReadData {
  71. Map<RES, int> external_resources;
  72. Map<int, RES> rev_external_resources;
  73. Set<RES> resource_set;
  74. Map<int, RES> resource_map;
  75. };
  76. static Error _parse_sub_resource_dummys(void *p_self, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) { return _parse_sub_resource_dummy((DummyReadData *)(p_self), p_stream, r_res, line, r_err_str); }
  77. static Error _parse_ext_resource_dummys(void *p_self, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str) { return _parse_ext_resource_dummy((DummyReadData *)(p_self), p_stream, r_res, line, r_err_str); }
  78. static Error _parse_sub_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  79. static Error _parse_ext_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
  80. VariantParser::ResourceParser rp;
  81. friend class ResourceFormatLoaderText;
  82. Error error;
  83. RES resource;
  84. Ref<PackedScene> _parse_node_tag(VariantParser::ResourceParser &parser);
  85. public:
  86. virtual void set_local_path(const String &p_local_path);
  87. virtual Ref<Resource> get_resource();
  88. virtual Error poll();
  89. virtual int get_stage() const;
  90. virtual int get_stage_count() const;
  91. virtual void set_translation_remapped(bool p_remapped);
  92. void open(FileAccess *p_f, bool p_skip_first_tag = false);
  93. String recognize(FileAccess *p_f);
  94. void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
  95. Error rename_dependencies(FileAccess *p_f, const String &p_path, const Map<String, String> &p_map);
  96. Error save_as_binary(FileAccess *p_f, const String &p_path);
  97. ResourceInteractiveLoaderText();
  98. ~ResourceInteractiveLoaderText();
  99. };
  100. class ResourceFormatLoaderText : public ResourceFormatLoader {
  101. public:
  102. static ResourceFormatLoaderText *singleton;
  103. virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr);
  104. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  105. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  106. virtual bool handles_type(const String &p_type) const;
  107. virtual String get_resource_type(const String &p_path) const;
  108. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  109. virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
  110. static Error convert_file_to_binary(const String &p_src_path, const String &p_dst_path);
  111. ResourceFormatLoaderText() { singleton = this; }
  112. };
  113. class ResourceFormatSaverTextInstance {
  114. String local_path;
  115. Ref<PackedScene> packed_scene;
  116. bool takeover_paths;
  117. bool relative_paths;
  118. bool bundle_resources;
  119. bool skip_editor;
  120. FileAccess *f;
  121. struct NonPersistentKey { //for resource properties generated on the fly
  122. RES base;
  123. StringName property;
  124. bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; }
  125. };
  126. Map<NonPersistentKey, RES> non_persistent_map;
  127. Set<RES> resource_set;
  128. List<RES> saved_resources;
  129. Map<RES, int> external_resources;
  130. Map<RES, int> internal_resources;
  131. struct ResourceSort {
  132. RES resource;
  133. int index;
  134. bool operator<(const ResourceSort &p_right) const {
  135. return index < p_right.index;
  136. }
  137. };
  138. void _find_resources(const Variant &p_variant, bool p_main = false);
  139. static String _write_resources(void *ud, const RES &p_resource);
  140. String _write_resource(const RES &res);
  141. public:
  142. Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  143. };
  144. class ResourceFormatSaverText : public ResourceFormatSaver {
  145. public:
  146. static ResourceFormatSaverText *singleton;
  147. virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
  148. virtual bool recognize(const RES &p_resource) const;
  149. virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
  150. ResourceFormatSaverText();
  151. };
  152. #endif // RESOURCE_FORMAT_TEXT_H