mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Pass BufReader to Asset::load()
This commit is contained in:
@ -7,8 +7,7 @@ use std::{
|
|||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
env,
|
env,
|
||||||
fs::{read_dir, read_link, File, ReadDir},
|
fs::{read_dir, read_link, File, ReadDir},
|
||||||
io::BufReader,
|
io::{BufReader, Read},
|
||||||
io::Read,
|
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
sync::{Arc, RwLock},
|
sync::{Arc, RwLock},
|
||||||
};
|
};
|
||||||
@ -41,11 +40,13 @@ lazy_static! {
|
|||||||
/// Function used to load assets. Permits manipulating the loaded asset with a mapping function.
|
/// Function used to load assets. Permits manipulating the loaded asset with a mapping function.
|
||||||
/// Loaded assets are cached in a global singleton hashmap.
|
/// Loaded assets are cached in a global singleton hashmap.
|
||||||
/// Example usage:
|
/// Example usage:
|
||||||
/// ```no_run
|
/// ```no_run=
|
||||||
/// use image::DynamicImage;
|
/// use veloren_common::{assets, terrain::Structure};
|
||||||
/// use veloren_common::assets;
|
|
||||||
///
|
///
|
||||||
/// let my_image = assets::load::<DynamicImage>("core.ui.backgrounds.city").unwrap();
|
/// let my_tree_structure = assets::load_map(
|
||||||
|
/// "world/tree/oak_green/1.vox",
|
||||||
|
/// |s: Structure| s.with_center(Vec3::new(15, 18, 14)),
|
||||||
|
/// ).unwrap();
|
||||||
/// ```
|
/// ```
|
||||||
pub fn load_map<A: Asset + 'static, F: FnOnce(A) -> A>(
|
pub fn load_map<A: Asset + 'static, F: FnOnce(A) -> A>(
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
@ -55,7 +56,7 @@ pub fn load_map<A: Asset + 'static, F: FnOnce(A) -> A>(
|
|||||||
match assets_write.get(specifier) {
|
match assets_write.get(specifier) {
|
||||||
Some(asset) => Ok(Arc::clone(asset).downcast()?),
|
Some(asset) => Ok(Arc::clone(asset).downcast()?),
|
||||||
None => {
|
None => {
|
||||||
let asset = Arc::new(f(A::load(specifier)?));
|
let asset = Arc::new(f(A::load(load_from_path(specifier)?)?));
|
||||||
let clone = Arc::clone(&asset);
|
let clone = Arc::clone(&asset);
|
||||||
assets_write.insert(specifier.to_owned(), clone);
|
assets_write.insert(specifier.to_owned(), clone);
|
||||||
Ok(asset)
|
Ok(asset)
|
||||||
@ -92,28 +93,28 @@ pub fn load_expect<A: Asset + 'static>(specifier: &str) -> Arc<A> {
|
|||||||
|
|
||||||
/// Asset Trait
|
/// Asset Trait
|
||||||
pub trait Asset: Send + Sync + Sized {
|
pub trait Asset: Send + Sync + Sized {
|
||||||
fn load(specifier: &str) -> Result<Self, Error>;
|
fn load(buf_reader: BufReader<impl Read>) -> Result<Self, Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Asset for DynamicImage {
|
impl Asset for DynamicImage {
|
||||||
fn load(specifier: &str) -> Result<Self, Error> {
|
fn load(mut buf_reader: BufReader<impl Read>) -> Result<Self, Error> {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
load_from_path(specifier)?.read_to_end(&mut buf)?;
|
buf_reader.read_to_end(&mut buf)?;
|
||||||
Ok(image::load_from_memory(&buf).unwrap())
|
Ok(image::load_from_memory(&buf).unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Asset for DotVoxData {
|
impl Asset for DotVoxData {
|
||||||
fn load(specifier: &str) -> Result<Self, Error> {
|
fn load(mut buf_reader: BufReader<impl Read>) -> Result<Self, Error> {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
load_from_path(specifier)?.read_to_end(&mut buf)?;
|
buf_reader.read_to_end(&mut buf)?;
|
||||||
Ok(dot_vox::load_bytes(&buf).unwrap())
|
Ok(dot_vox::load_bytes(&buf).unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Asset for Value {
|
impl Asset for Value {
|
||||||
fn load(specifier: &str) -> Result<Self, Error> {
|
fn load(buf_reader: BufReader<impl Read>) -> Result<Self, Error> {
|
||||||
Ok(serde_json::from_reader(load_from_path(specifier)?).unwrap())
|
Ok(serde_json::from_reader(buf_reader).unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ use crate::{
|
|||||||
volumes::dyna::{Dyna, DynaErr},
|
volumes::dyna::{Dyna, DynaErr},
|
||||||
};
|
};
|
||||||
use dot_vox::DotVoxData;
|
use dot_vox::DotVoxData;
|
||||||
|
use std::io::{BufReader, Read};
|
||||||
use vek::*;
|
use vek::*;
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
@ -61,8 +62,8 @@ impl ReadVol for Structure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Asset for Structure {
|
impl Asset for Structure {
|
||||||
fn load(specifier: &str) -> Result<Self, assets::Error> {
|
fn load(buf_reader: BufReader<impl Read>) -> Result<Self, assets::Error> {
|
||||||
let dot_vox_data = DotVoxData::load(specifier)?;
|
let dot_vox_data = DotVoxData::load(buf_reader)?;
|
||||||
|
|
||||||
if let Some(model) = dot_vox_data.models.get(0) {
|
if let Some(model) = dot_vox_data.models.get(0) {
|
||||||
let palette = dot_vox_data
|
let palette = dot_vox_data
|
||||||
|
@ -41,9 +41,11 @@ use conrod_core::{
|
|||||||
use graphic::Id as GraphicId;
|
use graphic::Id as GraphicId;
|
||||||
use log::warn;
|
use log::warn;
|
||||||
use scale::Scale;
|
use scale::Scale;
|
||||||
use std::io::Read;
|
use std::{
|
||||||
use std::ops::Range;
|
io::{BufReader, Read},
|
||||||
use std::sync::Arc;
|
ops::Range,
|
||||||
|
sync::Arc,
|
||||||
|
};
|
||||||
use util::{linear_to_srgb, srgb_to_linear};
|
use util::{linear_to_srgb, srgb_to_linear};
|
||||||
use vek::*;
|
use vek::*;
|
||||||
|
|
||||||
@ -79,9 +81,9 @@ impl DrawCommand {
|
|||||||
|
|
||||||
pub struct Font(text::Font);
|
pub struct Font(text::Font);
|
||||||
impl assets::Asset for Font {
|
impl assets::Asset for Font {
|
||||||
fn load(specifier: &str) -> Result<Self, assets::Error> {
|
fn load(mut buf_reader: BufReader<impl Read>) -> Result<Self, assets::Error> {
|
||||||
let mut buf = Vec::new();
|
let mut buf = Vec::new();
|
||||||
assets::load_from_path(specifier)?.read_to_end(&mut buf)?;
|
buf_reader.read_to_end(&mut buf)?;
|
||||||
Ok(Font(text::Font::from_bytes(buf.clone()).unwrap()))
|
Ok(Font(text::Font::from_bytes(buf.clone()).unwrap()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user