From 7dd9df5e86f5c366231775c26e9ac92c7995857b Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 7 Aug 2022 21:26:40 +0200 Subject: [PATCH] Backport FileAccess and DirAccess usage. --- modules/gltf/gltf_document.cpp | 40 ++++++++++++++++++++------------- modules/gltf/gltf_document.h | 4 ++-- modules/gltf/register_types.cpp | 5 ++++- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index 290577446..a13d3dd81 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -262,7 +262,8 @@ Error GLTFDocument::_serialize_scenes(Ref state) { Error GLTFDocument::_parse_json(const String &p_path, Ref state) { Error err; - Ref f = FileAccess::open(p_path, FileAccess::READ, &err); + FileAccessRef f = FileAccess::open(p_path, FileAccess::READ, &err); + if (f.is_null()) { return err; } @@ -284,7 +285,7 @@ Error GLTFDocument::_parse_json(const String &p_path, Ref state) { return OK; } -Error GLTFDocument::_parse_glb(Ref f, Ref state) { +Error GLTFDocument::_parse_glb(FileAccess *f, Ref state) { ERR_FAIL_NULL_V(f, ERR_INVALID_PARAMETER); ERR_FAIL_NULL_V(state, ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(f->get_position() != 0, ERR_FILE_CANT_READ); @@ -711,7 +712,7 @@ Error GLTFDocument::_encode_buffer_glb(Ref state, const String &p_pat String filename = p_path.get_basename().get_file() + itos(i) + ".bin"; String path = p_path.get_base_dir() + "/" + filename; Error err; - Ref f = FileAccess::open(path, FileAccess::WRITE, &err); + FileAccessRef f = FileAccess::open(path, FileAccess::WRITE, &err); if (f.is_null()) { return err; } @@ -743,7 +744,7 @@ Error GLTFDocument::_encode_buffer_bins(Ref state, const String &p_pa String filename = p_path.get_basename().get_file() + itos(i) + ".bin"; String path = p_path.get_base_dir() + "/" + filename; Error err; - Ref f = FileAccess::open(path, FileAccess::WRITE, &err); + FileAccessRef f = FileAccess::open(path, FileAccess::WRITE, &err); if (f.is_null()) { return err; } @@ -3036,10 +3037,15 @@ Error GLTFDocument::_serialize_images(Ref state, const String &p_path String texture_dir = "textures"; String path = p_path.get_base_dir(); String new_texture_dir = path + "/" + texture_dir; - Ref da = DirAccess::open(path); + + DirAccess *da = DirAccess::open(path); + if (!da->dir_exists(new_texture_dir)) { da->make_dir(new_texture_dir); } + + memdelete(da); + image->save_png(new_texture_dir.plus_file(name)); d["uri"] = texture_dir.plus_file(name).uri_encode(); } @@ -6520,11 +6526,13 @@ void GLTFDocument::_convert_animation(Ref state, AnimationPlayer *ap, } } -Error GLTFDocument::_parse(Ref state, String p_path, Ref f, int p_bake_fps) { +Error GLTFDocument::_parse(Ref state, String p_path, FileAccess *f, int p_bake_fps) { Error err; - if (f.is_null()) { + + if (!f) { return FAILED; } + f->seek(0); uint32_t magic = f->get_32(); if (magic == 0x46546C67) { @@ -6637,7 +6645,7 @@ Error GLTFDocument::_serialize_file(Ref state, const String p_path) { if (p_path.to_lower().ends_with("glb")) { err = _encode_buffer_glb(state, p_path); ERR_FAIL_COND_V(err != OK, err); - Ref f = FileAccess::open(p_path, FileAccess::WRITE, &err); + FileAccessRef f = FileAccess::open(p_path, FileAccess::WRITE, &err); ERR_FAIL_COND_V(f.is_null(), FAILED); String json = Variant(state->json).to_json_string(); @@ -6678,7 +6686,7 @@ Error GLTFDocument::_serialize_file(Ref state, const String p_path) { } else { err = _encode_buffer_bins(state, p_path); ERR_FAIL_COND_V(err != OK, err); - Ref f = FileAccess::open(p_path, FileAccess::WRITE, &err); + FileAccessRef f = FileAccess::open(p_path, FileAccess::WRITE, &err); ERR_FAIL_COND_V(f.is_null(), FAILED); f->create(FileAccess::ACCESS_RESOURCES); @@ -6874,11 +6882,12 @@ Error GLTFDocument::append_from_buffer(PackedByteArray p_bytes, String p_base_pa state->use_named_skin_binds = p_flags & GLTF_IMPORT_USE_NAMED_SKIN_BINDS; state->discard_meshes_and_materials = p_flags & GLTF_IMPORT_DISCARD_MESHES_AND_MATERIALS; - Ref file_access; - file_access.instantiate(); - file_access->open_custom(p_bytes.ptr(), p_bytes.size()); + FileAccessMemory *fam = memnew(FileAccessMemory); + fam->open_custom(p_bytes.ptr(), p_bytes.size()); + FileAccessRef file_access = fam; + state->base_path = p_base_path.get_base_dir(); - err = _parse(state, state->base_path, file_access, p_bake_fps); + err = _parse(state, state->base_path, file_access.f, p_bake_fps); ERR_FAIL_COND_V(err != OK, err); for (int32_t ext_i = 0; ext_i < document_extensions.size(); ext_i++) { Ref ext = document_extensions[ext_i]; @@ -6886,6 +6895,7 @@ Error GLTFDocument::append_from_buffer(PackedByteArray p_bytes, String p_base_pa err = ext->import_post_parse(state); ERR_FAIL_COND_V(err != OK, err); } + return OK; } @@ -6989,7 +6999,7 @@ Error GLTFDocument::append_from_file(String p_path, Ref r_state, uint r_state->use_named_skin_binds = p_flags & GLTF_IMPORT_USE_NAMED_SKIN_BINDS; r_state->discard_meshes_and_materials = p_flags & GLTF_IMPORT_DISCARD_MESHES_AND_MATERIALS; Error err; - Ref f = FileAccess::open(p_path, FileAccess::READ, &err); + FileAccessRef f = FileAccess::open(p_path, FileAccess::READ, &err); ERR_FAIL_COND_V(err != OK, ERR_FILE_CANT_OPEN); ERR_FAIL_NULL_V(f, ERR_FILE_CANT_OPEN); String base_path = p_base_path; @@ -6997,7 +7007,7 @@ Error GLTFDocument::append_from_file(String p_path, Ref r_state, uint base_path = p_path.get_base_dir(); } r_state->base_path = base_path; - err = _parse(r_state, base_path, f, p_bake_fps); + err = _parse(r_state, base_path, f.f, p_bake_fps); ERR_FAIL_COND_V(err != OK, err); for (int32_t ext_i = 0; ext_i < document_extensions.size(); ext_i++) { Ref ext = document_extensions[ext_i]; diff --git a/modules/gltf/gltf_document.h b/modules/gltf/gltf_document.h index 88f6984d0..eb55808d9 100644 --- a/modules/gltf/gltf_document.h +++ b/modules/gltf/gltf_document.h @@ -99,7 +99,7 @@ private: Ref _get_texture(Ref state, const GLTFTextureIndex p_texture); Error _parse_json(const String &p_path, Ref state); - Error _parse_glb(Ref f, Ref state); + Error _parse_glb(FileAccess *f, Ref state); void _compute_node_heights(Ref state); Error _parse_buffers(Ref state, const String &p_base_path); Error _parse_buffer_views(Ref state); @@ -349,7 +349,7 @@ public: void _convert_animation(Ref state, AnimationPlayer *ap, String p_animation_track_name); Error _serialize(Ref state, const String &p_path); - Error _parse(Ref state, String p_path, Ref f, int p_bake_fps); + Error _parse(Ref state, String p_path, FileAccess *f, int p_bake_fps); }; #endif // GLTF_DOCUMENT_H diff --git a/modules/gltf/register_types.cpp b/modules/gltf/register_types.cpp index 791c2c0ee..a948d4460 100644 --- a/modules/gltf/register_types.cpp +++ b/modules/gltf/register_types.cpp @@ -95,7 +95,8 @@ static void _editor_init() { EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "filesystem/import/fbx/fbx2gltf_path", PROPERTY_HINT_GLOBAL_FILE)); if (fbx_enabled) { - Ref da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); + if (fbx2gltf_path.is_empty()) { WARN_PRINT("FBX file import is enabled, but no FBX2glTF path is configured. FBX files will not be imported."); } else if (!da->file_exists(fbx2gltf_path)) { @@ -105,6 +106,8 @@ static void _editor_init() { importer.instantiate(); ResourceImporterScene::add_importer(importer); } + + memdelete(da); } } #endif // TOOLS_ENABLED