add simple interface to get assets from various locations

Former-commit-id: 2ee6c4ef811e935eb8684a2cd827a97b8ad0be7a
This commit is contained in:
Marcel Märtens 2019-04-13 03:40:59 +02:00
parent 29d6cb5127
commit 251cccffe5
6 changed files with 76 additions and 22 deletions

View File

@ -15,3 +15,5 @@ mio-extras = "2.0"
serde = "1.0"
serde_derive = "1.0"
bincode = "1.0"
log = "0.4"
pretty_env_logger = "0.3"

47
common/src/assets/mod.rs Normal file
View File

@ -0,0 +1,47 @@
use std::env;
use std::fs;
use std::io;
use std::io::prelude::*;
use std::fs::File;
fn try_load(name: &str) -> Option<File> {
let basepaths = [
// if it's stupid and it works..,
"assets".to_string(),
"../../assets".to_string(),
"../assets".to_string(), /* optimizations */
[env!("CARGO_MANIFEST_DIR"), "/assets"].concat(),
[env!("CARGO_MANIFEST_DIR"), "/../../assets"].concat(),
[env!("CARGO_MANIFEST_DIR"), "/../assets"].concat(),
"../../../assets".to_string(),
[env!("CARGO_MANIFEST_DIR"), "/../../../assets"].concat(),
];
for bp in &basepaths {
let filename = [bp, name].concat();
match File::open(&filename) {
Ok(f) => {
debug!("loading {} succedeed", filename);
return Some(f);
},
Err(e) => {
debug!("loading {} did not work with error: {}", filename, e);
}
};
};
return None;
}
pub fn load(name: &str) -> Result<Vec<u8>,()> {
return match try_load(name) {
Some(mut f) => {
let mut content: Vec<u8> = vec!();
f.read_to_end(&mut content);
info!("loaded asset successful: {}", name);
Ok(content)
},
None => {
warn!("Loading asset failed, wanted to load {} but could not load it, check debug log!", name);
Err(())
}
};
}

View File

@ -2,7 +2,10 @@
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate log;
pub mod assets;
pub mod clock;
pub mod comp;
pub mod figure;

View File

@ -5,6 +5,7 @@ use crate::{
ui::{ScaleMode, ToggleButton, Ui},
window::{Event as WinEvent, Key, Window},
};
use common::assets;
use conrod_core::{
color, Color,
image::Id as ImgId,
@ -245,10 +246,13 @@ pub(self) struct Imgs {
impl Imgs {
fn new(ui: &mut Ui, renderer: &mut Renderer) -> Imgs {
let mut load = |filename| {
let image = image::open(
&[env!("CARGO_MANIFEST_DIR"), "/../assets/voxygen/", filename].concat(),
)
.unwrap();
let fullpath: String = [
"/voxygen/",
filename,
].concat();
let image = image::load_from_memory(
assets::load(fullpath.as_str()).expect("Error loading file").as_slice()
).unwrap();
ui.new_image(renderer, &image).unwrap()
};
Imgs {

View File

@ -3,6 +3,7 @@ use crate::{
ui::{self, ScaleMode, Ui},
window::Window,
};
use common::assets;
use conrod_core::{
color,
color::TRANSPARENT,
@ -224,15 +225,13 @@ struct Imgs {
impl Imgs {
fn new(ui: &mut Ui, renderer: &mut Renderer) -> Imgs {
let mut load = |filename| {
let image = image::open(
&[
env!("CARGO_MANIFEST_DIR"),
"/../assets/voxygen/",
filename,
]
.concat(),
)
.unwrap();
let fullpath: String = [
"/voxygen/",
filename,
].concat();
let image = image::load_from_memory(
assets::load(fullpath.as_str()).expect("Error loading file").as_slice()
).unwrap();
ui.new_image(renderer, &image).unwrap()
};
Imgs {

View File

@ -3,6 +3,7 @@ use crate::{
ui::{self, ScaleMode, Ui},
window::Window,
};
use common::assets;
use conrod_core::{
color::TRANSPARENT,
image::Id as ImgId,
@ -57,15 +58,13 @@ impl Imgs {
fn new(ui: &mut Ui, renderer: &mut Renderer) -> Imgs {
// TODO: update paths
let mut load = |filename| {
let image = image::open(
&[
env!("CARGO_MANIFEST_DIR"),
"/../assets/voxygen/",
filename,
]
.concat(),
)
.unwrap();
let fullpath: String = [
"/voxygen/",
filename,
].concat();
let image = image::load_from_memory(
assets::load(fullpath.as_str()).expect("Error loading file").as_slice()
).unwrap();
ui.new_image(renderer, &image).unwrap()
};
Imgs {