From e83997bc08d3166d12d7b6345f884544bc364982 Mon Sep 17 00:00:00 2001 From: galister <22305755+galister@users.noreply.github.com> Date: Sat, 6 Dec 2025 12:01:51 +0900 Subject: [PATCH] uidev: support external paths --- uidev/src/testbed/testbed_any.rs | 6 +++++- wgui/src/assets.rs | 4 ++++ wgui/src/globals.rs | 13 ++++++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/uidev/src/testbed/testbed_any.rs b/uidev/src/testbed/testbed_any.rs index e4531325..68cd7086 100644 --- a/uidev/src/testbed/testbed_any.rs +++ b/uidev/src/testbed/testbed_any.rs @@ -20,7 +20,11 @@ pub struct TestbedAny { impl TestbedAny { pub fn new(name: &str) -> anyhow::Result { - 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 {}), diff --git a/wgui/src/assets.rs b/wgui/src/assets.rs index cdceac17..acf76594 100644 --- a/wgui/src/assets.rs +++ b/wgui/src/assets.rs @@ -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)); + } _ => {} } } diff --git a/wgui/src/globals.rs b/wgui/src/globals.rs index 20dd202c..3bcc752c 100644 --- a/wgui/src/globals.rs +++ b/wgui/src/globals.rs @@ -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) } }