From 1be7a62d630e6c5c18c6006cab1629a27a834f8e Mon Sep 17 00:00:00 2001 From: Imbris Date: Mon, 14 Jun 2021 21:42:50 -0400 Subject: [PATCH] Fallback to executable strategy for the userdata rather than panicking when USERDATA_STRATEGY isn't set and the executable is moved out of the project folder --- common/base/src/userdata_dir.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/common/base/src/userdata_dir.rs b/common/base/src/userdata_dir.rs index a038c348d7..e1fe66c479 100644 --- a/common/base/src/userdata_dir.rs +++ b/common/base/src/userdata_dir.rs @@ -1,4 +1,5 @@ use std::path::PathBuf; +use tracing::warn; const VELOREN_USERDATA_ENV: &str = "VELOREN_USERDATA"; @@ -52,14 +53,20 @@ pub fn userdata_dir(workspace: bool, strategy: Option<&str>, manifest_dir: &str) } let exe_path = std::env::current_exe() .expect("Failed to retrieve executable path!"); - // Ensure this path exists - // Ensure that the binary path is prefixed by this path - if !path.exists() || !exe_path.starts_with(&path) { - panic!("Recompile with VELOREN_USERDATA_STRATEGY set to \"system\" or \"executable\" to run the binary outside of the project folder"); + // If this path exists + // and the binary path is prefixed by this path + // put the userdata folder there + if path.exists() && exe_path.starts_with(&path) { + path.push("userdata"); + path + } else { + // otherwise warn and fallback to the executable strategy + warn!("This binary was moved to outside the project folder where it was compiled and was not compiled with VELOREN_USERDATA_STRATEGY set to \"system\" or \"executable\". Falling back the to the \"executable\" strategy (the userdata folder will be placed in the same folder as the executable)"); + let mut path = exe_path; + path.pop(); + path.push("userdata"); + path } - - path.push("userdata"); - path }) }