diff options
Diffstat (limited to 'core/io')
-rw-r--r-- | core/io/compression.cpp | 20 | ||||
-rw-r--r-- | core/io/compression.h | 9 | ||||
-rw-r--r-- | core/io/file_access_compressed.cpp | 2 | ||||
-rw-r--r-- | core/io/file_access_compressed.h | 2 | ||||
-rw-r--r-- | core/io/http_client.cpp | 14 | ||||
-rw-r--r-- | core/io/marshalls.cpp | 20 | ||||
-rw-r--r-- | core/io/resource_format_binary.cpp | 27 | ||||
-rw-r--r-- | core/io/resource_format_binary.h | 2 | ||||
-rw-r--r-- | core/io/resource_import.h | 1 | ||||
-rw-r--r-- | core/io/resource_loader.cpp | 159 | ||||
-rw-r--r-- | core/io/resource_loader.h | 14 |
11 files changed, 216 insertions, 54 deletions
diff --git a/core/io/compression.cpp b/core/io/compression.cpp index 662411a62e..f806c4da6d 100644 --- a/core/io/compression.cpp +++ b/core/io/compression.cpp @@ -28,11 +28,12 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "compression.h" - +#include "global_config.h" #include "os/copymem.h" #include "zip_io.h" #include "thirdparty/misc/fastlz.h" +#include "thirdparty/zstd/zstd.h" #include <zlib.h> @@ -57,7 +58,8 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, strm.zalloc = zipio_alloc; strm.zfree = zipio_free; strm.opaque = Z_NULL; - int err = deflateInit(&strm, Z_DEFAULT_COMPRESSION); + int level = GLOBAL_GET("compression/zlib/compression_level"); + int err = deflateInit(&strm, level); if (err != Z_OK) return -1; @@ -76,6 +78,12 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, return aout; } break; + case MODE_ZSTD: { + + int max_dst_size = get_max_compressed_buffer_size(p_src_size, MODE_ZSTD); + int level = GLOBAL_GET("compression/zstd/compression_level"); + return ZSTD_compress(p_dst, max_dst_size, p_src, p_src_size, level); + } break; } ERR_FAIL_V(-1); @@ -105,6 +113,10 @@ int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) { deflateEnd(&strm); return aout; } break; + case MODE_ZSTD: { + + return ZSTD_compressBound(p_src_size); + } break; } ERR_FAIL_V(-1); @@ -148,6 +160,10 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p ERR_FAIL_COND_V(err != Z_STREAM_END, -1); return total; } break; + case MODE_ZSTD: { + + return ZSTD_decompress(p_dst, p_dst_max_size, p_src, p_src_size); + } break; } ERR_FAIL_V(-1); diff --git a/core/io/compression.h b/core/io/compression.h index a982a074b1..742f0f4d68 100644 --- a/core/io/compression.h +++ b/core/io/compression.h @@ -36,12 +36,13 @@ class Compression { public: enum Mode { MODE_FASTLZ, - MODE_DEFLATE + MODE_DEFLATE, + MODE_ZSTD }; - static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_FASTLZ); - static int get_max_compressed_buffer_size(int p_src_size, Mode p_mode = MODE_FASTLZ); - static int decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_FASTLZ); + static int compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD); + static int get_max_compressed_buffer_size(int p_src_size, Mode p_mode = MODE_ZSTD); + static int decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode = MODE_ZSTD); Compression(); }; diff --git a/core/io/file_access_compressed.cpp b/core/io/file_access_compressed.cpp index 4c6e8f5507..4e802579c6 100644 --- a/core/io/file_access_compressed.cpp +++ b/core/io/file_access_compressed.cpp @@ -369,7 +369,7 @@ FileAccessCompressed::FileAccessCompressed() { f = NULL; magic = "GCMP"; block_size = 16384; - cmode = Compression::MODE_DEFLATE; + cmode = Compression::MODE_ZSTD; writing = false; write_ptr = 0; write_buffer_size = 0; diff --git a/core/io/file_access_compressed.h b/core/io/file_access_compressed.h index 6af9d9b6ca..340c298a0f 100644 --- a/core/io/file_access_compressed.h +++ b/core/io/file_access_compressed.h @@ -64,7 +64,7 @@ class FileAccessCompressed : public FileAccess { FileAccess *f; public: - void configure(const String &p_magic, Compression::Mode p_mode = Compression::MODE_FASTLZ, int p_block_size = 4096); + void configure(const String &p_magic, Compression::Mode p_mode = Compression::MODE_ZSTD, int p_block_size = 4096); Error open_after_magic(FileAccess *p_base); diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 899f3b3b2d..0c84a5213f 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -96,7 +96,12 @@ Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector }; String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n"; - request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; + if ((ssl && conn_port == 443) || (!ssl && conn_port == 80)) { + // don't append the standard ports + request += "Host: " + conn_host + "\r\n"; + } else { + request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; + } bool add_clen = p_body.size() > 0; for (int i = 0; i < p_headers.size(); i++) { request += p_headers[i] + "\r\n"; @@ -151,7 +156,12 @@ Error HTTPClient::request(Method p_method, const String &p_url, const Vector<Str }; String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n"; - request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; + if ((ssl && conn_port == 443) || (!ssl && conn_port == 80)) { + // don't append the standard ports + request += "Host: " + conn_host + "\r\n"; + } else { + request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; + } bool add_clen = p_body.length() > 0; for (int i = 0; i < p_headers.size(); i++) { request += p_headers[i] + "\r\n"; diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index dccf70ad7a..75dfd563dd 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -139,8 +139,8 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int ERR_FAIL_COND_V(len < (int)4 * 4, ERR_INVALID_DATA); Rect2 val; - val.pos.x = decode_float(&buf[0]); - val.pos.y = decode_float(&buf[4]); + val.position.x = decode_float(&buf[0]); + val.position.y = decode_float(&buf[4]); val.size.x = decode_float(&buf[8]); val.size.y = decode_float(&buf[12]); r_variant = val; @@ -211,9 +211,9 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int ERR_FAIL_COND_V(len < (int)4 * 6, ERR_INVALID_DATA); Rect3 val; - val.pos.x = decode_float(&buf[0]); - val.pos.y = decode_float(&buf[4]); - val.pos.z = decode_float(&buf[8]); + val.position.x = decode_float(&buf[0]); + val.position.y = decode_float(&buf[4]); + val.position.z = decode_float(&buf[8]); val.size.x = decode_float(&buf[12]); val.size.y = decode_float(&buf[16]); val.size.z = decode_float(&buf[20]); @@ -861,8 +861,8 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len) { if (buf) { Rect2 r2 = p_variant; - encode_float(r2.pos.x, &buf[0]); - encode_float(r2.pos.y, &buf[4]); + encode_float(r2.position.x, &buf[0]); + encode_float(r2.position.y, &buf[4]); encode_float(r2.size.x, &buf[8]); encode_float(r2.size.y, &buf[12]); } @@ -926,9 +926,9 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len) { if (buf) { Rect3 aabb = p_variant; - encode_float(aabb.pos.x, &buf[0]); - encode_float(aabb.pos.y, &buf[4]); - encode_float(aabb.pos.z, &buf[8]); + encode_float(aabb.position.x, &buf[0]); + encode_float(aabb.position.y, &buf[4]); + encode_float(aabb.position.z, &buf[8]); encode_float(aabb.size.x, &buf[12]); encode_float(aabb.size.y, &buf[16]); encode_float(aabb.size.z, &buf[20]); diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 26b53c2a31..b474c2e078 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -155,8 +155,8 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { case VARIANT_RECT2: { Rect2 v; - v.pos.x = f->get_real(); - v.pos.y = f->get_real(); + v.position.x = f->get_real(); + v.position.y = f->get_real(); v.size.x = f->get_real(); v.size.y = f->get_real(); r_v = v; @@ -191,9 +191,9 @@ Error ResourceInteractiveLoaderBinary::parse_variant(Variant &r_v) { case VARIANT_RECT3: { Rect3 v; - v.pos.x = f->get_real(); - v.pos.y = f->get_real(); - v.pos.z = f->get_real(); + v.position.x = f->get_real(); + v.position.y = f->get_real(); + v.position.z = f->get_real(); v.size.x = f->get_real(); v.size.y = f->get_real(); v.size.z = f->get_real(); @@ -689,6 +689,7 @@ Error ResourceInteractiveLoaderBinary::poll() { f->close(); resource = res; + resource->set_as_translation_remapped(translation_remapped); error = ERR_FILE_EOF; } else { @@ -706,6 +707,11 @@ int ResourceInteractiveLoaderBinary::get_stage_count() const { return external_resources.size() + internal_resources.size(); } +void ResourceInteractiveLoaderBinary::set_translation_remapped(bool p_remapped) { + + translation_remapped = p_remapped; +} + static void save_ustring(FileAccess *f, const String &p_string) { CharString utf8 = p_string.utf8(); @@ -920,6 +926,7 @@ ResourceInteractiveLoaderBinary::ResourceInteractiveLoaderBinary() { endian_swap = false; use_real64 = false; error = OK; + translation_remapped = false; } ResourceInteractiveLoaderBinary::~ResourceInteractiveLoaderBinary() { @@ -1288,8 +1295,8 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property, f->store_32(VARIANT_RECT2); Rect2 val = p_property; - f->store_real(val.pos.x); - f->store_real(val.pos.y); + f->store_real(val.position.x); + f->store_real(val.position.y); f->store_real(val.size.x); f->store_real(val.size.y); @@ -1327,9 +1334,9 @@ void ResourceFormatSaverBinaryInstance::write_variant(const Variant &p_property, f->store_32(VARIANT_RECT3); Rect3 val = p_property; - f->store_real(val.pos.x); - f->store_real(val.pos.y); - f->store_real(val.pos.z); + f->store_real(val.position.x); + f->store_real(val.position.y); + f->store_real(val.position.z); f->store_real(val.size.x); f->store_real(val.size.y); f->store_real(val.size.z); diff --git a/core/io/resource_format_binary.h b/core/io/resource_format_binary.h index 59b9d66d8f..5da5a0fc37 100644 --- a/core/io/resource_format_binary.h +++ b/core/io/resource_format_binary.h @@ -36,6 +36,7 @@ class ResourceInteractiveLoaderBinary : public ResourceInteractiveLoader { + bool translation_remapped; String local_path; String res_path; String type; @@ -87,6 +88,7 @@ public: virtual Error poll(); virtual int get_stage() const; virtual int get_stage_count() const; + virtual void set_translation_remapped(bool p_remapped); void set_remaps(const Map<String, String> &p_remaps) { remaps = p_remaps; } void open(FileAccess *p_f); diff --git a/core/io/resource_import.h b/core/io/resource_import.h index 25b7a534b2..9d2a5180dc 100644 --- a/core/io/resource_import.h +++ b/core/io/resource_import.h @@ -61,6 +61,7 @@ public: String get_internal_resource_path(const String &p_path) const; void add_importer(const Ref<ResourceImporter> &p_importer) { importers.insert(p_importer); } + void remove_importer(const Ref<ResourceImporter> &p_importer) { importers.erase(p_importer); } Ref<ResourceImporter> get_importer_by_name(const String &p_name); Ref<ResourceImporter> get_importer_by_extension(const String &p_extension); void get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter> > *r_importers); diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index 234d71cb68..bb7be38413 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -29,10 +29,12 @@ /*************************************************************************/ #include "resource_loader.h" #include "global_config.h" +#include "io/resource_import.h" #include "os/file_access.h" #include "os/os.h" #include "path_remap.h" #include "print_string.h" +#include "translation.h" ResourceFormatLoader *ResourceLoader::loader[MAX_LOADERS]; int ResourceLoader::loader_count = 0; @@ -102,6 +104,7 @@ public: virtual Error poll() { return ERR_FILE_EOF; } virtual int get_stage() const { return 1; } virtual int get_stage_count() const { return 1; } + virtual void set_translation_remapped(bool p_remapped) { resource->set_as_translation_remapped(p_remapped); } ResourceInteractiveLoaderDefault() {} }; @@ -165,38 +168,45 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p else local_path = GlobalConfig::get_singleton()->localize_path(p_path); - ERR_FAIL_COND_V(local_path == "", RES()); + bool xl_remapped = false; + String path = _path_remap(local_path, &xl_remapped); - if (!p_no_cache && ResourceCache::has(local_path)) { + ERR_FAIL_COND_V(path == "", RES()); + + if (!p_no_cache && ResourceCache::has(path)) { if (OS::get_singleton()->is_stdout_verbose()) - print_line("load resource: " + local_path + " (cached)"); + print_line("load resource: " + path + " (cached)"); - return RES(ResourceCache::get(local_path)); + return RES(ResourceCache::get(path)); } if (OS::get_singleton()->is_stdout_verbose()) - print_line("load resource: " + local_path); + print_line("load resource: " + path); bool found = false; // Try all loaders and pick the first match for the type hint for (int i = 0; i < loader_count; i++) { - if (!loader[i]->recognize_path(local_path, p_type_hint)) { + if (!loader[i]->recognize_path(path, p_type_hint)) { continue; } found = true; - RES res = loader[i]->load(local_path, local_path, r_error); + RES res = loader[i]->load(path, path, r_error); if (res.is_null()) { continue; } if (!p_no_cache) res->set_path(local_path); + + if (xl_remapped) + res->set_as_translation_remapped(true); + #ifdef TOOLS_ENABLED res->set_edited(false); if (timestamp_on_load) { - uint64_t mt = FileAccess::get_modified_time(local_path); + uint64_t mt = FileAccess::get_modified_time(path); //printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt); res->set_last_modified_time(mt); } @@ -206,9 +216,9 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p } if (found) { - ERR_EXPLAIN("Failed loading resource: " + p_path); + ERR_EXPLAIN("Failed loading resource: " + path); } else { - ERR_EXPLAIN("No loader found for resource: " + p_path); + ERR_EXPLAIN("No loader found for resource: " + path); } ERR_FAIL_V(RES()); return RES(); @@ -225,14 +235,17 @@ Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_ else local_path = GlobalConfig::get_singleton()->localize_path(p_path); - ERR_FAIL_COND_V(local_path == "", Ref<ResourceInteractiveLoader>()); + bool xl_remapped = false; + String path = _path_remap(local_path, &xl_remapped); + + ERR_FAIL_COND_V(path == "", Ref<ResourceInteractiveLoader>()); - if (!p_no_cache && ResourceCache::has(local_path)) { + if (!p_no_cache && ResourceCache::has(path)) { if (OS::get_singleton()->is_stdout_verbose()) - print_line("load resource: " + local_path + " (cached)"); + print_line("load resource: " + path + " (cached)"); - Ref<Resource> res_cached = ResourceCache::get(local_path); + Ref<Resource> res_cached = ResourceCache::get(path); Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault)); ril->resource = res_cached; @@ -246,22 +259,24 @@ Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_ for (int i = 0; i < loader_count; i++) { - if (!loader[i]->recognize_path(local_path, p_type_hint)) + if (!loader[i]->recognize_path(path, p_type_hint)) continue; found = true; - Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(local_path, r_error); + Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(path, r_error); if (ril.is_null()) continue; if (!p_no_cache) ril->set_local_path(local_path); + if (xl_remapped) + ril->set_translation_remapped(true); return ril; } if (found) { - ERR_EXPLAIN("Failed loading resource: " + p_path); + ERR_EXPLAIN("Failed loading resource: " + path); } else { - ERR_EXPLAIN("No loader found for resource: " + p_path); + ERR_EXPLAIN("No loader found for resource: " + path); } ERR_FAIL_V(Ref<ResourceInteractiveLoader>()); return Ref<ResourceInteractiveLoader>(); @@ -283,11 +298,13 @@ void ResourceLoader::add_resource_format_loader(ResourceFormatLoader *p_format_l void ResourceLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) { + String path = _path_remap(p_path); + String local_path; - if (p_path.is_rel_path()) - local_path = "res://" + p_path; + if (path.is_rel_path()) + local_path = "res://" + path; else - local_path = GlobalConfig::get_singleton()->localize_path(p_path); + local_path = GlobalConfig::get_singleton()->localize_path(path); for (int i = 0; i < loader_count; i++) { @@ -304,11 +321,13 @@ void ResourceLoader::get_dependencies(const String &p_path, List<String> *p_depe Error ResourceLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) { + String path = _path_remap(p_path); + String local_path; - if (p_path.is_rel_path()) - local_path = "res://" + p_path; + if (path.is_rel_path()) + local_path = "res://" + path; else - local_path = GlobalConfig::get_singleton()->localize_path(p_path); + local_path = GlobalConfig::get_singleton()->localize_path(path); for (int i = 0; i < loader_count; i++) { @@ -342,6 +361,95 @@ String ResourceLoader::get_resource_type(const String &p_path) { return ""; } + +String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_remapped) { + + if (translation_remaps.has(p_path)) { + + Vector<String> &v = *translation_remaps.getptr(p_path); + String locale = TranslationServer::get_singleton()->get_locale(); + if (r_translation_remapped) { + *r_translation_remapped = true; + } + for (int i = 0; i < v.size(); i++) { + + int split = v[i].find_last(":"); + if (split == -1) + continue; + String l = v[i].right(split + 1).strip_edges(); + if (l == String()) + continue; + + if (l.begins_with(locale)) { + return v[i].left(split); + } + } + } + + return p_path; +} + +String ResourceLoader::import_remap(const String &p_path) { + + if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) { + + return ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path); + } + + return p_path; +} + +String ResourceLoader::path_remap(const String &p_path) { + return _path_remap(p_path); +} + +void ResourceLoader::reload_translation_remaps() { + + if (ResourceCache::lock) { + ResourceCache::lock->read_lock(); + } + + List<Resource *> to_reload; + SelfList<Resource> *E = remapped_list.first(); + + while (E) { + to_reload.push_back(E->self()); + E = E->next(); + } + + if (ResourceCache::lock) { + ResourceCache::lock->read_unlock(); + } + + //now just make sure to not delete any of these resources while changing locale.. + while (to_reload.front()) { + to_reload.front()->get()->reload_from_file(); + to_reload.pop_front(); + } +} + +void ResourceLoader::load_translation_remaps() { + + Dictionary remaps = GlobalConfig::get_singleton()->get("locale/translation_remaps"); + List<Variant> keys; + remaps.get_key_list(&keys); + for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { + + Array langs = remaps[E->get()]; + Vector<String> lang_remaps; + lang_remaps.resize(langs.size()); + for (int i = 0; i < langs.size(); i++) { + lang_remaps[i] = langs[i]; + } + + translation_remaps[String(E->get())] = lang_remaps; + } +} + +void ResourceLoader::clear_translation_remaps() { + translation_remaps.clear(); +} + ResourceLoadErrorNotify ResourceLoader::err_notify = NULL; void *ResourceLoader::err_notify_ud = NULL; @@ -350,3 +458,6 @@ void *ResourceLoader::dep_err_notify_ud = NULL; bool ResourceLoader::abort_on_missing_resource = true; bool ResourceLoader::timestamp_on_load = false; + +SelfList<Resource>::List ResourceLoader::remapped_list; +HashMap<String, Vector<String> > ResourceLoader::translation_remaps; diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index 54b62f6916..e6687800d7 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -49,6 +49,7 @@ public: virtual Error poll() = 0; virtual int get_stage() const = 0; virtual int get_stage_count() const = 0; + virtual void set_translation_remapped(bool p_remapped) = 0; virtual Error wait(); ResourceInteractiveLoader() {} @@ -87,6 +88,12 @@ class ResourceLoader { static void *dep_err_notify_ud; static DependencyErrorNotify dep_err_notify; static bool abort_on_missing_resource; + static HashMap<String, Vector<String> > translation_remaps; + + static String _path_remap(const String &p_path, bool *r_translation_remapped = NULL); + friend class Resource; + + static SelfList<Resource>::List remapped_list; public: static Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL); @@ -118,6 +125,13 @@ public: static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource = p_abort; } static bool get_abort_on_missing_resources() { return abort_on_missing_resource; } + + static String path_remap(const String &p_path); + static String import_remap(const String &p_path); + + static void reload_translation_remaps(); + static void load_translation_remaps(); + static void clear_translation_remaps(); }; #endif |