| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/io/file_access.h" |
| 34 |
#include "core/io/resource_loader.h" |
| 35 |
#include "core/io/resource_saver.h" |
| 36 |
#include "core/templates/rb_map.h" |
| 37 |
#include "core/variant/variant_parser.h" |
| 38 |
#include "scene/resources/packed_scene.h" |
| 39 |
|
| 40 |
class ResourceLoaderText { |
| 41 |
public: |
| 42 |
enum { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
FORMAT_VERSION = 4, |
| 47 |
|
| 48 |
FORMAT_VERSION_COMPAT = 3, |
| 49 |
}; |
| 50 |
|
| 51 |
private: |
| 52 |
bool translation_remapped = false; |
| 53 |
String local_path; |
| 54 |
String res_path; |
| 55 |
String error_text; |
| 56 |
|
| 57 |
Ref<FileAccess> f; |
| 58 |
|
| 59 |
VariantParser::StreamFile stream; |
| 60 |
|
| 61 |
struct ExtResource { |
| 62 |
Ref<ResourceLoader::LoadToken> load_token; |
| 63 |
String path; |
| 64 |
String type; |
| 65 |
}; |
| 66 |
|
| 67 |
bool is_scene = false; |
| 68 |
int format_version; |
| 69 |
String res_type; |
| 70 |
|
| 71 |
bool ignore_resource_parsing = false; |
| 72 |
|
| 73 |
HashMap<String, ExtResource> ext_resources; |
| 74 |
HashMap<String, Ref<Resource>> int_resources; |
| 75 |
|
| 76 |
int resources_total = 0; |
| 77 |
int resource_current = 0; |
| 78 |
|
| 79 |
String resource_type; |
| 80 |
String script_class; |
| 81 |
|
| 82 |
VariantParser::Tag next_tag; |
| 83 |
|
| 84 |
ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE; |
| 85 |
ResourceFormatLoader::CacheMode cache_mode_for_external = ResourceFormatLoader::CACHE_MODE_REUSE; |
| 86 |
|
| 87 |
bool use_sub_threads = false; |
| 88 |
float *progress = nullptr; |
| 89 |
|
| 90 |
mutable int lines = 0; |
| 91 |
|
| 92 |
ResourceUID::ID res_uid = ResourceUID::INVALID_ID; |
| 93 |
|
| 94 |
HashMap<String, String> remaps; |
| 95 |
|
| 96 |
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<ResourceLoaderText *>(p_self)->_parse_sub_resource(p_stream, r_res, line, r_err_str); } |
| 97 |
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<ResourceLoaderText *>(p_self)->_parse_ext_resource(p_stream, r_res, line, r_err_str); } |
| 98 |
|
| 99 |
Error _parse_sub_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str); |
| 100 |
Error _parse_ext_resource(VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str); |
| 101 |
void _count_resources(); |
| 102 |
|
| 103 |
struct DummyReadData { |
| 104 |
bool no_placeholders = false; |
| 105 |
HashMap<Ref<Resource>, int> external_resources; |
| 106 |
HashMap<String, Ref<Resource>> rev_external_resources; |
| 107 |
HashMap<Ref<Resource>, int> resource_index_map; |
| 108 |
HashMap<String, Ref<Resource>> resource_map; |
| 109 |
}; |
| 110 |
|
| 111 |
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(static_cast<DummyReadData *>(p_self), p_stream, r_res, line, r_err_str); } |
| 112 |
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(static_cast<DummyReadData *>(p_self), p_stream, r_res, line, r_err_str); } |
| 113 |
|
| 114 |
static Error _parse_sub_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str); |
| 115 |
static Error _parse_ext_resource_dummy(DummyReadData *p_data, VariantParser::Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str); |
| 116 |
void _printerr(); |
| 117 |
|
| 118 |
VariantParser::ResourceParser rp; |
| 119 |
|
| 120 |
friend class ResourceFormatLoaderText; |
| 121 |
friend class ResourceFormatSaverText; |
| 122 |
|
| 123 |
Error error = OK; |
| 124 |
|
| 125 |
Ref<Resource> resource; |
| 126 |
|
| 127 |
Ref<PackedScene> _parse_node_tag(VariantParser::ResourceParser &parser); |
| 128 |
|
| 129 |
public: |
| 130 |
Ref<Resource> get_resource(); |
| 131 |
Error load(); |
| 132 |
Error set_uid(Ref<FileAccess> p_f, ResourceUID::ID p_uid); |
| 133 |
int get_stage() const; |
| 134 |
int get_stage_count() const; |
| 135 |
void set_translation_remapped(bool p_remapped); |
| 136 |
|
| 137 |
void open(Ref<FileAccess> p_f, bool p_skip_first_tag = false); |
| 138 |
String recognize(Ref<FileAccess> p_f); |
| 139 |
String recognize_script_class(Ref<FileAccess> p_f); |
| 140 |
ResourceUID::ID get_uid(Ref<FileAccess> p_f); |
| 141 |
void get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types); |
| 142 |
Error rename_dependencies(Ref<FileAccess> p_f, const String &p_path, const HashMap<String, String> &p_map); |
| 143 |
Error get_classes_used(HashSet<StringName> *r_classes); |
| 144 |
|
| 145 |
ResourceLoaderText(); |
| 146 |
}; |
| 147 |
|
| 148 |
class ResourceFormatLoaderText : public ResourceFormatLoader { |
| 149 |
GDSOFTCLASS(ResourceFormatLoaderText, ResourceFormatLoader); |
| 150 |
|
| 151 |
public: |
| 152 |
static ResourceFormatLoaderText *singleton; |
| 153 |
virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override; |
| 154 |
virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const override; |
| 155 |
virtual void get_recognized_extensions(List<String> *p_extensions) const override; |
| 156 |
virtual bool handles_type(const String &p_type) const override; |
| 157 |
virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes) override; |
| 158 |
|
| 159 |
virtual String get_resource_type(const String &p_path) const override; |
| 160 |
virtual String get_resource_script_class(const String &p_path) const override; |
| 161 |
virtual ResourceUID::ID get_resource_uid(const String &p_path) const override; |
| 162 |
virtual bool has_custom_uid_support() const override; |
| 163 |
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override; |
| 164 |
virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) override; |
| 165 |
|
| 166 |
ResourceFormatLoaderText() { singleton = this; } |
| 167 |
}; |
| 168 |
|
| 169 |
class ResourceFormatSaverTextInstance { |
| 170 |
String local_path; |
| 171 |
|
| 172 |
Ref<PackedScene> packed_scene; |
| 173 |
|
| 174 |
bool takeover_paths = false; |
| 175 |
bool relative_paths = false; |
| 176 |
bool bundle_resources = false; |
| 177 |
bool skip_editor = false; |
| 178 |
|
| 179 |
struct NonPersistentKey { |
| 180 |
Ref<Resource> base; |
| 181 |
StringName property; |
| 182 |
bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; } |
| 183 |
}; |
| 184 |
|
| 185 |
RBMap<NonPersistentKey, Variant> non_persistent_map; |
| 186 |
|
| 187 |
HashSet<Ref<Resource>> resource_set; |
| 188 |
List<Ref<Resource>> saved_resources; |
| 189 |
HashMap<Ref<Resource>, String> external_resources; |
| 190 |
HashMap<Ref<Resource>, String> internal_resources; |
| 191 |
bool use_compat = true; |
| 192 |
|
| 193 |
struct ResourceSort { |
| 194 |
Ref<Resource> resource; |
| 195 |
String id; |
| 196 |
bool operator<(const ResourceSort &p_right) const { |
| 197 |
return id.naturalnocasecmp_to(p_right.id) < 0; |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
void _find_resources(const Variant &p_variant, bool p_main = false); |
| 202 |
|
| 203 |
static String _write_resources(void *ud, const Ref<Resource> &p_resource); |
| 204 |
String _write_resource(const Ref<Resource> &res); |
| 205 |
|
| 206 |
public: |
| 207 |
Error save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags = 0); |
| 208 |
}; |
| 209 |
|
| 210 |
class ResourceFormatSaverText : public ResourceFormatSaver { |
| 211 |
GDSOFTCLASS(ResourceFormatSaverText, ResourceFormatSaver); |
| 212 |
|
| 213 |
public: |
| 214 |
static ResourceFormatSaverText *singleton; |
| 215 |
virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override; |
| 216 |
virtual Error set_uid(const String &p_path, ResourceUID::ID p_uid) override; |
| 217 |
virtual bool recognize(const Ref<Resource> &p_resource) const override; |
| 218 |
virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override; |
| 219 |
|
| 220 |
ResourceFormatSaverText(); |
| 221 |
}; |