universal ascii-> binary conversion for native resources

This commit is contained in:
Dmitry
2025-10-19 12:04:47 +03:00
parent 6cc0c2e89b
commit 1efe57c471
3 changed files with 57 additions and 93 deletions
+2 -51
View File
@@ -29,18 +29,12 @@ use crate::{
manager::ResourceManager,
options::{try_get_import_settings, try_get_import_settings_opaque, BaseImportOptions},
},
core::{
io::FileError,
platform::TargetPlatform,
uuid::Uuid,
visitor::{Format, Visitor},
TypeUuidProvider,
},
core::{uuid::Uuid, TypeUuidProvider},
engine::SerializationContext,
resource::model::{Model, ModelImportOptions},
};
use fyrox_resource::state::LoadError;
use std::{future::Future, path::PathBuf, pin::Pin, sync::Arc};
use std::{path::PathBuf, sync::Arc};
/// Default implementation for model loading.
pub struct ModelLoader {
@@ -92,49 +86,6 @@ impl ResourceLoader for ModelLoader {
})
}
fn convert(
&self,
src_path: PathBuf,
dest_path: PathBuf,
_platform: TargetPlatform,
io: Arc<dyn ResourceIo>,
) -> Pin<Box<dyn Future<Output = Result<(), FileError>>>> {
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,
+54 -1
View File
@@ -29,6 +29,7 @@ use crate::{
};
use fyrox_core::io::FileError;
use fyrox_core::platform::TargetPlatform;
use fyrox_core::visitor::{Format, Visitor};
use std::{
any::Any,
future::Future,
@@ -49,6 +50,52 @@ pub trait BaseResourceLoader: Any + Send {}
#[cfg(not(target_arch = "wasm32"))]
impl<T: Any + Send> BaseResourceLoader for T {}
fn convert_ascii_to_binary<F>(
src_path: PathBuf,
dest_path: PathBuf,
is_native_extension: F,
_platform: TargetPlatform,
io: Arc<dyn ResourceIo>,
) -> Pin<Box<dyn Future<Output = Result<(), FileError>>>>
where
F: Fn(&str) -> bool,
{
if src_path
.extension()
.and_then(|src_ext| src_ext.to_str())
.is_some_and(is_native_extension)
{
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 {
Box::pin(async move { io.copy_file(&src_path, &dest_path).await })
}
}
/// Trait for resource loading.
pub trait ResourceLoader: BaseResourceLoader {
/// Returns a list of file extensions supported by the loader. Resource manager will use this list
@@ -87,7 +134,13 @@ pub trait ResourceLoader: BaseResourceLoader {
#[allow(unused_variables)] platform: TargetPlatform,
io: Arc<dyn ResourceIo>,
) -> Pin<Box<dyn Future<Output = Result<(), FileError>>>> {
Box::pin(async move { io.copy_file(&src_path, &dest_path).await })
convert_ascii_to_binary(
src_path,
dest_path,
|ext| self.is_native_extension(ext),
platform,
io,
)
}
/// Tries to load import settings for a resource.
+1 -41
View File
@@ -25,18 +25,13 @@ use crate::{
core::{uuid::Uuid, TypeUuidProvider},
UserInterface,
};
use fyrox_core::{
io::FileError,
platform::TargetPlatform,
visitor::{Format, Visitor},
};
use fyrox_resource::{
io::ResourceIo,
loader::{BoxedLoaderFuture, LoaderPayload, ResourceLoader},
manager::ResourceManager,
state::LoadError,
};
use std::{future::Future, path::PathBuf, pin::Pin, sync::Arc};
use std::{path::PathBuf, sync::Arc};
/// Default implementation for UI loading.
pub struct UserInterfaceLoader {
@@ -71,39 +66,4 @@ impl ResourceLoader for UserInterfaceLoader {
Ok(LoaderPayload::new(ui))
})
}
fn convert(
&self,
src_path: PathBuf,
dest_path: PathBuf,
_platform: TargetPlatform,
io: Arc<dyn ResourceIo>,
) -> Pin<Box<dyn Future<Output = Result<(), FileError>>>> {
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(())
}
}
})
}
}