2023-12-22 16:26:50 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
2024-04-19 22:18:06 -04:00
|
|
|
use crate::consts::*;
|
2023-12-22 16:26:50 -05:00
|
|
|
|
|
|
|
static LAST_SETTING_ID: Mutex<u128> = Mutex::new(0);
|
|
|
|
|
2024-01-06 13:26:35 -05:00
|
|
|
pub fn filename(id: u128, ext: &str) -> String {
|
|
|
|
format!("{}.{}", id, ext)
|
|
|
|
}
|
|
|
|
|
2023-12-22 16:26:50 -05:00
|
|
|
pub fn setting_path_by_id(root: impl AsRef<Path>, id: u128, ext: &str) -> PathBuf {
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(ID_FOLDER)
|
2024-01-06 13:26:35 -05:00
|
|
|
.join(filename(id, ext))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn setting_folder_by_app_id(root: impl AsRef<Path>, steam_app_id: u32) -> PathBuf {
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(APP_ID_FOLDER)
|
|
|
|
.join(steam_app_id.to_string())
|
|
|
|
}
|
|
|
|
|
2024-04-19 22:18:06 -04:00
|
|
|
pub fn setting_tag_folder_by_app_id(root: impl AsRef<Path>, steam_app_id: u32) -> PathBuf {
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(APP_ID_FOLDER)
|
|
|
|
.join(steam_app_id.to_string())
|
|
|
|
.join(TAG_FOLDER)
|
|
|
|
}
|
|
|
|
|
2024-04-13 10:14:02 -04:00
|
|
|
pub fn setting_folder_by_app_id_tag(root: impl AsRef<Path>, steam_app_id: u32, tag: &str) -> PathBuf {
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(APP_ID_FOLDER)
|
|
|
|
.join(steam_app_id.to_string())
|
|
|
|
.join(TAG_FOLDER)
|
|
|
|
.join(tag)
|
|
|
|
}
|
|
|
|
|
2024-01-06 13:26:35 -05:00
|
|
|
pub fn setting_folder_by_user_id(root: impl AsRef<Path>, steam_user_id: u64) -> PathBuf {
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(USER_ID_FOLDER)
|
|
|
|
.join(steam_user_id.to_string())
|
|
|
|
}
|
|
|
|
|
2024-04-19 22:18:06 -04:00
|
|
|
pub fn setting_tag_folder_by_user_id(root: impl AsRef<Path>, steam_user_id: u64) -> PathBuf {
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(USER_ID_FOLDER)
|
|
|
|
.join(steam_user_id.to_string())
|
|
|
|
.join(TAG_FOLDER)
|
|
|
|
}
|
|
|
|
|
2024-04-13 10:14:02 -04:00
|
|
|
pub fn setting_folder_by_user_id_tag(root: impl AsRef<Path>, steam_user_id: u64, tag: &str) -> PathBuf {
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(USER_ID_FOLDER)
|
|
|
|
.join(steam_user_id.to_string())
|
|
|
|
.join(TAG_FOLDER)
|
|
|
|
.join(tag)
|
|
|
|
}
|
|
|
|
|
2024-01-06 13:26:35 -05:00
|
|
|
pub fn setting_folder_by_tag(root: impl AsRef<Path>, tag: &str) -> PathBuf {
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(TAG_FOLDER)
|
|
|
|
.join(tag)
|
2023-12-22 16:26:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_setting_id(root: impl AsRef<Path>) -> u128 {
|
|
|
|
let mut lock = LAST_SETTING_ID.lock().unwrap();
|
|
|
|
let mut last_id = *lock;
|
|
|
|
if last_id == 0 {
|
|
|
|
// needs init
|
2024-04-12 21:35:10 -04:00
|
|
|
last_id = 1;
|
2023-12-22 16:26:50 -05:00
|
|
|
let mut path = setting_path_by_id(root.as_ref(), last_id, RON_EXTENSION);
|
|
|
|
while path.exists() {
|
|
|
|
last_id += 1;
|
|
|
|
path = setting_path_by_id(root.as_ref(), last_id, RON_EXTENSION);
|
|
|
|
}
|
2024-04-12 21:35:10 -04:00
|
|
|
*lock = last_id - 1;
|
|
|
|
log::info!("setting id initialized to {}", last_id);
|
2023-12-22 16:26:50 -05:00
|
|
|
}
|
|
|
|
*lock += 1;
|
|
|
|
*lock
|
|
|
|
}
|