From 8aaf64904bb9d6d708b56286b66dbbaec5fada24 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 21 May 2023 04:17:28 +0200 Subject: [PATCH] cross-platform assets --- kubi/src/filesystem.rs | 20 ++++++++++++++++++++ kubi/src/lib.rs | 1 + kubi/src/prefabs.rs | 2 +- kubi/src/prefabs/texture.rs | 5 +++-- 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 kubi/src/filesystem.rs diff --git a/kubi/src/filesystem.rs b/kubi/src/filesystem.rs new file mode 100644 index 0000000..33757f6 --- /dev/null +++ b/kubi/src/filesystem.rs @@ -0,0 +1,20 @@ +use std::{fs::File, path::Path, io::{Read, Seek}}; +use anyhow::Result; + +pub trait ReadOnly: Read + Seek {} +impl ReadOnly for T {} + +#[allow(unreachable_code)] +pub fn open_asset(path: &Path) -> Result> { + #[cfg(target_os = "android")] { + use anyhow::Context; + use std::ffi::CString; + + let asset_manager = ndk_glue::native_activity().asset_manager(); + let path_cstr = CString::new(path.to_string_lossy().as_bytes())?; + let handle = asset_manager.open(&path_cstr).context("Asset doesn't exist")?; + return Ok(Box::new(handle)); + } + let asset_path = Path::new("./assets/").join(path); + return Ok(Box::new(File::open(asset_path)?)) +} diff --git a/kubi/src/lib.rs b/kubi/src/lib.rs index 037f1df..c5f2596 100644 --- a/kubi/src/lib.rs +++ b/kubi/src/lib.rs @@ -38,6 +38,7 @@ pub(crate) mod color; pub(crate) mod loading_screen; pub(crate) mod connecting_screen; pub(crate) mod fixed_timestamp; +pub(crate) mod filesystem; use world::{ init_game_world, diff --git a/kubi/src/prefabs.rs b/kubi/src/prefabs.rs index a469cab..6802f54 100644 --- a/kubi/src/prefabs.rs +++ b/kubi/src/prefabs.rs @@ -59,7 +59,7 @@ pub fn load_prefabs( log::info!("Loading textures..."); storages.add_unique_non_send_sync(BlockTexturesPrefab( load_texture2darray_prefab::( - "./assets/blocks/".into(), + "blocks".into(), &renderer.display, MipmapsOption::AutoGeneratedMipmaps ) diff --git a/kubi/src/prefabs/texture.rs b/kubi/src/prefabs/texture.rs index 10a16ee..602903e 100644 --- a/kubi/src/prefabs/texture.rs +++ b/kubi/src/prefabs/texture.rs @@ -2,6 +2,7 @@ use strum::IntoEnumIterator; use rayon::prelude::*; use std::{fs::File, path::PathBuf, io::BufReader}; use glium::{texture::{SrgbTexture2dArray, RawImage2d, MipmapsOption}, backend::Facade}; +use crate::filesystem::open_asset; use super::AssetPaths; pub fn load_texture2darray_prefab< @@ -20,11 +21,11 @@ pub fn load_texture2darray_prefab< //Get path to the image and open the file let reader = { let path = directory.join(file_name); - BufReader::new(File::open(path).expect("Failed to open texture file")) + BufReader::new(open_asset(&path).expect("Failed to open texture file")) }; //Parse image data let (image_data, dimensions) = { - let image =image::load( + let image = image::load( reader, image::ImageFormat::Png ).unwrap().to_rgba8();