1 /*************************************************************************/
2 /*  resource_format_text.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 
31 #ifndef RESOURCE_FORMAT_TEXT_H
32 #define RESOURCE_FORMAT_TEXT_H
33 
34 #include "core/io/resource_loader.h"
35 #include "core/io/resource_saver.h"
36 #include "core/os/file_access.h"
37 #include "core/variant_parser.h"
38 #include "scene/resources/packed_scene.h"
39 
40 class ResourceInteractiveLoaderText : public ResourceInteractiveLoader {
41 
42 	bool translation_remapped;
43 	String local_path;
44 	String res_path;
45 	String error_text;
46 
47 	FileAccess *f;
48 
49 	VariantParser::StreamFile stream;
50 
51 	struct ExtResource {
52 		String path;
53 		String type;
54 	};
55 
56 	bool is_scene;
57 	String res_type;
58 
59 	bool ignore_resource_parsing;
60 
61 	//Map<String,String> remaps;
62 
63 	Map<int, ExtResource> ext_resources;
64 
65 	int resources_total;
66 	int resource_current;
67 	String resource_type;
68 
69 	VariantParser::Tag next_tag;
70 
71 	mutable int lines;
72 
73 	Map<String, String> remaps;
74 	//void _printerr();
75 
_parse_sub_resources(void * p_self,VariantParser::Stream * p_stream,Ref<Resource> & r_res,int & line,String & r_err_str)76 	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); }
_parse_ext_resources(void * p_self,VariantParser::Stream * p_stream,Ref<Resource> & r_res,int & line,String & r_err_str)77 	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); }
78 
79 	Error _parse_sub_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
80 	Error _parse_ext_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
81 
82 	// for converter
83 	class DummyResource : public Resource {
84 	public:
85 	};
86 
87 	struct DummyReadData {
88 
89 		Map<RES, int> external_resources;
90 		Map<int, RES> rev_external_resources;
91 		Set<RES> resource_set;
92 		Map<int, RES> resource_map;
93 	};
94 
_parse_sub_resource_dummys(void * p_self,VariantParser::Stream * p_stream,Ref<Resource> & r_res,int & line,String & r_err_str)95 	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); }
_parse_ext_resource_dummys(void * p_self,VariantParser::Stream * p_stream,Ref<Resource> & r_res,int & line,String & r_err_str)96 	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); }
97 
98 	static Error _parse_sub_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
99 	static Error _parse_ext_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);
100 
101 	VariantParser::ResourceParser rp;
102 
103 	friend class ResourceFormatLoaderText;
104 
105 	List<RES> resource_cache;
106 	Error error;
107 
108 	RES resource;
109 
110 	Ref<PackedScene> _parse_node_tag(VariantParser::ResourceParser &parser);
111 
112 public:
113 	virtual void set_local_path(const String &p_local_path);
114 	virtual Ref<Resource> get_resource();
115 	virtual Error poll();
116 	virtual int get_stage() const;
117 	virtual int get_stage_count() const;
118 	virtual void set_translation_remapped(bool p_remapped);
119 
120 	void open(FileAccess *p_f, bool p_skip_first_tag = false);
121 	String recognize(FileAccess *p_f);
122 	void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
123 	Error rename_dependencies(FileAccess *p_f, const String &p_path, const Map<String, String> &p_map);
124 
125 	Error save_as_binary(FileAccess *p_f, const String &p_path);
126 	ResourceInteractiveLoaderText();
127 	~ResourceInteractiveLoaderText();
128 };
129 
130 class ResourceFormatLoaderText : public ResourceFormatLoader {
131 public:
132 	static ResourceFormatLoaderText *singleton;
133 	virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
134 	virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
135 	virtual void get_recognized_extensions(List<String> *p_extensions) const;
136 	virtual bool handles_type(const String &p_type) const;
137 	virtual String get_resource_type(const String &p_path) const;
138 	virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
139 	virtual Error rename_dependencies(const String &p_path, const Map<String, String> &p_map);
140 
141 	static Error convert_file_to_binary(const String &p_src_path, const String &p_dst_path);
142 
ResourceFormatLoaderText()143 	ResourceFormatLoaderText() { singleton = this; }
144 };
145 
146 class ResourceFormatSaverTextInstance {
147 
148 	String local_path;
149 
150 	Ref<PackedScene> packed_scene;
151 
152 	bool takeover_paths;
153 	bool relative_paths;
154 	bool bundle_resources;
155 	bool skip_editor;
156 	FileAccess *f;
157 
158 	struct NonPersistentKey { //for resource properties generated on the fly
159 		RES base;
160 		StringName property;
161 		bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; }
162 	};
163 
164 	Map<NonPersistentKey, RES> non_persistent_map;
165 
166 	Set<RES> resource_set;
167 	List<RES> saved_resources;
168 	Map<RES, int> external_resources;
169 	Map<RES, int> internal_resources;
170 
171 	struct ResourceSort {
172 		RES resource;
173 		int index;
174 		bool operator<(const ResourceSort &p_right) const {
175 			return index < p_right.index;
176 		}
177 	};
178 
179 	void _find_resources(const Variant &p_variant, bool p_main = false);
180 
181 	static String _write_resources(void *ud, const RES &p_resource);
182 	String _write_resource(const RES &res);
183 
184 public:
185 	Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
186 };
187 
188 class ResourceFormatSaverText : public ResourceFormatSaver {
189 public:
190 	static ResourceFormatSaverText *singleton;
191 	virtual Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0);
192 	virtual bool recognize(const RES &p_resource) const;
193 	virtual void get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const;
194 
195 	ResourceFormatSaverText();
196 };
197 
198 #endif // RESOURCE_FORMAT_TEXT_H
199