| 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 |
|
| 38 |
class ResourceLoaderBinary { |
| 39 |
bool translation_remapped = false; |
| 40 |
String local_path; |
| 41 |
String res_path; |
| 42 |
String type; |
| 43 |
Ref<Resource> resource; |
| 44 |
uint32_t ver_format = 0; |
| 45 |
|
| 46 |
Ref<FileAccess> f; |
| 47 |
|
| 48 |
uint64_t importmd_ofs = 0; |
| 49 |
|
| 50 |
ResourceUID::ID uid = ResourceUID::INVALID_ID; |
| 51 |
|
| 52 |
Vector<char> str_buf; |
| 53 |
List<Ref<Resource>> resource_cache; |
| 54 |
|
| 55 |
Vector<StringName> string_map; |
| 56 |
|
| 57 |
StringName _get_string(); |
| 58 |
|
| 59 |
struct ExtResource { |
| 60 |
String path; |
| 61 |
String type; |
| 62 |
ResourceUID::ID uid = ResourceUID::INVALID_ID; |
| 63 |
Ref<ResourceLoader::LoadToken> load_token; |
| 64 |
}; |
| 65 |
|
| 66 |
bool using_named_scene_ids = false; |
| 67 |
bool using_uids = false; |
| 68 |
String script_class; |
| 69 |
bool use_sub_threads = false; |
| 70 |
float *progress = nullptr; |
| 71 |
Vector<ExtResource> external_resources; |
| 72 |
|
| 73 |
struct IntResource { |
| 74 |
String path; |
| 75 |
uint64_t offset; |
| 76 |
}; |
| 77 |
|
| 78 |
Vector<IntResource> internal_resources; |
| 79 |
HashMap<String, Ref<Resource>> internal_index_cache; |
| 80 |
|
| 81 |
String get_unicode_string(); |
| 82 |
void _advance_padding(uint32_t p_len); |
| 83 |
|
| 84 |
HashMap<String, String> remaps; |
| 85 |
Error error = OK; |
| 86 |
|
| 87 |
ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE; |
| 88 |
ResourceFormatLoader::CacheMode cache_mode_for_external = ResourceFormatLoader::CACHE_MODE_REUSE; |
| 89 |
|
| 90 |
friend class ResourceFormatLoaderBinary; |
| 91 |
|
| 92 |
Error parse_variant(Variant &r_v); |
| 93 |
|
| 94 |
HashMap<String, Ref<Resource>> dependency_cache; |
| 95 |
|
| 96 |
public: |
| 97 |
Ref<Resource> get_resource(); |
| 98 |
Error load(); |
| 99 |
void set_translation_remapped(bool p_remapped); |
| 100 |
|
| 101 |
void set_remaps(const HashMap<String, String> &p_remaps) { remaps = p_remaps; } |
| 102 |
void open(Ref<FileAccess> p_f, bool p_no_resources = false, bool p_keep_uuid_paths = false); |
| 103 |
String recognize(Ref<FileAccess> p_f); |
| 104 |
String recognize_script_class(Ref<FileAccess> p_f); |
| 105 |
void get_dependencies(Ref<FileAccess> p_f, List<String> *p_dependencies, bool p_add_types); |
| 106 |
void get_classes_used(Ref<FileAccess> p_f, HashSet<StringName> *p_classes); |
| 107 |
}; |
| 108 |
|
| 109 |
class ResourceFormatLoaderBinary : public ResourceFormatLoader { |
| 110 |
GDSOFTCLASS(ResourceFormatLoaderBinary, ResourceFormatLoader); |
| 111 |
|
| 112 |
public: |
| 113 |
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; |
| 114 |
virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const override; |
| 115 |
virtual void get_recognized_extensions(List<String> *p_extensions) const override; |
| 116 |
virtual bool handles_type(const String &p_type) const override; |
| 117 |
virtual String get_resource_type(const String &p_path) const override; |
| 118 |
virtual String get_resource_script_class(const String &p_path) const override; |
| 119 |
virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes) override; |
| 120 |
virtual ResourceUID::ID get_resource_uid(const String &p_path) const override; |
| 121 |
virtual bool has_custom_uid_support() const override; |
| 122 |
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override; |
| 123 |
virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) override; |
| 124 |
}; |
| 125 |
|
| 126 |
class ResourceFormatSaverBinaryInstance { |
| 127 |
String local_path; |
| 128 |
String path; |
| 129 |
|
| 130 |
bool relative_paths; |
| 131 |
bool bundle_resources; |
| 132 |
bool skip_editor; |
| 133 |
bool big_endian; |
| 134 |
bool takeover_paths; |
| 135 |
String magic; |
| 136 |
HashSet<Ref<Resource>> resource_set; |
| 137 |
|
| 138 |
struct NonPersistentKey { |
| 139 |
Ref<Resource> base; |
| 140 |
StringName property; |
| 141 |
bool operator<(const NonPersistentKey &p_key) const { return base == p_key.base ? property < p_key.property : base < p_key.base; } |
| 142 |
}; |
| 143 |
|
| 144 |
RBMap<NonPersistentKey, Variant> non_persistent_map; |
| 145 |
HashMap<StringName, int> string_map; |
| 146 |
Vector<StringName> strings; |
| 147 |
|
| 148 |
HashMap<Ref<Resource>, int> external_resources; |
| 149 |
List<Ref<Resource>> saved_resources; |
| 150 |
|
| 151 |
struct Property { |
| 152 |
int name_idx; |
| 153 |
Variant value; |
| 154 |
PropertyInfo pi; |
| 155 |
}; |
| 156 |
|
| 157 |
struct ResourceData { |
| 158 |
String type; |
| 159 |
List<Property> properties; |
| 160 |
}; |
| 161 |
|
| 162 |
static void _pad_buffer(Ref<FileAccess> f, int p_bytes); |
| 163 |
void _find_resources(const Variant &p_variant, bool p_main = false); |
| 164 |
static void save_unicode_string(Ref<FileAccess> f, const String &p_string, bool p_bit_on_len = false); |
| 165 |
int get_string_index(const String &p_string); |
| 166 |
|
| 167 |
public: |
| 168 |
enum { |
| 169 |
FORMAT_FLAG_NAMED_SCENE_IDS = 1, |
| 170 |
FORMAT_FLAG_UIDS = 2, |
| 171 |
FORMAT_FLAG_REAL_T_IS_DOUBLE = 4, |
| 172 |
FORMAT_FLAG_HAS_SCRIPT_CLASS = 8, |
| 173 |
|
| 174 |
|
| 175 |
RESERVED_FIELDS = 11 |
| 176 |
}; |
| 177 |
Error save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags = 0); |
| 178 |
Error set_uid(const String &p_path, ResourceUID::ID p_uid); |
| 179 |
static void write_variant(Ref<FileAccess> f, const Variant &p_property, HashMap<Ref<Resource>, int> &resource_map, HashMap<Ref<Resource>, int> &external_resources, HashMap<StringName, int> &string_map, const PropertyInfo &p_hint = PropertyInfo()); |
| 180 |
}; |
| 181 |
|
| 182 |
class ResourceFormatSaverBinary : public ResourceFormatSaver { |
| 183 |
GDSOFTCLASS(ResourceFormatSaverBinary, ResourceFormatSaver); |
| 184 |
|
| 185 |
public: |
| 186 |
static inline ResourceFormatSaverBinary *singleton = nullptr; |
| 187 |
virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override; |
| 188 |
virtual Error set_uid(const String &p_path, ResourceUID::ID p_uid) override; |
| 189 |
virtual bool recognize(const Ref<Resource> &p_resource) const override; |
| 190 |
virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override; |
| 191 |
|
| 192 |
ResourceFormatSaverBinary(); |
| 193 |
}; |