Backport FileAccess and DirAccess usage.

This commit is contained in:
Relintai 2022-08-07 21:26:40 +02:00
parent e15630d7f2
commit 7dd9df5e86
3 changed files with 31 additions and 18 deletions

View File

@ -262,7 +262,8 @@ Error GLTFDocument::_serialize_scenes(Ref<GLTFState> state) {
Error GLTFDocument::_parse_json(const String &p_path, Ref<GLTFState> state) {
Error err;
Ref<FileAccess> 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<GLTFState> state) {
return OK;
}
Error GLTFDocument::_parse_glb(Ref<FileAccess> f, Ref<GLTFState> state) {
Error GLTFDocument::_parse_glb(FileAccess *f, Ref<GLTFState> 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<GLTFState> 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<FileAccess> 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<GLTFState> 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<FileAccess> 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<GLTFState> state, const String &p_path
String texture_dir = "textures";
String path = p_path.get_base_dir();
String new_texture_dir = path + "/" + texture_dir;
Ref<DirAccess> 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<GLTFState> state, AnimationPlayer *ap,
}
}
Error GLTFDocument::_parse(Ref<GLTFState> state, String p_path, Ref<FileAccess> f, int p_bake_fps) {
Error GLTFDocument::_parse(Ref<GLTFState> 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<GLTFState> 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<FileAccess> 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<GLTFState> state, const String p_path) {
} else {
err = _encode_buffer_bins(state, p_path);
ERR_FAIL_COND_V(err != OK, err);
Ref<FileAccess> 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<FileAccessMemory> 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<GLTFDocumentExtension> 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<GLTFState> 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<FileAccess> 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<GLTFState> 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<GLTFDocumentExtension> ext = document_extensions[ext_i];

View File

@ -99,7 +99,7 @@ private:
Ref<Texture2D> _get_texture(Ref<GLTFState> state,
const GLTFTextureIndex p_texture);
Error _parse_json(const String &p_path, Ref<GLTFState> state);
Error _parse_glb(Ref<FileAccess> f, Ref<GLTFState> state);
Error _parse_glb(FileAccess *f, Ref<GLTFState> state);
void _compute_node_heights(Ref<GLTFState> state);
Error _parse_buffers(Ref<GLTFState> state, const String &p_base_path);
Error _parse_buffer_views(Ref<GLTFState> state);
@ -349,7 +349,7 @@ public:
void _convert_animation(Ref<GLTFState> state, AnimationPlayer *ap,
String p_animation_track_name);
Error _serialize(Ref<GLTFState> state, const String &p_path);
Error _parse(Ref<GLTFState> state, String p_path, Ref<FileAccess> f, int p_bake_fps);
Error _parse(Ref<GLTFState> state, String p_path, FileAccess *f, int p_bake_fps);
};
#endif // GLTF_DOCUMENT_H

View File

@ -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<DirAccess> 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