use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; use tauri::{AppHandle, Manager}; use std::{path::{Path, PathBuf}, sync::OnceLock}; use crate::model::misc::Game; #[cfg(not(target_os = "windows"))] static NAME: &str = "startliner"; #[cfg(target_os = "windows")] static NAME: &str = "STARTLINER"; #[derive(Clone, Serialize, Deserialize)] pub struct Dirs { config_dir: PathBuf, data_dir: PathBuf, cache_dir: PathBuf, } static DIRS: OnceLock = OnceLock::new(); pub fn init_dirs(apph: &AppHandle) { DIRS.get_or_init(|| { if cfg!(windows) { Dirs { config_dir: apph.path().data_dir().expect("Unable to set project directories").join(NAME), data_dir: apph.path().cache_dir().expect("Unable to set project directories").join(NAME).join("data"), cache_dir: apph.path().cache_dir().expect("Unable to set project directories").join(NAME).join("cache"), } } else { Dirs { config_dir: apph.path().config_dir().expect("Unable to set project directories").join(NAME), data_dir: apph.path().data_dir().expect("Unable to set project directories").join(NAME), cache_dir: apph.path().cache_dir().expect("Unable to set project directories").join(NAME), } } }); } pub fn all_dirs() -> &'static Dirs { &DIRS.get().expect("Directories uninitialized") } pub fn config_dir() -> &'static Path { &DIRS.get().expect("Directories uninitialized").config_dir } pub fn profile_config_dir(game: &Game, name: &str) -> PathBuf { config_dir().join(format!("profile-{}-{}", game, name)) } pub fn data_dir() -> &'static Path { &DIRS.get().expect("Directories uninitialized").data_dir } pub fn cache_dir() -> &'static Path { &DIRS.get().expect("Directories uninitialized").cache_dir } pub fn pkg_dir() -> PathBuf { data_dir().join("pkg") } pub fn pkg_dir_of(namespace: &str, name: &str) -> PathBuf { pkg_dir().join(format!("{}-{}", namespace, name)) } pub fn copy_directory(src: impl AsRef, dst: impl AsRef, recursive: bool) -> std::io::Result<()> { std::fs::create_dir_all(dst.as_ref()).unwrap(); for entry in std::fs::read_dir(src.as_ref())? { let entry = entry?; let meta = entry.metadata()?; if meta.is_dir() { if recursive == true { copy_directory(&entry.path(), &dst.as_ref().join(entry.file_name()), true)?; } else { log::warn!("Skipping directory {:?}", meta); } } else { std::fs::copy(&entry.path(), &dst.as_ref().join(entry.file_name()))?; } } Ok(()) } #[cfg(target_os = "linux")] pub async fn symlink(src: impl AsRef, dst: impl AsRef) -> std::io::Result<()> { fs::symlink(src, dst).await } #[cfg(target_os = "windows")] pub async fn symlink(src: impl AsRef, dst: impl AsRef) -> std::io::Result<()> { //std::os::windows::fs::junction_point(src, dst) // is unstable junction::create(src, dst) } #[cfg(target_os = "windows")] pub static CREATE_NO_WINDOW: u32 = 0x08000000; #[cfg(target_os = "windows")] pub async fn pkill(process_name: &str) { use tokio::process::Command; _ = Command::new("taskkill.exe").arg("/f").arg("/im").arg(process_name) .creation_flags(CREATE_NO_WINDOW).output().await; } #[cfg(target_os = "linux")] pub async fn pkill(process_name: &str) { _ = Command::new("pkill").arg(process_name) .output().await; } pub fn clean_up_opts(dir: impl AsRef) -> Result<()> { log::debug!("begin clean_up_opts"); if dir.as_ref().is_dir() { for entry in std::fs::read_dir(dir)? { let entry = entry?; let path = entry.path(); log::debug!("{:?}", path); if path.is_symlink() { std::fs::remove_dir(path)?; } else { log::error!("Not a symlink: {:?}", path); } } } log::debug!("end clean_up_opts"); Ok(()) } pub trait PathStr { fn stringify(&self) -> Result; } fn path_to_str(p: impl AsRef) -> Result { Ok(p.as_ref().to_str().ok_or_else(|| anyhow!("Invalid path: {:?}", p.as_ref()))?.to_owned()) } impl PathStr for Path { fn stringify(&self) -> Result { path_to_str(self) } } impl PathStr for PathBuf { fn stringify(&self) -> Result { path_to_str(&self) } }