cross-platform assets

This commit is contained in:
griffi-gh 2023-05-21 04:17:28 +02:00
parent df3791af17
commit 8aaf64904b
4 changed files with 25 additions and 3 deletions

20
kubi/src/filesystem.rs Normal file
View file

@ -0,0 +1,20 @@
use std::{fs::File, path::Path, io::{Read, Seek}};
use anyhow::Result;
pub trait ReadOnly: Read + Seek {}
impl<T: Read + Seek> ReadOnly for T {}
#[allow(unreachable_code)]
pub fn open_asset(path: &Path) -> Result<Box<dyn ReadOnly>> {
#[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)?))
}

View file

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

View file

@ -59,7 +59,7 @@ pub fn load_prefabs(
log::info!("Loading textures...");
storages.add_unique_non_send_sync(BlockTexturesPrefab(
load_texture2darray_prefab::<BlockTexture, _>(
"./assets/blocks/".into(),
"blocks".into(),
&renderer.display,
MipmapsOption::AutoGeneratedMipmaps
)

View file

@ -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,7 +21,7 @@ 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) = {