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