diff options
Diffstat (limited to 'core/io')
-rw-r--r-- | core/io/dir_access.cpp | 2 | ||||
-rw-r--r-- | core/io/dir_access.h | 4 | ||||
-rw-r--r-- | core/io/file_access.cpp | 2 | ||||
-rw-r--r-- | core/io/file_access.h | 34 | ||||
-rw-r--r-- | core/io/file_access_encrypted.cpp | 8 | ||||
-rw-r--r-- | core/io/file_access_network.cpp | 8 | ||||
-rw-r--r-- | core/io/file_access_zip.cpp | 2 | ||||
-rw-r--r-- | core/io/http_client_tcp.cpp | 20 | ||||
-rw-r--r-- | core/io/image.cpp | 93 | ||||
-rw-r--r-- | core/io/image.h | 18 | ||||
-rw-r--r-- | core/io/image_loader.cpp | 2 | ||||
-rw-r--r-- | core/io/logger.cpp | 2 | ||||
-rw-r--r-- | core/io/marshalls.cpp | 6 | ||||
-rw-r--r-- | core/io/resource_format_binary.cpp | 99 | ||||
-rw-r--r-- | core/io/resource_importer.cpp | 4 | ||||
-rw-r--r-- | core/io/resource_loader.cpp | 14 | ||||
-rw-r--r-- | core/io/resource_loader.h | 1 | ||||
-rw-r--r-- | core/io/resource_saver.cpp | 4 | ||||
-rw-r--r-- | core/io/resource_uid.cpp | 7 | ||||
-rw-r--r-- | core/io/stream_peer_tcp.cpp | 4 | ||||
-rw-r--r-- | core/io/stream_peer_tcp.h | 2 | ||||
-rw-r--r-- | core/io/zip_io.cpp | 12 |
22 files changed, 192 insertions, 156 deletions
diff --git a/core/io/dir_access.cpp b/core/io/dir_access.cpp index f86dfe8057..79e7fa16e3 100644 --- a/core/io/dir_access.cpp +++ b/core/io/dir_access.cpp @@ -492,6 +492,7 @@ PackedStringArray DirAccess::get_files() { PackedStringArray DirAccess::get_files_at(const String &p_path) { Ref<DirAccess> da = DirAccess::open(p_path); + ERR_FAIL_COND_V_MSG(da.is_null(), PackedStringArray(), vformat("Couldn't open directory at path \"%s\".", p_path)); return da->get_files(); } @@ -501,6 +502,7 @@ PackedStringArray DirAccess::get_directories() { PackedStringArray DirAccess::get_directories_at(const String &p_path) { Ref<DirAccess> da = DirAccess::open(p_path); + ERR_FAIL_COND_V_MSG(da.is_null(), PackedStringArray(), vformat("Couldn't open directory at path \"%s\".", p_path)); return da->get_directories(); } diff --git a/core/io/dir_access.h b/core/io/dir_access.h index ee675f1c89..bd24214e73 100644 --- a/core/io/dir_access.h +++ b/core/io/dir_access.h @@ -52,8 +52,10 @@ public: private: AccessType _access_type = ACCESS_FILESYSTEM; static CreateFunc create_func[ACCESS_MAX]; ///< set this to instance a filesystem object + static Ref<DirAccess> _open(const String &p_path); Error _copy_dir(Ref<DirAccess> &p_target_da, String p_to, int p_chmod_flags, bool p_copy_links); + PackedStringArray _get_contents(bool p_directories); thread_local static Error last_dir_open_error; bool include_navigational = false; @@ -134,7 +136,6 @@ public: } static Ref<DirAccess> open(const String &p_path, Error *r_error = nullptr); - static Ref<DirAccess> _open(const String &p_path); static int _get_drive_count(); static String get_drive_name(int p_idx); @@ -151,7 +152,6 @@ public: static PackedStringArray get_files_at(const String &p_path); PackedStringArray get_directories(); static PackedStringArray get_directories_at(const String &p_path); - PackedStringArray _get_contents(bool p_directories); String _get_next(); void set_include_navigational(bool p_enable); diff --git a/core/io/file_access.cpp b/core/io/file_access.cpp index 499f083f51..cb25564342 100644 --- a/core/io/file_access.cpp +++ b/core/io/file_access.cpp @@ -548,7 +548,7 @@ void FileAccess::store_64(uint64_t p_dest) { } void FileAccess::store_real(real_t p_real) { - if (sizeof(real_t) == 4) { + if constexpr (sizeof(real_t) == 4) { store_float(p_real); } else { store_double(p_real); diff --git a/core/io/file_access.h b/core/io/file_access.h index f8c42d3c0d..8ca44306a0 100644 --- a/core/io/file_access.h +++ b/core/io/file_access.h @@ -53,6 +53,20 @@ public: ACCESS_MAX }; + enum ModeFlags { + READ = 1, + WRITE = 2, + READ_WRITE = 3, + WRITE_READ = 7, + }; + + enum CompressionMode { + COMPRESSION_FASTLZ = Compression::MODE_FASTLZ, + COMPRESSION_DEFLATE = Compression::MODE_DEFLATE, + COMPRESSION_ZSTD = Compression::MODE_ZSTD, + COMPRESSION_GZIP = Compression::MODE_GZIP + }; + typedef void (*FileCloseFailNotify)(const String &); typedef Ref<FileAccess> (*CreateFunc)(); @@ -69,6 +83,7 @@ protected: String fix_path(const String &p_path) const; virtual Error open_internal(const String &p_path, int p_mode_flags) = 0; ///< open a file virtual uint64_t _get_modified_time(const String &p_file) = 0; + virtual void _set_access_type(AccessType p_access); static FileCloseFailNotify close_fail_notify; @@ -83,25 +98,11 @@ private: return memnew(T); } + static Ref<FileAccess> _open(const String &p_path, ModeFlags p_mode_flags); + public: static void set_file_close_fail_notify_callback(FileCloseFailNotify p_cbk) { close_fail_notify = p_cbk; } - virtual void _set_access_type(AccessType p_access); - - enum ModeFlags { - READ = 1, - WRITE = 2, - READ_WRITE = 3, - WRITE_READ = 7, - }; - - enum CompressionMode { - COMPRESSION_FASTLZ = Compression::MODE_FASTLZ, - COMPRESSION_DEFLATE = Compression::MODE_DEFLATE, - COMPRESSION_ZSTD = Compression::MODE_ZSTD, - COMPRESSION_GZIP = Compression::MODE_GZIP - }; - virtual bool is_open() const = 0; ///< true when file is open virtual String get_path() const { return ""; } /// returns the path for the current open file @@ -173,7 +174,6 @@ public: static Ref<FileAccess> create_for_path(const String &p_path); static Ref<FileAccess> open(const String &p_path, int p_mode_flags, Error *r_error = nullptr); /// Create a file access (for the current platform) this is the only portable way of accessing files. - static Ref<FileAccess> _open(const String &p_path, ModeFlags p_mode_flags); static Ref<FileAccess> open_encrypted(const String &p_path, ModeFlags p_mode_flags, const Vector<uint8_t> &p_key); static Ref<FileAccess> open_encrypted_pass(const String &p_path, ModeFlags p_mode_flags, const String &p_pass); static Ref<FileAccess> open_compressed(const String &p_path, ModeFlags p_mode_flags, CompressionMode p_compress_mode = COMPRESSION_FASTLZ); diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp index be502dacd9..e7b2a2dfee 100644 --- a/core/io/file_access_encrypted.cpp +++ b/core/io/file_access_encrypted.cpp @@ -102,13 +102,13 @@ Error FileAccessEncrypted::open_and_parse(Ref<FileAccess> p_base, const Vector<u Error FileAccessEncrypted::open_and_parse_password(Ref<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); + Vector<uint8_t> key_md5; + key_md5.resize(32); for (int i = 0; i < 32; i++) { - key.write[i] = cs[i]; + key_md5.write[i] = cs[i]; } - return open_and_parse(p_base, key, p_mode); + return open_and_parse(p_base, key_md5, p_mode); } Error FileAccessEncrypted::open_internal(const String &p_path, int p_mode_flags) { diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index 13730518bf..87f3f66597 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -137,12 +137,12 @@ void FileAccessNetworkClient::_thread_func() { int64_t offset = get_64(); int32_t len = get_32(); - Vector<uint8_t> block; - block.resize(len); - client->get_data(block.ptrw(), len); + Vector<uint8_t> resp_block; + resp_block.resize(len); + client->get_data(resp_block.ptrw(), len); if (fa) { //may have been queued - fa->_set_block(offset, block); + fa->_set_block(offset, resp_block); } } break; diff --git a/core/io/file_access_zip.cpp b/core/io/file_access_zip.cpp index 2af6f370cf..72503851c1 100644 --- a/core/io/file_access_zip.cpp +++ b/core/io/file_access_zip.cpp @@ -337,7 +337,7 @@ bool FileAccessZip::file_exists(const String &p_name) { } FileAccessZip::FileAccessZip(const String &p_path, const PackedData::PackedFile &p_file) { - _open(p_path, FileAccess::READ); + open_internal(p_path, FileAccess::READ); } FileAccessZip::~FileAccessZip() { diff --git a/core/io/http_client_tcp.cpp b/core/io/http_client_tcp.cpp index 5c1d00a330..aff79320ca 100644 --- a/core/io/http_client_tcp.cpp +++ b/core/io/http_client_tcp.cpp @@ -358,38 +358,38 @@ Error HTTPClientTCP::poll() { } break; } } else if (tls) { - Ref<StreamPeerTLS> tls; + Ref<StreamPeerTLS> tls_conn; if (!handshaking) { // Connect the StreamPeerTLS and start handshaking. - tls = Ref<StreamPeerTLS>(StreamPeerTLS::create()); - tls->set_blocking_handshake_enabled(false); - Error err = tls->connect_to_stream(tcp_connection, tls_verify_host, conn_host); + tls_conn = Ref<StreamPeerTLS>(StreamPeerTLS::create()); + tls_conn->set_blocking_handshake_enabled(false); + Error err = tls_conn->connect_to_stream(tcp_connection, tls_verify_host, conn_host); if (err != OK) { close(); status = STATUS_TLS_HANDSHAKE_ERROR; return ERR_CANT_CONNECT; } - connection = tls; + connection = tls_conn; handshaking = true; } else { // We are already handshaking, which means we can use your already active TLS connection. - tls = static_cast<Ref<StreamPeerTLS>>(connection); - if (tls.is_null()) { + tls_conn = static_cast<Ref<StreamPeerTLS>>(connection); + if (tls_conn.is_null()) { close(); status = STATUS_TLS_HANDSHAKE_ERROR; return ERR_CANT_CONNECT; } - tls->poll(); // Try to finish the handshake. + tls_conn->poll(); // Try to finish the handshake. } - if (tls->get_status() == StreamPeerTLS::STATUS_CONNECTED) { + if (tls_conn->get_status() == StreamPeerTLS::STATUS_CONNECTED) { // Handshake has been successful. handshaking = false; ip_candidates.clear(); status = STATUS_CONNECTED; return OK; - } else if (tls->get_status() != StreamPeerTLS::STATUS_HANDSHAKING) { + } else if (tls_conn->get_status() != StreamPeerTLS::STATUS_HANDSHAKING) { // Handshake has failed. close(); status = STATUS_TLS_HANDSHAKE_ERROR; diff --git a/core/io/image.cpp b/core/io/image.cpp index 812bfa8263..3450eb7abd 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -444,7 +444,7 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p uint8_t rgba[4] = { 0, 0, 0, 255 }; - if (read_gray) { + if constexpr (read_gray) { rgba[0] = rofs[0]; rgba[1] = rofs[0]; rgba[2] = rofs[0]; @@ -454,11 +454,11 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p } } - if (read_alpha || write_alpha) { + if constexpr (read_alpha || write_alpha) { rgba[3] = read_alpha ? rofs[read_bytes] : 255; } - if (write_gray) { + if constexpr (write_gray) { //TODO: not correct grayscale, should use fixed point version of actual weights wofs[0] = uint8_t((uint16_t(rgba[0]) + uint16_t(rgba[1]) + uint16_t(rgba[2])) / 3); } else { @@ -467,7 +467,7 @@ static void _convert(int p_width, int p_height, const uint8_t *p_src, uint8_t *p } } - if (write_alpha) { + if constexpr (write_alpha) { wofs[write_bytes] = rgba[3]; } } @@ -640,7 +640,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ 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; + double ox, oy, dx, dy; int ox1, oy1, ox2, oy2; // destination pixel values // width and height decreased by 1 @@ -671,7 +671,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ for (int n = -1; n < 3; n++) { // get Y coefficient - k1 = _bicubic_interp_kernel(dy - (double)n); + [[maybe_unused]] double k1 = _bicubic_interp_kernel(dy - (double)n); oy2 = oy1 + n; if (oy2 < 0) { @@ -683,7 +683,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ for (int m = -1; m < 3; m++) { // get X coefficient - k2 = k1 * _bicubic_interp_kernel((double)m - dx); + [[maybe_unused]] double k2 = k1 * _bicubic_interp_kernel((double)m - dx); ox2 = ox1 + m; if (ox2 < 0) { @@ -697,7 +697,7 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ 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 + if constexpr (sizeof(T) == 2) { //half float color[i] = Math::half_to_float(p[i]); } else { color[i] += p[i] * k2; @@ -707,9 +707,9 @@ static void _scale_cubic(const uint8_t *__restrict p_src, uint8_t *__restrict p_ } for (int i = 0; i < CC; i++) { - if (sizeof(T) == 1) { //byte + if constexpr (sizeof(T) == 1) { //byte dst[i] = CLAMP(Math::fast_ftoi(color[i]), 0, 255); - } else if (sizeof(T) == 2) { //half float + } else if constexpr (sizeof(T) == 2) { //half float dst[i] = Math::make_half_float(color[i]); } else { dst[i] = color[i]; @@ -758,7 +758,7 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict src_xofs_right *= CC; for (uint32_t l = 0; l < CC; l++) { - if (sizeof(T) == 1) { //uint8 + if constexpr (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; @@ -769,7 +769,7 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict 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] = uint8_t(interp); - } else if (sizeof(T) == 2) { //half float + } else if constexpr (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); @@ -786,7 +786,7 @@ static void _scale_bilinear(const uint8_t *__restrict p_src, uint8_t *__restrict 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 + } else if constexpr (sizeof(T) == 4) { //float float xofs_frac = float(src_xofs_frac) / (1 << FRAC_BITS); float yofs_frac = float(src_yofs_frac) / (1 << FRAC_BITS); @@ -877,7 +877,7 @@ static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict 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 + if constexpr (sizeof(T) == 2) { //half float pixel[i] += Math::half_to_float(src_data[i]) * lanczos_val; } else { pixel[i] += src_data[i] * lanczos_val; @@ -934,9 +934,9 @@ static void _scale_lanczos(const uint8_t *__restrict p_src, uint8_t *__restrict for (uint32_t i = 0; i < CC; i++) { pixel[i] /= weight; - if (sizeof(T) == 1) { //byte + if constexpr (sizeof(T) == 1) { //byte dst_data[i] = CLAMP(Math::fast_ftoi(pixel[i]), 0, 255); - } else if (sizeof(T) == 2) { //half float + } else if constexpr (sizeof(T) == 2) { //half float dst_data[i] = Math::make_half_float(pixel[i]); } else { // float dst_data[i] = pixel[i]; @@ -982,7 +982,7 @@ void Image::resize_to_po2(bool p_square, Interpolation 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(data.size() == 0, "Cannot resize image before creating it, use set_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 */; @@ -1017,7 +1017,7 @@ void Image::resize(int p_width, int p_height, Interpolation p_interpolation) { } bool interpolate_mipmaps = mipmap_aware && mip1 != mip2; if (interpolate_mipmaps) { - dst2.create(p_width, p_height, false, format); + dst2.initialize_data(p_width, p_height, false, format); } bool had_mipmaps = mipmaps; @@ -2016,9 +2016,7 @@ Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, con uint8_t* wr = imgdata.ptrw(); memcpy(wr.ptr(), ptr, size); wr = uint8_t*(); - Ref<Image> im; - im.instantiate(); - im->create(w, h, false, format, imgdata); + Ref<Image> im = Image::create_from_data(w, h, false, format, imgdata); im->save_png("res://mipmap_" + itos(i) + ".png"); } #endif @@ -2051,7 +2049,25 @@ 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) { +Ref<Image> Image::create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { + Ref<Image> image; + image.instantiate(); + image->initialize_data(p_width, p_height, p_use_mipmaps, p_format); + return image; +} + +Ref<Image> Image::create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + Ref<Image> image; + image.instantiate(); + image->initialize_data(p_width, p_height, p_use_mipmaps, p_format, p_data); + return image; +} + +void Image::set_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data) { + initialize_data(p_width, p_height, p_use_mipmaps, p_format, p_data); +} + +void Image::initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { ERR_FAIL_COND_MSG(p_width <= 0, "The Image width specified (" + itos(p_width) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_height <= 0, "The Image height specified (" + itos(p_height) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, @@ -2077,7 +2093,7 @@ void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_forma 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) { +void Image::initialize_data(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, "The Image width specified (" + itos(p_width) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_height <= 0, "The Image height specified (" + itos(p_height) + " pixels) must be greater than 0 pixels."); ERR_FAIL_COND_MSG(p_width > MAX_WIDTH, @@ -2115,7 +2131,7 @@ void Image::create(int p_width, int p_height, bool p_use_mipmaps, Format p_forma mipmaps = p_use_mipmaps; } -void Image::create(const char **p_xpm) { +void Image::initialize_data(const char **p_xpm) { int size_width = 0; int size_height = 0; int pixelchars = 0; @@ -2230,7 +2246,7 @@ void Image::create(const char **p_xpm) { } if (line == colormap_size) { status = READING_PIXELS; - create(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8); + initialize_data(size_width, size_height, false, has_alpha ? FORMAT_RGBA8 : FORMAT_RGB8); data_write = data.ptrw(); pixel_size = has_alpha ? 4 : 3; } @@ -2559,7 +2575,7 @@ Image::Image(const char **p_xpm) { mipmaps = false; format = FORMAT_L8; - create(p_xpm); + initialize_data(p_xpm); } Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { @@ -2568,7 +2584,7 @@ Image::Image(int p_width, int p_height, bool p_use_mipmaps, Format p_format) { mipmaps = p_use_mipmaps; format = FORMAT_L8; - create(p_width, p_height, p_use_mipmaps, p_format); + initialize_data(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) { @@ -2577,7 +2593,7 @@ Image::Image(int p_width, int p_height, bool p_mipmaps, Format p_format, const V mipmaps = p_mipmaps; format = FORMAT_L8; - create(p_width, p_height, p_mipmaps, p_format, p_data); + initialize_data(p_width, p_height, p_mipmaps, p_format, p_data); } Rect2i Image::get_used_rect() const { @@ -2931,7 +2947,7 @@ void Image::_set_data(const Dictionary &p_data) { ERR_FAIL_COND(ddformat == FORMAT_MAX); - create(dwidth, dheight, dmipmaps, ddformat, ddata); + initialize_data(dwidth, dheight, dmipmaps, ddformat, ddata); } Dictionary Image::_get_data() const { @@ -3294,8 +3310,9 @@ void Image::_bind_methods() { 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_static_method("Image", D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::create_empty); + ClassDB::bind_static_method("Image", D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::create_from_data); + ClassDB::bind_method(D_METHOD("set_data", "width", "height", "use_mipmaps", "format", "data"), &Image::set_data); ClassDB::bind_method(D_METHOD("is_empty"), &Image::is_empty); @@ -3460,9 +3477,7 @@ Ref<Image> Image::rgbe_to_srgb() { ERR_FAIL_COND_V(format != FORMAT_RGBE9995, Ref<Image>()); - Ref<Image> new_image; - new_image.instantiate(); - new_image->create(width, height, false, Image::FORMAT_RGB8); + Ref<Image> new_image = create_empty(width, height, false, Image::FORMAT_RGB8); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { @@ -3869,15 +3884,15 @@ Dictionary Image::compute_image_metrics(const Ref<Image> p_compared_image, bool double image_metric_max, image_metric_mean, image_metric_mean_squared, image_metric_root_mean_squared, image_metric_peak_snr = 0.0; const bool average_component_error = true; - const uint32_t width = MIN(compared_image->get_width(), source_image->get_width()); - const uint32_t height = MIN(compared_image->get_height(), source_image->get_height()); + const uint32_t w = MIN(compared_image->get_width(), source_image->get_width()); + const uint32_t h = MIN(compared_image->get_height(), source_image->get_height()); // Histogram approach originally due to Charles Bloom. double hist[256]; memset(hist, 0, sizeof(hist)); - for (uint32_t y = 0; y < height; y++) { - for (uint32_t x = 0; x < width; x++) { + for (uint32_t y = 0; y < h; y++) { + for (uint32_t x = 0; x < w; x++) { const Color color_a = compared_image->get_pixel(x, y); const Color color_b = source_image->get_pixel(x, y); @@ -3922,7 +3937,7 @@ Dictionary Image::compute_image_metrics(const Ref<Image> p_compared_image, bool } // See http://richg42.blogspot.com/2016/09/how-to-compute-psnr-from-old-berkeley.html - double total_values = width * height; + double total_values = w * h; if (average_component_error) { total_values *= 4; diff --git a/core/io/image.h b/core/io/image.h index fd264a7a38..194ee05bc7 100644 --- a/core/io/image.h +++ b/core/io/image.h @@ -279,12 +279,12 @@ public: void normalize(); //for normal maps /** - * Create a new image of a given size and format. Current image will be lost + * Creates new internal image data 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 initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format); + void initialize_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data); + void initialize_data(const char **p_xpm); - void create(const char **p_xpm); /** * returns true when the image is empty (0,0) in size */ @@ -303,13 +303,9 @@ public: Error save_webp(const String &p_path, const bool p_lossy = false, const float p_quality = 0.75f) const; Vector<uint8_t> save_webp_to_buffer(const bool p_lossy = false, const float p_quality = 0.75f) const; - 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); - } + static Ref<Image> create_empty(int p_width, int p_height, bool p_use_mipmaps, Format p_format); + static Ref<Image> create_from_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data); + void set_data(int p_width, int p_height, bool p_use_mipmaps, Format p_format, const Vector<uint8_t> &p_data); /** * create an empty image diff --git a/core/io/image_loader.cpp b/core/io/image_loader.cpp index d6854666c0..397984a2ab 100644 --- a/core/io/image_loader.cpp +++ b/core/io/image_loader.cpp @@ -51,7 +51,7 @@ bool ImageFormatLoader::recognize(const String &p_extension) const { } Error ImageFormatLoaderExtension::load_image(Ref<Image> p_image, Ref<FileAccess> p_fileaccess, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) { - Error err; + Error err = ERR_UNAVAILABLE; if (GDVIRTUAL_CALL(_load_image, p_image, p_fileaccess, p_flags, p_scale, err)) { return err; } diff --git a/core/io/logger.cpp b/core/io/logger.cpp index b0f74f8db5..288e53d075 100644 --- a/core/io/logger.cpp +++ b/core/io/logger.cpp @@ -87,7 +87,7 @@ void Logger::log_error(const char *p_function, const char *p_file, int p_line, c } else { logf_error("USER %s: %s\n", err_type, err_details); } - logf_error(" at: %s (%s:%i) - %s\n", p_function, p_file, p_line, p_code); + logf_error(" at: %s (%s:%i)\n", p_function, p_file, p_line); } void Logger::logf(const char *p_format, ...) { diff --git a/core/io/marshalls.cpp b/core/io/marshalls.cpp index b24c49f58d..9ba653e1a9 100644 --- a/core/io/marshalls.cpp +++ b/core/io/marshalls.cpp @@ -503,7 +503,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int ERR_FAIL_COND_V((size_t)len < sizeof(double) * 16, ERR_INVALID_DATA); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - val.matrix[i][j] = decode_double(&buf[(i * 4 + j) * sizeof(double)]); + val.columns[i][j] = decode_double(&buf[(i * 4 + j) * sizeof(double)]); } } if (r_len) { @@ -513,7 +513,7 @@ Error decode_variant(Variant &r_variant, const uint8_t *p_buffer, int p_len, int ERR_FAIL_COND_V((size_t)len < sizeof(float) * 16, ERR_INVALID_DATA); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - val.matrix[i][j] = decode_float(&buf[(i * 4 + j) * sizeof(float)]); + val.columns[i][j] = decode_float(&buf[(i * 4 + j) * sizeof(float)]); } } @@ -1450,7 +1450,7 @@ Error encode_variant(const Variant &p_variant, uint8_t *r_buffer, int &r_len, bo Projection val = p_variant; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { - memcpy(&buf[(i * 4 + j) * sizeof(real_t)], &val.matrix[i][j], sizeof(real_t)); + memcpy(&buf[(i * 4 + j) * sizeof(real_t)], &val.columns[i][j], sizeof(real_t)); } } } diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 06649aba5b..4611528db7 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -107,7 +107,7 @@ void ResourceLoaderBinary::_advance_padding(uint32_t p_len) { static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) { if (f->real_is_double) { - if (sizeof(real_t) == 8) { + if constexpr (sizeof(real_t) == 8) { // Ideal case with double-precision f->get_buffer((uint8_t *)dst, count * sizeof(double)); #ifdef BIG_ENDIAN_ENABLED @@ -118,7 +118,7 @@ static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) { } } #endif - } else if (sizeof(real_t) == 4) { + } else if constexpr (sizeof(real_t) == 4) { // May be slower, but this is for compatibility. Eventually the data should be converted. for (size_t i = 0; i < count; ++i) { dst[i] = f->get_double(); @@ -127,7 +127,7 @@ static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) { ERR_FAIL_V_MSG(ERR_UNAVAILABLE, "real_t size is neither 4 nor 8!"); } } else { - if (sizeof(real_t) == 4) { + if constexpr (sizeof(real_t) == 4) { // Ideal case with float-precision f->get_buffer((uint8_t *)dst, count * sizeof(float)); #ifdef BIG_ENDIAN_ENABLED @@ -138,7 +138,7 @@ static Error read_reals(real_t *dst, Ref<FileAccess> &f, size_t count) { } } #endif - } else if (sizeof(real_t) == 8) { + } else if constexpr (sizeof(real_t) == 8) { for (size_t i = 0; i < count; ++i) { dst[i] = f->get_float(); } @@ -169,10 +169,10 @@ 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)); + uint32_t prop_type = f->get_32(); + print_bl("find property of type: " + itos(prop_type)); - switch (type) { + switch (prop_type) { case VARIANT_NIL: { r_v = Variant(); } break; @@ -327,22 +327,22 @@ Error ResourceLoaderBinary::parse_variant(Variant &r_v) { } break; case VARIANT_PROJECTION: { Projection v; - v.matrix[0].x = f->get_real(); - v.matrix[0].y = f->get_real(); - v.matrix[0].z = f->get_real(); - v.matrix[0].w = f->get_real(); - v.matrix[1].x = f->get_real(); - v.matrix[1].y = f->get_real(); - v.matrix[1].z = f->get_real(); - v.matrix[1].w = f->get_real(); - v.matrix[2].x = f->get_real(); - v.matrix[2].y = f->get_real(); - v.matrix[2].z = f->get_real(); - v.matrix[2].w = f->get_real(); - v.matrix[3].x = f->get_real(); - v.matrix[3].y = f->get_real(); - v.matrix[3].z = f->get_real(); - v.matrix[3].w = f->get_real(); + v.columns[0].x = f->get_real(); + v.columns[0].y = f->get_real(); + v.columns[0].z = f->get_real(); + v.columns[0].w = f->get_real(); + v.columns[1].x = f->get_real(); + v.columns[1].y = f->get_real(); + v.columns[1].z = f->get_real(); + v.columns[1].w = f->get_real(); + v.columns[2].x = f->get_real(); + v.columns[2].y = f->get_real(); + v.columns[2].z = f->get_real(); + v.columns[2].w = f->get_real(); + v.columns[3].x = f->get_real(); + v.columns[3].y = f->get_real(); + v.columns[3].z = f->get_real(); + v.columns[3].w = f->get_real(); r_v = v; } break; case VARIANT_COLOR: { @@ -1042,7 +1042,14 @@ void ResourceLoaderBinary::open(Ref<FileAccess> p_f, bool p_no_resources, bool p // If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path. er.path = ResourceUID::get_singleton()->get_id_path(er.uid); } else { +#ifdef TOOLS_ENABLED + // Silence a warning that can happen during the initial filesystem scan due to cache being regenerated. + if (ResourceLoader::get_resource_uid(res_path) != er.uid) { + WARN_PRINT(String(res_path + ": In external resource #" + itos(i) + ", invalid UUID: " + ResourceUID::get_singleton()->id_to_text(er.uid) + " - using text path instead: " + er.path).utf8().get_data()); + } +#else WARN_PRINT(String(res_path + ": In external resource #" + itos(i) + ", invalid UUID: " + ResourceUID::get_singleton()->id_to_text(er.uid) + " - using text path instead: " + er.path).utf8().get_data()); +#endif } } } @@ -1100,16 +1107,14 @@ String ResourceLoaderBinary::recognize(Ref<FileAccess> p_f) { uint32_t ver_major = f->get_32(); f->get_32(); // ver_minor - uint32_t ver_format = f->get_32(); + uint32_t ver_fmt = f->get_32(); - if (ver_format > FORMAT_VERSION || ver_major > VERSION_MAJOR) { + if (ver_fmt > FORMAT_VERSION || ver_major > VERSION_MAJOR) { f.unref(); return ""; } - String type = get_unicode_string(); - - return type; + return get_unicode_string(); } Ref<Resource> ResourceFormatLoaderBinary::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) { @@ -1630,22 +1635,22 @@ void ResourceFormatSaverBinaryInstance::write_variant(Ref<FileAccess> f, const V case Variant::PROJECTION: { f->store_32(VARIANT_PROJECTION); Projection val = p_property; - f->store_real(val.matrix[0].x); - f->store_real(val.matrix[0].y); - f->store_real(val.matrix[0].z); - f->store_real(val.matrix[0].w); - f->store_real(val.matrix[1].x); - f->store_real(val.matrix[1].y); - f->store_real(val.matrix[1].z); - f->store_real(val.matrix[1].w); - f->store_real(val.matrix[2].x); - f->store_real(val.matrix[2].y); - f->store_real(val.matrix[2].z); - f->store_real(val.matrix[2].w); - f->store_real(val.matrix[3].x); - f->store_real(val.matrix[3].y); - f->store_real(val.matrix[3].z); - f->store_real(val.matrix[3].w); + f->store_real(val.columns[0].x); + f->store_real(val.columns[0].y); + f->store_real(val.columns[0].z); + f->store_real(val.columns[0].w); + f->store_real(val.columns[1].x); + f->store_real(val.columns[1].y); + f->store_real(val.columns[1].z); + f->store_real(val.columns[1].w); + f->store_real(val.columns[2].x); + f->store_real(val.columns[2].y); + f->store_real(val.columns[2].z); + f->store_real(val.columns[2].w); + f->store_real(val.columns[3].x); + f->store_real(val.columns[3].y); + f->store_real(val.columns[3].z); + f->store_real(val.columns[3].w); } break; case Variant::COLOR: { @@ -2118,9 +2123,9 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const Ref<Re 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; - save_unicode_string(f, path); + String res_path = save_order[i]->get_path(); + res_path = relative_paths ? local_path.path_to_file(res_path) : res_path; + save_unicode_string(f, res_path); ResourceUID::ID ruid = ResourceSaver::get_resource_id_for_path(save_order[i]->get_path(), false); f->store_64(ruid); } diff --git a/core/io/resource_importer.cpp b/core/io/resource_importer.cpp index d923522317..564b37e662 100644 --- a/core/io/resource_importer.cpp +++ b/core/io/resource_importer.cpp @@ -110,8 +110,8 @@ Error ResourceFormatImporter::_get_path_and_type(const String &p_path, PathAndTy #ifdef TOOLS_ENABLED if (r_path_and_type.metadata && !r_path_and_type.path.is_empty()) { - Dictionary metadata = r_path_and_type.metadata; - if (metadata.has("has_editor_variant")) { + Dictionary meta = r_path_and_type.metadata; + if (meta.has("has_editor_variant")) { r_path_and_type.path = r_path_and_type.path.get_basename() + ".editor." + r_path_and_type.path.get_extension(); } } diff --git a/core/io/resource_loader.cpp b/core/io/resource_loader.cpp index eccb397e2e..2fb357b520 100644 --- a/core/io/resource_loader.cpp +++ b/core/io/resource_loader.cpp @@ -49,6 +49,11 @@ 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 { + bool ret = false; + if (GDVIRTUAL_CALL(_recognize_path, p_path, p_for_type, ret)) { + return ret; + } + String extension = p_path.get_extension(); List<String> extensions; @@ -68,7 +73,7 @@ bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_ } bool ResourceFormatLoader::handles_type(const String &p_type) const { - bool success; + bool success = false; if (GDVIRTUAL_CALL(_handles_type, p_type, success)) { return success; } @@ -102,7 +107,7 @@ String ResourceFormatLoader::get_resource_type(const String &p_path) const { } ResourceUID::ID ResourceFormatLoader::get_resource_uid(const String &p_path) const { - int64_t uid; + int64_t uid = ResourceUID::INVALID_ID; if (GDVIRTUAL_CALL(_get_resource_uid, p_path, uid)) { return uid; } @@ -123,7 +128,7 @@ void ResourceLoader::get_recognized_extensions_for_type(const String &p_type, Li } bool ResourceFormatLoader::exists(const String &p_path) const { - bool success; + bool success = false; if (GDVIRTUAL_CALL(_exists, p_path, success)) { return success; } @@ -175,7 +180,7 @@ Error ResourceFormatLoader::rename_dependencies(const String &p_path, const Hash deps_dict[E.key] = E.value; } - int64_t err; + int64_t err = OK; if (GDVIRTUAL_CALL(_rename_dependencies, p_path, deps_dict, err)) { return (Error)err; } @@ -189,6 +194,7 @@ void ResourceFormatLoader::_bind_methods() { BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE); GDVIRTUAL_BIND(_get_recognized_extensions); + GDVIRTUAL_BIND(_recognize_path, "path", "type"); GDVIRTUAL_BIND(_handles_type, "type"); GDVIRTUAL_BIND(_get_resource_type, "path"); GDVIRTUAL_BIND(_get_resource_uid, "path"); diff --git a/core/io/resource_loader.h b/core/io/resource_loader.h index 91ba930176..243670b2d0 100644 --- a/core/io/resource_loader.h +++ b/core/io/resource_loader.h @@ -51,6 +51,7 @@ protected: static void _bind_methods(); GDVIRTUAL0RC(Vector<String>, _get_recognized_extensions) + GDVIRTUAL2RC(bool, _recognize_path, String, StringName) GDVIRTUAL1RC(bool, _handles_type, StringName) GDVIRTUAL1RC(String, _get_resource_type, String) GDVIRTUAL1RC(ResourceUID::ID, _get_resource_uid, String) diff --git a/core/io/resource_saver.cpp b/core/io/resource_saver.cpp index 386ccb78e9..2f863baaac 100644 --- a/core/io/resource_saver.cpp +++ b/core/io/resource_saver.cpp @@ -42,7 +42,7 @@ ResourceSavedCallback ResourceSaver::save_callback = nullptr; ResourceSaverGetResourceIDForPath ResourceSaver::save_get_id_for_path = nullptr; Error ResourceFormatSaver::save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags) { - int64_t res; + int64_t res = ERR_METHOD_NOT_FOUND; if (GDVIRTUAL_CALL(_save, p_resource, p_path, p_flags, res)) { return (Error)res; } @@ -51,7 +51,7 @@ Error ResourceFormatSaver::save(const Ref<Resource> &p_resource, const String &p } bool ResourceFormatSaver::recognize(const Ref<Resource> &p_resource) const { - bool success; + bool success = false; if (GDVIRTUAL_CALL(_recognize, p_resource, success)) { return success; } diff --git a/core/io/resource_uid.cpp b/core/io/resource_uid.cpp index 5324c5dd84..ed5ce3b911 100644 --- a/core/io/resource_uid.cpp +++ b/core/io/resource_uid.cpp @@ -113,7 +113,12 @@ void ResourceUID::set_id(ID p_id, const String &p_path) { MutexLock l(mutex); ERR_FAIL_COND(!unique_ids.has(p_id)); CharString cs = p_path.utf8(); - if (strcmp(cs.ptr(), unique_ids[p_id].cs.ptr()) != 0) { + const char *update_ptr = cs.ptr(); + const char *cached_ptr = unique_ids[p_id].cs.ptr(); + if (update_ptr == nullptr && cached_ptr == nullptr) { + return; // Both are empty strings. + } + if ((update_ptr == nullptr) != (cached_ptr == nullptr) || strcmp(update_ptr, cached_ptr) != 0) { unique_ids[p_id].cs = cs; unique_ids[p_id].saved_to_cache = false; //changed changed = true; diff --git a/core/io/stream_peer_tcp.cpp b/core/io/stream_peer_tcp.cpp index ba79590c19..e035e1b613 100644 --- a/core/io/stream_peer_tcp.cpp +++ b/core/io/stream_peer_tcp.cpp @@ -256,9 +256,9 @@ void StreamPeerTCP::disconnect_from_host() { peer_port = 0; } -Error StreamPeerTCP::wait(NetSocket::PollType p_type, int timeout) { +Error StreamPeerTCP::wait(NetSocket::PollType p_type, int p_timeout) { ERR_FAIL_COND_V(_sock.is_null() || !_sock->is_open(), ERR_UNAVAILABLE); - return _sock->poll(p_type, timeout); + return _sock->poll(p_type, p_timeout); } Error StreamPeerTCP::put_data(const uint8_t *p_data, int p_bytes) { diff --git a/core/io/stream_peer_tcp.h b/core/io/stream_peer_tcp.h index 39c2e84346..778fb83374 100644 --- a/core/io/stream_peer_tcp.h +++ b/core/io/stream_peer_tcp.h @@ -79,7 +79,7 @@ public: Error poll(); // Wait or check for writable, readable. - Error wait(NetSocket::PollType p_type, int timeout = 0); + Error wait(NetSocket::PollType p_type, int p_timeout = 0); // Read/Write from StreamPeer Error put_data(const uint8_t *p_data, int p_bytes) override; diff --git a/core/io/zip_io.cpp b/core/io/zip_io.cpp index e573e8de19..200e5f5e83 100644 --- a/core/io/zip_io.cpp +++ b/core/io/zip_io.cpp @@ -37,11 +37,17 @@ void *zipio_open(voidpf opaque, const char *p_fname, int mode) { String fname; fname.parse_utf8(p_fname); + int file_access_mode = 0; if (mode & ZLIB_FILEFUNC_MODE_WRITE) { - (*fa) = FileAccess::open(fname, FileAccess::WRITE); - } else { - (*fa) = FileAccess::open(fname, FileAccess::READ); + file_access_mode |= FileAccess::WRITE; } + if (mode & ZLIB_FILEFUNC_MODE_READ) { + file_access_mode |= FileAccess::READ; + } + if (mode & ZLIB_FILEFUNC_MODE_CREATE) { + file_access_mode |= FileAccess::WRITE_READ; + } + (*fa) = FileAccess::open(fname, file_access_mode); if (fa->is_null()) { return nullptr; |