feat: new config format

This commit is contained in:
2025-03-13 23:26:00 +00:00
parent 48dc9ec4df
commit fd27000c05
30 changed files with 1447 additions and 833 deletions

View File

@ -3,6 +3,8 @@ 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";
@ -22,9 +24,9 @@ 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).join("cfg"),
data_dir: apph.path().data_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),
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 {
@ -44,6 +46,10 @@ 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
}
@ -60,11 +66,6 @@ pub fn pkg_dir_of(namespace: &str, name: &str) -> PathBuf {
pkg_dir().join(format!("{}-{}", namespace, name))
}
pub fn path_to_str(p: impl AsRef<Path>) -> Result<String> {
Ok(p.as_ref().to_str()
.ok_or_else(|| anyhow!("Invalid path: {}", p.as_ref().to_string_lossy()))?.to_owned())
}
pub fn copy_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
std::fs::create_dir_all(&dst).unwrap();
for entry in std::fs::read_dir(src)? {
@ -77,4 +78,70 @@ pub fn copy_recursive(src: &Path, dst: &Path) -> std::io::Result<()> {
}
}
Ok(())
}
#[cfg(target_os = "linux")]
pub async fn symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> std::io::Result<()> {
fs::symlink(src, dst).await
}
#[cfg(target_os = "windows")]
pub async fn symlink(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> 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<Path>) -> 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<String>;
}
fn path_to_str(p: impl AsRef<Path>) -> Result<String> {
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<String> {
path_to_str(self)
}
}
impl PathStr for PathBuf {
fn stringify(&self) -> Result<String> {
path_to_str(&self)
}
}