Skip to content

Commit

Permalink
refactor: use dirs-next instead of tauri path
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMakesGames committed Mar 16, 2024
1 parent d09b116 commit b2ba628
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tauri-plugin-dialog = "2.0.0-beta.0"
tauri-plugin-fs = "2.0.0-beta.0"
logos = "0.14.0"
tauri-plugin-shell = "2.0.0-beta.2"
dirs-next = "2.0.0"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem and the built-in dev server is disabled.
Expand Down
48 changes: 17 additions & 31 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::path::Path;
use std::path::PathBuf;
use std::time::UNIX_EPOCH;
use steamlocate::SteamDir;
use tauri::Manager;
use dirs_next;
use zip;

mod lexer;
Expand All @@ -45,11 +45,9 @@ pub fn main() {

#[tauri::command]
async fn get_stellaris_colors_cmd(
app: tauri::AppHandle,
path: String,
) -> Result<Vec<String>, String> {
let paths = get_stellaris_data_paths(
&app,
Path::new(&path).to_path_buf(),
Path::new("flags").to_path_buf(),
FileFilter::Name(OsString::from("colors.txt")),
Expand All @@ -67,17 +65,15 @@ async fn get_stellaris_colors_cmd(

#[tauri::command]
async fn get_stellaris_loc_cmd(
app: tauri::AppHandle,
path: String,
) -> Result<HashMap<String, String>, String> {
return get_stellaris_loc(&app, path).map_err(|err| err.to_string());
return get_stellaris_loc(path).map_err(|err| err.to_string());
}

#[tauri::command]
async fn get_stellaris_save_metadata_cmd(
app: tauri::AppHandle,
) -> Result<Vec<Vec<StellarisSave>>, String> {
get_stellaris_save_metadata(&app).map_err(|err| err.to_string())
get_stellaris_save_metadata().map_err(|err| err.to_string())
}

#[tauri::command]
Expand All @@ -87,12 +83,11 @@ async fn get_stellaris_save_cmd(path: String, filter: Value) -> Result<Value, St

#[tauri::command]
async fn get_emblem_cmd(
app: tauri::AppHandle,
path: String,
category: String,
file: String,
) -> Result<String, String> {
return get_emblem(&app, Path::new(&path).to_path_buf(), category, file)
return get_emblem(Path::new(&path).to_path_buf(), category, file)
.map_err(|err| err.to_string());
}

Expand Down Expand Up @@ -130,23 +125,19 @@ fn get_stellaris_install_dir() -> anyhow::Result<PathBuf> {
}
}

fn get_stellaris_user_data_dir(app: &tauri::AppHandle) -> PathBuf {
fn get_stellaris_user_data_dir() -> PathBuf {
match env::consts::OS {
"linux" => {
let home_dir = env::var("HOME").unwrap();
return Path::new(home_dir.as_str()).join(".local/share/Paradox Interactive/Stellaris");
}
"macos" => {
return app
.path()
.document_dir()
return dirs_next::document_dir()
.unwrap()
.join("Paradox Interactive/Stellaris");
}
"windows" => {
return app
.path()
.document_dir()
return dirs_next::document_dir()
.unwrap()
.join("Paradox Interactive\\Stellaris");
}
Expand All @@ -160,10 +151,9 @@ fn get_steam_user_data_dirs() -> anyhow::Result<Vec<PathBuf>> {
}

fn get_mod_path(
app: &tauri::AppHandle,
enabled_mod: &serde_json::Value,
) -> anyhow::Result<PathBuf> {
let user_data_dir = get_stellaris_user_data_dir(&app);
let user_data_dir = get_stellaris_user_data_dir();
let enabled_mod_descriptor = user_data_dir.join(
enabled_mod
.as_str()
Expand All @@ -179,8 +169,8 @@ fn get_mod_path(
return Ok(Path::new(&mod_path).to_path_buf());
}

fn get_enabled_mod_dirs(app: &tauri::AppHandle) -> anyhow::Result<Vec<PathBuf>> {
let user_data_dir = get_stellaris_user_data_dir(app);
fn get_enabled_mod_dirs() -> anyhow::Result<Vec<PathBuf>> {
let user_data_dir = get_stellaris_user_data_dir();
let dlc_load = user_data_dir.join("dlc_load.json");
let dlc_load = fs::File::open(dlc_load)?;
let dlc_load: serde_json::Value = serde_json::from_reader(dlc_load)?;
Expand All @@ -192,17 +182,17 @@ fn get_enabled_mod_dirs(app: &tauri::AppHandle) -> anyhow::Result<Vec<PathBuf>>
.as_array()
.ok_or(anyhow::anyhow!("Expected enabled_mods to be string array"))?
{
match get_mod_path(app, enabled_mod) {
match get_mod_path(enabled_mod) {
Ok(mod_path) => mod_dirs.push(mod_path),
_ => (),
}
}
return Ok(mod_dirs);
}

fn get_stellaris_data_dirs(app: &tauri::AppHandle, install_path: PathBuf) -> Vec<PathBuf> {
fn get_stellaris_data_dirs(install_path: PathBuf) -> Vec<PathBuf> {
let mut dirs = vec![install_path];
match get_enabled_mod_dirs(app) {
match get_enabled_mod_dirs() {
Ok(mod_dirs) => {
for dir in mod_dirs {
dirs.push(dir);
Expand Down Expand Up @@ -327,13 +317,13 @@ impl StellarisSave {
}
}

fn get_stellaris_save_metadata(app: &tauri::AppHandle) -> anyhow::Result<Vec<Vec<StellarisSave>>> {
fn get_stellaris_save_metadata() -> anyhow::Result<Vec<Vec<StellarisSave>>> {
let saves: Vec<Vec<StellarisSave>> = get_steam_user_data_dirs()
.unwrap_or(Vec::new())
.iter()
.map(|path| path.join("281990").join("remote").join("save games"))
.chain(std::iter::once(
get_stellaris_user_data_dir(app).join("save games"),
get_stellaris_user_data_dir().join("save games"),
))
.flat_map(|path| get_sub_dirs(&path).unwrap_or_default())
.map(|path| {
Expand All @@ -350,13 +340,12 @@ fn get_stellaris_save_metadata(app: &tauri::AppHandle) -> anyhow::Result<Vec<Vec
}

fn get_stellaris_data_paths(
app: &tauri::AppHandle,
install_path: PathBuf,
data_relative_dir: PathBuf,
filter: FileFilter,
depth: u8,
) -> Vec<PathBuf> {
let data_root_dirs = get_stellaris_data_dirs(app, install_path);
let data_root_dirs = get_stellaris_data_dirs(install_path);
let mut file_path_to_root_dir: HashMap<PathBuf, PathBuf> = HashMap::new();
for data_root_dir in data_root_dirs {
let dir = data_root_dir.join(&data_relative_dir);
Expand Down Expand Up @@ -384,11 +373,9 @@ fn get_stellaris_data_paths(
}

fn get_stellaris_loc(
app: &tauri::AppHandle,
path: String,
) -> anyhow::Result<HashMap<String, String>> {
let loc_file_paths = get_stellaris_data_paths(
app,
Path::new(&path).to_path_buf(),
Path::new("localisation").to_path_buf(),
FileFilter::Extension(OsString::from("yml")),
Expand All @@ -415,12 +402,11 @@ fn get_stellaris_loc(
}

fn get_emblem(
app: &tauri::AppHandle,
install_path: PathBuf,
category: String,
file: String,
) -> anyhow::Result<String> {
let mut dirs = get_stellaris_data_dirs(app, install_path).to_owned();
let mut dirs = get_stellaris_data_dirs(install_path).to_owned();
dirs.reverse();
for dir in dirs {
let path = dir.join("flags").join(&category).join("map").join(&file);
Expand Down

0 comments on commit b2ba628

Please sign in to comment.