From f384959fc6a043648e0c9538a3a974f68f2c5336 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Fri, 28 Feb 2025 06:48:19 +0100 Subject: [PATCH] don't crash on missing resource base path --- src/elements/button.rs | 5 ++++- src/gui_handler/gui_handler.rs | 36 ++++++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/elements/button.rs b/src/elements/button.rs index 56ff62d..d4f8932 100644 --- a/src/elements/button.rs +++ b/src/elements/button.rs @@ -404,7 +404,10 @@ impl Button { .clone() .map(|icon| { Image::from_file(AssetPath::from(( - gui_handler.resource_base_path().full_path(), + gui_handler + .resource_base_path() + .ok_or(anyhow!("resource base path not set!"))? + .full_path(), icon, )))? .attach_sampler(Sampler::nearest_sampler().build(gui_handler.device())?) diff --git a/src/gui_handler/gui_handler.rs b/src/gui_handler/gui_handler.rs index 395236f..ce9483d 100644 --- a/src/gui_handler/gui_handler.rs +++ b/src/gui_handler/gui_handler.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use anyhow::Result; +use anyhow::{Result, anyhow}; use assetpath::AssetPath; use serde::{Deserialize, Serialize}; use utilities::{impl_reprc, prelude::*}; @@ -189,7 +189,7 @@ pub struct GuiHandler { width: AtomicU32, height: AtomicU32, - resource_base_path: AssetPath, + resource_base_path: Option, top_ui: RwLock>>, tooltip_ui: RwLock>>, @@ -330,9 +330,7 @@ impl GuiHandler { hover_sound }), - resource_base_path: gui_handler_create_info - .resource_directory - .expect("missing resource directory"), + resource_base_path: gui_handler_create_info.resource_directory, top_ui: RwLock::new(None), tooltip_ui: RwLock::new(None), @@ -437,7 +435,13 @@ impl GuiHandler { Some(image) => Ok(image), None => { if !path.has_prefix() { - path.set_prefix(&self.resource_base_path().full_path()); + path.set_prefix( + &self + .resource_base_path + .as_ref() + .ok_or(anyhow!("resource base path not set!"))? + .full_path(), + ); } let image = Image::from_file(path)? @@ -452,7 +456,13 @@ impl GuiHandler { }, None => { if !path.has_prefix() { - path.set_prefix(&self.resource_base_path().full_path()); + path.set_prefix( + &self + .resource_base_path + .as_ref() + .ok_or(anyhow!("resource base path not set!"))? + .full_path(), + ); } let image = Image::from_file(path.clone())? @@ -499,7 +509,13 @@ impl GuiHandler { pub(crate) fn displayable_image_from_path(&self, mut path: AssetPath) -> Result> { if !path.has_prefix() { - path.set_prefix(&self.resource_base_path.full_path()); + path.set_prefix( + &self + .resource_base_path + .as_ref() + .ok_or(anyhow!("resource base path not set!"))? + .full_path(), + ); } if self.internal_textures.read().unwrap().contains_key(&path) { @@ -571,8 +587,8 @@ impl GuiHandler { *self.ortho.read().unwrap() } - pub(crate) fn resource_base_path(&self) -> &AssetPath { - &self.resource_base_path + pub(crate) fn resource_base_path(&self) -> Option<&AssetPath> { + self.resource_base_path.as_ref() } #[cfg(feature = "audio")]