uidev: support external paths
This commit is contained in:
@@ -20,7 +20,11 @@ pub struct TestbedAny {
|
||||
|
||||
impl TestbedAny {
|
||||
pub fn new(name: &str) -> anyhow::Result<Self> {
|
||||
let path = AssetPath::BuiltIn(&format!("gui/{name}.xml"));
|
||||
let path = if name.ends_with(".xml") {
|
||||
AssetPath::Filesystem(name)
|
||||
} else {
|
||||
AssetPath::BuiltIn(&format!("gui/{name}.xml"))
|
||||
};
|
||||
|
||||
let globals = WguiGlobals::new(
|
||||
Box::new(assets::Asset {}),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use flate2::read::GzDecoder;
|
||||
use std::ffi::OsStr;
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
@@ -96,6 +97,9 @@ pub fn normalize_path(path: &Path) -> PathBuf {
|
||||
std::path::Component::Normal(name) => {
|
||||
stack.push(name);
|
||||
}
|
||||
std::path::Component::RootDir => {
|
||||
stack.push(OsStr::new(std::path::MAIN_SEPARATOR_STR));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,13 +66,20 @@ impl WguiGlobals {
|
||||
AssetPath::WguiInternal(path) => self.assets_internal().load_from_path(path),
|
||||
AssetPath::BuiltIn(path) => self.assets_builtin().load_from_path(path),
|
||||
AssetPath::Filesystem(path) => {
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
let mut file = match std::fs::File::open(path) {
|
||||
Ok(f) => f,
|
||||
Err(e) => {
|
||||
anyhow::bail!("Could not open asset from {path}: {e}");
|
||||
}
|
||||
};
|
||||
/* 16 MiB safeguard */
|
||||
if file.metadata()?.len() > 16 * 1024 * 1024 {
|
||||
anyhow::bail!("Too large file size");
|
||||
anyhow::bail!("Could not open asset from {path}: Over size limit (16MiB)");
|
||||
}
|
||||
let mut data = Vec::new();
|
||||
file.read_to_end(&mut data)?;
|
||||
if let Err(e) = file.read_to_end(&mut data) {
|
||||
anyhow::bail!("Could not read asset from {path}: {e}");
|
||||
}
|
||||
Ok(data)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user