From ecb88da0ac45dd1f440af541e9c39cccd713eda5 Mon Sep 17 00:00:00 2001 From: Dmitry Stepanov Date: Sun, 27 Apr 2025 22:49:26 +0300 Subject: [PATCH] `ResourceLoader::convert` impl for native scenes - allows resaving ascii to binary --- fyrox-impl/src/resource/model/loader.rs | 53 ++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/fyrox-impl/src/resource/model/loader.rs b/fyrox-impl/src/resource/model/loader.rs index 48017e492..999883d68 100644 --- a/fyrox-impl/src/resource/model/loader.rs +++ b/fyrox-impl/src/resource/model/loader.rs @@ -29,12 +29,18 @@ use crate::{ manager::ResourceManager, options::{try_get_import_settings, try_get_import_settings_opaque, BaseImportOptions}, }, - core::{uuid::Uuid, TypeUuidProvider}, + core::{ + io::FileError, + platform::TargetPlatform, + uuid::Uuid, + visitor::{Format, Visitor}, + TypeUuidProvider, + }, engine::SerializationContext, resource::model::{Model, ModelImportOptions}, }; use fyrox_resource::state::LoadError; -use std::{path::PathBuf, sync::Arc}; +use std::{future::Future, path::PathBuf, pin::Pin, sync::Arc}; /// Default implementation for model loading. pub struct ModelLoader { @@ -82,6 +88,49 @@ impl ResourceLoader for ModelLoader { }) } + fn convert( + &self, + src_path: PathBuf, + dest_path: PathBuf, + _platform: TargetPlatform, + io: Arc, + ) -> Pin>>> { + if src_path.extension().is_some_and(|ext| { + fyrox_core::cmp_strings_case_insensitive(ext.to_string_lossy(), "rgs") + }) { + // Convert scenes to the binary format where possible. + Box::pin(async move { + let data = io.load_file(&src_path).await?; + match Visitor::detect_format_from_slice(&data) { + Format::Unknown => Err(FileError::Custom("Unknown format!".to_string())), + Format::Binary => { + // Copy the binary format as-is. + Ok(io.copy_file(&src_path, &dest_path).await?) + } + Format::Ascii => { + // Resave the ascii format as binary. + let visitor = Visitor::load_from_memory(&data).map_err(|err| { + FileError::Custom(format!( + "Unable to load {}. Reason: {err}", + src_path.display() + )) + })?; + visitor.save_binary_to_file(dest_path).map_err(|err| { + FileError::Custom(format!( + "Unable to save {}. Reason: {err}", + src_path.display() + )) + })?; + Ok(()) + } + } + }) + } else { + // FBX and other will be copied as is. + Box::pin(async move { io.copy_file(&src_path, &dest_path).await }) + } + } + fn try_load_import_settings( &self, resource_path: PathBuf,