From 9381acb6a42da653cb6dfd9e610dfccead11aa98 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Wed, 23 Mar 2022 11:08:58 +0200 Subject: Make FileAccess and DirAccess classes reference counted. --- platform/uwp/export/app_packager.cpp | 49 ++++++++++++++--------------------- platform/uwp/export/app_packager.h | 4 +-- platform/uwp/export/export_plugin.cpp | 5 ++-- platform/uwp/export/export_plugin.h | 4 +-- 4 files changed, 24 insertions(+), 38 deletions(-) (limited to 'platform/uwp') diff --git a/platform/uwp/export/app_packager.cpp b/platform/uwp/export/app_packager.cpp index e5a1e951e4..8d09a16653 100644 --- a/platform/uwp/export/app_packager.cpp +++ b/platform/uwp/export/app_packager.cpp @@ -46,7 +46,7 @@ String AppxPackager::hash_block(const uint8_t *p_block_data, size_t p_block_len) } void AppxPackager::make_block_map(const String &p_path) { - FileAccess *tmp_file = FileAccess::open(p_path, FileAccess::WRITE); + Ref tmp_file = FileAccess::open(p_path, FileAccess::WRITE); tmp_file->store_string(""); tmp_file->store_string(""); @@ -69,9 +69,6 @@ void AppxPackager::make_block_map(const String &p_path) { } tmp_file->store_string(""); - - tmp_file->close(); - memdelete(tmp_file); } String AppxPackager::content_type(String p_extension) { @@ -89,7 +86,7 @@ String AppxPackager::content_type(String p_extension) { } void AppxPackager::make_content_types(const String &p_path) { - FileAccess *tmp_file = FileAccess::open(p_path, FileAccess::WRITE); + Ref tmp_file = FileAccess::open(p_path, FileAccess::WRITE); tmp_file->store_string(""); tmp_file->store_string(""); @@ -118,9 +115,6 @@ void AppxPackager::make_content_types(const String &p_path) { tmp_file->store_string(""); tmp_file->store_string(""); - - tmp_file->close(); - memdelete(tmp_file); } Vector AppxPackager::make_file_header(FileMeta p_file_meta) { @@ -285,7 +279,7 @@ Vector AppxPackager::make_end_of_central_record() { return buf; } -void AppxPackager::init(FileAccess *p_fa) { +void AppxPackager::init(Ref p_fa) { package = p_fa; central_dir_offset = 0; end_of_central_dir_offset = 0; @@ -308,7 +302,6 @@ Error AppxPackager::add_file(String p_file_name, const uint8_t *p_buffer, size_t // Data for compression z_stream strm; - FileAccess *strm_f = nullptr; Vector strm_in; strm_in.resize(BLOCK_SIZE); Vector strm_out; @@ -316,7 +309,7 @@ Error AppxPackager::add_file(String p_file_name, const uint8_t *p_buffer, size_t if (p_compress) { strm.zalloc = zipio_alloc; strm.zfree = zipio_free; - strm.opaque = &strm_f; + strm.opaque = Z_NULL; strm_out.resize(BLOCK_SIZE + 8); @@ -418,16 +411,15 @@ void AppxPackager::finish() { const String &tmp_blockmap_file_path = EditorPaths::get_singleton()->get_cache_dir().plus_file("tmpblockmap.xml"); make_block_map(tmp_blockmap_file_path); - FileAccess *blockmap_file = FileAccess::open(tmp_blockmap_file_path, FileAccess::READ); - Vector blockmap_buffer; - blockmap_buffer.resize(blockmap_file->get_length()); - - blockmap_file->get_buffer(blockmap_buffer.ptrw(), blockmap_buffer.size()); + { + Ref blockmap_file = FileAccess::open(tmp_blockmap_file_path, FileAccess::READ); + Vector blockmap_buffer; + blockmap_buffer.resize(blockmap_file->get_length()); - add_file("AppxBlockMap.xml", blockmap_buffer.ptr(), blockmap_buffer.size(), -1, -1, true); + blockmap_file->get_buffer(blockmap_buffer.ptrw(), blockmap_buffer.size()); - blockmap_file->close(); - memdelete(blockmap_file); + add_file("AppxBlockMap.xml", blockmap_buffer.ptr(), blockmap_buffer.size(), -1, -1, true); + } // Add content types @@ -436,16 +428,15 @@ void AppxPackager::finish() { const String &tmp_content_types_file_path = EditorPaths::get_singleton()->get_cache_dir().plus_file("tmpcontenttypes.xml"); make_content_types(tmp_content_types_file_path); - FileAccess *types_file = FileAccess::open(tmp_content_types_file_path, FileAccess::READ); - Vector types_buffer; - types_buffer.resize(types_file->get_length()); - - types_file->get_buffer(types_buffer.ptrw(), types_buffer.size()); + { + Ref types_file = FileAccess::open(tmp_content_types_file_path, FileAccess::READ); + Vector types_buffer; + types_buffer.resize(types_file->get_length()); - add_file("[Content_Types].xml", types_buffer.ptr(), types_buffer.size(), -1, -1, true); + types_file->get_buffer(types_buffer.ptrw(), types_buffer.size()); - types_file->close(); - memdelete(types_file); + add_file("[Content_Types].xml", types_buffer.ptr(), types_buffer.size(), -1, -1, true); + } // Cleanup generated files. DirAccess::remove_file_or_error(tmp_blockmap_file_path); @@ -466,9 +457,7 @@ void AppxPackager::finish() { Vector end_record = make_end_of_central_record(); package->store_buffer(end_record.ptr(), end_record.size()); - package->close(); - memdelete(package); - package = nullptr; + package = Ref(); } AppxPackager::AppxPackager() {} diff --git a/platform/uwp/export/app_packager.h b/platform/uwp/export/app_packager.h index da118449c7..a32b78bf04 100644 --- a/platform/uwp/export/app_packager.h +++ b/platform/uwp/export/app_packager.h @@ -87,7 +87,7 @@ class AppxPackager { }; String progress_task; - FileAccess *package = nullptr; + Ref package; Set mime_types; @@ -138,7 +138,7 @@ class AppxPackager { public: void set_progress_task(String p_task) { progress_task = p_task; } - void init(FileAccess *p_fa); + void init(Ref p_fa); Error add_file(String p_file_name, const uint8_t *p_buffer, size_t p_len, int p_file_no, int p_total_files, bool p_compress = false); void finish(); diff --git a/platform/uwp/export/export_plugin.cpp b/platform/uwp/export/export_plugin.cpp index 375e860f5a..7e06bf01e3 100644 --- a/platform/uwp/export/export_plugin.cpp +++ b/platform/uwp/export/export_plugin.cpp @@ -295,14 +295,13 @@ Error EditorExportPlatformUWP::export_project(const Ref &p_p Error err = OK; - FileAccess *fa_pack = FileAccess::open(p_path, FileAccess::WRITE, &err); + Ref fa_pack = FileAccess::open(p_path, FileAccess::WRITE, &err); ERR_FAIL_COND_V_MSG(err != OK, ERR_CANT_CREATE, "Cannot create file '" + p_path + "'."); AppxPackager packager; packager.init(fa_pack); - FileAccess *src_f = nullptr; - zlib_filefunc_def io = zipio_create_io_from_file(&src_f); + zlib_filefunc_def io = zipio_create_io(); if (ep.step("Creating package...", 0)) { return ERR_SKIP; diff --git a/platform/uwp/export/export_plugin.h b/platform/uwp/export/export_plugin.h index e2a4314ef5..7f10b00dc1 100644 --- a/platform/uwp/export/export_plugin.h +++ b/platform/uwp/export/export_plugin.h @@ -346,7 +346,7 @@ class EditorExportPlatformUWP : public EditorExportPlatform { ERR_FAIL_V_MSG(data, err_string); } - FileAccess *f = FileAccess::open(tmp_path, FileAccess::READ, &err); + Ref f = FileAccess::open(tmp_path, FileAccess::READ, &err); if (err != OK) { String err_string = "Couldn't open temp logo file."; @@ -359,8 +359,6 @@ class EditorExportPlatformUWP : public EditorExportPlatform { data.resize(f->get_length()); f->get_buffer(data.ptrw(), data.size()); - f->close(); - memdelete(f); DirAccess::remove_file_or_error(tmp_path); return data; -- cgit v1.2.3