diff options
Diffstat (limited to 'core/io')
79 files changed, 7345 insertions, 2709 deletions
diff --git a/core/io/compression.cpp b/core/io/compression.cpp index 20c9fdca6f..456023e2a6 100644 --- a/core/io/compression.cpp +++ b/core/io/compression.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,9 +30,9 @@ #include "compression.h" +#include "core/config/project_settings.h" #include "core/io/zip_io.h" #include "core/os/copymem.h" -#include "core/project_settings.h" #include "thirdparty/misc/fastlz.h" @@ -40,10 +40,8 @@ #include <zstd.h> int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, Mode p_mode) { - switch (p_mode) { case MODE_FASTLZ: { - if (p_src_size < 16) { uint8_t src[16]; zeromem(&src[p_src_size], 16 - p_src_size); @@ -56,7 +54,6 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, } break; case MODE_DEFLATE: case MODE_GZIP: { - int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16; z_stream strm; @@ -65,8 +62,9 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, strm.opaque = Z_NULL; int level = p_mode == MODE_DEFLATE ? zlib_level : gzip_level; int err = deflateInit2(&strm, level, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY); - if (err != Z_OK) + if (err != Z_OK) { return -1; + } strm.avail_in = p_src_size; int aout = deflateBound(&strm, p_src_size); @@ -97,19 +95,17 @@ int Compression::compress(uint8_t *p_dst, const uint8_t *p_src, int p_src_size, } int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) { - switch (p_mode) { case MODE_FASTLZ: { - int ss = p_src_size + p_src_size * 6 / 100; - if (ss < 66) + if (ss < 66) { ss = 66; + } return ss; } break; case MODE_DEFLATE: case MODE_GZIP: { - int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16; z_stream strm; @@ -117,14 +113,14 @@ int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) { strm.zfree = zipio_free; strm.opaque = Z_NULL; int err = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, window_bits, 8, Z_DEFAULT_STRATEGY); - if (err != Z_OK) + if (err != Z_OK) { return -1; + } int aout = deflateBound(&strm, p_src_size); deflateEnd(&strm); return aout; } break; case MODE_ZSTD: { - return ZSTD_compressBound(p_src_size); } break; } @@ -133,10 +129,8 @@ int Compression::get_max_compressed_buffer_size(int p_src_size, Mode p_mode) { } int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p_src, int p_src_size, Mode p_mode) { - switch (p_mode) { case MODE_FASTLZ: { - int ret_size = 0; if (p_dst_max_size < 16) { @@ -150,7 +144,6 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p } break; case MODE_DEFLATE: case MODE_GZIP: { - int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16; z_stream strm; @@ -187,8 +180,95 @@ int Compression::decompress(uint8_t *p_dst, int p_dst_max_size, const uint8_t *p ERR_FAIL_V(-1); } +/** + This will handle both Gzip and Deflat streams. It will automatically allocate the output buffer into the provided p_dst_vect Vector. + This is required for compressed data who's final uncompressed size is unknown, as is the case for HTTP response bodies. + This is much slower however than using Compression::decompress because it may result in multiple full copies of the output buffer. +*/ +int Compression::decompress_dynamic(Vector<uint8_t> *p_dst_vect, int p_max_dst_size, const uint8_t *p_src, int p_src_size, Mode p_mode) { + int ret; + uint8_t *dst = nullptr; + int out_mark = 0; + z_stream strm; + + ERR_FAIL_COND_V(p_src_size <= 0, Z_DATA_ERROR); + + // This function only supports GZip and Deflate + int window_bits = p_mode == MODE_DEFLATE ? 15 : 15 + 16; + ERR_FAIL_COND_V(p_mode != MODE_DEFLATE && p_mode != MODE_GZIP, Z_ERRNO); + + // Initialize the stream + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = 0; + strm.next_in = Z_NULL; + + int err = inflateInit2(&strm, window_bits); + ERR_FAIL_COND_V(err != Z_OK, -1); + + // Setup the stream inputs + strm.next_in = (Bytef *)p_src; + strm.avail_in = p_src_size; + + // Ensure the destination buffer is empty + p_dst_vect->resize(0); + + // decompress until deflate stream ends or end of file + do { + // Add another chunk size to the output buffer + // This forces a copy of the whole buffer + p_dst_vect->resize(p_dst_vect->size() + gzip_chunk); + // Get pointer to the actual output buffer + dst = p_dst_vect->ptrw(); + + // Set the stream to the new output stream + // Since it was copied, we need to reset the stream to the new buffer + strm.next_out = &(dst[out_mark]); + strm.avail_out = gzip_chunk; + + // run inflate() on input until output buffer is full and needs to be resized + // or input runs out + do { + ret = inflate(&strm, Z_SYNC_FLUSH); + + switch (ret) { + case Z_NEED_DICT: + ret = Z_DATA_ERROR; + [[fallthrough]]; + case Z_DATA_ERROR: + case Z_MEM_ERROR: + case Z_STREAM_ERROR: + WARN_PRINT(strm.msg); + (void)inflateEnd(&strm); + p_dst_vect->resize(0); + return ret; + } + } while (strm.avail_out > 0 && strm.avail_in > 0); + + out_mark += gzip_chunk; + + // Encorce max output size + if (p_max_dst_size > -1 && strm.total_out > (uint64_t)p_max_dst_size) { + (void)inflateEnd(&strm); + p_dst_vect->resize(0); + return Z_BUF_ERROR; + } + } while (ret != Z_STREAM_END); + + // If all done successfully, resize the output if it's larger than the actual output + if ((unsigned long)p_dst_vect->size() > strm.total_out) { + p_dst_vect->resize(strm.total_out); + } + + // clean up and return + (void)inflateEnd(&strm); + return Z_OK; +} + int Compression::zlib_level = Z_DEFAULT_COMPRESSION; int Compression::gzip_level = Z_DEFAULT_COMPRESSION; int Compression::zstd_level = 3; bool Compression::zstd_long_distance_matching = false; int Compression::zstd_window_log_size = 27; // ZSTD_WINDOWLOG_LIMIT_DEFAULT +int Compression::gzip_chunk = 16384; diff --git a/core/io/compression.h b/core/io/compression.h index 8354b581fa..cbfed74124 100644 --- a/core/io/compression.h +++ b/core/io/compression.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,16 +31,17 @@ #ifndef COMPRESSION_H #define COMPRESSION_H +#include "core/templates/vector.h" #include "core/typedefs.h" class Compression { - public: static int zlib_level; static int gzip_level; static int zstd_level; static bool zstd_long_distance_matching; static int zstd_window_log_size; + static int gzip_chunk; enum Mode { MODE_FASTLZ, @@ -52,8 +53,9 @@ public: 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); + static int decompress_dynamic(Vector<uint8_t> *p_dst_vect, int p_max_dst_size, const uint8_t *p_src, int p_src_size, Mode p_mode); - Compression(); + Compression() {} }; #endif // COMPRESSION_H diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index 73230e3a3c..015c1f0d90 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -32,17 +32,15 @@ #include "core/io/file_access_encrypted.h" #include "core/os/keyboard.h" -#include "core/variant_parser.h" +#include "core/variant/variant_parser.h" PackedStringArray ConfigFile::_get_sections() const { - List<String> s; get_sections(&s); PackedStringArray arr; arr.resize(s.size()); int idx = 0; for (const List<String>::Element *E = s.front(); E; E = E->next()) { - arr.set(idx++, E->get()); } @@ -50,14 +48,12 @@ PackedStringArray ConfigFile::_get_sections() const { } PackedStringArray ConfigFile::_get_section_keys(const String &p_section) const { - List<String> s; get_section_keys(p_section, &s); PackedStringArray arr; arr.resize(s.size()); int idx = 0; for (const List<String>::Element *E = s.front(); E; E = E->next()) { - arr.set(idx++, E->get()); } @@ -65,13 +61,13 @@ PackedStringArray ConfigFile::_get_section_keys(const String &p_section) const { } void ConfigFile::set_value(const String &p_section, const String &p_key, const Variant &p_value) { - if (p_value.get_type() == Variant::NIL) { //erase - if (!values.has(p_section)) + if (!values.has(p_section)) { return; // ? + } values[p_section].erase(p_key); - if (values[p_section].empty()) { + if (values[p_section].is_empty()) { values.erase(p_section); } @@ -83,8 +79,8 @@ void ConfigFile::set_value(const String &p_section, const String &p_key, const V values[p_section][p_key] = p_value; } } -Variant ConfigFile::get_value(const String &p_section, const String &p_key, Variant p_default) const { +Variant ConfigFile::get_value(const String &p_section, const String &p_key, Variant p_default) const { if (!values.has(p_section) || !values[p_section].has(p_key)) { ERR_FAIL_COND_V_MSG(p_default.get_type() == Variant::NIL, Variant(), vformat("Couldn't find the given section \"%s\" and key \"%s\", and no default was given.", p_section, p_key)); @@ -95,24 +91,23 @@ Variant ConfigFile::get_value(const String &p_section, const String &p_key, Vari } bool ConfigFile::has_section(const String &p_section) const { - return values.has(p_section); } -bool ConfigFile::has_section_key(const String &p_section, const String &p_key) const { - if (!values.has(p_section)) +bool ConfigFile::has_section_key(const String &p_section, const String &p_key) const { + if (!values.has(p_section)) { return false; + } return values[p_section].has(p_key); } void ConfigFile::get_sections(List<String> *r_sections) const { - for (OrderedHashMap<String, OrderedHashMap<String, Variant>>::ConstElement E = values.front(); E; E = E.next()) { r_sections->push_back(E.key()); } } -void ConfigFile::get_section_keys(const String &p_section, List<String> *r_keys) const { +void ConfigFile::get_section_keys(const String &p_section, List<String> *r_keys) const { ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot get keys from nonexistent section \"%s\".", p_section)); for (OrderedHashMap<String, Variant>::ConstElement E = values[p_section].front(); E; E = E.next()) { @@ -121,13 +116,11 @@ void ConfigFile::get_section_keys(const String &p_section, List<String> *r_keys) } void ConfigFile::erase_section(const String &p_section) { - ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot erase nonexistent section \"%s\".", p_section)); values.erase(p_section); } void ConfigFile::erase_section_key(const String &p_section, const String &p_key) { - ERR_FAIL_COND_MSG(!values.has(p_section), vformat("Cannot erase key \"%s\" from nonexistent section \"%s\".", p_key, p_section)); ERR_FAIL_COND_MSG(!values[p_section].has(p_key), vformat("Cannot erase nonexistent key \"%s\" from section \"%s\".", p_key, p_section)); @@ -135,13 +128,13 @@ void ConfigFile::erase_section_key(const String &p_section, const String &p_key) } Error ConfigFile::save(const String &p_path) { - Error err; FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err); if (err) { - if (file) + if (file) { memdelete(file); + } return err; } @@ -149,12 +142,12 @@ Error ConfigFile::save(const String &p_path) { } Error ConfigFile::save_encrypted(const String &p_path, const Vector<uint8_t> &p_key) { - Error err; FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err); - if (err) + if (err) { return err; + } FileAccessEncrypted *fae = memnew(FileAccessEncrypted); err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_WRITE_AES256); @@ -167,12 +160,12 @@ Error ConfigFile::save_encrypted(const String &p_path, const Vector<uint8_t> &p_ } Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass) { - Error err; FileAccess *f = FileAccess::open(p_path, FileAccess::WRITE, &err); - if (err) + if (err) { return err; + } FileAccessEncrypted *fae = memnew(FileAccessEncrypted); err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_WRITE_AES256); @@ -186,15 +179,13 @@ Error ConfigFile::save_encrypted_pass(const String &p_path, const String &p_pass } Error ConfigFile::_internal_save(FileAccess *file) { - for (OrderedHashMap<String, OrderedHashMap<String, Variant>>::Element E = values.front(); E; E = E.next()) { - - if (E != values.front()) + if (E != values.front()) { file->store_string("\n"); + } file->store_string("[" + E.key() + "]\n\n"); for (OrderedHashMap<String, Variant>::Element F = E.get().front(); F; F = F.next()) { - String vstr; VariantWriter::write_to_string(F.get(), vstr); file->store_string(F.key() + "=" + vstr + "\n"); @@ -207,23 +198,23 @@ Error ConfigFile::_internal_save(FileAccess *file) { } Error ConfigFile::load(const String &p_path) { - Error err; FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); - if (!f) + if (!f) { return err; + } return _internal_load(p_path, f); } Error ConfigFile::load_encrypted(const String &p_path, const Vector<uint8_t> &p_key) { - Error err; FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); - if (err) + if (err) { return err; + } FileAccessEncrypted *fae = memnew(FileAccessEncrypted); err = fae->open_and_parse(f, p_key, FileAccessEncrypted::MODE_READ); @@ -236,12 +227,12 @@ Error ConfigFile::load_encrypted(const String &p_path, const Vector<uint8_t> &p_ } Error ConfigFile::load_encrypted_pass(const String &p_path, const String &p_pass) { - Error err; FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); - if (err) + if (err) { return err; + } FileAccessEncrypted *fae = memnew(FileAccessEncrypted); err = fae->open_and_parse_password(f, p_pass, FileAccessEncrypted::MODE_READ); @@ -255,7 +246,6 @@ Error ConfigFile::load_encrypted_pass(const String &p_path, const String &p_pass } Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) { - VariantParser::StreamFile stream; stream.f = f; @@ -267,14 +257,12 @@ Error ConfigFile::_internal_load(const String &p_path, FileAccess *f) { } Error ConfigFile::parse(const String &p_data) { - VariantParser::StreamString stream; stream.s = p_data; return _parse("<string>", &stream); } Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) { - String assign; Variant value; VariantParser::Tag next_tag; @@ -285,7 +273,6 @@ Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) String section; while (true) { - assign = Variant(); next_tag.fields.clear(); next_tag.name = String(); @@ -309,7 +296,6 @@ Error ConfigFile::_parse(const String &p_path, VariantParser::Stream *p_stream) } void ConfigFile::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_value", "section", "key", "value"), &ConfigFile::set_value); ClassDB::bind_method(D_METHOD("get_value", "section", "key", "default"), &ConfigFile::get_value, DEFVAL(Variant())); diff --git a/core/io/config_file.h b/core/io/config_file.h index 39fc2ab412..386d304f07 100644 --- a/core/io/config_file.h +++ b/core/io/config_file.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,13 +31,12 @@ #ifndef CONFIG_FILE_H #define CONFIG_FILE_H -#include "core/ordered_hash_map.h" +#include "core/object/reference.h" #include "core/os/file_access.h" -#include "core/reference.h" -#include "core/variant_parser.h" +#include "core/templates/ordered_hash_map.h" +#include "core/variant/variant_parser.h" class ConfigFile : public Reference { - GDCLASS(ConfigFile, Reference); OrderedHashMap<String, OrderedHashMap<String, Variant>> values; diff --git a/core/io/dtls_server.cpp b/core/io/dtls_server.cpp index 5bda06e5b9..288b2efe0e 100644 --- a/core/io/dtls_server.cpp +++ b/core/io/dtls_server.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -29,15 +29,18 @@ /*************************************************************************/ #include "dtls_server.h" + +#include "core/config/project_settings.h" #include "core/os/file_access.h" -#include "core/project_settings.h" DTLSServer *(*DTLSServer::_create)() = nullptr; bool DTLSServer::available = false; DTLSServer *DTLSServer::create() { - - return _create(); + if (_create) { + return _create(); + } + return nullptr; } bool DTLSServer::is_available() { @@ -45,10 +48,6 @@ bool DTLSServer::is_available() { } void DTLSServer::_bind_methods() { - ClassDB::bind_method(D_METHOD("setup", "key", "certificate", "chain"), &DTLSServer::setup, DEFVAL(Ref<X509Certificate>())); ClassDB::bind_method(D_METHOD("take_connection", "udp_peer"), &DTLSServer::take_connection); } - -DTLSServer::DTLSServer() { -} diff --git a/core/io/dtls_server.h b/core/io/dtls_server.h index 7b08138f7f..92b6caf508 100644 --- a/core/io/dtls_server.h +++ b/core/io/dtls_server.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -51,7 +51,7 @@ public: virtual void stop() = 0; virtual Ref<PacketPeerDTLS> take_connection(Ref<PacketPeerUDP> p_peer) = 0; - DTLSServer(); + DTLSServer() {} }; #endif // DTLS_SERVER_H diff --git a/core/io/file_access_buffered.cpp b/core/io/file_access_buffered.cpp deleted file mode 100644 index ab0fb3943c..0000000000 --- a/core/io/file_access_buffered.cpp +++ /dev/null @@ -1,176 +0,0 @@ -/*************************************************************************/ -/* file_access_buffered.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "file_access_buffered.h" - -#include "core/error_macros.h" - -Error FileAccessBuffered::set_error(Error p_error) const { - - return (last_error = p_error); -} - -void FileAccessBuffered::set_cache_size(int p_size) { - - cache_size = p_size; -} - -int FileAccessBuffered::get_cache_size() { - - return cache_size; -} - -int FileAccessBuffered::cache_data_left() const { - - if (file.offset >= file.size) { - return 0; - } - - if (cache.offset == -1 || file.offset < cache.offset || file.offset >= cache.offset + cache.buffer.size()) { - - return read_data_block(file.offset, cache_size); - } - - return cache.buffer.size() - (file.offset - cache.offset); -} - -void FileAccessBuffered::seek(size_t p_position) { - - file.offset = p_position; -} - -void FileAccessBuffered::seek_end(int64_t p_position) { - - file.offset = file.size + p_position; -} - -size_t FileAccessBuffered::get_position() const { - - return file.offset; -} - -size_t FileAccessBuffered::get_len() const { - - return file.size; -} - -bool FileAccessBuffered::eof_reached() const { - - return file.offset > file.size; -} - -uint8_t FileAccessBuffered::get_8() const { - - ERR_FAIL_COND_V_MSG(!file.open, 0, "Can't get data, when file is not opened."); - - uint8_t byte = 0; - if (cache_data_left() >= 1) { - - byte = cache.buffer[file.offset - cache.offset]; - } - - ++file.offset; - - return byte; -} - -int FileAccessBuffered::get_buffer(uint8_t *p_dest, int p_length) const { - - ERR_FAIL_COND_V_MSG(!file.open, -1, "Can't get buffer, when file is not opened."); - - if (p_length > cache_size) { - - int total_read = 0; - - if (!(cache.offset == -1 || file.offset < cache.offset || file.offset >= cache.offset + cache.buffer.size())) { - - int size = (cache.buffer.size() - (file.offset - cache.offset)); - size = size - (size % 4); - //const uint8_t* read = cache.buffer.ptr(); - //memcpy(p_dest, read.ptr() + (file.offset - cache.offset), size); - memcpy(p_dest, cache.buffer.ptr() + (file.offset - cache.offset), size); - p_dest += size; - p_length -= size; - file.offset += size; - total_read += size; - } - - int err = read_data_block(file.offset, p_length, p_dest); - if (err >= 0) { - total_read += err; - file.offset += err; - } - - return total_read; - } - - int to_read = p_length; - int total_read = 0; - while (to_read > 0) { - - int left = cache_data_left(); - if (left == 0) { - file.offset += to_read; - return total_read; - } - if (left < 0) { - return left; - } - - int r = MIN(left, to_read); - //const uint8_t* read = cache.buffer.ptr(); - //memcpy(p_dest+total_read, &read.ptr()[file.offset - cache.offset], r); - memcpy(p_dest + total_read, cache.buffer.ptr() + (file.offset - cache.offset), r); - - file.offset += r; - total_read += r; - to_read -= r; - } - - return p_length; -} - -bool FileAccessBuffered::is_open() const { - - return file.open; -} - -Error FileAccessBuffered::get_error() const { - - return last_error; -} - -FileAccessBuffered::FileAccessBuffered() { - - cache_size = DEFAULT_CACHE_SIZE; -} - -FileAccessBuffered::~FileAccessBuffered() { -} diff --git a/core/io/file_access_buffered.h b/core/io/file_access_buffered.h deleted file mode 100644 index a6177c20be..0000000000 --- a/core/io/file_access_buffered.h +++ /dev/null @@ -1,94 +0,0 @@ -/*************************************************************************/ -/* file_access_buffered.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef FILE_ACCESS_BUFFERED_H -#define FILE_ACCESS_BUFFERED_H - -#include "core/os/file_access.h" - -#include "core/ustring.h" - -class FileAccessBuffered : public FileAccess { - -public: - enum { - DEFAULT_CACHE_SIZE = 128 * 1024, - }; - -private: - int cache_size; - - int cache_data_left() const; - mutable Error last_error; - -protected: - Error set_error(Error p_error) const; - - mutable struct File { - - bool open; - int size; - int offset; - String name; - int access_flags; - } file; - - mutable struct Cache { - - Vector<uint8_t> buffer; - int offset; - } cache; - - virtual int read_data_block(int p_offset, int p_size, uint8_t *p_dest = 0) const = 0; - - void set_cache_size(int p_size); - int get_cache_size(); - -public: - virtual size_t get_position() const; ///< get position in the file - virtual size_t get_len() const; ///< get size of the file - - virtual void seek(size_t p_position); ///< seek to a given position - virtual void seek_end(int64_t p_position = 0); ///< seek from the end of file - - virtual bool eof_reached() const; - - virtual uint8_t get_8() const; - virtual int get_buffer(uint8_t *p_dest, int p_length) const; ///< get an array of bytes - - virtual bool is_open() const; - - virtual Error get_error() const; - - FileAccessBuffered(); - virtual ~FileAccessBuffered(); -}; - -#endif diff --git a/core/io/file_access_buffered_fa.h b/core/io/file_access_buffered_fa.h deleted file mode 100644 index 6ec77d503b..0000000000 --- a/core/io/file_access_buffered_fa.h +++ /dev/null @@ -1,159 +0,0 @@ -/*************************************************************************/ -/* file_access_buffered_fa.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#ifndef FILE_ACCESS_BUFFERED_FA_H -#define FILE_ACCESS_BUFFERED_FA_H - -#include "core/io/file_access_buffered.h" - -template <class T> -class FileAccessBufferedFA : public FileAccessBuffered { - - T f; - - int read_data_block(int p_offset, int p_size, uint8_t *p_dest = 0) const { - - ERR_FAIL_COND_V_MSG(!f.is_open(), -1, "Can't read data block when file is not opened."); - - ((T *)&f)->seek(p_offset); - - if (p_dest) { - - f.get_buffer(p_dest, p_size); - return p_size; - - } else { - - cache.offset = p_offset; - cache.buffer.resize(p_size); - - // on Vector - //uint8_t* write = cache.buffer.ptrw(); - //f.get_buffer(write.ptrw(), p_size); - - // on vector - f.get_buffer(cache.buffer.ptrw(), p_size); - - return p_size; - }; - }; - - static FileAccess *create() { - - return memnew(FileAccessBufferedFA<T>()); - }; - -protected: - virtual void _set_access_type(AccessType p_access) { - f._set_access_type(p_access); - FileAccessBuffered::_set_access_type(p_access); - }; - -public: - void flush() { - - f.flush(); - }; - - void store_8(uint8_t p_dest) { - - f.store_8(p_dest); - }; - - void store_buffer(const uint8_t *p_src, int p_length) { - - f.store_buffer(p_src, p_length); - }; - - bool file_exists(const String &p_name) { - - return f.file_exists(p_name); - }; - - Error _open(const String &p_path, int p_mode_flags) { - - close(); - - Error ret = f._open(p_path, p_mode_flags); - if (ret != OK) - return ret; - //ERR_FAIL_COND_V( ret != OK, ret ); - - file.size = f.get_len(); - file.offset = 0; - file.open = true; - file.name = p_path; - file.access_flags = p_mode_flags; - - cache.buffer.resize(0); - cache.offset = 0; - - return set_error(OK); - }; - - void close() { - - f.close(); - - file.offset = 0; - file.size = 0; - file.open = false; - file.name = ""; - - cache.buffer.resize(0); - cache.offset = 0; - set_error(OK); - }; - - /* - static void make_default() { - FileAccess::create_func = FileAccessBufferedFA<T>::create; - }; - */ - - virtual uint64_t _get_modified_time(const String &p_file) { - - return f._get_modified_time(p_file); - } - - virtual uint32_t _get_unix_permissions(const String &p_file) { - return f._get_unix_permissions(p_file); - } - - virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) { - return f._set_unix_permissions(p_file, p_permissions); - } - - FileAccessBufferedFA(){ - - }; -}; - -#endif // FILE_ACCESS_BUFFERED_FA_H diff --git a/core/io/file_access_compressed.cpp b/core/io/file_access_compressed.cpp index c76142d22d..9ec2b27e88 100644 --- a/core/io/file_access_compressed.cpp +++ b/core/io/file_access_compressed.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,16 +30,16 @@ #include "file_access_compressed.h" -#include "core/print_string.h" +#include "core/string/print_string.h" void FileAccessCompressed::configure(const String &p_magic, Compression::Mode p_mode, int p_block_size) { - magic = p_magic.ascii().get_data(); - if (magic.length() > 4) + if (magic.length() > 4) { magic = magic.substr(0, 4); - else { - while (magic.length() < 4) + } else { + while (magic.length() < 4) { magic += " "; + } } cmode = p_mode; @@ -59,7 +59,6 @@ void FileAccessCompressed::configure(const String &p_magic, Compression::Mode p_ } Error FileAccessCompressed::open_after_magic(FileAccess *p_base) { - f = p_base; cmode = (Compression::Mode)f->get_32(); block_size = f->get_32(); @@ -72,7 +71,6 @@ Error FileAccessCompressed::open_after_magic(FileAccess *p_base) { int acc_ofs = f->get_position() + bc * 4; int max_bs = 0; for (int i = 0; i < bc; i++) { - ReadBlock rb; rb.offset = acc_ofs; rb.csize = f->get_32(); @@ -98,11 +96,11 @@ Error FileAccessCompressed::open_after_magic(FileAccess *p_base) { } Error FileAccessCompressed::_open(const String &p_path, int p_mode_flags) { - ERR_FAIL_COND_V(p_mode_flags == READ_WRITE, ERR_UNAVAILABLE); - if (f) + if (f) { close(); + } Error err; f = FileAccess::open(p_path, p_mode_flags, &err); @@ -114,7 +112,6 @@ Error FileAccessCompressed::_open(const String &p_path, int p_mode_flags) { } if (p_mode_flags & WRITE) { - buffer.clear(); writing = true; write_pos = 0; @@ -125,7 +122,6 @@ Error FileAccessCompressed::_open(const String &p_path, int p_mode_flags) { //don't store anything else unless it's done saving! } else { - char rmagic[5]; f->get_buffer((uint8_t *)rmagic, 4); rmagic[4] = 0; @@ -138,10 +134,11 @@ Error FileAccessCompressed::_open(const String &p_path, int p_mode_flags) { return OK; } -void FileAccessCompressed::close() { - if (!f) +void FileAccessCompressed::close() { + if (!f) { return; + } if (writing) { //save block table and all compressed blocks @@ -159,7 +156,6 @@ void FileAccessCompressed::close() { Vector<int> block_sizes; for (int i = 0; i < bc; i++) { - int bl = i == (bc - 1) ? write_max % block_size : block_size; uint8_t *bp = &write_ptr[i * block_size]; @@ -172,15 +168,15 @@ void FileAccessCompressed::close() { } f->seek(16); //ok write block sizes - for (int i = 0; i < bc; i++) + for (int i = 0; i < bc; i++) { f->store_32(block_sizes[i]); + } f->seek_end(); f->store_buffer((const uint8_t *)mgc.get_data(), mgc.length()); //magic at the end too buffer.clear(); } else { - comp_buffer.clear(); buffer.clear(); read_blocks.clear(); @@ -191,21 +187,17 @@ void FileAccessCompressed::close() { } bool FileAccessCompressed::is_open() const { - return f != nullptr; } void FileAccessCompressed::seek(size_t p_position) { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); if (writing) { - ERR_FAIL_COND(p_position > write_max); write_pos = p_position; } else { - ERR_FAIL_COND(p_position > read_total); if (p_position == read_total) { at_end = true; @@ -214,7 +206,6 @@ void FileAccessCompressed::seek(size_t p_position) { read_eof = false; int block_idx = p_position / block_size; if (block_idx != read_block) { - read_block = block_idx; f->seek(read_blocks[read_block].offset); f->get_buffer(comp_buffer.ptrw(), read_blocks[read_block].csize); @@ -228,32 +219,26 @@ void FileAccessCompressed::seek(size_t p_position) { } void FileAccessCompressed::seek_end(int64_t p_position) { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); if (writing) { - seek(write_max + p_position); } else { - seek(read_total + p_position); } } -size_t FileAccessCompressed::get_position() const { +size_t FileAccessCompressed::get_position() const { ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); if (writing) { - return write_pos; } else { - return read_block * block_size + read_pos; } } -size_t FileAccessCompressed::get_len() const { +size_t FileAccessCompressed::get_len() const { ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); if (writing) { - return write_max; } else { return read_total; @@ -261,7 +246,6 @@ size_t FileAccessCompressed::get_len() const { } bool FileAccessCompressed::eof_reached() const { - ERR_FAIL_COND_V_MSG(!f, false, "File must be opened before use."); if (writing) { return false; @@ -271,7 +255,6 @@ bool FileAccessCompressed::eof_reached() const { } uint8_t FileAccessCompressed::get_8() const { - ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); @@ -301,8 +284,8 @@ uint8_t FileAccessCompressed::get_8() const { return ret; } -int FileAccessCompressed::get_buffer(uint8_t *p_dst, int p_length) const { +int FileAccessCompressed::get_buffer(uint8_t *p_dst, int p_length) const { ERR_FAIL_COND_V_MSG(!f, 0, "File must be opened before use."); ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); @@ -312,7 +295,6 @@ int FileAccessCompressed::get_buffer(uint8_t *p_dst, int p_length) const { } for (int i = 0; i < p_length; i++) { - p_dst[i] = read_ptr[read_pos]; read_pos++; if (read_pos >= read_block_size) { @@ -328,8 +310,9 @@ int FileAccessCompressed::get_buffer(uint8_t *p_dst, int p_length) const { } else { read_block--; at_end = true; - if (i < p_length - 1) + if (i < p_length - 1) { read_eof = true; + } return i; } } @@ -339,46 +322,45 @@ int FileAccessCompressed::get_buffer(uint8_t *p_dst, int p_length) const { } Error FileAccessCompressed::get_error() const { - return read_eof ? ERR_FILE_EOF : OK; } void FileAccessCompressed::flush() { ERR_FAIL_COND_MSG(!f, "File must be opened before use."); - ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); // compressed files keep data in memory till close() } void FileAccessCompressed::store_8(uint8_t p_dest) { - ERR_FAIL_COND_MSG(!f, "File must be opened before use."); - ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); WRITE_FIT(1); write_ptr[write_pos++] = p_dest; } bool FileAccessCompressed::file_exists(const String &p_name) { - FileAccess *fa = FileAccess::open(p_name, FileAccess::READ); - if (!fa) + if (!fa) { return false; + } memdelete(fa); return true; } uint64_t FileAccessCompressed::_get_modified_time(const String &p_file) { - - if (f) + if (f) { return f->get_modified_time(p_file); - else + } else { return 0; + } } uint32_t FileAccessCompressed::_get_unix_permissions(const String &p_file) { - if (f) + if (f) { return f->_get_unix_permissions(p_file); + } return 0; } @@ -389,27 +371,8 @@ Error FileAccessCompressed::_set_unix_permissions(const String &p_file, uint32_t return FAILED; } -FileAccessCompressed::FileAccessCompressed() : - cmode(Compression::MODE_ZSTD), - writing(false), - write_ptr(nullptr), - write_buffer_size(0), - write_max(0), - block_size(0), - read_eof(false), - at_end(false), - read_ptr(nullptr), - read_block(0), - read_block_count(0), - read_block_size(0), - read_pos(0), - read_total(0), - magic("GCMP"), - f(nullptr) { -} - FileAccessCompressed::~FileAccessCompressed() { - - if (f) + if (f) { close(); + } } diff --git a/core/io/file_access_compressed.h b/core/io/file_access_compressed.h index 0bb311faa8..118d05ea57 100644 --- a/core/io/file_access_compressed.h +++ b/core/io/file_access_compressed.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -35,16 +35,15 @@ #include "core/os/file_access.h" class FileAccessCompressed : public FileAccess { - - Compression::Mode cmode; - bool writing; - uint32_t write_pos; - uint8_t *write_ptr; - uint32_t write_buffer_size; - uint32_t write_max; - uint32_t block_size; - mutable bool read_eof; - mutable bool at_end; + Compression::Mode cmode = Compression::MODE_ZSTD; + bool writing = false; + uint32_t write_pos = 0; + uint8_t *write_ptr = nullptr; + uint32_t write_buffer_size = 0; + uint32_t write_max = 0; + uint32_t block_size = 0; + mutable bool read_eof = false; + mutable bool at_end = false; struct ReadBlock { int csize; @@ -52,17 +51,17 @@ class FileAccessCompressed : public FileAccess { }; mutable Vector<uint8_t> comp_buffer; - uint8_t *read_ptr; - mutable int read_block; - int read_block_count; - mutable int read_block_size; - mutable int read_pos; + uint8_t *read_ptr = nullptr; + mutable int read_block = 0; + int read_block_count = 0; + mutable int read_block_size = 0; + mutable int read_pos = 0; Vector<ReadBlock> read_blocks; - uint32_t read_total; + uint32_t read_total = 0; - String magic; + String magic = "GCMP"; mutable Vector<uint8_t> buffer; - FileAccess *f; + FileAccess *f = nullptr; public: void configure(const String &p_magic, Compression::Mode p_mode = Compression::MODE_ZSTD, int p_block_size = 4096); @@ -94,7 +93,7 @@ public: virtual uint32_t _get_unix_permissions(const String &p_file); virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions); - FileAccessCompressed(); + FileAccessCompressed() {} virtual ~FileAccessCompressed(); }; diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index a5b3807789..8b4c57ce64 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -32,61 +32,59 @@ #include "core/crypto/crypto_core.h" #include "core/os/copymem.h" -#include "core/print_string.h" -#include "core/variant.h" +#include "core/string/print_string.h" +#include "core/variant/variant.h" #include <stdio.h> -#define COMP_MAGIC 0x43454447 - -Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode) { - +Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode, bool p_with_magic) { ERR_FAIL_COND_V_MSG(file != nullptr, ERR_ALREADY_IN_USE, "Can't open file while another file from path '" + file->get_path_absolute() + "' is open."); ERR_FAIL_COND_V(p_key.size() != 32, ERR_INVALID_PARAMETER); pos = 0; eofed = false; + use_magic = p_with_magic; if (p_mode == MODE_WRITE_AES256) { - data.clear(); writing = true; file = p_base; - mode = p_mode; key = p_key; } else if (p_mode == MODE_READ) { - writing = false; key = p_key; - uint32_t magic = p_base->get_32(); - ERR_FAIL_COND_V(magic != COMP_MAGIC, ERR_FILE_UNRECOGNIZED); - mode = Mode(p_base->get_32()); - ERR_FAIL_INDEX_V(mode, MODE_MAX, ERR_FILE_CORRUPT); - ERR_FAIL_COND_V(mode == 0, ERR_FILE_CORRUPT); + if (use_magic) { + uint32_t magic = p_base->get_32(); + ERR_FAIL_COND_V(magic != ENCRYPTED_HEADER_MAGIC, ERR_FILE_UNRECOGNIZED); + } unsigned char md5d[16]; p_base->get_buffer(md5d, 16); length = p_base->get_64(); + + unsigned char iv[16]; + for (int i = 0; i < 16; i++) { + iv[i] = p_base->get_8(); + } + base = p_base->get_position(); ERR_FAIL_COND_V(p_base->get_len() < base + length, ERR_FILE_CORRUPT); uint32_t ds = length; if (ds % 16) { ds += 16 - (ds % 16); } - data.resize(ds); uint32_t blen = p_base->get_buffer(data.ptrw(), ds); ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT); - CryptoCore::AESContext ctx; - ctx.set_decode_key(key.ptrw(), 256); + { + CryptoCore::AESContext ctx; - for (size_t i = 0; i < ds; i += 16) { - - ctx.decrypt_ecb(&data.write[i], &data.write[i]); + ctx.set_encode_key(key.ptrw(), 256); // Due to the nature of CFB, same key schedule is used for both encryption and decryption! + ctx.decrypt_cfb(ds, iv, data.ptrw(), data.ptrw()); } data.resize(length); @@ -103,13 +101,11 @@ Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8 } Error FileAccessEncrypted::open_and_parse_password(FileAccess *p_base, const String &p_key, Mode p_mode) { - String cs = p_key.md5_text(); ERR_FAIL_COND_V(cs.length() != 32, ERR_INVALID_PARAMETER); Vector<uint8_t> key; key.resize(32); for (int i = 0; i < 32; i++) { - key.write[i] = cs[i]; } @@ -117,16 +113,34 @@ Error FileAccessEncrypted::open_and_parse_password(FileAccess *p_base, const Str } Error FileAccessEncrypted::_open(const String &p_path, int p_mode_flags) { - return OK; } + void FileAccessEncrypted::close() { + if (!file) { + return; + } + + _release(); + + file->close(); + memdelete(file); - if (!file) + file = nullptr; +} + +void FileAccessEncrypted::release() { + if (!file) { return; + } - if (writing) { + _release(); + file = nullptr; +} + +void FileAccessEncrypted::_release() { + if (writing) { Vector<uint8_t> compressed; size_t len = data.size(); if (len % 16) { @@ -145,82 +159,72 @@ void FileAccessEncrypted::close() { CryptoCore::AESContext ctx; ctx.set_encode_key(key.ptrw(), 256); - for (size_t i = 0; i < len; i += 16) { - - ctx.encrypt_ecb(&compressed.write[i], &compressed.write[i]); + if (use_magic) { + file->store_32(ENCRYPTED_HEADER_MAGIC); } - file->store_32(COMP_MAGIC); - file->store_32(mode); - file->store_buffer(hash, 16); file->store_64(data.size()); - file->store_buffer(compressed.ptr(), compressed.size()); - file->close(); - memdelete(file); - file = nullptr; - data.clear(); + unsigned char iv[16]; + for (int i = 0; i < 16; i++) { + iv[i] = Math::rand() % 256; + file->store_8(iv[i]); + } - } else { + ctx.encrypt_cfb(len, iv, compressed.ptrw(), compressed.ptrw()); - file->close(); - memdelete(file); + file->store_buffer(compressed.ptr(), compressed.size()); data.clear(); - file = nullptr; } } bool FileAccessEncrypted::is_open() const { - return file != nullptr; } String FileAccessEncrypted::get_path() const { - - if (file) + if (file) { return file->get_path(); - else + } else { return ""; + } } String FileAccessEncrypted::get_path_absolute() const { - - if (file) + if (file) { return file->get_path_absolute(); - else + } else { return ""; + } } void FileAccessEncrypted::seek(size_t p_position) { - - if (p_position > (size_t)data.size()) + if (p_position > (size_t)data.size()) { p_position = data.size(); + } pos = p_position; eofed = false; } void FileAccessEncrypted::seek_end(int64_t p_position) { - seek(data.size() + p_position); } -size_t FileAccessEncrypted::get_position() const { +size_t FileAccessEncrypted::get_position() const { return pos; } -size_t FileAccessEncrypted::get_len() const { +size_t FileAccessEncrypted::get_len() const { return data.size(); } bool FileAccessEncrypted::eof_reached() const { - return eofed; } uint8_t FileAccessEncrypted::get_8() const { - ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); if (pos >= data.size()) { eofed = true; @@ -231,13 +235,12 @@ uint8_t FileAccessEncrypted::get_8() const { pos++; return b; } -int FileAccessEncrypted::get_buffer(uint8_t *p_dst, int p_length) const { +int FileAccessEncrypted::get_buffer(uint8_t *p_dst, int p_length) const { ERR_FAIL_COND_V_MSG(writing, 0, "File has not been opened in read mode."); int to_copy = MIN(p_length, data.size() - pos); for (int i = 0; i < to_copy; i++) { - p_dst[i] = data[pos++]; } @@ -249,25 +252,19 @@ int FileAccessEncrypted::get_buffer(uint8_t *p_dst, int p_length) const { } Error FileAccessEncrypted::get_error() const { - return eofed ? ERR_FILE_EOF : OK; } void FileAccessEncrypted::store_buffer(const uint8_t *p_src, int p_length) { - - ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); if (pos < data.size()) { - for (int i = 0; i < p_length; i++) { - store_8(p_src[i]); } } else if (pos == data.size()) { - data.resize(pos + p_length); for (int i = 0; i < p_length; i++) { - data.write[pos + i] = p_src[i]; } pos += p_length; @@ -275,14 +272,13 @@ void FileAccessEncrypted::store_buffer(const uint8_t *p_src, int p_length) { } void FileAccessEncrypted::flush() { - ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); // encrypted files keep data in memory till close() } void FileAccessEncrypted::store_8(uint8_t p_dest) { - - ERR_FAIL_COND_MSG(!writing, "File has not been opened in read mode."); + ERR_FAIL_COND_MSG(!writing, "File has not been opened in write mode."); if (pos < data.size()) { data.write[pos] = p_dest; @@ -294,21 +290,19 @@ void FileAccessEncrypted::store_8(uint8_t p_dest) { } bool FileAccessEncrypted::file_exists(const String &p_name) { - FileAccess *fa = FileAccess::open(p_name, FileAccess::READ); - if (!fa) + if (!fa) { return false; + } memdelete(fa); return true; } uint64_t FileAccessEncrypted::_get_modified_time(const String &p_file) { - return 0; } uint32_t FileAccessEncrypted::_get_unix_permissions(const String &p_file) { - return 0; } @@ -317,17 +311,8 @@ Error FileAccessEncrypted::_set_unix_permissions(const String &p_file, uint32_t return ERR_UNAVAILABLE; } -FileAccessEncrypted::FileAccessEncrypted() { - - file = nullptr; - pos = 0; - eofed = false; - mode = MODE_MAX; - writing = false; -} - FileAccessEncrypted::~FileAccessEncrypted() { - - if (file) + if (file) { close(); + } } diff --git a/core/io/file_access_encrypted.h b/core/io/file_access_encrypted.h index 7a9f4ecdd8..969052d04f 100644 --- a/core/io/file_access_encrypted.h +++ b/core/io/file_access_encrypted.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,6 +33,8 @@ #include "core/os/file_access.h" +#define ENCRYPTED_HEADER_MAGIC 0x43454447 + class FileAccessEncrypted : public FileAccess { public: enum Mode { @@ -42,22 +44,25 @@ public: }; private: - Mode mode; Vector<uint8_t> key; - bool writing; - FileAccess *file; - size_t base; - size_t length; + bool writing = false; + FileAccess *file = nullptr; + size_t base = 0; + size_t length = 0; Vector<uint8_t> data; - mutable int pos; - mutable bool eofed; + mutable int pos = 0; + mutable bool eofed = false; + bool use_magic = true; + + void _release(); public: - Error open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode); + Error open_and_parse(FileAccess *p_base, const Vector<uint8_t> &p_key, Mode p_mode, bool p_with_magic = true); Error open_and_parse_password(FileAccess *p_base, const String &p_key, Mode p_mode); virtual Error _open(const String &p_path, int p_mode_flags); ///< open a file virtual void close(); ///< close a file + virtual void release(); ///< finish and keep base file open virtual bool is_open() const; ///< true when file is open virtual String get_path() const; /// returns the path for the current open file @@ -85,7 +90,7 @@ public: virtual uint32_t _get_unix_permissions(const String &p_file); virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions); - FileAccessEncrypted(); + FileAccessEncrypted() {} ~FileAccessEncrypted(); }; diff --git a/core/io/file_access_memory.cpp b/core/io/file_access_memory.cpp index a2379ce88f..04270de77f 100644 --- a/core/io/file_access_memory.cpp +++ b/core/io/file_access_memory.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,44 +30,42 @@ #include "file_access_memory.h" -#include "core/map.h" +#include "core/config/project_settings.h" #include "core/os/copymem.h" #include "core/os/dir_access.h" -#include "core/project_settings.h" +#include "core/templates/map.h" static Map<String, Vector<uint8_t>> *files = nullptr; void FileAccessMemory::register_file(String p_name, Vector<uint8_t> p_data) { - if (!files) { files = memnew((Map<String, Vector<uint8_t>>)); } String name; - if (ProjectSettings::get_singleton()) + if (ProjectSettings::get_singleton()) { name = ProjectSettings::get_singleton()->globalize_path(p_name); - else + } else { name = p_name; + } //name = DirAccess::normalize_path(name); (*files)[name] = p_data; } void FileAccessMemory::cleanup() { - - if (!files) + if (!files) { return; + } memdelete(files); } FileAccess *FileAccessMemory::create() { - return memnew(FileAccessMemory); } bool FileAccessMemory::file_exists(const String &p_name) { - String name = fix_path(p_name); //name = DirAccess::normalize_path(name); @@ -75,7 +73,6 @@ bool FileAccessMemory::file_exists(const String &p_name) { } Error FileAccessMemory::open_custom(const uint8_t *p_data, int p_len) { - data = (uint8_t *)p_data; length = p_len; pos = 0; @@ -83,7 +80,6 @@ Error FileAccessMemory::open_custom(const uint8_t *p_data, int p_len) { } Error FileAccessMemory::_open(const String &p_path, int p_mode_flags) { - ERR_FAIL_COND_V(!files, ERR_FILE_NOT_FOUND); String name = fix_path(p_path); @@ -100,46 +96,38 @@ Error FileAccessMemory::_open(const String &p_path, int p_mode_flags) { } void FileAccessMemory::close() { - data = nullptr; } bool FileAccessMemory::is_open() const { - return data != nullptr; } void FileAccessMemory::seek(size_t p_position) { - ERR_FAIL_COND(!data); pos = p_position; } void FileAccessMemory::seek_end(int64_t p_position) { - ERR_FAIL_COND(!data); pos = length + p_position; } size_t FileAccessMemory::get_position() const { - ERR_FAIL_COND_V(!data, 0); return pos; } size_t FileAccessMemory::get_len() const { - ERR_FAIL_COND_V(!data, 0); return length; } bool FileAccessMemory::eof_reached() const { - return pos > length; } uint8_t FileAccessMemory::get_8() const { - uint8_t ret = 0; if (pos < length) { ret = data[pos]; @@ -150,7 +138,6 @@ uint8_t FileAccessMemory::get_8() const { } int FileAccessMemory::get_buffer(uint8_t *p_dst, int p_length) const { - ERR_FAIL_COND_V(!data, -1); int left = length - pos; @@ -158,7 +145,7 @@ int FileAccessMemory::get_buffer(uint8_t *p_dst, int p_length) const { if (read < p_length) { WARN_PRINT("Reading less data than requested"); - }; + } copymem(p_dst, &data[pos], read); pos += p_length; @@ -167,7 +154,6 @@ int FileAccessMemory::get_buffer(uint8_t *p_dst, int p_length) const { } Error FileAccessMemory::get_error() const { - return pos >= length ? ERR_FILE_EOF : OK; } @@ -176,14 +162,12 @@ void FileAccessMemory::flush() { } void FileAccessMemory::store_8(uint8_t p_byte) { - ERR_FAIL_COND(!data); ERR_FAIL_COND(pos >= length); data[pos++] = p_byte; } void FileAccessMemory::store_buffer(const uint8_t *p_src, int p_length) { - int left = length - pos; int write = MIN(p_length, left); if (write < p_length) { @@ -193,8 +177,3 @@ void FileAccessMemory::store_buffer(const uint8_t *p_src, int p_length) { copymem(&data[pos], p_src, write); pos += p_length; } - -FileAccessMemory::FileAccessMemory() { - - data = nullptr; -} diff --git a/core/io/file_access_memory.h b/core/io/file_access_memory.h index 2db14db265..0e3b0ad7b1 100644 --- a/core/io/file_access_memory.h +++ b/core/io/file_access_memory.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -34,10 +34,9 @@ #include "core/os/file_access.h" class FileAccessMemory : public FileAccess { - - uint8_t *data; - int length; - mutable int pos; + uint8_t *data = nullptr; + int length = 0; + mutable int pos = 0; static FileAccess *create(); @@ -73,7 +72,7 @@ public: virtual uint32_t _get_unix_permissions(const String &p_file) { return 0; } virtual Error _set_unix_permissions(const String &p_file, uint32_t p_permissions) { return FAILED; } - FileAccessMemory(); + FileAccessMemory() {} }; #endif // FILE_ACCESS_MEMORY_H diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index a3f307393f..1d9aa846eb 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,10 +30,10 @@ #include "file_access_network.h" +#include "core/config/project_settings.h" #include "core/io/ip.h" #include "core/io/marshalls.h" #include "core/os/os.h" -#include "core/project_settings.h" //#define DEBUG_PRINT(m_p) print_line(m_p) //#define DEBUG_TIME(m_what) printf("MS: %s - %lli\n",m_what,OS::get_singleton()->get_ticks_usec()); @@ -41,19 +41,16 @@ #define DEBUG_TIME(m_what) void FileAccessNetworkClient::lock_mutex() { - mutex.lock(); lockcount++; } void FileAccessNetworkClient::unlock_mutex() { - lockcount--; mutex.unlock(); } void FileAccessNetworkClient::put_32(int p_32) { - uint8_t buf[4]; encode_uint32(p_32, buf); client->put_data(buf, 4); @@ -61,7 +58,6 @@ void FileAccessNetworkClient::put_32(int p_32) { } void FileAccessNetworkClient::put_64(int64_t p_64) { - uint8_t buf[8]; encode_uint64(p_64, buf); client->put_data(buf, 8); @@ -69,24 +65,20 @@ void FileAccessNetworkClient::put_64(int64_t p_64) { } int FileAccessNetworkClient::get_32() { - uint8_t buf[4]; client->get_data(buf, 4); return decode_uint32(buf); } int64_t FileAccessNetworkClient::get_64() { - uint8_t buf[8]; client->get_data(buf, 8); return decode_uint64(buf); } void FileAccessNetworkClient::_thread_func() { - client->set_no_delay(true); while (!quit) { - DEBUG_PRINT("SEM WAIT - " + itos(sem->get())); sem.wait(); DEBUG_TIME("sem_unlock"); @@ -123,13 +115,12 @@ void FileAccessNetworkClient::_thread_func() { } } - if (accesses.has(id)) + if (accesses.has(id)) { fa = accesses[id]; + } switch (response) { - case FileAccessNetwork::RESPONSE_OPEN: { - DEBUG_TIME("sem_open"); int status = get_32(); if (status != OK) { @@ -143,7 +134,6 @@ void FileAccessNetworkClient::_thread_func() { } break; case FileAccessNetwork::RESPONSE_DATA: { - int64_t offset = get_64(); uint32_t len = get_32(); @@ -151,19 +141,18 @@ void FileAccessNetworkClient::_thread_func() { block.resize(len); client->get_data(block.ptrw(), len); - if (fa) //may have been queued + if (fa) { //may have been queued fa->_set_block(offset, block); + } } break; case FileAccessNetwork::RESPONSE_FILE_EXISTS: { - int status = get_32(); fa->exists_modtime = status != 0; fa->sem.post(); } break; case FileAccessNetwork::RESPONSE_GET_MODTIME: { - uint64_t status = get_64(); fa->exists_modtime = status; fa->sem.post(); @@ -176,14 +165,12 @@ void FileAccessNetworkClient::_thread_func() { } void FileAccessNetworkClient::_thread_func(void *s) { - FileAccessNetworkClient *self = (FileAccessNetworkClient *)s; self->_thread_func(); } Error FileAccessNetworkClient::connect(const String &p_host, int p_port, const String &p_password) { - IP_Address ip; if (p_host.is_valid_ip_address()) { @@ -222,17 +209,11 @@ Error FileAccessNetworkClient::connect(const String &p_host, int p_port, const S FileAccessNetworkClient *FileAccessNetworkClient::singleton = nullptr; FileAccessNetworkClient::FileAccessNetworkClient() { - - thread = nullptr; - quit = false; singleton = this; - last_id = 0; client.instance(); - lockcount = 0; } FileAccessNetworkClient::~FileAccessNetworkClient() { - if (thread) { quit = true; sem.post(); @@ -242,7 +223,6 @@ FileAccessNetworkClient::~FileAccessNetworkClient() { } void FileAccessNetwork::_set_block(int p_offset, const Vector<uint8_t> &p_block) { - int page = p_offset / page_size; ERR_FAIL_INDEX(page, pages.size()); if (page < pages.size() - 1) { @@ -264,11 +244,11 @@ void FileAccessNetwork::_set_block(int p_offset, const Vector<uint8_t> &p_block) } void FileAccessNetwork::_respond(size_t p_len, Error p_status) { - DEBUG_PRINT("GOT RESPONSE - len: " + itos(p_len) + " status: " + itos(p_status)); response = p_status; - if (response != OK) + if (response != OK) { return; + } opened = true; total_size = p_len; int pc = ((total_size - 1) / page_size) + 1; @@ -276,10 +256,10 @@ void FileAccessNetwork::_respond(size_t p_len, Error p_status) { } Error FileAccessNetwork::_open(const String &p_path, int p_mode_flags) { - ERR_FAIL_COND_V(p_mode_flags != READ, ERR_UNAVAILABLE); - if (opened) + if (opened) { close(); + } FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; DEBUG_PRINT("open: " + p_path); @@ -311,9 +291,9 @@ Error FileAccessNetwork::_open(const String &p_path, int p_mode_flags) { } void FileAccessNetwork::close() { - - if (!opened) + if (!opened) { return; + } FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; @@ -325,13 +305,12 @@ void FileAccessNetwork::close() { opened = false; nc->unlock_mutex(); } -bool FileAccessNetwork::is_open() const { +bool FileAccessNetwork::is_open() const { return opened; } void FileAccessNetwork::seek(size_t p_position) { - ERR_FAIL_COND_MSG(!opened, "File must be opened before use."); eof_flag = p_position > total_size; @@ -343,39 +322,35 @@ void FileAccessNetwork::seek(size_t p_position) { } void FileAccessNetwork::seek_end(int64_t p_position) { - seek(total_size + p_position); } -size_t FileAccessNetwork::get_position() const { +size_t FileAccessNetwork::get_position() const { ERR_FAIL_COND_V_MSG(!opened, 0, "File must be opened before use."); return pos; } -size_t FileAccessNetwork::get_len() const { +size_t FileAccessNetwork::get_len() const { ERR_FAIL_COND_V_MSG(!opened, 0, "File must be opened before use."); return total_size; } bool FileAccessNetwork::eof_reached() const { - ERR_FAIL_COND_V_MSG(!opened, false, "File must be opened before use."); return eof_flag; } uint8_t FileAccessNetwork::get_8() const { - uint8_t v; get_buffer(&v, 1); return v; } void FileAccessNetwork::_queue_page(int p_page) const { - - if (p_page >= pages.size()) + if (p_page >= pages.size()) { return; - if (pages[p_page].buffer.empty() && !pages[p_page].queued) { - + } + if (pages[p_page].buffer.is_empty() && !pages[p_page].queued) { FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; { MutexLock lock(nc->blockrequest_mutex); @@ -394,7 +369,6 @@ void FileAccessNetwork::_queue_page(int p_page) const { } int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const { - //bool eof=false; if (pos + p_length > total_size) { eof_flag = true; @@ -408,15 +382,13 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const { uint8_t *buff = last_page_buff; for (int i = 0; i < p_length; i++) { - int page = pos / page_size; if (page != last_page) { buffer_mutex.lock(); - if (pages[page].buffer.empty()) { + if (pages[page].buffer.is_empty()) { waiting_on_page = page; for (int j = 0; j < read_ahead; j++) { - _queue_page(page + j); } buffer_mutex.unlock(); @@ -424,9 +396,7 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const { page_sem.wait(); DEBUG_PRINT("done"); } else { - for (int j = 0; j < read_ahead; j++) { - _queue_page(page + j); } //queue pages @@ -446,7 +416,6 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const { } Error FileAccessNetwork::get_error() const { - return pos == total_size ? ERR_FILE_EOF : OK; } @@ -455,12 +424,10 @@ void FileAccessNetwork::flush() { } void FileAccessNetwork::store_8(uint8_t p_dest) { - ERR_FAIL(); } bool FileAccessNetwork::file_exists(const String &p_path) { - FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; nc->lock_mutex(); nc->put_32(id); @@ -477,7 +444,6 @@ bool FileAccessNetwork::file_exists(const String &p_path) { } uint64_t FileAccessNetwork::_get_modified_time(const String &p_file) { - FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; nc->lock_mutex(); nc->put_32(id); @@ -504,7 +470,6 @@ Error FileAccessNetwork::_set_unix_permissions(const String &p_file, uint32_t p_ } void FileAccessNetwork::configure() { - GLOBAL_DEF("network/remote_fs/page_size", 65536); ProjectSettings::get_singleton()->set_custom_property_info("network/remote_fs/page_size", PropertyInfo(Variant::INT, "network/remote_fs/page_size", PROPERTY_HINT_RANGE, "1,65536,1,or_greater")); //is used as denominator and can't be zero GLOBAL_DEF("network/remote_fs/page_read_ahead", 4); @@ -512,10 +477,6 @@ void FileAccessNetwork::configure() { } FileAccessNetwork::FileAccessNetwork() { - - eof_flag = false; - opened = false; - pos = 0; FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; nc->lock_mutex(); id = nc->last_id++; @@ -523,13 +484,9 @@ FileAccessNetwork::FileAccessNetwork() { nc->unlock_mutex(); page_size = GLOBAL_GET("network/remote_fs/page_size"); read_ahead = GLOBAL_GET("network/remote_fs/page_read_ahead"); - last_activity_val = 0; - waiting_on_page = -1; - last_page = -1; } FileAccessNetwork::~FileAccessNetwork() { - close(); FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; diff --git a/core/io/file_access_network.h b/core/io/file_access_network.h index 7f664b46f7..6aec2869fc 100644 --- a/core/io/file_access_network.h +++ b/core/io/file_access_network.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -39,9 +39,7 @@ class FileAccessNetwork; class FileAccessNetworkClient { - struct BlockRequest { - int id; uint64_t offset; int size; @@ -50,13 +48,14 @@ class FileAccessNetworkClient { List<BlockRequest> block_requests; Semaphore sem; - Thread *thread; - bool quit; + Thread *thread = nullptr; + bool quit = false; Mutex mutex; Mutex blockrequest_mutex; Map<int, FileAccessNetwork *> accesses; Ref<StreamPeerTCP> client; - int last_id; + int last_id = 0; + int lockcount = 0; Vector<uint8_t> block; @@ -67,7 +66,6 @@ class FileAccessNetworkClient { void put_64(int64_t p_64); int get_32(); int64_t get_64(); - int lockcount; void lock_mutex(); void unlock_mutex(); @@ -84,31 +82,26 @@ public: }; class FileAccessNetwork : public FileAccess { - Semaphore sem; Semaphore page_sem; Mutex buffer_mutex; - bool opened; + bool opened = false; size_t total_size; - mutable size_t pos; + mutable size_t pos = 0; int id; - mutable bool eof_flag; - mutable int last_page; - mutable uint8_t *last_page_buff; + mutable bool eof_flag = false; + mutable int last_page = -1; + mutable uint8_t *last_page_buff = nullptr; int page_size; int read_ahead; - mutable int waiting_on_page; - mutable int last_activity_val; + mutable int waiting_on_page = -1; + struct Page { - int activity; - bool queued; + int activity = 0; + bool queued = false; Vector<uint8_t> buffer; - Page() { - activity = 0; - queued = false; - } }; mutable Vector<Page> pages; diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp index 0a7dee9444..faf4fca14f 100644 --- a/core/io/file_access_pack.cpp +++ b/core/io/file_access_pack.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,40 +30,41 @@ #include "file_access_pack.h" +#include "core/io/file_access_encrypted.h" +#include "core/object/script_language.h" #include "core/version.h" #include <stdio.h> -Error PackedData::add_pack(const String &p_path, bool p_replace_files) { - +Error PackedData::add_pack(const String &p_path, bool p_replace_files, size_t p_offset) { for (int i = 0; i < sources.size(); i++) { - - if (sources[i]->try_open_pack(p_path, p_replace_files)) { - + if (sources[i]->try_open_pack(p_path, p_replace_files, p_offset)) { return OK; - }; - }; + } + } return ERR_FILE_UNRECOGNIZED; -}; - -void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files) { +} +void PackedData::add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files, bool p_encrypted) { PathMD5 pmd5(path.md5_buffer()); - //printf("adding path %ls, %lli, %lli\n", path.c_str(), pmd5.a, pmd5.b); + //printf("adding path %s, %lli, %lli\n", path.utf8().get_data(), pmd5.a, pmd5.b); bool exists = files.has(pmd5); PackedFile pf; + pf.encrypted = p_encrypted; pf.pack = pkg_path; pf.offset = ofs; pf.size = size; - for (int i = 0; i < 16; i++) + for (int i = 0; i < 16; i++) { pf.md5[i] = p_md5[i]; + } pf.src = p_src; - if (!exists || p_replace_files) + if (!exists || p_replace_files) { files[pmd5] = pf; + } if (!exists) { //search for dir @@ -75,9 +76,7 @@ void PackedData::add_path(const String &pkg_path, const String &path, uint64_t o Vector<String> ds = p.get_base_dir().split("/"); for (int j = 0; j < ds.size(); j++) { - if (!cd->subdirs.has(ds[j])) { - PackedDir *pd = memnew(PackedDir); pd->name = ds[j]; pd->parent = cd; @@ -90,40 +89,35 @@ void PackedData::add_path(const String &pkg_path, const String &path, uint64_t o } String filename = path.get_file(); // Don't add as a file if the path points to a directory - if (!filename.empty()) { + if (!filename.is_empty()) { cd->files.insert(filename); } } } void PackedData::add_pack_source(PackSource *p_source) { - if (p_source != nullptr) { sources.push_back(p_source); } -}; +} PackedData *PackedData::singleton = nullptr; PackedData::PackedData() { - singleton = this; root = memnew(PackedDir); - root->parent = nullptr; - disabled = false; add_pack_source(memnew(PackedSourcePCK)); } void PackedData::_free_packed_dirs(PackedDir *p_dir) { - - for (Map<String, PackedDir *>::Element *E = p_dir->subdirs.front(); E; E = E->next()) + for (Map<String, PackedDir *>::Element *E = p_dir->subdirs.front(); E; E = E->next()) { _free_packed_dirs(E->get()); + } memdelete(p_dir); } PackedData::~PackedData() { - for (int i = 0; i < sources.size(); i++) { memdelete(sources[i]); } @@ -132,21 +126,29 @@ PackedData::~PackedData() { ////////////////////////////////////////////////////////////////// -bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) { - +bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset) { FileAccess *f = FileAccess::open(p_path, FileAccess::READ); - if (!f) + if (!f) { return false; + } + + f->seek(p_offset); uint32_t magic = f->get_32(); if (magic != PACK_HEADER_MAGIC) { + // loading with offset feature not supported for self contained exe files + if (p_offset != 0) { + f->close(); + memdelete(f); + ERR_FAIL_V_MSG(false, "Loading self-contained executable with offset not supported."); + } + //maybe at the end.... self contained exe f->seek_end(); f->seek(f->get_position() - 4); magic = f->get_32(); if (magic != PACK_HEADER_MAGIC) { - f->close(); memdelete(f); return false; @@ -158,7 +160,6 @@ bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) magic = f->get_32(); if (magic != PACK_HEADER_MAGIC) { - f->close(); memdelete(f); return false; @@ -181,6 +182,11 @@ bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) ERR_FAIL_V_MSG(false, "Pack created with a newer version of the engine: " + itos(ver_major) + "." + itos(ver_minor) + "."); } + uint32_t pack_flags = f->get_32(); + uint64_t file_base = f->get_64(); + + bool enc_directory = (pack_flags & PACK_DIR_ENCRYPTED); + for (int i = 0; i < 16; i++) { //reserved f->get_32(); @@ -188,8 +194,31 @@ bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) int file_count = f->get_32(); - for (int i = 0; i < file_count; i++) { + if (enc_directory) { + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + if (!fae) { + f->close(); + memdelete(f); + ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory."); + } + + Vector<uint8_t> key; + key.resize(32); + for (int i = 0; i < key.size(); i++) { + key.write[i] = script_encryption_key[i]; + } + Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false); + if (err) { + f->close(); + memdelete(f); + memdelete(fae); + ERR_FAIL_V_MSG(false, "Can't open encrypted pack directory."); + } + f = fae; + } + + for (int i = 0; i < file_count; i++) { uint32_t sl = f->get_32(); CharString cs; cs.resize(sl + 1); @@ -199,72 +228,67 @@ bool PackedSourcePCK::try_open_pack(const String &p_path, bool p_replace_files) String path; path.parse_utf8(cs.ptr()); - uint64_t ofs = f->get_64(); + uint64_t ofs = file_base + f->get_64(); uint64_t size = f->get_64(); uint8_t md5[16]; f->get_buffer(md5, 16); - PackedData::get_singleton()->add_path(p_path, path, ofs, size, md5, this, p_replace_files); - }; + uint32_t flags = f->get_32(); + + PackedData::get_singleton()->add_path(p_path, path, ofs + p_offset, size, md5, this, p_replace_files, (flags & PACK_FILE_ENCRYPTED)); + } f->close(); memdelete(f); return true; -}; +} FileAccess *PackedSourcePCK::get_file(const String &p_path, PackedData::PackedFile *p_file) { - return memnew(FileAccessPack(p_path, *p_file)); -}; +} ////////////////////////////////////////////////////////////////// Error FileAccessPack::_open(const String &p_path, int p_mode_flags) { - ERR_FAIL_V(ERR_UNAVAILABLE); return ERR_UNAVAILABLE; } void FileAccessPack::close() { - f->close(); } bool FileAccessPack::is_open() const { - return f->is_open(); } void FileAccessPack::seek(size_t p_position) { - if (p_position > pf.size) { eof = true; } else { eof = false; } - f->seek(pf.offset + p_position); + f->seek(off + p_position); pos = p_position; } -void FileAccessPack::seek_end(int64_t p_position) { +void FileAccessPack::seek_end(int64_t p_position) { seek(pf.size + p_position); } -size_t FileAccessPack::get_position() const { +size_t FileAccessPack::get_position() const { return pos; } -size_t FileAccessPack::get_len() const { +size_t FileAccessPack::get_len() const { return pf.size; } bool FileAccessPack::eof_reached() const { - return eof; } uint8_t FileAccessPack::get_8() const { - if (pos >= pf.size) { eof = true; return 0; @@ -275,9 +299,9 @@ uint8_t FileAccessPack::get_8() const { } int FileAccessPack::get_buffer(uint8_t *p_dst, int p_length) const { - - if (eof) + if (eof) { return 0; + } uint64_t to_read = p_length; if (to_read + pos > pf.size) { @@ -287,8 +311,9 @@ int FileAccessPack::get_buffer(uint8_t *p_dst, int p_length) const { pos += p_length; - if (to_read <= 0) + if (to_read <= 0) { return 0; + } f->get_buffer(p_dst, to_read); return to_read; @@ -300,46 +325,65 @@ void FileAccessPack::set_endian_swap(bool p_swap) { } Error FileAccessPack::get_error() const { - - if (eof) + if (eof) { return ERR_FILE_EOF; + } return OK; } void FileAccessPack::flush() { - ERR_FAIL(); } void FileAccessPack::store_8(uint8_t p_dest) { - ERR_FAIL(); } void FileAccessPack::store_buffer(const uint8_t *p_src, int p_length) { - ERR_FAIL(); } bool FileAccessPack::file_exists(const String &p_name) { - return false; } FileAccessPack::FileAccessPack(const String &p_path, const PackedData::PackedFile &p_file) : pf(p_file), f(FileAccess::open(pf.pack, FileAccess::READ)) { - ERR_FAIL_COND_MSG(!f, "Can't open pack-referenced file '" + String(pf.pack) + "'."); f->seek(pf.offset); + off = pf.offset; + + if (pf.encrypted) { + FileAccessEncrypted *fae = memnew(FileAccessEncrypted); + if (!fae) { + ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'."); + } + + Vector<uint8_t> key; + key.resize(32); + for (int i = 0; i < key.size(); i++) { + key.write[i] = script_encryption_key[i]; + } + + Error err = fae->open_and_parse(f, key, FileAccessEncrypted::MODE_READ, false); + if (err) { + memdelete(fae); + ERR_FAIL_MSG("Can't open encrypted pack-referenced file '" + String(pf.pack) + "'."); + } + f = fae; + off = 0; + } pos = 0; eof = false; } FileAccessPack::~FileAccessPack() { - if (f) + if (f) { + f->close(); memdelete(f); + } } ////////////////////////////////////////////////////////////////////////////////// @@ -347,17 +391,14 @@ FileAccessPack::~FileAccessPack() { ////////////////////////////////////////////////////////////////////////////////// Error DirAccessPack::list_dir_begin() { - list_dirs.clear(); list_files.clear(); for (Map<String, PackedData::PackedDir *>::Element *E = current->subdirs.front(); E; E = E->next()) { - list_dirs.push_back(E->key()); } for (Set<String>::Element *E = current->files.front(); E; E = E->next()) { - list_files.push_back(E->get()); } @@ -365,7 +406,6 @@ Error DirAccessPack::list_dir_begin() { } String DirAccessPack::get_next() { - if (list_dirs.size()) { cdir = true; String d = list_dirs.front()->get(); @@ -380,32 +420,36 @@ String DirAccessPack::get_next() { return String(); } } -bool DirAccessPack::current_is_dir() const { +bool DirAccessPack::current_is_dir() const { return cdir; } -bool DirAccessPack::current_is_hidden() const { +bool DirAccessPack::current_is_hidden() const { return false; } -void DirAccessPack::list_dir_end() { +void DirAccessPack::list_dir_end() { list_dirs.clear(); list_files.clear(); } int DirAccessPack::get_drive_count() { - return 0; } -String DirAccessPack::get_drive(int p_drive) { +String DirAccessPack::get_drive(int p_drive) { return ""; } -Error DirAccessPack::change_dir(String p_dir) { - +PackedData::PackedDir *DirAccessPack::_find_dir(String p_dir) { String nd = p_dir.replace("\\", "/"); + + // Special handling since simplify_path() will forbid it + if (p_dir == "..") { + return current->parent; + } + bool absolute = false; if (nd.begins_with("res://")) { nd = nd.replace_first("res://", ""); @@ -414,7 +458,9 @@ Error DirAccessPack::change_dir(String p_dir) { nd = nd.simplify_path(); - if (nd == "") nd = "."; + if (nd == "") { + nd = "."; + } if (nd.begins_with("/")) { nd = nd.replace_first("/", ""); @@ -425,13 +471,13 @@ Error DirAccessPack::change_dir(String p_dir) { PackedData::PackedDir *pd; - if (absolute) + if (absolute) { pd = PackedData::get_singleton()->root; - else + } else { pd = current; + } for (int i = 0; i < paths.size(); i++) { - String p = paths[i]; if (p == ".") { continue; @@ -440,22 +486,27 @@ Error DirAccessPack::change_dir(String p_dir) { pd = pd->parent; } } else if (pd->subdirs.has(p)) { - pd = pd->subdirs[p]; } else { - - return ERR_INVALID_PARAMETER; + return nullptr; } } - current = pd; + return pd; +} - return OK; +Error DirAccessPack::change_dir(String p_dir) { + PackedData::PackedDir *pd = _find_dir(p_dir); + if (pd) { + current = pd; + return OK; + } else { + return ERR_INVALID_PARAMETER; + } } String DirAccessPack::get_current_dir(bool p_include_drive) { - PackedData::PackedDir *pd = current; String p = current->name; @@ -468,35 +519,34 @@ String DirAccessPack::get_current_dir(bool p_include_drive) { } bool DirAccessPack::file_exists(String p_file) { - p_file = fix_path(p_file); - return current->files.has(p_file); + PackedData::PackedDir *pd = _find_dir(p_file.get_base_dir()); + if (!pd) { + return false; + } + return pd->files.has(p_file.get_file()); } bool DirAccessPack::dir_exists(String p_dir) { - p_dir = fix_path(p_dir); - return current->subdirs.has(p_dir); + return _find_dir(p_dir) != nullptr; } Error DirAccessPack::make_dir(String p_dir) { - return ERR_UNAVAILABLE; } Error DirAccessPack::rename(String p_from, String p_to) { - return ERR_UNAVAILABLE; } -Error DirAccessPack::remove(String p_name) { +Error DirAccessPack::remove(String p_name) { return ERR_UNAVAILABLE; } size_t DirAccessPack::get_space_left() { - return 0; } @@ -505,10 +555,5 @@ String DirAccessPack::get_filesystem_type() const { } DirAccessPack::DirAccessPack() { - current = PackedData::get_singleton()->root; - cdir = false; -} - -DirAccessPack::~DirAccessPack() { } diff --git a/core/io/file_access_pack.h b/core/io/file_access_pack.h index 8df6826ac9..3c84e6b656 100644 --- a/core/io/file_access_pack.h +++ b/core/io/file_access_pack.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,16 +31,24 @@ #ifndef FILE_ACCESS_PACK_H #define FILE_ACCESS_PACK_H -#include "core/list.h" -#include "core/map.h" #include "core/os/dir_access.h" #include "core/os/file_access.h" -#include "core/print_string.h" +#include "core/string/print_string.h" +#include "core/templates/list.h" +#include "core/templates/map.h" // Godot's packed file magic header ("GDPC" in ASCII). #define PACK_HEADER_MAGIC 0x43504447 // The current packed file format version number. -#define PACK_FORMAT_VERSION 1 +#define PACK_FORMAT_VERSION 2 + +enum PackFlags { + PACK_DIR_ENCRYPTED = 1 << 0 +}; + +enum PackFileFlags { + PACK_FILE_ENCRYPTED = 1 << 0 +}; class PackSource; @@ -51,27 +59,26 @@ class PackedData { public: struct PackedFile { - String pack; uint64_t offset; //if offset is ZERO, the file was ERASED uint64_t size; uint8_t md5[16]; PackSource *src; + bool encrypted; }; private: struct PackedDir { - PackedDir *parent; + PackedDir *parent = nullptr; String name; Map<String, PackedDir *> subdirs; Set<String> files; }; struct PathMD5 { - uint64_t a; - uint64_t b; + uint64_t a = 0; + uint64_t b = 0; bool operator<(const PathMD5 &p_md5) const { - if (p_md5.a == a) { return b < p_md5.b; } else { @@ -81,16 +88,14 @@ private: bool operator==(const PathMD5 &p_md5) const { return a == p_md5.a && b == p_md5.b; - }; + } - PathMD5() { - a = b = 0; - }; + PathMD5() {} PathMD5(const Vector<uint8_t> p_buf) { a = *((uint64_t *)&p_buf[0]); b = *((uint64_t *)&p_buf[8]); - }; + } }; Map<PathMD5, PackedFile> files; @@ -98,51 +103,51 @@ private: Vector<PackSource *> sources; PackedDir *root; - //Map<String,PackedDir*> dirs; static PackedData *singleton; - bool disabled; + bool disabled = false; void _free_packed_dirs(PackedDir *p_dir); public: void add_pack_source(PackSource *p_source); - void add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files); // for PackSource + void add_path(const String &pkg_path, const String &path, uint64_t ofs, uint64_t size, const uint8_t *p_md5, PackSource *p_src, bool p_replace_files, bool p_encrypted = false); // for PackSource void set_disabled(bool p_disabled) { disabled = p_disabled; } _FORCE_INLINE_ bool is_disabled() const { return disabled; } static PackedData *get_singleton() { return singleton; } - Error add_pack(const String &p_path, bool p_replace_files); + Error add_pack(const String &p_path, bool p_replace_files, size_t p_offset); _FORCE_INLINE_ FileAccess *try_open_path(const String &p_path); _FORCE_INLINE_ bool has_path(const String &p_path); + _FORCE_INLINE_ DirAccess *try_open_directory(const String &p_path); + _FORCE_INLINE_ bool has_directory(const String &p_path); + PackedData(); ~PackedData(); }; class PackSource { - public: - virtual bool try_open_pack(const String &p_path, bool p_replace_files) = 0; + virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset) = 0; virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file) = 0; virtual ~PackSource() {} }; class PackedSourcePCK : public PackSource { - public: - virtual bool try_open_pack(const String &p_path, bool p_replace_files); + virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset); virtual FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file); }; class FileAccessPack : public FileAccess { - PackedData::PackedFile pf; mutable size_t pos; mutable bool eof; + uint64_t off; FileAccess *f; virtual Error _open(const String &p_path, int p_mode_flags); @@ -181,29 +186,40 @@ public: }; FileAccess *PackedData::try_open_path(const String &p_path) { - PathMD5 pmd5(p_path.md5_buffer()); Map<PathMD5, PackedFile>::Element *E = files.find(pmd5); - if (!E) + if (!E) { return nullptr; //not found - if (E->get().offset == 0) + } + if (E->get().offset == 0) { return nullptr; //was erased + } return E->get().src->get_file(p_path, &E->get()); } bool PackedData::has_path(const String &p_path) { - return files.has(PathMD5(p_path.md5_buffer())); } -class DirAccessPack : public DirAccess { +bool PackedData::has_directory(const String &p_path) { + DirAccess *da = try_open_directory(p_path); + if (da) { + memdelete(da); + return true; + } else { + return false; + } +} +class DirAccessPack : public DirAccess { PackedData::PackedDir *current; List<String> list_dirs; List<String> list_files; - bool cdir; + bool cdir = false; + + PackedData::PackedDir *_find_dir(String p_dir); public: virtual Error list_dir_begin(); @@ -231,7 +247,16 @@ public: virtual String get_filesystem_type() const; DirAccessPack(); - ~DirAccessPack(); + ~DirAccessPack() {} }; +DirAccess *PackedData::try_open_directory(const String &p_path) { + DirAccess *da = memnew(DirAccessPack()); + if (da->change_dir(p_path) != OK) { + memdelete(da); + da = nullptr; + } + return da; +} + #endif // FILE_ACCESS_PACK_H diff --git a/core/io/file_access_zip.cpp b/core/io/file_access_zip.cpp index 57de66afaf..01f9337a80 100644 --- a/core/io/file_access_zip.cpp +++ b/core/io/file_access_zip.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -40,7 +40,6 @@ ZipArchive *ZipArchive::instance = nullptr; extern "C" { static void *godot_open(void *data, const char *p_fname, int mode) { - if (mode & ZLIB_FILEFUNC_MODE_WRITE) { return nullptr; } @@ -52,30 +51,25 @@ static void *godot_open(void *data, const char *p_fname, int mode) { } static uLong godot_read(void *data, void *fdata, void *buf, uLong size) { - FileAccess *f = (FileAccess *)data; f->get_buffer((uint8_t *)buf, size); return size; } static uLong godot_write(voidpf opaque, voidpf stream, const void *buf, uLong size) { - return 0; } static long godot_tell(voidpf opaque, voidpf stream) { - FileAccess *f = (FileAccess *)opaque; return f->get_position(); } static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { - FileAccess *f = (FileAccess *)opaque; int pos = offset; switch (origin) { - case ZLIB_FILEFUNC_SEEK_CUR: pos = f->get_position() + offset; break; @@ -91,32 +85,26 @@ static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { } static int godot_close(voidpf opaque, voidpf stream) { - FileAccess *f = (FileAccess *)opaque; f->close(); return 0; } static int godot_testerror(voidpf opaque, voidpf stream) { - FileAccess *f = (FileAccess *)opaque; return f->get_error() != OK ? 1 : 0; } static voidpf godot_alloc(voidpf opaque, uInt items, uInt size) { - return memalloc(items * size); } static void godot_free(voidpf opaque, voidpf address) { - memfree(address); } - } // extern "C" void ZipArchive::close_handle(unzFile p_file) const { - ERR_FAIL_COND_MSG(!p_file, "Cannot close a file if none is open."); FileAccess *f = (FileAccess *)unzGetOpaque(p_file); unzCloseCurrentFile(p_file); @@ -125,7 +113,6 @@ void ZipArchive::close_handle(unzFile p_file) const { } unzFile ZipArchive::get_file_handle(String p_file) const { - ERR_FAIL_COND_V_MSG(!file_exists(p_file), nullptr, "File '" + p_file + " doesn't exist."); File file = files[p_file]; @@ -152,7 +139,6 @@ unzFile ZipArchive::get_file_handle(String p_file) const { ERR_FAIL_COND_V(!pkg, nullptr); int unz_err = unzGoToFilePos(pkg, &file.file_pos); if (unz_err != UNZ_OK || unzOpenCurrentFile(pkg) != UNZ_OK) { - unzClose(pkg); ERR_FAIL_V(nullptr); } @@ -160,17 +146,21 @@ unzFile ZipArchive::get_file_handle(String p_file) const { return pkg; } -bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files) { +bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset = 0) { + //printf("opening zip pack %s, %i, %i\n", p_name.utf8().get_data(), p_name.extension().nocasecmp_to("zip"), p_name.extension().nocasecmp_to("pcz")); + // load with offset feature only supported for PCK files + ERR_FAIL_COND_V_MSG(p_offset != 0, false, "Invalid PCK data. Note that loading files with a non-zero offset isn't supported with ZIP archives."); - //printf("opening zip pack %ls, %i, %i\n", p_name.c_str(), p_name.extension().nocasecmp_to("zip"), p_name.extension().nocasecmp_to("pcz")); - if (p_path.get_extension().nocasecmp_to("zip") != 0 && p_path.get_extension().nocasecmp_to("pcz") != 0) + if (p_path.get_extension().nocasecmp_to("zip") != 0 && p_path.get_extension().nocasecmp_to("pcz") != 0) { return false; + } zlib_filefunc_def io; FileAccess *fa = FileAccess::open(p_path, FileAccess::READ); - if (!fa) + if (!fa) { return false; + } io.opaque = fa; io.zopen_file = godot_open; io.zread_file = godot_read; @@ -195,7 +185,6 @@ bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files) { int pkg_num = packages.size() - 1; for (uint64_t i = 0; i < gi.number_entry; i++) { - char filename_inzip[256]; unz_file_info64 file_info; @@ -210,8 +199,8 @@ bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files) { files[fname] = f; uint8_t md5[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files); - //printf("packed data add path %ls, %ls\n", p_name.c_str(), fname.c_str()); + PackedData::get_singleton()->add_path(p_path, fname, 1, 0, md5, this, p_replace_files, false); + //printf("packed data add path %s, %s\n", p_name.utf8().get_data(), fname.utf8().get_data()); if ((i + 1) < gi.number_entry) { unzGoToNextFile(zfile); @@ -222,17 +211,14 @@ bool ZipArchive::try_open_pack(const String &p_path, bool p_replace_files) { } bool ZipArchive::file_exists(String p_name) const { - return files.has(p_name); } FileAccess *ZipArchive::get_file(const String &p_path, PackedData::PackedFile *p_file) { - return memnew(FileAccessZip(p_path, *p_file)); } ZipArchive *ZipArchive::get_singleton() { - if (instance == nullptr) { instance = memnew(ZipArchive); } @@ -241,15 +227,11 @@ ZipArchive *ZipArchive::get_singleton() { } ZipArchive::ZipArchive() { - instance = this; - //fa_create_func = FileAccess::get_create_func(); } ZipArchive::~ZipArchive() { - for (int i = 0; i < packages.size(); i++) { - FileAccess *f = (FileAccess *)unzGetOpaque(packages[i].zfile); unzClose(packages[i].zfile); memdelete(f); @@ -259,7 +241,6 @@ ZipArchive::~ZipArchive() { } Error FileAccessZip::_open(const String &p_path, int p_mode_flags) { - close(); ERR_FAIL_COND_V(p_mode_flags & FileAccess::WRITE, FAILED); @@ -275,9 +256,9 @@ Error FileAccessZip::_open(const String &p_path, int p_mode_flags) { } void FileAccessZip::close() { - - if (!zfile) + if (!zfile) { return; + } ZipArchive *arch = ZipArchive::get_singleton(); ERR_FAIL_COND(!arch); @@ -286,65 +267,57 @@ void FileAccessZip::close() { } bool FileAccessZip::is_open() const { - return zfile != nullptr; } void FileAccessZip::seek(size_t p_position) { - ERR_FAIL_COND(!zfile); unzSeekCurrentFile(zfile, p_position); } void FileAccessZip::seek_end(int64_t p_position) { - ERR_FAIL_COND(!zfile); unzSeekCurrentFile(zfile, get_len() + p_position); } size_t FileAccessZip::get_position() const { - ERR_FAIL_COND_V(!zfile, 0); return unztell(zfile); } size_t FileAccessZip::get_len() const { - ERR_FAIL_COND_V(!zfile, 0); return file_info.uncompressed_size; } bool FileAccessZip::eof_reached() const { - ERR_FAIL_COND_V(!zfile, true); return at_eof; } uint8_t FileAccessZip::get_8() const { - uint8_t ret = 0; get_buffer(&ret, 1); return ret; } int FileAccessZip::get_buffer(uint8_t *p_dst, int p_length) const { - ERR_FAIL_COND_V(!zfile, -1); at_eof = unzeof(zfile); - if (at_eof) + if (at_eof) { return 0; + } int read = unzReadCurrentFile(zfile, p_dst, p_length); ERR_FAIL_COND_V(read < 0, read); - if (read < p_length) + if (read < p_length) { at_eof = true; + } return read; } Error FileAccessZip::get_error() const { - if (!zfile) { - return ERR_UNCONFIGURED; } if (eof_reached()) { @@ -355,28 +328,23 @@ Error FileAccessZip::get_error() const { } void FileAccessZip::flush() { - ERR_FAIL(); } void FileAccessZip::store_8(uint8_t p_dest) { - ERR_FAIL(); } bool FileAccessZip::file_exists(const String &p_name) { - return false; } -FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) : - zfile(nullptr) { +FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) { _open(p_path, FileAccess::READ); } FileAccessZip::~FileAccessZip() { - close(); } -#endif +#endif // MINIZIP_ENABLED diff --git a/core/io/file_access_zip.h b/core/io/file_access_zip.h index d5ce7d7a8d..8559f871ce 100644 --- a/core/io/file_access_zip.h +++ b/core/io/file_access_zip.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -28,35 +28,30 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ -#ifdef MINIZIP_ENABLED - #ifndef FILE_ACCESS_ZIP_H #define FILE_ACCESS_ZIP_H +#ifdef MINIZIP_ENABLED + #include "core/io/file_access_pack.h" -#include "core/map.h" +#include "core/templates/map.h" #include "thirdparty/minizip/unzip.h" #include <stdlib.h> class ZipArchive : public PackSource { - public: struct File { - - int package; + int package = -1; unz_file_pos file_pos; - File() { - - package = -1; - }; + File() {} }; private: struct Package { String filename; - unzFile zfile; + unzFile zfile = nullptr; }; Vector<Package> packages; @@ -64,8 +59,6 @@ private: static ZipArchive *instance; - FileAccess::CreateFunc fa_create_func; - public: void close_handle(unzFile p_file) const; unzFile get_file_handle(String p_file) const; @@ -74,7 +67,7 @@ public: bool file_exists(String p_name) const; - virtual bool try_open_pack(const String &p_path, bool p_replace_files); + virtual bool try_open_pack(const String &p_path, bool p_replace_files, size_t p_offset); FileAccess *get_file(const String &p_path, PackedData::PackedFile *p_file); static ZipArchive *get_singleton(); @@ -84,8 +77,7 @@ public: }; class FileAccessZip : public FileAccess { - - unzFile zfile; + unzFile zfile = nullptr; unz_file_info64 file_info; mutable bool at_eof; @@ -119,6 +111,6 @@ public: ~FileAccessZip(); }; -#endif // FILE_ACCESS_ZIP_H - #endif // MINIZIP_ENABLED + +#endif // FILE_ACCESS_ZIP_H diff --git a/core/io/http_client.cpp b/core/io/http_client.cpp index 56f8f1ff91..a2fcf074ae 100644 --- a/core/io/http_client.cpp +++ b/core/io/http_client.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -47,7 +47,6 @@ const char *HTTPClient::_methods[METHOD_MAX] = { #ifndef JAVASCRIPT_ENABLED Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl, bool p_verify_host) { - close(); conn_port = p_port; @@ -58,10 +57,8 @@ Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl, String host_lower = conn_host.to_lower(); if (host_lower.begins_with("http://")) { - conn_host = conn_host.substr(7, conn_host.length() - 7); } else if (host_lower.begins_with("https://")) { - ssl = true; conn_host = conn_host.substr(8, conn_host.length() - 8); } @@ -97,38 +94,56 @@ Error HTTPClient::connect_to_host(const String &p_host, int p_port, bool p_ssl, } void HTTPClient::set_connection(const Ref<StreamPeer> &p_connection) { - ERR_FAIL_COND_MSG(p_connection.is_null(), "Connection is not a reference to a valid StreamPeer object."); + if (connection == p_connection) { + return; + } + close(); connection = p_connection; status = STATUS_CONNECTED; } Ref<StreamPeer> HTTPClient::get_connection() const { - return connection; } -Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const Vector<uint8_t> &p_body) { +static bool _check_request_url(HTTPClient::Method p_method, const String &p_url) { + switch (p_method) { + case HTTPClient::METHOD_CONNECT: { + // Authority in host:port format, as in RFC7231 + int pos = p_url.find_char(':'); + return 0 < pos && pos < p_url.length() - 1; + } + case HTTPClient::METHOD_OPTIONS: { + if (p_url == "*") { + return true; + } + [[fallthrough]]; + } + default: + // Absolute path or absolute URL + return p_url.begins_with("/") || p_url.begins_with("http://") || p_url.begins_with("https://"); + } +} +Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector<String> &p_headers, const Vector<uint8_t> &p_body) { ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER); - ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!_check_request_url(p_method, p_url), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA); String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n"; - if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) { - // Don't append the standard ports - request += "Host: " + conn_host + "\r\n"; - } else { - request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; - } + bool add_host = true; bool add_clen = p_body.size() > 0; bool add_uagent = true; bool add_accept = true; for (int i = 0; i < p_headers.size(); i++) { request += p_headers[i] + "\r\n"; + if (add_host && p_headers[i].findn("Host:") == 0) { + add_host = false; + } if (add_clen && p_headers[i].findn("Content-Length:") == 0) { add_clen = false; } @@ -139,6 +154,14 @@ Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector add_accept = false; } } + if (add_host) { + if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) { + // Don't append the standard ports + request += "Host: " + conn_host + "\r\n"; + } else { + request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; + } + } if (add_clen) { request += "Content-Length: " + itos(p_body.size()) + "\r\n"; // Should it add utf8 encoding? @@ -179,24 +202,21 @@ Error HTTPClient::request_raw(Method p_method, const String &p_url, const Vector } Error HTTPClient::request(Method p_method, const String &p_url, const Vector<String> &p_headers, const String &p_body) { - ERR_FAIL_INDEX_V(p_method, METHOD_MAX, ERR_INVALID_PARAMETER); - ERR_FAIL_COND_V(!p_url.begins_with("/"), ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!_check_request_url(p_method, p_url), ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(status != STATUS_CONNECTED, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(connection.is_null(), ERR_INVALID_DATA); String request = String(_methods[p_method]) + " " + p_url + " HTTP/1.1\r\n"; - if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) { - // Don't append the standard ports - request += "Host: " + conn_host + "\r\n"; - } else { - request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; - } + bool add_host = true; bool add_uagent = true; bool add_accept = true; bool add_clen = p_body.length() > 0; for (int i = 0; i < p_headers.size(); i++) { request += p_headers[i] + "\r\n"; + if (add_host && p_headers[i].findn("Host:") == 0) { + add_host = false; + } if (add_clen && p_headers[i].findn("Content-Length:") == 0) { add_clen = false; } @@ -207,6 +227,14 @@ Error HTTPClient::request(Method p_method, const String &p_url, const Vector<Str add_accept = false; } } + if (add_host) { + if ((ssl && conn_port == PORT_HTTPS) || (!ssl && conn_port == PORT_HTTP)) { + // Don't append the standard ports + request += "Host: " + conn_host + "\r\n"; + } else { + request += "Host: " + conn_host + ":" + itos(conn_port) + "\r\n"; + } + } if (add_clen) { request += "Content-Length: " + itos(p_body.utf8().length()) + "\r\n"; // Should it add utf8 encoding? @@ -235,27 +263,23 @@ Error HTTPClient::request(Method p_method, const String &p_url, const Vector<Str } bool HTTPClient::has_response() const { - return response_headers.size() != 0; } bool HTTPClient::is_response_chunked() const { - return chunked; } int HTTPClient::get_response_code() const { - return response_num; } Error HTTPClient::get_response_headers(List<String> *r_response) { - - if (!response_headers.size()) + if (!response_headers.size()) { return ERR_INVALID_PARAMETER; + } for (int i = 0; i < response_headers.size(); i++) { - r_response->push_back(response_headers[i]); } @@ -265,15 +289,14 @@ Error HTTPClient::get_response_headers(List<String> *r_response) { } void HTTPClient::close() { - - if (tcp_connection->get_status() != StreamPeerTCP::STATUS_NONE) + if (tcp_connection->get_status() != StreamPeerTCP::STATUS_NONE) { tcp_connection->disconnect_from_host(); + } connection.unref(); status = STATUS_DISCONNECTED; head_request = false; if (resolving != IP::RESOLVER_INVALID_ID) { - IP::get_singleton()->erase_resolve_item(resolving); resolving = IP::RESOLVER_INVALID_ID; } @@ -283,16 +306,14 @@ void HTTPClient::close() { body_size = -1; body_left = 0; chunk_left = 0; - chunk_trailer_part = 0; + chunk_trailer_part = false; read_until_eof = false; response_num = 0; handshaking = false; } Error HTTPClient::poll() { - switch (status) { - case STATUS_RESOLVING: { ERR_FAIL_COND_V(resolving == IP::RESOLVER_INVALID_ID, ERR_BUG); @@ -302,7 +323,6 @@ Error HTTPClient::poll() { return OK; // Still resolving case IP::RESOLVER_STATUS_DONE: { - IP_Address host = IP::get_singleton()->get_resolve_item_address(resolving); Error err = tcp_connection->connect_to_host(host, conn_port); IP::get_singleton()->erase_resolve_item(resolving); @@ -316,7 +336,6 @@ Error HTTPClient::poll() { } break; case IP::RESOLVER_STATUS_NONE: case IP::RESOLVER_STATUS_ERROR: { - IP::get_singleton()->erase_resolve_item(resolving); resolving = IP::RESOLVER_INVALID_ID; close(); @@ -326,10 +345,8 @@ Error HTTPClient::poll() { } } break; case STATUS_CONNECTING: { - StreamPeerTCP::Status s = tcp_connection->get_status(); switch (s) { - case StreamPeerTCP::STATUS_CONNECTING: { return OK; } break; @@ -379,7 +396,6 @@ Error HTTPClient::poll() { } break; case StreamPeerTCP::STATUS_ERROR: case StreamPeerTCP::STATUS_NONE: { - close(); status = STATUS_CANT_CONNECT; return ERR_CANT_CONNECT; @@ -404,7 +420,6 @@ Error HTTPClient::poll() { return OK; } break; case STATUS_REQUESTING: { - while (true) { uint8_t byte; int rec = 0; @@ -415,15 +430,15 @@ Error HTTPClient::poll() { return ERR_CONNECTION_ERROR; } - if (rec == 0) + if (rec == 0) { return OK; // Still requesting, keep trying! + } response_str.push_back(byte); int rs = response_str.size(); if ( (rs >= 2 && response_str[rs - 2] == '\n' && response_str[rs - 1] == '\n') || (rs >= 4 && response_str[rs - 4] == '\r' && response_str[rs - 3] == '\n' && response_str[rs - 2] == '\r' && response_str[rs - 1] == '\n')) { - // End of response, parse. response_str.push_back(0); String response; @@ -445,11 +460,11 @@ Error HTTPClient::poll() { bool keep_alive = true; for (int i = 0; i < responses.size(); i++) { - String header = responses[i].strip_edges(); String s = header.to_lower(); - if (s.length() == 0) + if (s.length() == 0) { continue; + } if (s.begins_with("content-length:")) { body_size = s.substr(s.find(":") + 1, s.length()).strip_edges().to_int(); body_left = body_size; @@ -464,30 +479,25 @@ Error HTTPClient::poll() { } if (i == 0 && responses[i].begins_with("HTTP")) { - String num = responses[i].get_slicec(' ', 1); response_num = num.to_int(); } else { - response_headers.push_back(header); } } - // This is a HEAD request, we wont receive anything. + // This is a HEAD request, we won't receive anything. if (head_request) { body_size = 0; body_left = 0; } if (body_size != -1 || chunked) { - status = STATUS_BODY; } else if (!keep_alive) { - read_until_eof = true; status = STATUS_BODY; } else { - status = STATUS_CONNECTED; } return OK; @@ -513,29 +523,26 @@ Error HTTPClient::poll() { } int HTTPClient::get_response_body_length() const { - return body_size; } PackedByteArray HTTPClient::read_response_body_chunk() { - ERR_FAIL_COND_V(status != STATUS_BODY, PackedByteArray()); PackedByteArray ret; Error err = OK; if (chunked) { - while (true) { - if (chunk_trailer_part) { // We need to consume the trailer part too or keep-alive will break uint8_t b; int rec = 0; err = _get_http_data(&b, 1, rec); - if (rec == 0) + if (rec == 0) { break; + } chunk.push_back(b); int cs = chunk.size(); @@ -557,8 +564,9 @@ PackedByteArray HTTPClient::read_response_body_chunk() { int rec = 0; err = _get_http_data(&b, 1, rec); - if (rec == 0) + if (rec == 0) { break; + } chunk.push_back(b); @@ -569,18 +577,17 @@ PackedByteArray HTTPClient::read_response_body_chunk() { } if (chunk.size() > 2 && chunk[chunk.size() - 2] == '\r' && chunk[chunk.size() - 1] == '\n') { - int len = 0; for (int i = 0; i < chunk.size() - 2; i++) { char c = chunk[i]; int v = 0; - if (c >= '0' && c <= '9') + if (c >= '0' && c <= '9') { v = c - '0'; - else if (c >= 'a' && c <= 'f') + } else if (c >= 'a' && c <= 'f') { v = c - 'a' + 10; - else if (c >= 'A' && c <= 'F') + } else if (c >= 'A' && c <= 'F') { v = c - 'A' + 10; - else { + } else { ERR_PRINT("HTTP Chunk len not in hex!!"); status = STATUS_CONNECTION_ERROR; break; @@ -605,7 +612,6 @@ PackedByteArray HTTPClient::read_response_body_chunk() { chunk.resize(chunk_left); } } else { - int rec = 0; err = _get_http_data(&chunk.write[chunk.size() - chunk_left], chunk_left, rec); if (rec == 0) { @@ -614,7 +620,6 @@ PackedByteArray HTTPClient::read_response_body_chunk() { chunk_left -= rec; if (chunk_left == 0) { - if (chunk[chunk.size() - 2] != '\r' || chunk[chunk.size() - 1] != '\n') { ERR_PRINT("HTTP Invalid chunk terminator (not \\r\\n)"); status = STATUS_CONNECTION_ERROR; @@ -632,7 +637,6 @@ PackedByteArray HTTPClient::read_response_body_chunk() { } } else { - int to_read = !read_until_eof ? MIN(body_left, read_chunk_size) : read_chunk_size; ret.resize(to_read); int _offset = 0; @@ -652,24 +656,21 @@ PackedByteArray HTTPClient::read_response_body_chunk() { body_left -= rec; } } - if (err != OK) + if (err != OK) { break; + } } } if (err != OK) { - close(); if (err == ERR_FILE_EOF) { - status = STATUS_DISCONNECTED; // Server disconnected } else { - status = STATUS_CONNECTION_ERROR; } } else if (body_left == 0 && !chunked && !read_until_eof) { - status = STATUS_CONNECTED; } @@ -677,24 +678,19 @@ PackedByteArray HTTPClient::read_response_body_chunk() { } HTTPClient::Status HTTPClient::get_status() const { - return status; } void HTTPClient::set_blocking_mode(bool p_enable) { - blocking = p_enable; } bool HTTPClient::is_blocking_mode_enabled() const { - return blocking; } Error HTTPClient::_get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received) { - if (blocking) { - // We can't use StreamPeer.get_data, since when reaching EOF we will get an // error without knowing how many bytes we received. Error err = ERR_FILE_EOF; @@ -729,27 +725,10 @@ int HTTPClient::get_read_chunk_size() const { } HTTPClient::HTTPClient() { - tcp_connection.instance(); - resolving = IP::RESOLVER_INVALID_ID; - status = STATUS_DISCONNECTED; - head_request = false; - conn_port = -1; - body_size = -1; - chunked = false; - body_left = 0; - read_until_eof = false; - chunk_left = 0; - chunk_trailer_part = false; - response_num = 0; - ssl = false; - blocking = false; - handshaking = false; - read_chunk_size = 4096; } -HTTPClient::~HTTPClient() { -} +HTTPClient::~HTTPClient() {} #endif // #ifndef JAVASCRIPT_ENABLED @@ -784,15 +763,15 @@ String HTTPClient::query_string_from_dict(const Dictionary &p_dict) { } Dictionary HTTPClient::_get_response_headers_as_dictionary() { - List<String> rh; get_response_headers(&rh); Dictionary ret; for (const List<String>::Element *E = rh.front(); E; E = E->next()) { const String &s = E->get(); int sp = s.find(":"); - if (sp == -1) + if (sp == -1) { continue; + } String key = s.substr(0, sp).strip_edges(); String value = s.substr(sp + 1, s.length()).strip_edges(); ret[key] = value; @@ -802,7 +781,6 @@ Dictionary HTTPClient::_get_response_headers_as_dictionary() { } PackedStringArray HTTPClient::_get_response_headers() { - List<String> rh; get_response_headers(&rh); PackedStringArray ret; @@ -816,7 +794,6 @@ PackedStringArray HTTPClient::_get_response_headers() { } void HTTPClient::_bind_methods() { - ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port", "use_ssl", "verify_host"), &HTTPClient::connect_to_host, DEFVAL(-1), DEFVAL(false), DEFVAL(true)); ClassDB::bind_method(D_METHOD("set_connection", "connection"), &HTTPClient::set_connection); ClassDB::bind_method(D_METHOD("get_connection"), &HTTPClient::get_connection); diff --git a/core/io/http_client.h b/core/io/http_client.h index 03ba20f8dd..ec4b86b26f 100644 --- a/core/io/http_client.h +++ b/core/io/http_client.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -34,15 +34,13 @@ #include "core/io/ip.h" #include "core/io/stream_peer.h" #include "core/io/stream_peer_tcp.h" -#include "core/reference.h" +#include "core/object/reference.h" class HTTPClient : public Reference { - GDCLASS(HTTPClient, Reference); public: enum ResponseCode { - // 1xx informational RESPONSE_CONTINUE = 100, RESPONSE_SWITCHING_PROTOCOLS = 101, @@ -117,7 +115,6 @@ public: }; enum Method { - METHOD_GET, METHOD_HEAD, METHOD_POST, @@ -132,7 +129,6 @@ public: }; enum Status { - STATUS_DISCONNECTED, STATUS_RESOLVING, // Resolving hostname (if passed a hostname) STATUS_CANT_RESOLVE, @@ -151,39 +147,39 @@ private: static const int HOST_MIN_LEN = 4; enum Port { - PORT_HTTP = 80, PORT_HTTPS = 443, }; #ifndef JAVASCRIPT_ENABLED - Status status; - IP::ResolverID resolving; - int conn_port; + Status status = STATUS_DISCONNECTED; + IP::ResolverID resolving = IP::RESOLVER_INVALID_ID; + int conn_port = -1; String conn_host; - bool ssl; - bool ssl_verify_host; - bool blocking; - bool handshaking; - bool head_request; + bool ssl = false; + bool ssl_verify_host = false; + bool blocking = false; + bool handshaking = false; + bool head_request = false; Vector<uint8_t> response_str; - bool chunked; + bool chunked = false; Vector<uint8_t> chunk; - int chunk_left; - bool chunk_trailer_part; - int body_size; - int body_left; - bool read_until_eof; + int chunk_left = 0; + bool chunk_trailer_part = false; + int body_size = -1; + int body_left = 0; + bool read_until_eof = false; Ref<StreamPeerTCP> tcp_connection; Ref<StreamPeer> connection; - int response_num; + int response_num = 0; Vector<String> response_headers; - int read_chunk_size; + // 64 KiB by default (favors fast download speeds at the cost of memory usage). + int read_chunk_size = 65536; Error _get_http_data(uint8_t *p_buffer, int p_bytes, int &r_received); diff --git a/core/io/image.cpp b/core/io/image.cpp new file mode 100644 index 0000000000..986c29b539 --- /dev/null +++ b/core/io/image.cpp @@ -0,0 +1,3602 @@ +/*************************************************************************/ +/* image.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "image.h" + +#include "core/error/error_macros.h" +#include "core/io/image_loader.h" +#include "core/io/resource_loader.h" +#include "core/math/math_funcs.h" +#include "core/os/copymem.h" +#include "core/string/print_string.h" +#include "core/templates/hash_map.h" + +#include <stdio.h> + +const char *Image::format_names[Image::FORMAT_MAX] = { + "Lum8", //luminance + "LumAlpha8", //luminance-alpha + "Red8", + "RedGreen", + "RGB8", + "RGBA8", + "RGBA4444", + "RGBA5551", + "RFloat", //float + "RGFloat", + "RGBFloat", + "RGBAFloat", + "RHalf", //half float + "RGHalf", + "RGBHalf", + "RGBAHalf", + "RGBE9995", + "DXT1 RGB8", //s3tc + "DXT3 RGBA8", + "DXT5 RGBA8", + "RGTC Red8", + "RGTC RedGreen8", + "BPTC_RGBA", + "BPTC_RGBF", + "BPTC_RGBFU", + "PVRTC1_2", //pvrtc + "PVRTC1_2A", + "PVRTC1_4", + "PVRTC1_4A", + "ETC", //etc1 + "ETC2_R11", //etc2 + "ETC2_R11S", //signed", NOT srgb. + "ETC2_RG11", + "ETC2_RG11S", + "ETC2_RGB8", + "ETC2_RGBA8", + "ETC2_RGB8A1", + "ETC2_RA_AS_RG", + "FORMAT_DXT5_RA_AS_RG", +}; + +SavePNGFunc Image::save_png_func = nullptr; +SaveEXRFunc Image::save_exr_func = nullptr; + +SavePNGBufferFunc Image::save_png_buffer_func = nullptr; + +void Image::_put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel) { + uint32_t ofs = (p_y * width + p_x) * p_pixelsize; + + for (uint32_t i = 0; i < p_pixelsize; i++) { + p_data[ofs + i] = p_pixel[i]; + } +} + +void Image::_get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel) { + uint32_t ofs = (p_y * width + p_x) * p_pixelsize; + + for (uint32_t i = 0; i < p_pixelsize; i++) { + p_pixel[i] = p_data[ofs + i]; + } +} + +int Image::get_format_pixel_size(Format p_format) { + switch (p_format) { + case FORMAT_L8: + return 1; //luminance + case FORMAT_LA8: + return 2; //luminance-alpha + case FORMAT_R8: + return 1; + case FORMAT_RG8: + return 2; + case FORMAT_RGB8: + return 3; + case FORMAT_RGBA8: + return 4; + case FORMAT_RGBA4444: + return 2; + case FORMAT_RGB565: + return 2; + case FORMAT_RF: + return 4; //float + case FORMAT_RGF: + return 8; + case FORMAT_RGBF: + return 12; + case FORMAT_RGBAF: + return 16; + case FORMAT_RH: + return 2; //half float + case FORMAT_RGH: + return 4; + case FORMAT_RGBH: + return 6; + case FORMAT_RGBAH: + return 8; + case FORMAT_RGBE9995: + return 4; + case FORMAT_DXT1: + return 1; //s3tc bc1 + case FORMAT_DXT3: + return 1; //bc2 + case FORMAT_DXT5: + return 1; //bc3 + case FORMAT_RGTC_R: + return 1; //bc4 + case FORMAT_RGTC_RG: + return 1; //bc5 + case FORMAT_BPTC_RGBA: + return 1; //btpc bc6h + case FORMAT_BPTC_RGBF: + return 1; //float / + case FORMAT_BPTC_RGBFU: + return 1; //unsigned float + case FORMAT_PVRTC1_2: + return 1; //pvrtc + case FORMAT_PVRTC1_2A: + return 1; + case FORMAT_PVRTC1_4: + return 1; + case FORMAT_PVRTC1_4A: + return 1; + case FORMAT_ETC: + return 1; //etc1 + case FORMAT_ETC2_R11: + return 1; //etc2 + case FORMAT_ETC2_R11S: + return 1; //signed: return 1; NOT srgb. + case FORMAT_ETC2_RG11: + return 1; + case FORMAT_ETC2_RG11S: + return 1; + case FORMAT_ETC2_RGB8: + return 1; + case FORMAT_ETC2_RGBA8: + return 1; + case FORMAT_ETC2_RGB8A1: + return 1; + case FORMAT_ETC2_RA_AS_RG: + return 1; + case FORMAT_DXT5_RA_AS_RG: + return 1; + case FORMAT_MAX: { + } + } + return 0; +} + +void Image::get_format_min_pixel_size(Format p_format, int &r_w, int &r_h) { + switch (p_format) { + case FORMAT_DXT1: //s3tc bc1 + case FORMAT_DXT3: //bc2 + case FORMAT_DXT5: //bc3 + case FORMAT_RGTC_R: //bc4 + case FORMAT_RGTC_RG: { //bc5 case case FORMAT_DXT1: + + r_w = 4; + r_h = 4; + } break; + case FORMAT_PVRTC1_2: + case FORMAT_PVRTC1_2A: { + r_w = 16; + r_h = 8; + } break; + case FORMAT_PVRTC1_4A: + case FORMAT_PVRTC1_4: { + r_w = 8; + r_h = 8; + } break; + case FORMAT_ETC: { + r_w = 4; + r_h = 4; + } break; + case FORMAT_BPTC_RGBA: + case FORMAT_BPTC_RGBF: + case FORMAT_BPTC_RGBFU: { + r_w = 4; + r_h = 4; + } break; + case FORMAT_ETC2_R11: //etc2 + case FORMAT_ETC2_R11S: //signed: NOT srgb. + case FORMAT_ETC2_RG11: + case FORMAT_ETC2_RG11S: + case FORMAT_ETC2_RGB8: + case FORMAT_ETC2_RGBA8: + case FORMAT_ETC2_RGB8A1: + case FORMAT_ETC2_RA_AS_RG: + case FORMAT_DXT5_RA_AS_RG: { + r_w = 4; + r_h = 4; + + } break; + + default: { + r_w = 1; + r_h = 1; + } break; + } +} + +int Image::get_format_pixel_rshift(Format p_format) { + if (p_format == FORMAT_DXT1 || p_format == FORMAT_RGTC_R || p_format == FORMAT_PVRTC1_4 || p_format == FORMAT_PVRTC1_4A || p_format == FORMAT_ETC || p_format == FORMAT_ETC2_R11 || p_format == FORMAT_ETC2_R11S || p_format == FORMAT_ETC2_RGB8 || p_format == FORMAT_ETC2_RGB8A1) { + return 1; + } else if (p_format == FORMAT_PVRTC1_2 || p_format == FORMAT_PVRTC1_2A) { + return 2; + } else { + return 0; + } +} + +int Image::get_format_block_size(Format p_format) { + switch (p_format) { + case FORMAT_DXT1: //s3tc bc1 + case FORMAT_DXT3: //bc2 + case FORMAT_DXT5: //bc3 + case FORMAT_RGTC_R: //bc4 + case FORMAT_RGTC_RG: { //bc5 case case FORMAT_DXT1: + + return 4; + } + case FORMAT_PVRTC1_2: + case FORMAT_PVRTC1_2A: { + return 4; + } + case FORMAT_PVRTC1_4A: + case FORMAT_PVRTC1_4: { + return 4; + } + case FORMAT_ETC: { + return 4; + } + case FORMAT_BPTC_RGBA: + case FORMAT_BPTC_RGBF: + case FORMAT_BPTC_RGBFU: { + return 4; + } + case FORMAT_ETC2_R11: //etc2 + case FORMAT_ETC2_R11S: //signed: NOT srgb. + case FORMAT_ETC2_RG11: + case FORMAT_ETC2_RG11S: + case FORMAT_ETC2_RGB8: + case FORMAT_ETC2_RGBA8: + case FORMAT_ETC2_RGB8A1: + case FORMAT_ETC2_RA_AS_RG: //used to make basis universal happy + case FORMAT_DXT5_RA_AS_RG: //used to make basis universal happy + + { + return 4; + } + default: { + } + } + + return 1; +} + +void Image::_get_mipmap_offset_and_size(int p_mipmap, int &r_offset, int &r_width, int &r_height) const { + int w = width; + int h = height; + int ofs = 0; + + int pixel_size = get_format_pixel_size(format); + int pixel_rshift = get_format_pixel_rshift(format); + int block = get_format_block_size(format); + int minw, minh; + get_format_min_pixel_size(format, minw, minh); + + for (int i = 0; i < p_mipmap; i++) { + int bw = w % block != 0 ? w + (block - w % block) : w; + int bh = h % block != 0 ? h + (block - h % block) : h; + + int s = bw * bh; + + s *= pixel_size; + s >>= pixel_rshift; + ofs += s; + w = MAX(minw, w >> 1); + h = MAX(minh, h >> 1); + } + + r_offset = ofs; + r_width = w; + r_height = h; +} + +int Image::get_mipmap_offset(int p_mipmap) const { + ERR_FAIL_INDEX_V(p_mipmap, get_mipmap_count() + 1, -1); + + int ofs, w, h; + _get_mipmap_offset_and_size(p_mipmap, ofs, w, h); + return ofs; +} + +int Image::get_mipmap_byte_size(int p_mipmap) const { + ERR_FAIL_INDEX_V(p_mipmap, get_mipmap_count() + 1, -1); + + int ofs, w, h; + _get_mipmap_offset_and_size(p_mipmap, ofs, w, h); + int ofs2; + _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w, h); + return ofs2 - ofs; +} + +void Image::get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const { + int ofs, w, h; + _get_mipmap_offset_and_size(p_mipmap, ofs, w, h); + int ofs2; + _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w, h); + r_ofs = ofs; + r_size = ofs2 - ofs; +} + +void Image::get_mipmap_offset_size_and_dimensions(int p_mipmap, int &r_ofs, int &r_size, int &w, int &h) const { + int ofs; + _get_mipmap_offset_and_size(p_mipmap, ofs, w, h); + int ofs2, w2, h2; + _get_mipmap_offset_and_size(p_mipmap + 1, ofs2, w2, h2); + r_ofs = ofs; + r_size = ofs2 - ofs; +} + +Image::Image3DValidateError Image::validate_3d_image(Image::Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_images) { + int w = p_width; + int h = p_height; + int d = p_depth; + + int arr_ofs = 0; + + while (true) { + for (int i = 0; i < d; i++) { + int idx = i + arr_ofs; + if (idx >= p_images.size()) { + return VALIDATE_3D_ERR_MISSING_IMAGES; + } + if (p_images[idx].is_null() || p_images[idx]->is_empty()) { + return VALIDATE_3D_ERR_IMAGE_EMPTY; + } + if (p_images[idx]->get_format() != p_format) { + return VALIDATE_3D_ERR_IMAGE_FORMAT_MISMATCH; + } + if (p_images[idx]->get_width() != w || p_images[idx]->get_height() != h) { + return VALIDATE_3D_ERR_IMAGE_SIZE_MISMATCH; + } + if (p_images[idx]->has_mipmaps()) { + return VALIDATE_3D_ERR_IMAGE_HAS_MIPMAPS; + } + } + + arr_ofs += d; + + if (!p_mipmaps) { + break; + } + + if (w == 1 && h == 1 && d == 1) { + break; + } + + w = MAX(1, w >> 1); + h = MAX(1, h >> 1); + d = MAX(1, d >> 1); + } + + if (arr_ofs != p_images.size()) { + return VALIDATE_3D_ERR_EXTRA_IMAGES; + } + + return VALIDATE_3D_OK; +} + +String Image::get_3d_image_validation_error_text(Image3DValidateError p_error) { + switch (p_error) { + case VALIDATE_3D_OK: { + return TTR("Ok"); + } break; + case VALIDATE_3D_ERR_IMAGE_EMPTY: { + return TTR("Empty Image found"); + } break; + case VALIDATE_3D_ERR_MISSING_IMAGES: { + return TTR("Missing Images"); + } break; + case VALIDATE_3D_ERR_EXTRA_IMAGES: { + return TTR("Too many Images"); + } break; + case VALIDATE_3D_ERR_IMAGE_SIZE_MISMATCH: { + return TTR("Image size mismatch"); + } break; + case VALIDATE_3D_ERR_IMAGE_FORMAT_MISMATCH: { + return TTR("Image format mismatch"); + } break; + case VALIDATE_3D_ERR_IMAGE_HAS_MIPMAPS: { + return TTR("Image has included mipmaps"); + } break; + } + return String(); +} + +int Image::get_width() const { + return width; +} + +int Image::get_height() const { + return height; +} + +Vector2 Image::get_size() const { + return Vector2(width, height); +} + +bool Image::has_mipmaps() const { + return mipmaps; +} + +int Image::get_mipmap_count() const { + if (mipmaps) { + return get_image_required_mipmaps(width, height, format); + } else { + return 0; + } +} + +//using template generates perfectly optimized code due to constant expression reduction and unused variable removal present in all compilers +template <uint32_t read_bytes, bool read_alpha, uint32_t write_bytes, bool write_alpha, bool read_gray, bool write_gray> +static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p_dst) { + uint32_t max_bytes = MAX(read_bytes, write_bytes); + + for (int y = 0; y < p_height; y++) { + for (int x = 0; x < p_width; x++) { + const uint8_t *rofs = &p_src[((y * p_width) + x) * (read_bytes + (read_alpha ? 1 : 0))]; + uint8_t *wofs = &p_dst[((y * p_width) + x) * (write_bytes + (write_alpha ? 1 : 0))]; + + uint8_t rgba[4]; + + if (read_gray) { + rgba[0] = rofs[0]; + rgba[1] = rofs[0]; + rgba[2] = rofs[0]; + } else { + for (uint32_t i = 0; i < max_bytes; i++) { + rgba[i] = (i < read_bytes) ? rofs[i] : 0; + } + } + + if (read_alpha || write_alpha) { + rgba[3] = read_alpha ? rofs[read_bytes] : 255; + } + + if (write_gray) { + //TODO: not correct grayscale, should use fixed point version of actual weights + wofs[0] = uint8_t((uint16_t(rofs[0]) + uint16_t(rofs[1]) + uint16_t(rofs[2])) / 3); + } else { + for (uint32_t i = 0; i < write_bytes; i++) { + wofs[i] = rgba[i]; + } + } + + if (write_alpha) { + wofs[write_bytes] = rgba[3]; + } + } + } +} + +void Image::convert(Format p_new_format) { + if (data.size() == 0) { + return; + } + + if (p_new_format == format) { + return; + } + + if (format > FORMAT_RGBE9995 || p_new_format > FORMAT_RGBE9995) { + ERR_FAIL_MSG("Cannot convert to <-> from compressed formats. Use compress() and decompress() instead."); + + } else if (format > FORMAT_RGBA8 || p_new_format > FORMAT_RGBA8) { + //use put/set pixel which is slower but works with non byte formats + Image new_img(width, height, false, p_new_format); + + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + new_img.set_pixel(i, j, get_pixel(i, j)); + } + } + + if (has_mipmaps()) { + new_img.generate_mipmaps(); + } + + _copy_internals_from(new_img); + + return; + } + + Image new_img(width, height, false, p_new_format); + + const uint8_t *rptr = data.ptr(); + uint8_t *wptr = new_img.data.ptrw(); + + int conversion_type = format | p_new_format << 8; + + switch (conversion_type) { + case FORMAT_L8 | (FORMAT_LA8 << 8): + _convert<1, false, 1, true, true, true>(width, height, rptr, wptr); + break; + case FORMAT_L8 | (FORMAT_R8 << 8): + _convert<1, false, 1, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_L8 | (FORMAT_RG8 << 8): + _convert<1, false, 2, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_L8 | (FORMAT_RGB8 << 8): + _convert<1, false, 3, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_L8 | (FORMAT_RGBA8 << 8): + _convert<1, false, 3, true, true, false>(width, height, rptr, wptr); + break; + case FORMAT_LA8 | (FORMAT_L8 << 8): + _convert<1, true, 1, false, true, true>(width, height, rptr, wptr); + break; + case FORMAT_LA8 | (FORMAT_R8 << 8): + _convert<1, true, 1, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_LA8 | (FORMAT_RG8 << 8): + _convert<1, true, 2, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_LA8 | (FORMAT_RGB8 << 8): + _convert<1, true, 3, false, true, false>(width, height, rptr, wptr); + break; + case FORMAT_LA8 | (FORMAT_RGBA8 << 8): + _convert<1, true, 3, true, true, false>(width, height, rptr, wptr); + break; + case FORMAT_R8 | (FORMAT_L8 << 8): + _convert<1, false, 1, false, false, true>(width, height, rptr, wptr); + break; + case FORMAT_R8 | (FORMAT_LA8 << 8): + _convert<1, false, 1, true, false, true>(width, height, rptr, wptr); + break; + case FORMAT_R8 | (FORMAT_RG8 << 8): + _convert<1, false, 2, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_R8 | (FORMAT_RGB8 << 8): + _convert<1, false, 3, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_R8 | (FORMAT_RGBA8 << 8): + _convert<1, false, 3, true, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RG8 | (FORMAT_L8 << 8): + _convert<2, false, 1, false, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RG8 | (FORMAT_LA8 << 8): + _convert<2, false, 1, true, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RG8 | (FORMAT_R8 << 8): + _convert<2, false, 1, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RG8 | (FORMAT_RGB8 << 8): + _convert<2, false, 3, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RG8 | (FORMAT_RGBA8 << 8): + _convert<2, false, 3, true, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGB8 | (FORMAT_L8 << 8): + _convert<3, false, 1, false, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RGB8 | (FORMAT_LA8 << 8): + _convert<3, false, 1, true, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RGB8 | (FORMAT_R8 << 8): + _convert<3, false, 1, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGB8 | (FORMAT_RG8 << 8): + _convert<3, false, 2, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGB8 | (FORMAT_RGBA8 << 8): + _convert<3, false, 3, true, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGBA8 | (FORMAT_L8 << 8): + _convert<3, true, 1, false, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RGBA8 | (FORMAT_LA8 << 8): + _convert<3, true, 1, true, false, true>(width, height, rptr, wptr); + break; + case FORMAT_RGBA8 | (FORMAT_R8 << 8): + _convert<3, true, 1, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGBA8 | (FORMAT_RG8 << 8): + _convert<3, true, 2, false, false, false>(width, height, rptr, wptr); + break; + case FORMAT_RGBA8 | (FORMAT_RGB8 << 8): + _convert<3, true, 3, false, false, false>(width, height, rptr, wptr); + break; + } + + bool gen_mipmaps = mipmaps; + + _copy_internals_from(new_img); + + if (gen_mipmaps) { + generate_mipmaps(); + } +} + +Image::Format Image::get_format() const { + return format; +} + +static double _bicubic_interp_kernel(double x) { + x = ABS(x); + + double bc = 0; + + if (x <= 1) { + bc = (1.5 * x - 2.5) * x * x + 1; + } else if (x < 2) { + bc = ((-0.5 * x + 2.5) * x - 4) * x + 2; + } + + return bc; +} + +template <int CC, class T> +static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) { + // get source image size + int width = p_src_width; + int height = p_src_height; + double xfac = (double)width / p_dst_width; + double yfac = (double)height / p_dst_height; + // coordinates of source points and coefficients + double ox, oy, dx, dy, k1, k2; + int ox1, oy1, ox2, oy2; + // destination pixel values + // width and height decreased by 1 + int ymax = height - 1; + int xmax = width - 1; + // temporary pointer + + for (uint32_t y = 0; y < p_dst_height; y++) { + // Y coordinates + oy = (double)y * yfac - 0.5f; + oy1 = (int)oy; + dy = oy - (double)oy1; + + for (uint32_t x = 0; x < p_dst_width; x++) { + // X coordinates + ox = (double)x * xfac - 0.5f; + ox1 = (int)ox; + dx = ox - (double)ox1; + + // initial pixel value + + T *__restrict dst = ((T *)p_dst) + (y * p_dst_width + x) * CC; + + double color[CC]; + for (int i = 0; i < CC; i++) { + color[i] = 0; + } + + for (int n = -1; n < 3; n++) { + // get Y coefficient + k1 = _bicubic_interp_kernel(dy - (double)n); + + oy2 = oy1 + n; + if (oy2 < 0) { + oy2 = 0; + } + if (oy2 > ymax) { + oy2 = ymax; + } + + for (int m = -1; m < 3; m++) { + // get X coefficient + k2 = k1 * _bicubic_interp_kernel((double)m - dx); + + ox2 = ox1 + m; + if (ox2 < 0) { + ox2 = 0; + } + if (ox2 > xmax) { + ox2 = xmax; + } + + // get pixel of original image + const T *__restrict p = ((T *)p_src) + (oy2 * p_src_width + ox2) * CC; + + for (int i = 0; i < CC; i++) { + if (sizeof(T) == 2) { //half float + color[i] = Math::half_to_float(p[i]); + } else { + color[i] += p[i] * k2; + } + } + } + } + + for (int i = 0; i < CC; i++) { + if (sizeof(T) == 1) { //byte + dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255); + } else if (sizeof(T) == 2) { //half float + dst[i] = Math::make_half_float(color[i]); + } else { + dst[i] = color[i]; + } + } + } + } +} + +template <int CC, class T> +static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) { + enum { + FRAC_BITS = 8, + FRAC_LEN = (1 << FRAC_BITS), + FRAC_HALF = (FRAC_LEN >> 1), + FRAC_MASK = FRAC_LEN - 1 + }; + + for (uint32_t i = 0; i < p_dst_height; i++) { + // Add 0.5 in order to interpolate based on pixel center + uint32_t src_yofs_up_fp = (i + 0.5) * p_src_height * FRAC_LEN / p_dst_height; + // Calculate nearest src pixel center above current, and truncate to get y index + uint32_t src_yofs_up = src_yofs_up_fp >= FRAC_HALF ? (src_yofs_up_fp - FRAC_HALF) >> FRAC_BITS : 0; + uint32_t src_yofs_down = (src_yofs_up_fp + FRAC_HALF) >> FRAC_BITS; + if (src_yofs_down >= p_src_height) { + src_yofs_down = p_src_height - 1; + } + // Calculate distance to pixel center of src_yofs_up + uint32_t src_yofs_frac = src_yofs_up_fp & FRAC_MASK; + src_yofs_frac = src_yofs_frac >= FRAC_HALF ? src_yofs_frac - FRAC_HALF : src_yofs_frac + FRAC_HALF; + + uint32_t y_ofs_up = src_yofs_up * p_src_width * CC; + uint32_t y_ofs_down = src_yofs_down * p_src_width * CC; + + for (uint32_t j = 0; j < p_dst_width; j++) { + uint32_t src_xofs_left_fp = (j + 0.5) * p_src_width * FRAC_LEN / p_dst_width; + uint32_t src_xofs_left = src_xofs_left_fp >= FRAC_HALF ? (src_xofs_left_fp - FRAC_HALF) >> FRAC_BITS : 0; + uint32_t src_xofs_right = (src_xofs_left_fp + FRAC_HALF) >> FRAC_BITS; + if (src_xofs_right >= p_src_width) { + src_xofs_right = p_src_width - 1; + } + uint32_t src_xofs_frac = src_xofs_left_fp & FRAC_MASK; + src_xofs_frac = src_xofs_frac >= FRAC_HALF ? src_xofs_frac - FRAC_HALF : src_xofs_frac + FRAC_HALF; + + src_xofs_left *= CC; + src_xofs_right *= CC; + + for (uint32_t l = 0; l < CC; l++) { + if (sizeof(T) == 1) { //uint8 + uint32_t p00 = p_src[y_ofs_up + src_xofs_left + l] << FRAC_BITS; + uint32_t p10 = p_src[y_ofs_up + src_xofs_right + l] << FRAC_BITS; + uint32_t p01 = p_src[y_ofs_down + src_xofs_left + l] << FRAC_BITS; + uint32_t p11 = p_src[y_ofs_down + src_xofs_right + l] << FRAC_BITS; + + uint32_t interp_up = p00 + (((p10 - p00) * src_xofs_frac) >> FRAC_BITS); + uint32_t interp_down = p01 + (((p11 - p01) * src_xofs_frac) >> FRAC_BITS); + uint32_t interp = interp_up + (((interp_down - interp_up) * src_yofs_frac) >> FRAC_BITS); + interp >>= FRAC_BITS; + p_dst[i * p_dst_width * CC + j * CC + l] = interp; + } else if (sizeof(T) == 2) { //half float + + float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS); + float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS); + const T *src = ((const T *)p_src); + T *dst = ((T *)p_dst); + + float p00 = Math::half_to_float(src[y_ofs_up + src_xofs_left + l]); + float p10 = Math::half_to_float(src[y_ofs_up + src_xofs_right + l]); + float p01 = Math::half_to_float(src[y_ofs_down + src_xofs_left + l]); + float p11 = Math::half_to_float(src[y_ofs_down + src_xofs_right + l]); + + float interp_up = p00 + (p10 - p00) * xofs_frac; + float interp_down = p01 + (p11 - p01) * xofs_frac; + float interp = interp_up + ((interp_down - interp_up) * yofs_frac); + + dst[i * p_dst_width * CC + j * CC + l] = Math::make_half_float(interp); + } else if (sizeof(T) == 4) { //float + + float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS); + float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS); + const T *src = ((const T *)p_src); + T *dst = ((T *)p_dst); + + float p00 = src[y_ofs_up + src_xofs_left + l]; + float p10 = src[y_ofs_up + src_xofs_right + l]; + float p01 = src[y_ofs_down + src_xofs_left + l]; + float p11 = src[y_ofs_down + src_xofs_right + l]; + + float interp_up = p00 + (p10 - p00) * xofs_frac; + float interp_down = p01 + (p11 - p01) * xofs_frac; + float interp = interp_up + ((interp_down - interp_up) * yofs_frac); + + dst[i * p_dst_width * CC + j * CC + l] = interp; + } + } + } + } +} + +template <int CC, class T> +static void _scale_nearest(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) { + for (uint32_t i = 0; i < p_dst_height; i++) { + uint32_t src_yofs = i * p_src_height / p_dst_height; + uint32_t y_ofs = src_yofs * p_src_width * CC; + + for (uint32_t j = 0; j < p_dst_width; j++) { + uint32_t src_xofs = j * p_src_width / p_dst_width; + src_xofs *= CC; + + for (uint32_t l = 0; l < CC; l++) { + const T *src = ((const T *)p_src); + T *dst = ((T *)p_dst); + + T p = src[y_ofs + src_xofs + l]; + dst[i * p_dst_width * CC + j * CC + l] = p; + } + } + } +} + +#define LANCZOS_TYPE 3 + +static float _lanczos(float p_x) { + return Math::abs(p_x) >= LANCZOS_TYPE ? 0 : Math::sincn(p_x) * Math::sincn(p_x / LANCZOS_TYPE); +} + +template <int CC, class T> +static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, uint32_t p_src_width, uint32_t p_src_height, uint32_t p_dst_width, uint32_t p_dst_height) { + int32_t src_width = p_src_width; + int32_t src_height = p_src_height; + int32_t dst_height = p_dst_height; + int32_t dst_width = p_dst_width; + + uint32_t buffer_size = src_height * dst_width * CC; + float *buffer = memnew_arr(float, buffer_size); // Store the first pass in a buffer + + { // FIRST PASS (horizontal) + + float x_scale = float(src_width) / float(dst_width); + + float scale_factor = MAX(x_scale, 1); // A larger kernel is required only when downscaling + int32_t half_kernel = LANCZOS_TYPE * scale_factor; + + float *kernel = memnew_arr(float, half_kernel * 2); + + for (int32_t buffer_x = 0; buffer_x < dst_width; buffer_x++) { + // The corresponding point on the source image + float src_x = (buffer_x + 0.5f) * x_scale; // Offset by 0.5 so it uses the pixel's center + int32_t start_x = MAX(0, int32_t(src_x) - half_kernel + 1); + int32_t end_x = MIN(src_width - 1, int32_t(src_x) + half_kernel); + + // Create the kernel used by all the pixels of the column + for (int32_t target_x = start_x; target_x <= end_x; target_x++) { + kernel[target_x - start_x] = _lanczos((target_x + 0.5f - src_x) / scale_factor); + } + + for (int32_t buffer_y = 0; buffer_y < src_height; buffer_y++) { + float pixel[CC] = { 0 }; + float weight = 0; + + for (int32_t target_x = start_x; target_x <= end_x; target_x++) { + float lanczos_val = kernel[target_x - start_x]; + weight += lanczos_val; + + const T *__restrict src_data = ((const T *)p_src) + (buffer_y * src_width + target_x) * CC; + + for (uint32_t i = 0; i < CC; i++) { + if (sizeof(T) == 2) { //half float + pixel[i] += Math::half_to_float(src_data[i]) * lanczos_val; + } else { + pixel[i] += src_data[i] * lanczos_val; + } + } + } + + float *dst_data = ((float *)buffer) + (buffer_y * dst_width + buffer_x) * CC; + + for (uint32_t i = 0; i < CC; i++) { + dst_data[i] = pixel[i] / weight; // Normalize the sum of all the samples + } + } + } + + memdelete_arr(kernel); + } // End of first pass + + { // SECOND PASS (vertical + result) + + float y_scale = float(src_height) / float(dst_height); + + float scale_factor = MAX(y_scale, 1); + int32_t half_kernel = LANCZOS_TYPE * scale_factor; + + float *kernel = memnew_arr(float, half_kernel * 2); + + for (int32_t dst_y = 0; dst_y < dst_height; dst_y++) { + float buffer_y = (dst_y + 0.5f) * y_scale; + int32_t start_y = MAX(0, int32_t(buffer_y) - half_kernel + 1); + int32_t end_y = MIN(src_height - 1, int32_t(buffer_y) + half_kernel); + + for (int32_t target_y = start_y; target_y <= end_y; target_y++) { + kernel[target_y - start_y] = _lanczos((target_y + 0.5f - buffer_y) / scale_factor); + } + + for (int32_t dst_x = 0; dst_x < dst_width; dst_x++) { + float pixel[CC] = { 0 }; + float weight = 0; + + for (int32_t target_y = start_y; target_y <= end_y; target_y++) { + float lanczos_val = kernel[target_y - start_y]; + weight += lanczos_val; + + float *buffer_data = ((float *)buffer) + (target_y * dst_width + dst_x) * CC; + + for (uint32_t i = 0; i < CC; i++) { + pixel[i] += buffer_data[i] * lanczos_val; + } + } + + T *dst_data = ((T *)p_dst) + (dst_y * dst_width + dst_x) * CC; + + for (uint32_t i = 0; i < CC; i++) { + pixel[i] /= weight; + + if (sizeof(T) == 1) { //byte + dst_data[i] = CLAMP(Math::fast_ftoi(pixel[i]), 0, 255); + } else if (sizeof(T) == 2) { //half float + dst_data[i] = Math::make_half_float(pixel[i]); + } else { // float + dst_data[i] = pixel[i]; + } + } + } + } + + memdelete_arr(kernel); + } // End of second pass + + memdelete_arr(buffer); +} + +static void _overlay(const uint8_t *__restrict p_src, uint8_t *__restrict p_dst, float p_alpha, uint32_t p_width, uint32_t p_height, uint32_t p_pixel_size) { + uint16_t alpha = MIN((uint16_t)(p_alpha * 256.0f), 256); + + for (uint32_t i = 0; i < p_width * p_height * p_pixel_size; i++) { + p_dst[i] = (p_dst[i] * (256 - alpha) + p_src[i] * alpha) >> 8; + } +} + +bool Image::is_size_po2() const { + return uint32_t(width) == next_power_of_2(width) && uint32_t(height) == next_power_of_2(height); +} + +void Image::resize_to_po2(bool p_square, Interpolation p_interpolation) { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats."); + + int w = next_power_of_2(width); + int h = next_power_of_2(height); + if (p_square) { + w = h = MAX(w, h); + } + + if (w == width && h == height) { + if (!p_square || w == h) { + return; //nothing to do + } + } + + resize(w, h, p_interpolation); +} + +void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { + ERR_FAIL_COND_MSG(data.size() == 0, "Cannot resize image before creating it, use create() or create_from_data() first."); + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot resize in compressed or custom image formats."); + + bool mipmap_aware = p_interpolation == INTERPOLATE_TRILINEAR /* || p_interpolation == INTERPOLATE_TRICUBIC */; + + ERR_FAIL_COND_MSG(p_width <= 0, "Image width must be greater than 0."); + ERR_FAIL_COND_MSG(p_height <= 0, "Image height must be greater than 0."); + ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, "Image width cannot be greater than " + itos(MAX_WIDTH) + "."); + ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, "Image height cannot be greater than " + itos(MAX_HEIGHT) + "."); + ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS, "Too many pixels for image, maximum is " + itos(MAX_PIXELS)); + + if (p_width == width && p_height == height) { + return; + } + + Image dst(p_width, p_height, false, format); + + // Setup mipmap-aware scaling + Image dst2; + int mip1 = 0; + int mip2 = 0; + float mip1_weight = 0; + if (mipmap_aware) { + float avg_scale = ((float)p_width / width + (float)p_height / height) * 0.5f; + if (avg_scale >= 1.0f) { + mipmap_aware = false; + } else { + float level = Math::log(1.0f / avg_scale) / Math::log(2.0f); + mip1 = CLAMP((int)Math::floor(level), 0, get_mipmap_count()); + mip2 = CLAMP((int)Math::ceil(level), 0, get_mipmap_count()); + mip1_weight = 1.0f - (level - mip1); + } + } + bool interpolate_mipmaps = mipmap_aware && mip1 != mip2; + if (interpolate_mipmaps) { + dst2.create(p_width, p_height, false, format); + } + + bool had_mipmaps = mipmaps; + if (interpolate_mipmaps && !had_mipmaps) { + generate_mipmaps(); + } + // -- + + const uint8_t *r = data.ptr(); + const unsigned char *r_ptr = r; + + uint8_t *w = dst.data.ptrw(); + unsigned char *w_ptr = w; + + switch (p_interpolation) { + case INTERPOLATE_NEAREST: { + if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) { + switch (get_format_pixel_size(format)) { + case 1: + _scale_nearest<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 2: + _scale_nearest<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 3: + _scale_nearest<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_nearest<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) { + switch (get_format_pixel_size(format)) { + case 4: + _scale_nearest<1, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_nearest<2, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 12: + _scale_nearest<3, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 16: + _scale_nearest<4, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + + } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) { + switch (get_format_pixel_size(format)) { + case 2: + _scale_nearest<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_nearest<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 6: + _scale_nearest<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_nearest<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } + + } break; + case INTERPOLATE_BILINEAR: + case INTERPOLATE_TRILINEAR: { + for (int i = 0; i < 2; ++i) { + int src_width; + int src_height; + const unsigned char *src_ptr; + + if (!mipmap_aware) { + if (i == 0) { + // Standard behavior + src_width = width; + src_height = height; + src_ptr = r_ptr; + } else { + // No need for a second iteration + break; + } + } else { + if (i == 0) { + // Read from the first mipmap that will be interpolated + // (if both levels are the same, we will not interpolate, but at least we'll sample from the right level) + int offs; + _get_mipmap_offset_and_size(mip1, offs, src_width, src_height); + src_ptr = r_ptr + offs; + } else if (!interpolate_mipmaps) { + // No need generate a second image + break; + } else { + // Switch to read from the second mipmap that will be interpolated + int offs; + _get_mipmap_offset_and_size(mip2, offs, src_width, src_height); + src_ptr = r_ptr + offs; + // Switch to write to the second destination image + w = dst2.data.ptrw(); + w_ptr = w; + } + } + + if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) { + switch (get_format_pixel_size(format)) { + case 1: + _scale_bilinear<1, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 2: + _scale_bilinear<2, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 3: + _scale_bilinear<3, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 4: + _scale_bilinear<4, uint8_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) { + switch (get_format_pixel_size(format)) { + case 4: + _scale_bilinear<1, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 8: + _scale_bilinear<2, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 12: + _scale_bilinear<3, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 16: + _scale_bilinear<4, float>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) { + switch (get_format_pixel_size(format)) { + case 2: + _scale_bilinear<1, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 4: + _scale_bilinear<2, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 6: + _scale_bilinear<3, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + case 8: + _scale_bilinear<4, uint16_t>(src_ptr, w_ptr, src_width, src_height, p_width, p_height); + break; + } + } + } + + if (interpolate_mipmaps) { + // Switch to read again from the first scaled mipmap to overlay it over the second + r = dst.data.ptr(); + _overlay(r, w, mip1_weight, p_width, p_height, get_format_pixel_size(format)); + } + + } break; + case INTERPOLATE_CUBIC: { + if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) { + switch (get_format_pixel_size(format)) { + case 1: + _scale_cubic<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 2: + _scale_cubic<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 3: + _scale_cubic<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_cubic<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) { + switch (get_format_pixel_size(format)) { + case 4: + _scale_cubic<1, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_cubic<2, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 12: + _scale_cubic<3, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 16: + _scale_cubic<4, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) { + switch (get_format_pixel_size(format)) { + case 2: + _scale_cubic<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_cubic<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 6: + _scale_cubic<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_cubic<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } + } break; + case INTERPOLATE_LANCZOS: { + if (format >= FORMAT_L8 && format <= FORMAT_RGBA8) { + switch (get_format_pixel_size(format)) { + case 1: + _scale_lanczos<1, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 2: + _scale_lanczos<2, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 3: + _scale_lanczos<3, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_lanczos<4, uint8_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RF && format <= FORMAT_RGBAF) { + switch (get_format_pixel_size(format)) { + case 4: + _scale_lanczos<1, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_lanczos<2, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 12: + _scale_lanczos<3, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 16: + _scale_lanczos<4, float>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } else if (format >= FORMAT_RH && format <= FORMAT_RGBAH) { + switch (get_format_pixel_size(format)) { + case 2: + _scale_lanczos<1, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 4: + _scale_lanczos<2, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 6: + _scale_lanczos<3, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + case 8: + _scale_lanczos<4, uint16_t>(r_ptr, w_ptr, width, height, p_width, p_height); + break; + } + } + } break; + } + + if (interpolate_mipmaps) { + dst._copy_internals_from(dst2); + } + + if (had_mipmaps) { + dst.generate_mipmaps(); + } + + _copy_internals_from(dst); +} + +void Image::crop_from_point(int p_x, int p_y, int p_width, int p_height) { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot crop in compressed or custom image formats."); + + ERR_FAIL_COND_MSG(p_x < 0, "Start x position cannot be smaller than 0."); + ERR_FAIL_COND_MSG(p_y < 0, "Start y position cannot be smaller than 0."); + ERR_FAIL_COND_MSG(p_width <= 0, "Width of image must be greater than 0."); + ERR_FAIL_COND_MSG(p_height <= 0, "Height of image must be greater than 0."); + ERR_FAIL_COND_MSG(p_x + p_width > MAX_WIDTH, "End x position cannot be greater than " + itos(MAX_WIDTH) + "."); + ERR_FAIL_COND_MSG(p_y + p_height > MAX_HEIGHT, "End y position cannot be greater than " + itos(MAX_HEIGHT) + "."); + + /* to save memory, cropping should be done in-place, however, since this function + will most likely either not be used much, or in critical areas, for now it won't, because + it's a waste of time. */ + + if (p_width == width && p_height == height && p_x == 0 && p_y == 0) { + return; + } + + uint8_t pdata[16]; //largest is 16 + uint32_t pixel_size = get_format_pixel_size(format); + + Image dst(p_width, p_height, false, format); + + { + const uint8_t *r = data.ptr(); + uint8_t *w = dst.data.ptrw(); + + int m_h = p_y + p_height; + int m_w = p_x + p_width; + for (int y = p_y; y < m_h; y++) { + for (int x = p_x; x < m_w; x++) { + if ((x >= width || y >= height)) { + for (uint32_t i = 0; i < pixel_size; i++) { + pdata[i] = 0; + } + } else { + _get_pixelb(x, y, pixel_size, r, pdata); + } + + dst._put_pixelb(x - p_x, y - p_y, pixel_size, w, pdata); + } + } + } + + if (has_mipmaps()) { + dst.generate_mipmaps(); + } + _copy_internals_from(dst); +} + +void Image::crop(int p_width, int p_height) { + crop_from_point(0, 0, p_width, p_height); +} + +void Image::flip_y() { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot flip_y in compressed or custom image formats."); + + bool used_mipmaps = has_mipmaps(); + if (used_mipmaps) { + clear_mipmaps(); + } + + { + uint8_t *w = data.ptrw(); + uint8_t up[16]; + uint8_t down[16]; + uint32_t pixel_size = get_format_pixel_size(format); + + for (int y = 0; y < height / 2; y++) { + for (int x = 0; x < width; x++) { + _get_pixelb(x, y, pixel_size, w, up); + _get_pixelb(x, height - y - 1, pixel_size, w, down); + + _put_pixelb(x, height - y - 1, pixel_size, w, up); + _put_pixelb(x, y, pixel_size, w, down); + } + } + } + + if (used_mipmaps) { + generate_mipmaps(); + } +} + +void Image::flip_x() { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot flip_x in compressed or custom image formats."); + + bool used_mipmaps = has_mipmaps(); + if (used_mipmaps) { + clear_mipmaps(); + } + + { + uint8_t *w = data.ptrw(); + uint8_t up[16]; + uint8_t down[16]; + uint32_t pixel_size = get_format_pixel_size(format); + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width / 2; x++) { + _get_pixelb(x, y, pixel_size, w, up); + _get_pixelb(width - x - 1, y, pixel_size, w, down); + + _put_pixelb(width - x - 1, y, pixel_size, w, up); + _put_pixelb(x, y, pixel_size, w, down); + } + } + } + + if (used_mipmaps) { + generate_mipmaps(); + } +} + +int Image::_get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps, int *r_mm_width, int *r_mm_height) { + int size = 0; + int w = p_width; + int h = p_height; + int mm = 0; + + int pixsize = get_format_pixel_size(p_format); + int pixshift = get_format_pixel_rshift(p_format); + int block = get_format_block_size(p_format); + //technically, you can still compress up to 1 px no matter the format, so commenting this + //int minw, minh; + //get_format_min_pixel_size(p_format, minw, minh); + int minw = 1, minh = 1; + + while (true) { + int bw = w % block != 0 ? w + (block - w % block) : w; + int bh = h % block != 0 ? h + (block - h % block) : h; + + int s = bw * bh; + + s *= pixsize; + s >>= pixshift; + + size += s; + + if (r_mm_width) { + *r_mm_width = bw; + } + if (r_mm_height) { + *r_mm_height = bh; + } + + if (p_mipmaps >= 0 && mm == p_mipmaps) { + break; + } + + if (p_mipmaps >= 0) { + w = MAX(minw, w >> 1); + h = MAX(minh, h >> 1); + } else { + if (w == minw && h == minh) { + break; + } + w = MAX(minw, w >> 1); + h = MAX(minh, h >> 1); + } + mm++; + } + + r_mipmaps = mm; + return size; +} + +bool Image::_can_modify(Format p_format) const { + return p_format <= FORMAT_RGBE9995; +} + +template <class Component, int CC, bool renormalize, + void (*average_func)(Component &, const Component &, const Component &, const Component &, const Component &), + void (*renormalize_func)(Component *)> +static void _generate_po2_mipmap(const Component *p_src, Component *p_dst, uint32_t p_width, uint32_t p_height) { + //fast power of 2 mipmap generation + uint32_t dst_w = MAX(p_width >> 1, 1); + uint32_t dst_h = MAX(p_height >> 1, 1); + + int right_step = (p_width == 1) ? 0 : CC; + int down_step = (p_height == 1) ? 0 : (p_width * CC); + + for (uint32_t i = 0; i < dst_h; i++) { + const Component *rup_ptr = &p_src[i * 2 * down_step]; + const Component *rdown_ptr = rup_ptr + down_step; + Component *dst_ptr = &p_dst[i * dst_w * CC]; + uint32_t count = dst_w; + + while (count) { + count--; + for (int j = 0; j < CC; j++) { + average_func(dst_ptr[j], rup_ptr[j], rup_ptr[j + right_step], rdown_ptr[j], rdown_ptr[j + right_step]); + } + + if (renormalize) { + renormalize_func(dst_ptr); + } + + dst_ptr += CC; + rup_ptr += right_step * 2; + rdown_ptr += right_step * 2; + } + } +} + +void Image::shrink_x2() { + ERR_FAIL_COND(data.size() == 0); + + if (mipmaps) { + //just use the lower mipmap as base and copy all + Vector<uint8_t> new_img; + + int ofs = get_mipmap_offset(1); + + int new_size = data.size() - ofs; + new_img.resize(new_size); + ERR_FAIL_COND(new_img.size() == 0); + + { + uint8_t *w = new_img.ptrw(); + const uint8_t *r = data.ptr(); + + copymem(w, &r[ofs], new_size); + } + + width = MAX(width / 2, 1); + height = MAX(height / 2, 1); + data = new_img; + + } else { + Vector<uint8_t> new_img; + + ERR_FAIL_COND(!_can_modify(format)); + int ps = get_format_pixel_size(format); + new_img.resize((width / 2) * (height / 2) * ps); + ERR_FAIL_COND(new_img.size() == 0); + ERR_FAIL_COND(data.size() == 0); + + { + uint8_t *w = new_img.ptrw(); + const uint8_t *r = data.ptr(); + + switch (format) { + case FORMAT_L8: + case FORMAT_R8: + _generate_po2_mipmap<uint8_t, 1, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height); + break; + case FORMAT_LA8: + _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height); + break; + case FORMAT_RG8: + _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height); + break; + case FORMAT_RGB8: + _generate_po2_mipmap<uint8_t, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height); + break; + case FORMAT_RGBA8: + _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(r, w, width, height); + break; + + case FORMAT_RF: + _generate_po2_mipmap<float, 1, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height); + break; + case FORMAT_RGF: + _generate_po2_mipmap<float, 2, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height); + break; + case FORMAT_RGBF: + _generate_po2_mipmap<float, 3, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height); + break; + case FORMAT_RGBAF: + _generate_po2_mipmap<float, 4, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(r), reinterpret_cast<float *>(w), width, height); + break; + + case FORMAT_RH: + _generate_po2_mipmap<uint16_t, 1, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r), reinterpret_cast<uint16_t *>(w), width, height); + break; + case FORMAT_RGH: + _generate_po2_mipmap<uint16_t, 2, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r), reinterpret_cast<uint16_t *>(w), width, height); + break; + case FORMAT_RGBH: + _generate_po2_mipmap<uint16_t, 3, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r), reinterpret_cast<uint16_t *>(w), width, height); + break; + case FORMAT_RGBAH: + _generate_po2_mipmap<uint16_t, 4, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(r), reinterpret_cast<uint16_t *>(w), width, height); + break; + + case FORMAT_RGBE9995: + _generate_po2_mipmap<uint32_t, 1, false, Image::average_4_rgbe9995, Image::renormalize_rgbe9995>(reinterpret_cast<const uint32_t *>(r), reinterpret_cast<uint32_t *>(w), width, height); + break; + default: { + } + } + } + + width /= 2; + height /= 2; + data = new_img; + } +} + +void Image::normalize() { + bool used_mipmaps = has_mipmaps(); + if (used_mipmaps) { + clear_mipmaps(); + } + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + Color c = get_pixel(x, y); + Vector3 v(c.r * 2.0 - 1.0, c.g * 2.0 - 1.0, c.b * 2.0 - 1.0); + v.normalize(); + c.r = v.x * 0.5 + 0.5; + c.g = v.y * 0.5 + 0.5; + c.b = v.z * 0.5 + 0.5; + set_pixel(x, y, c); + } + } + + if (used_mipmaps) { + generate_mipmaps(true); + } +} + +Error Image::generate_mipmaps(bool p_renormalize) { + ERR_FAIL_COND_V_MSG(!_can_modify(format), ERR_UNAVAILABLE, "Cannot generate mipmaps in compressed or custom image formats."); + + ERR_FAIL_COND_V_MSG(format == FORMAT_RGBA4444, ERR_UNAVAILABLE, "Cannot generate mipmaps from RGBA4444 format."); + + ERR_FAIL_COND_V_MSG(width == 0 || height == 0, ERR_UNCONFIGURED, "Cannot generate mipmaps with width or height equal to 0."); + + int mmcount; + + int size = _get_dst_image_size(width, height, format, mmcount); + + data.resize(size); + + uint8_t *wp = data.ptrw(); + + int prev_ofs = 0; + int prev_h = height; + int prev_w = width; + + for (int i = 1; i <= mmcount; i++) { + int ofs, w, h; + _get_mipmap_offset_and_size(i, ofs, w, h); + + switch (format) { + case FORMAT_L8: + case FORMAT_R8: + _generate_po2_mipmap<uint8_t, 1, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + break; + case FORMAT_LA8: + case FORMAT_RG8: + _generate_po2_mipmap<uint8_t, 2, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + break; + case FORMAT_RGB8: + if (p_renormalize) { + _generate_po2_mipmap<uint8_t, 3, true, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + } else { + _generate_po2_mipmap<uint8_t, 3, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + } + + break; + case FORMAT_RGBA8: + if (p_renormalize) { + _generate_po2_mipmap<uint8_t, 4, true, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + } else { + _generate_po2_mipmap<uint8_t, 4, false, Image::average_4_uint8, Image::renormalize_uint8>(&wp[prev_ofs], &wp[ofs], prev_w, prev_h); + } + break; + case FORMAT_RF: + _generate_po2_mipmap<float, 1, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + break; + case FORMAT_RGF: + _generate_po2_mipmap<float, 2, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + break; + case FORMAT_RGBF: + if (p_renormalize) { + _generate_po2_mipmap<float, 3, true, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + } else { + _generate_po2_mipmap<float, 3, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + } + + break; + case FORMAT_RGBAF: + if (p_renormalize) { + _generate_po2_mipmap<float, 4, true, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + } else { + _generate_po2_mipmap<float, 4, false, Image::average_4_float, Image::renormalize_float>(reinterpret_cast<const float *>(&wp[prev_ofs]), reinterpret_cast<float *>(&wp[ofs]), prev_w, prev_h); + } + + break; + case FORMAT_RH: + _generate_po2_mipmap<uint16_t, 1, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + break; + case FORMAT_RGH: + _generate_po2_mipmap<uint16_t, 2, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + break; + case FORMAT_RGBH: + if (p_renormalize) { + _generate_po2_mipmap<uint16_t, 3, true, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + } else { + _generate_po2_mipmap<uint16_t, 3, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + } + + break; + case FORMAT_RGBAH: + if (p_renormalize) { + _generate_po2_mipmap<uint16_t, 4, true, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + } else { + _generate_po2_mipmap<uint16_t, 4, false, Image::average_4_half, Image::renormalize_half>(reinterpret_cast<const uint16_t *>(&wp[prev_ofs]), reinterpret_cast<uint16_t *>(&wp[ofs]), prev_w, prev_h); + } + + break; + case FORMAT_RGBE9995: + if (p_renormalize) { + _generate_po2_mipmap<uint32_t, 1, true, Image::average_4_rgbe9995, Image::renormalize_rgbe9995>(reinterpret_cast<const uint32_t *>(&wp[prev_ofs]), reinterpret_cast<uint32_t *>(&wp[ofs]), prev_w, prev_h); + } else { + _generate_po2_mipmap<uint32_t, 1, false, Image::average_4_rgbe9995, Image::renormalize_rgbe9995>(reinterpret_cast<const uint32_t *>(&wp[prev_ofs]), reinterpret_cast<uint32_t *>(&wp[ofs]), prev_w, prev_h); + } + + break; + default: { + } + } + + prev_ofs = ofs; + prev_w = w; + prev_h = h; + } + + mipmaps = true; + + return OK; +} + +Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, const Ref<Image> &p_normal_map) { + Vector<double> normal_sat_vec; //summed area table + double *normal_sat = nullptr; //summed area table for normal map + int normal_w = 0, normal_h = 0; + + ERR_FAIL_COND_V_MSG(p_normal_map.is_null() || p_normal_map->is_empty(), ERR_INVALID_PARAMETER, "Must provide a valid normal map for roughness mipmaps"); + + Ref<Image> nm = p_normal_map->duplicate(); + if (nm->is_compressed()) { + nm->decompress(); + } + + normal_w = nm->get_width(); + normal_h = nm->get_height(); + + normal_sat_vec.resize(normal_w * normal_h * 3); + + normal_sat = normal_sat_vec.ptrw(); + + //create summed area table + + for (int y = 0; y < normal_h; y++) { + double line_sum[3] = { 0, 0, 0 }; + for (int x = 0; x < normal_w; x++) { + double normal[3]; + Color color = nm->get_pixel(x, y); + normal[0] = color.r * 2.0 - 1.0; + normal[1] = color.g * 2.0 - 1.0; + normal[2] = Math::sqrt(MAX(0.0, 1.0 - (normal[0] * normal[0] + normal[1] * normal[1]))); //reconstruct if missing + + line_sum[0] += normal[0]; + line_sum[1] += normal[1]; + line_sum[2] += normal[2]; + + uint32_t ofs = (y * normal_w + x) * 3; + + normal_sat[ofs + 0] = line_sum[0]; + normal_sat[ofs + 1] = line_sum[1]; + normal_sat[ofs + 2] = line_sum[2]; + + if (y > 0) { + uint32_t prev_ofs = ((y - 1) * normal_w + x) * 3; + normal_sat[ofs + 0] += normal_sat[prev_ofs + 0]; + normal_sat[ofs + 1] += normal_sat[prev_ofs + 1]; + normal_sat[ofs + 2] += normal_sat[prev_ofs + 2]; + } + } + } + +#if 0 + { + Vector3 beg(normal_sat_vec[0], normal_sat_vec[1], normal_sat_vec[2]); + Vector3 end(normal_sat_vec[normal_sat_vec.size() - 3], normal_sat_vec[normal_sat_vec.size() - 2], normal_sat_vec[normal_sat_vec.size() - 1]); + Vector3 avg = (end - beg) / (normal_w * normal_h); + print_line("average: " + avg); + } +#endif + + int mmcount; + + _get_dst_image_size(width, height, format, mmcount); + + uint8_t *base_ptr = data.ptrw(); + + for (int i = 1; i <= mmcount; i++) { + int ofs, w, h; + _get_mipmap_offset_and_size(i, ofs, w, h); + uint8_t *ptr = &base_ptr[ofs]; + + for (int x = 0; x < w; x++) { + for (int y = 0; y < h; y++) { + int from_x = x * normal_w / w; + int from_y = y * normal_h / h; + int to_x = (x + 1) * normal_w / w; + int to_y = (y + 1) * normal_h / h; + to_x = MIN(to_x - 1, normal_w); + to_y = MIN(to_y - 1, normal_h); + + int size_x = (to_x - from_x) + 1; + int size_y = (to_y - from_y) + 1; + + //summed area table version (much faster) + + double avg[3] = { 0, 0, 0 }; + + if (from_x > 0 && from_y > 0) { + uint32_t tofs = ((from_y - 1) * normal_w + (from_x - 1)) * 3; + avg[0] += normal_sat[tofs + 0]; + avg[1] += normal_sat[tofs + 1]; + avg[2] += normal_sat[tofs + 2]; + } + + if (from_y > 0) { + uint32_t tofs = ((from_y - 1) * normal_w + to_x) * 3; + avg[0] -= normal_sat[tofs + 0]; + avg[1] -= normal_sat[tofs + 1]; + avg[2] -= normal_sat[tofs + 2]; + } + + if (from_x > 0) { + uint32_t tofs = (to_y * normal_w + (from_x - 1)) * 3; + avg[0] -= normal_sat[tofs + 0]; + avg[1] -= normal_sat[tofs + 1]; + avg[2] -= normal_sat[tofs + 2]; + } + + uint32_t tofs = (to_y * normal_w + to_x) * 3; + avg[0] += normal_sat[tofs + 0]; + avg[1] += normal_sat[tofs + 1]; + avg[2] += normal_sat[tofs + 2]; + + double div = double(size_x * size_y); + Vector3 vec(avg[0] / div, avg[1] / div, avg[2] / div); + + float r = vec.length(); + + int pixel_ofs = y * w + x; + Color c = _get_color_at_ofs(ptr, pixel_ofs); + + float roughness = 0; + + switch (p_roughness_channel) { + case ROUGHNESS_CHANNEL_R: { + roughness = c.r; + } break; + case ROUGHNESS_CHANNEL_G: { + roughness = c.g; + } break; + case ROUGHNESS_CHANNEL_B: { + roughness = c.b; + } break; + case ROUGHNESS_CHANNEL_L: { + roughness = c.get_v(); + } break; + case ROUGHNESS_CHANNEL_A: { + roughness = c.a; + } break; + } + + float variance = 0; + if (r < 1.0f) { + float r2 = r * r; + float kappa = (3.0f * r - r * r2) / (1.0f - r2); + variance = 0.25f / kappa; + } + + float threshold = 0.4; + roughness = Math::sqrt(roughness * roughness + MIN(3.0f * variance, threshold * threshold)); + + switch (p_roughness_channel) { + case ROUGHNESS_CHANNEL_R: { + c.r = roughness; + } break; + case ROUGHNESS_CHANNEL_G: { + c.g = roughness; + } break; + case ROUGHNESS_CHANNEL_B: { + c.b = roughness; + } break; + case ROUGHNESS_CHANNEL_L: { + c.r = roughness; + c.g = roughness; + c.b = roughness; + } break; + case ROUGHNESS_CHANNEL_A: { + c.a = roughness; + } break; + } + + _set_color_at_ofs(ptr, pixel_ofs, c); + } + } +#if 0 + { + int size = get_mipmap_byte_size(i); + print_line("size for mimpap " + itos(i) + ": " + itos(size)); + Vector<uint8_t> imgdata; + imgdata.resize(size); + + + uint8_t* wr = imgdata.ptrw(); + copymem(wr.ptr(), ptr, size); + wr = uint8_t*(); + Ref<Image> im; + im.instance(); + im->create(w, h, false, format, imgdata); + im->save_png("res://mipmap_" + itos(i) + ".png"); + } +#endif + } + + return OK; +} + +void Image::clear_mipmaps() { + if (!mipmaps) { + return; + } + + if (is_empty()) { + return; + } + + int ofs, w, h; + _get_mipmap_offset_and_size(1, ofs, w, h); + data.resize(ofs); + + mipmaps = false; +} + +bool Image::is_empty() const { + return (data.size() == 0); +} + +Vector<uint8_t> Image::get_data() const { + return data; +} + +void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { + ERR_FAIL_COND_MSG(p_width <= 0, "Image width must be greater than 0."); + ERR_FAIL_COND_MSG(p_height <= 0, "Image height must be greater than 0."); + ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, "Image width cannot be greater than " + itos(MAX_WIDTH) + "."); + ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, "Image height cannot be greater than " + itos(MAX_HEIGHT) + "."); + ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS, "Too many pixels for image, maximum is " + itos(MAX_PIXELS)); + + int mm = 0; + int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0); + data.resize(size); + + { + uint8_t *w = data.ptrw(); + zeromem(w, size); + } + + width = p_width; + height = p_height; + mipmaps = p_use_mipmaps; + format = p_format; +} + +void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + ERR_FAIL_COND_MSG(p_width <= 0, "Image width must be greater than 0."); + ERR_FAIL_COND_MSG(p_height <= 0, "Image height must be greater than 0."); + ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, "Image width cannot be greater than " + itos(MAX_WIDTH) + "."); + ERR_FAIL_COND_MSG(p_height > MAX_HEIGHT, "Image height cannot be greater than " + itos(MAX_HEIGHT) + "."); + ERR_FAIL_COND_MSG(p_width * p_height > MAX_PIXELS, "Too many pixels for image, maximum is " + itos(MAX_PIXELS)); + + int mm; + int size = _get_dst_image_size(p_width, p_height, p_format, mm, p_use_mipmaps ? -1 : 0); + + ERR_FAIL_COND_MSG(p_data.size() != size, "Expected data size of " + itos(size) + " bytes in Image::create(), got instead " + itos(p_data.size()) + " bytes."); + + height = p_height; + width = p_width; + format = p_format; + data = p_data; + + mipmaps = p_use_mipmaps; +} + +void Image::create(const char **p_xpm) { + int size_width = 0; + int size_height = 0; + int pixelchars = 0; + mipmaps = false; + bool has_alpha = false; + + enum Status { + READING_HEADER, + READING_COLORS, + READING_PIXELS, + DONE + }; + + Status status = READING_HEADER; + int line = 0; + + HashMap<String, Color> colormap; + int colormap_size = 0; + uint32_t pixel_size = 0; + uint8_t *data_write = nullptr; + + while (status != DONE) { + const char *line_ptr = p_xpm[line]; + + switch (status) { + case READING_HEADER: { + String line_str = line_ptr; + line_str.replace("\t", " "); + + size_width = line_str.get_slicec(' ', 0).to_int(); + size_height = line_str.get_slicec(' ', 1).to_int(); + colormap_size = line_str.get_slicec(' ', 2).to_int(); + pixelchars = line_str.get_slicec(' ', 3).to_int(); + ERR_FAIL_COND(colormap_size > 32766); + ERR_FAIL_COND(pixelchars > 5); + ERR_FAIL_COND(size_width > 32767); + ERR_FAIL_COND(size_height > 32767); + status = READING_COLORS; + } break; + case READING_COLORS: { + String colorstring; + for (int i = 0; i < pixelchars; i++) { + colorstring += *line_ptr; + line_ptr++; + } + //skip spaces + while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) { + if (*line_ptr == 0) { + break; + } + line_ptr++; + } + if (*line_ptr == 'c') { + line_ptr++; + while (*line_ptr == ' ' || *line_ptr == '\t' || *line_ptr == 0) { + if (*line_ptr == 0) { + break; + } + line_ptr++; + } + + if (*line_ptr == '#') { + line_ptr++; + uint8_t col_r = 0; + uint8_t col_g = 0; + uint8_t col_b = 0; + //uint8_t col_a=255; + + for (int i = 0; i < 6; i++) { + char v = line_ptr[i]; + + if (v >= '0' && v <= '9') { + v -= '0'; + } else if (v >= 'A' && v <= 'F') { + v = (v - 'A') + 10; + } else if (v >= 'a' && v <= 'f') { + v = (v - 'a') + 10; + } else { + break; + } + + switch (i) { + case 0: + col_r = v << 4; + break; + case 1: + col_r |= v; + break; + case 2: + col_g = v << 4; + break; + case 3: + col_g |= v; + break; + case 4: + col_b = v << 4; + break; + case 5: + col_b |= v; + break; + } + } + + // magenta mask + if (col_r == 255 && col_g == 0 && col_b == 255) { + colormap[colorstring] = Color(0, 0, 0, 0); + has_alpha = true; + } else { + colormap[colorstring] = Color(col_r / 255.0, col_g / 255.0, col_b / 255.0, 1.0); + } + } + } + if (line == colormap_size) { + status = READING_PIXELS; + create(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8); + data_write = data.ptrw(); + pixel_size = has_alpha ? 4 : 3; + } + } break; + case READING_PIXELS: { + int y = line - colormap_size - 1; + for (int x = 0; x < size_width; x++) { + char pixelstr[6] = { 0, 0, 0, 0, 0, 0 }; + for (int i = 0; i < pixelchars; i++) { + pixelstr[i] = line_ptr[x * pixelchars + i]; + } + + Color *colorptr = colormap.getptr(pixelstr); + ERR_FAIL_COND(!colorptr); + uint8_t pixel[4]; + for (uint32_t i = 0; i < pixel_size; i++) { + pixel[i] = CLAMP((*colorptr)[i] * 255, 0, 255); + } + _put_pixelb(x, y, pixel_size, data_write, pixel); + } + + if (y == (size_height - 1)) { + status = DONE; + } + } break; + default: { + } + } + + line++; + } +} +#define DETECT_ALPHA_MAX_THRESHOLD 254 +#define DETECT_ALPHA_MIN_THRESHOLD 2 + +#define DETECT_ALPHA(m_value) \ + { \ + uint8_t value = m_value; \ + if (value < DETECT_ALPHA_MIN_THRESHOLD) \ + bit = true; \ + else if (value < DETECT_ALPHA_MAX_THRESHOLD) { \ + detected = true; \ + break; \ + } \ + } + +#define DETECT_NON_ALPHA(m_value) \ + { \ + uint8_t value = m_value; \ + if (value > 0) { \ + detected = true; \ + break; \ + } \ + } + +bool Image::is_invisible() const { + if (format == FORMAT_L8 || + format == FORMAT_RGB8 || format == FORMAT_RG8) { + return false; + } + + int len = data.size(); + + if (len == 0) { + return true; + } + + int w, h; + _get_mipmap_offset_and_size(1, len, w, h); + + const uint8_t *r = data.ptr(); + const unsigned char *data_ptr = r; + + bool detected = false; + + switch (format) { + case FORMAT_LA8: { + for (int i = 0; i < (len >> 1); i++) { + DETECT_NON_ALPHA(data_ptr[(i << 1) + 1]); + } + + } break; + case FORMAT_RGBA8: { + for (int i = 0; i < (len >> 2); i++) { + DETECT_NON_ALPHA(data_ptr[(i << 2) + 3]) + } + + } break; + + case FORMAT_PVRTC1_2A: + case FORMAT_PVRTC1_4A: + case FORMAT_DXT3: + case FORMAT_DXT5: { + detected = true; + } break; + default: { + } + } + + return !detected; +} + +Image::AlphaMode Image::detect_alpha() const { + int len = data.size(); + + if (len == 0) { + return ALPHA_NONE; + } + + int w, h; + _get_mipmap_offset_and_size(1, len, w, h); + + const uint8_t *r = data.ptr(); + const unsigned char *data_ptr = r; + + bool bit = false; + bool detected = false; + + switch (format) { + case FORMAT_LA8: { + for (int i = 0; i < (len >> 1); i++) { + DETECT_ALPHA(data_ptr[(i << 1) + 1]); + } + + } break; + case FORMAT_RGBA8: { + for (int i = 0; i < (len >> 2); i++) { + DETECT_ALPHA(data_ptr[(i << 2) + 3]) + } + + } break; + case FORMAT_PVRTC1_2A: + case FORMAT_PVRTC1_4A: + case FORMAT_DXT3: + case FORMAT_DXT5: { + detected = true; + } break; + default: { + } + } + + if (detected) { + return ALPHA_BLEND; + } else if (bit) { + return ALPHA_BIT; + } else { + return ALPHA_NONE; + } +} + +Error Image::load(const String &p_path) { +#ifdef DEBUG_ENABLED + if (p_path.begins_with("res://") && ResourceLoader::exists(p_path)) { + WARN_PRINT("Loaded resource as image file, this will not work on export: '" + p_path + "'. Instead, import the image file as an Image resource and load it normally as a resource."); + } +#endif + return ImageLoader::load_image(p_path, this); +} + +Error Image::save_png(const String &p_path) const { + if (save_png_func == nullptr) { + return ERR_UNAVAILABLE; + } + + return save_png_func(p_path, Ref<Image>((Image *)this)); +} + +Vector<uint8_t> Image::save_png_to_buffer() const { + if (save_png_buffer_func == nullptr) { + return Vector<uint8_t>(); + } + + return save_png_buffer_func(Ref<Image>((Image *)this)); +} + +Error Image::save_exr(const String &p_path, bool p_grayscale) const { + if (save_exr_func == nullptr) { + return ERR_UNAVAILABLE; + } + + return save_exr_func(p_path, Ref<Image>((Image *)this), p_grayscale); +} + +int Image::get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps) { + int mm; + return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmaps ? -1 : 0); +} + +int Image::get_image_required_mipmaps(int p_width, int p_height, Format p_format) { + int mm; + _get_dst_image_size(p_width, p_height, p_format, mm, -1); + return mm; +} + +Size2i Image::get_image_mipmap_size(int p_width, int p_height, Format p_format, int p_mipmap) { + int mm; + Size2i ret; + _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap, &ret.x, &ret.y); + return ret; +} + +int Image::get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap) { + if (p_mipmap <= 0) { + return 0; + } + int mm; + return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap - 1); +} + +int Image::get_image_mipmap_offset_and_dimensions(int p_width, int p_height, Format p_format, int p_mipmap, int &r_w, int &r_h) { + if (p_mipmap <= 0) { + r_w = p_width; + r_h = p_height; + return 0; + } + int mm; + return _get_dst_image_size(p_width, p_height, p_format, mm, p_mipmap - 1, &r_w, &r_h); +} + +bool Image::is_compressed() const { + return format > FORMAT_RGBE9995; +} + +Error Image::decompress() { + if (((format >= FORMAT_DXT1 && format <= FORMAT_RGTC_RG) || (format == FORMAT_DXT5_RA_AS_RG)) && _image_decompress_bc) { + _image_decompress_bc(this); + } else if (format >= FORMAT_BPTC_RGBA && format <= FORMAT_BPTC_RGBFU && _image_decompress_bptc) { + _image_decompress_bptc(this); + } else if (format >= FORMAT_PVRTC1_2 && format <= FORMAT_PVRTC1_4A && _image_decompress_pvrtc) { + _image_decompress_pvrtc(this); + } else if (format == FORMAT_ETC && _image_decompress_etc1) { + _image_decompress_etc1(this); + } else if (format >= FORMAT_ETC2_R11 && format <= FORMAT_ETC2_RA_AS_RG && _image_decompress_etc2) { + _image_decompress_etc2(this); + } else { + return ERR_UNAVAILABLE; + } + return OK; +} + +Error Image::compress(CompressMode p_mode, CompressSource p_source, float p_lossy_quality) { + return compress_from_channels(p_mode, detect_used_channels(p_source), p_lossy_quality); +} + +Error Image::compress_from_channels(CompressMode p_mode, UsedChannels p_channels, float p_lossy_quality) { + switch (p_mode) { + case COMPRESS_S3TC: { + ERR_FAIL_COND_V(!_image_compress_bc_func, ERR_UNAVAILABLE); + _image_compress_bc_func(this, p_lossy_quality, p_channels); + } break; + case COMPRESS_PVRTC1_4: { + ERR_FAIL_COND_V(!_image_compress_pvrtc1_4bpp_func, ERR_UNAVAILABLE); + _image_compress_pvrtc1_4bpp_func(this); + } break; + case COMPRESS_ETC: { + ERR_FAIL_COND_V(!_image_compress_etc1_func, ERR_UNAVAILABLE); + _image_compress_etc1_func(this, p_lossy_quality); + } break; + case COMPRESS_ETC2: { + ERR_FAIL_COND_V(!_image_compress_etc2_func, ERR_UNAVAILABLE); + _image_compress_etc2_func(this, p_lossy_quality, p_channels); + } break; + case COMPRESS_BPTC: { + ERR_FAIL_COND_V(!_image_compress_bptc_func, ERR_UNAVAILABLE); + _image_compress_bptc_func(this, p_lossy_quality, p_channels); + } break; + } + + return OK; +} + +Image::Image(const char **p_xpm) { + width = 0; + height = 0; + mipmaps = false; + format = FORMAT_L8; + + create(p_xpm); +} + +Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { + width = 0; + height = 0; + mipmaps = p_use_mipmaps; + format = FORMAT_L8; + + create(p_width, p_height, p_use_mipmaps, p_format); +} + +Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + width = 0; + height = 0; + mipmaps = p_mipmaps; + format = FORMAT_L8; + + create(p_width, p_height, p_mipmaps, p_format, p_data); +} + +Rect2 Image::get_used_rect() const { + if (format != FORMAT_LA8 && format != FORMAT_RGBA8 && format != FORMAT_RGBAF && format != FORMAT_RGBAH && format != FORMAT_RGBA4444 && format != FORMAT_RGB565) { + return Rect2(Point2(), Size2(width, height)); + } + + int len = data.size(); + + if (len == 0) { + return Rect2(); + } + + int minx = 0xFFFFFF, miny = 0xFFFFFFF; + int maxx = -1, maxy = -1; + for (int j = 0; j < height; j++) { + for (int i = 0; i < width; i++) { + if (!(get_pixel(i, j).a > 0)) { + continue; + } + if (i > maxx) { + maxx = i; + } + if (j > maxy) { + maxy = j; + } + if (i < minx) { + minx = i; + } + if (j < miny) { + miny = j; + } + } + } + + if (maxx == -1) { + return Rect2(); + } else { + return Rect2(minx, miny, maxx - minx + 1, maxy - miny + 1); + } +} + +Ref<Image> Image::get_rect(const Rect2 &p_area) const { + Ref<Image> img = memnew(Image(p_area.size.x, p_area.size.y, mipmaps, format)); + img->blit_rect(Ref<Image>((Image *)this), p_area, Point2(0, 0)); + return img; +} + +void Image::blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) { + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); + int dsize = data.size(); + int srcdsize = p_src->data.size(); + ERR_FAIL_COND(dsize == 0); + ERR_FAIL_COND(srcdsize == 0); + ERR_FAIL_COND(format != p_src->format); + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot blit_rect in compressed or custom image formats."); + + Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect); + + if (p_dest.x < 0) { + clipped_src_rect.position.x = ABS(p_dest.x); + } + if (p_dest.y < 0) { + clipped_src_rect.position.y = ABS(p_dest.y); + } + + if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0) { + return; + } + + Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y)); + Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size)); + + uint8_t *wp = data.ptrw(); + uint8_t *dst_data_ptr = wp; + + const uint8_t *rp = p_src->data.ptr(); + const uint8_t *src_data_ptr = rp; + + int pixel_size = get_format_pixel_size(format); + + for (int i = 0; i < dest_rect.size.y; i++) { + for (int j = 0; j < dest_rect.size.x; j++) { + int src_x = clipped_src_rect.position.x + j; + int src_y = clipped_src_rect.position.y + i; + + int dst_x = dest_rect.position.x + j; + int dst_y = dest_rect.position.y + i; + + const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size]; + uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size]; + + for (int k = 0; k < pixel_size; k++) { + dst[k] = src[k]; + } + } + } +} + +void Image::blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) { + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); + ERR_FAIL_COND_MSG(p_mask.is_null(), "It's not a reference to a valid Image object."); + int dsize = data.size(); + int srcdsize = p_src->data.size(); + int maskdsize = p_mask->data.size(); + ERR_FAIL_COND(dsize == 0); + ERR_FAIL_COND(srcdsize == 0); + ERR_FAIL_COND(maskdsize == 0); + ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width."); + ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height."); + ERR_FAIL_COND(format != p_src->format); + + Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect); + + if (p_dest.x < 0) { + clipped_src_rect.position.x = ABS(p_dest.x); + } + if (p_dest.y < 0) { + clipped_src_rect.position.y = ABS(p_dest.y); + } + + if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0) { + return; + } + + Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y)); + Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size)); + + uint8_t *wp = data.ptrw(); + uint8_t *dst_data_ptr = wp; + + const uint8_t *rp = p_src->data.ptr(); + const uint8_t *src_data_ptr = rp; + + int pixel_size = get_format_pixel_size(format); + + Ref<Image> msk = p_mask; + + for (int i = 0; i < dest_rect.size.y; i++) { + for (int j = 0; j < dest_rect.size.x; j++) { + int src_x = clipped_src_rect.position.x + j; + int src_y = clipped_src_rect.position.y + i; + + if (msk->get_pixel(src_x, src_y).a != 0) { + int dst_x = dest_rect.position.x + j; + int dst_y = dest_rect.position.y + i; + + const uint8_t *src = &src_data_ptr[(src_y * p_src->width + src_x) * pixel_size]; + uint8_t *dst = &dst_data_ptr[(dst_y * width + dst_x) * pixel_size]; + + for (int k = 0; k < pixel_size; k++) { + dst[k] = src[k]; + } + } + } + } +} + +void Image::blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest) { + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); + int dsize = data.size(); + int srcdsize = p_src->data.size(); + ERR_FAIL_COND(dsize == 0); + ERR_FAIL_COND(srcdsize == 0); + ERR_FAIL_COND(format != p_src->format); + + Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect); + + if (p_dest.x < 0) { + clipped_src_rect.position.x = ABS(p_dest.x); + } + if (p_dest.y < 0) { + clipped_src_rect.position.y = ABS(p_dest.y); + } + + if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0) { + return; + } + + Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y)); + Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size)); + + Ref<Image> img = p_src; + + for (int i = 0; i < dest_rect.size.y; i++) { + for (int j = 0; j < dest_rect.size.x; j++) { + int src_x = clipped_src_rect.position.x + j; + int src_y = clipped_src_rect.position.y + i; + + int dst_x = dest_rect.position.x + j; + int dst_y = dest_rect.position.y + i; + + Color sc = img->get_pixel(src_x, src_y); + if (sc.a != 0) { + Color dc = get_pixel(dst_x, dst_y); + dc = dc.blend(sc); + set_pixel(dst_x, dst_y, dc); + } + } + } +} + +void Image::blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest) { + ERR_FAIL_COND_MSG(p_src.is_null(), "It's not a reference to a valid Image object."); + ERR_FAIL_COND_MSG(p_mask.is_null(), "It's not a reference to a valid Image object."); + int dsize = data.size(); + int srcdsize = p_src->data.size(); + int maskdsize = p_mask->data.size(); + ERR_FAIL_COND(dsize == 0); + ERR_FAIL_COND(srcdsize == 0); + ERR_FAIL_COND(maskdsize == 0); + ERR_FAIL_COND_MSG(p_src->width != p_mask->width, "Source image width is different from mask width."); + ERR_FAIL_COND_MSG(p_src->height != p_mask->height, "Source image height is different from mask height."); + ERR_FAIL_COND(format != p_src->format); + + Rect2i clipped_src_rect = Rect2i(0, 0, p_src->width, p_src->height).intersection(p_src_rect); + + if (p_dest.x < 0) { + clipped_src_rect.position.x = ABS(p_dest.x); + } + if (p_dest.y < 0) { + clipped_src_rect.position.y = ABS(p_dest.y); + } + + if (clipped_src_rect.size.x <= 0 || clipped_src_rect.size.y <= 0) { + return; + } + + Point2 src_underscan = Point2(MIN(0, p_src_rect.position.x), MIN(0, p_src_rect.position.y)); + Rect2i dest_rect = Rect2i(0, 0, width, height).intersection(Rect2i(p_dest - src_underscan, clipped_src_rect.size)); + + Ref<Image> img = p_src; + Ref<Image> msk = p_mask; + + for (int i = 0; i < dest_rect.size.y; i++) { + for (int j = 0; j < dest_rect.size.x; j++) { + int src_x = clipped_src_rect.position.x + j; + int src_y = clipped_src_rect.position.y + i; + + // If the mask's pixel is transparent then we skip it + //Color c = msk->get_pixel(src_x, src_y); + //if (c.a == 0) continue; + if (msk->get_pixel(src_x, src_y).a != 0) { + int dst_x = dest_rect.position.x + j; + int dst_y = dest_rect.position.y + i; + + Color sc = img->get_pixel(src_x, src_y); + if (sc.a != 0) { + Color dc = get_pixel(dst_x, dst_y); + dc = dc.blend(sc); + set_pixel(dst_x, dst_y, dc); + } + } + } + } +} + +void Image::fill(const Color &c) { + ERR_FAIL_COND_MSG(!_can_modify(format), "Cannot fill in compressed or custom image formats."); + + uint8_t *wp = data.ptrw(); + uint8_t *dst_data_ptr = wp; + + int pixel_size = get_format_pixel_size(format); + + // put first pixel with the format-aware API + set_pixel(0, 0, c); + + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + uint8_t *dst = &dst_data_ptr[(y * width + x) * pixel_size]; + + for (int k = 0; k < pixel_size; k++) { + dst[k] = dst_data_ptr[k]; + } + } + } +} + +ImageMemLoadFunc Image::_png_mem_loader_func = nullptr; +ImageMemLoadFunc Image::_jpg_mem_loader_func = nullptr; +ImageMemLoadFunc Image::_webp_mem_loader_func = nullptr; +ImageMemLoadFunc Image::_tga_mem_loader_func = nullptr; +ImageMemLoadFunc Image::_bmp_mem_loader_func = nullptr; + +void (*Image::_image_compress_bc_func)(Image *, float, Image::UsedChannels) = nullptr; +void (*Image::_image_compress_bptc_func)(Image *, float, Image::UsedChannels) = nullptr; +void (*Image::_image_compress_pvrtc1_4bpp_func)(Image *) = nullptr; +void (*Image::_image_compress_etc1_func)(Image *, float) = nullptr; +void (*Image::_image_compress_etc2_func)(Image *, float, Image::UsedChannels) = nullptr; +void (*Image::_image_decompress_pvrtc)(Image *) = nullptr; +void (*Image::_image_decompress_bc)(Image *) = nullptr; +void (*Image::_image_decompress_bptc)(Image *) = nullptr; +void (*Image::_image_decompress_etc1)(Image *) = nullptr; +void (*Image::_image_decompress_etc2)(Image *) = nullptr; + +Vector<uint8_t> (*Image::lossy_packer)(const Ref<Image> &, float) = nullptr; +Ref<Image> (*Image::lossy_unpacker)(const Vector<uint8_t> &) = nullptr; +Vector<uint8_t> (*Image::lossless_packer)(const Ref<Image> &) = nullptr; +Ref<Image> (*Image::lossless_unpacker)(const Vector<uint8_t> &) = nullptr; +Vector<uint8_t> (*Image::basis_universal_packer)(const Ref<Image> &, Image::UsedChannels) = nullptr; +Ref<Image> (*Image::basis_universal_unpacker)(const Vector<uint8_t> &) = nullptr; + +void Image::_set_data(const Dictionary &p_data) { + ERR_FAIL_COND(!p_data.has("width")); + ERR_FAIL_COND(!p_data.has("height")); + ERR_FAIL_COND(!p_data.has("format")); + ERR_FAIL_COND(!p_data.has("mipmaps")); + ERR_FAIL_COND(!p_data.has("data")); + + int dwidth = p_data["width"]; + int dheight = p_data["height"]; + String dformat = p_data["format"]; + bool dmipmaps = p_data["mipmaps"]; + Vector<uint8_t> ddata = p_data["data"]; + Format ddformat = FORMAT_MAX; + for (int i = 0; i < FORMAT_MAX; i++) { + if (dformat == get_format_name(Format(i))) { + ddformat = Format(i); + break; + } + } + + ERR_FAIL_COND(ddformat == FORMAT_MAX); + + create(dwidth, dheight, dmipmaps, ddformat, ddata); +} + +Dictionary Image::_get_data() const { + Dictionary d; + d["width"] = width; + d["height"] = height; + d["format"] = get_format_name(format); + d["mipmaps"] = mipmaps; + d["data"] = data; + return d; +} + +Color Image::get_pixelv(const Point2i &p_point) const { + return get_pixel(p_point.x, p_point.y); +} + +Color Image::_get_color_at_ofs(const uint8_t *ptr, uint32_t ofs) const { + switch (format) { + case FORMAT_L8: { + float l = ptr[ofs] / 255.0; + return Color(l, l, l, 1); + } + case FORMAT_LA8: { + float l = ptr[ofs * 2 + 0] / 255.0; + float a = ptr[ofs * 2 + 1] / 255.0; + return Color(l, l, l, a); + } + case FORMAT_R8: { + float r = ptr[ofs] / 255.0; + return Color(r, 0, 0, 1); + } + case FORMAT_RG8: { + float r = ptr[ofs * 2 + 0] / 255.0; + float g = ptr[ofs * 2 + 1] / 255.0; + return Color(r, g, 0, 1); + } + case FORMAT_RGB8: { + float r = ptr[ofs * 3 + 0] / 255.0; + float g = ptr[ofs * 3 + 1] / 255.0; + float b = ptr[ofs * 3 + 2] / 255.0; + return Color(r, g, b, 1); + } + case FORMAT_RGBA8: { + float r = ptr[ofs * 4 + 0] / 255.0; + float g = ptr[ofs * 4 + 1] / 255.0; + float b = ptr[ofs * 4 + 2] / 255.0; + float a = ptr[ofs * 4 + 3] / 255.0; + return Color(r, g, b, a); + } + case FORMAT_RGBA4444: { + uint16_t u = ((uint16_t *)ptr)[ofs]; + float r = ((u >> 12) & 0xF) / 15.0; + float g = ((u >> 8) & 0xF) / 15.0; + float b = ((u >> 4) & 0xF) / 15.0; + float a = (u & 0xF) / 15.0; + return Color(r, g, b, a); + } + case FORMAT_RGB565: { + uint16_t u = ((uint16_t *)ptr)[ofs]; + float r = (u & 0x1F) / 31.0; + float g = ((u >> 5) & 0x3F) / 63.0; + float b = ((u >> 11) & 0x1F) / 31.0; + return Color(r, g, b, 1.0); + } + case FORMAT_RF: { + float r = ((float *)ptr)[ofs]; + return Color(r, 0, 0, 1); + } + case FORMAT_RGF: { + float r = ((float *)ptr)[ofs * 2 + 0]; + float g = ((float *)ptr)[ofs * 2 + 1]; + return Color(r, g, 0, 1); + } + case FORMAT_RGBF: { + float r = ((float *)ptr)[ofs * 3 + 0]; + float g = ((float *)ptr)[ofs * 3 + 1]; + float b = ((float *)ptr)[ofs * 3 + 2]; + return Color(r, g, b, 1); + } + case FORMAT_RGBAF: { + float r = ((float *)ptr)[ofs * 4 + 0]; + float g = ((float *)ptr)[ofs * 4 + 1]; + float b = ((float *)ptr)[ofs * 4 + 2]; + float a = ((float *)ptr)[ofs * 4 + 3]; + return Color(r, g, b, a); + } + case FORMAT_RH: { + uint16_t r = ((uint16_t *)ptr)[ofs]; + return Color(Math::half_to_float(r), 0, 0, 1); + } + case FORMAT_RGH: { + uint16_t r = ((uint16_t *)ptr)[ofs * 2 + 0]; + uint16_t g = ((uint16_t *)ptr)[ofs * 2 + 1]; + return Color(Math::half_to_float(r), Math::half_to_float(g), 0, 1); + } + case FORMAT_RGBH: { + uint16_t r = ((uint16_t *)ptr)[ofs * 3 + 0]; + uint16_t g = ((uint16_t *)ptr)[ofs * 3 + 1]; + uint16_t b = ((uint16_t *)ptr)[ofs * 3 + 2]; + return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), 1); + } + case FORMAT_RGBAH: { + uint16_t r = ((uint16_t *)ptr)[ofs * 4 + 0]; + uint16_t g = ((uint16_t *)ptr)[ofs * 4 + 1]; + uint16_t b = ((uint16_t *)ptr)[ofs * 4 + 2]; + uint16_t a = ((uint16_t *)ptr)[ofs * 4 + 3]; + return Color(Math::half_to_float(r), Math::half_to_float(g), Math::half_to_float(b), Math::half_to_float(a)); + } + case FORMAT_RGBE9995: { + return Color::from_rgbe9995(((uint32_t *)ptr)[ofs]); + } + default: { + ERR_FAIL_V_MSG(Color(), "Can't get_pixel() on compressed image, sorry."); + } + } +} + +void Image::_set_color_at_ofs(uint8_t *ptr, uint32_t ofs, const Color &p_color) { + switch (format) { + case FORMAT_L8: { + ptr[ofs] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255)); + } break; + case FORMAT_LA8: { + ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.get_v() * 255.0, 0, 255)); + ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255)); + } break; + case FORMAT_R8: { + ptr[ofs] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255)); + } break; + case FORMAT_RG8: { + ptr[ofs * 2 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255)); + ptr[ofs * 2 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255)); + } break; + case FORMAT_RGB8: { + ptr[ofs * 3 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255)); + ptr[ofs * 3 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255)); + ptr[ofs * 3 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255)); + } break; + case FORMAT_RGBA8: { + ptr[ofs * 4 + 0] = uint8_t(CLAMP(p_color.r * 255.0, 0, 255)); + ptr[ofs * 4 + 1] = uint8_t(CLAMP(p_color.g * 255.0, 0, 255)); + ptr[ofs * 4 + 2] = uint8_t(CLAMP(p_color.b * 255.0, 0, 255)); + ptr[ofs * 4 + 3] = uint8_t(CLAMP(p_color.a * 255.0, 0, 255)); + + } break; + case FORMAT_RGBA4444: { + uint16_t rgba = 0; + + rgba = uint16_t(CLAMP(p_color.r * 15.0, 0, 15)) << 12; + rgba |= uint16_t(CLAMP(p_color.g * 15.0, 0, 15)) << 8; + rgba |= uint16_t(CLAMP(p_color.b * 15.0, 0, 15)) << 4; + rgba |= uint16_t(CLAMP(p_color.a * 15.0, 0, 15)); + + ((uint16_t *)ptr)[ofs] = rgba; + + } break; + case FORMAT_RGB565: { + uint16_t rgba = 0; + + rgba = uint16_t(CLAMP(p_color.r * 31.0, 0, 31)); + rgba |= uint16_t(CLAMP(p_color.g * 63.0, 0, 33)) << 5; + rgba |= uint16_t(CLAMP(p_color.b * 31.0, 0, 31)) << 11; + + ((uint16_t *)ptr)[ofs] = rgba; + + } break; + case FORMAT_RF: { + ((float *)ptr)[ofs] = p_color.r; + } break; + case FORMAT_RGF: { + ((float *)ptr)[ofs * 2 + 0] = p_color.r; + ((float *)ptr)[ofs * 2 + 1] = p_color.g; + } break; + case FORMAT_RGBF: { + ((float *)ptr)[ofs * 3 + 0] = p_color.r; + ((float *)ptr)[ofs * 3 + 1] = p_color.g; + ((float *)ptr)[ofs * 3 + 2] = p_color.b; + } break; + case FORMAT_RGBAF: { + ((float *)ptr)[ofs * 4 + 0] = p_color.r; + ((float *)ptr)[ofs * 4 + 1] = p_color.g; + ((float *)ptr)[ofs * 4 + 2] = p_color.b; + ((float *)ptr)[ofs * 4 + 3] = p_color.a; + } break; + case FORMAT_RH: { + ((uint16_t *)ptr)[ofs] = Math::make_half_float(p_color.r); + } break; + case FORMAT_RGH: { + ((uint16_t *)ptr)[ofs * 2 + 0] = Math::make_half_float(p_color.r); + ((uint16_t *)ptr)[ofs * 2 + 1] = Math::make_half_float(p_color.g); + } break; + case FORMAT_RGBH: { + ((uint16_t *)ptr)[ofs * 3 + 0] = Math::make_half_float(p_color.r); + ((uint16_t *)ptr)[ofs * 3 + 1] = Math::make_half_float(p_color.g); + ((uint16_t *)ptr)[ofs * 3 + 2] = Math::make_half_float(p_color.b); + } break; + case FORMAT_RGBAH: { + ((uint16_t *)ptr)[ofs * 4 + 0] = Math::make_half_float(p_color.r); + ((uint16_t *)ptr)[ofs * 4 + 1] = Math::make_half_float(p_color.g); + ((uint16_t *)ptr)[ofs * 4 + 2] = Math::make_half_float(p_color.b); + ((uint16_t *)ptr)[ofs * 4 + 3] = Math::make_half_float(p_color.a); + } break; + case FORMAT_RGBE9995: { + ((uint32_t *)ptr)[ofs] = p_color.to_rgbe9995(); + + } break; + default: { + ERR_FAIL_MSG("Can't set_pixel() on compressed image, sorry."); + } + } +} + +Color Image::get_pixel(int p_x, int p_y) const { +#ifdef DEBUG_ENABLED + ERR_FAIL_INDEX_V(p_x, width, Color()); + ERR_FAIL_INDEX_V(p_y, height, Color()); +#endif + + uint32_t ofs = p_y * width + p_x; + return _get_color_at_ofs(data.ptr(), ofs); +} + +void Image::set_pixelv(const Point2i &p_point, const Color &p_color) { + set_pixel(p_point.x, p_point.y, p_color); +} + +void Image::set_pixel(int p_x, int p_y, const Color &p_color) { +#ifdef DEBUG_ENABLED + ERR_FAIL_INDEX(p_x, width); + ERR_FAIL_INDEX(p_y, height); +#endif + + uint32_t ofs = p_y * width + p_x; + _set_color_at_ofs(data.ptrw(), ofs, p_color); +} + +Image::UsedChannels Image::detect_used_channels(CompressSource p_source) { + ERR_FAIL_COND_V(data.size() == 0, USED_CHANNELS_RGBA); + ERR_FAIL_COND_V(is_compressed(), USED_CHANNELS_RGBA); + bool r = false, g = false, b = false, a = false, c = false; + + for (int i = 0; i < width; i++) { + for (int j = 0; j < height; j++) { + Color col = get_pixel(i, j); + + if (col.r > 0.001) { + r = true; + } + if (col.g > 0.001) { + g = true; + } + if (col.b > 0.001) { + b = true; + } + if (col.a < 0.999) { + a = true; + } + + if (col.r != col.b || col.r != col.g || col.b != col.g) { + c = true; + } + } + } + + UsedChannels used_channels; + + if (!c && !a) { + used_channels = USED_CHANNELS_L; + } else if (!c && a) { + used_channels = USED_CHANNELS_LA; + } else if (r && !g && !b && !a) { + used_channels = USED_CHANNELS_R; + } else if (r && g && !b && !a) { + used_channels = USED_CHANNELS_RG; + } else if (r && g && b && !a) { + used_channels = USED_CHANNELS_RGB; + } else { + used_channels = USED_CHANNELS_RGBA; + } + + if (p_source == COMPRESS_SOURCE_SRGB && (used_channels == USED_CHANNELS_R || used_channels == USED_CHANNELS_RG)) { + //R and RG do not support SRGB + used_channels = USED_CHANNELS_RGB; + } + + if (p_source == COMPRESS_SOURCE_NORMAL) { + //use RG channels only for normal + used_channels = USED_CHANNELS_RG; + } + + return used_channels; +} + +void Image::optimize_channels() { + switch (detect_used_channels()) { + case USED_CHANNELS_L: + convert(FORMAT_L8); + break; + case USED_CHANNELS_LA: + convert(FORMAT_LA8); + break; + case USED_CHANNELS_R: + convert(FORMAT_R8); + break; + case USED_CHANNELS_RG: + convert(FORMAT_RG8); + break; + case USED_CHANNELS_RGB: + convert(FORMAT_RGB8); + break; + case USED_CHANNELS_RGBA: + convert(FORMAT_RGBA8); + break; + } +} + +void Image::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_width"), &Image::get_width); + ClassDB::bind_method(D_METHOD("get_height"), &Image::get_height); + ClassDB::bind_method(D_METHOD("get_size"), &Image::get_size); + ClassDB::bind_method(D_METHOD("has_mipmaps"), &Image::has_mipmaps); + ClassDB::bind_method(D_METHOD("get_format"), &Image::get_format); + ClassDB::bind_method(D_METHOD("get_data"), &Image::get_data); + + ClassDB::bind_method(D_METHOD("convert", "format"), &Image::convert); + + ClassDB::bind_method(D_METHOD("get_mipmap_offset", "mipmap"), &Image::get_mipmap_offset); + + ClassDB::bind_method(D_METHOD("resize_to_po2", "square", "interpolation"), &Image::resize_to_po2, DEFVAL(false), DEFVAL(INTERPOLATE_BILINEAR)); + ClassDB::bind_method(D_METHOD("resize", "width", "height", "interpolation"), &Image::resize, DEFVAL(INTERPOLATE_BILINEAR)); + ClassDB::bind_method(D_METHOD("shrink_x2"), &Image::shrink_x2); + + ClassDB::bind_method(D_METHOD("crop", "width", "height"), &Image::crop); + ClassDB::bind_method(D_METHOD("flip_x"), &Image::flip_x); + ClassDB::bind_method(D_METHOD("flip_y"), &Image::flip_y); + ClassDB::bind_method(D_METHOD("generate_mipmaps", "renormalize"), &Image::generate_mipmaps, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("clear_mipmaps"), &Image::clear_mipmaps); + + ClassDB::bind_method(D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::_create_empty); + ClassDB::bind_method(D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::_create_from_data); + + ClassDB::bind_method(D_METHOD("is_empty"), &Image::is_empty); + + ClassDB::bind_method(D_METHOD("load", "path"), &Image::load); + ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png); + ClassDB::bind_method(D_METHOD("save_png_to_buffer"), &Image::save_png_to_buffer); + ClassDB::bind_method(D_METHOD("save_exr", "path", "grayscale"), &Image::save_exr, DEFVAL(false)); + + ClassDB::bind_method(D_METHOD("detect_alpha"), &Image::detect_alpha); + ClassDB::bind_method(D_METHOD("is_invisible"), &Image::is_invisible); + + ClassDB::bind_method(D_METHOD("detect_used_channels", "source"), &Image::detect_used_channels, DEFVAL(COMPRESS_SOURCE_GENERIC)); + ClassDB::bind_method(D_METHOD("compress", "mode", "source", "lossy_quality"), &Image::compress, DEFVAL(COMPRESS_SOURCE_GENERIC), DEFVAL(0.7)); + ClassDB::bind_method(D_METHOD("compress_from_channels", "mode", "channels", "lossy_quality"), &Image::compress_from_channels, DEFVAL(0.7)); + ClassDB::bind_method(D_METHOD("decompress"), &Image::decompress); + ClassDB::bind_method(D_METHOD("is_compressed"), &Image::is_compressed); + + ClassDB::bind_method(D_METHOD("fix_alpha_edges"), &Image::fix_alpha_edges); + ClassDB::bind_method(D_METHOD("premultiply_alpha"), &Image::premultiply_alpha); + ClassDB::bind_method(D_METHOD("srgb_to_linear"), &Image::srgb_to_linear); + ClassDB::bind_method(D_METHOD("normal_map_to_xy"), &Image::normal_map_to_xy); + ClassDB::bind_method(D_METHOD("rgbe_to_srgb"), &Image::rgbe_to_srgb); + ClassDB::bind_method(D_METHOD("bump_map_to_normal_map", "bump_scale"), &Image::bump_map_to_normal_map, DEFVAL(1.0)); + + ClassDB::bind_method(D_METHOD("blit_rect", "src", "src_rect", "dst"), &Image::blit_rect); + ClassDB::bind_method(D_METHOD("blit_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blit_rect_mask); + ClassDB::bind_method(D_METHOD("blend_rect", "src", "src_rect", "dst"), &Image::blend_rect); + ClassDB::bind_method(D_METHOD("blend_rect_mask", "src", "mask", "src_rect", "dst"), &Image::blend_rect_mask); + ClassDB::bind_method(D_METHOD("fill", "color"), &Image::fill); + + ClassDB::bind_method(D_METHOD("get_used_rect"), &Image::get_used_rect); + ClassDB::bind_method(D_METHOD("get_rect", "rect"), &Image::get_rect); + + ClassDB::bind_method(D_METHOD("copy_from", "src"), &Image::copy_internals_from); + + ClassDB::bind_method(D_METHOD("_set_data", "data"), &Image::_set_data); + ClassDB::bind_method(D_METHOD("_get_data"), &Image::_get_data); + + ClassDB::bind_method(D_METHOD("get_pixelv", "point"), &Image::get_pixelv); + ClassDB::bind_method(D_METHOD("get_pixel", "x", "y"), &Image::get_pixel); + ClassDB::bind_method(D_METHOD("set_pixelv", "point", "color"), &Image::set_pixelv); + ClassDB::bind_method(D_METHOD("set_pixel", "x", "y", "color"), &Image::set_pixel); + + ClassDB::bind_method(D_METHOD("load_png_from_buffer", "buffer"), &Image::load_png_from_buffer); + ClassDB::bind_method(D_METHOD("load_jpg_from_buffer", "buffer"), &Image::load_jpg_from_buffer); + ClassDB::bind_method(D_METHOD("load_webp_from_buffer", "buffer"), &Image::load_webp_from_buffer); + ClassDB::bind_method(D_METHOD("load_tga_from_buffer", "buffer"), &Image::load_tga_from_buffer); + ClassDB::bind_method(D_METHOD("load_bmp_from_buffer", "buffer"), &Image::load_bmp_from_buffer); + + ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "_set_data", "_get_data"); + + BIND_CONSTANT(MAX_WIDTH); + BIND_CONSTANT(MAX_HEIGHT); + + BIND_ENUM_CONSTANT(FORMAT_L8); //luminance + BIND_ENUM_CONSTANT(FORMAT_LA8); //luminance-alpha + BIND_ENUM_CONSTANT(FORMAT_R8); + BIND_ENUM_CONSTANT(FORMAT_RG8); + BIND_ENUM_CONSTANT(FORMAT_RGB8); + BIND_ENUM_CONSTANT(FORMAT_RGBA8); + BIND_ENUM_CONSTANT(FORMAT_RGBA4444); + BIND_ENUM_CONSTANT(FORMAT_RGB565); + BIND_ENUM_CONSTANT(FORMAT_RF); //float + BIND_ENUM_CONSTANT(FORMAT_RGF); + BIND_ENUM_CONSTANT(FORMAT_RGBF); + BIND_ENUM_CONSTANT(FORMAT_RGBAF); + BIND_ENUM_CONSTANT(FORMAT_RH); //half float + BIND_ENUM_CONSTANT(FORMAT_RGH); + BIND_ENUM_CONSTANT(FORMAT_RGBH); + BIND_ENUM_CONSTANT(FORMAT_RGBAH); + BIND_ENUM_CONSTANT(FORMAT_RGBE9995); + BIND_ENUM_CONSTANT(FORMAT_DXT1); //s3tc bc1 + BIND_ENUM_CONSTANT(FORMAT_DXT3); //bc2 + BIND_ENUM_CONSTANT(FORMAT_DXT5); //bc3 + BIND_ENUM_CONSTANT(FORMAT_RGTC_R); + BIND_ENUM_CONSTANT(FORMAT_RGTC_RG); + BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBA); //btpc bc6h + BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBF); //float / + BIND_ENUM_CONSTANT(FORMAT_BPTC_RGBFU); //unsigned float + BIND_ENUM_CONSTANT(FORMAT_PVRTC1_2); //pvrtc + BIND_ENUM_CONSTANT(FORMAT_PVRTC1_2A); + BIND_ENUM_CONSTANT(FORMAT_PVRTC1_4); + BIND_ENUM_CONSTANT(FORMAT_PVRTC1_4A); + BIND_ENUM_CONSTANT(FORMAT_ETC); //etc1 + BIND_ENUM_CONSTANT(FORMAT_ETC2_R11); //etc2 + BIND_ENUM_CONSTANT(FORMAT_ETC2_R11S); //signed ); NOT srgb. + BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11); + BIND_ENUM_CONSTANT(FORMAT_ETC2_RG11S); + BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8); + BIND_ENUM_CONSTANT(FORMAT_ETC2_RGBA8); + BIND_ENUM_CONSTANT(FORMAT_ETC2_RGB8A1); + BIND_ENUM_CONSTANT(FORMAT_ETC2_RA_AS_RG); + BIND_ENUM_CONSTANT(FORMAT_DXT5_RA_AS_RG); + BIND_ENUM_CONSTANT(FORMAT_MAX); + + BIND_ENUM_CONSTANT(INTERPOLATE_NEAREST); + BIND_ENUM_CONSTANT(INTERPOLATE_BILINEAR); + BIND_ENUM_CONSTANT(INTERPOLATE_CUBIC); + BIND_ENUM_CONSTANT(INTERPOLATE_TRILINEAR); + BIND_ENUM_CONSTANT(INTERPOLATE_LANCZOS); + + BIND_ENUM_CONSTANT(ALPHA_NONE); + BIND_ENUM_CONSTANT(ALPHA_BIT); + BIND_ENUM_CONSTANT(ALPHA_BLEND); + + BIND_ENUM_CONSTANT(COMPRESS_S3TC); + BIND_ENUM_CONSTANT(COMPRESS_PVRTC1_4); + BIND_ENUM_CONSTANT(COMPRESS_ETC); + BIND_ENUM_CONSTANT(COMPRESS_ETC2); + BIND_ENUM_CONSTANT(COMPRESS_BPTC); + + BIND_ENUM_CONSTANT(USED_CHANNELS_L); + BIND_ENUM_CONSTANT(USED_CHANNELS_LA); + BIND_ENUM_CONSTANT(USED_CHANNELS_R); + BIND_ENUM_CONSTANT(USED_CHANNELS_RG); + BIND_ENUM_CONSTANT(USED_CHANNELS_RGB); + BIND_ENUM_CONSTANT(USED_CHANNELS_RGBA); + + BIND_ENUM_CONSTANT(COMPRESS_SOURCE_GENERIC); + BIND_ENUM_CONSTANT(COMPRESS_SOURCE_SRGB); + BIND_ENUM_CONSTANT(COMPRESS_SOURCE_NORMAL); +} + +void Image::set_compress_bc_func(void (*p_compress_func)(Image *, float, UsedChannels)) { + _image_compress_bc_func = p_compress_func; +} + +void Image::set_compress_bptc_func(void (*p_compress_func)(Image *, float, UsedChannels)) { + _image_compress_bptc_func = p_compress_func; +} + +void Image::normal_map_to_xy() { + convert(Image::FORMAT_RGBA8); + + { + int len = data.size() / 4; + uint8_t *data_ptr = data.ptrw(); + + for (int i = 0; i < len; i++) { + data_ptr[(i << 2) + 3] = data_ptr[(i << 2) + 0]; //x to w + data_ptr[(i << 2) + 0] = data_ptr[(i << 2) + 1]; //y to xz + data_ptr[(i << 2) + 2] = data_ptr[(i << 2) + 1]; + } + } + + convert(Image::FORMAT_LA8); +} + +Ref<Image> Image::rgbe_to_srgb() { + if (data.size() == 0) { + return Ref<Image>(); + } + + ERR_FAIL_COND_V(format != FORMAT_RGBE9995, Ref<Image>()); + + Ref<Image> new_image; + new_image.instance(); + new_image->create(width, height, false, Image::FORMAT_RGB8); + + for (int row = 0; row < height; row++) { + for (int col = 0; col < width; col++) { + new_image->set_pixel(col, row, get_pixel(col, row).to_srgb()); + } + } + + if (has_mipmaps()) { + new_image->generate_mipmaps(); + } + + return new_image; +} + +Ref<Image> Image::get_image_from_mipmap(int p_mipamp) const { + int ofs, size, w, h; + get_mipmap_offset_size_and_dimensions(p_mipamp, ofs, size, w, h); + + Vector<uint8_t> new_data; + new_data.resize(size); + + { + uint8_t *wr = new_data.ptrw(); + const uint8_t *rd = data.ptr(); + copymem(wr, rd + ofs, size); + } + + Ref<Image> image; + image.instance(); + image->width = w; + image->height = h; + image->format = format; + image->data = new_data; + + image->mipmaps = false; + return image; +} + +void Image::bump_map_to_normal_map(float bump_scale) { + ERR_FAIL_COND(!_can_modify(format)); + convert(Image::FORMAT_RF); + + Vector<uint8_t> result_image; //rgba output + result_image.resize(width * height * 4); + + { + const uint8_t *rp = data.ptr(); + uint8_t *wp = result_image.ptrw(); + + ERR_FAIL_COND(!rp); + + unsigned char *write_ptr = wp; + float *read_ptr = (float *)rp; + + for (int ty = 0; ty < height; ty++) { + int py = ty + 1; + if (py >= height) { + py -= height; + } + + for (int tx = 0; tx < width; tx++) { + int px = tx + 1; + if (px >= width) { + px -= width; + } + float here = read_ptr[ty * width + tx]; + float to_right = read_ptr[ty * width + px]; + float above = read_ptr[py * width + tx]; + Vector3 up = Vector3(0, 1, (here - above) * bump_scale); + Vector3 across = Vector3(1, 0, (to_right - here) * bump_scale); + + Vector3 normal = across.cross(up); + normal.normalize(); + + write_ptr[((ty * width + tx) << 2) + 0] = (127.5 + normal.x * 127.5); + write_ptr[((ty * width + tx) << 2) + 1] = (127.5 + normal.y * 127.5); + write_ptr[((ty * width + tx) << 2) + 2] = (127.5 + normal.z * 127.5); + write_ptr[((ty * width + tx) << 2) + 3] = 255; + } + } + } + format = FORMAT_RGBA8; + data = result_image; +} + +void Image::srgb_to_linear() { + if (data.size() == 0) { + return; + } + + static const uint8_t srgb2lin[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 22, 22, 23, 23, 24, 24, 25, 26, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 33, 34, 35, 36, 36, 37, 38, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 47, 48, 49, 50, 51, 52, 53, 54, 55, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 70, 71, 72, 73, 74, 75, 76, 77, 78, 80, 81, 82, 83, 84, 85, 87, 88, 89, 90, 92, 93, 94, 95, 97, 98, 99, 101, 102, 103, 105, 106, 107, 109, 110, 112, 113, 114, 116, 117, 119, 120, 122, 123, 125, 126, 128, 129, 131, 132, 134, 135, 137, 139, 140, 142, 144, 145, 147, 148, 150, 152, 153, 155, 157, 159, 160, 162, 164, 166, 167, 169, 171, 173, 175, 176, 178, 180, 182, 184, 186, 188, 190, 192, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 218, 220, 222, 224, 226, 228, 230, 232, 235, 237, 239, 241, 243, 245, 248, 250, 252, 255 }; + + ERR_FAIL_COND(format != FORMAT_RGB8 && format != FORMAT_RGBA8); + + if (format == FORMAT_RGBA8) { + int len = data.size() / 4; + uint8_t *data_ptr = data.ptrw(); + + for (int i = 0; i < len; i++) { + data_ptr[(i << 2) + 0] = srgb2lin[data_ptr[(i << 2) + 0]]; + data_ptr[(i << 2) + 1] = srgb2lin[data_ptr[(i << 2) + 1]]; + data_ptr[(i << 2) + 2] = srgb2lin[data_ptr[(i << 2) + 2]]; + } + + } else if (format == FORMAT_RGB8) { + int len = data.size() / 3; + uint8_t *data_ptr = data.ptrw(); + + for (int i = 0; i < len; i++) { + data_ptr[(i * 3) + 0] = srgb2lin[data_ptr[(i * 3) + 0]]; + data_ptr[(i * 3) + 1] = srgb2lin[data_ptr[(i * 3) + 1]]; + data_ptr[(i * 3) + 2] = srgb2lin[data_ptr[(i * 3) + 2]]; + } + } +} + +void Image::premultiply_alpha() { + if (data.size() == 0) { + return; + } + + if (format != FORMAT_RGBA8) { + return; //not needed + } + + uint8_t *data_ptr = data.ptrw(); + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + uint8_t *ptr = &data_ptr[(i * width + j) * 4]; + + ptr[0] = (uint16_t(ptr[0]) * uint16_t(ptr[3])) >> 8; + ptr[1] = (uint16_t(ptr[1]) * uint16_t(ptr[3])) >> 8; + ptr[2] = (uint16_t(ptr[2]) * uint16_t(ptr[3])) >> 8; + } + } +} + +void Image::fix_alpha_edges() { + if (data.size() == 0) { + return; + } + + if (format != FORMAT_RGBA8) { + return; //not needed + } + + Vector<uint8_t> dcopy = data; + const uint8_t *srcptr = dcopy.ptr(); + + uint8_t *data_ptr = data.ptrw(); + + const int max_radius = 4; + const int alpha_threshold = 20; + const int max_dist = 0x7FFFFFFF; + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + const uint8_t *rptr = &srcptr[(i * width + j) * 4]; + uint8_t *wptr = &data_ptr[(i * width + j) * 4]; + + if (rptr[3] >= alpha_threshold) { + continue; + } + + int closest_dist = max_dist; + uint8_t closest_color[3]; + + int from_x = MAX(0, j - max_radius); + int to_x = MIN(width - 1, j + max_radius); + int from_y = MAX(0, i - max_radius); + int to_y = MIN(height - 1, i + max_radius); + + for (int k = from_y; k <= to_y; k++) { + for (int l = from_x; l <= to_x; l++) { + int dy = i - k; + int dx = j - l; + int dist = dy * dy + dx * dx; + if (dist >= closest_dist) { + continue; + } + + const uint8_t *rp2 = &srcptr[(k * width + l) << 2]; + + if (rp2[3] < alpha_threshold) { + continue; + } + + closest_dist = dist; + closest_color[0] = rp2[0]; + closest_color[1] = rp2[1]; + closest_color[2] = rp2[2]; + } + } + + if (closest_dist != max_dist) { + wptr[0] = closest_color[0]; + wptr[1] = closest_color[1]; + wptr[2] = closest_color[2]; + } + } + } +} + +String Image::get_format_name(Format p_format) { + ERR_FAIL_INDEX_V(p_format, FORMAT_MAX, String()); + return format_names[p_format]; +} + +Error Image::load_png_from_buffer(const Vector<uint8_t> &p_array) { + return _load_from_buffer(p_array, _png_mem_loader_func); +} + +Error Image::load_jpg_from_buffer(const Vector<uint8_t> &p_array) { + return _load_from_buffer(p_array, _jpg_mem_loader_func); +} + +Error Image::load_webp_from_buffer(const Vector<uint8_t> &p_array) { + return _load_from_buffer(p_array, _webp_mem_loader_func); +} + +Error Image::load_tga_from_buffer(const Vector<uint8_t> &p_array) { + ERR_FAIL_NULL_V_MSG( + _tga_mem_loader_func, + ERR_UNAVAILABLE, + "The TGA module isn't enabled. Recompile the Godot editor or export template binary with the `module_tga_enabled=yes` SCons option."); + return _load_from_buffer(p_array, _tga_mem_loader_func); +} + +Error Image::load_bmp_from_buffer(const Vector<uint8_t> &p_array) { + ERR_FAIL_NULL_V_MSG( + _bmp_mem_loader_func, + ERR_UNAVAILABLE, + "The BMP module isn't enabled. Recompile the Godot editor or export template binary with the `module_bmp_enabled=yes` SCons option."); + return _load_from_buffer(p_array, _bmp_mem_loader_func); +} + +void Image::convert_rg_to_ra_rgba8() { + ERR_FAIL_COND(format != FORMAT_RGBA8); + ERR_FAIL_COND(!data.size()); + + int s = data.size(); + uint8_t *w = data.ptrw(); + for (int i = 0; i < s; i += 4) { + w[i + 3] = w[i + 1]; + w[i + 1] = 0; + w[i + 2] = 0; + } +} + +void Image::convert_ra_rgba8_to_rg() { + ERR_FAIL_COND(format != FORMAT_RGBA8); + ERR_FAIL_COND(!data.size()); + + int s = data.size(); + uint8_t *w = data.ptrw(); + for (int i = 0; i < s; i += 4) { + w[i + 1] = w[i + 3]; + w[i + 2] = 0; + w[i + 3] = 255; + } +} + +Error Image::_load_from_buffer(const Vector<uint8_t> &p_array, ImageMemLoadFunc p_loader) { + int buffer_size = p_array.size(); + + ERR_FAIL_COND_V(buffer_size == 0, ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V(!p_loader, ERR_INVALID_PARAMETER); + + const uint8_t *r = p_array.ptr(); + + Ref<Image> image = p_loader(r, buffer_size); + ERR_FAIL_COND_V(!image.is_valid(), ERR_PARSE_ERROR); + + copy_internals_from(image); + + return OK; +} + +void Image::average_4_uint8(uint8_t &p_out, const uint8_t &p_a, const uint8_t &p_b, const uint8_t &p_c, const uint8_t &p_d) { + p_out = static_cast<uint8_t>((p_a + p_b + p_c + p_d + 2) >> 2); +} + +void Image::average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d) { + p_out = (p_a + p_b + p_c + p_d) * 0.25f; +} + +void Image::average_4_half(uint16_t &p_out, const uint16_t &p_a, const uint16_t &p_b, const uint16_t &p_c, const uint16_t &p_d) { + p_out = Math::make_half_float((Math::half_to_float(p_a) + Math::half_to_float(p_b) + Math::half_to_float(p_c) + Math::half_to_float(p_d)) * 0.25f); +} + +void Image::average_4_rgbe9995(uint32_t &p_out, const uint32_t &p_a, const uint32_t &p_b, const uint32_t &p_c, const uint32_t &p_d) { + p_out = ((Color::from_rgbe9995(p_a) + Color::from_rgbe9995(p_b) + Color::from_rgbe9995(p_c) + Color::from_rgbe9995(p_d)) * 0.25f).to_rgbe9995(); +} + +void Image::renormalize_uint8(uint8_t *p_rgb) { + Vector3 n(p_rgb[0] / 255.0, p_rgb[1] / 255.0, p_rgb[2] / 255.0); + n *= 2.0; + n -= Vector3(1, 1, 1); + n.normalize(); + n += Vector3(1, 1, 1); + n *= 0.5; + n *= 255; + p_rgb[0] = CLAMP(int(n.x), 0, 255); + p_rgb[1] = CLAMP(int(n.y), 0, 255); + p_rgb[2] = CLAMP(int(n.z), 0, 255); +} + +void Image::renormalize_float(float *p_rgb) { + Vector3 n(p_rgb[0], p_rgb[1], p_rgb[2]); + n.normalize(); + p_rgb[0] = n.x; + p_rgb[1] = n.y; + p_rgb[2] = n.z; +} + +void Image::renormalize_half(uint16_t *p_rgb) { + Vector3 n(Math::half_to_float(p_rgb[0]), Math::half_to_float(p_rgb[1]), Math::half_to_float(p_rgb[2])); + n.normalize(); + p_rgb[0] = Math::make_half_float(n.x); + p_rgb[1] = Math::make_half_float(n.y); + p_rgb[2] = Math::make_half_float(n.z); +} + +void Image::renormalize_rgbe9995(uint32_t *p_rgb) { + // Never used +} + +Image::Image(const uint8_t *p_mem_png_jpg, int p_len) { + width = 0; + height = 0; + mipmaps = false; + format = FORMAT_L8; + + if (_png_mem_loader_func) { + copy_internals_from(_png_mem_loader_func(p_mem_png_jpg, p_len)); + } + + if (is_empty() && _jpg_mem_loader_func) { + copy_internals_from(_jpg_mem_loader_func(p_mem_png_jpg, p_len)); + } +} + +Ref<Resource> Image::duplicate(bool p_subresources) const { + Ref<Image> copy; + copy.instance(); + copy->_copy_internals_from(*this); + return copy; +} + +void Image::set_as_black() { + zeromem(data.ptrw(), data.size()); +} diff --git a/core/io/image.h b/core/io/image.h new file mode 100644 index 0000000000..b894be7df4 --- /dev/null +++ b/core/io/image.h @@ -0,0 +1,413 @@ +/*************************************************************************/ +/* image.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef IMAGE_H +#define IMAGE_H + +#include "core/io/resource.h" +#include "core/math/color.h" +#include "core/math/rect2.h" + +/** + * @author Juan Linietsky <reduzio@gmail.com> + * + * Image storage class. This is used to store an image in user memory, as well as + * providing some basic methods for image manipulation. + * Images can be loaded from a file, or registered into the Render object as textures. +*/ + +class Image; + +typedef Error (*SavePNGFunc)(const String &p_path, const Ref<Image> &p_img); +typedef Vector<uint8_t> (*SavePNGBufferFunc)(const Ref<Image> &p_img); +typedef Ref<Image> (*ImageMemLoadFunc)(const uint8_t *p_png, int p_size); + +typedef Error (*SaveEXRFunc)(const String &p_path, const Ref<Image> &p_img, bool p_grayscale); + +class Image : public Resource { + GDCLASS(Image, Resource); + +public: + static SavePNGFunc save_png_func; + static SaveEXRFunc save_exr_func; + static SavePNGBufferFunc save_png_buffer_func; + + enum { + MAX_WIDTH = (1 << 24), // force a limit somehow + MAX_HEIGHT = (1 << 24), // force a limit somehow + MAX_PIXELS = 268435456 + }; + + enum Format { + FORMAT_L8, //luminance + FORMAT_LA8, //luminance-alpha + FORMAT_R8, + FORMAT_RG8, + FORMAT_RGB8, + FORMAT_RGBA8, + FORMAT_RGBA4444, + FORMAT_RGB565, + FORMAT_RF, //float + FORMAT_RGF, + FORMAT_RGBF, + FORMAT_RGBAF, + FORMAT_RH, //half float + FORMAT_RGH, + FORMAT_RGBH, + FORMAT_RGBAH, + FORMAT_RGBE9995, + FORMAT_DXT1, //s3tc bc1 + FORMAT_DXT3, //bc2 + FORMAT_DXT5, //bc3 + FORMAT_RGTC_R, + FORMAT_RGTC_RG, + FORMAT_BPTC_RGBA, //btpc bc7 + FORMAT_BPTC_RGBF, //float bc6h + FORMAT_BPTC_RGBFU, //unsigned float bc6hu + FORMAT_PVRTC1_2, //pvrtc1 + FORMAT_PVRTC1_2A, + FORMAT_PVRTC1_4, + FORMAT_PVRTC1_4A, + FORMAT_ETC, //etc1 + FORMAT_ETC2_R11, //etc2 + FORMAT_ETC2_R11S, //signed, NOT srgb. + FORMAT_ETC2_RG11, + FORMAT_ETC2_RG11S, + FORMAT_ETC2_RGB8, + FORMAT_ETC2_RGBA8, + FORMAT_ETC2_RGB8A1, + FORMAT_ETC2_RA_AS_RG, //used to make basis universal happy + FORMAT_DXT5_RA_AS_RG, //used to make basis universal happy + FORMAT_MAX + }; + + static const char *format_names[FORMAT_MAX]; + enum Interpolation { + INTERPOLATE_NEAREST, + INTERPOLATE_BILINEAR, + INTERPOLATE_CUBIC, + INTERPOLATE_TRILINEAR, + INTERPOLATE_LANCZOS, + /* INTERPOLATE_TRICUBIC, */ + /* INTERPOLATE GAUSS */ + }; + + //this is used for compression + enum UsedChannels { + USED_CHANNELS_L, + USED_CHANNELS_LA, + USED_CHANNELS_R, + USED_CHANNELS_RG, + USED_CHANNELS_RGB, + USED_CHANNELS_RGBA, + }; + //some functions provided by something else + + static ImageMemLoadFunc _png_mem_loader_func; + static ImageMemLoadFunc _jpg_mem_loader_func; + static ImageMemLoadFunc _webp_mem_loader_func; + static ImageMemLoadFunc _tga_mem_loader_func; + static ImageMemLoadFunc _bmp_mem_loader_func; + + static void (*_image_compress_bc_func)(Image *, float, UsedChannels p_channels); + static void (*_image_compress_bptc_func)(Image *, float p_lossy_quality, UsedChannels p_channels); + static void (*_image_compress_pvrtc1_4bpp_func)(Image *); + static void (*_image_compress_etc1_func)(Image *, float); + static void (*_image_compress_etc2_func)(Image *, float, UsedChannels p_channels); + + static void (*_image_decompress_pvrtc)(Image *); + static void (*_image_decompress_bc)(Image *); + static void (*_image_decompress_bptc)(Image *); + static void (*_image_decompress_etc1)(Image *); + static void (*_image_decompress_etc2)(Image *); + + static Vector<uint8_t> (*lossy_packer)(const Ref<Image> &p_image, float p_quality); + static Ref<Image> (*lossy_unpacker)(const Vector<uint8_t> &p_buffer); + static Vector<uint8_t> (*lossless_packer)(const Ref<Image> &p_image); + static Ref<Image> (*lossless_unpacker)(const Vector<uint8_t> &p_buffer); + static Vector<uint8_t> (*basis_universal_packer)(const Ref<Image> &p_image, UsedChannels p_channels); + static Ref<Image> (*basis_universal_unpacker)(const Vector<uint8_t> &p_buffer); + + _FORCE_INLINE_ Color _get_color_at_ofs(const uint8_t *ptr, uint32_t ofs) const; + _FORCE_INLINE_ void _set_color_at_ofs(uint8_t *ptr, uint32_t ofs, const Color &p_color); + +protected: + static void _bind_methods(); + +private: + void _create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { + create(p_width, p_height, p_use_mipmaps, p_format); + } + + void _create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + create(p_width, p_height, p_use_mipmaps, p_format, p_data); + } + + Format format = FORMAT_L8; + Vector<uint8_t> data; + int width = 0; + int height = 0; + bool mipmaps = false; + + void _copy_internals_from(const Image &p_image) { + format = p_image.format; + width = p_image.width; + height = p_image.height; + mipmaps = p_image.mipmaps; + data = p_image.data; + } + + _FORCE_INLINE_ void _get_mipmap_offset_and_size(int p_mipmap, int &r_offset, int &r_width, int &r_height) const; //get where the mipmap begins in data + + static int _get_dst_image_size(int p_width, int p_height, Format p_format, int &r_mipmaps, int p_mipmaps = -1, int *r_mm_width = nullptr, int *r_mm_height = nullptr); + bool _can_modify(Format p_format) const; + + _FORCE_INLINE_ void _put_pixelb(int p_x, int p_y, uint32_t p_pixelsize, uint8_t *p_data, const uint8_t *p_pixel); + _FORCE_INLINE_ void _get_pixelb(int p_x, int p_y, uint32_t p_pixelsize, const uint8_t *p_data, uint8_t *p_pixel); + + void _set_data(const Dictionary &p_data); + Dictionary _get_data() const; + + Error _load_from_buffer(const Vector<uint8_t> &p_array, ImageMemLoadFunc p_loader); + + static void average_4_uint8(uint8_t &p_out, const uint8_t &p_a, const uint8_t &p_b, const uint8_t &p_c, const uint8_t &p_d); + static void average_4_float(float &p_out, const float &p_a, const float &p_b, const float &p_c, const float &p_d); + static void average_4_half(uint16_t &p_out, const uint16_t &p_a, const uint16_t &p_b, const uint16_t &p_c, const uint16_t &p_d); + static void average_4_rgbe9995(uint32_t &p_out, const uint32_t &p_a, const uint32_t &p_b, const uint32_t &p_c, const uint32_t &p_d); + static void renormalize_uint8(uint8_t *p_rgb); + static void renormalize_float(float *p_rgb); + static void renormalize_half(uint16_t *p_rgb); + static void renormalize_rgbe9995(uint32_t *p_rgb); + +public: + int get_width() const; ///< Get image width + int get_height() const; ///< Get image height + Vector2 get_size() const; + bool has_mipmaps() const; + int get_mipmap_count() const; + + /** + * Convert the image to another format, conversion only to raw byte format + */ + void convert(Format p_new_format); + + /** + * Get the current image format. + */ + Format get_format() const; + + int get_mipmap_byte_size(int p_mipmap) const; //get where the mipmap begins in data + int get_mipmap_offset(int p_mipmap) const; //get where the mipmap begins in data + void get_mipmap_offset_and_size(int p_mipmap, int &r_ofs, int &r_size) const; //get where the mipmap begins in data + void get_mipmap_offset_size_and_dimensions(int p_mipmap, int &r_ofs, int &r_size, int &w, int &h) const; //get where the mipmap begins in data + + enum Image3DValidateError { + VALIDATE_3D_OK, + VALIDATE_3D_ERR_IMAGE_EMPTY, + VALIDATE_3D_ERR_MISSING_IMAGES, + VALIDATE_3D_ERR_EXTRA_IMAGES, + VALIDATE_3D_ERR_IMAGE_SIZE_MISMATCH, + VALIDATE_3D_ERR_IMAGE_FORMAT_MISMATCH, + VALIDATE_3D_ERR_IMAGE_HAS_MIPMAPS, + }; + + static Image3DValidateError validate_3d_image(Format p_format, int p_width, int p_height, int p_depth, bool p_mipmaps, const Vector<Ref<Image>> &p_images); + static String get_3d_image_validation_error_text(Image3DValidateError p_error); + + /** + * Resize the image, using the preferred interpolation method. + */ + void resize_to_po2(bool p_square = false, Interpolation p_interpolation = INTERPOLATE_BILINEAR); + void resize(int p_width, int p_height, Interpolation p_interpolation = INTERPOLATE_BILINEAR); + void shrink_x2(); + bool is_size_po2() const; + /** + * Crop the image to a specific size, if larger, then the image is filled by black + */ + void crop_from_point(int p_x, int p_y, int p_width, int p_height); + void crop(int p_width, int p_height); + + void flip_x(); + void flip_y(); + + /** + * Generate a mipmap to an image (creates an image 1/4 the size, with averaging of 4->1) + */ + Error generate_mipmaps(bool p_renormalize = false); + + enum RoughnessChannel { + ROUGHNESS_CHANNEL_R, + ROUGHNESS_CHANNEL_G, + ROUGHNESS_CHANNEL_B, + ROUGHNESS_CHANNEL_A, + ROUGHNESS_CHANNEL_L, + }; + + Error generate_mipmap_roughness(RoughnessChannel p_roughness_channel, const Ref<Image> &p_normal_map); + + void clear_mipmaps(); + void normalize(); //for normal maps + + /** + * Create a new image of a given size and format. Current image will be lost + */ + void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format); + void create(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data); + + void create(const char **p_xpm); + /** + * returns true when the image is empty (0,0) in size + */ + bool is_empty() const; + + Vector<uint8_t> get_data() const; + + Error load(const String &p_path); + Error save_png(const String &p_path) const; + Vector<uint8_t> save_png_to_buffer() const; + Error save_exr(const String &p_path, bool p_grayscale) const; + + /** + * create an empty image + */ + Image() {} + /** + * create an empty image of a specific size and format + */ + Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format); + /** + * import an image of a specific size and format from a pointer + */ + Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const Vector<uint8_t> &p_data); + + ~Image() {} + + enum AlphaMode { + ALPHA_NONE, + ALPHA_BIT, + ALPHA_BLEND + }; + + AlphaMode detect_alpha() const; + bool is_invisible() const; + + static int get_format_pixel_size(Format p_format); + static int get_format_pixel_rshift(Format p_format); + static int get_format_block_size(Format p_format); + static void get_format_min_pixel_size(Format p_format, int &r_w, int &r_h); + + static int get_image_data_size(int p_width, int p_height, Format p_format, bool p_mipmaps = false); + static int get_image_required_mipmaps(int p_width, int p_height, Format p_format); + static Size2i get_image_mipmap_size(int p_width, int p_height, Format p_format, int p_mipmap); + static int get_image_mipmap_offset(int p_width, int p_height, Format p_format, int p_mipmap); + static int get_image_mipmap_offset_and_dimensions(int p_width, int p_height, Format p_format, int p_mipmap, int &r_w, int &r_h); + + enum CompressMode { + COMPRESS_S3TC, + COMPRESS_PVRTC1_4, + COMPRESS_ETC, + COMPRESS_ETC2, + COMPRESS_BPTC, + }; + enum CompressSource { + COMPRESS_SOURCE_GENERIC, + COMPRESS_SOURCE_SRGB, + COMPRESS_SOURCE_NORMAL + }; + + Error compress(CompressMode p_mode, CompressSource p_source = COMPRESS_SOURCE_GENERIC, float p_lossy_quality = 0.7); + Error compress_from_channels(CompressMode p_mode, UsedChannels p_channels, float p_lossy_quality = 0.7); + Error decompress(); + bool is_compressed() const; + + void fix_alpha_edges(); + void premultiply_alpha(); + void srgb_to_linear(); + void normal_map_to_xy(); + Ref<Image> rgbe_to_srgb(); + Ref<Image> get_image_from_mipmap(int p_mipamp) const; + void bump_map_to_normal_map(float bump_scale = 1.0); + + void blit_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest); + void blit_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest); + void blend_rect(const Ref<Image> &p_src, const Rect2 &p_src_rect, const Point2 &p_dest); + void blend_rect_mask(const Ref<Image> &p_src, const Ref<Image> &p_mask, const Rect2 &p_src_rect, const Point2 &p_dest); + void fill(const Color &c); + + Rect2 get_used_rect() const; + Ref<Image> get_rect(const Rect2 &p_area) const; + + static void set_compress_bc_func(void (*p_compress_func)(Image *, float, UsedChannels)); + static void set_compress_bptc_func(void (*p_compress_func)(Image *, float, UsedChannels)); + static String get_format_name(Format p_format); + + Error load_png_from_buffer(const Vector<uint8_t> &p_array); + Error load_jpg_from_buffer(const Vector<uint8_t> &p_array); + Error load_webp_from_buffer(const Vector<uint8_t> &p_array); + Error load_tga_from_buffer(const Vector<uint8_t> &p_array); + Error load_bmp_from_buffer(const Vector<uint8_t> &p_array); + + void convert_rg_to_ra_rgba8(); + void convert_ra_rgba8_to_rg(); + + Image(const uint8_t *p_mem_png_jpg, int p_len = -1); + Image(const char **p_xpm); + + virtual Ref<Resource> duplicate(bool p_subresources = false) const override; + + UsedChannels detect_used_channels(CompressSource p_source = COMPRESS_SOURCE_GENERIC); + void optimize_channels(); + + Color get_pixelv(const Point2i &p_point) const; + Color get_pixel(int p_x, int p_y) const; + void set_pixelv(const Point2i &p_point, const Color &p_color); + void set_pixel(int p_x, int p_y, const Color &p_color); + + void set_as_black(); + + void copy_internals_from(const Ref<Image> &p_image) { + ERR_FAIL_COND_MSG(p_image.is_null(), "It's not a reference to a valid Image object."); + format = p_image->format; + width = p_image->width; + height = p_image->height; + mipmaps = p_image->mipmaps; + data = p_image->data; + } +}; + +VARIANT_ENUM_CAST(Image::Format) +VARIANT_ENUM_CAST(Image::Interpolation) +VARIANT_ENUM_CAST(Image::CompressMode) +VARIANT_ENUM_CAST(Image::CompressSource) +VARIANT_ENUM_CAST(Image::UsedChannels) +VARIANT_ENUM_CAST(Image::AlphaMode) +VARIANT_ENUM_CAST(Image::RoughnessChannel) + +#endif // IMAGE_H diff --git a/core/io/image_loader.cpp b/core/io/image_loader.cpp index 01e6bb5618..8ca1cb3beb 100644 --- a/core/io/image_loader.cpp +++ b/core/io/image_loader.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,16 +30,15 @@ #include "image_loader.h" -#include "core/print_string.h" +#include "core/string/print_string.h" bool ImageFormatLoader::recognize(const String &p_extension) const { - List<String> extensions; get_recognized_extensions(&extensions); for (List<String>::Element *E = extensions.front(); E; E = E->next()) { - - if (E->get().nocasecmp_to(p_extension) == 0) + if (E->get().nocasecmp_to(p_extension) == 0) { return true; + } } return false; @@ -61,43 +60,41 @@ Error ImageLoader::load_image(String p_file, Ref<Image> p_image, FileAccess *p_c String extension = p_file.get_extension(); for (int i = 0; i < loader.size(); i++) { - - if (!loader[i]->recognize(extension)) + if (!loader[i]->recognize(extension)) { continue; + } Error err = loader[i]->load_image(p_image, f, p_force_linear, p_scale); if (err != OK) { ERR_PRINT("Error loading image: " + p_file); } if (err != ERR_FILE_UNRECOGNIZED) { - - if (!p_custom) + if (!p_custom) { memdelete(f); + } return err; } } - if (!p_custom) + if (!p_custom) { memdelete(f); + } return ERR_FILE_UNRECOGNIZED; } void ImageLoader::get_recognized_extensions(List<String> *p_extensions) { - for (int i = 0; i < loader.size(); i++) { - loader[i]->get_recognized_extensions(p_extensions); } } ImageFormatLoader *ImageLoader::recognize(const String &p_extension) { - for (int i = 0; i < loader.size(); i++) { - - if (loader[i]->recognize(p_extension)) + if (loader[i]->recognize(p_extension)) { return loader[i]; + } } return nullptr; @@ -106,22 +103,18 @@ ImageFormatLoader *ImageLoader::recognize(const String &p_extension) { Vector<ImageFormatLoader *> ImageLoader::loader; void ImageLoader::add_image_format_loader(ImageFormatLoader *p_loader) { - loader.push_back(p_loader); } void ImageLoader::remove_image_format_loader(ImageFormatLoader *p_loader) { - loader.erase(p_loader); } const Vector<ImageFormatLoader *> &ImageLoader::get_image_format_loaders() { - return loader; } void ImageLoader::cleanup() { - while (loader.size()) { remove_image_format_loader(loader[0]); } @@ -130,7 +123,6 @@ void ImageLoader::cleanup() { ///////////////// RES ResourceFormatLoaderImage::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) { - FileAccess *f = FileAccess::open(p_path, FileAccess::READ); if (!f) { if (r_error) { @@ -192,16 +184,13 @@ RES ResourceFormatLoaderImage::load(const String &p_path, const String &p_origin } void ResourceFormatLoaderImage::get_recognized_extensions(List<String> *p_extensions) const { - p_extensions->push_back("image"); } bool ResourceFormatLoaderImage::handles_type(const String &p_type) const { - return p_type == "Image"; } String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const { - return p_path.get_extension().to_lower() == "image" ? "Image" : String(); } diff --git a/core/io/image_loader.h b/core/io/image_loader.h index 15ce6031d7..bf67e1486f 100644 --- a/core/io/image_loader.h +++ b/core/io/image_loader.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,11 +31,11 @@ #ifndef IMAGE_LOADER_H #define IMAGE_LOADER_H -#include "core/image.h" +#include "core/io/image.h" #include "core/io/resource_loader.h" -#include "core/list.h" #include "core/os/file_access.h" -#include "core/ustring.h" +#include "core/string/ustring.h" +#include "core/templates/list.h" class ImageLoader; @@ -53,7 +53,6 @@ public: }; class ImageLoader { - static Vector<ImageFormatLoader *> loader; friend class ResourceFormatLoaderImage; diff --git a/core/io/ip.cpp b/core/io/ip.cpp index 5de7fb7186..6fb812e78d 100644 --- a/core/io/ip.cpp +++ b/core/io/ip.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,18 +30,16 @@ #include "ip.h" -#include "core/hash_map.h" #include "core/os/semaphore.h" #include "core/os/thread.h" +#include "core/templates/hash_map.h" VARIANT_ENUM_CAST(IP::ResolverStatus); /************* RESOLVER ******************/ struct _IP_ResolverPrivate { - struct QueueItem { - volatile IP::ResolverStatus status; IP_Address response; String hostname; @@ -56,16 +54,16 @@ struct _IP_ResolverPrivate { QueueItem() { clear(); - }; + } }; QueueItem queue[IP::RESOLVER_MAX_QUERIES]; IP::ResolverID find_empty_id() const { - for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) { - if (queue[i].status == IP::RESOLVER_STATUS_NONE) + if (queue[i].status == IP::RESOLVER_STATUS_NONE) { return i; + } } return IP::RESOLVER_INVALID_ID; } @@ -78,26 +76,24 @@ struct _IP_ResolverPrivate { bool thread_abort; void resolve_queues() { - for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) { - - if (queue[i].status != IP::RESOLVER_STATUS_WAITING) + if (queue[i].status != IP::RESOLVER_STATUS_WAITING) { continue; + } queue[i].response = IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type); - if (!queue[i].response.is_valid()) + if (!queue[i].response.is_valid()) { queue[i].status = IP::RESOLVER_STATUS_ERROR; - else + } else { queue[i].status = IP::RESOLVER_STATUS_DONE; + } } } static void _thread_function(void *self) { - _IP_ResolverPrivate *ipr = (_IP_ResolverPrivate *)self; while (!ipr->thread_abort) { - ipr->sem.wait(); MutexLock lock(ipr->mutex); @@ -113,7 +109,6 @@ struct _IP_ResolverPrivate { }; IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) { - MutexLock lock(resolver->mutex); String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type); @@ -128,7 +123,6 @@ IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) { } IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) { - MutexLock lock(resolver->mutex); ResolverID id = resolver->find_empty_id(); @@ -147,17 +141,17 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Typ } else { resolver->queue[id].response = IP_Address(); resolver->queue[id].status = IP::RESOLVER_STATUS_WAITING; - if (resolver->thread) + if (resolver->thread) { resolver->sem.post(); - else + } else { resolver->resolve_queues(); + } } return id; } IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const { - ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE); MutexLock lock(resolver->mutex); @@ -171,7 +165,6 @@ IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const { } IP_Address IP::get_resolve_item_address(ResolverID p_id) const { - ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP_Address()); MutexLock lock(resolver->mutex); @@ -186,7 +179,6 @@ IP_Address IP::get_resolve_item_address(ResolverID p_id) const { } void IP::erase_resolve_item(ResolverID p_id) { - ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES); MutexLock lock(resolver->mutex); @@ -195,10 +187,9 @@ void IP::erase_resolve_item(ResolverID p_id) { } void IP::clear_cache(const String &p_hostname) { - MutexLock lock(resolver->mutex); - if (p_hostname.empty()) { + if (p_hostname.is_empty()) { resolver->cache.clear(); } else { resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE)); @@ -209,7 +200,6 @@ void IP::clear_cache(const String &p_hostname) { } Array IP::_get_local_addresses() const { - Array addresses; List<IP_Address> ip_addresses; get_local_addresses(&ip_addresses); @@ -221,7 +211,6 @@ Array IP::_get_local_addresses() const { } Array IP::_get_local_interfaces() const { - Array results; Map<String, Interface_Info> interfaces; get_local_interfaces(&interfaces); @@ -245,7 +234,6 @@ Array IP::_get_local_interfaces() const { } void IP::get_local_addresses(List<IP_Address> *r_addresses) const { - Map<String, Interface_Info> interfaces; get_local_interfaces(&interfaces); for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) { @@ -256,7 +244,6 @@ void IP::get_local_addresses(List<IP_Address> *r_addresses) const { } void IP::_bind_methods() { - ClassDB::bind_method(D_METHOD("resolve_hostname", "host", "ip_type"), &IP::resolve_hostname, DEFVAL(IP::TYPE_ANY)); ClassDB::bind_method(D_METHOD("resolve_hostname_queue_item", "host", "ip_type"), &IP::resolve_hostname_queue_item, DEFVAL(IP::TYPE_ANY)); ClassDB::bind_method(D_METHOD("get_resolve_item_status", "id"), &IP::get_resolve_item_status); @@ -283,21 +270,18 @@ void IP::_bind_methods() { IP *IP::singleton = nullptr; IP *IP::get_singleton() { - return singleton; } IP *(*IP::_create)() = nullptr; IP *IP::create() { - ERR_FAIL_COND_V_MSG(singleton, nullptr, "IP singleton already exist."); ERR_FAIL_COND_V(!_create, nullptr); return _create(); } IP::IP() { - singleton = this; resolver = memnew(_IP_ResolverPrivate); @@ -312,7 +296,6 @@ IP::IP() { } IP::~IP() { - #ifndef NO_THREADS if (resolver->thread) { resolver->thread_abort = true; diff --git a/core/io/ip.h b/core/io/ip.h index d434d02f9b..ae080b8e26 100644 --- a/core/io/ip.h +++ b/core/io/ip.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -42,7 +42,6 @@ class IP : public Object { public: enum ResolverStatus { - RESOLVER_STATUS_NONE, RESOLVER_STATUS_WAITING, RESOLVER_STATUS_DONE, @@ -50,7 +49,6 @@ public: }; enum Type { - TYPE_NONE = 0, TYPE_IPV4 = 1, TYPE_IPV6 = 2, diff --git a/core/io/ip_address.cpp b/core/io/ip_address.cpp index f5fd8ae205..5f98eb69e8 100644 --- a/core/io/ip_address.cpp +++ b/core/io/ip_address.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,7 +31,6 @@ #include "ip_address.h" /* IP_Address::operator Variant() const { - return operator String(); }*/ @@ -39,40 +38,40 @@ IP_Address::operator Variant() const { #include <string.h> IP_Address::operator String() const { - - if (wildcard) + if (wildcard) { return "*"; + } - if (!valid) + if (!valid) { return ""; + } - if (is_ipv4()) + if (is_ipv4()) { // IPv4 address mapped to IPv6 return itos(field8[12]) + "." + itos(field8[13]) + "." + itos(field8[14]) + "." + itos(field8[15]); + } String ret; for (int i = 0; i < 8; i++) { - if (i > 0) + if (i > 0) { ret = ret + ":"; + } uint16_t num = (field8[i * 2] << 8) + field8[i * 2 + 1]; ret = ret + String::num_int64(num, 16); - }; + } return ret; } static void _parse_hex(const String &p_string, int p_start, uint8_t *p_dst) { - uint16_t ret = 0; for (int i = p_start; i < p_start + 4; i++) { - if (i >= p_string.length()) { break; - }; + } int n = 0; - CharType c = p_string[i]; + char32_t c = p_string[i]; if (c >= '0' && c <= '9') { - n = c - '0'; } else if (c >= 'a' && c <= 'f') { n = 10 + (c - 'a'); @@ -82,17 +81,16 @@ static void _parse_hex(const String &p_string, int p_start, uint8_t *p_dst) { break; } else { ERR_FAIL_MSG("Invalid character in IPv6 address: " + p_string + "."); - }; + } ret = ret << 4; ret += n; - }; + } p_dst[0] = ret >> 8; p_dst[1] = ret & 0xff; -}; +} void IP_Address::_parse_ipv6(const String &p_string) { - static const int parts_total = 8; int parts[parts_total] = { 0 }; int parts_count = 0; @@ -102,20 +100,17 @@ void IP_Address::_parse_ipv6(const String &p_string) { int parts_idx = 0; for (int i = 0; i < p_string.length(); i++) { - - CharType c = p_string[i]; + char32_t c = p_string[i]; if (c == ':') { - if (i == 0) { continue; // next must be a ":" - }; + } if (!part_found) { part_skip = true; parts[parts_idx++] = -1; - }; + } part_found = false; } else if (c == '.') { - part_ipv4 = true; } else if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { @@ -123,58 +118,54 @@ void IP_Address::_parse_ipv6(const String &p_string) { parts[parts_idx++] = i; part_found = true; ++parts_count; - }; + } } else { ERR_FAIL_MSG("Invalid character in IPv6 address: " + p_string + "."); - }; - }; + } + } int parts_extra = 0; if (part_skip) { parts_extra = parts_total - parts_count; - }; + } int idx = 0; for (int i = 0; i < parts_idx; i++) { - if (parts[i] == -1) { - for (int j = 0; j < parts_extra; j++) { field16[idx++] = 0; - }; + } continue; - }; + } if (part_ipv4 && i == parts_idx - 1) { _parse_ipv4(p_string, parts[i], (uint8_t *)&field16[idx]); // should be the last one } else { _parse_hex(p_string, parts[i], (uint8_t *)&(field16[idx++])); - }; - }; -}; + } + } +} void IP_Address::_parse_ipv4(const String &p_string, int p_start, uint8_t *p_ret) { - String ip; if (p_start != 0) { ip = p_string.substr(p_start, p_string.length() - p_start); } else { ip = p_string; - }; + } int slices = ip.get_slice_count("."); ERR_FAIL_COND_MSG(slices != 4, "Invalid IP address string: " + ip + "."); for (int i = 0; i < 4; i++) { p_ret[i] = ip.get_slicec('.', i).to_int(); } -}; +} void IP_Address::clear() { - memset(&field8[0], 0, sizeof(field8)); valid = false; wildcard = false; -}; +} bool IP_Address::is_ipv4() const { return (field32[0] == 0 && field32[1] == 0 && field16[4] == 0 && field16[5] == 0xffff); @@ -199,12 +190,12 @@ const uint8_t *IP_Address::get_ipv6() const { void IP_Address::set_ipv6(const uint8_t *p_buf) { clear(); valid = true; - for (int i = 0; i < 16; i++) + for (int i = 0; i < 16; i++) { field8[i] = p_buf[i]; + } } IP_Address::IP_Address(const String &p_string) { - clear(); if (p_string == "*") { @@ -228,15 +219,13 @@ IP_Address::IP_Address(const String &p_string) { } _FORCE_INLINE_ static void _32_to_buf(uint8_t *p_dst, uint32_t p_n) { - p_dst[0] = (p_n >> 24) & 0xff; p_dst[1] = (p_n >> 16) & 0xff; p_dst[2] = (p_n >> 8) & 0xff; p_dst[3] = (p_n >> 0) & 0xff; -}; +} IP_Address::IP_Address(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, bool is_v6) { - clear(); valid = true; if (!is_v6) { @@ -247,7 +236,6 @@ IP_Address::IP_Address(uint32_t p_a, uint32_t p_b, uint32_t p_c, uint32_t p_d, b field8[14] = p_c; field8[15] = p_d; } else { - _32_to_buf(&field8[0], p_a); _32_to_buf(&field8[4], p_b); _32_to_buf(&field8[8], p_c); diff --git a/core/io/ip_address.h b/core/io/ip_address.h index 89cf37ff8f..49bf83d72f 100644 --- a/core/io/ip_address.h +++ b/core/io/ip_address.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,10 +31,9 @@ #ifndef IP_ADDRESS_H #define IP_ADDRESS_H -#include "core/ustring.h" +#include "core/string/ustring.h" struct IP_Address { - private: union { uint8_t field8[16]; @@ -52,19 +51,32 @@ protected: public: //operator Variant() const; bool operator==(const IP_Address &p_ip) const { - if (p_ip.valid != valid) return false; - if (!valid) return false; - for (int i = 0; i < 4; i++) - if (field32[i] != p_ip.field32[i]) + if (p_ip.valid != valid) { + return false; + } + if (!valid) { + return false; + } + for (int i = 0; i < 4; i++) { + if (field32[i] != p_ip.field32[i]) { return false; + } + } return true; } + bool operator!=(const IP_Address &p_ip) const { - if (p_ip.valid != valid) return true; - if (!valid) return true; - for (int i = 0; i < 4; i++) - if (field32[i] != p_ip.field32[i]) + if (p_ip.valid != valid) { + return true; + } + if (!valid) { + return true; + } + for (int i = 0; i < 4; i++) { + if (field32[i] != p_ip.field32[i]) { return true; + } + } return false; } diff --git a/core/io/json.cpp b/core/io/json.cpp index 3a0edceb81..bc4527869b 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,7 +30,7 @@ #include "json.h" -#include "core/print_string.h" +#include "core/string/print_string.h" const char *JSON::tk_name[TK_MAX] = { "'{'", @@ -46,38 +46,39 @@ const char *JSON::tk_name[TK_MAX] = { }; static String _make_indent(const String &p_indent, int p_size) { - String indent_text = ""; - if (!p_indent.empty()) { - for (int i = 0; i < p_size; i++) + if (!p_indent.is_empty()) { + for (int i = 0; i < p_size; i++) { indent_text += p_indent; + } } return indent_text; } String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys) { - String colon = ":"; String end_statement = ""; - if (!p_indent.empty()) { + if (!p_indent.is_empty()) { colon += " "; end_statement += "\n"; } switch (p_var.get_type()) { - - case Variant::NIL: return "null"; - case Variant::BOOL: return p_var.operator bool() ? "true" : "false"; - case Variant::INT: return itos(p_var); - case Variant::FLOAT: return rtos(p_var); + case Variant::NIL: + return "null"; + case Variant::BOOL: + return p_var.operator bool() ? "true" : "false"; + case Variant::INT: + return itos(p_var); + case Variant::FLOAT: + return rtos(p_var); case Variant::PACKED_INT32_ARRAY: case Variant::PACKED_INT64_ARRAY: case Variant::PACKED_FLOAT32_ARRAY: case Variant::PACKED_FLOAT64_ARRAY: case Variant::PACKED_STRING_ARRAY: case Variant::ARRAY: { - String s = "["; s += end_statement; Array a = p_var; @@ -90,20 +91,19 @@ String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_ } s += end_statement + _make_indent(p_indent, p_cur_indent) + "]"; return s; - }; + } case Variant::DICTIONARY: { - String s = "{"; s += end_statement; Dictionary d = p_var; List<Variant> keys; d.get_key_list(&keys); - if (p_sort_keys) + if (p_sort_keys) { keys.sort(); + } for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { - if (E != keys.front()) { s += ","; s += end_statement; @@ -115,69 +115,59 @@ String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_ s += end_statement + _make_indent(p_indent, p_cur_indent) + "}"; return s; - }; - default: return "\"" + String(p_var).json_escape() + "\""; + } + default: + return "\"" + String(p_var).json_escape() + "\""; } } String JSON::print(const Variant &p_var, const String &p_indent, bool p_sort_keys) { - return _print_var(p_var, p_indent, 0, p_sort_keys); } -Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) { - +Error JSON::_get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str) { while (p_len > 0) { switch (p_str[index]) { - case '\n': { - line++; index++; break; - }; + } case 0: { r_token.type = TK_EOF; return OK; } break; case '{': { - r_token.type = TK_CURLY_BRACKET_OPEN; index++; return OK; - }; + } case '}': { - r_token.type = TK_CURLY_BRACKET_CLOSE; index++; return OK; - }; + } case '[': { - r_token.type = TK_BRACKET_OPEN; index++; return OK; - }; + } case ']': { - r_token.type = TK_BRACKET_CLOSE; index++; return OK; - }; + } case ':': { - r_token.type = TK_COLON; index++; return OK; - }; + } case ',': { - r_token.type = TK_COMMA; index++; return OK; - }; + } case '"': { - index++; String str; while (true) { @@ -190,34 +180,42 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to } else if (p_str[index] == '\\') { //escaped characters... index++; - CharType next = p_str[index]; + char32_t next = p_str[index]; if (next == 0) { r_err_str = "Unterminated String"; return ERR_PARSE_ERROR; } - CharType res = 0; + char32_t res = 0; switch (next) { - - case 'b': res = 8; break; - case 't': res = 9; break; - case 'n': res = 10; break; - case 'f': res = 12; break; - case 'r': res = 13; break; + case 'b': + res = 8; + break; + case 't': + res = 9; + break; + case 'n': + res = 10; + break; + case 'f': + res = 12; + break; + case 'r': + res = 13; + break; case 'u': { // hex number for (int j = 0; j < 4; j++) { - CharType c = p_str[index + j + 1]; + char32_t c = p_str[index + j + 1]; if (c == 0) { r_err_str = "Unterminated String"; return ERR_PARSE_ERROR; } if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))) { - r_err_str = "Malformed hex constant in string"; return ERR_PARSE_ERROR; } - CharType v; + char32_t v; if (c >= '0' && c <= '9') { v = c - '0'; } else if (c >= 'a' && c <= 'f') { @@ -245,8 +243,9 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to str += res; } else { - if (p_str[index] == '\n') + if (p_str[index] == '\n') { line++; + } str += p_str[index]; } index++; @@ -258,7 +257,6 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to } break; default: { - if (p_str[index] <= 32) { index++; break; @@ -266,19 +264,17 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to if (p_str[index] == '-' || (p_str[index] >= '0' && p_str[index] <= '9')) { //a number - const CharType *rptr; - double number = String::to_double(&p_str[index], &rptr); + const char32_t *rptr; + double number = String::to_float(&p_str[index], &rptr); index += (rptr - &p_str[index]); r_token.type = TK_NUMBER; r_token.value = number; return OK; } else if ((p_str[index] >= 'A' && p_str[index] <= 'Z') || (p_str[index] >= 'a' && p_str[index] <= 'z')) { - String id; while ((p_str[index] >= 'A' && p_str[index] <= 'Z') || (p_str[index] >= 'a' && p_str[index] <= 'z')) { - id += p_str[index]; index++; } @@ -297,46 +293,42 @@ Error JSON::_get_token(const CharType *p_str, int &index, int p_len, Token &r_to return ERR_PARSE_ERROR; } -Error JSON::_parse_value(Variant &value, Token &token, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str) { - +Error JSON::_parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) { if (token.type == TK_CURLY_BRACKET_OPEN) { - Dictionary d; Error err = _parse_object(d, p_str, index, p_len, line, r_err_str); - if (err) + if (err) { return err; + } value = d; return OK; } else if (token.type == TK_BRACKET_OPEN) { - Array a; Error err = _parse_array(a, p_str, index, p_len, line, r_err_str); - if (err) + if (err) { return err; + } value = a; return OK; } else if (token.type == TK_IDENTIFIER) { - String id = token.value; - if (id == "true") + if (id == "true") { value = true; - else if (id == "false") + } else if (id == "false") { value = false; - else if (id == "null") + } else if (id == "null") { value = Variant(); - else { + } else { r_err_str = "Expected 'true','false' or 'null', got '" + id + "'."; return ERR_PARSE_ERROR; } return OK; } else if (token.type == TK_NUMBER) { - value = token.value; return OK; } else if (token.type == TK_STRING) { - value = token.value; return OK; } else { @@ -345,26 +337,22 @@ Error JSON::_parse_value(Variant &value, Token &token, const CharType *p_str, in } } -Error JSON::_parse_array(Array &array, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str) { - +Error JSON::_parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) { Token token; bool need_comma = false; while (index < p_len) { - Error err = _get_token(p_str, index, p_len, token, line, r_err_str); - if (err != OK) + if (err != OK) { return err; + } if (token.type == TK_BRACKET_CLOSE) { - return OK; } if (need_comma) { - if (token.type != TK_COMMA) { - r_err_str = "Expected ','"; return ERR_PARSE_ERROR; } else { @@ -375,40 +363,37 @@ Error JSON::_parse_array(Array &array, const CharType *p_str, int &index, int p_ Variant v; err = _parse_value(v, token, p_str, index, p_len, line, r_err_str); - if (err) + if (err) { return err; + } array.push_back(v); need_comma = true; } + r_err_str = "Expected ']'"; return ERR_PARSE_ERROR; } -Error JSON::_parse_object(Dictionary &object, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str) { - +Error JSON::_parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str) { bool at_key = true; String key; Token token; bool need_comma = false; while (index < p_len) { - if (at_key) { - Error err = _get_token(p_str, index, p_len, token, line, r_err_str); - if (err != OK) + if (err != OK) { return err; + } if (token.type == TK_CURLY_BRACKET_CLOSE) { - return OK; } if (need_comma) { - if (token.type != TK_COMMA) { - r_err_str = "Expected '}' or ','"; return ERR_PARSE_ERROR; } else { @@ -418,43 +403,43 @@ Error JSON::_parse_object(Dictionary &object, const CharType *p_str, int &index, } if (token.type != TK_STRING) { - r_err_str = "Expected key"; return ERR_PARSE_ERROR; } key = token.value; err = _get_token(p_str, index, p_len, token, line, r_err_str); - if (err != OK) + if (err != OK) { return err; + } if (token.type != TK_COLON) { - r_err_str = "Expected ':'"; return ERR_PARSE_ERROR; } at_key = false; } else { - Error err = _get_token(p_str, index, p_len, token, line, r_err_str); - if (err != OK) + if (err != OK) { return err; + } Variant v; err = _parse_value(v, token, p_str, index, p_len, line, r_err_str); - if (err) + if (err) { return err; + } object[key] = v; need_comma = true; at_key = true; } } + r_err_str = "Expected '}'"; return ERR_PARSE_ERROR; } Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line) { - - const CharType *str = p_json.ptr(); + const char32_t *str = p_json.ptr(); int idx = 0; int len = p_json.length(); Token token; @@ -462,10 +447,43 @@ Error JSON::parse(const String &p_json, Variant &r_ret, String &r_err_str, int & String aux_key; Error err = _get_token(str, idx, len, token, r_err_line, r_err_str); - if (err) + if (err) { return err; + } err = _parse_value(r_ret, token, str, idx, len, r_err_line, r_err_str); return err; } + +Error JSONParser::parse_string(const String &p_json_string) { + return JSON::parse(p_json_string, data, err_text, err_line); +} +String JSONParser::get_error_text() const { + return err_text; +} +int JSONParser::get_error_line() const { + return err_line; +} +Variant JSONParser::get_data() const { + return data; +} + +Error JSONParser::decode_data(const Variant &p_data, const String &p_indent, bool p_sort_keys) { + string = JSON::print(p_data, p_indent, p_sort_keys); + data = p_data; + return OK; +} + +String JSONParser::get_string() const { + return string; +} + +void JSONParser::_bind_methods() { + ClassDB::bind_method(D_METHOD("parse_string", "json_string"), &JSONParser::parse_string); + ClassDB::bind_method(D_METHOD("get_error_text"), &JSONParser::get_error_text); + ClassDB::bind_method(D_METHOD("get_error_line"), &JSONParser::get_error_line); + ClassDB::bind_method(D_METHOD("get_data"), &JSONParser::get_data); + ClassDB::bind_method(D_METHOD("decode_data", "data", "indent", "sort_keys"), &JSONParser::decode_data, DEFVAL(""), DEFVAL(true)); + ClassDB::bind_method(D_METHOD("get_string"), &JSONParser::get_string); +} diff --git a/core/io/json.h b/core/io/json.h index 2e851afcf4..537477666e 100644 --- a/core/io/json.h +++ b/core/io/json.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,10 +31,9 @@ #ifndef JSON_H #define JSON_H -#include "core/variant.h" - +#include "core/object/reference.h" +#include "core/variant/variant.h" class JSON { - enum TokenType { TK_CURLY_BRACKET_OPEN, TK_CURLY_BRACKET_CLOSE, @@ -50,7 +49,6 @@ class JSON { }; enum Expecting { - EXPECT_OBJECT, EXPECT_OBJECT_KEY, EXPECT_COLON, @@ -58,7 +56,6 @@ class JSON { }; struct Token { - TokenType type; Variant value; }; @@ -67,14 +64,35 @@ class JSON { static String _print_var(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys); - static Error _get_token(const CharType *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str); - static Error _parse_value(Variant &value, Token &token, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str); - static Error _parse_array(Array &array, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str); - static Error _parse_object(Dictionary &object, const CharType *p_str, int &index, int p_len, int &line, String &r_err_str); + static Error _get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str); + static Error _parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); + static Error _parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); + static Error _parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, String &r_err_str); public: static String print(const Variant &p_var, const String &p_indent = "", bool p_sort_keys = true); static Error parse(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line); }; +class JSONParser : public Reference { + GDCLASS(JSONParser, Reference); + + Variant data; + String string; + String err_text; + int err_line = 0; + +protected: + static void _bind_methods(); + +public: + Error parse_string(const String &p_json_string); + String get_error_text() const; + int get_error_line() const; + Variant get_data() const; + + Error decode_data(const Variant &p_data, const String &p_indent = "", bool p_sort_keys = true); + String get_string() const; +}; + #endif // JSON_H diff --git a/core/io/logger.cpp b/core/io/logger.cpp index ad0cc81023..da200f5717 100644 --- a/core/io/logger.cpp +++ b/core/io/logger.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,9 +30,10 @@ #include "logger.h" +#include "core/config/project_settings.h" #include "core/os/dir_access.h" #include "core/os/os.h" -#include "core/print_string.h" +#include "core/string/print_string.h" #if defined(MINGW_ENABLED) || defined(_MSC_VER) #define sprintf sprintf_s @@ -49,18 +50,29 @@ void Logger::log_error(const char *p_function, const char *p_file, int p_line, c const char *err_type = "ERROR"; switch (p_type) { - case ERR_ERROR: err_type = "ERROR"; break; - case ERR_WARNING: err_type = "WARNING"; break; - case ERR_SCRIPT: err_type = "SCRIPT ERROR"; break; - case ERR_SHADER: err_type = "SHADER ERROR"; break; - default: ERR_PRINT("Unknown error type"); break; + case ERR_ERROR: + err_type = "ERROR"; + break; + case ERR_WARNING: + err_type = "WARNING"; + break; + case ERR_SCRIPT: + err_type = "SCRIPT ERROR"; + break; + case ERR_SHADER: + err_type = "SHADER ERROR"; + break; + default: + ERR_PRINT("Unknown error type"); + break; } const char *err_details; - if (p_rationale && *p_rationale) + if (p_rationale && *p_rationale) { err_details = p_rationale; - else + } else { err_details = p_code; + } logf_error("%s: %s\n", err_type, err_details); logf_error(" at: %s (%s:%i) - %s\n", p_function, p_file, p_line, p_code); @@ -92,8 +104,6 @@ void Logger::logf_error(const char *p_format, ...) { va_end(argp); } -Logger::~Logger() {} - void RotatedFileLogger::close_file() { if (file) { memdelete(file); @@ -143,7 +153,7 @@ void RotatedFileLogger::rotate_file() { char timestamp[21]; OS::Date date = OS::get_singleton()->get_date(); OS::Time time = OS::get_singleton()->get_time(); - sprintf(timestamp, "-%04d-%02d-%02d-%02d-%02d-%02d", date.year, date.month, date.day, time.hour, time.min, time.sec); + sprintf(timestamp, "_%04d-%02d-%02d_%02d.%02d.%02d", date.year, date.month, date.day, time.hour, time.min, time.sec); String backup_name = base_path.get_basename() + timestamp; if (base_path.get_extension() != String()) { @@ -170,8 +180,7 @@ void RotatedFileLogger::rotate_file() { RotatedFileLogger::RotatedFileLogger(const String &p_base_path, int p_max_files) : base_path(p_base_path.simplify_path()), - max_files(p_max_files > 0 ? p_max_files : 1), - file(nullptr) { + max_files(p_max_files > 0 ? p_max_files : 1) { rotate_file(); } @@ -193,15 +202,14 @@ void RotatedFileLogger::logv(const char *p_format, va_list p_list, bool p_err) { } va_end(list_copy); file->store_buffer((uint8_t *)buf, len); + if (len >= static_buf_size) { Memory::free_static(buf); } -#ifdef DEBUG_ENABLED - const bool need_flush = true; -#else - bool need_flush = p_err; -#endif - if (need_flush) { + + if (p_err || GLOBAL_GET("application/run/flush_stdout_on_print")) { + // Don't always flush when printing stdout to avoid performance + // issues when `print()` is spammed in release builds. file->flush(); } } @@ -220,14 +228,14 @@ void StdLogger::logv(const char *p_format, va_list p_list, bool p_err) { vfprintf(stderr, p_format, p_list); } else { vprintf(p_format, p_list); -#ifdef DEBUG_ENABLED - fflush(stdout); -#endif + if (GLOBAL_GET("application/run/flush_stdout_on_print")) { + // Don't always flush when printing stdout to avoid performance + // issues when `print()` is spammed in release builds. + fflush(stdout); + } } } -StdLogger::~StdLogger() {} - CompositeLogger::CompositeLogger(Vector<Logger *> p_loggers) : loggers(p_loggers) { } diff --git a/core/io/logger.h b/core/io/logger.h index 7028551185..b8e615b436 100644 --- a/core/io/logger.h +++ b/core/io/logger.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -32,8 +32,8 @@ #define LOGGER_H #include "core/os/file_access.h" -#include "core/ustring.h" -#include "core/vector.h" +#include "core/string/ustring.h" +#include "core/templates/vector.h" #include <stdarg.h> @@ -55,17 +55,16 @@ public: void logf(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; void logf_error(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_2_3; - virtual ~Logger(); + virtual ~Logger() {} }; /** * Writes messages to stdout/stderr. */ class StdLogger : public Logger { - public: virtual void logv(const char *p_format, va_list p_list, bool p_err) _PRINTF_FORMAT_ATTRIBUTE_2_0; - virtual ~StdLogger(); + virtual ~StdLogger() {} }; /** @@ -78,7 +77,7 @@ class RotatedFileLogger : public Logger { String base_path; int max_files; - FileAccess *file; + FileAccess *file = nullptr; void rotate_file_without_closing(); void close_file(); diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index 81bc45b2f7..218a612da2 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,9 +30,9 @@ #include "marshalls.h" +#include "core/object/reference.h" #include "core/os/keyboard.h" -#include "core/print_string.h" -#include "core/reference.h" +#include "core/string/print_string.h" #include <limits.h> #include <stdio.h> @@ -49,13 +49,9 @@ void EncodedObjectAsID::set_object_id(ObjectID p_id) { } ObjectID EncodedObjectAsID::get_object_id() const { - return id; } -EncodedObjectAsID::EncodedObjectAsID() { -} - #define _S(a) ((int32_t)a) #define ERR_FAIL_ADD_OF(a, b, err) ERR_FAIL_COND_V(_S(b) < 0 || _S(a) < 0 || _S(a) > INT_MAX - _S(b), err) #define ERR_FAIL_MUL_OF(a, b, err) ERR_FAIL_COND_V(_S(a) < 0 || _S(b) <= 0 || _S(a) > INT_MAX / _S(b), err) @@ -100,7 +96,6 @@ static Error _decode_string(const uint8_t *&buf, int &len, int *r_len, String &r } Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len, bool p_allow_objects) { - const uint8_t *buf = p_buffer; int len = p_len; @@ -112,95 +107,95 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int buf += 4; len -= 4; - if (r_len) + if (r_len) { *r_len = 4; + } switch (type & ENCODE_MASK) { - case Variant::NIL: { - r_variant = Variant(); } break; case Variant::BOOL: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); bool val = decode_uint32(buf); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4; + } } break; case Variant::INT: { - if (type & ENCODE_FLAG_64) { ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA); int64_t val = decode_uint64(buf); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 8; + } } else { ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t val = decode_uint32(buf); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4; + } } } break; case Variant::FLOAT: { - if (type & ENCODE_FLAG_64) { ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA); double val = decode_double(buf); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 8; + } } else { ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); float val = decode_float(buf); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4; + } } } break; case Variant::STRING: { - String str; Error err = _decode_string(buf, len, r_len, str); - if (err) + if (err) { return err; + } r_variant = str; } break; // math types case Variant::VECTOR2: { - ERR_FAIL_COND_V(len < 4 * 2, ERR_INVALID_DATA); Vector2 val; val.x = decode_float(&buf[0]); val.y = decode_float(&buf[4]); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 2; + } } break; case Variant::VECTOR2I: { - ERR_FAIL_COND_V(len < 4 * 2, ERR_INVALID_DATA); Vector2i val; val.x = decode_uint32(&buf[0]); val.y = decode_uint32(&buf[4]); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 2; + } } break; case Variant::RECT2: { - ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA); Rect2 val; val.position.x = decode_float(&buf[0]); @@ -209,12 +204,12 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int val.size.y = decode_float(&buf[12]); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 4; + } } break; case Variant::RECT2I: { - ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA); Rect2i val; val.position.x = decode_uint32(&buf[0]); @@ -223,12 +218,12 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int val.size.y = decode_uint32(&buf[12]); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 4; + } } break; case Variant::VECTOR3: { - ERR_FAIL_COND_V(len < 4 * 3, ERR_INVALID_DATA); Vector3 val; val.x = decode_float(&buf[0]); @@ -236,12 +231,12 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int val.z = decode_float(&buf[8]); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 3; + } } break; case Variant::VECTOR3I: { - ERR_FAIL_COND_V(len < 4 * 3, ERR_INVALID_DATA); Vector3i val; val.x = decode_uint32(&buf[0]); @@ -249,29 +244,28 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int val.z = decode_uint32(&buf[8]); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 3; + } } break; case Variant::TRANSFORM2D: { - ERR_FAIL_COND_V(len < 4 * 6, ERR_INVALID_DATA); Transform2D val; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { - val.elements[i][j] = decode_float(&buf[(i * 2 + j) * 4]); } } r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 6; + } } break; case Variant::PLANE: { - ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA); Plane val; val.normal.x = decode_float(&buf[0]); @@ -280,12 +274,12 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int val.d = decode_float(&buf[12]); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 4; + } } break; case Variant::QUAT: { - ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA); Quat val; val.x = decode_float(&buf[0]); @@ -294,12 +288,12 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int val.w = decode_float(&buf[12]); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 4; + } } break; case Variant::AABB: { - ERR_FAIL_COND_V(len < 4 * 6, ERR_INVALID_DATA); AABB val; val.position.x = decode_float(&buf[0]); @@ -310,34 +304,32 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int val.size.z = decode_float(&buf[20]); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 6; + } } break; case Variant::BASIS: { - ERR_FAIL_COND_V(len < 4 * 9, ERR_INVALID_DATA); Basis val; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { - val.elements[i][j] = decode_float(&buf[(i * 3 + j) * 4]); } } r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 9; + } } break; case Variant::TRANSFORM: { - ERR_FAIL_COND_V(len < 4 * 12, ERR_INVALID_DATA); Transform val; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { - val.basis.elements[i][j] = decode_float(&buf[(i * 3 + j) * 4]); } } @@ -347,14 +339,14 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 12; + } } break; // misc types case Variant::COLOR: { - ERR_FAIL_COND_V(len < 4 * 4, ERR_INVALID_DATA); Color val; val.r = decode_float(&buf[0]); @@ -363,22 +355,22 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int val.a = decode_float(&buf[12]); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4 * 4; + } } break; case Variant::STRING_NAME: { - String str; Error err = _decode_string(buf, len, r_len, str); - if (err) + if (err) { return err; + } r_variant = StringName(str); } break; case Variant::NODE_PATH: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t strlen = decode_uint32(buf); @@ -395,25 +387,28 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int len -= 12; buf += 12; - if (flags & 2) // Obsolete format with property separate from subpath + if (flags & 2) { // Obsolete format with property separate from subpath subnamecount++; + } uint32_t total = namecount + subnamecount; - if (r_len) + if (r_len) { (*r_len) += 12; + } for (uint32_t i = 0; i < total; i++) { - String str; Error err = _decode_string(buf, len, r_len, str); - if (err) + if (err) { return err; + } - if (i < namecount) + if (i < namecount) { names.push_back(str); - else + } else { subnames.push_back(str); + } } r_variant = NodePath(names, subnames, flags & 1); @@ -425,18 +420,17 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } } break; - case Variant::_RID: { - + case Variant::RID: { r_variant = RID(); } break; case Variant::OBJECT: { - if (type & ENCODE_FLAG_OBJECT_AS_ID) { //this _is_ allowed ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA); ObjectID val = ObjectID(decode_uint64(buf)); - if (r_len) + if (r_len) { (*r_len) += 8; + } if (val.is_null()) { r_variant = (Object *)nullptr; @@ -453,13 +447,13 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int String str; Error err = _decode_string(buf, len, r_len, str); - if (err) + if (err) { return err; + } if (str == String()) { r_variant = (Object *)nullptr; } else { - Object *obj = ClassDB::instance(str); ERR_FAIL_COND_V(!obj, ERR_UNAVAILABLE); @@ -473,17 +467,18 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } for (int i = 0; i < count; i++) { - str = String(); err = _decode_string(buf, len, r_len, str); - if (err) + if (err) { return err; + } Variant value; int used; err = decode_variant(value, buf, len, &used, p_allow_objects); - if (err) + if (err) { return err; + } buf += used; len -= used; @@ -505,16 +500,13 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } break; case Variant::CALLABLE: { - r_variant = Callable(); } break; case Variant::SIGNAL: { - r_variant = Signal(); } break; case Variant::DICTIONARY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t count = decode_uint32(buf); // bool shared = count&0x80000000; @@ -530,7 +522,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int Dictionary d; for (int i = 0; i < count; i++) { - Variant key, value; int used; @@ -559,7 +550,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } break; case Variant::ARRAY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t count = decode_uint32(buf); // bool shared = count&0x80000000; @@ -575,7 +565,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int Array varr; for (int i = 0; i < count; i++) { - int used = 0; Variant v; Error err = decode_variant(v, buf, len, &used, p_allow_objects); @@ -594,7 +583,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int // arrays case Variant::PACKED_BYTE_ARRAY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t count = decode_uint32(buf); buf += 4; @@ -607,7 +595,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int data.resize(count); uint8_t *w = data.ptrw(); for (int32_t i = 0; i < count; i++) { - w[i] = buf[i]; } } @@ -615,14 +602,14 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int r_variant = data; if (r_len) { - if (count % 4) + if (count % 4) { (*r_len) += 4 - count % 4; + } (*r_len) += 4 + count; } } break; case Variant::PACKED_INT32_ARRAY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t count = decode_uint32(buf); buf += 4; @@ -637,7 +624,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int data.resize(count); int32_t *w = data.ptrw(); for (int32_t i = 0; i < count; i++) { - w[i] = decode_uint32(&buf[i * 4]); } } @@ -648,7 +634,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } break; case Variant::PACKED_INT64_ARRAY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int64_t count = decode_uint64(buf); buf += 4; @@ -663,7 +648,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int data.resize(count); int64_t *w = data.ptrw(); for (int64_t i = 0; i < count; i++) { - w[i] = decode_uint64(&buf[i * 8]); } } @@ -674,7 +658,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } break; case Variant::PACKED_FLOAT32_ARRAY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t count = decode_uint32(buf); buf += 4; @@ -689,7 +672,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int data.resize(count); float *w = data.ptrw(); for (int32_t i = 0; i < count; i++) { - w[i] = decode_float(&buf[i * 4]); } } @@ -701,7 +683,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } break; case Variant::PACKED_FLOAT64_ARRAY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int64_t count = decode_uint64(buf); buf += 4; @@ -716,7 +697,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int data.resize(count); double *w = data.ptrw(); for (int64_t i = 0; i < count; i++) { - w[i] = decode_double(&buf[i * 8]); } } @@ -728,7 +708,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } break; case Variant::PACKED_STRING_ARRAY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t count = decode_uint32(buf); @@ -736,16 +715,17 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int buf += 4; len -= 4; - if (r_len) + if (r_len) { (*r_len) += 4; + } //printf("string count: %i\n",count); for (int32_t i = 0; i < count; i++) { - String str; Error err = _decode_string(buf, len, r_len, str); - if (err) + if (err) { return err; + } strings.push_back(str); } @@ -754,7 +734,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } break; case Variant::PACKED_VECTOR2_ARRAY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t count = decode_uint32(buf); buf += 4; @@ -773,22 +752,21 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int Vector2 *w = varray.ptrw(); for (int32_t i = 0; i < count; i++) { - w[i].x = decode_float(buf + i * 4 * 2 + 4 * 0); w[i].y = decode_float(buf + i * 4 * 2 + 4 * 1); } int adv = 4 * 2 * count; - if (r_len) + if (r_len) { (*r_len) += adv; + } } r_variant = varray; } break; case Variant::PACKED_VECTOR3_ARRAY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t count = decode_uint32(buf); buf += 4; @@ -808,7 +786,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int Vector3 *w = varray.ptrw(); for (int32_t i = 0; i < count; i++) { - w[i].x = decode_float(buf + i * 4 * 3 + 4 * 0); w[i].y = decode_float(buf + i * 4 * 3 + 4 * 1); w[i].z = decode_float(buf + i * 4 * 3 + 4 * 2); @@ -816,15 +793,15 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int int adv = 4 * 3 * count; - if (r_len) + if (r_len) { (*r_len) += adv; + } } r_variant = varray; } break; case Variant::PACKED_COLOR_ARRAY: { - ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t count = decode_uint32(buf); buf += 4; @@ -844,7 +821,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int Color *w = carray.ptrw(); for (int32_t i = 0; i < count; i++) { - w[i].r = decode_float(buf + i * 4 * 4 + 4 * 0); w[i].g = decode_float(buf + i * 4 * 4 + 4 * 1); w[i].b = decode_float(buf + i * 4 * 4 + 4 * 2); @@ -853,8 +829,9 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int int adv = 4 * 4 * count; - if (r_len) + if (r_len) { (*r_len) += adv; + } } r_variant = carray; @@ -869,7 +846,6 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int } static void _encode_string(const String &p_string, uint8_t *&buf, int &r_len) { - CharString utf8 = p_string.utf8(); if (buf) { @@ -889,7 +865,6 @@ static void _encode_string(const String &p_string, uint8_t *&buf, int &r_len) { } Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bool p_full_objects) { - uint8_t *buf = r_buffer; r_len = 0; @@ -897,7 +872,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo uint32_t flags = 0; switch (p_variant.get_type()) { - case Variant::INT: { int64_t val = p_variant; if (val > (int64_t)INT_MAX || val < (int64_t)INT_MIN) { @@ -905,7 +879,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } } break; case Variant::FLOAT: { - double d = p_variant; float f = d; if (double(f) != d) { @@ -913,7 +886,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } } break; case Variant::OBJECT: { - // Test for potential wrong values sent by the debugger when it breaks. Object *obj = p_variant.get_validated_object(); if (!obj) { @@ -940,13 +912,10 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4; switch (p_variant.get_type()) { - case Variant::NIL: { - //nothing to do } break; case Variant::BOOL: { - if (buf) { encode_uint32(p_variant.operator bool(), buf); } @@ -955,7 +924,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::INT: { - if (flags & ENCODE_FLAG_64) { //64 bits if (buf) { @@ -972,7 +940,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } } break; case Variant::FLOAT: { - if (flags & ENCODE_FLAG_64) { if (buf) { encode_double(p_variant.operator double(), buf); @@ -981,7 +948,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 8; } else { - if (buf) { encode_float(p_variant.operator float(), buf); } @@ -991,14 +957,14 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::NODE_PATH: { - NodePath np = p_variant; if (buf) { encode_uint32(uint32_t(np.get_name_count()) | 0x80000000, buf); //for compatibility with the old format encode_uint32(np.get_subname_count(), buf + 4); uint32_t np_flags = 0; - if (np.is_absolute()) + if (np.is_absolute()) { np_flags |= 1; + } encode_uint32(np_flags, buf + 8); @@ -1010,20 +976,21 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo int total = np.get_name_count() + np.get_subname_count(); for (int i = 0; i < total; i++) { - String str; - if (i < np.get_name_count()) + if (i < np.get_name_count()) { str = np.get_name(i); - else + } else { str = np.get_subname(i - np.get_name_count()); + } CharString utf8 = str.utf8(); int pad = 0; - if (utf8.length() % 4) + if (utf8.length() % 4) { pad = 4 - utf8.length() % 4; + } if (buf) { encode_uint32(utf8.length(), buf); @@ -1036,20 +1003,14 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } } break; - case Variant::STRING: { - - _encode_string(p_variant, buf, r_len); - - } break; + case Variant::STRING: case Variant::STRING_NAME: { - _encode_string(p_variant, buf, r_len); } break; // math types case Variant::VECTOR2: { - if (buf) { Vector2 v2 = p_variant; encode_float(v2.x, &buf[0]); @@ -1060,7 +1021,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::VECTOR2I: { - if (buf) { Vector2i v2 = p_variant; encode_uint32(v2.x, &buf[0]); @@ -1071,7 +1031,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::RECT2: { - if (buf) { Rect2 r2 = p_variant; encode_float(r2.position.x, &buf[0]); @@ -1083,7 +1042,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::RECT2I: { - if (buf) { Rect2i r2 = p_variant; encode_uint32(r2.position.x, &buf[0]); @@ -1095,7 +1053,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::VECTOR3: { - if (buf) { Vector3 v3 = p_variant; encode_float(v3.x, &buf[0]); @@ -1107,7 +1064,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::VECTOR3I: { - if (buf) { Vector3i v3 = p_variant; encode_uint32(v3.x, &buf[0]); @@ -1119,12 +1075,10 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::TRANSFORM2D: { - if (buf) { Transform2D val = p_variant; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { - copymem(&buf[(i * 2 + j) * 4], &val.elements[i][j], sizeof(float)); } } @@ -1134,7 +1088,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::PLANE: { - if (buf) { Plane p = p_variant; encode_float(p.normal.x, &buf[0]); @@ -1147,7 +1100,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::QUAT: { - if (buf) { Quat q = p_variant; encode_float(q.x, &buf[0]); @@ -1160,7 +1112,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::AABB: { - if (buf) { AABB aabb = p_variant; encode_float(aabb.position.x, &buf[0]); @@ -1175,12 +1126,10 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::BASIS: { - if (buf) { Basis val = p_variant; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { - copymem(&buf[(i * 3 + j) * 4], &val.elements[i][j], sizeof(float)); } } @@ -1190,12 +1139,10 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::TRANSFORM: { - if (buf) { Transform val = p_variant; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { - copymem(&buf[(i * 3 + j) * 4], &val.basis.elements[i][j], sizeof(float)); } } @@ -1211,7 +1158,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo // misc types case Variant::COLOR: { - if (buf) { Color c = p_variant; encode_float(c.r, &buf[0]); @@ -1223,19 +1169,14 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4 * 4; } break; - case Variant::_RID: { - + case Variant::RID: { } break; case Variant::CALLABLE: { - } break; case Variant::SIGNAL: { - } break; case Variant::OBJECT: { - if (p_full_objects) { - Object *obj = p_variant; if (!obj) { if (buf) { @@ -1251,9 +1192,9 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo int pc = 0; for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) { - - if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { continue; + } pc++; } @@ -1265,25 +1206,26 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4; for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) { - - if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { continue; + } _encode_string(E->get().name, buf, r_len); int len; Error err = encode_variant(obj->get(E->get().name), buf, len, p_full_objects); - if (err) + if (err) { return err; + } ERR_FAIL_COND_V(len % 4, ERR_BUG); r_len += len; - if (buf) + if (buf) { buf += len; + } } } } else { if (buf) { - Object *obj = p_variant.get_validated_object(); ObjectID id; if (obj) { @@ -1298,7 +1240,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::DICTIONARY: { - Dictionary d = p_variant; if (buf) { @@ -1311,7 +1252,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo d.get_key_list(&keys); for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { - /* CharString utf8 = E->->utf8(); @@ -1329,20 +1269,21 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo encode_variant(E->get(), buf, len, p_full_objects); ERR_FAIL_COND_V(len % 4, ERR_BUG); r_len += len; - if (buf) + if (buf) { buf += len; + } Variant *v = d.getptr(E->get()); ERR_FAIL_COND_V(!v, ERR_BUG); encode_variant(*v, buf, len, p_full_objects); ERR_FAIL_COND_V(len % 4, ERR_BUG); r_len += len; - if (buf) + if (buf) { buf += len; + } } } break; case Variant::ARRAY: { - Array v = p_variant; if (buf) { @@ -1353,19 +1294,18 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4; for (int i = 0; i < v.size(); i++) { - int len; encode_variant(v.get(i), buf, len, p_full_objects); ERR_FAIL_COND_V(len % 4, ERR_BUG); r_len += len; - if (buf) + if (buf) { buf += len; + } } } break; // arrays case Variant::PACKED_BYTE_ARRAY: { - Vector<uint8_t> data = p_variant; int datalen = data.size(); int datasize = sizeof(uint8_t); @@ -1381,13 +1321,13 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4 + datalen * datasize; while (r_len % 4) { r_len++; - if (buf) + if (buf) { *(buf++) = 0; + } } } break; case Variant::PACKED_INT32_ARRAY: { - Vector<int32_t> data = p_variant; int datalen = data.size(); int datasize = sizeof(int32_t); @@ -1396,15 +1336,15 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo encode_uint32(datalen, buf); buf += 4; const int32_t *r = data.ptr(); - for (int32_t i = 0; i < datalen; i++) + for (int32_t i = 0; i < datalen; i++) { encode_uint32(r[i], &buf[i * datasize]); + } } r_len += 4 + datalen * datasize; } break; case Variant::PACKED_INT64_ARRAY: { - Vector<int64_t> data = p_variant; int datalen = data.size(); int datasize = sizeof(int64_t); @@ -1413,15 +1353,15 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo encode_uint64(datalen, buf); buf += 4; const int64_t *r = data.ptr(); - for (int64_t i = 0; i < datalen; i++) + for (int64_t i = 0; i < datalen; i++) { encode_uint64(r[i], &buf[i * datasize]); + } } r_len += 4 + datalen * datasize; } break; case Variant::PACKED_FLOAT32_ARRAY: { - Vector<float> data = p_variant; int datalen = data.size(); int datasize = sizeof(float); @@ -1430,15 +1370,15 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo encode_uint32(datalen, buf); buf += 4; const float *r = data.ptr(); - for (int i = 0; i < datalen; i++) + for (int i = 0; i < datalen; i++) { encode_float(r[i], &buf[i * datasize]); + } } r_len += 4 + datalen * datasize; } break; case Variant::PACKED_FLOAT64_ARRAY: { - Vector<double> data = p_variant; int datalen = data.size(); int datasize = sizeof(double); @@ -1447,15 +1387,15 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo encode_uint32(datalen, buf); buf += 4; const double *r = data.ptr(); - for (int i = 0; i < datalen; i++) + for (int i = 0; i < datalen; i++) { encode_double(r[i], &buf[i * datasize]); + } } r_len += 4 + datalen * datasize; } break; case Variant::PACKED_STRING_ARRAY: { - Vector<String> data = p_variant; int len = data.size(); @@ -1467,7 +1407,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4; for (int i = 0; i < len; i++) { - CharString utf8 = data.get(i).utf8(); if (buf) { @@ -1480,14 +1419,14 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4 + utf8.length() + 1; while (r_len % 4) { r_len++; //pad - if (buf) + if (buf) { *(buf++) = 0; + } } } } break; case Variant::PACKED_VECTOR2_ARRAY: { - Vector<Vector2> data = p_variant; int len = data.size(); @@ -1499,9 +1438,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4; if (buf) { - for (int i = 0; i < len; i++) { - Vector2 v = data.get(i); encode_float(v.x, &buf[0]); @@ -1514,7 +1451,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::PACKED_VECTOR3_ARRAY: { - Vector<Vector3> data = p_variant; int len = data.size(); @@ -1526,9 +1462,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4; if (buf) { - for (int i = 0; i < len; i++) { - Vector3 v = data.get(i); encode_float(v.x, &buf[0]); @@ -1542,7 +1476,6 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo } break; case Variant::PACKED_COLOR_ARRAY: { - Vector<Color> data = p_variant; int len = data.size(); @@ -1554,9 +1487,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo r_len += 4; if (buf) { - for (int i = 0; i < len; i++) { - Color c = data.get(i); encode_float(c.r, &buf[0]); diff --git a/core/io/marshalls.h b/core/io/marshalls.h index d029ed238c..cc0e9ba301 100644 --- a/core/io/marshalls.h +++ b/core/io/marshalls.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,9 +31,9 @@ #ifndef MARSHALLS_H #define MARSHALLS_H -#include "core/reference.h" +#include "core/object/reference.h" #include "core/typedefs.h" -#include "core/variant.h" +#include "core/variant/variant.h" /** * Miscellaneous helpers for marshalling data types, and encoding @@ -41,21 +41,17 @@ */ union MarshallFloat { - uint32_t i; ///< int float f; ///< float }; union MarshallDouble { - uint64_t l; ///< long long double d; ///< double }; static inline unsigned int encode_uint16(uint16_t p_uint, uint8_t *p_arr) { - for (int i = 0; i < 2; i++) { - *p_arr = p_uint & 0xFF; p_arr++; p_uint >>= 8; @@ -65,9 +61,7 @@ static inline unsigned int encode_uint16(uint16_t p_uint, uint8_t *p_arr) { } static inline unsigned int encode_uint32(uint32_t p_uint, uint8_t *p_arr) { - for (int i = 0; i < 4; i++) { - *p_arr = p_uint & 0xFF; p_arr++; p_uint >>= 8; @@ -77,7 +71,6 @@ static inline unsigned int encode_uint32(uint32_t p_uint, uint8_t *p_arr) { } static inline unsigned int encode_float(float p_float, uint8_t *p_arr) { - MarshallFloat mf; mf.f = p_float; encode_uint32(mf.i, p_arr); @@ -86,9 +79,7 @@ static inline unsigned int encode_float(float p_float, uint8_t *p_arr) { } static inline unsigned int encode_uint64(uint64_t p_uint, uint8_t *p_arr) { - for (int i = 0; i < 8; i++) { - *p_arr = p_uint & 0xFF; p_arr++; p_uint >>= 8; @@ -98,7 +89,6 @@ static inline unsigned int encode_uint64(uint64_t p_uint, uint8_t *p_arr) { } static inline unsigned int encode_double(double p_double, uint8_t *p_arr) { - MarshallDouble md; md.d = p_double; encode_uint64(md.l, p_arr); @@ -107,30 +97,27 @@ static inline unsigned int encode_double(double p_double, uint8_t *p_arr) { } static inline int encode_cstring(const char *p_string, uint8_t *p_data) { - int len = 0; while (*p_string) { - if (p_data) { - *p_data = (uint8_t)*p_string; p_data++; } p_string++; len++; - }; + } - if (p_data) *p_data = 0; + if (p_data) { + *p_data = 0; + } return len + 1; } static inline uint16_t decode_uint16(const uint8_t *p_arr) { - uint16_t u = 0; for (int i = 0; i < 2; i++) { - uint16_t b = *p_arr; b <<= (i * 8); u |= b; @@ -141,11 +128,9 @@ static inline uint16_t decode_uint16(const uint8_t *p_arr) { } static inline uint32_t decode_uint32(const uint8_t *p_arr) { - uint32_t u = 0; for (int i = 0; i < 4; i++) { - uint32_t b = *p_arr; b <<= (i * 8); u |= b; @@ -156,18 +141,15 @@ static inline uint32_t decode_uint32(const uint8_t *p_arr) { } static inline float decode_float(const uint8_t *p_arr) { - MarshallFloat mf; mf.i = decode_uint32(p_arr); return mf.f; } static inline uint64_t decode_uint64(const uint8_t *p_arr) { - uint64_t u = 0; for (int i = 0; i < 8; i++) { - uint64_t b = (*p_arr) & 0xFF; b <<= (i * 8); u |= b; @@ -178,7 +160,6 @@ static inline uint64_t decode_uint64(const uint8_t *p_arr) { } static inline double decode_double(const uint8_t *p_arr) { - MarshallDouble md; md.l = decode_uint64(p_arr); return md.d; @@ -196,7 +177,7 @@ public: void set_object_id(ObjectID p_id); ObjectID get_object_id() const; - EncodedObjectAsID(); + EncodedObjectAsID() {} }; Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len = nullptr, bool p_allow_objects = false); diff --git a/core/io/multiplayer_api.cpp b/core/io/multiplayer_api.cpp index 3bec52416e..6b550e69c8 100644 --- a/core/io/multiplayer_api.cpp +++ b/core/io/multiplayer_api.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,6 +33,7 @@ #include "core/debugger/engine_debugger.h" #include "core/io/marshalls.h" #include "scene/main/node.h" + #include <stdint.h> #define NODE_ID_COMPRESSION_SHIFT 3 @@ -44,9 +45,7 @@ #endif _FORCE_INLINE_ bool _should_call_local(MultiplayerAPI::RPCMode mode, bool is_master, bool &r_skip_rpc) { - switch (mode) { - case MultiplayerAPI::RPC_MODE_DISABLED: { // Do nothing. } break; @@ -54,8 +53,9 @@ _FORCE_INLINE_ bool _should_call_local(MultiplayerAPI::RPCMode mode, bool is_mas // Do nothing also. Remote cannot produce a local call. } break; case MultiplayerAPI::RPC_MODE_MASTERSYNC: { - if (is_master) + if (is_master) { r_skip_rpc = true; // I am the master, so skip remote call. + } [[fallthrough]]; } case MultiplayerAPI::RPC_MODE_REMOTESYNC: @@ -64,8 +64,9 @@ _FORCE_INLINE_ bool _should_call_local(MultiplayerAPI::RPCMode mode, bool is_mas return true; } break; case MultiplayerAPI::RPC_MODE_MASTER: { - if (is_master) + if (is_master) { r_skip_rpc = true; // I am the master, so skip remote call. + } return is_master; } break; case MultiplayerAPI::RPC_MODE_PUPPET: { @@ -77,7 +78,6 @@ _FORCE_INLINE_ bool _should_call_local(MultiplayerAPI::RPCMode mode, bool is_mas _FORCE_INLINE_ bool _can_call_mode(Node *p_node, MultiplayerAPI::RPCMode mode, int p_remote_id) { switch (mode) { - case MultiplayerAPI::RPC_MODE_DISABLED: { return false; } break; @@ -99,17 +99,17 @@ _FORCE_INLINE_ bool _can_call_mode(Node *p_node, MultiplayerAPI::RPCMode mode, i } void MultiplayerAPI::poll() { - - if (!network_peer.is_valid() || network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED) + if (!network_peer.is_valid() || network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED) { return; + } network_peer->poll(); - if (!network_peer.is_valid()) // It's possible that polling might have resulted in a disconnection, so check here. + if (!network_peer.is_valid()) { // It's possible that polling might have resulted in a disconnection, so check here. return; + } while (network_peer->get_available_packet_count()) { - int sender = network_peer->get_packet_peer(); const uint8_t *packet; int len; @@ -142,9 +142,14 @@ void MultiplayerAPI::set_root_node(Node *p_node) { root_node = p_node; } -void MultiplayerAPI::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer) { +Node *MultiplayerAPI::get_root_node() { + return root_node; +} - if (p_peer == network_peer) return; // Nothing to do +void MultiplayerAPI::set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer) { + if (p_peer == network_peer) { + return; // Nothing to do + } ERR_FAIL_COND_MSG(p_peer.is_valid() && p_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_DISCONNECTED, "Supplied NetworkedMultiplayerPeer must be connecting or connected."); @@ -183,6 +188,7 @@ void _profile_node_data(const String &p_what, ObjectID p_id) { EngineDebugger::profiler_add_frame_data("multiplayer", values); } } + void _profile_bandwidth_data(const String &p_inout, int p_size) { if (EngineDebugger::is_profiling("multiplayer")) { Array values; @@ -206,7 +212,6 @@ int get_packet_len(uint32_t p_node_target, int p_packet_len) { } void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_packet_len) { - ERR_FAIL_COND_MSG(root_node == nullptr, "Multiplayer root node was not initialized. If you are using custom multiplayer, remember to set the root node via MultiplayerAPI.set_root_node before using it."); ERR_FAIL_COND_MSG(p_packet_len < 1, "Invalid packet received. Size too small."); @@ -218,20 +223,16 @@ void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_ uint8_t packet_type = p_packet[0] & 7; switch (packet_type) { - case NETWORK_COMMAND_SIMPLIFY_PATH: { - _process_simplify_path(p_from, p_packet, p_packet_len); } break; case NETWORK_COMMAND_CONFIRM_PATH: { - _process_confirm_path(p_from, p_packet, p_packet_len); } break; case NETWORK_COMMAND_REMOTE_CALL: case NETWORK_COMMAND_REMOTE_SET: { - // Extract packet meta int packet_min_size = 1; int name_id_offset = 1; @@ -302,25 +303,21 @@ void MultiplayerAPI::_process_packet(int p_from, const uint8_t *p_packet, int p_ const int packet_len = get_packet_len(node_target, p_packet_len); if (packet_type == NETWORK_COMMAND_REMOTE_CALL) { - _process_rpc(node, name_id, p_from, p_packet, packet_len, packet_min_size); } else { - _process_rset(node, name_id, p_from, p_packet, packet_len, packet_min_size); } } break; case NETWORK_COMMAND_RAW: { - _process_raw(p_from, p_packet, p_packet_len); } break; } } Node *MultiplayerAPI::_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len) { - Node *node = nullptr; if (p_node_target & 0x80000000) { @@ -337,8 +334,9 @@ Node *MultiplayerAPI::_process_get_node(int p_from, const uint8_t *p_packet, uin node = root_node->get_node(np); - if (!node) + if (!node) { ERR_PRINT("Failed to get path from RPC: " + String(np) + "."); + } } else { // Use cached path. int id = p_node_target; @@ -353,14 +351,14 @@ Node *MultiplayerAPI::_process_get_node(int p_from, const uint8_t *p_packet, uin // Do proper caching later. node = root_node->get_node(ni->path); - if (!node) + if (!node) { ERR_PRINT("Failed to get cached path from RPC: " + String(ni->path) + "."); + } } return node; } void MultiplayerAPI::_process_rpc(Node *p_node, const uint16_t p_rpc_method_id, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) { - ERR_FAIL_COND_MSG(p_offset > p_packet_len, "Invalid packet received. Size too small."); // Check that remote can call the RPC on this node. @@ -413,7 +411,6 @@ void MultiplayerAPI::_process_rpc(Node *p_node, const uint16_t p_rpc_method_id, p_offset += len; } else { for (int i = 0; i < argc; i++) { - ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small."); int vlen; @@ -436,7 +433,6 @@ void MultiplayerAPI::_process_rpc(Node *p_node, const uint16_t p_rpc_method_id, } void MultiplayerAPI::_process_rset(Node *p_node, const uint16_t p_rpc_property_id, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset) { - ERR_FAIL_COND_MSG(p_offset >= p_packet_len, "Invalid packet received. Size too small."); // Check that remote can call the RSET on this node. @@ -470,7 +466,6 @@ void MultiplayerAPI::_process_rset(Node *p_node, const uint16_t p_rpc_property_i } void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet, int p_packet_len) { - ERR_FAIL_COND_MSG(p_packet_len < 38, "Invalid packet received. Size too small."); int ofs = 1; @@ -519,7 +514,6 @@ void MultiplayerAPI::_process_simplify_path(int p_from, const uint8_t *p_packet, } void MultiplayerAPI::_process_confirm_path(int p_from, const uint8_t *p_packet, int p_packet_len) { - ERR_FAIL_COND_MSG(p_packet_len < 3, "Invalid packet received. Size too small."); const bool valid_rpc_checksum = p_packet[1]; @@ -546,12 +540,13 @@ bool MultiplayerAPI::_send_confirm_path(Node *p_node, NodePath p_path, PathSentC List<int> peers_to_add; // If one is missing, take note to add it. for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) { - - if (p_target < 0 && E->get() == -p_target) + if (p_target < 0 && E->get() == -p_target) { continue; // Continue, excluded. + } - if (p_target > 0 && E->get() != p_target) + if (p_target > 0 && E->get() != p_target) { continue; // Continue, not for this peer. + } Map<int, bool>::Element *F = psc->confirmed_peers.find(E->get()); @@ -567,7 +562,6 @@ bool MultiplayerAPI::_send_confirm_path(Node *p_node, NodePath p_path, PathSentC } if (peers_to_add.size() > 0) { - // Those that need to be added, send a message for this. // Encode function name. @@ -592,7 +586,6 @@ bool MultiplayerAPI::_send_confirm_path(Node *p_node, NodePath p_path, PathSentC ofs += encode_cstring(path.get_data(), &packet.write[ofs]); for (List<int>::Element *E = peers_to_add.front(); E; E = E->next()) { - network_peer->set_target_peer(E->get()); // To all of you. network_peer->set_transfer_mode(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE); network_peer->put_packet(packet.ptr(), packet.size()); @@ -617,7 +610,6 @@ bool MultiplayerAPI::_send_confirm_path(Node *p_node, NodePath p_path, PathSentC #define ENCODE_32 2 << 5 #define ENCODE_64 3 << 5 Error MultiplayerAPI::_encode_and_compress_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len) { - // Unreachable because `VARIANT_MAX` == 27 and `ENCODE_VARIANT_MASK` == 31 CRASH_COND(p_variant.get_type() > VARIANT_META_TYPE_MASK); @@ -679,8 +671,9 @@ Error MultiplayerAPI::_encode_and_compress_variant(const Variant &p_variant, uin default: // Any other case is not yet compressed. Error err = encode_variant(p_variant, r_buffer, r_len, allow_object_decoding); - if (err != OK) + if (err != OK) { return err; + } if (r_buffer) { // The first byte is not used by the marshaling, so store the type // so we know how to decompress and decode this variant. @@ -690,8 +683,8 @@ Error MultiplayerAPI::_encode_and_compress_variant(const Variant &p_variant, uin return OK; } -Error MultiplayerAPI::_decode_and_decompress_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len) { +Error MultiplayerAPI::_decode_and_decompress_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int *r_len) { const uint8_t *buf = p_buffer; int len = p_len; @@ -705,55 +698,61 @@ Error MultiplayerAPI::_decode_and_decompress_variant(Variant &r_variant, const u case Variant::BOOL: { bool val = (buf[0] & VARIANT_META_BOOL_MASK) > 0; r_variant = val; - if (r_len) + if (r_len) { *r_len = 1; + } } break; case Variant::INT: { buf += 1; len -= 1; - if (r_len) + if (r_len) { *r_len = 1; + } if (encode_mode == ENCODE_8) { // 8 bits. ERR_FAIL_COND_V(len < 1, ERR_INVALID_DATA); int8_t val = buf[0]; r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 1; + } } else if (encode_mode == ENCODE_16) { // 16 bits. ERR_FAIL_COND_V(len < 2, ERR_INVALID_DATA); int16_t val = decode_uint16(buf); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 2; + } } else if (encode_mode == ENCODE_32) { // 32 bits. ERR_FAIL_COND_V(len < 4, ERR_INVALID_DATA); int32_t val = decode_uint32(buf); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 4; + } } else { // 64 bits. ERR_FAIL_COND_V(len < 8, ERR_INVALID_DATA); int64_t val = decode_uint64(buf); r_variant = val; - if (r_len) + if (r_len) { (*r_len) += 8; + } } } break; default: Error err = decode_variant(r_variant, p_buffer, p_len, r_len, allow_object_decoding); - if (err != OK) + if (err != OK) { return err; + } } return OK; } void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p_set, const StringName &p_name, const Variant **p_arg, int p_argcount) { - ERR_FAIL_COND_MSG(network_peer.is_null(), "Attempt to remote call/set when networking is not active in SceneTree."); ERR_FAIL_COND_MSG(network_peer->get_connection_status() == NetworkedMultiplayerPeer::CONNECTION_CONNECTING, "Attempt to remote call/set when networking is not connected yet in SceneTree."); @@ -787,8 +786,9 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p int ofs = 0; -#define MAKE_ROOM(m_amount) \ - if (packet_cache.size() < m_amount) packet_cache.resize(m_amount); +#define MAKE_ROOM(m_amount) \ + if (packet_cache.size() < m_amount) \ + packet_cache.resize(m_amount); // Encode meta. // The meta is composed by a single byte that contains (starting from the least segnificant bit): @@ -838,7 +838,6 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p } if (p_set) { - // Take the rpc property ID uint16_t property_id = p_from->get_node_rset_property_id(p_name); if (property_id == UINT16_MAX && p_from->get_script_instance()) { @@ -931,7 +930,6 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p network_peer->set_transfer_mode(p_unreliable ? NetworkedMultiplayerPeer::TRANSFER_MODE_UNRELIABLE : NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE); if (has_all_peers) { - // They all have verified paths, so send fast. network_peer->set_target_peer(p_to); // To all of you. network_peer->put_packet(packet_cache.ptr(), ofs); // A message with love. @@ -948,12 +946,13 @@ void MultiplayerAPI::_send_rpc(Node *p_from, int p_to, bool p_unreliable, bool p encode_cstring(pname.get_data(), &(packet_cache.write[ofs])); for (Set<int>::Element *E = connected_peers.front(); E; E = E->next()) { - - if (p_to < 0 && E->get() == -p_to) + if (p_to < 0 && E->get() == -p_to) { continue; // Continue, excluded. + } - if (p_to > 0 && E->get() != p_to) + if (p_to > 0 && E->get() != p_to) { continue; // Continue, not for this peer. + } Map<int, bool>::Element *F = psc->confirmed_peers.find(E->get()); ERR_CONTINUE(!F); // Should never happen. @@ -995,22 +994,18 @@ void MultiplayerAPI::_del_peer(int p_id) { } void MultiplayerAPI::_connected_to_server() { - emit_signal("connected_to_server"); } void MultiplayerAPI::_connection_failed() { - emit_signal("connection_failed"); } void MultiplayerAPI::_server_disconnected() { - emit_signal("server_disconnected"); } void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_method, const Variant **p_arg, int p_argcount) { - ERR_FAIL_COND_MSG(!network_peer.is_valid(), "Trying to call an RPC while no network peer is active."); ERR_FAIL_COND_MSG(!p_node->is_inside_tree(), "Trying to call an RPC on a node which is not inside SceneTree."); ERR_FAIL_COND_MSG(network_peer->get_connection_status() != NetworkedMultiplayerPeer::CONNECTION_CONNECTED, "Trying to call an RPC via a network peer which is not connected."); @@ -1037,7 +1032,6 @@ void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const } if (!skip_rpc) { - #ifdef DEBUG_ENABLED _profile_node_data("out_rpc", p_node->get_instance_id()); #endif @@ -1078,7 +1072,6 @@ void MultiplayerAPI::rpcp(Node *p_node, int p_peer_id, bool p_unreliable, const } void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const StringName &p_property, const Variant &p_value) { - ERR_FAIL_COND_MSG(!network_peer.is_valid(), "Trying to RSET while no network peer is active."); ERR_FAIL_COND_MSG(!p_node->is_inside_tree(), "Trying to RSET on a node which is not inside SceneTree."); ERR_FAIL_COND_MSG(network_peer->get_connection_status() != NetworkedMultiplayerPeer::CONNECTION_CONNECTED, "Trying to send an RSET via a network peer which is not connected."); @@ -1143,7 +1136,6 @@ void MultiplayerAPI::rsetp(Node *p_node, int p_peer_id, bool p_unreliable, const } Error MultiplayerAPI::send_bytes(Vector<uint8_t> p_data, int p_to, NetworkedMultiplayerPeer::TransferMode p_mode) { - ERR_FAIL_COND_V_MSG(p_data.size() < 1, ERR_INVALID_DATA, "Trying to send an empty raw packet."); ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), ERR_UNCONFIGURED, "Trying to send a raw packet while no network peer is active."); ERR_FAIL_COND_V_MSG(network_peer->get_connection_status() != NetworkedMultiplayerPeer::CONNECTION_CONNECTED, ERR_UNCONFIGURED, "Trying to send a raw packet via a network peer which is not connected."); @@ -1160,7 +1152,6 @@ Error MultiplayerAPI::send_bytes(Vector<uint8_t> p_data, int p_to, NetworkedMult } void MultiplayerAPI::_process_raw(int p_from, const uint8_t *p_packet, int p_packet_len) { - ERR_FAIL_COND_MSG(p_packet_len < 2, "Invalid packet received. Size too small."); Vector<uint8_t> out; @@ -1174,32 +1165,27 @@ void MultiplayerAPI::_process_raw(int p_from, const uint8_t *p_packet, int p_pac } int MultiplayerAPI::get_network_unique_id() const { - ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), 0, "No network peer is assigned. Unable to get unique network ID."); return network_peer->get_unique_id(); } bool MultiplayerAPI::is_network_server() const { - // XXX Maybe fail silently? Maybe should actually return true to make development of both local and online multiplayer easier? ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), false, "No network peer is assigned. I can't be a server."); return network_peer->is_server(); } void MultiplayerAPI::set_refuse_new_network_connections(bool p_refuse) { - ERR_FAIL_COND_MSG(!network_peer.is_valid(), "No network peer is assigned. Unable to set 'refuse_new_connections'."); network_peer->set_refuse_new_connections(p_refuse); } bool MultiplayerAPI::is_refusing_new_network_connections() const { - ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), false, "No network peer is assigned. Unable to get 'refuse_new_connections'."); return network_peer->is_refusing_new_connections(); } Vector<int> MultiplayerAPI::get_network_connected_peers() const { - ERR_FAIL_COND_V_MSG(!network_peer.is_valid(), Vector<int>(), "No network peer is assigned. Assume no peers are connected."); Vector<int> ret; @@ -1211,17 +1197,16 @@ Vector<int> MultiplayerAPI::get_network_connected_peers() const { } void MultiplayerAPI::set_allow_object_decoding(bool p_enable) { - allow_object_decoding = p_enable; } bool MultiplayerAPI::is_object_decoding_allowed() const { - return allow_object_decoding; } void MultiplayerAPI::_bind_methods() { ClassDB::bind_method(D_METHOD("set_root_node", "node"), &MultiplayerAPI::set_root_node); + ClassDB::bind_method(D_METHOD("get_root_node"), &MultiplayerAPI::get_root_node); ClassDB::bind_method(D_METHOD("send_bytes", "bytes", "id", "mode"), &MultiplayerAPI::send_bytes, DEFVAL(NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST), DEFVAL(NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE)); ClassDB::bind_method(D_METHOD("has_network_peer"), &MultiplayerAPI::has_network_peer); ClassDB::bind_method(D_METHOD("get_network_peer"), &MultiplayerAPI::get_network_peer); @@ -1241,6 +1226,7 @@ void MultiplayerAPI::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_object_decoding"), "set_allow_object_decoding", "is_object_decoding_allowed"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_network_connections"), "set_refuse_new_network_connections", "is_refusing_new_network_connections"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "network_peer", PROPERTY_HINT_RESOURCE_TYPE, "NetworkedMultiplayerPeer", 0), "set_network_peer", "get_network_peer"); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root_node", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_root_node", "get_root_node"); ADD_PROPERTY_DEFAULT("refuse_new_network_connections", false); ADD_SIGNAL(MethodInfo("network_peer_connected", PropertyInfo(Variant::INT, "id"))); @@ -1259,10 +1245,7 @@ void MultiplayerAPI::_bind_methods() { BIND_ENUM_CONSTANT(RPC_MODE_PUPPETSYNC); } -MultiplayerAPI::MultiplayerAPI() : - allow_object_decoding(false) { - rpc_sender_id = 0; - root_node = nullptr; +MultiplayerAPI::MultiplayerAPI() { clear(); } diff --git a/core/io/multiplayer_api.h b/core/io/multiplayer_api.h index 4eb4a53e99..7f88b53a27 100644 --- a/core/io/multiplayer_api.h +++ b/core/io/multiplayer_api.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -32,10 +32,9 @@ #define MULTIPLAYER_API_H #include "core/io/networked_multiplayer_peer.h" -#include "core/reference.h" +#include "core/object/reference.h" class MultiplayerAPI : public Reference { - GDCLASS(MultiplayerAPI, Reference); private: @@ -56,14 +55,14 @@ private: }; Ref<NetworkedMultiplayerPeer> network_peer; - int rpc_sender_id; + int rpc_sender_id = 0; Set<int> connected_peers; HashMap<NodePath, PathSentCache> path_send_cache; Map<int, PathGetCache> path_get_cache; int last_send_cache_id; Vector<uint8_t> packet_cache; - Node *root_node; - bool allow_object_decoding; + Node *root_node = nullptr; + bool allow_object_decoding = false; protected: static void _bind_methods(); @@ -103,7 +102,6 @@ public: }; enum RPCMode { - RPC_MODE_DISABLED, // No rpc for this method, calls to this will be blocked (default) RPC_MODE_REMOTE, // Using rpc() on it will call method / set property in all remote peers RPC_MODE_MASTER, // Using rpc() on it will call method on wherever the master is, be it local or remote @@ -116,6 +114,7 @@ public: void poll(); void clear(); void set_root_node(Node *p_node); + Node *get_root_node(); void set_network_peer(const Ref<NetworkedMultiplayerPeer> &p_peer); Ref<NetworkedMultiplayerPeer> get_network_peer() const; Error send_bytes(Vector<uint8_t> p_data, int p_to = NetworkedMultiplayerPeer::TARGET_PEER_BROADCAST, NetworkedMultiplayerPeer::TransferMode p_mode = NetworkedMultiplayerPeer::TRANSFER_MODE_RELIABLE); diff --git a/core/io/net_socket.cpp b/core/io/net_socket.cpp index 838c674cec..b51d26ba83 100644 --- a/core/io/net_socket.cpp +++ b/core/io/net_socket.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,9 +33,9 @@ NetSocket *(*NetSocket::_create)() = nullptr; NetSocket *NetSocket::create() { - - if (_create) + if (_create) { return _create(); + } ERR_PRINT("Unable to create network socket, platform not supported"); return nullptr; diff --git a/core/io/net_socket.h b/core/io/net_socket.h index 376fd87a27..bc09477693 100644 --- a/core/io/net_socket.h +++ b/core/io/net_socket.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -32,10 +32,9 @@ #define NET_SOCKET_H #include "core/io/ip.h" -#include "core/reference.h" +#include "core/object/reference.h" class NetSocket : public Reference { - protected: static NetSocket *(*_create)(); diff --git a/core/io/networked_multiplayer_peer.cpp b/core/io/networked_multiplayer_peer.cpp index b2f810d212..b6af046e77 100644 --- a/core/io/networked_multiplayer_peer.cpp +++ b/core/io/networked_multiplayer_peer.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,7 +31,6 @@ #include "networked_multiplayer_peer.h" void NetworkedMultiplayerPeer::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_transfer_mode", "mode"), &NetworkedMultiplayerPeer::set_transfer_mode); ClassDB::bind_method(D_METHOD("get_transfer_mode"), &NetworkedMultiplayerPeer::get_transfer_mode); ClassDB::bind_method(D_METHOD("set_target_peer", "id"), &NetworkedMultiplayerPeer::set_target_peer); @@ -66,6 +65,3 @@ void NetworkedMultiplayerPeer::_bind_methods() { ADD_SIGNAL(MethodInfo("connection_succeeded")); ADD_SIGNAL(MethodInfo("connection_failed")); } - -NetworkedMultiplayerPeer::NetworkedMultiplayerPeer() { -} diff --git a/core/io/networked_multiplayer_peer.h b/core/io/networked_multiplayer_peer.h index c1f1924051..7c90f97d88 100644 --- a/core/io/networked_multiplayer_peer.h +++ b/core/io/networked_multiplayer_peer.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -34,7 +34,6 @@ #include "core/io/packet_peer.h" class NetworkedMultiplayerPeer : public PacketPeer { - GDCLASS(NetworkedMultiplayerPeer, PacketPeer); protected: @@ -74,7 +73,7 @@ public: virtual ConnectionStatus get_connection_status() const = 0; - NetworkedMultiplayerPeer(); + NetworkedMultiplayerPeer() {} }; VARIANT_ENUM_CAST(NetworkedMultiplayerPeer::TransferMode) diff --git a/core/io/packed_data_container.cpp b/core/io/packed_data_container.cpp new file mode 100644 index 0000000000..a0b97772e6 --- /dev/null +++ b/core/io/packed_data_container.cpp @@ -0,0 +1,395 @@ +/*************************************************************************/ +/* packed_data_container.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "packed_data_container.h" + +#include "core/core_string_names.h" +#include "core/io/marshalls.h" + +Variant PackedDataContainer::getvar(const Variant &p_key, bool *r_valid) const { + bool err = false; + Variant ret = _key_at_ofs(0, p_key, err); + if (r_valid) { + *r_valid = !err; + } + if (err) { + return Object::getvar(p_key, r_valid); + } + return ret; +} + +int PackedDataContainer::size() const { + return _size(0); +} + +Variant PackedDataContainer::_iter_init_ofs(const Array &p_iter, uint32_t p_offset) { + Array ref = p_iter; + uint32_t size = _size(p_offset); + if (size == 0 || ref.size() != 1) { + return false; + } else { + ref[0] = 0; + return true; + } +} + +Variant PackedDataContainer::_iter_next_ofs(const Array &p_iter, uint32_t p_offset) { + Array ref = p_iter; + int size = _size(p_offset); + if (ref.size() != 1) { + return false; + } + int pos = ref[0]; + if (pos < 0 || pos >= size) { + return false; + } + pos += 1; + ref[0] = pos; + return pos != size; +} + +Variant PackedDataContainer::_iter_get_ofs(const Variant &p_iter, uint32_t p_offset) { + int size = _size(p_offset); + int pos = p_iter; + if (pos < 0 || pos >= size) { + return Variant(); + } + + const uint8_t *rd = data.ptr(); + const uint8_t *r = &rd[p_offset]; + uint32_t type = decode_uint32(r); + + bool err = false; + if (type == TYPE_ARRAY) { + uint32_t vpos = decode_uint32(rd + p_offset + 8 + pos * 4); + return _get_at_ofs(vpos, rd, err); + + } else if (type == TYPE_DICT) { + uint32_t vpos = decode_uint32(rd + p_offset + 8 + pos * 12 + 4); + return _get_at_ofs(vpos, rd, err); + } else { + ERR_FAIL_V(Variant()); + } +} + +Variant PackedDataContainer::_get_at_ofs(uint32_t p_ofs, const uint8_t *p_buf, bool &err) const { + uint32_t type = decode_uint32(p_buf + p_ofs); + + if (type == TYPE_ARRAY || type == TYPE_DICT) { + Ref<PackedDataContainerRef> pdcr = memnew(PackedDataContainerRef); + Ref<PackedDataContainer> pdc = Ref<PackedDataContainer>((PackedDataContainer *)this); + + pdcr->from = pdc; + pdcr->offset = p_ofs; + return pdcr; + } else { + Variant v; + Error rerr = decode_variant(v, p_buf + p_ofs, datalen - p_ofs, nullptr, false); + + if (rerr != OK) { + err = true; + ERR_FAIL_COND_V_MSG(err != OK, Variant(), "Error when trying to decode Variant."); + } + return v; + } +} + +uint32_t PackedDataContainer::_type_at_ofs(uint32_t p_ofs) const { + const uint8_t *rd = data.ptr(); + const uint8_t *r = &rd[p_ofs]; + uint32_t type = decode_uint32(r); + + return type; +} + +int PackedDataContainer::_size(uint32_t p_ofs) const { + const uint8_t *rd = data.ptr(); + ERR_FAIL_COND_V(!rd, 0); + const uint8_t *r = &rd[p_ofs]; + uint32_t type = decode_uint32(r); + + if (type == TYPE_ARRAY) { + uint32_t len = decode_uint32(r + 4); + return len; + + } else if (type == TYPE_DICT) { + uint32_t len = decode_uint32(r + 4); + return len; + } + + return -1; +} + +Variant PackedDataContainer::_key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const { + const uint8_t *rd = data.ptr(); + const uint8_t *r = &rd[p_ofs]; + uint32_t type = decode_uint32(r); + + if (type == TYPE_ARRAY) { + if (p_key.is_num()) { + int idx = p_key; + int len = decode_uint32(r + 4); + if (idx < 0 || idx >= len) { + err = true; + return Variant(); + } + uint32_t ofs = decode_uint32(r + 8 + 4 * idx); + return _get_at_ofs(ofs, rd, err); + + } else { + err = true; + return Variant(); + } + + } else if (type == TYPE_DICT) { + uint32_t hash = p_key.hash(); + uint32_t len = decode_uint32(r + 4); + + bool found = false; + for (uint32_t i = 0; i < len; i++) { + uint32_t khash = decode_uint32(r + 8 + i * 12 + 0); + if (khash == hash) { + Variant key = _get_at_ofs(decode_uint32(r + 8 + i * 12 + 4), rd, err); + if (err) { + return Variant(); + } + if (key == p_key) { + //key matches, return value + return _get_at_ofs(decode_uint32(r + 8 + i * 12 + 8), rd, err); + } + found = true; + } else { + if (found) { + break; + } + } + } + + err = true; + return Variant(); + + } else { + err = true; + return Variant(); + } +} + +uint32_t PackedDataContainer::_pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache) { + switch (p_data.get_type()) { + case Variant::STRING: { + String s = p_data; + if (string_cache.has(s)) { + return string_cache[s]; + } + + string_cache[s] = tmpdata.size(); + + [[fallthrough]]; + } + case Variant::NIL: + case Variant::BOOL: + case Variant::INT: + case Variant::FLOAT: + case Variant::VECTOR2: + case Variant::RECT2: + case Variant::VECTOR3: + case Variant::TRANSFORM2D: + case Variant::PLANE: + case Variant::QUAT: + case Variant::AABB: + case Variant::BASIS: + case Variant::TRANSFORM: + case Variant::PACKED_BYTE_ARRAY: + case Variant::PACKED_INT32_ARRAY: + case Variant::PACKED_INT64_ARRAY: + case Variant::PACKED_FLOAT32_ARRAY: + case Variant::PACKED_FLOAT64_ARRAY: + case Variant::PACKED_STRING_ARRAY: + case Variant::PACKED_VECTOR2_ARRAY: + case Variant::PACKED_VECTOR3_ARRAY: + case Variant::PACKED_COLOR_ARRAY: + case Variant::STRING_NAME: + case Variant::NODE_PATH: { + uint32_t pos = tmpdata.size(); + int len; + encode_variant(p_data, nullptr, len, false); + tmpdata.resize(tmpdata.size() + len); + encode_variant(p_data, &tmpdata.write[pos], len, false); + return pos; + + } break; + // misc types + case Variant::RID: + case Variant::OBJECT: { + return _pack(Variant(), tmpdata, string_cache); + } break; + case Variant::DICTIONARY: { + Dictionary d = p_data; + //size is known, use sort + uint32_t pos = tmpdata.size(); + int len = d.size(); + tmpdata.resize(tmpdata.size() + len * 12 + 8); + encode_uint32(TYPE_DICT, &tmpdata.write[pos + 0]); + encode_uint32(len, &tmpdata.write[pos + 4]); + + List<Variant> keys; + d.get_key_list(&keys); + List<DictKey> sortk; + + for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { + DictKey dk; + dk.hash = E->get().hash(); + dk.key = E->get(); + sortk.push_back(dk); + } + + sortk.sort(); + + int idx = 0; + for (List<DictKey>::Element *E = sortk.front(); E; E = E->next()) { + encode_uint32(E->get().hash, &tmpdata.write[pos + 8 + idx * 12 + 0]); + uint32_t ofs = _pack(E->get().key, tmpdata, string_cache); + encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 4]); + ofs = _pack(d[E->get().key], tmpdata, string_cache); + encode_uint32(ofs, &tmpdata.write[pos + 8 + idx * 12 + 8]); + idx++; + } + + return pos; + + } break; + case Variant::ARRAY: { + Array a = p_data; + //size is known, use sort + uint32_t pos = tmpdata.size(); + int len = a.size(); + tmpdata.resize(tmpdata.size() + len * 4 + 8); + encode_uint32(TYPE_ARRAY, &tmpdata.write[pos + 0]); + encode_uint32(len, &tmpdata.write[pos + 4]); + + for (int i = 0; i < len; i++) { + uint32_t ofs = _pack(a[i], tmpdata, string_cache); + encode_uint32(ofs, &tmpdata.write[pos + 8 + i * 4]); + } + + return pos; + + } break; + + default: { + } + } + + return OK; +} + +Error PackedDataContainer::pack(const Variant &p_data) { + Vector<uint8_t> tmpdata; + Map<String, uint32_t> string_cache; + _pack(p_data, tmpdata, string_cache); + datalen = tmpdata.size(); + data.resize(tmpdata.size()); + uint8_t *w = data.ptrw(); + copymem(w, tmpdata.ptr(), tmpdata.size()); + + return OK; +} + +void PackedDataContainer::_set_data(const Vector<uint8_t> &p_data) { + data = p_data; + datalen = data.size(); +} + +Vector<uint8_t> PackedDataContainer::_get_data() const { + return data; +} + +Variant PackedDataContainer::_iter_init(const Array &p_iter) { + return _iter_init_ofs(p_iter, 0); +} + +Variant PackedDataContainer::_iter_next(const Array &p_iter) { + return _iter_next_ofs(p_iter, 0); +} + +Variant PackedDataContainer::_iter_get(const Variant &p_iter) { + return _iter_get_ofs(p_iter, 0); +} + +void PackedDataContainer::_bind_methods() { + ClassDB::bind_method(D_METHOD("_set_data"), &PackedDataContainer::_set_data); + ClassDB::bind_method(D_METHOD("_get_data"), &PackedDataContainer::_get_data); + ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainer::_iter_init); + ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainer::_iter_get); + ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainer::_iter_next); + ClassDB::bind_method(D_METHOD("pack", "value"), &PackedDataContainer::pack); + ClassDB::bind_method(D_METHOD("size"), &PackedDataContainer::size); + + ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "__data__"), "_set_data", "_get_data"); +} + +////////////////// + +Variant PackedDataContainerRef::_iter_init(const Array &p_iter) { + return from->_iter_init_ofs(p_iter, offset); +} + +Variant PackedDataContainerRef::_iter_next(const Array &p_iter) { + return from->_iter_next_ofs(p_iter, offset); +} + +Variant PackedDataContainerRef::_iter_get(const Variant &p_iter) { + return from->_iter_get_ofs(p_iter, offset); +} + +bool PackedDataContainerRef::_is_dictionary() const { + return from->_type_at_ofs(offset) == PackedDataContainer::TYPE_DICT; +} + +void PackedDataContainerRef::_bind_methods() { + ClassDB::bind_method(D_METHOD("size"), &PackedDataContainerRef::size); + ClassDB::bind_method(D_METHOD("_iter_init"), &PackedDataContainerRef::_iter_init); + ClassDB::bind_method(D_METHOD("_iter_get"), &PackedDataContainerRef::_iter_get); + ClassDB::bind_method(D_METHOD("_iter_next"), &PackedDataContainerRef::_iter_next); + ClassDB::bind_method(D_METHOD("_is_dictionary"), &PackedDataContainerRef::_is_dictionary); +} + +Variant PackedDataContainerRef::getvar(const Variant &p_key, bool *r_valid) const { + bool err = false; + Variant ret = from->_key_at_ofs(offset, p_key, err); + if (r_valid) { + *r_valid = !err; + } + return ret; +} + +int PackedDataContainerRef::size() const { + return from->_size(offset); +} diff --git a/core/io/packed_data_container.h b/core/io/packed_data_container.h new file mode 100644 index 0000000000..7791e21bb3 --- /dev/null +++ b/core/io/packed_data_container.h @@ -0,0 +1,105 @@ +/*************************************************************************/ +/* packed_data_container.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef PACKED_DATA_CONTAINER_H +#define PACKED_DATA_CONTAINER_H + +#include "core/io/resource.h" + +class PackedDataContainer : public Resource { + GDCLASS(PackedDataContainer, Resource); + + enum { + TYPE_DICT = 0xFFFFFFFF, + TYPE_ARRAY = 0xFFFFFFFE, + }; + + struct DictKey { + uint32_t hash; + Variant key; + bool operator<(const DictKey &p_key) const { return hash < p_key.hash; } + }; + + Vector<uint8_t> data; + int datalen = 0; + + uint32_t _pack(const Variant &p_data, Vector<uint8_t> &tmpdata, Map<String, uint32_t> &string_cache); + + Variant _iter_init_ofs(const Array &p_iter, uint32_t p_offset); + Variant _iter_next_ofs(const Array &p_iter, uint32_t p_offset); + Variant _iter_get_ofs(const Variant &p_iter, uint32_t p_offset); + + Variant _iter_init(const Array &p_iter); + Variant _iter_next(const Array &p_iter); + Variant _iter_get(const Variant &p_iter); + + friend class PackedDataContainerRef; + Variant _key_at_ofs(uint32_t p_ofs, const Variant &p_key, bool &err) const; + Variant _get_at_ofs(uint32_t p_ofs, const uint8_t *p_buf, bool &err) const; + uint32_t _type_at_ofs(uint32_t p_ofs) const; + int _size(uint32_t p_ofs) const; + +protected: + void _set_data(const Vector<uint8_t> &p_data); + Vector<uint8_t> _get_data() const; + static void _bind_methods(); + +public: + virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override; + Error pack(const Variant &p_data); + + int size() const; + + PackedDataContainer() {} +}; + +class PackedDataContainerRef : public Reference { + GDCLASS(PackedDataContainerRef, Reference); + + friend class PackedDataContainer; + uint32_t offset = 0; + Ref<PackedDataContainer> from; + +protected: + static void _bind_methods(); + +public: + Variant _iter_init(const Array &p_iter); + Variant _iter_next(const Array &p_iter); + Variant _iter_get(const Variant &p_iter); + bool _is_dictionary() const; + + int size() const; + virtual Variant getvar(const Variant &p_key, bool *r_valid = nullptr) const override; + + PackedDataContainerRef() {} +}; + +#endif // PACKED_DATA_CONTAINER_H diff --git a/core/io/packet_peer.cpp b/core/io/packet_peer.cpp index 38abb5c0d6..318fd10243 100644 --- a/core/io/packet_peer.cpp +++ b/core/io/packet_peer.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,18 +30,12 @@ #include "packet_peer.h" +#include "core/config/project_settings.h" #include "core/io/marshalls.h" -#include "core/project_settings.h" /* helpers / binders */ -PacketPeer::PacketPeer() : - last_get_error(OK), - encode_buffer_max_size(8 * 1024 * 1024) { -} - void PacketPeer::set_encode_buffer_max_size(int p_max_size) { - ERR_FAIL_COND_MSG(p_max_size < 1024, "Max encode buffer must be at least 1024 bytes"); ERR_FAIL_COND_MSG(p_max_size > 256 * 1024 * 1024, "Max encode buffer cannot exceed 256 MiB"); encode_buffer_max_size = next_power_of_2(p_max_size); @@ -49,59 +43,61 @@ void PacketPeer::set_encode_buffer_max_size(int p_max_size) { } int PacketPeer::get_encode_buffer_max_size() const { - return encode_buffer_max_size; } Error PacketPeer::get_packet_buffer(Vector<uint8_t> &r_buffer) { - const uint8_t *buffer; int buffer_size; Error err = get_packet(&buffer, buffer_size); - if (err) + if (err) { return err; + } r_buffer.resize(buffer_size); - if (buffer_size == 0) + if (buffer_size == 0) { return OK; + } uint8_t *w = r_buffer.ptrw(); - for (int i = 0; i < buffer_size; i++) + for (int i = 0; i < buffer_size; i++) { w[i] = buffer[i]; + } return OK; } Error PacketPeer::put_packet_buffer(const Vector<uint8_t> &p_buffer) { - int len = p_buffer.size(); - if (len == 0) + if (len == 0) { return OK; + } const uint8_t *r = p_buffer.ptr(); return put_packet(&r[0], len); } Error PacketPeer::get_var(Variant &r_variant, bool p_allow_objects) { - const uint8_t *buffer; int buffer_size; Error err = get_packet(&buffer, buffer_size); - if (err) + if (err) { return err; + } return decode_variant(r_variant, buffer, buffer_size, nullptr, p_allow_objects); } Error PacketPeer::put_var(const Variant &p_packet, bool p_full_objects) { - int len; Error err = encode_variant(p_packet, nullptr, len, p_full_objects); // compute len first - if (err) + if (err) { return err; + } - if (len == 0) + if (len == 0) { return OK; + } ERR_FAIL_COND_V_MSG(len > encode_buffer_max_size, ERR_OUT_OF_MEMORY, "Failed to encode variant, encode size is bigger then encode_buffer_max_size. Consider raising it via 'set_encode_buffer_max_size'."); @@ -128,20 +124,18 @@ Variant PacketPeer::_bnd_get_var(bool p_allow_objects) { Error PacketPeer::_put_packet(const Vector<uint8_t> &p_buffer) { return put_packet_buffer(p_buffer); } -Vector<uint8_t> PacketPeer::_get_packet() { +Vector<uint8_t> PacketPeer::_get_packet() { Vector<uint8_t> raw; last_get_error = get_packet_buffer(raw); return raw; } Error PacketPeer::_get_packet_error() const { - return last_get_error; } void PacketPeer::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_var", "allow_objects"), &PacketPeer::_bnd_get_var, DEFVAL(false)); ClassDB::bind_method(D_METHOD("put_var", "var", "full_objects"), &PacketPeer::put_var, DEFVAL(false)); ClassDB::bind_method(D_METHOD("get_packet"), &PacketPeer::_get_packet); @@ -153,18 +147,11 @@ void PacketPeer::_bind_methods() { ClassDB::bind_method(D_METHOD("set_encode_buffer_max_size", "max_size"), &PacketPeer::set_encode_buffer_max_size); ADD_PROPERTY(PropertyInfo(Variant::INT, "encode_buffer_max_size"), "set_encode_buffer_max_size", "get_encode_buffer_max_size"); -}; +} /***************/ -void PacketPeerStream::_set_stream_peer(REF p_peer) { - - ERR_FAIL_COND_MSG(p_peer.is_null(), "It's not a reference to a valid Resource object."); - set_stream_peer(p_peer); -} - void PacketPeerStream::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_stream_peer", "peer"), &PacketPeerStream::set_stream_peer); ClassDB::bind_method(D_METHOD("get_stream_peer"), &PacketPeerStream::get_stream_peer); ClassDB::bind_method(D_METHOD("set_input_buffer_max_size", "max_size_bytes"), &PacketPeerStream::set_input_buffer_max_size); @@ -178,16 +165,17 @@ void PacketPeerStream::_bind_methods() { } Error PacketPeerStream::_poll_buffer() const { - ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED); int read = 0; ERR_FAIL_COND_V(input_buffer.size() < ring_buffer.space_left(), ERR_UNAVAILABLE); Error err = peer->get_partial_data(input_buffer.ptrw(), ring_buffer.space_left(), read); - if (err) + if (err) { return err; - if (read == 0) + } + if (read == 0) { return OK; + } int w = ring_buffer.write(&input_buffer[0], read); ERR_FAIL_COND_V(w != read, ERR_BUG); @@ -196,7 +184,6 @@ Error PacketPeerStream::_poll_buffer() const { } int PacketPeerStream::get_available_packet_count() const { - _poll_buffer(); uint32_t remaining = ring_buffer.data_left(); @@ -205,14 +192,14 @@ int PacketPeerStream::get_available_packet_count() const { int count = 0; while (remaining >= 4) { - uint8_t lbuf[4]; ring_buffer.copy(lbuf, ofs, 4); uint32_t len = decode_uint32(lbuf); remaining -= 4; ofs += 4; - if (len > remaining) + if (len > remaining) { break; + } remaining -= len; ofs += len; count++; @@ -222,7 +209,6 @@ int PacketPeerStream::get_available_packet_count() const { } Error PacketPeerStream::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED); _poll_buffer(); @@ -244,50 +230,46 @@ Error PacketPeerStream::get_packet(const uint8_t **r_buffer, int &r_buffer_size) } Error PacketPeerStream::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - ERR_FAIL_COND_V(peer.is_null(), ERR_UNCONFIGURED); Error err = _poll_buffer(); //won't hurt to poll here too - if (err) + if (err) { return err; + } - if (p_buffer_size == 0) + if (p_buffer_size == 0) { return OK; + } ERR_FAIL_COND_V(p_buffer_size < 0, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(p_buffer_size + 4 > output_buffer.size(), ERR_INVALID_PARAMETER); encode_uint32(p_buffer_size, output_buffer.ptrw()); uint8_t *dst = &output_buffer.write[4]; - for (int i = 0; i < p_buffer_size; i++) + for (int i = 0; i < p_buffer_size; i++) { dst[i] = p_buffer[i]; + } return peer->put_data(&output_buffer[0], p_buffer_size + 4); } int PacketPeerStream::get_max_packet_size() const { - return output_buffer.size(); } void PacketPeerStream::set_stream_peer(const Ref<StreamPeer> &p_peer) { - - //ERR_FAIL_COND(p_peer.is_null()); - if (p_peer.ptr() != peer.ptr()) { - ring_buffer.advance_read(ring_buffer.data_left()); // reset the ring buffer - }; + ring_buffer.advance_read(ring_buffer.data_left()); // Reset the ring buffer. + } peer = p_peer; } Ref<StreamPeer> PacketPeerStream::get_stream_peer() const { - return peer; } void PacketPeerStream::set_input_buffer_max_size(int p_max_size) { - ERR_FAIL_COND_MSG(p_max_size < 0, "Max size of input buffer size cannot be smaller than 0."); //warning may lose packets ERR_FAIL_COND_MSG(ring_buffer.data_left(), "Buffer in use, resizing would cause loss of data."); @@ -296,22 +278,18 @@ void PacketPeerStream::set_input_buffer_max_size(int p_max_size) { } int PacketPeerStream::get_input_buffer_max_size() const { - return input_buffer.size() - 4; } void PacketPeerStream::set_output_buffer_max_size(int p_max_size) { - output_buffer.resize(next_power_of_2(p_max_size + 4)); } int PacketPeerStream::get_output_buffer_max_size() const { - return output_buffer.size() - 4; } PacketPeerStream::PacketPeerStream() { - int rbsize = GLOBAL_GET("network/limits/packet_peer_stream/max_buffer_po2"); ring_buffer.resize(rbsize); diff --git a/core/io/packet_peer.h b/core/io/packet_peer.h index 62144259cc..9e03c44750 100644 --- a/core/io/packet_peer.h +++ b/core/io/packet_peer.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -32,11 +32,10 @@ #define PACKET_PEER_H #include "core/io/stream_peer.h" -#include "core/object.h" -#include "core/ring_buffer.h" +#include "core/object/class_db.h" +#include "core/templates/ring_buffer.h" class PacketPeer : public Reference { - GDCLASS(PacketPeer, Reference); Variant _bnd_get_var(bool p_allow_objects = false); @@ -47,9 +46,9 @@ class PacketPeer : public Reference { Vector<uint8_t> _get_packet(); Error _get_packet_error() const; - mutable Error last_get_error; + mutable Error last_get_error = OK; - int encode_buffer_max_size; + int encode_buffer_max_size = 8 * 1024 * 1024; Vector<uint8_t> encode_buffer; public: @@ -70,12 +69,11 @@ public: void set_encode_buffer_max_size(int p_max_size); int get_encode_buffer_max_size() const; - PacketPeer(); + PacketPeer() {} ~PacketPeer() {} }; class PacketPeerStream : public PacketPeer { - GDCLASS(PacketPeerStream, PacketPeer); //the way the buffers work sucks, will change later @@ -88,15 +86,14 @@ class PacketPeerStream : public PacketPeer { Error _poll_buffer() const; protected: - void _set_stream_peer(REF p_peer); static void _bind_methods(); public: - virtual int get_available_packet_count() const; - virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); - virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size); + virtual int get_available_packet_count() const override; + virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; + virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; - virtual int get_max_packet_size() const; + virtual int get_max_packet_size() const override; void set_stream_peer(const Ref<StreamPeer> &p_peer); Ref<StreamPeer> get_stream_peer() const; diff --git a/core/io/packet_peer_dtls.cpp b/core/io/packet_peer_dtls.cpp index 6da115eed2..bac98e20e7 100644 --- a/core/io/packet_peer_dtls.cpp +++ b/core/io/packet_peer_dtls.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -29,15 +29,17 @@ /*************************************************************************/ #include "packet_peer_dtls.h" +#include "core/config/project_settings.h" #include "core/os/file_access.h" -#include "core/project_settings.h" PacketPeerDTLS *(*PacketPeerDTLS::_create)() = nullptr; bool PacketPeerDTLS::available = false; PacketPeerDTLS *PacketPeerDTLS::create() { - - return _create(); + if (_create) { + return _create(); + } + return nullptr; } bool PacketPeerDTLS::is_available() { @@ -45,7 +47,6 @@ bool PacketPeerDTLS::is_available() { } void PacketPeerDTLS::_bind_methods() { - ClassDB::bind_method(D_METHOD("poll"), &PacketPeerDTLS::poll); ClassDB::bind_method(D_METHOD("connect_to_peer", "packet_peer", "validate_certs", "for_hostname", "valid_certificate"), &PacketPeerDTLS::connect_to_peer, DEFVAL(true), DEFVAL(String()), DEFVAL(Ref<X509Certificate>())); ClassDB::bind_method(D_METHOD("get_status"), &PacketPeerDTLS::get_status); @@ -57,6 +58,3 @@ void PacketPeerDTLS::_bind_methods() { BIND_ENUM_CONSTANT(STATUS_ERROR); BIND_ENUM_CONSTANT(STATUS_ERROR_HOSTNAME_MISMATCH); } - -PacketPeerDTLS::PacketPeerDTLS() { -} diff --git a/core/io/packet_peer_dtls.h b/core/io/packet_peer_dtls.h index 4f9f4535bc..31c52f539f 100644 --- a/core/io/packet_peer_dtls.h +++ b/core/io/packet_peer_dtls.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -60,7 +60,7 @@ public: static PacketPeerDTLS *create(); static bool is_available(); - PacketPeerDTLS(); + PacketPeerDTLS() {} }; VARIANT_ENUM_CAST(PacketPeerDTLS::Status); diff --git a/core/io/packet_peer_udp.cpp b/core/io/packet_peer_udp.cpp index f800ffc3db..d8d63d976f 100644 --- a/core/io/packet_peer_udp.cpp +++ b/core/io/packet_peer_udp.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,20 +31,22 @@ #include "packet_peer_udp.h" #include "core/io/ip.h" +#include "core/io/udp_server.h" void PacketPeerUDP::set_blocking_mode(bool p_enable) { - blocking = p_enable; } void PacketPeerUDP::set_broadcast_enabled(bool p_enabled) { + ERR_FAIL_COND(udp_server); broadcast = p_enabled; - if (_sock.is_valid() && _sock->is_open()) + if (_sock.is_valid() && _sock->is_open()) { _sock->set_broadcasting_enabled(p_enabled); + } } Error PacketPeerUDP::join_multicast_group(IP_Address p_multi_address, String p_if_name) { - + ERR_FAIL_COND_V(udp_server, ERR_LOCKED); ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(!p_multi_address.is_valid(), ERR_INVALID_PARAMETER); @@ -59,26 +61,25 @@ Error PacketPeerUDP::join_multicast_group(IP_Address p_multi_address, String p_i } Error PacketPeerUDP::leave_multicast_group(IP_Address p_multi_address, String p_if_name) { - + ERR_FAIL_COND_V(udp_server, ERR_LOCKED); ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(!_sock->is_open(), ERR_UNCONFIGURED); return _sock->leave_multicast_group(p_multi_address, p_if_name); } String PacketPeerUDP::_get_packet_ip() const { - return get_packet_address(); } Error PacketPeerUDP::_set_dest_address(const String &p_address, int p_port) { - IP_Address ip; if (p_address.is_valid_ip_address()) { ip = p_address; } else { ip = IP::get_singleton()->resolve_hostname(p_address); - if (!ip.is_valid()) + if (!ip.is_valid()) { return ERR_CANT_RESOLVE; + } } set_dest_address(ip, p_port); @@ -86,22 +87,23 @@ Error PacketPeerUDP::_set_dest_address(const String &p_address, int p_port) { } int PacketPeerUDP::get_available_packet_count() const { - // TODO we should deprecate this, and expose poll instead! Error err = const_cast<PacketPeerUDP *>(this)->_poll(); - if (err != OK) + if (err != OK) { return -1; + } return queue_count; } Error PacketPeerUDP::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { - Error err = _poll(); - if (err != OK) + if (err != OK) { return err; - if (queue_count == 0) + } + if (queue_count == 0) { return ERR_UNAVAILABLE; + } uint32_t size = 0; uint8_t ipv6[16]; @@ -117,7 +119,6 @@ Error PacketPeerUDP::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { } Error PacketPeerUDP::put_packet(const uint8_t *p_buffer, int p_buffer_size) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED); @@ -133,16 +134,17 @@ Error PacketPeerUDP::put_packet(const uint8_t *p_buffer, int p_buffer_size) { } do { - if (connected) { + if (connected && !udp_server) { err = _sock->send(p_buffer, p_buffer_size, sent); } else { err = _sock->sendto(p_buffer, p_buffer_size, sent, peer_addr, peer_port); } if (err != OK) { - if (err != ERR_BUSY) + if (err != ERR_BUSY) { return FAILED; - else if (!blocking) + } else if (!blocking) { return ERR_BUSY; + } // Keep trying to send full packet continue; } @@ -154,12 +156,10 @@ Error PacketPeerUDP::put_packet(const uint8_t *p_buffer, int p_buffer_size) { } int PacketPeerUDP::get_max_packet_size() const { - return 512; // uhm maybe not } Error PacketPeerUDP::listen(int p_port, const IP_Address &p_bind_address, int p_recv_buffer_size) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); @@ -167,16 +167,17 @@ Error PacketPeerUDP::listen(int p_port, const IP_Address &p_bind_address, int p_ Error err; IP::Type ip_type = IP::TYPE_ANY; - if (p_bind_address.is_valid()) + if (p_bind_address.is_valid()) { ip_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; + } err = _sock->open(NetSocket::TYPE_UDP, ip_type); - if (err != OK) + if (err != OK) { return ERR_CANT_CREATE; + } _sock->set_blocking_enabled(false); - _sock->set_reuse_address_enabled(true); _sock->set_broadcasting_enabled(broadcast); err = _sock->bind(p_bind_address, p_port); @@ -188,26 +189,25 @@ Error PacketPeerUDP::listen(int p_port, const IP_Address &p_bind_address, int p_ return OK; } -Error PacketPeerUDP::connect_socket(Ref<NetSocket> p_sock) { - Error err; - int read = 0; - uint16_t r_port; - IP_Address r_ip; - - err = p_sock->recvfrom(recv_buffer, sizeof(recv_buffer), read, r_ip, r_port, true); - ERR_FAIL_COND_V(err != OK, err); - err = p_sock->connect_to_host(r_ip, r_port); - ERR_FAIL_COND_V(err != OK, err); +Error PacketPeerUDP::connect_shared_socket(Ref<NetSocket> p_sock, IP_Address p_ip, uint16_t p_port, UDPServer *p_server) { + udp_server = p_server; + connected = true; _sock = p_sock; - peer_addr = r_ip; - peer_port = r_port; + peer_addr = p_ip; + peer_port = p_port; packet_ip = peer_addr; packet_port = peer_port; - connected = true; return OK; } +void PacketPeerUDP::disconnect_shared_socket() { + udp_server = nullptr; + _sock = Ref<NetSocket>(NetSocket::create()); + close(); +} + Error PacketPeerUDP::connect_to_host(const IP_Address &p_host, int p_port) { + ERR_FAIL_COND_V(udp_server, ERR_LOCKED); ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER); @@ -245,27 +245,32 @@ bool PacketPeerUDP::is_connected_to_host() const { } void PacketPeerUDP::close() { - - if (_sock.is_valid()) + if (udp_server) { + udp_server->remove_peer(peer_addr, peer_port); + udp_server = nullptr; + _sock = Ref<NetSocket>(NetSocket::create()); + } else if (_sock.is_valid()) { _sock->close(); + } rb.resize(16); queue_count = 0; connected = false; } Error PacketPeerUDP::wait() { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); return _sock->poll(NetSocket::POLL_TYPE_IN, -1); } Error PacketPeerUDP::_poll() { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); if (!_sock->is_open()) { return FAILED; } + if (udp_server) { + return OK; // Handled by UDPServer. + } Error err; int read; @@ -282,52 +287,54 @@ Error PacketPeerUDP::_poll() { } if (err != OK) { - if (err == ERR_BUSY) + if (err == ERR_BUSY) { break; + } return FAILED; } - if (rb.space_left() < read + 24) { + err = store_packet(ip, port, recv_buffer, read); #ifdef TOOLS_ENABLED + if (err != OK) { WARN_PRINT("Buffer full, dropping packets!"); -#endif - continue; } - - uint32_t port32 = port; - rb.write(ip.get_ipv6(), 16); - rb.write((uint8_t *)&port32, 4); - rb.write((uint8_t *)&read, 4); - rb.write(recv_buffer, read); - ++queue_count; +#endif } return OK; } -bool PacketPeerUDP::is_listening() const { +Error PacketPeerUDP::store_packet(IP_Address p_ip, uint32_t p_port, uint8_t *p_buf, int p_buf_size) { + if (rb.space_left() < p_buf_size + 24) { + return ERR_OUT_OF_MEMORY; + } + rb.write(p_ip.get_ipv6(), 16); + rb.write((uint8_t *)&p_port, 4); + rb.write((uint8_t *)&p_buf_size, 4); + rb.write(p_buf, p_buf_size); + ++queue_count; + return OK; +} + +bool PacketPeerUDP::is_listening() const { return _sock.is_valid() && _sock->is_open(); } IP_Address PacketPeerUDP::get_packet_address() const { - return packet_ip; } int PacketPeerUDP::get_packet_port() const { - return packet_port; } void PacketPeerUDP::set_dest_address(const IP_Address &p_address, int p_port) { - ERR_FAIL_COND_MSG(connected, "Destination address cannot be set for connected sockets"); peer_addr = p_address; peer_port = p_port; } void PacketPeerUDP::_bind_methods() { - ClassDB::bind_method(D_METHOD("listen", "port", "bind_address", "recv_buf_size"), &PacketPeerUDP::listen, DEFVAL("*"), DEFVAL(65536)); ClassDB::bind_method(D_METHOD("close"), &PacketPeerUDP::close); ClassDB::bind_method(D_METHOD("wait"), &PacketPeerUDP::wait); @@ -343,17 +350,10 @@ void PacketPeerUDP::_bind_methods() { } PacketPeerUDP::PacketPeerUDP() : - packet_port(0), - queue_count(0), - peer_port(0), - connected(false), - blocking(true), - broadcast(false), _sock(Ref<NetSocket>(NetSocket::create())) { rb.resize(16); } PacketPeerUDP::~PacketPeerUDP() { - close(); } diff --git a/core/io/packet_peer_udp.h b/core/io/packet_peer_udp.h index b5a9fc9ec3..4bac6994fc 100644 --- a/core/io/packet_peer_udp.h +++ b/core/io/packet_peer_udp.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -35,6 +35,8 @@ #include "core/io/net_socket.h" #include "core/io/packet_peer.h" +class UDPServer; + class PacketPeerUDP : public PacketPeer { GDCLASS(PacketPeerUDP, PacketPeer); @@ -47,14 +49,15 @@ protected: uint8_t recv_buffer[PACKET_BUFFER_SIZE]; uint8_t packet_buffer[PACKET_BUFFER_SIZE]; IP_Address packet_ip; - int packet_port; - int queue_count; + int packet_port = 0; + int queue_count = 0; IP_Address peer_addr; - int peer_port; - bool connected; - bool blocking; - bool broadcast; + int peer_port = 0; + bool connected = false; + bool blocking = true; + bool broadcast = false; + UDPServer *udp_server = nullptr; Ref<NetSocket> _sock; static void _bind_methods(); @@ -72,7 +75,9 @@ public: Error wait(); bool is_listening() const; - Error connect_socket(Ref<NetSocket> p_sock); // Used by UDPServer + Error connect_shared_socket(Ref<NetSocket> p_sock, IP_Address p_ip, uint16_t p_port, UDPServer *ref); // Used by UDPServer + void disconnect_shared_socket(); // Used by UDPServer + Error store_packet(IP_Address p_ip, uint32_t p_port, uint8_t *p_buf, int p_buf_size); // Used internally and by UDPServer Error connect_to_host(const IP_Address &p_host, int p_port); bool is_connected_to_host() const; @@ -80,10 +85,10 @@ public: int get_packet_port() const; void set_dest_address(const IP_Address &p_address, int p_port); - Error put_packet(const uint8_t *p_buffer, int p_buffer_size); - Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); - int get_available_packet_count() const; - int get_max_packet_size() const; + Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; + Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; + int get_available_packet_count() const override; + int get_max_packet_size() const override; void set_broadcast_enabled(bool p_enabled); Error join_multicast_group(IP_Address p_multi_address, String p_if_name); Error leave_multicast_group(IP_Address p_multi_address, String p_if_name); diff --git a/core/io/pck_packer.cpp b/core/io/pck_packer.cpp index 5c4b3379ee..a0697ca18b 100644 --- a/core/io/pck_packer.cpp +++ b/core/io/pck_packer.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,38 +30,57 @@ #include "pck_packer.h" +#include "core/crypto/crypto_core.h" +#include "core/io/file_access_encrypted.h" #include "core/io/file_access_pack.h" // PACK_HEADER_MAGIC, PACK_FORMAT_VERSION #include "core/os/file_access.h" #include "core/version.h" -static uint64_t _align(uint64_t p_n, int p_alignment) { - - if (p_alignment == 0) - return p_n; - - uint64_t rest = p_n % p_alignment; - if (rest == 0) - return p_n; - else - return p_n + (p_alignment - rest); -}; - -static void _pad(FileAccess *p_file, int p_bytes) { - - for (int i = 0; i < p_bytes; i++) { +static int _get_pad(int p_alignment, int p_n) { + int rest = p_n % p_alignment; + int pad = 0; + if (rest > 0) { + pad = p_alignment - rest; + } - p_file->store_8(0); - }; -}; + return pad; +} void PCKPacker::_bind_methods() { - - ClassDB::bind_method(D_METHOD("pck_start", "pck_name", "alignment"), &PCKPacker::pck_start, DEFVAL(0)); - ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path"), &PCKPacker::add_file); + ClassDB::bind_method(D_METHOD("pck_start", "pck_name", "alignment", "key", "encrypt_directory"), &PCKPacker::pck_start, DEFVAL(0), DEFVAL(String()), DEFVAL(false)); + ClassDB::bind_method(D_METHOD("add_file", "pck_path", "source_path", "encrypt"), &PCKPacker::add_file, DEFVAL(false)); ClassDB::bind_method(D_METHOD("flush", "verbose"), &PCKPacker::flush, DEFVAL(false)); -}; - -Error PCKPacker::pck_start(const String &p_file, int p_alignment) { +} + +Error PCKPacker::pck_start(const String &p_file, int p_alignment, const String &p_key, bool p_encrypt_directory) { + ERR_FAIL_COND_V_MSG((p_key.is_empty() || !p_key.is_valid_hex_number(false) || p_key.length() != 64), ERR_CANT_CREATE, "Invalid Encryption Key (must be 64 characters long)."); + + String _key = p_key.to_lower(); + key.resize(32); + for (int i = 0; i < 32; i++) { + int v = 0; + if (i * 2 < _key.length()) { + char32_t ct = _key[i * 2]; + if (ct >= '0' && ct <= '9') { + ct = ct - '0'; + } else if (ct >= 'a' && ct <= 'f') { + ct = 10 + ct - 'a'; + } + v |= ct << 4; + } + + if (i * 2 + 1 < _key.length()) { + char32_t ct = _key[i * 2 + 1]; + if (ct >= '0' && ct <= '9') { + ct = ct - '0'; + } else if (ct >= 'a' && ct <= 'f') { + ct = 10 + ct - 'a'; + } + v |= ct; + } + key.write[i] = v; + } + enc_dir = p_encrypt_directory; if (file != nullptr) { memdelete(file); @@ -79,28 +98,53 @@ Error PCKPacker::pck_start(const String &p_file, int p_alignment) { file->store_32(VERSION_MINOR); file->store_32(VERSION_PATCH); - for (int i = 0; i < 16; i++) { - - file->store_32(0); // reserved - }; + uint32_t pack_flags = 0; + if (enc_dir) { + pack_flags |= PACK_DIR_ENCRYPTED; + } + file->store_32(pack_flags); // flags files.clear(); + ofs = 0; return OK; -}; - -Error PCKPacker::add_file(const String &p_file, const String &p_src) { +} +Error PCKPacker::add_file(const String &p_file, const String &p_src, bool p_encrypt) { FileAccess *f = FileAccess::open(p_src, FileAccess::READ); if (!f) { return ERR_FILE_CANT_OPEN; - }; + } File pf; pf.path = p_file; pf.src_path = p_src; + pf.ofs = ofs; pf.size = f->get_len(); - pf.offset_offset = 0; + + Vector<uint8_t> data = FileAccess::get_file_as_array(p_src); + { + unsigned char hash[16]; + CryptoCore::md5(data.ptr(), data.size(), hash); + pf.md5.resize(16); + for (int i = 0; i < 16; i++) { + pf.md5.write[i] = hash[i]; + } + } + pf.encrypted = p_encrypt; + + uint64_t _size = pf.size; + if (p_encrypt) { // Add encryption overhead. + if (_size % 16) { // Pad to encryption block size. + _size += 16 - (_size % 16); + } + _size += 16; // hash + _size += 8; // data size + _size += 16; // iv + } + + int pad = _get_pad(alignment, ofs + _size); + ofs = ofs + _size + pad; files.push_back(pf); @@ -108,86 +152,130 @@ Error PCKPacker::add_file(const String &p_file, const String &p_src) { memdelete(f); return OK; -}; +} Error PCKPacker::flush(bool p_verbose) { - ERR_FAIL_COND_V_MSG(!file, ERR_INVALID_PARAMETER, "File must be opened before use."); - // write the index + int64_t file_base_ofs = file->get_position(); + file->store_64(0); // files base + for (int i = 0; i < 16; i++) { + file->store_32(0); // reserved + } + + // write the index file->store_32(files.size()); - for (int i = 0; i < files.size(); i++) { + FileAccessEncrypted *fae = nullptr; + FileAccess *fhead = file; - file->store_pascal_string(files[i].path); - files.write[i].offset_offset = file->get_position(); - file->store_64(0); // offset - file->store_64(files[i].size); // size + if (enc_dir) { + fae = memnew(FileAccessEncrypted); + ERR_FAIL_COND_V(!fae, ERR_CANT_CREATE); - // # empty md5 - file->store_32(0); - file->store_32(0); - file->store_32(0); - file->store_32(0); - }; + Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false); + ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); - uint64_t ofs = file->get_position(); - ofs = _align(ofs, alignment); + fhead = fae; + } + + for (int i = 0; i < files.size(); i++) { + int string_len = files[i].path.utf8().length(); + int pad = _get_pad(4, string_len); + + fhead->store_32(string_len + pad); + fhead->store_buffer((const uint8_t *)files[i].path.utf8().get_data(), string_len); + for (int j = 0; j < pad; j++) { + fhead->store_8(0); + } + + fhead->store_64(files[i].ofs); + fhead->store_64(files[i].size); // pay attention here, this is where file is + fhead->store_buffer(files[i].md5.ptr(), 16); //also save md5 for file + + uint32_t flags = 0; + if (files[i].encrypted) { + flags |= PACK_FILE_ENCRYPTED; + } + fhead->store_32(flags); + } + + if (fae) { + fae->release(); + memdelete(fae); + } + + int header_padding = _get_pad(alignment, file->get_position()); + for (int i = 0; i < header_padding; i++) { + file->store_8(Math::rand() % 256); + } - _pad(file, ofs - file->get_position()); + int64_t file_base = file->get_position(); + file->seek(file_base_ofs); + file->store_64(file_base); // update files base + file->seek(file_base); const uint32_t buf_max = 65536; uint8_t *buf = memnew_arr(uint8_t, buf_max); int count = 0; for (int i = 0; i < files.size(); i++) { - FileAccess *src = FileAccess::open(files[i].src_path, FileAccess::READ); uint64_t to_write = files[i].size; - while (to_write > 0) { + fae = nullptr; + FileAccess *ftmp = file; + if (files[i].encrypted) { + fae = memnew(FileAccessEncrypted); + ERR_FAIL_COND_V(!fae, ERR_CANT_CREATE); + + Error err = fae->open_and_parse(file, key, FileAccessEncrypted::MODE_WRITE_AES256, false); + ERR_FAIL_COND_V(err != OK, ERR_CANT_CREATE); + ftmp = fae; + } + + while (to_write > 0) { int read = src->get_buffer(buf, MIN(to_write, buf_max)); - file->store_buffer(buf, read); + ftmp->store_buffer(buf, read); to_write -= read; - }; + } - uint64_t pos = file->get_position(); - file->seek(files[i].offset_offset); // go back to store the file's offset - file->store_64(ofs); - file->seek(pos); + if (fae) { + fae->release(); + memdelete(fae); + } - ofs = _align(ofs + files[i].size, alignment); - _pad(file, ofs - pos); + int pad = _get_pad(alignment, file->get_position()); + for (int j = 0; j < pad; j++) { + file->store_8(Math::rand() % 256); + } src->close(); memdelete(src); count += 1; - if (p_verbose && files.size() > 0) { + const int file_num = files.size(); + if (p_verbose && (file_num > 0)) { if (count % 100 == 0) { - printf("%i/%i (%.2f)\r", count, files.size(), float(count) / files.size() * 100); + printf("%i/%i (%.2f)\r", count, file_num, float(count) / file_num * 100); fflush(stdout); - }; - }; - }; + } + } + } - if (p_verbose) + if (p_verbose) { printf("\n"); + } file->close(); memdelete_arr(buf); return OK; -}; - -PCKPacker::PCKPacker() { - - file = nullptr; -}; +} PCKPacker::~PCKPacker() { if (file != nullptr) { memdelete(file); - }; + } file = nullptr; -}; +} diff --git a/core/io/pck_packer.h b/core/io/pck_packer.h index 6058de8345..dec8f8748d 100644 --- a/core/io/pck_packer.h +++ b/core/io/pck_packer.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,34 +31,38 @@ #ifndef PCK_PACKER_H #define PCK_PACKER_H -#include "core/reference.h" +#include "core/object/reference.h" class FileAccess; class PCKPacker : public Reference { - GDCLASS(PCKPacker, Reference); - FileAccess *file; - int alignment; + FileAccess *file = nullptr; + int alignment = 0; + uint64_t ofs = 0; + + Vector<uint8_t> key; + bool enc_dir = false; static void _bind_methods(); struct File { - String path; String src_path; - int size; - uint64_t offset_offset; + uint64_t ofs = 0; + uint64_t size = 0; + bool encrypted = false; + Vector<uint8_t> md5; }; Vector<File> files; public: - Error pck_start(const String &p_file, int p_alignment = 0); - Error add_file(const String &p_file, const String &p_src); + Error pck_start(const String &p_file, int p_alignment = 0, const String &p_key = String(), bool p_encrypt_directory = false); + Error add_file(const String &p_file, const String &p_src, bool p_encrypt = false); Error flush(bool p_verbose = false); - PCKPacker(); + PCKPacker() {} ~PCKPacker(); }; diff --git a/core/io/resource.cpp b/core/io/resource.cpp new file mode 100644 index 0000000000..2c97e617f2 --- /dev/null +++ b/core/io/resource.cpp @@ -0,0 +1,535 @@ +/*************************************************************************/ +/* resource.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "resource.h" + +#include "core/core_string_names.h" +#include "core/io/resource_loader.h" +#include "core/object/script_language.h" +#include "core/os/file_access.h" +#include "core/os/os.h" +#include "scene/main/node.h" //only so casting works + +#include <stdio.h> + +void Resource::emit_changed() { + emit_signal(CoreStringNames::get_singleton()->changed); +} + +void Resource::_resource_path_changed() { +} + +void Resource::set_path(const String &p_path, bool p_take_over) { + if (path_cache == p_path) { + return; + } + + if (path_cache != "") { + ResourceCache::lock->write_lock(); + ResourceCache::resources.erase(path_cache); + ResourceCache::lock->write_unlock(); + } + + path_cache = ""; + + ResourceCache::lock->read_lock(); + bool has_path = ResourceCache::resources.has(p_path); + ResourceCache::lock->read_unlock(); + + if (has_path) { + if (p_take_over) { + ResourceCache::lock->write_lock(); + Resource **res = ResourceCache::resources.getptr(p_path); + if (res) { + (*res)->set_name(""); + } + ResourceCache::lock->write_unlock(); + } else { + ResourceCache::lock->read_lock(); + bool exists = ResourceCache::resources.has(p_path); + ResourceCache::lock->read_unlock(); + + ERR_FAIL_COND_MSG(exists, "Another resource is loaded from path '" + p_path + "' (possible cyclic resource inclusion)."); + } + } + path_cache = p_path; + + if (path_cache != "") { + ResourceCache::lock->write_lock(); + ResourceCache::resources[path_cache] = this; + ResourceCache::lock->write_unlock(); + } + + _change_notify("resource_path"); + _resource_path_changed(); +} + +String Resource::get_path() const { + return path_cache; +} + +void Resource::set_subindex(int p_sub_index) { + subindex = p_sub_index; +} + +int Resource::get_subindex() const { + return subindex; +} + +void Resource::set_name(const String &p_name) { + name = p_name; + _change_notify("resource_name"); +} + +String Resource::get_name() const { + return name; +} + +bool Resource::editor_can_reload_from_file() { + return true; //by default yes +} + +void Resource::reload_from_file() { + String path = get_path(); + if (!path.is_resource_file()) { + return; + } + + Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), true); + + if (!s.is_valid()) { + return; + } + + List<PropertyInfo> pi; + s->get_property_list(&pi); + + for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) { + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { + continue; + } + if (E->get().name == "resource_path") { + continue; //do not change path + } + + set(E->get().name, s->get(E->get().name)); + } +} + +Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache) { + List<PropertyInfo> plist; + get_property_list(&plist); + + Ref<Resource> r = Object::cast_to<Resource>(ClassDB::instance(get_class())); + ERR_FAIL_COND_V(r.is_null(), Ref<Resource>()); + + r->local_scene = p_for_scene; + + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { + continue; + } + Variant p = get(E->get().name); + if (p.get_type() == Variant::OBJECT) { + RES sr = p; + if (sr.is_valid()) { + if (sr->is_local_to_scene()) { + if (remap_cache.has(sr)) { + p = remap_cache[sr]; + } else { + RES dupe = sr->duplicate_for_local_scene(p_for_scene, remap_cache); + p = dupe; + remap_cache[sr] = dupe; + } + } + } + } + + r->set(E->get().name, p); + } + + return r; +} + +void Resource::configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache) { + List<PropertyInfo> plist; + get_property_list(&plist); + + local_scene = p_for_scene; + + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { + continue; + } + Variant p = get(E->get().name); + if (p.get_type() == Variant::OBJECT) { + RES sr = p; + if (sr.is_valid()) { + if (sr->is_local_to_scene()) { + if (!remap_cache.has(sr)) { + sr->configure_for_local_scene(p_for_scene, remap_cache); + remap_cache[sr] = sr; + } + } + } + } + } +} + +Ref<Resource> Resource::duplicate(bool p_subresources) const { + List<PropertyInfo> plist; + get_property_list(&plist); + + Ref<Resource> r = (Resource *)ClassDB::instance(get_class()); + ERR_FAIL_COND_V(r.is_null(), Ref<Resource>()); + + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { + if (!(E->get().usage & PROPERTY_USAGE_STORAGE)) { + continue; + } + Variant p = get(E->get().name); + + if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) { + r->set(E->get().name, p.duplicate(p_subresources)); + } else if (p.get_type() == Variant::OBJECT && (p_subresources || (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) { + RES sr = p; + if (sr.is_valid()) { + r->set(E->get().name, sr->duplicate(p_subresources)); + } + } else { + r->set(E->get().name, p); + } + } + + return r; +} + +void Resource::_set_path(const String &p_path) { + set_path(p_path, false); +} + +void Resource::_take_over_path(const String &p_path) { + set_path(p_path, true); +} + +RID Resource::get_rid() const { + return RID(); +} + +void Resource::register_owner(Object *p_owner) { + owners.insert(p_owner->get_instance_id()); +} + +void Resource::unregister_owner(Object *p_owner) { + owners.erase(p_owner->get_instance_id()); +} + +void Resource::notify_change_to_owners() { + for (Set<ObjectID>::Element *E = owners.front(); E; E = E->next()) { + Object *obj = ObjectDB::get_instance(E->get()); + ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf + //TODO store string + obj->call("resource_changed", RES(this)); + } +} + +#ifdef TOOLS_ENABLED + +uint32_t Resource::hash_edited_version() const { + uint32_t hash = hash_djb2_one_32(get_edited_version()); + + List<PropertyInfo> plist; + get_property_list(&plist); + + for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) { + if (E->get().usage & PROPERTY_USAGE_STORAGE && E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) { + RES res = get(E->get().name); + if (res.is_valid()) { + hash = hash_djb2_one_32(res->hash_edited_version(), hash); + } + } + } + + return hash; +} + +#endif + +void Resource::set_local_to_scene(bool p_enable) { + local_to_scene = p_enable; +} + +bool Resource::is_local_to_scene() const { + return local_to_scene; +} + +Node *Resource::get_local_scene() const { + if (local_scene) { + return local_scene; + } + + if (_get_local_scene_func) { + return _get_local_scene_func(); + } + + return nullptr; +} + +void Resource::setup_local_to_scene() { + if (get_script_instance()) { + get_script_instance()->call("_setup_local_to_scene"); + } +} + +Node *(*Resource::_get_local_scene_func)() = nullptr; + +void Resource::set_as_translation_remapped(bool p_remapped) { + if (remapped_list.in_list() == p_remapped) { + return; + } + + if (ResourceCache::lock) { + ResourceCache::lock->write_lock(); + } + + if (p_remapped) { + ResourceLoader::remapped_list.add(&remapped_list); + } else { + ResourceLoader::remapped_list.remove(&remapped_list); + } + + if (ResourceCache::lock) { + ResourceCache::lock->write_unlock(); + } +} + +bool Resource::is_translation_remapped() const { + return remapped_list.in_list(); +} + +#ifdef TOOLS_ENABLED +//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored +void Resource::set_id_for_path(const String &p_path, int p_id) { + if (p_id == -1) { + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->write_lock(); + } + ResourceCache::resource_path_cache[p_path].erase(get_path()); + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->write_unlock(); + } + } else { + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->write_lock(); + } + ResourceCache::resource_path_cache[p_path][get_path()] = p_id; + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->write_unlock(); + } + } +} + +int Resource::get_id_for_path(const String &p_path) const { + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->read_lock(); + } + if (ResourceCache::resource_path_cache[p_path].has(get_path())) { + int result = ResourceCache::resource_path_cache[p_path][get_path()]; + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->read_unlock(); + } + return result; + } else { + if (ResourceCache::path_cache_lock) { + ResourceCache::path_cache_lock->read_unlock(); + } + return -1; + } +} +#endif + +void Resource::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path); + ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path); + ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path); + ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name); + ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name); + ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid); + ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene); + ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene); + ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene); + ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene); + ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed); + + ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false)); + ADD_SIGNAL(MethodInfo("changed")); + ADD_GROUP("Resource", "resource_"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene"); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path"); + ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "resource_name"), "set_name", "get_name"); + + BIND_VMETHOD(MethodInfo("_setup_local_to_scene")); +} + +Resource::Resource() : + remapped_list(this) {} + +Resource::~Resource() { + if (path_cache != "") { + ResourceCache::lock->write_lock(); + ResourceCache::resources.erase(path_cache); + ResourceCache::lock->write_unlock(); + } + if (owners.size()) { + WARN_PRINT("Resource is still owned."); + } +} + +HashMap<String, Resource *> ResourceCache::resources; +#ifdef TOOLS_ENABLED +HashMap<String, HashMap<String, int>> ResourceCache::resource_path_cache; +#endif + +RWLock *ResourceCache::lock = nullptr; +#ifdef TOOLS_ENABLED +RWLock *ResourceCache::path_cache_lock = nullptr; +#endif + +void ResourceCache::setup() { + lock = RWLock::create(); +#ifdef TOOLS_ENABLED + path_cache_lock = RWLock::create(); +#endif +} + +void ResourceCache::clear() { + if (resources.size()) { + ERR_PRINT("Resources still in use at exit (run with --verbose for details)."); + if (OS::get_singleton()->is_stdout_verbose()) { + const String *K = nullptr; + while ((K = resources.next(K))) { + Resource *r = resources[*K]; + print_line(vformat("Resource still in use: %s (%s)", *K, r->get_class())); + } + } + } + + resources.clear(); + memdelete(lock); +#ifdef TOOLS_ENABLED + memdelete(path_cache_lock); +#endif +} + +void ResourceCache::reload_externals() { +} + +bool ResourceCache::has(const String &p_path) { + lock->read_lock(); + bool b = resources.has(p_path); + lock->read_unlock(); + + return b; +} + +Resource *ResourceCache::get(const String &p_path) { + lock->read_lock(); + + Resource **res = resources.getptr(p_path); + + lock->read_unlock(); + + if (!res) { + return nullptr; + } + + return *res; +} + +void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) { + lock->read_lock(); + const String *K = nullptr; + while ((K = resources.next(K))) { + Resource *r = resources[*K]; + p_resources->push_back(Ref<Resource>(r)); + } + lock->read_unlock(); +} + +int ResourceCache::get_cached_resource_count() { + lock->read_lock(); + int rc = resources.size(); + lock->read_unlock(); + + return rc; +} + +void ResourceCache::dump(const char *p_file, bool p_short) { +#ifdef DEBUG_ENABLED + lock->read_lock(); + + Map<String, int> type_count; + + FileAccess *f = nullptr; + if (p_file) { + f = FileAccess::open(p_file, FileAccess::WRITE); + ERR_FAIL_COND_MSG(!f, "Cannot create file at path '" + String(p_file) + "'."); + } + + const String *K = nullptr; + while ((K = resources.next(K))) { + Resource *r = resources[*K]; + + if (!type_count.has(r->get_class())) { + type_count[r->get_class()] = 0; + } + + type_count[r->get_class()]++; + + if (!p_short) { + if (f) { + f->store_line(r->get_class() + ": " + r->get_path()); + } + } + } + + for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) { + if (f) { + f->store_line(E->key() + " count: " + itos(E->get())); + } + } + if (f) { + f->close(); + memdelete(f); + } + + lock->read_unlock(); +#endif +} diff --git a/core/io/resource.h b/core/io/resource.h new file mode 100644 index 0000000000..eda18a8538 --- /dev/null +++ b/core/io/resource.h @@ -0,0 +1,172 @@ +/*************************************************************************/ +/* resource.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef RESOURCE_H +#define RESOURCE_H + +#include "core/object/class_db.h" +#include "core/object/reference.h" +#include "core/templates/safe_refcount.h" +#include "core/templates/self_list.h" + +#define RES_BASE_EXTENSION(m_ext) \ +public: \ + static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension(m_ext, get_class_static()); } \ + virtual String get_base_extension() const override { return m_ext; } \ + \ +private: + +class Resource : public Reference { + GDCLASS(Resource, Reference); + OBJ_CATEGORY("Resources"); + +public: + static void register_custom_data_to_otdb() { ClassDB::add_resource_base_extension("res", get_class_static()); } + virtual String get_base_extension() const { return "res"; } + +private: + Set<ObjectID> owners; + + friend class ResBase; + friend class ResourceCache; + + String name; + String path_cache; + int subindex = 0; + + virtual bool _use_builtin_script() const { return true; } + +#ifdef TOOLS_ENABLED + uint64_t last_modified_time = 0; + uint64_t import_last_modified_time = 0; + String import_path; +#endif + + bool local_to_scene = false; + friend class SceneState; + Node *local_scene = nullptr; + + SelfList<Resource> remapped_list; + +protected: + void emit_changed(); + + void notify_change_to_owners(); + + virtual void _resource_path_changed(); + static void _bind_methods(); + + void _set_path(const String &p_path); + void _take_over_path(const String &p_path); + +public: + static Node *(*_get_local_scene_func)(); //used by editor + + virtual bool editor_can_reload_from_file(); + virtual void reload_from_file(); + + void register_owner(Object *p_owner); + void unregister_owner(Object *p_owner); + + void set_name(const String &p_name); + String get_name() const; + + virtual void set_path(const String &p_path, bool p_take_over = false); + String get_path() const; + + void set_subindex(int p_sub_index); + int get_subindex() const; + + virtual Ref<Resource> duplicate(bool p_subresources = false) const; + Ref<Resource> duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache); + void configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache); + + void set_local_to_scene(bool p_enable); + bool is_local_to_scene() const; + virtual void setup_local_to_scene(); + + Node *get_local_scene() const; + +#ifdef TOOLS_ENABLED + + uint32_t hash_edited_version() const; + + virtual void set_last_modified_time(uint64_t p_time) { last_modified_time = p_time; } + uint64_t get_last_modified_time() const { return last_modified_time; } + + virtual void set_import_last_modified_time(uint64_t p_time) { import_last_modified_time = p_time; } + uint64_t get_import_last_modified_time() const { return import_last_modified_time; } + + void set_import_path(const String &p_path) { import_path = p_path; } + String get_import_path() const { return import_path; } + +#endif + + void set_as_translation_remapped(bool p_remapped); + bool is_translation_remapped() const; + + virtual RID get_rid() const; // some resources may offer conversion to RID + +#ifdef TOOLS_ENABLED + //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored + void set_id_for_path(const String &p_path, int p_id); + int get_id_for_path(const String &p_path) const; +#endif + + Resource(); + ~Resource(); +}; + +typedef Ref<Resource> RES; + +class ResourceCache { + friend class Resource; + friend class ResourceLoader; //need the lock + static RWLock *lock; + static HashMap<String, Resource *> resources; +#ifdef TOOLS_ENABLED + static HashMap<String, HashMap<String, int>> resource_path_cache; // each tscn has a set of resource paths and IDs + static RWLock *path_cache_lock; +#endif // TOOLS_ENABLED + friend void unregister_core_types(); + static void clear(); + friend void register_core_types(); + static void setup(); + +public: + static void reload_externals(); + static bool has(const String &p_path); + static Resource *get(const String &p_path); + static void dump(const char *p_file = nullptr, bool p_short = false); + static void get_cached_resources(List<Ref<Resource>> *p_resources); + static int get_cached_resource_count(); +}; + +#endif // RESOURCE_H diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 8c7559479b..ae4643a19f 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,18 +30,17 @@ #include "resource_format_binary.h" -#include "core/image.h" +#include "core/config/project_settings.h" #include "core/io/file_access_compressed.h" +#include "core/io/image.h" #include "core/io/marshalls.h" #include "core/os/dir_access.h" -#include "core/project_settings.h" #include "core/version.h" //#define print_bl(m_what) print_line(m_what) #define print_bl(m_what) (void)(m_what) enum { - //numbering must be different from variant, in case new variant types are added (variant must be always contiguous for jumptable optimization) VARIANT_NIL = 1, VARIANT_BOOL = 2, @@ -90,28 +89,27 @@ enum { FORMAT_VERSION = 3, FORMAT_VERSION_CAN_RENAME_DEPS = 1, FORMAT_VERSION_NO_NODEPATH_PROPERTY = 3, - }; void ResourceLoaderBinary::_advance_padding(uint32_t p_len) { - uint32_t extra = 4 - (p_len % 4); if (extra < 4) { - for (uint32_t i = 0; i < extra; i++) + for (uint32_t i = 0; i < extra; i++) { f->get_8(); //pad to 32 + } } } StringName ResourceLoaderBinary::_get_string() { - uint32_t id = f->get_32(); if (id & 0x80000000) { uint32_t len = id & 0x7FFFFFFF; if ((int)len > str_buf.size()) { str_buf.resize(len); } - if (len == 0) + if (len == 0) { return StringName(); + } f->get_buffer((uint8_t *)&str_buf[0], len); String s; s.parse_utf8(&str_buf[0]); @@ -122,42 +120,32 @@ StringName ResourceLoaderBinary::_get_string() { } Error ResourceLoaderBinary::parse_variant(Variant &r_v) { - uint32_t type = f->get_32(); print_bl("find property of type: " + itos(type)); switch (type) { - case VARIANT_NIL: { - r_v = Variant(); } break; case VARIANT_BOOL: { - r_v = bool(f->get_32()); } break; case VARIANT_INT: { - r_v = int(f->get_32()); } break; case VARIANT_INT64: { - r_v = int64_t(f->get_64()); } break; case VARIANT_FLOAT: { - r_v = f->get_real(); } break; case VARIANT_DOUBLE: { - r_v = f->get_double(); } break; case VARIANT_STRING: { - r_v = get_unicode_string(); } break; case VARIANT_VECTOR2: { - Vector2 v; v.x = f->get_real(); v.y = f->get_real(); @@ -165,7 +153,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_VECTOR2I: { - Vector2i v; v.x = f->get_32(); v.y = f->get_32(); @@ -173,7 +160,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_RECT2: { - Rect2 v; v.position.x = f->get_real(); v.position.y = f->get_real(); @@ -183,7 +169,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_RECT2I: { - Rect2i v; v.position.x = f->get_32(); v.position.y = f->get_32(); @@ -193,7 +178,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_VECTOR3: { - Vector3 v; v.x = f->get_real(); v.y = f->get_real(); @@ -201,7 +185,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { r_v = v; } break; case VARIANT_VECTOR3I: { - Vector3i v; v.x = f->get_32(); v.y = f->get_32(); @@ -209,7 +192,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { r_v = v; } break; case VARIANT_PLANE: { - Plane v; v.normal.x = f->get_real(); v.normal.y = f->get_real(); @@ -227,7 +209,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_AABB: { - AABB v; v.position.x = f->get_real(); v.position.y = f->get_real(); @@ -239,7 +220,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_MATRIX32: { - Transform2D v; v.elements[0].x = f->get_real(); v.elements[0].y = f->get_real(); @@ -251,7 +231,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_MATRIX3: { - Basis v; v.elements[0].x = f->get_real(); v.elements[0].y = f->get_real(); @@ -266,7 +245,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_TRANSFORM: { - Transform v; v.basis.elements[0].x = f->get_real(); v.basis.elements[0].y = f->get_real(); @@ -283,7 +261,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { r_v = v; } break; case VARIANT_COLOR: { - Color v; v.r = f->get_real(); v.g = f->get_real(); @@ -293,12 +270,10 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_STRING_NAME: { - r_v = StringName(get_unicode_string()); } break; case VARIANT_NODE_PATH: { - Vector<StringName> names; Vector<StringName> subnames; bool absolute; @@ -311,10 +286,12 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { subname_count += 1; // has a property field, so we should count it as well } - for (int i = 0; i < name_count; i++) + for (int i = 0; i < name_count; i++) { names.push_back(_get_string()); - for (uint32_t i = 0; i < subname_count; i++) + } + for (uint32_t i = 0; i < subname_count; i++) { subnames.push_back(_get_string()); + } NodePath np = NodePath(names, subnames, absolute); @@ -322,25 +299,26 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_RID: { - r_v = f->get_32(); } break; case VARIANT_OBJECT: { - uint32_t objtype = f->get_32(); switch (objtype) { - case OBJECT_EMPTY: { //do none } break; case OBJECT_INTERNAL_RESOURCE: { uint32_t index = f->get_32(); + String path = res_path + "::" + itos(index); + if (use_nocache) { - r_v = internal_resources[index].cache; + if (!internal_index_cache.has(path)) { + WARN_PRINT(String("Couldn't load resource (no cache): " + path).utf8().get_data()); + } + r_v = internal_index_cache[path]; } else { - String path = res_path + "::" + itos(index); RES res = ResourceLoader::load(path); if (res.is_null()) { WARN_PRINT(String("Couldn't load resource: " + path).utf8().get_data()); @@ -380,7 +358,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { WARN_PRINT("Broken external resource! (index out of size)"); r_v = Variant(); } else { - if (external_resources[erindex].cache.is_null()) { //cache not here yet, wait for it? if (use_sub_threads) { @@ -389,10 +366,8 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { if (err != OK || external_resources[erindex].cache.is_null()) { if (!ResourceLoader::get_abort_on_missing_resources()) { - ResourceLoader::notify_dependency_error(local_path, external_resources[erindex].path, external_resources[erindex].type); } else { - error = ERR_FILE_MISSING_DEPENDENCIES; ERR_FAIL_V_MSG(error, "Can't load dependency: " + external_resources[erindex].path + "."); } @@ -405,23 +380,19 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; default: { - ERR_FAIL_V(ERR_FILE_CORRUPT); } break; } } break; case VARIANT_CALLABLE: { - r_v = Callable(); } break; case VARIANT_SIGNAL: { - r_v = Signal(); } break; case VARIANT_DICTIONARY: { - uint32_t len = f->get_32(); Dictionary d; //last bit means shared len &= 0x7FFFFFFF; @@ -437,7 +408,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { r_v = d; } break; case VARIANT_ARRAY: { - uint32_t len = f->get_32(); Array a; //last bit means shared len &= 0x7FFFFFFF; @@ -452,7 +422,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_RAW_ARRAY: { - uint32_t len = f->get_32(); Vector<uint8_t> array; @@ -465,7 +434,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_INT32_ARRAY: { - uint32_t len = f->get_32(); Vector<int32_t> array; @@ -476,7 +444,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { { uint32_t *ptr = (uint32_t *)w.ptr(); for (int i = 0; i < len; i++) { - ptr[i] = BSWAP32(ptr[i]); } } @@ -486,7 +453,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { r_v = array; } break; case VARIANT_INT64_ARRAY: { - uint32_t len = f->get_32(); Vector<int64_t> array; @@ -497,7 +463,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { { uint64_t *ptr = (uint64_t *)w.ptr(); for (int i = 0; i < len; i++) { - ptr[i] = BSWAP64(ptr[i]); } } @@ -507,7 +472,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { r_v = array; } break; case VARIANT_FLOAT32_ARRAY: { - uint32_t len = f->get_32(); Vector<float> array; @@ -518,7 +482,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { { uint32_t *ptr = (uint32_t *)w.ptr(); for (int i = 0; i < len; i++) { - ptr[i] = BSWAP32(ptr[i]); } } @@ -528,7 +491,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { r_v = array; } break; case VARIANT_FLOAT64_ARRAY: { - uint32_t len = f->get_32(); Vector<double> array; @@ -539,7 +501,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { { uint64_t *ptr = (uint64_t *)w.ptr(); for (int i = 0; i < len; i++) { - ptr[i] = BSWAP64(ptr[i]); } } @@ -549,19 +510,18 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { r_v = array; } break; case VARIANT_STRING_ARRAY: { - uint32_t len = f->get_32(); Vector<String> array; array.resize(len); String *w = array.ptrw(); - for (uint32_t i = 0; i < len; i++) + for (uint32_t i = 0; i < len; i++) { w[i] = get_unicode_string(); + } r_v = array; } break; case VARIANT_VECTOR2_ARRAY: { - uint32_t len = f->get_32(); Vector<Vector2> array; @@ -573,7 +533,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { { uint32_t *ptr = (uint32_t *)w.ptr(); for (int i = 0; i < len * 2; i++) { - ptr[i] = BSWAP32(ptr[i]); } } @@ -588,7 +547,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_VECTOR3_ARRAY: { - uint32_t len = f->get_32(); Vector<Vector3> array; @@ -600,7 +558,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { { uint32_t *ptr = (uint32_t *)w.ptr(); for (int i = 0; i < len * 3; i++) { - ptr[i] = BSWAP32(ptr[i]); } } @@ -615,7 +572,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_COLOR_ARRAY: { - uint32_t len = f->get_32(); Vector<Color> array; @@ -627,7 +583,6 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { { uint32_t *ptr = (uint32_t *)w.ptr(); for (int i = 0; i < len * 4; i++) { - ptr[i] = BSWAP32(ptr[i]); } } @@ -649,23 +604,21 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } void ResourceLoaderBinary::set_local_path(const String &p_local_path) { - res_path = p_local_path; } Ref<Resource> ResourceLoaderBinary::get_resource() { - return resource; } -Error ResourceLoaderBinary::load() { - if (error != OK) +Error ResourceLoaderBinary::load() { + if (error != OK) { return error; + } int stage = 0; for (int i = 0; i < external_resources.size(); i++) { - String path = external_resources[i].path; if (remaps.has(path)) { @@ -684,10 +637,8 @@ Error ResourceLoaderBinary::load() { if (external_resources[i].cache.is_null()) { if (!ResourceLoader::get_abort_on_missing_resources()) { - ResourceLoader::notify_dependency_error(local_path, path, external_resources[i].type); } else { - error = ERR_FILE_MISSING_DEPENDENCIES; ERR_FAIL_V_MSG(error, "Can't load dependency: " + path + "."); } @@ -697,10 +648,8 @@ Error ResourceLoaderBinary::load() { Error err = ResourceLoader::load_threaded_request(path, external_resources[i].type, use_sub_threads, local_path); if (err != OK) { if (!ResourceLoader::get_abort_on_missing_resources()) { - ResourceLoader::notify_dependency_error(local_path, path, external_resources[i].type); } else { - error = ERR_FILE_MISSING_DEPENDENCIES; ERR_FAIL_V_MSG(error, "Can't load dependency: " + path + "."); } @@ -711,7 +660,6 @@ Error ResourceLoaderBinary::load() { } for (int i = 0; i < internal_resources.size(); i++) { - bool main = i == (internal_resources.size() - 1); //maybe it is loaded already @@ -719,15 +667,15 @@ Error ResourceLoaderBinary::load() { int subindex = 0; if (!main) { + path = internal_resources[i].path; - if (!use_nocache) { - path = internal_resources[i].path; - if (path.begins_with("local://")) { - path = path.replace_first("local://", ""); - subindex = path.to_int(); - path = res_path + "::" + path; - } + if (path.begins_with("local://")) { + path = path.replace_first("local://", ""); + subindex = path.to_int(); + path = res_path + "::" + path; + } + if (!use_nocache) { if (ResourceCache::has(path)) { //already loaded, don't do anything stage++; @@ -736,9 +684,9 @@ Error ResourceLoaderBinary::load() { } } } else { - - if (!use_nocache && !ResourceCache::has(res_path)) + if (!use_nocache && !ResourceCache::has(res_path)) { path = res_path; + } } uint64_t offset = internal_resources[i].offset; @@ -769,7 +717,7 @@ Error ResourceLoaderBinary::load() { r->set_subindex(subindex); if (!main) { - internal_resources.write[i].cache = res; + internal_index_cache[path] = res; } int pc = f->get_32(); @@ -777,7 +725,6 @@ Error ResourceLoaderBinary::load() { //set properties for (int j = 0; j < pc; j++) { - StringName name = _get_string(); if (name == StringName()) { @@ -788,8 +735,9 @@ Error ResourceLoaderBinary::load() { Variant value; error = parse_variant(value); - if (error) + if (error) { return error; + } res->set(name, value); } @@ -805,7 +753,6 @@ Error ResourceLoaderBinary::load() { resource_cache.push_back(res); if (main) { - f->close(); resource = res; resource->set_as_translation_remapped(translation_remapped); @@ -818,19 +765,16 @@ Error ResourceLoaderBinary::load() { } void ResourceLoaderBinary::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(); f->store_32(utf8.length() + 1); f->store_buffer((const uint8_t *)utf8.get_data(), utf8.length() + 1); } static String get_ustring(FileAccess *f) { - int len = f->get_32(); Vector<char> str_buf; str_buf.resize(len); @@ -841,13 +785,13 @@ static String get_ustring(FileAccess *f) { } String ResourceLoaderBinary::get_unicode_string() { - int len = f->get_32(); if (len > str_buf.size()) { str_buf.resize(len); } - if (len == 0) + if (len == 0) { return String(); + } f->get_buffer((uint8_t *)&str_buf[0], len); String s; s.parse_utf8(&str_buf[0]); @@ -855,13 +799,12 @@ String ResourceLoaderBinary::get_unicode_string() { } void ResourceLoaderBinary::get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types) { - open(p_f); - if (error) + if (error) { return; + } for (int i = 0; i < external_resources.size(); i++) { - String dep = external_resources[i].path; if (p_add_types && external_resources[i].type != String()) { @@ -873,7 +816,6 @@ void ResourceLoaderBinary::get_dependencies(FileAccess *p_f, List<String> *p_dep } void ResourceLoaderBinary::open(FileAccess *p_f) { - error = OK; f = p_f; @@ -918,9 +860,9 @@ void ResourceLoaderBinary::open(FileAccess *p_f) { print_bl("format: " + itos(ver_format)); if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { - f->close(); - ERR_FAIL_MSG("File format '" + itos(FORMAT_VERSION) + "." + itos(ver_major) + "." + itos(ver_minor) + "' is too new! Please upgrade to a new engine version: " + local_path + "."); + ERR_FAIL_MSG(vformat("File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s).", + local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH)); } type = get_unicode_string(); @@ -928,13 +870,13 @@ void ResourceLoaderBinary::open(FileAccess *p_f) { print_bl("type: " + type); importmd_ofs = f->get_64(); - for (int i = 0; i < 14; i++) + for (int i = 0; i < 14; i++) { f->get_32(); //skip a few reserved fields + } uint32_t string_table_size = f->get_32(); string_map.resize(string_table_size); for (uint32_t i = 0; i < string_table_size; i++) { - StringName s = get_unicode_string(); string_map.write[i] = s; } @@ -943,7 +885,6 @@ void ResourceLoaderBinary::open(FileAccess *p_f) { uint32_t ext_resources_size = f->get_32(); for (uint32_t i = 0; i < ext_resources_size; i++) { - ExtResource er; er.type = get_unicode_string(); @@ -956,7 +897,6 @@ void ResourceLoaderBinary::open(FileAccess *p_f) { uint32_t int_resources_size = f->get_32(); for (uint32_t i = 0; i < int_resources_size; i++) { - IntResource ir; ir.path = get_unicode_string(); ir.offset = f->get_64(); @@ -966,7 +906,6 @@ void ResourceLoaderBinary::open(FileAccess *p_f) { print_bl("int resources: " + itos(int_resources_size)); if (f->eof_reached()) { - error = ERR_FILE_CORRUPT; f->close(); ERR_FAIL_MSG("Premature end of file (EOF): " + local_path + "."); @@ -974,7 +913,6 @@ void ResourceLoaderBinary::open(FileAccess *p_f) { } String ResourceLoaderBinary::recognize(FileAccess *p_f) { - error = OK; f = p_f; @@ -1008,7 +946,6 @@ String ResourceLoaderBinary::recognize(FileAccess *p_f) { uint32_t ver_format = f->get_32(); if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { - f->close(); return ""; } @@ -1018,28 +955,16 @@ String ResourceLoaderBinary::recognize(FileAccess *p_f) { return type; } -ResourceLoaderBinary::ResourceLoaderBinary() : - translation_remapped(false), - ver_format(0), - f(nullptr), - importmd_ofs(0), - error(OK) { - - use_nocache = false; - progress = nullptr; - use_sub_threads = false; -} - ResourceLoaderBinary::~ResourceLoaderBinary() { - - if (f) + if (f) { memdelete(f); + } } RES ResourceFormatLoaderBinary::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) { - - if (r_error) + if (r_error) { *r_error = ERR_FILE_CANT_OPEN; + } Error err; FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err); @@ -1069,7 +994,6 @@ RES ResourceFormatLoaderBinary::load(const String &p_path, const String &p_origi } void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const { - if (p_type == "") { get_recognized_extensions(p_extensions); return; @@ -1085,8 +1009,8 @@ void ResourceFormatLoaderBinary::get_recognized_extensions_for_type(const String p_extensions->push_back(ext); } } -void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const { +void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_extensions) const { List<String> extensions; ClassDB::get_resource_base_extensions(&extensions); extensions.sort(); @@ -1098,12 +1022,10 @@ void ResourceFormatLoaderBinary::get_recognized_extensions(List<String> *p_exten } bool ResourceFormatLoaderBinary::handles_type(const String &p_type) const { - return true; //handles all } void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) { - FileAccess *f = FileAccess::open(p_path, FileAccess::READ); ERR_FAIL_COND_MSG(!f, "Cannot open file '" + p_path + "'."); @@ -1115,7 +1037,6 @@ void ResourceFormatLoaderBinary::get_dependencies(const String &p_path, List<Str } Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, const Map<String, String> &p_map) { - //Error error=OK; FileAccess *f = FileAccess::open(p_path, FileAccess::READ); @@ -1181,7 +1102,6 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons uint32_t ver_format = f->get_32(); if (ver_format < FORMAT_VERSION_CAN_RENAME_DEPS) { - memdelete(f); memdelete(fw); DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); @@ -1213,10 +1133,11 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons } if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { - memdelete(f); memdelete(fw); - ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, "File format '" + itos(FORMAT_VERSION) + "." + itos(ver_major) + "." + itos(ver_minor) + "' is too new! Please upgrade to a new engine version: " + local_path + "."); + ERR_FAIL_V_MSG(ERR_FILE_UNRECOGNIZED, + vformat("File '%s' can't be loaded, as it uses a format version (%d) or engine version (%d.%d) which are not supported by your engine version (%s).", + local_path, ver_format, ver_major, ver_minor, VERSION_BRANCH)); } // Since we're not actually converting the file contents, leave the version @@ -1242,7 +1163,6 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons fw->store_32(string_table_size); for (uint32_t i = 0; i < string_table_size; i++) { - String s = get_ustring(f); save_ustring(fw, s); } @@ -1251,7 +1171,6 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons uint32_t ext_resources_size = f->get_32(); fw->store_32(ext_resources_size); for (uint32_t i = 0; i < ext_resources_size; i++) { - String type = get_ustring(f); String path = get_ustring(f); @@ -1282,7 +1201,6 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons fw->store_32(int_resources_size); for (uint32_t i = 0; i < int_resources_size; i++) { - String path = get_ustring(f); uint64_t offset = f->get_64(); save_ustring(fw, path); @@ -1316,7 +1234,6 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons } String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const { - FileAccess *f = FileAccess::open(p_path, FileAccess::READ); if (!f) { return ""; //could not rwead @@ -1335,36 +1252,30 @@ String ResourceFormatLoaderBinary::get_resource_type(const String &p_path) const /////////////////////////////////////////////////////////// void ResourceFormatSaverBinaryInstance::_pad_buffer(FileAccess *f, int p_bytes) { - int extra = 4 - (p_bytes % 4); if (extra < 4) { - for (int i = 0; i < extra; i++) + for (int i = 0; i < extra; i++) { f->store_8(0); //pad to 32 + } } } void ResourceFormatSaverBinaryInstance::_write_variant(const Variant &p_property, const PropertyInfo &p_hint) { - write_variant(f, p_property, resource_set, external_resources, string_map, p_hint); } void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Variant &p_property, Set<RES> &resource_set, Map<RES, int> &external_resources, Map<StringName, int> &string_map, const PropertyInfo &p_hint) { - switch (p_property.get_type()) { - case Variant::NIL: { - f->store_32(VARIANT_NIL); // don't store anything } break; case Variant::BOOL: { - f->store_32(VARIANT_BOOL); bool val = p_property; f->store_32(val); } break; case Variant::INT: { - int64_t val = p_property; if (val > 0x7FFFFFFF || val < -(int64_t)0x80000000) { f->store_32(VARIANT_INT64); @@ -1377,28 +1288,24 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::FLOAT: { - double d = p_property; float fl = d; if (double(fl) != d) { f->store_32(VARIANT_DOUBLE); f->store_double(d); } else { - f->store_32(VARIANT_FLOAT); f->store_real(fl); } } break; case Variant::STRING: { - f->store_32(VARIANT_STRING); String val = p_property; save_unicode_string(f, val); } break; case Variant::VECTOR2: { - f->store_32(VARIANT_VECTOR2); Vector2 val = p_property; f->store_real(val.x); @@ -1406,7 +1313,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::VECTOR2I: { - f->store_32(VARIANT_VECTOR2I); Vector2i val = p_property; f->store_32(val.x); @@ -1414,7 +1320,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::RECT2: { - f->store_32(VARIANT_RECT2); Rect2 val = p_property; f->store_real(val.position.x); @@ -1424,7 +1329,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::RECT2I: { - f->store_32(VARIANT_RECT2I); Rect2i val = p_property; f->store_32(val.position.x); @@ -1434,7 +1338,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::VECTOR3: { - f->store_32(VARIANT_VECTOR3); Vector3 val = p_property; f->store_real(val.x); @@ -1443,7 +1346,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::VECTOR3I: { - f->store_32(VARIANT_VECTOR3I); Vector3i val = p_property; f->store_32(val.x); @@ -1452,7 +1354,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::PLANE: { - f->store_32(VARIANT_PLANE); Plane val = p_property; f->store_real(val.normal.x); @@ -1462,7 +1363,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::QUAT: { - f->store_32(VARIANT_QUAT); Quat val = p_property; f->store_real(val.x); @@ -1472,7 +1372,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::AABB: { - f->store_32(VARIANT_AABB); AABB val = p_property; f->store_real(val.position.x); @@ -1484,7 +1383,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::TRANSFORM2D: { - f->store_32(VARIANT_MATRIX32); Transform2D val = p_property; f->store_real(val.elements[0].x); @@ -1496,7 +1394,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::BASIS: { - f->store_32(VARIANT_MATRIX3); Basis val = p_property; f->store_real(val.elements[0].x); @@ -1511,7 +1408,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::TRANSFORM: { - f->store_32(VARIANT_TRANSFORM); Transform val = p_property; f->store_real(val.basis.elements[0].x); @@ -1529,7 +1425,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::COLOR: { - f->store_32(VARIANT_COLOR); Color val = p_property; f->store_real(val.r); @@ -1539,7 +1434,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::STRING_NAME: { - f->store_32(VARIANT_STRING_NAME); String val = p_property; save_unicode_string(f, val); @@ -1551,8 +1445,9 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia NodePath np = p_property; f->store_16(np.get_name_count()); uint16_t snc = np.get_subname_count(); - if (np.is_absolute()) + if (np.is_absolute()) { snc |= 0x8000; + } f->store_16(snc); for (int i = 0; i < np.get_name_count(); i++) { if (string_map.has(np.get_name(i))) { @@ -1570,15 +1465,13 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } } break; - case Variant::_RID: { - + case Variant::RID: { f->store_32(VARIANT_RID); WARN_PRINT("Can't save RIDs."); RID val = p_property; f->store_32(val.get_id()); } break; case Variant::OBJECT: { - f->store_32(VARIANT_OBJECT); RES res = p_property; if (res.is_null()) { @@ -1590,7 +1483,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia f->store_32(OBJECT_EXTERNAL_RESOURCE_INDEX); f->store_32(external_resources[res]); } else { - if (!resource_set.has(res)) { f->store_32(OBJECT_EMPTY); ERR_FAIL_MSG("Resource was not pre cached for the resource section, most likely due to circular reference."); @@ -1603,18 +1495,15 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::CALLABLE: { - f->store_32(VARIANT_CALLABLE); WARN_PRINT("Can't save Callables."); } break; case Variant::SIGNAL: { - f->store_32(VARIANT_SIGNAL); WARN_PRINT("Can't save Signals."); } break; case Variant::DICTIONARY: { - f->store_32(VARIANT_DICTIONARY); Dictionary d = p_property; f->store_32(uint32_t(d.size())); @@ -1623,7 +1512,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia d.get_key_list(&keys); for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { - /* if (!_check_type(dict[E->get()])) continue; @@ -1635,18 +1523,15 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::ARRAY: { - f->store_32(VARIANT_ARRAY); Array a = p_property; f->store_32(uint32_t(a.size())); for (int i = 0; i < a.size(); i++) { - write_variant(f, a[i], resource_set, external_resources, string_map); } } break; case Variant::PACKED_BYTE_ARRAY: { - f->store_32(VARIANT_RAW_ARRAY); Vector<uint8_t> arr = p_property; int len = arr.size(); @@ -1657,29 +1542,28 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::PACKED_INT32_ARRAY: { - f->store_32(VARIANT_INT32_ARRAY); Vector<int32_t> arr = p_property; int len = arr.size(); f->store_32(len); const int32_t *r = arr.ptr(); - for (int i = 0; i < len; i++) + for (int i = 0; i < len; i++) { f->store_32(r[i]); + } } break; case Variant::PACKED_INT64_ARRAY: { - f->store_32(VARIANT_INT64_ARRAY); Vector<int64_t> arr = p_property; int len = arr.size(); f->store_32(len); const int64_t *r = arr.ptr(); - for (int i = 0; i < len; i++) + for (int i = 0; i < len; i++) { f->store_64(r[i]); + } } break; case Variant::PACKED_FLOAT32_ARRAY: { - f->store_32(VARIANT_FLOAT32_ARRAY); Vector<float> arr = p_property; int len = arr.size(); @@ -1691,7 +1575,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::PACKED_FLOAT64_ARRAY: { - f->store_32(VARIANT_FLOAT64_ARRAY); Vector<double> arr = p_property; int len = arr.size(); @@ -1703,7 +1586,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::PACKED_STRING_ARRAY: { - f->store_32(VARIANT_STRING_ARRAY); Vector<String> arr = p_property; int len = arr.size(); @@ -1715,7 +1597,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::PACKED_VECTOR3_ARRAY: { - f->store_32(VARIANT_VECTOR3_ARRAY); Vector<Vector3> arr = p_property; int len = arr.size(); @@ -1729,7 +1610,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::PACKED_VECTOR2_ARRAY: { - f->store_32(VARIANT_VECTOR2_ARRAY); Vector<Vector2> arr = p_property; int len = arr.size(); @@ -1742,7 +1622,6 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; case Variant::PACKED_COLOR_ARRAY: { - f->store_32(VARIANT_COLOR_ARRAY); Vector<Color> arr = p_property; int len = arr.size(); @@ -1757,21 +1636,19 @@ void ResourceFormatSaverBinaryInstance::write_variant(FileAccess *f, const Varia } break; default: { - ERR_FAIL_MSG("Invalid variant."); } } } void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant, bool p_main) { - switch (p_variant.get_type()) { case Variant::OBJECT: { - RES res = p_variant; - if (res.is_null() || external_resources.has(res)) + if (res.is_null() || external_resources.has(res)) { return; + } if (!p_main && (!bundle_resources) && res->get_path().length() && res->get_path().find("::") == -1) { if (res->get_path() == path) { @@ -1783,17 +1660,16 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant return; } - if (resource_set.has(res)) + if (resource_set.has(res)) { return; + } List<PropertyInfo> property_list; res->get_property_list(&property_list); for (List<PropertyInfo>::Element *E = property_list.front(); E; E = E->next()) { - if (E->get().usage & PROPERTY_USAGE_STORAGE) { - Variant value = res->get(E->get().name); if (E->get().usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) { RES sres = value; @@ -1817,11 +1693,9 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant } break; case Variant::ARRAY: { - Array varray = p_variant; int len = varray.size(); for (int i = 0; i < len; i++) { - const Variant &v = varray.get(i); _find_resources(v); } @@ -1829,12 +1703,10 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant } break; case Variant::DICTIONARY: { - Dictionary d = p_variant; List<Variant> keys; d.get_key_list(&keys); for (List<Variant>::Element *E = keys.front(); E; E = E->next()) { - _find_resources(E->get()); Variant v = d[E->get()]; _find_resources(v); @@ -1843,10 +1715,12 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant case Variant::NODE_PATH: { //take the chance and save node path strings NodePath np = p_variant; - for (int i = 0; i < np.get_name_count(); i++) + for (int i = 0; i < np.get_name_count(); i++) { get_string_index(np.get_name(i)); - for (int i = 0; i < np.get_subname_count(); i++) + } + for (int i = 0; i < np.get_subname_count(); i++) { get_string_index(np.get_subname(i)); + } } break; default: { @@ -1855,7 +1729,6 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant } void ResourceFormatSaverBinaryInstance::save_unicode_string(FileAccess *f, const String &p_string, bool p_bit_on_len) { - CharString utf8 = p_string.utf8(); if (p_bit_on_len) { f->store_32((utf8.length() + 1) | 0x80000000); @@ -1866,10 +1739,10 @@ void ResourceFormatSaverBinaryInstance::save_unicode_string(FileAccess *f, const } int ResourceFormatSaverBinaryInstance::get_string_index(const String &p_string) { - StringName s = p_string; - if (string_map.has(s)) + if (string_map.has(s)) { return string_map[s]; + } string_map[s] = strings.size(); strings.push_back(s); @@ -1877,15 +1750,15 @@ int ResourceFormatSaverBinaryInstance::get_string_index(const String &p_string) } Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p_resource, uint32_t p_flags) { - Error err; if (p_flags & ResourceSaver::FLAG_COMPRESS) { FileAccessCompressed *fac = memnew(FileAccessCompressed); fac->configure("RSCC"); f = fac; err = fac->_open(p_path, FileAccess::WRITE); - if (err) + if (err) { memdelete(f); + } } else { f = FileAccess::open(p_path, FileAccess::WRITE, &err); @@ -1899,8 +1772,9 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p big_endian = p_flags & ResourceSaver::FLAG_SAVE_BIG_ENDIAN; takeover_paths = p_flags & ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS; - if (!p_path.begins_with("res://")) + if (!p_path.begins_with("res://")) { takeover_paths = false; + } local_path = p_path.get_base_dir(); path = ProjectSettings::get_singleton()->localize_path(p_path); @@ -1916,8 +1790,9 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p if (big_endian) { f->store_32(1); f->set_endian_swap(true); - } else + } else { f->store_32(0); + } f->store_32(0); //64 bits file, false for now f->store_32(VERSION_MAJOR); @@ -1932,15 +1807,14 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p save_unicode_string(f, p_resource->get_class()); f->store_64(0); //offset to import metadata - for (int i = 0; i < 14; i++) + for (int i = 0; i < 14; i++) { f->store_32(0); // reserved + } List<ResourceData> resources; { - for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) { - ResourceData &rd = resources.push_back(ResourceData())->get(); rd.type = E->get()->get_class(); @@ -1948,9 +1822,9 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p E->get()->get_property_list(&property_list); for (List<PropertyInfo>::Element *F = property_list.front(); F; F = F->next()) { - - if (skip_editor && F->get().name.begins_with("__editor")) + if (skip_editor && F->get().name.begins_with("__editor")) { continue; + } if ((F->get().usage & PROPERTY_USAGE_STORAGE)) { Property p; p.name_idx = get_string_index(F->get().name); @@ -1995,7 +1869,6 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p } for (int i = 0; i < save_order.size(); i++) { - save_unicode_string(f, save_order[i]->get_save_class()); String path = save_order[i]->get_path(); path = relative_paths ? local_path.path_to_file(path) : path; @@ -2007,10 +1880,8 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p Set<int> used_indices; for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) { - RES r = E->get(); if (r->get_path() == "" || r->get_path().find("::") != -1) { - if (r->get_subindex() != 0) { if (used_indices.has(r->get_subindex())) { r->set_subindex(0); //repeated @@ -2022,7 +1893,6 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p } for (List<RES>::Element *E = saved_resources.front(); E; E = E->next()) { - RES r = E->get(); if (r->get_path() == "" || r->get_path().find("::") != -1) { if (r->get_subindex() == 0) { @@ -2053,7 +1923,6 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p //now actually save the resources for (List<ResourceData>::Element *E = resources.front(); E; E = E->next()) { - ResourceData &rd = E->get(); ofs_table.push_back(f->get_position()); @@ -2061,7 +1930,6 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p f->store_32(rd.properties.size()); for (List<Property>::Element *F = rd.properties.front(); F; F = F->next()) { - Property &p = F->get(); f->store_32(p.name_idx); _write_variant(p.value, F->get().pi); @@ -2090,28 +1958,25 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p } Error ResourceFormatSaverBinary::save(const String &p_path, const RES &p_resource, uint32_t p_flags) { - String local_path = ProjectSettings::get_singleton()->localize_path(p_path); ResourceFormatSaverBinaryInstance saver; return saver.save(local_path, p_resource, p_flags); } bool ResourceFormatSaverBinary::recognize(const RES &p_resource) const { - return true; //all recognized } void ResourceFormatSaverBinary::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const { - String base = p_resource->get_base_extension().to_lower(); p_extensions->push_back(base); - if (base != "res") + if (base != "res") { p_extensions->push_back("res"); + } } ResourceFormatSaverBinary *ResourceFormatSaverBinary::singleton = nullptr; ResourceFormatSaverBinary::ResourceFormatSaverBinary() { - singleton = this; } diff --git a/core/io/resource_format_binary.h b/core/io/resource_format_binary.h index 0f8fc9445b..428725f1d2 100644 --- a/core/io/resource_format_binary.h +++ b/core/io/resource_format_binary.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -36,17 +36,16 @@ #include "core/os/file_access.h" class ResourceLoaderBinary { - - bool translation_remapped; + bool translation_remapped = false; String local_path; String res_path; String type; Ref<Resource> resource; - uint32_t ver_format; + uint32_t ver_format = 0; - FileAccess *f; + FileAccess *f = nullptr; - uint64_t importmd_ofs; + uint64_t importmd_ofs = 0; Vector<char> str_buf; List<RES> resource_cache; @@ -61,25 +60,25 @@ class ResourceLoaderBinary { RES cache; }; - bool use_sub_threads; - float *progress; + bool use_sub_threads = false; + float *progress = nullptr; Vector<ExtResource> external_resources; struct IntResource { String path; uint64_t offset; - RES cache; }; Vector<IntResource> internal_resources; + Map<String, RES> internal_index_cache; String get_unicode_string(); void _advance_padding(uint32_t p_len); Map<String, String> remaps; - Error error; + Error error = OK; - bool use_nocache; + bool use_nocache = false; friend class ResourceFormatLoaderBinary; @@ -98,7 +97,7 @@ public: String recognize(FileAccess *p_f); void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types); - ResourceLoaderBinary(); + ResourceLoaderBinary() {} ~ResourceLoaderBinary(); }; @@ -114,7 +113,6 @@ public: }; class ResourceFormatSaverBinaryInstance { - String local_path; String path; @@ -147,7 +145,6 @@ class ResourceFormatSaverBinaryInstance { }; struct ResourceData { - String type; List<Property> properties; }; diff --git a/core/io/resource_importer.cpp b/core/io/resource_importer.cpp index 643df53f8c..d86877ee14 100644 --- a/core/io/resource_importer.cpp +++ b/core/io/resource_importer.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,15 +30,15 @@ #include "resource_importer.h" +#include "core/config/project_settings.h" #include "core/os/os.h" -#include "core/variant_parser.h" +#include "core/variant/variant_parser.h" bool ResourceFormatImporter::SortImporterByName::operator()(const Ref<ResourceImporter> &p_a, const Ref<ResourceImporter> &p_b) const { return p_a->get_importer_name() < p_b->get_importer_name(); } Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndType &r_path_and_type, bool *r_valid) const { - Error err; FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err); @@ -64,7 +64,6 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy String error_text; bool path_found = false; //first match must have priority while (true) { - assign = Variant(); next_tag.fields.clear(); next_tag.name = String(); @@ -91,7 +90,7 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy r_path_and_type.path = value; path_found = true; //first match must have priority } else if (assign == "type") { - r_path_and_type.type = value; + r_path_and_type.type = ClassDB::get_compatibility_remapped_class(value); } else if (assign == "importer") { r_path_and_type.importer = value; } else if (assign == "group_file") { @@ -118,14 +117,13 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy } RES ResourceFormatImporter::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) { - PathAndType pat; Error err = _get_path_and_type(p_path, pat); if (err != OK) { - - if (r_error) + if (r_error) { *r_error = err; + } return RES(); } @@ -143,7 +141,6 @@ RES ResourceFormatImporter::load(const String &p_path, const String &p_original_ } void ResourceFormatImporter::get_recognized_extensions(List<String> *p_extensions) const { - Set<String> found; for (int i = 0; i < importers.size(); i++) { @@ -159,7 +156,6 @@ void ResourceFormatImporter::get_recognized_extensions(List<String> *p_extension } void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const { - if (p_type == "") { get_recognized_extensions(p_extensions); return; @@ -169,11 +165,13 @@ void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_ for (int i = 0; i < importers.size(); i++) { String res_type = importers[i]->get_resource_type(); - if (res_type == String()) + if (res_type == String()) { continue; + } - if (!ClassDB::is_parent_class(res_type, p_type)) + if (!ClassDB::is_parent_class(res_type, p_type)) { continue; + } List<String> local_exts; importers[i]->get_recognized_extensions(&local_exts); @@ -187,26 +185,17 @@ void ResourceFormatImporter::get_recognized_extensions_for_type(const String &p_ } bool ResourceFormatImporter::exists(const String &p_path) const { - return FileAccess::exists(p_path + ".import"); } bool ResourceFormatImporter::recognize_path(const String &p_path, const String &p_for_type) const { - return FileAccess::exists(p_path + ".import"); } -bool ResourceFormatImporter::can_be_imported(const String &p_path) const { - - return ResourceFormatLoader::recognize_path(p_path); -} - int ResourceFormatImporter::get_import_order(const String &p_path) const { - Ref<ResourceImporter> importer; if (FileAccess::exists(p_path + ".import")) { - PathAndType pat; Error err = _get_path_and_type(p_path, pat); @@ -214,37 +203,35 @@ int ResourceFormatImporter::get_import_order(const String &p_path) const { importer = get_importer_by_name(pat.importer); } } else { - importer = get_importer_by_extension(p_path.get_extension().to_lower()); } - if (importer.is_valid()) + if (importer.is_valid()) { return importer->get_import_order(); + } return 0; } bool ResourceFormatImporter::handles_type(const String &p_type) const { - for (int i = 0; i < importers.size(); i++) { - String res_type = importers[i]->get_resource_type(); - if (res_type == String()) + if (res_type == String()) { continue; - if (ClassDB::is_parent_class(res_type, p_type)) + } + if (ClassDB::is_parent_class(res_type, p_type)) { return true; + } } return true; } String ResourceFormatImporter::get_internal_resource_path(const String &p_path) const { - PathAndType pat; Error err = _get_path_and_type(p_path, pat); if (err != OK) { - return String(); } @@ -252,12 +239,12 @@ String ResourceFormatImporter::get_internal_resource_path(const String &p_path) } void ResourceFormatImporter::get_internal_resource_path_list(const String &p_path, List<String> *r_paths) { - Error err; FileAccess *f = FileAccess::open(p_path + ".import", FileAccess::READ, &err); - if (!f) + if (!f) { return; + } VariantParser::StreamFile stream; stream.f = f; @@ -269,7 +256,6 @@ void ResourceFormatImporter::get_internal_resource_path_list(const String &p_pat int lines = 0; String error_text; while (true) { - assign = Variant(); next_tag.fields.clear(); next_tag.name = String(); @@ -298,7 +284,6 @@ void ResourceFormatImporter::get_internal_resource_path_list(const String &p_pat } String ResourceFormatImporter::get_import_group_file(const String &p_path) const { - bool valid = true; PathAndType pat; _get_path_and_type(p_path, pat, &valid); @@ -306,7 +291,6 @@ String ResourceFormatImporter::get_import_group_file(const String &p_path) const } bool ResourceFormatImporter::is_import_valid(const String &p_path) const { - bool valid = true; PathAndType pat; _get_path_and_type(p_path, pat, &valid); @@ -314,12 +298,10 @@ bool ResourceFormatImporter::is_import_valid(const String &p_path) const { } String ResourceFormatImporter::get_resource_type(const String &p_path) const { - PathAndType pat; Error err = _get_path_and_type(p_path, pat); if (err != OK) { - return ""; } @@ -331,7 +313,6 @@ Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) cons Error err = _get_path_and_type(p_path, pat); if (err != OK) { - return Variant(); } @@ -339,12 +320,10 @@ Variant ResourceFormatImporter::get_resource_metadata(const String &p_path) cons } void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) { - PathAndType pat; Error err = _get_path_and_type(p_path, pat); if (err != OK) { - return; } @@ -352,7 +331,6 @@ void ResourceFormatImporter::get_dependencies(const String &p_path, List<String> } Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String &p_name) const { - for (int i = 0; i < importers.size(); i++) { if (importers[i]->get_importer_name() == p_name) { return importers[i]; @@ -363,7 +341,6 @@ Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_name(const String } void ResourceFormatImporter::get_importers_for_extension(const String &p_extension, List<Ref<ResourceImporter>> *r_importers) { - for (int i = 0; i < importers.size(); i++) { List<String> local_exts; importers[i]->get_recognized_extensions(&local_exts); @@ -376,12 +353,10 @@ void ResourceFormatImporter::get_importers_for_extension(const String &p_extensi } Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const String &p_extension) const { - Ref<ResourceImporter> importer; float priority = 0; for (int i = 0; i < importers.size(); i++) { - List<String> local_exts; importers[i]->get_recognized_extensions(&local_exts); for (List<String>::Element *F = local_exts.front(); F; F = F->next()) { @@ -396,12 +371,10 @@ Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_extension(const St } String ResourceFormatImporter::get_import_base_path(const String &p_for_file) const { - - return "res://.import/" + p_for_file.get_file() + "-" + p_for_file.md5_text(); + return ProjectSettings::IMPORTED_FILES_PATH.plus_file(p_for_file.get_file() + "-" + p_for_file.md5_text()); } bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) const { - bool valid = true; PathAndType pat; _get_path_and_type(p_path, pat, &valid); @@ -422,7 +395,6 @@ bool ResourceFormatImporter::are_import_settings_valid(const String &p_path) con } String ResourceFormatImporter::get_import_settings_hash() const { - Vector<Ref<ResourceImporter>> sorted_importers = importers; sorted_importers.sort_custom<SortImporterByName>(); diff --git a/core/io/resource_importer.h b/core/io/resource_importer.h index 6b76912494..d31273e3cb 100644 --- a/core/io/resource_importer.h +++ b/core/io/resource_importer.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -36,7 +36,6 @@ class ResourceImporter; class ResourceFormatImporter : public ResourceFormatLoader { - struct PathAndType { String path; String type; @@ -71,7 +70,6 @@ public: virtual String get_import_group_file(const String &p_path) const; virtual bool exists(const String &p_path) const; - virtual bool can_be_imported(const String &p_path) const; virtual int get_import_order(const String &p_path) const; String get_internal_resource_path(const String &p_path) const; @@ -93,7 +91,6 @@ public: }; class ResourceImporter : public Reference { - GDCLASS(ResourceImporter, Reference); public: @@ -104,6 +101,7 @@ public: virtual String get_resource_type() const = 0; virtual float get_priority() const { return 1.0; } virtual int get_import_order() const { return 0; } + virtual int get_format_version() const { return 0; } struct ImportOption { PropertyInfo option; diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index d90802d7e2..821e468aee 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,14 +30,13 @@ #include "resource_loader.h" +#include "core/config/project_settings.h" #include "core/io/resource_importer.h" #include "core/os/file_access.h" #include "core/os/os.h" -#include "core/path_remap.h" -#include "core/print_string.h" -#include "core/project_settings.h" -#include "core/translation.h" -#include "core/variant_parser.h" +#include "core/string/print_string.h" +#include "core/string/translation.h" +#include "core/variant/variant_parser.h" #ifdef DEBUG_LOAD_THREADED #define print_lt(m_text) print_line(m_text) @@ -50,7 +49,6 @@ Ref<ResourceFormatLoader> ResourceLoader::loader[ResourceLoader::MAX_LOADERS]; int ResourceLoader::loader_count = 0; bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_for_type) const { - String extension = p_path.get_extension(); List<String> extensions; @@ -61,16 +59,15 @@ bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_ } for (List<String>::Element *E = extensions.front(); E; E = E->next()) { - - if (E->get().nocasecmp_to(extension) == 0) + if (E->get().nocasecmp_to(extension) == 0) { return true; + } } return false; } bool ResourceFormatLoader::handles_type(const String &p_type) const { - if (get_script_instance() && get_script_instance()->has_method("handles_type")) { // I guess custom loaders for custom resources should use "Resource" return get_script_instance()->call("handles_type", p_type); @@ -80,7 +77,6 @@ bool ResourceFormatLoader::handles_type(const String &p_type) const { } String ResourceFormatLoader::get_resource_type(const String &p_path) const { - if (get_script_instance() && get_script_instance()->has_method("get_resource_type")) { return get_script_instance()->call("get_resource_type", p_path); } @@ -89,13 +85,12 @@ String ResourceFormatLoader::get_resource_type(const String &p_path) const { } void ResourceFormatLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const { - - if (p_type == "" || handles_type(p_type)) + if (p_type == "" || handles_type(p_type)) { get_recognized_extensions(p_extensions); + } } void ResourceLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) { - for (int i = 0; i < loader_count; i++) { loader[i]->get_recognized_extensions_for_type(p_type, p_extensions); } @@ -106,7 +101,6 @@ bool ResourceFormatLoader::exists(const String &p_path) const { } void ResourceFormatLoader::get_recognized_extensions(List<String> *p_extensions) const { - if (get_script_instance() && get_script_instance()->has_method("get_recognized_extensions")) { PackedStringArray exts = get_script_instance()->call("get_recognized_extensions"); @@ -120,19 +114,18 @@ void ResourceFormatLoader::get_recognized_extensions(List<String> *p_extensions) } RES ResourceFormatLoader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) { - if (get_script_instance() && get_script_instance()->has_method("load")) { Variant res = get_script_instance()->call("load", p_path, p_original_path, p_use_sub_threads); if (res.get_type() == Variant::INT) { - - if (r_error) + if (r_error) { *r_error = (Error)res.operator int64_t(); + } } else { - - if (r_error) + if (r_error) { *r_error = OK; + } return res; } @@ -143,7 +136,6 @@ RES ResourceFormatLoader::load(const String &p_path, const String &p_original_pa } void ResourceFormatLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) { - if (get_script_instance() && get_script_instance()->has_method("get_dependencies")) { PackedStringArray deps = get_script_instance()->call("get_dependencies", p_path, p_add_types); @@ -157,9 +149,7 @@ void ResourceFormatLoader::get_dependencies(const String &p_path, List<String> * } Error ResourceFormatLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) { - if (get_script_instance() && get_script_instance()->has_method("rename_dependencies")) { - Dictionary deps_dict; for (Map<String, String>::Element *E = p_map.front(); E; E = E->next()) { deps_dict[E->key()] = E->value(); @@ -173,7 +163,6 @@ Error ResourceFormatLoader::rename_dependencies(const String &p_path, const Map< } void ResourceFormatLoader::_bind_methods() { - { MethodInfo info = MethodInfo(Variant::NIL, "load", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "original_path")); info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; @@ -190,12 +179,10 @@ void ResourceFormatLoader::_bind_methods() { /////////////////////////////////// RES ResourceLoader::_load(const String &p_path, const String &p_original_path, const String &p_type_hint, bool p_no_cache, Error *r_error, bool p_use_sub_threads, float *r_progress) { - 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(p_path, p_type_hint)) { continue; } @@ -208,7 +195,8 @@ RES ResourceLoader::_load(const String &p_path, const String &p_original_path, c return res; } - ERR_FAIL_COND_V_MSG(found, RES(), "Failed loading resource: " + p_path + "."); + ERR_FAIL_COND_V_MSG(found, RES(), + vformat("Failed loading resource: %s. Make sure resources have been imported by opening the project in the editor at least once.", p_path)); #ifdef TOOLS_ENABLED FileAccessRef file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES); @@ -219,7 +207,6 @@ RES ResourceLoader::_load(const String &p_path, const String &p_original_path, c } void ResourceLoader::_thread_load_function(void *p_userdata) { - ThreadLoadTask &load_task = *(ThreadLoadTask *)p_userdata; load_task.loader_id = Thread::get_caller_id(); @@ -238,7 +225,6 @@ void ResourceLoader::_thread_load_function(void *p_userdata) { load_task.status = THREAD_LOAD_LOADED; } if (load_task.semaphore) { - if (load_task.start_next && thread_waiting_count > 0) { thread_waiting_count--; //thread loading count remains constant, this ends but another one begins @@ -259,8 +245,9 @@ void ResourceLoader::_thread_load_function(void *p_userdata) { if (load_task.resource.is_valid()) { load_task.resource->set_path(load_task.local_path); - if (load_task.xl_remapped) + if (load_task.xl_remapped) { load_task.resource->set_as_translation_remapped(true); + } #ifdef TOOLS_ENABLED @@ -279,13 +266,14 @@ void ResourceLoader::_thread_load_function(void *p_userdata) { thread_load_mutex->unlock(); } -Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, const String &p_source_resource) { +Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, const String &p_source_resource) { String local_path; - if (p_path.is_rel_path()) + if (p_path.is_rel_path()) { local_path = "res://" + p_path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(p_path); + } thread_load_mutex->lock(); @@ -388,7 +376,6 @@ Error ResourceLoader::load_threaded_request(const String &p_path, const String & } float ResourceLoader::_dependency_get_progress(const String &p_path) { - if (thread_load_tasks.has(p_path)) { ThreadLoadTask &load_task = thread_load_tasks[p_path]; int dep_count = load_task.sub_tasks.size(); @@ -411,12 +398,12 @@ float ResourceLoader::_dependency_get_progress(const String &p_path) { } ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const String &p_path, float *r_progress) { - String local_path; - if (p_path.is_rel_path()) + if (p_path.is_rel_path()) { local_path = "res://" + p_path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(p_path); + } thread_load_mutex->lock(); if (!thread_load_tasks.has(local_path)) { @@ -434,13 +421,14 @@ ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const return status; } -RES ResourceLoader::load_threaded_get(const String &p_path, Error *r_error) { +RES ResourceLoader::load_threaded_get(const String &p_path, Error *r_error) { String local_path; - if (p_path.is_rel_path()) + if (p_path.is_rel_path()) { local_path = "res://" + p_path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(p_path); + } thread_load_mutex->lock(); if (!thread_load_tasks.has(local_path)) { @@ -517,18 +505,18 @@ RES ResourceLoader::load_threaded_get(const String &p_path, Error *r_error) { } RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) { - - if (r_error) + if (r_error) { *r_error = ERR_CANT_OPEN; + } String local_path; - if (p_path.is_rel_path()) + if (p_path.is_rel_path()) { local_path = "res://" + p_path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(p_path); + } if (!p_no_cache) { - thread_load_mutex->lock(); //Is it already being loaded? poll until done @@ -593,7 +581,6 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p return load_threaded_get(p_path, r_error); } else { - bool xl_remapped = false; String path = _path_remap(local_path, &xl_remapped); @@ -610,8 +597,9 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p return RES(); } - if (xl_remapped) + if (xl_remapped) { res->set_as_translation_remapped(true); + } #ifdef TOOLS_ENABLED @@ -628,15 +616,14 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p } bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) { - String local_path; - if (p_path.is_rel_path()) + if (p_path.is_rel_path()) { local_path = "res://" + p_path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(p_path); + } if (ResourceCache::has(local_path)) { - return true; // If cached, it probably exists } @@ -645,20 +632,19 @@ bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) { // 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(path, p_type_hint)) { continue; } - if (loader[i]->exists(path)) + if (loader[i]->exists(path)) { return true; + } } return false; } void ResourceLoader::add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front) { - ERR_FAIL_COND(p_format_loader.is_null()); ERR_FAIL_COND(loader_count >= MAX_LOADERS); @@ -674,14 +660,14 @@ void ResourceLoader::add_resource_format_loader(Ref<ResourceFormatLoader> p_form } void ResourceLoader::remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader) { - ERR_FAIL_COND(p_format_loader.is_null()); // Find loader int i = 0; for (; i < loader_count; ++i) { - if (loader[i] == p_format_loader) + if (loader[i] == p_format_loader) { break; + } } ERR_FAIL_COND(i >= loader_count); // Not found @@ -695,19 +681,19 @@ void ResourceLoader::remove_resource_format_loader(Ref<ResourceFormatLoader> p_f } int ResourceLoader::get_import_order(const String &p_path) { - String path = _path_remap(p_path); String local_path; - if (path.is_rel_path()) + if (path.is_rel_path()) { local_path = "res://" + path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(path); + } for (int i = 0; i < loader_count; i++) { - - if (!loader[i]->recognize_path(local_path)) + if (!loader[i]->recognize_path(local_path)) { continue; + } /* if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint)) continue; @@ -723,15 +709,16 @@ String ResourceLoader::get_import_group_file(const String &p_path) { String path = _path_remap(p_path); String local_path; - if (path.is_rel_path()) + if (path.is_rel_path()) { local_path = "res://" + path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(path); + } for (int i = 0; i < loader_count; i++) { - - if (!loader[i]->recognize_path(local_path)) + if (!loader[i]->recognize_path(local_path)) { continue; + } /* if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint)) continue; @@ -744,19 +731,19 @@ String ResourceLoader::get_import_group_file(const String &p_path) { } bool ResourceLoader::is_import_valid(const String &p_path) { - String path = _path_remap(p_path); String local_path; - if (path.is_rel_path()) + if (path.is_rel_path()) { local_path = "res://" + path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(path); + } for (int i = 0; i < loader_count; i++) { - - if (!loader[i]->recognize_path(local_path)) + if (!loader[i]->recognize_path(local_path)) { continue; + } /* if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint)) continue; @@ -769,19 +756,19 @@ bool ResourceLoader::is_import_valid(const String &p_path) { } bool ResourceLoader::is_imported(const String &p_path) { - String path = _path_remap(p_path); String local_path; - if (path.is_rel_path()) + if (path.is_rel_path()) { local_path = "res://" + path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(path); + } for (int i = 0; i < loader_count; i++) { - - if (!loader[i]->recognize_path(local_path)) + if (!loader[i]->recognize_path(local_path)) { continue; + } /* if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint)) continue; @@ -794,19 +781,19 @@ bool ResourceLoader::is_imported(const String &p_path) { } 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 (path.is_rel_path()) + if (path.is_rel_path()) { local_path = "res://" + path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(path); + } for (int i = 0; i < loader_count; i++) { - - if (!loader[i]->recognize_path(local_path)) + if (!loader[i]->recognize_path(local_path)) { continue; + } /* if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint)) continue; @@ -817,19 +804,19 @@ 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 (path.is_rel_path()) + if (path.is_rel_path()) { local_path = "res://" + path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(path); + } for (int i = 0; i < loader_count; i++) { - - if (!loader[i]->recognize_path(local_path)) + if (!loader[i]->recognize_path(local_path)) { continue; + } /* if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint)) continue; @@ -842,15 +829,14 @@ Error ResourceLoader::rename_dependencies(const String &p_path, const Map<String } String ResourceLoader::get_resource_type(const String &p_path) { - String local_path; - if (p_path.is_rel_path()) + if (p_path.is_rel_path()) { local_path = "res://" + p_path; - else + } else { local_path = ProjectSettings::get_singleton()->localize_path(p_path); + } for (int i = 0; i < loader_count; i++) { - String result = loader[i]->get_resource_type(local_path); if (result != "") { return result; @@ -861,7 +847,6 @@ String ResourceLoader::get_resource_type(const String &p_path) { } String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_remapped) { - String new_path = p_path; if (translation_remaps.has(p_path)) { @@ -881,7 +866,7 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem bool near_match = false; for (int i = 0; i < res_remaps.size(); i++) { - int split = res_remaps[i].find_last(":"); + int split = res_remaps[i].rfind(":"); if (split == -1) { continue; } @@ -920,7 +905,6 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem FileAccess *f = FileAccess::open(p_path + ".remap", FileAccess::READ, &err); if (f) { - VariantParser::StreamFile stream; stream.f = f; @@ -931,7 +915,6 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem int lines = 0; String error_text; while (true) { - assign = Variant(); next_tag.fields.clear(); next_tag.name = String(); @@ -960,9 +943,7 @@ String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_rem } 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); } @@ -974,7 +955,6 @@ String ResourceLoader::path_remap(const String &p_path) { } void ResourceLoader::reload_translation_remaps() { - if (ResourceCache::lock) { ResourceCache::lock->read_lock(); } @@ -999,15 +979,14 @@ void ResourceLoader::reload_translation_remaps() { } void ResourceLoader::load_translation_remaps() { - - if (!ProjectSettings::get_singleton()->has_setting("locale/translation_remaps")) + if (!ProjectSettings::get_singleton()->has_setting("locale/translation_remaps")) { return; + } Dictionary remaps = ProjectSettings::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()); @@ -1021,12 +1000,15 @@ void ResourceLoader::load_translation_remaps() { void ResourceLoader::clear_translation_remaps() { translation_remaps.clear(); + while (remapped_list.first() != nullptr) { + remapped_list.remove(remapped_list.first()); + } } void ResourceLoader::load_path_remaps() { - - if (!ProjectSettings::get_singleton()->has_setting("path_remap/remapped_paths")) + if (!ProjectSettings::get_singleton()->has_setting("path_remap/remapped_paths")) { return; + } Vector<String> remaps = ProjectSettings::get_singleton()->get("path_remap/remapped_paths"); int rc = remaps.size(); @@ -1034,13 +1016,11 @@ void ResourceLoader::load_path_remaps() { const String *r = remaps.ptr(); for (int i = 0; i < rc; i += 2) { - path_remaps[r[i]] = r[i + 1]; } } void ResourceLoader::clear_path_remaps() { - path_remaps.clear(); } @@ -1060,9 +1040,9 @@ Ref<ResourceFormatLoader> ResourceLoader::_find_custom_resource_format_loader(St } bool ResourceLoader::add_custom_resource_format_loader(String script_path) { - - if (_find_custom_resource_format_loader(script_path).is_valid()) + if (_find_custom_resource_format_loader(script_path).is_valid()) { return false; + } Ref<Resource> res = ResourceLoader::load(script_path); ERR_FAIL_COND_V(res.is_null(), false); @@ -1077,7 +1057,7 @@ bool ResourceLoader::add_custom_resource_format_loader(String script_path) { ERR_FAIL_COND_V_MSG(obj == nullptr, false, "Cannot instance script as custom resource loader, expected 'ResourceFormatLoader' inheritance, got: " + String(ibt) + "."); - ResourceFormatLoader *crl = Object::cast_to<ResourceFormatLoader>(obj); + Ref<ResourceFormatLoader> crl = Object::cast_to<ResourceFormatLoader>(obj); crl->set_script(s); ResourceLoader::add_resource_format_loader(crl); @@ -1085,10 +1065,10 @@ bool ResourceLoader::add_custom_resource_format_loader(String script_path) { } void ResourceLoader::remove_custom_resource_format_loader(String script_path) { - Ref<ResourceFormatLoader> custom_loader = _find_custom_resource_format_loader(script_path); - if (custom_loader.is_valid()) + if (custom_loader.is_valid()) { remove_resource_format_loader(custom_loader); + } } void ResourceLoader::add_custom_loaders() { @@ -1100,7 +1080,6 @@ void ResourceLoader::add_custom_loaders() { ScriptServer::get_global_class_list(&global_classes); for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) { - StringName class_name = E->get(); StringName base_class = ScriptServer::get_global_class_native_base(class_name); @@ -1112,7 +1091,6 @@ void ResourceLoader::add_custom_loaders() { } void ResourceLoader::remove_custom_loaders() { - Vector<Ref<ResourceFormatLoader>> custom_loaders; for (int i = 0; i < loader_count; ++i) { if (loader[i]->get_script_instance()) { @@ -1135,7 +1113,6 @@ void ResourceLoader::initialize() { } void ResourceLoader::finalize() { - memdelete(thread_load_mutex); memdelete(thread_load_semaphore); } diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index 2d95d5545f..dbf6be46c5 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,12 +31,11 @@ #ifndef RESOURCE_LOADER_H #define RESOURCE_LOADER_H +#include "core/io/resource.h" #include "core/os/semaphore.h" #include "core/os/thread.h" -#include "core/resource.h" class ResourceFormatLoader : public Reference { - GDCLASS(ResourceFormatLoader, Reference); protected: @@ -67,7 +66,6 @@ typedef Error (*ResourceLoaderImport)(const String &p_path); typedef void (*ResourceLoadedCallback)(RES p_resource, const String &p_path); class ResourceLoader { - enum { MAX_LOADERS = 64 }; @@ -160,7 +158,9 @@ public: static bool get_timestamp_on_load() { return timestamp_on_load; } static void notify_load_error(const String &p_err) { - if (err_notify) err_notify(err_notify_ud, p_err); + if (err_notify) { + err_notify(err_notify_ud, p_err); + } } static void set_error_notify_func(void *p_ud, ResourceLoadErrorNotify p_err_notify) { err_notify = p_err_notify; @@ -168,7 +168,9 @@ public: } static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) { - if (dep_err_notify) dep_err_notify(dep_err_notify_ud, p_path, p_dependency, p_type); + if (dep_err_notify) { + dep_err_notify(dep_err_notify_ud, p_path, p_dependency, p_type); + } } static void set_dependency_error_notify_func(void *p_ud, DependencyErrorNotify p_err_notify) { dep_err_notify = p_err_notify; diff --git a/core/io/resource_saver.cpp b/core/io/resource_saver.cpp index 09128adb50..7ebc7f34b3 100644 --- a/core/io/resource_saver.cpp +++ b/core/io/resource_saver.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -29,10 +29,10 @@ /*************************************************************************/ #include "resource_saver.h" +#include "core/config/project_settings.h" #include "core/io/resource_loader.h" +#include "core/object/script_language.h" #include "core/os/file_access.h" -#include "core/project_settings.h" -#include "core/script_language.h" Ref<ResourceFormatSaver> ResourceSaver::saver[MAX_SAVERS]; @@ -41,7 +41,6 @@ bool ResourceSaver::timestamp_on_save = false; ResourceSavedCallback ResourceSaver::save_callback = nullptr; Error ResourceFormatSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) { - if (get_script_instance() && get_script_instance()->has_method("save")) { return (Error)get_script_instance()->call("save", p_path, p_resource, p_flags).operator int64_t(); } @@ -50,7 +49,6 @@ Error ResourceFormatSaver::save(const String &p_path, const RES &p_resource, uin } bool ResourceFormatSaver::recognize(const RES &p_resource) const { - if (get_script_instance() && get_script_instance()->has_method("recognize")) { return get_script_instance()->call("recognize", p_resource); } @@ -59,7 +57,6 @@ bool ResourceFormatSaver::recognize(const RES &p_resource) const { } void ResourceFormatSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const { - if (get_script_instance() && get_script_instance()->has_method("get_recognized_extensions")) { PackedStringArray exts = get_script_instance()->call("get_recognized_extensions", p_resource); @@ -73,7 +70,6 @@ void ResourceFormatSaver::get_recognized_extensions(const RES &p_resource, List< } void ResourceFormatSaver::_bind_methods() { - { PropertyInfo arg0 = PropertyInfo(Variant::STRING, "path"); PropertyInfo arg1 = PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"); @@ -86,40 +82,40 @@ void ResourceFormatSaver::_bind_methods() { } Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t p_flags) { - String extension = p_path.get_extension(); Error err = ERR_FILE_UNRECOGNIZED; for (int i = 0; i < saver_count; i++) { - - if (!saver[i]->recognize(p_resource)) + if (!saver[i]->recognize(p_resource)) { continue; + } List<String> extensions; bool recognized = false; saver[i]->get_recognized_extensions(p_resource, &extensions); for (List<String>::Element *E = extensions.front(); E; E = E->next()) { - - if (E->get().nocasecmp_to(extension) == 0) + if (E->get().nocasecmp_to(extension) == 0) { recognized = true; + } } - if (!recognized) + if (!recognized) { continue; + } String old_path = p_resource->get_path(); String local_path = ProjectSettings::get_singleton()->localize_path(p_path); RES rwcopy = p_resource; - if (p_flags & FLAG_CHANGE_PATH) + if (p_flags & FLAG_CHANGE_PATH) { rwcopy->set_path(local_path); + } err = saver[i]->save(p_path, p_resource, p_flags); if (err == OK) { - #ifdef TOOLS_ENABLED ((Resource *)p_resource.ptr())->set_edited(false); @@ -130,11 +126,13 @@ Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t } #endif - if (p_flags & FLAG_CHANGE_PATH) + if (p_flags & FLAG_CHANGE_PATH) { rwcopy->set_path(old_path); + } - if (save_callback && p_path.begins_with("res://")) + if (save_callback && p_path.begins_with("res://")) { save_callback(p_resource, p_path); + } return OK; } @@ -144,20 +142,16 @@ Error ResourceSaver::save(const String &p_path, const RES &p_resource, uint32_t } void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) { - save_callback = p_callback; } void ResourceSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) { - for (int i = 0; i < saver_count; i++) { - saver[i]->get_recognized_extensions(p_resource, p_extensions); } } void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver, bool p_at_front) { - ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object."); ERR_FAIL_COND(saver_count >= MAX_SAVERS); @@ -173,14 +167,14 @@ void ResourceSaver::add_resource_format_saver(Ref<ResourceFormatSaver> p_format_ } void ResourceSaver::remove_resource_format_saver(Ref<ResourceFormatSaver> p_format_saver) { - ERR_FAIL_COND_MSG(p_format_saver.is_null(), "It's not a reference to a valid ResourceFormatSaver object."); // Find saver int i = 0; for (; i < saver_count; ++i) { - if (saver[i] == p_format_saver) + if (saver[i] == p_format_saver) { break; + } } ERR_FAIL_COND(i >= saver_count); // Not found @@ -203,9 +197,9 @@ Ref<ResourceFormatSaver> ResourceSaver::_find_custom_resource_format_saver(Strin } bool ResourceSaver::add_custom_resource_format_saver(String script_path) { - - if (_find_custom_resource_format_saver(script_path).is_valid()) + if (_find_custom_resource_format_saver(script_path).is_valid()) { return false; + } Ref<Resource> res = ResourceLoader::load(script_path); ERR_FAIL_COND_V(res.is_null(), false); @@ -220,7 +214,7 @@ bool ResourceSaver::add_custom_resource_format_saver(String script_path) { ERR_FAIL_COND_V_MSG(obj == nullptr, false, "Cannot instance script as custom resource saver, expected 'ResourceFormatSaver' inheritance, got: " + String(ibt) + "."); - ResourceFormatSaver *crl = Object::cast_to<ResourceFormatSaver>(obj); + Ref<ResourceFormatSaver> crl = Object::cast_to<ResourceFormatSaver>(obj); crl->set_script(s); ResourceSaver::add_resource_format_saver(crl); @@ -228,10 +222,10 @@ bool ResourceSaver::add_custom_resource_format_saver(String script_path) { } void ResourceSaver::remove_custom_resource_format_saver(String script_path) { - Ref<ResourceFormatSaver> custom_saver = _find_custom_resource_format_saver(script_path); - if (custom_saver.is_valid()) + if (custom_saver.is_valid()) { remove_resource_format_saver(custom_saver); + } } void ResourceSaver::add_custom_savers() { @@ -243,7 +237,6 @@ void ResourceSaver::add_custom_savers() { ScriptServer::get_global_class_list(&global_classes); for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) { - StringName class_name = E->get(); StringName base_class = ScriptServer::get_global_class_native_base(class_name); @@ -255,7 +248,6 @@ void ResourceSaver::add_custom_savers() { } void ResourceSaver::remove_custom_savers() { - Vector<Ref<ResourceFormatSaver>> custom_savers; for (int i = 0; i < saver_count; ++i) { if (saver[i]->get_script_instance()) { diff --git a/core/io/resource_saver.h b/core/io/resource_saver.h index 2ddebf0581..2c9e8f1aa3 100644 --- a/core/io/resource_saver.h +++ b/core/io/resource_saver.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,7 +31,7 @@ #ifndef RESOURCE_SAVER_H #define RESOURCE_SAVER_H -#include "core/resource.h" +#include "core/io/resource.h" class ResourceFormatSaver : public Reference { GDCLASS(ResourceFormatSaver, Reference); @@ -50,7 +50,6 @@ public: typedef void (*ResourceSavedCallback)(Ref<Resource> p_resource, const String &p_path); class ResourceSaver { - enum { MAX_SAVERS = 64 }; @@ -64,7 +63,6 @@ class ResourceSaver { public: enum SaverFlags { - FLAG_RELATIVE_PATHS = 1, FLAG_BUNDLE_RESOURCES = 2, FLAG_CHANGE_PATH = 4, diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index b28b17aa95..8407d55196 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,16 +33,15 @@ #include "core/io/marshalls.h" Error StreamPeer::_put_data(const Vector<uint8_t> &p_data) { - int len = p_data.size(); - if (len == 0) + if (len == 0) { return OK; + } const uint8_t *r = p_data.ptr(); return put_data(&r[0], len); } Array StreamPeer::_put_partial_data(const Vector<uint8_t> &p_data) { - Array ret; int len = p_data.size(); @@ -65,13 +64,11 @@ Array StreamPeer::_put_partial_data(const Vector<uint8_t> &p_data) { } Array StreamPeer::_get_data(int p_bytes) { - Array ret; Vector<uint8_t> data; data.resize(p_bytes); if (data.size() != p_bytes) { - ret.push_back(ERR_OUT_OF_MEMORY); ret.push_back(Vector<uint8_t>()); return ret; @@ -86,13 +83,11 @@ Array StreamPeer::_get_data(int p_bytes) { } Array StreamPeer::_get_partial_data(int p_bytes) { - Array ret; Vector<uint8_t> data; data.resize(p_bytes); if (data.size() != p_bytes) { - ret.push_back(ERR_OUT_OF_MEMORY); ret.push_back(Vector<uint8_t>()); return ret; @@ -105,7 +100,6 @@ Array StreamPeer::_get_partial_data(int p_bytes) { if (err != OK) { data.resize(0); } else if (received != data.size()) { - data.resize(received); } @@ -115,12 +109,10 @@ Array StreamPeer::_get_partial_data(int p_bytes) { } void StreamPeer::set_big_endian(bool p_enable) { - big_endian = p_enable; } bool StreamPeer::is_big_endian_enabled() const { - return big_endian; } @@ -129,11 +121,10 @@ void StreamPeer::put_u8(uint8_t p_val) { } void StreamPeer::put_8(int8_t p_val) { - put_data((const uint8_t *)&p_val, 1); } -void StreamPeer::put_u16(uint16_t p_val) { +void StreamPeer::put_u16(uint16_t p_val) { if (big_endian) { p_val = BSWAP16(p_val); } @@ -141,8 +132,8 @@ void StreamPeer::put_u16(uint16_t p_val) { encode_uint16(p_val, buf); put_data(buf, 2); } -void StreamPeer::put_16(int16_t p_val) { +void StreamPeer::put_16(int16_t p_val) { if (big_endian) { p_val = BSWAP16(p_val); } @@ -150,8 +141,8 @@ void StreamPeer::put_16(int16_t p_val) { encode_uint16(p_val, buf); put_data(buf, 2); } -void StreamPeer::put_u32(uint32_t p_val) { +void StreamPeer::put_u32(uint32_t p_val) { if (big_endian) { p_val = BSWAP32(p_val); } @@ -159,8 +150,8 @@ void StreamPeer::put_u32(uint32_t p_val) { encode_uint32(p_val, buf); put_data(buf, 4); } -void StreamPeer::put_32(int32_t p_val) { +void StreamPeer::put_32(int32_t p_val) { if (big_endian) { p_val = BSWAP32(p_val); } @@ -168,8 +159,8 @@ void StreamPeer::put_32(int32_t p_val) { encode_uint32(p_val, buf); put_data(buf, 4); } -void StreamPeer::put_u64(uint64_t p_val) { +void StreamPeer::put_u64(uint64_t p_val) { if (big_endian) { p_val = BSWAP64(p_val); } @@ -177,8 +168,8 @@ void StreamPeer::put_u64(uint64_t p_val) { encode_uint64(p_val, buf); put_data(buf, 8); } -void StreamPeer::put_64(int64_t p_val) { +void StreamPeer::put_64(int64_t p_val) { if (big_endian) { p_val = BSWAP64(p_val); } @@ -186,8 +177,8 @@ void StreamPeer::put_64(int64_t p_val) { encode_uint64(p_val, buf); put_data(buf, 8); } -void StreamPeer::put_float(float p_val) { +void StreamPeer::put_float(float p_val) { uint8_t buf[4]; encode_float(p_val, buf); @@ -198,8 +189,8 @@ void StreamPeer::put_float(float p_val) { put_data(buf, 4); } -void StreamPeer::put_double(double p_val) { +void StreamPeer::put_double(double p_val) { uint8_t buf[8]; encode_double(p_val, buf); if (big_endian) { @@ -208,20 +199,20 @@ void StreamPeer::put_double(double p_val) { } put_data(buf, 8); } -void StreamPeer::put_string(const String &p_string) { +void StreamPeer::put_string(const String &p_string) { CharString cs = p_string.ascii(); put_u32(cs.length()); put_data((const uint8_t *)cs.get_data(), cs.length()); } -void StreamPeer::put_utf8_string(const String &p_string) { +void StreamPeer::put_utf8_string(const String &p_string) { CharString cs = p_string.utf8(); put_u32(cs.length()); put_data((const uint8_t *)cs.get_data(), cs.length()); } -void StreamPeer::put_var(const Variant &p_variant, bool p_full_objects) { +void StreamPeer::put_var(const Variant &p_variant, bool p_full_objects) { int len = 0; Vector<uint8_t> buf; encode_variant(p_variant, nullptr, len, p_full_objects); @@ -232,19 +223,18 @@ void StreamPeer::put_var(const Variant &p_variant, bool p_full_objects) { } uint8_t StreamPeer::get_u8() { - uint8_t buf[1]; get_data(buf, 1); return buf[0]; } -int8_t StreamPeer::get_8() { +int8_t StreamPeer::get_8() { uint8_t buf[1]; get_data(buf, 1); return buf[0]; } -uint16_t StreamPeer::get_u16() { +uint16_t StreamPeer::get_u16() { uint8_t buf[2]; get_data(buf, 2); uint16_t r = decode_uint16(buf); @@ -253,8 +243,8 @@ uint16_t StreamPeer::get_u16() { } return r; } -int16_t StreamPeer::get_16() { +int16_t StreamPeer::get_16() { uint8_t buf[2]; get_data(buf, 2); uint16_t r = decode_uint16(buf); @@ -263,8 +253,8 @@ int16_t StreamPeer::get_16() { } return r; } -uint32_t StreamPeer::get_u32() { +uint32_t StreamPeer::get_u32() { uint8_t buf[4]; get_data(buf, 4); uint32_t r = decode_uint32(buf); @@ -273,8 +263,8 @@ uint32_t StreamPeer::get_u32() { } return r; } -int32_t StreamPeer::get_32() { +int32_t StreamPeer::get_32() { uint8_t buf[4]; get_data(buf, 4); uint32_t r = decode_uint32(buf); @@ -283,8 +273,8 @@ int32_t StreamPeer::get_32() { } return r; } -uint64_t StreamPeer::get_u64() { +uint64_t StreamPeer::get_u64() { uint8_t buf[8]; get_data(buf, 8); uint64_t r = decode_uint64(buf); @@ -293,8 +283,8 @@ uint64_t StreamPeer::get_u64() { } return r; } -int64_t StreamPeer::get_64() { +int64_t StreamPeer::get_64() { uint8_t buf[8]; get_data(buf, 8); uint64_t r = decode_uint64(buf); @@ -303,8 +293,8 @@ int64_t StreamPeer::get_64() { } return r; } -float StreamPeer::get_float() { +float StreamPeer::get_float() { uint8_t buf[4]; get_data(buf, 4); @@ -317,7 +307,6 @@ float StreamPeer::get_float() { } double StreamPeer::get_double() { - uint8_t buf[8]; get_data(buf, 8); @@ -328,10 +317,11 @@ double StreamPeer::get_double() { return decode_double(buf); } -String StreamPeer::get_string(int p_bytes) { - if (p_bytes < 0) +String StreamPeer::get_string(int p_bytes) { + if (p_bytes < 0) { p_bytes = get_u32(); + } ERR_FAIL_COND_V(p_bytes < 0, String()); Vector<char> buf; @@ -342,10 +332,11 @@ String StreamPeer::get_string(int p_bytes) { buf.write[p_bytes] = 0; return buf.ptr(); } -String StreamPeer::get_utf8_string(int p_bytes) { - if (p_bytes < 0) +String StreamPeer::get_utf8_string(int p_bytes) { + if (p_bytes < 0) { p_bytes = get_u32(); + } ERR_FAIL_COND_V(p_bytes < 0, String()); Vector<uint8_t> buf; @@ -358,8 +349,8 @@ String StreamPeer::get_utf8_string(int p_bytes) { ret.parse_utf8((const char *)buf.ptr(), buf.size()); return ret; } -Variant StreamPeer::get_var(bool p_allow_objects) { +Variant StreamPeer::get_var(bool p_allow_objects) { int len = get_32(); Vector<uint8_t> var; Error err = var.resize(len); @@ -375,7 +366,6 @@ Variant StreamPeer::get_var(bool p_allow_objects) { } void StreamPeer::_bind_methods() { - ClassDB::bind_method(D_METHOD("put_data", "data"), &StreamPeer::_put_data); ClassDB::bind_method(D_METHOD("put_partial_data", "data"), &StreamPeer::_put_partial_data); @@ -417,10 +407,10 @@ void StreamPeer::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::BOOL, "big_endian"), "set_big_endian", "is_big_endian_enabled"); } + //////////////////////////////// void StreamPeerBuffer::_bind_methods() { - ClassDB::bind_method(D_METHOD("seek", "position"), &StreamPeerBuffer::seek); ClassDB::bind_method(D_METHOD("get_size"), &StreamPeerBuffer::get_size); ClassDB::bind_method(D_METHOD("get_position"), &StreamPeerBuffer::get_position); @@ -434,9 +424,9 @@ void StreamPeerBuffer::_bind_methods() { } Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) { - - if (p_bytes <= 0) + if (p_bytes <= 0) { return OK; + } if (pointer + p_bytes > data.size()) { data.resize(pointer + p_bytes); @@ -450,23 +440,21 @@ Error StreamPeerBuffer::put_data(const uint8_t *p_data, int p_bytes) { } Error StreamPeerBuffer::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { - r_sent = p_bytes; return put_data(p_data, p_bytes); } Error StreamPeerBuffer::get_data(uint8_t *p_buffer, int p_bytes) { - int recv; get_partial_data(p_buffer, p_bytes, recv); - if (recv != p_bytes) + if (recv != p_bytes) { return ERR_INVALID_PARAMETER; + } return OK; } Error StreamPeerBuffer::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) { - if (pointer + p_bytes > data.size()) { r_received = data.size() - pointer; if (r_received <= 0) { @@ -487,57 +475,44 @@ Error StreamPeerBuffer::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_ } int StreamPeerBuffer::get_available_bytes() const { - return data.size() - pointer; } void StreamPeerBuffer::seek(int p_pos) { - ERR_FAIL_COND(p_pos < 0); ERR_FAIL_COND(p_pos > data.size()); pointer = p_pos; } -int StreamPeerBuffer::get_size() const { +int StreamPeerBuffer::get_size() const { return data.size(); } int StreamPeerBuffer::get_position() const { - return pointer; } void StreamPeerBuffer::resize(int p_size) { - data.resize(p_size); } void StreamPeerBuffer::set_data_array(const Vector<uint8_t> &p_data) { - data = p_data; pointer = 0; } Vector<uint8_t> StreamPeerBuffer::get_data_array() const { - return data; } void StreamPeerBuffer::clear() { - data.resize(0); pointer = 0; } Ref<StreamPeerBuffer> StreamPeerBuffer::duplicate() const { - Ref<StreamPeerBuffer> spb; spb.instance(); spb->data = data; return spb; } - -StreamPeerBuffer::StreamPeerBuffer() { - - pointer = 0; -} diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h index 9358a2c07c..dadedbd2dc 100644 --- a/core/io/stream_peer.h +++ b/core/io/stream_peer.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,7 +31,7 @@ #ifndef STREAM_PEER_H #define STREAM_PEER_H -#include "core/reference.h" +#include "core/object/reference.h" class StreamPeer : public Reference { GDCLASS(StreamPeer, Reference); @@ -47,7 +47,7 @@ protected: Array _get_data(int p_bytes); Array _get_partial_data(int p_bytes); - bool big_endian; + bool big_endian = false; public: virtual Error put_data(const uint8_t *p_data, int p_bytes) = 0; ///< put a whole chunk of data, blocking until it sent @@ -89,27 +89,26 @@ public: String get_utf8_string(int p_bytes = -1); Variant get_var(bool p_allow_objects = false); - StreamPeer() { big_endian = false; } + StreamPeer() {} }; class StreamPeerBuffer : public StreamPeer { - GDCLASS(StreamPeerBuffer, StreamPeer); Vector<uint8_t> data; - int pointer; + int pointer = 0; protected: static void _bind_methods(); public: - Error put_data(const uint8_t *p_data, int p_bytes); - Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent); + Error put_data(const uint8_t *p_data, int p_bytes) override; + Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) override; - Error get_data(uint8_t *p_buffer, int p_bytes); - Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received); + Error get_data(uint8_t *p_buffer, int p_bytes) override; + Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) override; - virtual int get_available_bytes() const; + virtual int get_available_bytes() const override; void seek(int p_pos); int get_size() const; @@ -123,7 +122,7 @@ public: Ref<StreamPeerBuffer> duplicate() const; - StreamPeerBuffer(); + StreamPeerBuffer() {} }; #endif // STREAM_PEER_H diff --git a/core/io/stream_peer_ssl.cpp b/core/io/stream_peer_ssl.cpp index d98935f77c..6f5a06474c 100644 --- a/core/io/stream_peer_ssl.cpp +++ b/core/io/stream_peer_ssl.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,14 +30,14 @@ #include "stream_peer_ssl.h" -#include "core/engine.h" +#include "core/config/engine.h" StreamPeerSSL *(*StreamPeerSSL::_create)() = nullptr; StreamPeerSSL *StreamPeerSSL::create() { - - if (_create) + if (_create) { return _create(); + } return nullptr; } @@ -56,7 +56,6 @@ bool StreamPeerSSL::is_blocking_handshake_enabled() const { } void StreamPeerSSL::_bind_methods() { - ClassDB::bind_method(D_METHOD("poll"), &StreamPeerSSL::poll); ClassDB::bind_method(D_METHOD("accept_stream", "stream", "private_key", "certificate", "chain"), &StreamPeerSSL::accept_stream, DEFVAL(Ref<X509Certificate>())); ClassDB::bind_method(D_METHOD("connect_to_stream", "stream", "validate_certs", "for_hostname", "valid_certificate"), &StreamPeerSSL::connect_to_stream, DEFVAL(false), DEFVAL(String()), DEFVAL(Ref<X509Certificate>())); @@ -73,7 +72,3 @@ void StreamPeerSSL::_bind_methods() { BIND_ENUM_CONSTANT(STATUS_ERROR); BIND_ENUM_CONSTANT(STATUS_ERROR_HOSTNAME_MISMATCH); } - -StreamPeerSSL::StreamPeerSSL() { - blocking_handshake = true; -} diff --git a/core/io/stream_peer_ssl.h b/core/io/stream_peer_ssl.h index de3cb09c60..1df9ced08c 100644 --- a/core/io/stream_peer_ssl.h +++ b/core/io/stream_peer_ssl.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -43,7 +43,7 @@ protected: static bool available; - bool blocking_handshake; + bool blocking_handshake = true; public: enum Status { @@ -68,7 +68,7 @@ public: static bool is_available(); - StreamPeerSSL(); + StreamPeerSSL() {} }; VARIANT_ENUM_CAST(StreamPeerSSL::Status); diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index f0c5816d73..760710a9eb 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,10 +30,9 @@ #include "stream_peer_tcp.h" -#include "core/project_settings.h" +#include "core/config/project_settings.h" Error StreamPeerTCP::_poll_connection() { - ERR_FAIL_COND_V(status != STATUS_CONNECTING || !_sock.is_valid() || !_sock->is_open(), FAILED); Error err = _sock->connect_to_host(peer_host, peer_port); @@ -58,7 +57,6 @@ Error StreamPeerTCP::_poll_connection() { } void StreamPeerTCP::accept_socket(Ref<NetSocket> p_sock, IP_Address p_host, uint16_t p_port) { - _sock = p_sock; _sock->set_blocking_enabled(false); @@ -70,7 +68,6 @@ void StreamPeerTCP::accept_socket(Ref<NetSocket> p_sock, IP_Address p_host, uint } Error StreamPeerTCP::connect_to_host(const IP_Address &p_host, uint16_t p_port) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(!p_host.is_valid(), ERR_INVALID_PARAMETER); @@ -103,18 +100,14 @@ Error StreamPeerTCP::connect_to_host(const IP_Address &p_host, uint16_t p_port) } Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool p_block) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); if (status == STATUS_NONE || status == STATUS_ERROR) { - return FAILED; } if (status != STATUS_CONNECTED) { - if (_poll_connection() != OK) { - return FAILED; } @@ -124,8 +117,9 @@ Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool } } - if (!_sock->is_open()) + if (!_sock->is_open()) { return FAILED; + } Error err; int data_to_send = p_bytes; @@ -133,12 +127,10 @@ Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool int total_sent = 0; while (data_to_send) { - int sent_amount = 0; err = _sock->send(offset, data_to_send, sent_amount); if (err != OK) { - if (err != ERR_BUSY) { disconnect_from_host(); return FAILED; @@ -156,7 +148,6 @@ Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool return FAILED; } } else { - data_to_send -= sent_amount; offset += sent_amount; total_sent += sent_amount; @@ -169,16 +160,12 @@ Error StreamPeerTCP::write(const uint8_t *p_data, int p_bytes, int &r_sent, bool } Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool p_block) { - if (!is_connected_to_host()) { - return FAILED; } if (status == STATUS_CONNECTING) { - if (_poll_connection() != OK) { - return FAILED; } @@ -194,12 +181,10 @@ Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool r_received = 0; while (to_read) { - int read = 0; err = _sock->recv(p_buffer + total_read, to_read, read); if (err != OK) { - if (err != ERR_BUSY) { disconnect_from_host(); return FAILED; @@ -218,13 +203,11 @@ Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool } } else if (read == 0) { - disconnect_from_host(); r_received = total_read; return ERR_FILE_EOF; } else { - to_read -= read; total_read += read; @@ -241,18 +224,15 @@ Error StreamPeerTCP::read(uint8_t *p_buffer, int p_bytes, int &r_received, bool } void StreamPeerTCP::set_no_delay(bool p_enabled) { - ERR_FAIL_COND(!is_connected_to_host()); _sock->set_tcp_no_delay_enabled(p_enabled); } bool StreamPeerTCP::is_connected_to_host() const { - return _sock.is_valid() && _sock->is_open() && (status == STATUS_CONNECTED || status == STATUS_CONNECTING); } StreamPeerTCP::Status StreamPeerTCP::get_status() { - if (status == STATUS_CONNECTING) { _poll_connection(); } else if (status == STATUS_CONNECTED) { @@ -278,9 +258,9 @@ StreamPeerTCP::Status StreamPeerTCP::get_status() { } void StreamPeerTCP::disconnect_from_host() { - - if (_sock.is_valid() && _sock->is_open()) + if (_sock.is_valid() && _sock->is_open()) { _sock->close(); + } timeout = 0; status = STATUS_NONE; @@ -294,59 +274,51 @@ Error StreamPeerTCP::poll(NetSocket::PollType p_type, int timeout) { } Error StreamPeerTCP::put_data(const uint8_t *p_data, int p_bytes) { - int total; return write(p_data, p_bytes, total, true); } Error StreamPeerTCP::put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) { - return write(p_data, p_bytes, r_sent, false); } Error StreamPeerTCP::get_data(uint8_t *p_buffer, int p_bytes) { - int total; return read(p_buffer, p_bytes, total, true); } Error StreamPeerTCP::get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) { - return read(p_buffer, p_bytes, r_received, false); } int StreamPeerTCP::get_available_bytes() const { - ERR_FAIL_COND_V(!_sock.is_valid(), -1); return _sock->get_available_bytes(); } IP_Address StreamPeerTCP::get_connected_host() const { - return peer_host; } uint16_t StreamPeerTCP::get_connected_port() const { - return peer_port; } Error StreamPeerTCP::_connect(const String &p_address, int p_port) { - IP_Address ip; if (p_address.is_valid_ip_address()) { ip = p_address; } else { ip = IP::get_singleton()->resolve_hostname(p_address); - if (!ip.is_valid()) + if (!ip.is_valid()) { return ERR_CANT_RESOLVE; + } } return connect_to_host(ip, p_port); } void StreamPeerTCP::_bind_methods() { - ClassDB::bind_method(D_METHOD("connect_to_host", "host", "port"), &StreamPeerTCP::_connect); ClassDB::bind_method(D_METHOD("is_connected_to_host"), &StreamPeerTCP::is_connected_to_host); ClassDB::bind_method(D_METHOD("get_status"), &StreamPeerTCP::get_status); @@ -362,13 +334,9 @@ void StreamPeerTCP::_bind_methods() { } StreamPeerTCP::StreamPeerTCP() : - _sock(Ref<NetSocket>(NetSocket::create())), - timeout(0), - status(STATUS_NONE), - peer_port(0) { + _sock(Ref<NetSocket>(NetSocket::create())) { } StreamPeerTCP::~StreamPeerTCP() { - disconnect_from_host(); } diff --git a/core/io/stream_peer_tcp.h b/core/io/stream_peer_tcp.h index 86df9ab8cf..10b90908d4 100644 --- a/core/io/stream_peer_tcp.h +++ b/core/io/stream_peer_tcp.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -37,13 +37,11 @@ #include "core/io/stream_peer.h" class StreamPeerTCP : public StreamPeer { - GDCLASS(StreamPeerTCP, StreamPeer); OBJ_CATEGORY("Networking"); public: enum Status { - STATUS_NONE, STATUS_CONNECTING, STATUS_CONNECTED, @@ -52,10 +50,10 @@ public: protected: Ref<NetSocket> _sock; - uint64_t timeout; - Status status; + uint64_t timeout = 0; + Status status = STATUS_NONE; IP_Address peer_host; - uint16_t peer_port; + uint16_t peer_port = 0; Error _connect(const String &p_address, int p_port); Error _poll_connection(); @@ -73,7 +71,7 @@ public: uint16_t get_connected_port() const; void disconnect_from_host(); - int get_available_bytes() const; + int get_available_bytes() const override; Status get_status(); void set_no_delay(bool p_enabled); @@ -82,10 +80,10 @@ public: Error poll(NetSocket::PollType p_type, int timeout = 0); // Read/Write from StreamPeer - Error put_data(const uint8_t *p_data, int p_bytes); - Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent); - Error get_data(uint8_t *p_buffer, int p_bytes); - Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received); + Error put_data(const uint8_t *p_data, int p_bytes) override; + Error put_partial_data(const uint8_t *p_data, int p_bytes, int &r_sent) override; + Error get_data(uint8_t *p_buffer, int p_bytes) override; + Error get_partial_data(uint8_t *p_buffer, int p_bytes, int &r_received) override; StreamPeerTCP(); ~StreamPeerTCP(); diff --git a/core/io/tcp_server.cpp b/core/io/tcp_server.cpp index 69c2ba7943..323d2bbd7f 100644 --- a/core/io/tcp_server.cpp +++ b/core/io/tcp_server.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,7 +31,6 @@ #include "tcp_server.h" void TCP_Server::_bind_methods() { - ClassDB::bind_method(D_METHOD("listen", "port", "bind_address"), &TCP_Server::listen, DEFVAL("*")); ClassDB::bind_method(D_METHOD("is_connection_available"), &TCP_Server::is_connection_available); ClassDB::bind_method(D_METHOD("is_listening"), &TCP_Server::is_listening); @@ -40,7 +39,6 @@ void TCP_Server::_bind_methods() { } Error TCP_Server::listen(uint16_t p_port, const IP_Address &p_bind_address) { - ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); @@ -49,8 +47,9 @@ Error TCP_Server::listen(uint16_t p_port, const IP_Address &p_bind_address) { IP::Type ip_type = IP::TYPE_ANY; // If the bind address is valid use its type as the socket type - if (p_bind_address.is_valid()) + if (p_bind_address.is_valid()) { ip_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; + } err = _sock->open(NetSocket::TYPE_TCP, ip_type); @@ -62,7 +61,6 @@ Error TCP_Server::listen(uint16_t p_port, const IP_Address &p_bind_address) { err = _sock->bind(p_bind_address, p_port); if (err != OK) { - _sock->close(); return ERR_ALREADY_IN_USE; } @@ -83,18 +81,17 @@ bool TCP_Server::is_listening() const { } bool TCP_Server::is_connection_available() const { - ERR_FAIL_COND_V(!_sock.is_valid(), false); - if (!_sock->is_open()) + if (!_sock->is_open()) { return false; + } Error err = _sock->poll(NetSocket::POLL_TYPE_IN, 0); return (err == OK); } Ref<StreamPeerTCP> TCP_Server::take_connection() { - Ref<StreamPeerTCP> conn; if (!is_connection_available()) { return conn; @@ -104,8 +101,9 @@ Ref<StreamPeerTCP> TCP_Server::take_connection() { IP_Address ip; uint16_t port = 0; ns = _sock->accept(ip, port); - if (!ns.is_valid()) + if (!ns.is_valid()) { return conn; + } conn = Ref<StreamPeerTCP>(memnew(StreamPeerTCP)); conn->accept_socket(ns, ip, port); @@ -113,7 +111,6 @@ Ref<StreamPeerTCP> TCP_Server::take_connection() { } void TCP_Server::stop() { - if (_sock.is_valid()) { _sock->close(); } @@ -124,6 +121,5 @@ TCP_Server::TCP_Server() : } TCP_Server::~TCP_Server() { - stop(); } diff --git a/core/io/tcp_server.h b/core/io/tcp_server.h index ca52b13ba1..f06ddd2d99 100644 --- a/core/io/tcp_server.h +++ b/core/io/tcp_server.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -37,7 +37,6 @@ #include "core/io/stream_peer_tcp.h" class TCP_Server : public Reference { - GDCLASS(TCP_Server, Reference); protected: diff --git a/core/io/translation_loader_po.cpp b/core/io/translation_loader_po.cpp index bce5361c76..ce2c3eb1cd 100644 --- a/core/io/translation_loader_po.cpp +++ b/core/io/translation_loader_po.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,76 +31,147 @@ #include "translation_loader_po.h" #include "core/os/file_access.h" -#include "core/translation.h" +#include "core/string/translation.h" +#include "core/string/translation_po.h" RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { - enum Status { - STATUS_NONE, STATUS_READING_ID, STATUS_READING_STRING, + STATUS_READING_CONTEXT, + STATUS_READING_PLURAL, }; Status status = STATUS_NONE; String msg_id; String msg_str; + String msg_context; + Vector<String> msgs_plural; String config; - if (r_error) + if (r_error) { *r_error = ERR_FILE_CORRUPT; + } - Ref<Translation> translation = Ref<Translation>(memnew(Translation)); + Ref<TranslationPO> translation = Ref<TranslationPO>(memnew(TranslationPO)); int line = 1; + int plural_forms = 0; + int plural_index = -1; + bool entered_context = false; bool skip_this = false; bool skip_next = false; bool is_eof = false; + const String path = f->get_path(); while (!is_eof) { - String l = f->get_line().strip_edges(); is_eof = f->eof_reached(); // If we reached last line and it's not a content line, break, otherwise let processing that last loop - if (is_eof && l.empty()) { - - if (status == STATUS_READING_ID) { + if (is_eof && l.is_empty()) { + if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || (status == STATUS_READING_PLURAL && plural_index != plural_forms - 1)) { memdelete(f); - ERR_FAIL_V_MSG(RES(), f->get_path() + ":" + itos(line) + " Unexpected EOF while reading 'msgid' at file: "); + ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading PO file at: " + path + ":" + itos(line)); } else { break; } } - if (l.begins_with("msgid")) { + if (l.begins_with("msgctxt")) { + if (status != STATUS_READING_STRING && status != STATUS_READING_PLURAL) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line)); + } - if (status == STATUS_READING_ID) { + // In PO file, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read + // and set "entered_context" to true to prevent adding twice. + if (!skip_this && msg_id != "") { + if (status == STATUS_READING_STRING) { + translation->add_message(msg_id, msg_str, msg_context); + } else if (status == STATUS_READING_PLURAL) { + if (plural_index != plural_forms - 1) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line)); + } + translation->add_plural_message(msg_id, msgs_plural, msg_context); + } + } + msg_context = ""; + l = l.substr(7, l.length()).strip_edges(); + status = STATUS_READING_CONTEXT; + entered_context = true; + } + if (l.begins_with("msgid_plural")) { + if (plural_forms == 0) { memdelete(f); - ERR_FAIL_V_MSG(RES(), f->get_path() + ":" + itos(line) + " Unexpected 'msgid', was expecting 'msgstr' while parsing: "); + ERR_FAIL_V_MSG(RES(), "PO file uses 'msgid_plural' but 'Plural-Forms' is invalid or missing in header: " + path + ":" + itos(line)); + } else if (status != STATUS_READING_ID) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line)); + } + // We don't record the message in "msgid_plural" itself as tr_n(), TTRN(), RTRN() interfaces provide the plural string already. + // We just have to reset variables related to plurals for "msgstr[]" later on. + l = l.substr(12, l.length()).strip_edges(); + plural_index = -1; + msgs_plural.clear(); + msgs_plural.resize(plural_forms); + status = STATUS_READING_PLURAL; + } else if (l.begins_with("msgid")) { + if (status == STATUS_READING_ID) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line)); } if (msg_id != "") { - if (!skip_this) - translation->add_message(msg_id, msg_str); - } else if (config == "") + if (!skip_this && !entered_context) { + if (status == STATUS_READING_STRING) { + translation->add_message(msg_id, msg_str, msg_context); + } else if (status == STATUS_READING_PLURAL) { + if (plural_index != plural_forms - 1) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line)); + } + translation->add_plural_message(msg_id, msgs_plural, msg_context); + } + } + } else if (config == "") { config = msg_str; + // Record plural rule. + int p_start = config.find("Plural-Forms"); + if (p_start != -1) { + int p_end = config.find("\n", p_start); + translation->set_plural_rule(config.substr(p_start, p_end - p_start)); + plural_forms = translation->get_plural_forms(); + } + } l = l.substr(5, l.length()).strip_edges(); status = STATUS_READING_ID; + // If we did not encounter msgctxt, we reset context to empty to reset it. + if (!entered_context) { + msg_context = ""; + } msg_id = ""; msg_str = ""; skip_this = skip_next; skip_next = false; + entered_context = false; } - if (l.begins_with("msgstr")) { - + if (l.begins_with("msgstr[")) { + if (status != STATUS_READING_PLURAL) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line)); + } + plural_index++; // Increment to add to the next slot in vector msgs_plural. + l = l.substr(9, l.length()).strip_edges(); + } else if (l.begins_with("msgstr")) { if (status != STATUS_READING_ID) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), f->get_path() + ":" + itos(line) + " Unexpected 'msgstr', was expecting 'msgid' while parsing: "); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line)); } l = l.substr(6, l.length()).strip_edges(); @@ -112,10 +183,13 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { skip_next = true; } line++; - continue; //nothing to read or comment + continue; // Nothing to read or comment. } - ERR_FAIL_COND_V_MSG(!l.begins_with("\"") || status == STATUS_NONE, RES(), f->get_path() + ":" + itos(line) + " Invalid line '" + l + "' while parsing: "); + if (!l.begins_with("\"") || status == STATUS_NONE) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line)); + } l = l.substr(1, l.length()); // Find final quote, ignoring escaped ones (\"). @@ -137,40 +211,57 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { escape_next = false; } - ERR_FAIL_COND_V_MSG(end_pos == -1, RES(), f->get_path() + ":" + itos(line) + ": Expected '\"' at end of message while parsing file."); + if (end_pos == -1) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line)); + } l = l.substr(0, end_pos); l = l.c_unescape(); - if (status == STATUS_READING_ID) + if (status == STATUS_READING_ID) { msg_id += l; - else + } else if (status == STATUS_READING_STRING) { msg_str += l; + } else if (status == STATUS_READING_CONTEXT) { + msg_context += l; + } else if (status == STATUS_READING_PLURAL && plural_index >= 0) { + msgs_plural.write[plural_index] = msgs_plural[plural_index] + l; + } line++; } - f->close(); memdelete(f); + // Add the last set of data from last iteration. if (status == STATUS_READING_STRING) { - if (msg_id != "") { - if (!skip_this) - translation->add_message(msg_id, msg_str); - } else if (config == "") + if (!skip_this) { + translation->add_message(msg_id, msg_str, msg_context); + } + } else if (config == "") { config = msg_str; + } + } else if (status == STATUS_READING_PLURAL) { + if (!skip_this && msg_id != "") { + if (plural_index != plural_forms - 1) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line)); + } + translation->add_plural_message(msg_id, msgs_plural, msg_context); + } } - ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + f->get_path() + "."); + ERR_FAIL_COND_V_MSG(config == "", RES(), "No config found in file: " + path + "."); Vector<String> configs = config.split("\n"); for (int i = 0; i < configs.size(); i++) { - String c = configs[i].strip_edges(); int p = c.find(":"); - if (p == -1) + if (p == -1) { continue; + } String prop = c.substr(0, p).strip_edges(); String value = c.substr(p + 1, c.length()).strip_edges(); @@ -179,16 +270,17 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { } } - if (r_error) + if (r_error) { *r_error = OK; + } return translation; } RES TranslationLoaderPO::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, bool p_no_cache) { - - if (r_error) + if (r_error) { *r_error = ERR_CANT_OPEN; + } FileAccess *f = FileAccess::open(p_path, FileAccess::READ); ERR_FAIL_COND_V_MSG(!f, RES(), "Cannot open file '" + p_path + "'."); @@ -197,21 +289,16 @@ RES TranslationLoaderPO::load(const String &p_path, const String &p_original_pat } void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const { - p_extensions->push_back("po"); - //p_extensions->push_back("mo"); //mo in the future... } -bool TranslationLoaderPO::handles_type(const String &p_type) const { +bool TranslationLoaderPO::handles_type(const String &p_type) const { return (p_type == "Translation"); } String TranslationLoaderPO::get_resource_type(const String &p_path) const { - - if (p_path.get_extension().to_lower() == "po") + if (p_path.get_extension().to_lower() == "po") { return "Translation"; + } return ""; } - -TranslationLoaderPO::TranslationLoaderPO() { -} diff --git a/core/io/translation_loader_po.h b/core/io/translation_loader_po.h index 137dfd1768..a524972588 100644 --- a/core/io/translation_loader_po.h +++ b/core/io/translation_loader_po.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,7 +33,7 @@ #include "core/io/resource_loader.h" #include "core/os/file_access.h" -#include "core/translation.h" +#include "core/string/translation.h" class TranslationLoaderPO : public ResourceFormatLoader { public: @@ -43,7 +43,7 @@ public: virtual bool handles_type(const String &p_type) const; virtual String get_resource_type(const String &p_path) const; - TranslationLoaderPO(); + TranslationLoaderPO() {} }; #endif // TRANSLATION_LOADER_PO_H diff --git a/core/io/udp_server.cpp b/core/io/udp_server.cpp index 16b7863cdd..f56fb431ef 100644 --- a/core/io/udp_server.cpp +++ b/core/io/udp_server.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,16 +31,62 @@ #include "udp_server.h" void UDPServer::_bind_methods() { - ClassDB::bind_method(D_METHOD("listen", "port", "bind_address"), &UDPServer::listen, DEFVAL("*")); + ClassDB::bind_method(D_METHOD("poll"), &UDPServer::poll); ClassDB::bind_method(D_METHOD("is_connection_available"), &UDPServer::is_connection_available); ClassDB::bind_method(D_METHOD("is_listening"), &UDPServer::is_listening); ClassDB::bind_method(D_METHOD("take_connection"), &UDPServer::take_connection); ClassDB::bind_method(D_METHOD("stop"), &UDPServer::stop); + ClassDB::bind_method(D_METHOD("set_max_pending_connections", "max_pending_connections"), &UDPServer::set_max_pending_connections); + ClassDB::bind_method(D_METHOD("get_max_pending_connections"), &UDPServer::get_max_pending_connections); + ADD_PROPERTY(PropertyInfo(Variant::INT, "max_pending_connections", PROPERTY_HINT_RANGE, "0,256,1"), "set_max_pending_connections", "get_max_pending_connections"); } -Error UDPServer::listen(uint16_t p_port, const IP_Address &p_bind_address) { +Error UDPServer::poll() { + ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); + if (!_sock->is_open()) { + return ERR_UNCONFIGURED; + } + Error err; + int read; + IP_Address ip; + uint16_t port; + while (true) { + err = _sock->recvfrom(recv_buffer, sizeof(recv_buffer), read, ip, port); + if (err != OK) { + if (err == ERR_BUSY) { + break; + } + return FAILED; + } + Peer p; + p.ip = ip; + p.port = port; + List<Peer>::Element *E = peers.find(p); + if (!E) { + E = pending.find(p); + } + if (E) { + E->get().peer->store_packet(ip, port, recv_buffer, read); + } else { + if (pending.size() >= max_pending_connections) { + // Drop connection. + continue; + } + // It's a new peer, add it to the pending list. + Peer peer; + peer.ip = ip; + peer.port = port; + peer.peer = memnew(PacketPeerUDP); + peer.peer->connect_shared_socket(_sock, ip, port, this); + peer.peer->store_packet(ip, port, recv_buffer, read); + pending.push_back(peer); + } + } + return OK; +} +Error UDPServer::listen(uint16_t p_port, const IP_Address &p_bind_address) { ERR_FAIL_COND_V(!_sock.is_valid(), ERR_UNAVAILABLE); ERR_FAIL_COND_V(_sock->is_open(), ERR_ALREADY_IN_USE); ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER); @@ -48,13 +94,15 @@ Error UDPServer::listen(uint16_t p_port, const IP_Address &p_bind_address) { Error err; IP::Type ip_type = IP::TYPE_ANY; - if (p_bind_address.is_valid()) + if (p_bind_address.is_valid()) { ip_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6; + } err = _sock->open(NetSocket::TYPE_UDP, ip_type); - if (err != OK) + if (err != OK) { return ERR_CANT_CREATE; + } _sock->set_blocking_enabled(false); _sock->set_reuse_address_enabled(true); @@ -76,37 +124,73 @@ bool UDPServer::is_listening() const { } bool UDPServer::is_connection_available() const { - ERR_FAIL_COND_V(!_sock.is_valid(), false); - if (!_sock->is_open()) + if (!_sock->is_open()) { return false; + } - Error err = _sock->poll(NetSocket::POLL_TYPE_IN, 0); - return (err == OK); + return pending.size() > 0; } -Ref<PacketPeerUDP> UDPServer::take_connection() { +void UDPServer::set_max_pending_connections(int p_max) { + ERR_FAIL_COND_MSG(p_max < 0, "Max pending connections value must be a positive number (0 means refuse new connections)."); + max_pending_connections = p_max; + while (p_max > pending.size()) { + List<Peer>::Element *E = pending.back(); + if (!E) { + break; + } + memdelete(E->get().peer); + pending.erase(E); + } +} +int UDPServer::get_max_pending_connections() const { + return max_pending_connections; +} + +Ref<PacketPeerUDP> UDPServer::take_connection() { Ref<PacketPeerUDP> conn; if (!is_connection_available()) { return conn; } - conn = Ref<PacketPeerUDP>(memnew(PacketPeerUDP)); - conn->connect_socket(_sock); - _sock = Ref<NetSocket>(NetSocket::create()); - listen(bind_port, bind_address); - return conn; + Peer peer = pending[0]; + pending.pop_front(); + peers.push_back(peer); + return peer.peer; } -void UDPServer::stop() { +void UDPServer::remove_peer(IP_Address p_ip, int p_port) { + Peer peer; + peer.ip = p_ip; + peer.port = p_port; + List<Peer>::Element *E = peers.find(peer); + if (E) { + peers.erase(E); + } +} +void UDPServer::stop() { if (_sock.is_valid()) { _sock->close(); } bind_port = 0; bind_address = IP_Address(); + List<Peer>::Element *E = peers.front(); + while (E) { + E->get().peer->disconnect_shared_socket(); + E = E->next(); + } + E = pending.front(); + while (E) { + E->get().peer->disconnect_shared_socket(); + memdelete(E->get().peer); + E = E->next(); + } + peers.clear(); + pending.clear(); } UDPServer::UDPServer() : @@ -114,6 +198,5 @@ UDPServer::UDPServer() : } UDPServer::~UDPServer() { - stop(); } diff --git a/core/io/udp_server.h b/core/io/udp_server.h index 90bb82b62b..bbd2f951c9 100644 --- a/core/io/udp_server.h +++ b/core/io/udp_server.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -38,15 +38,40 @@ class UDPServer : public Reference { GDCLASS(UDPServer, Reference); protected: - static void _bind_methods(); - int bind_port; + enum { + PACKET_BUFFER_SIZE = 65536 + }; + + struct Peer { + PacketPeerUDP *peer; + IP_Address ip; + uint16_t port = 0; + + bool operator==(const Peer &p_other) const { + return (ip == p_other.ip && port == p_other.port); + } + }; + uint8_t recv_buffer[PACKET_BUFFER_SIZE]; + + int bind_port = 0; IP_Address bind_address; + + List<Peer> peers; + List<Peer> pending; + int max_pending_connections = 16; + Ref<NetSocket> _sock; + static void _bind_methods(); + public: + void remove_peer(IP_Address p_ip, int p_port); Error listen(uint16_t p_port, const IP_Address &p_bind_address = IP_Address("*")); + Error poll(); bool is_listening() const; bool is_connection_available() const; + void set_max_pending_connections(int p_max); + int get_max_pending_connections() const; Ref<PacketPeerUDP> take_connection(); void stop(); diff --git a/core/io/xml_parser.cpp b/core/io/xml_parser.cpp index 9613ad3f10..905be6089d 100644 --- a/core/io/xml_parser.cpp +++ b/core/io/xml_parser.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -30,17 +30,19 @@ #include "xml_parser.h" -#include "core/print_string.h" +#include "core/string/print_string.h" //#define DEBUG_XML VARIANT_ENUM_CAST(XMLParser::NodeType); -static bool _equalsn(const CharType *str1, const CharType *str2, int len) { +static bool _equalsn(const char32_t *str1, const char32_t *str2, int len) { int i; - for (i = 0; i < len && str1[i] && str2[i]; ++i) - if (str1[i] != str2[i]) + for (i = 0; i < len && str1[i] && str2[i]; ++i) { + if (str1[i] != str2[i]) { return false; + } + } // if one (or both) of the strings was smaller then they // are only equal if they have the same length @@ -48,12 +50,12 @@ static bool _equalsn(const CharType *str1, const CharType *str2, int len) { } String XMLParser::_replace_special_characters(const String &origstr) { - int pos = origstr.find("&"); int oldPos = 0; - if (pos == -1) + if (pos == -1) { return origstr; + } String newstr; @@ -62,7 +64,7 @@ String XMLParser::_replace_special_characters(const String &origstr) { int specialChar = -1; for (int i = 0; i < (int)special_characters.size(); ++i) { - const CharType *p = &origstr[pos] + 1; + const char32_t *p = &origstr[pos] + 1; if (_equalsn(&special_characters[i][1], p, special_characters[i].length() - 1)) { specialChar = i; @@ -84,8 +86,9 @@ String XMLParser::_replace_special_characters(const String &origstr) { pos = origstr.find("&", pos); } - if (oldPos < origstr.length() - 1) + if (oldPos < origstr.length() - 1) { newstr += (origstr.substr(oldPos, origstr.length() - oldPos)); + } return newstr; } @@ -100,12 +103,15 @@ bool XMLParser::_set_text(char *start, char *end) { // only white space, so that this text won't be reported if (end - start < 3) { char *p = start; - for (; p != end; ++p) - if (!_is_white_space(*p)) + for (; p != end; ++p) { + if (!_is_white_space(*p)) { break; + } + } - if (p == end) + if (p == end) { return false; + } } // set current text to the parsed text, and replace xml special characters @@ -126,8 +132,9 @@ void XMLParser::_parse_closing_xml_element() { ++P; const char *pBeginClose = P; - while (*P != '>') + while (*P != '>') { ++P; + } node_name = String::utf8(pBeginClose, (int)(P - pBeginClose)); #ifdef DEBUG_XML @@ -141,16 +148,17 @@ void XMLParser::_ignore_definition() { char *F = P; // move until end marked with '>' reached - while (*P != '>') + while (*P != '>') { ++P; + } node_name.parse_utf8(F, P - F); ++P; } bool XMLParser::_parse_cdata() { - - if (*(P + 1) != '[') + if (*(P + 1) != '[') { return false; + } node_type = NODE_CDATA; @@ -161,8 +169,9 @@ bool XMLParser::_parse_cdata() { ++count; } - if (!*P) + if (!*P) { return true; + } char *cDataBegin = P; char *cDataEnd = nullptr; @@ -178,10 +187,11 @@ bool XMLParser::_parse_cdata() { ++P; } - if (cDataEnd) + if (cDataEnd) { node_name = String::utf8(cDataBegin, (int)(cDataEnd - cDataBegin)); - else + } else { node_name = ""; + } #ifdef DEBUG_XML print_line("XML CDATA: " + node_name); #endif @@ -190,7 +200,6 @@ bool XMLParser::_parse_cdata() { } void XMLParser::_parse_comment() { - node_type = NODE_COMMENT; P += 1; @@ -200,10 +209,11 @@ void XMLParser::_parse_comment() { // move until end of comment reached while (count) { - if (*P == '>') + if (*P == '>') { --count; - else if (*P == '<') + } else if (*P == '<') { ++count; + } ++P; } @@ -217,7 +227,6 @@ void XMLParser::_parse_comment() { } void XMLParser::_parse_opening_xml_element() { - node_type = NODE_ELEMENT; node_empty = false; attributes.clear(); @@ -226,46 +235,52 @@ void XMLParser::_parse_opening_xml_element() { const char *startName = P; // find end of element - while (*P != '>' && !_is_white_space(*P)) + while (*P != '>' && !_is_white_space(*P)) { ++P; + } const char *endName = P; // find attributes while (*P != '>') { - if (_is_white_space(*P)) + if (_is_white_space(*P)) { ++P; - else { + } else { if (*P != '/') { // we've got an attribute // read the attribute names const char *attributeNameBegin = P; - while (!_is_white_space(*P) && *P != '=') + while (!_is_white_space(*P) && *P != '=') { ++P; + } const char *attributeNameEnd = P; ++P; // read the attribute value // check for quotes and single quotes, thx to murphy - while ((*P != '\"') && (*P != '\'') && *P) + while ((*P != '\"') && (*P != '\'') && *P) { ++P; + } - if (!*P) // malformatted xml file + if (!*P) { // malformatted xml file return; + } const char attributeQuoteChar = *P; ++P; const char *attributeValueBegin = P; - while (*P != attributeQuoteChar && *P) + while (*P != attributeQuoteChar && *P) { ++P; + } - if (!*P) // malformatted xml file + if (!*P) { // malformatted xml file return; + } const char *attributeValueEnd = P; ++P; @@ -304,21 +319,23 @@ void XMLParser::_parse_opening_xml_element() { } void XMLParser::_parse_current_node() { - char *start = P; node_offset = P - data; // more forward until '<' found - while (*P != '<' && *P) + while (*P != '<' && *P) { ++P; + } - if (!*P) + if (!*P) { return; + } if (P - start > 0) { // we found some text, store it - if (_set_text(start, P)) + if (_set_text(start, P)) { return; + } } ++P; @@ -332,8 +349,9 @@ void XMLParser::_parse_current_node() { _ignore_definition(); break; case '!': - if (!_parse_cdata()) + if (!_parse_cdata()) { _parse_comment(); + } break; default: _parse_opening_xml_element(); @@ -342,22 +360,19 @@ void XMLParser::_parse_current_node() { } uint64_t XMLParser::get_node_offset() const { - return node_offset; -}; +} Error XMLParser::seek(uint64_t p_pos) { - ERR_FAIL_COND_V(!data, ERR_FILE_EOF); ERR_FAIL_COND_V(p_pos >= length, ERR_FILE_EOF); P = data + p_pos; return read(); -}; +} void XMLParser::_bind_methods() { - ClassDB::bind_method(D_METHOD("read"), &XMLParser::read); ClassDB::bind_method(D_METHOD("get_node_type"), &XMLParser::get_node_type); ClassDB::bind_method(D_METHOD("get_node_name"), &XMLParser::get_node_name); @@ -383,10 +398,9 @@ void XMLParser::_bind_methods() { BIND_ENUM_CONSTANT(NODE_COMMENT); BIND_ENUM_CONSTANT(NODE_CDATA); BIND_ENUM_CONSTANT(NODE_UNKNOWN); -}; +} Error XMLParser::read() { - // if not end reached, parse the node if (P && (P - data) < (int64_t)length - 1 && *P != 0) { _parse_current_node(); @@ -397,11 +411,10 @@ Error XMLParser::read() { } XMLParser::NodeType XMLParser::get_node_type() { - return node_type; } -String XMLParser::get_node_data() const { +String XMLParser::get_node_data() const { ERR_FAIL_COND_V(node_type != NODE_TEXT, ""); return node_name; } @@ -410,31 +423,32 @@ String XMLParser::get_node_name() const { ERR_FAIL_COND_V(node_type == NODE_TEXT, ""); return node_name; } -int XMLParser::get_attribute_count() const { +int XMLParser::get_attribute_count() const { return attributes.size(); } -String XMLParser::get_attribute_name(int p_idx) const { +String XMLParser::get_attribute_name(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, attributes.size(), ""); return attributes[p_idx].name; } -String XMLParser::get_attribute_value(int p_idx) const { +String XMLParser::get_attribute_value(int p_idx) const { ERR_FAIL_INDEX_V(p_idx, attributes.size(), ""); return attributes[p_idx].value; } -bool XMLParser::has_attribute(const String &p_name) const { +bool XMLParser::has_attribute(const String &p_name) const { for (int i = 0; i < attributes.size(); i++) { - if (attributes[i].name == p_name) + if (attributes[i].name == p_name) { return true; + } } return false; } -String XMLParser::get_attribute_value(const String &p_name) const { +String XMLParser::get_attribute_value(const String &p_name) const { int idx = -1; for (int i = 0; i < attributes.size(); i++) { if (attributes[i].name == p_name) { @@ -449,7 +463,6 @@ String XMLParser::get_attribute_value(const String &p_name) const { } String XMLParser::get_attribute_value_safe(const String &p_name) const { - int idx = -1; for (int i = 0; i < attributes.size(); i++) { if (attributes[i].name == p_name) { @@ -458,17 +471,17 @@ String XMLParser::get_attribute_value_safe(const String &p_name) const { } } - if (idx < 0) + if (idx < 0) { return ""; + } return attributes[idx].value; } -bool XMLParser::is_empty() const { +bool XMLParser::is_empty() const { return node_empty; } Error XMLParser::open_buffer(const Vector<uint8_t> &p_buffer) { - ERR_FAIL_COND_V(p_buffer.size() == 0, ERR_INVALID_DATA); if (data) { @@ -484,7 +497,6 @@ Error XMLParser::open_buffer(const Vector<uint8_t> &p_buffer) { } Error XMLParser::open(const String &p_path) { - Error err; FileAccess *file = FileAccess::open(p_path, FileAccess::READ, &err); @@ -508,10 +520,10 @@ Error XMLParser::open(const String &p_path) { } void XMLParser::skip_section() { - // skip if this element is empty anyway. - if (is_empty()) + if (is_empty()) { return; + } // read until we've reached the last element in this section int tagcount = 1; @@ -520,15 +532,16 @@ void XMLParser::skip_section() { if (get_node_type() == XMLParser::NODE_ELEMENT && !is_empty()) { ++tagcount; - } else if (get_node_type() == XMLParser::NODE_ELEMENT_END) + } else if (get_node_type() == XMLParser::NODE_ELEMENT_END) { --tagcount; + } } } void XMLParser::close() { - - if (data) + if (data) { memdelete_arr(data); + } data = nullptr; length = 0; P = nullptr; @@ -538,22 +551,19 @@ void XMLParser::close() { } int XMLParser::get_current_line() const { - return 0; } XMLParser::XMLParser() { - - data = nullptr; - close(); special_characters.push_back("&"); special_characters.push_back("<lt;"); special_characters.push_back(">gt;"); special_characters.push_back("\"quot;"); special_characters.push_back("'apos;"); } -XMLParser::~XMLParser() { - if (data) +XMLParser::~XMLParser() { + if (data) { memdelete_arr(data); + } } diff --git a/core/io/xml_parser.h b/core/io/xml_parser.h index 26c3e6802f..01af6a90ad 100644 --- a/core/io/xml_parser.h +++ b/core/io/xml_parser.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -31,17 +31,16 @@ #ifndef XML_PARSER_H #define XML_PARSER_H +#include "core/object/reference.h" #include "core/os/file_access.h" -#include "core/reference.h" -#include "core/ustring.h" -#include "core/vector.h" +#include "core/string/ustring.h" +#include "core/templates/vector.h" /* Based on irrXML (see their zlib license). Added mainly for compatibility with their Collada loader. */ class XMLParser : public Reference { - GDCLASS(XMLParser, Reference); public: @@ -66,15 +65,15 @@ public: }; private: - char *data; - char *P; - uint64_t length; + char *data = nullptr; + char *P = nullptr; + uint64_t length = 0; void unescape(String &p_str); Vector<String> special_characters; String node_name; - bool node_empty; - NodeType node_type; - uint64_t node_offset; + bool node_empty = false; + NodeType node_type = NODE_NONE; + uint64_t node_offset = 0; struct Attribute { String name; diff --git a/core/io/zip_io.cpp b/core/io/zip_io.cpp index 3a2a207d22..4b4a46e198 100644 --- a/core/io/zip_io.cpp +++ b/core/io/zip_io.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -33,7 +33,6 @@ #include "core/os/copymem.h" void *zipio_open(void *data, const char *p_fname, int mode) { - FileAccess *&f = *(FileAccess **)data; String fname; @@ -42,42 +41,37 @@ void *zipio_open(void *data, const char *p_fname, int mode) { if (mode & ZLIB_FILEFUNC_MODE_WRITE) { f = FileAccess::open(fname, FileAccess::WRITE); } else { - f = FileAccess::open(fname, FileAccess::READ); } - if (!f) + if (!f) { return nullptr; + } return data; } uLong zipio_read(void *data, void *fdata, void *buf, uLong size) { - FileAccess *f = *(FileAccess **)data; return f->get_buffer((uint8_t *)buf, size); } uLong zipio_write(voidpf opaque, voidpf stream, const void *buf, uLong size) { - FileAccess *f = *(FileAccess **)opaque; f->store_buffer((uint8_t *)buf, size); return size; } long zipio_tell(voidpf opaque, voidpf stream) { - FileAccess *f = *(FileAccess **)opaque; return f->get_position(); } long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { - FileAccess *f = *(FileAccess **)opaque; int pos = offset; switch (origin) { - case ZLIB_FILEFUNC_SEEK_CUR: pos = f->get_position() + offset; break; @@ -86,14 +80,13 @@ long zipio_seek(voidpf opaque, voidpf stream, uLong offset, int origin) { break; default: break; - }; + } f->seek(pos); return 0; } int zipio_close(voidpf opaque, voidpf stream) { - FileAccess *&f = *(FileAccess **)opaque; if (f) { f->close(); @@ -104,25 +97,21 @@ int zipio_close(voidpf opaque, voidpf stream) { } int zipio_testerror(voidpf opaque, voidpf stream) { - FileAccess *f = *(FileAccess **)opaque; return (f && f->get_error() != OK) ? 1 : 0; } voidpf zipio_alloc(voidpf opaque, uInt items, uInt size) { - voidpf ptr = memalloc(items * size); zeromem(ptr, items * size); return ptr; } void zipio_free(voidpf opaque, voidpf address) { - memfree(address); } zlib_filefunc_def zipio_create_io_from_file(FileAccess **p_file) { - zlib_filefunc_def io; io.opaque = p_file; io.zopen_file = zipio_open; diff --git a/core/io/zip_io.h b/core/io/zip_io.h index ba2065b5d1..52691c65e9 100644 --- a/core/io/zip_io.h +++ b/core/io/zip_io.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ |