A GameCube/Wii emulator and debugger written in Rust.
path helper
Layle4e6371fe9f86
diff --git a/crates/app/src/app.rs b/crates/app/src/app.rsindex 04cfec7..4eef0d1 100644--- a/crates/app/src/app.rs+++ b/crates/app/src/app.rs@@ -924,7 +924,7 @@ fn resolve_palette(pref: ThemePreference, system_mode: Mode) -> Palette {}fn screenshot_path(game_id: &str) -> PathBuf {- config::exe_relative(capture::timestamped_path(game_id))+ capture::timestamped_path(game_id)}fn corner_card<'a>(palette: &Palette, content: Element<'a, Message>) -> Element<'a, Message> {diff --git a/crates/app/src/config.rs b/crates/app/src/config.rsindex b667eb9..0ba912c 100644--- a/crates/app/src/config.rs+++ b/crates/app/src/config.rs@@ -88,11 +88,7 @@ impl Config {}pub fn exe_relative(rel: impl AsRef<Path>) -> PathBuf {- let rel = rel.as_ref();- std::env::current_exe()- .ok()- .and_then(|exe| exe.parent().map(|p| p.join(rel)))- .unwrap_or_else(|| rel.to_path_buf())+ gecko::paths::resolve(rel)}pub fn config_path() -> PathBuf {diff --git a/crates/backend-wgpu/src/capture.rs b/crates/backend-wgpu/src/capture.rsindex ed271d9..51ffeaa 100644--- a/crates/backend-wgpu/src/capture.rs+++ b/crates/backend-wgpu/src/capture.rs@@ -175,5 +175,5 @@ pub fn timestamped_path(prefix: &str) -> PathBuf {.duration_since(UNIX_EPOCH).map(|d| d.as_millis()).unwrap_or(0);- PathBuf::from("screenshots").join(format!("{prefix}_{millis}.png"))+ gecko::paths::resolve("screenshots").join(format!("{prefix}_{millis}.png"))}diff --git a/crates/backend-wgpu/src/lib.rs b/crates/backend-wgpu/src/lib.rsindex 846d1d5..d9cb85c 100644--- a/crates/backend-wgpu/src/lib.rs+++ b/crates/backend-wgpu/src/lib.rs@@ -907,8 +907,7 @@ impl GxRenderer {EFB_SAMPLE_COUNT,);- let cache_path = std::path::Path::new(shader_specialization::SHADER_CACHE_PATH);- let cached_keys = shader_specialization::load_cached_keys(cache_path);+ let cached_keys = shader_specialization::load_cached_keys(&shader_specialization::shader_cache_path());let prewarmed = prewarm_shader_variants(device, &cached_keys);let mut shader_cache: FxHashMap<ShaderKey, wgpu::ShaderModule> =@@ -1271,16 +1270,16 @@ impl GxRenderer {}pub fn save_shader_cache(&self) -> std::io::Result<usize> {- let path = std::path::Path::new(shader_specialization::SHADER_CACHE_PATH);+ let path = shader_specialization::shader_cache_path();let keys: Vec<ShaderKey> = self.shader_cache.keys().copied().collect();- shader_specialization::save_keys(path, &keys)?;+ shader_specialization::save_keys(&path, &keys)?;Ok(keys.len())}pub fn save_pipeline_cache(&self) -> std::io::Result<usize> {- let path = std::path::Path::new(pipeline::PIPELINE_CACHE_PATH);+ let path = pipeline::pipeline_cache_path();let keys: Vec<pipeline::FullPipelineKey> = self.pipeline_cache.keys().copied().collect();- pipeline::save_pipeline_keys(path, &keys)?;+ pipeline::save_pipeline_keys(&path, &keys)?;Ok(keys.len())}@@ -1323,8 +1322,8 @@ impl GxRenderer {}pub fn prewarm_pipeline_cache(&mut self, device: &wgpu::Device) {- let path = std::path::Path::new(pipeline::PIPELINE_CACHE_PATH);- let keys: Vec<pipeline::FullPipelineKey> = pipeline::load_cached_pipeline_keys(path)+ let path = pipeline::pipeline_cache_path();+ let keys: Vec<pipeline::FullPipelineKey> = pipeline::load_cached_pipeline_keys(&path).into_iter().filter(|k| !self.pipeline_cache.contains_key(k) && self.shader_cache.contains_key(&k.shader)).collect();diff --git a/crates/backend-wgpu/src/pipeline.rs b/crates/backend-wgpu/src/pipeline.rsindex bf1c7a5..0a2c417 100644--- a/crates/backend-wgpu/src/pipeline.rs+++ b/crates/backend-wgpu/src/pipeline.rs@@ -131,7 +131,9 @@ impl From<FullPipelineKey> for UberPipelineKey {pub(crate) const FULL_PIPELINE_KEY_BYTES: usize = SHADER_KEY_BYTES + SPECIALIZATION_KEY_BYTES + PipelineKey::BYTES;const PIPELINE_CACHE_MAGIC: [u8; 4] = *b"GPKC";const PIPELINE_CACHE_VERSION: u32 = crate::shader_specialization::CACHE_VERSION;-pub(crate) const PIPELINE_CACHE_PATH: &str = "cache/pipeline_keys.bin";+pub(crate) fn pipeline_cache_path() -> std::path::PathBuf {+ gecko::paths::cache("pipeline_keys.bin")+}impl FullPipelineKey {fn to_bytes(self) -> [u8; FULL_PIPELINE_KEY_BYTES] {diff --git a/crates/backend-wgpu/src/shader_specialization.rs b/crates/backend-wgpu/src/shader_specialization.rsindex acf75e0..170cd48 100644--- a/crates/backend-wgpu/src/shader_specialization.rs+++ b/crates/backend-wgpu/src/shader_specialization.rs@@ -17,7 +17,9 @@ pub(crate) const KEY_BYTES: usize = 6;pub(crate) const SPECIALIZATION_KEY_BYTES: usize = 82 * size_of::<u32>();const CACHE_MAGIC: [u8; 4] = *b"GSKC";pub(crate) const CACHE_VERSION: u32 = 8;-pub(crate) const SHADER_CACHE_PATH: &str = "cache/shader_keys.bin";+pub(crate) fn shader_cache_path() -> std::path::PathBuf {+ gecko::paths::cache("shader_keys.bin")+}#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]pub(crate) struct ShaderKey {@@ -436,10 +438,10 @@ pub(crate) fn compile_variant(key: ShaderKey) -> String {.to_string();#[cfg(feature = "dump-wgsl")]{- let dir = "cache/wgsl";- let _ = std::fs::create_dir_all(dir);+ let dir = gecko::paths::cache("wgsl");+ let _ = std::fs::create_dir_all(&dir);let name: String = key.to_bytes().iter().map(|b| format!("{b:02X}")).collect();- let _ = std::fs::write(format!("{dir}/variant_{name}.wgsl"), &out);+ let _ = std::fs::write(dir.join(format!("variant_{name}.wgsl")), &out);}out}diff --git a/crates/gecko/src/jit/cache.rs b/crates/gecko/src/jit/cache.rsindex 577882e..60972c3 100644--- a/crates/gecko/src/jit/cache.rs+++ b/crates/gecko/src/jit/cache.rs@@ -76,7 +76,7 @@ pub fn dol_cache_id(data: &[u8]) -> String {}pub fn cache_dir(game_id: &str) -> PathBuf {- PathBuf::from("cache").join(game_id)+ crate::paths::cache(game_id)}pub fn ppc_cache_path(game_id: &str) -> PathBuf {diff --git a/crates/gecko/src/lib.rs b/crates/gecko/src/lib.rsindex 9593161..82743d3 100644--- a/crates/gecko/src/lib.rs+++ b/crates/gecko/src/lib.rs@@ -12,6 +12,7 @@ pub mod input;pub mod ipl;pub mod jit;pub mod mmio;+pub mod paths;#[cfg(any(feature = "jit-stats", feature = "profile", feature = "gx-stats"))]pub mod profile;pub mod savestate;diff --git a/crates/gecko/src/paths.rs b/crates/gecko/src/paths.rsnew file mode 100644index 0000000..172a29e--- /dev/null+++ b/crates/gecko/src/paths.rs@@ -0,0 +1,29 @@+use std::path::{Path, PathBuf};+use std::sync::OnceLock;++static BASE: OnceLock<PathBuf> = OnceLock::new();++pub fn set_base(dir: impl Into<PathBuf>) {+ let _ = BASE.set(dir.into());+}++pub fn base() -> &'static Path {+ BASE.get_or_init(|| {+ if let Some(custom) = std::env::var_os("GECKO_DATA_DIR") {+ return PathBuf::from(custom);+ }++ std::env::current_exe()+ .ok()+ .and_then(|exe| exe.parent().map(Path::to_path_buf))+ .unwrap_or_else(|| PathBuf::from("."))+ })+}++pub fn resolve(rel: impl AsRef<Path>) -> PathBuf {+ self::base().join(rel)+}++pub fn cache(rel: impl AsRef<Path>) -> PathBuf {+ self::resolve(Path::new("cache").join(rel))+}diff --git a/crates/gecko/src/starlet.rs b/crates/gecko/src/starlet.rsindex 35ec531..65aed4d 100644--- a/crates/gecko/src/starlet.rs+++ b/crates/gecko/src/starlet.rs@@ -173,13 +173,7 @@ fn default_host_fs_root() -> PathBuf {return PathBuf::from(custom);}- if let Ok(exe) = std::env::current_exe() {- if let Some(dir) = exe.parent() {- return dir.join("fs");- }- }-- PathBuf::from("fs")+ crate::paths::resolve("fs")}impl System<{ crate::WII }> {