2023-12-22 21:26:50 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use std::sync::Mutex;
|
|
|
|
|
|
|
|
pub const RON_EXTENSION: &'static str = "ron";
|
|
|
|
pub const JSON_EXTENSION: &'static str = "json";
|
|
|
|
|
|
|
|
const SETTING_FOLDER: &'static str = "settings";
|
|
|
|
const ID_FOLDER: &'static str = "by_id";
|
2024-01-06 18:26:35 +00:00
|
|
|
const APP_ID_FOLDER: &'static str = "by_app_id";
|
|
|
|
const USER_ID_FOLDER: &'static str = "by_user_id";
|
|
|
|
const TAG_FOLDER: &'static str = "by_tag";
|
2023-12-22 21:26:50 +00:00
|
|
|
|
|
|
|
static LAST_SETTING_ID: Mutex<u128> = Mutex::new(0);
|
|
|
|
|
2024-01-06 18:26:35 +00:00
|
|
|
pub fn build_folder_layout(root: impl AsRef<Path>) -> std::io::Result<()> {
|
|
|
|
std::fs::create_dir_all(
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(ID_FOLDER)
|
|
|
|
)?;
|
|
|
|
std::fs::create_dir_all(
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(APP_ID_FOLDER)
|
|
|
|
)?;
|
|
|
|
std::fs::create_dir_all(
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(USER_ID_FOLDER)
|
|
|
|
)?;
|
|
|
|
std::fs::create_dir_all(
|
|
|
|
root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(TAG_FOLDER)
|
|
|
|
)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-12 22:31:48 +01:00
|
|
|
pub fn fix_symlinks(root: impl AsRef<Path>) -> std::io::Result<()> {
|
|
|
|
log::info!("root setttings folder: {} aka {} (absolute)", root.as_ref().display(), root.as_ref().canonicalize()?.display());
|
|
|
|
for dir_entry in root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(APP_ID_FOLDER)
|
|
|
|
.read_dir()? {
|
|
|
|
let dir_entry = dir_entry?;
|
|
|
|
if dir_entry.file_type()?.is_dir() {
|
|
|
|
make_symlinks_absolute_in_dir(root.as_ref(), dir_entry.path())?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for dir_entry in root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(USER_ID_FOLDER)
|
|
|
|
.read_dir()? {
|
|
|
|
let dir_entry = dir_entry?;
|
|
|
|
if dir_entry.file_type()?.is_dir() {
|
|
|
|
make_symlinks_absolute_in_dir(root.as_ref(), dir_entry.path())?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for dir_entry in root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(TAG_FOLDER).read_dir()? {
|
|
|
|
let dir_entry = dir_entry?;
|
|
|
|
if dir_entry.file_type()?.is_dir() {
|
|
|
|
make_symlinks_absolute_in_dir(root.as_ref(), dir_entry.path())?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_symlinks_absolute_in_dir(root: impl AsRef<Path>, dir: impl AsRef<Path>) -> std::io::Result<()> {
|
|
|
|
let abs_root = root.as_ref().canonicalize()?;
|
|
|
|
assert!(abs_root.is_absolute());
|
|
|
|
for dir_entry in dir.as_ref()
|
|
|
|
.read_dir()? {
|
|
|
|
let dir_entry = dir_entry?;
|
|
|
|
if dir_entry.file_type()?.is_symlink() {
|
|
|
|
let path = dir_entry.path();
|
|
|
|
let link_path = path.read_link()?;
|
|
|
|
if !link_path.is_absolute() {
|
|
|
|
let new_link = abs_root.join(
|
|
|
|
link_path.strip_prefix(&root).expect("Symlinked path does not begin with root settings folder")
|
|
|
|
);
|
|
|
|
log::info!("Fixing {} -> {} to -> {}", path.display(), link_path.display(), new_link.display());
|
|
|
|
std::fs::remove_file(&path)?;
|
|
|
|
#[cfg(target_family = "windows")] // NOTE: windows support is untested and unmaintained
|
|
|
|
{
|
|
|
|
std::os::windows::fs::symlink_file(new_link, &path)?;
|
|
|
|
}
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
{
|
|
|
|
std::os::unix::fs::symlink(new_link, &path)?;
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
log::info!("Found already-absolute symlink {} -> {}", path.display(), link_path.display());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log::info!("Found non-symlink {}: {:?}", dir_entry.path().display(), dir_entry.file_type()?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-13 02:55:29 +01:00
|
|
|
pub fn sync_ids(root: impl AsRef<Path>) -> std::io::Result<()> {
|
|
|
|
for dir_entry in root.as_ref()
|
|
|
|
.join(SETTING_FOLDER)
|
|
|
|
.join(ID_FOLDER)
|
|
|
|
.read_dir()? {
|
|
|
|
let dir_entry = dir_entry?;
|
|
|
|
let f_path = dir_entry.path();
|
|
|
|
if let Some(ext) = f_path.extension() {
|
|
|
|
let id = f_path.file_stem().map(|os| os.to_string_lossy().to_string()).unwrap();
|
|
|
|
if ext == RON_EXTENSION {
|
|
|
|
let reader = std::io::BufReader::new(std::fs::File::open(&f_path)?);
|
|
|
|
let mut setting: community_settings_core::v1::Metadata = match ron::de::from_reader(reader) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(e) => {
|
|
|
|
log::debug!("Error while reading {}: {}", f_path.display(), e);
|
|
|
|
let e_msg = format!("{}", e);
|
|
|
|
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e_msg));
|
|
|
|
}
|
|
|
|
};
|
2024-04-13 15:14:02 +01:00
|
|
|
if setting.id != id {
|
|
|
|
setting.id = id;
|
|
|
|
ron::ser::to_writer(std::fs::File::create(&f_path)?, &setting).unwrap();
|
|
|
|
}
|
2024-04-13 02:55:29 +01:00
|
|
|
} else if ext == JSON_EXTENSION {
|
|
|
|
let reader = std::io::BufReader::new(std::fs::File::open(&f_path)?);
|
|
|
|
let mut setting: community_settings_core::v1::Metadata = match serde_json::from_reader(reader) {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(e) => {
|
|
|
|
log::debug!("Error while reading {}: {}", f_path.display(), e);
|
|
|
|
let e_msg = format!("{}", e);
|
|
|
|
return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e_msg));
|
|
|
|
}
|
|
|
|
};
|
2024-04-13 15:14:02 +01:00
|
|
|
if setting.id != id {
|
|
|
|
setting.id = id;
|
|
|
|
serde_json::to_writer(std::fs::File::create(&f_path)?, &setting).unwrap();
|
|
|
|
}
|
2024-04-13 02:55:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-01-06 18:26:35 +00:00
|
|
|
pub fn filename(id: u128, ext: &str) -> String {
|
|
|
|
format!("{}.{}", id, ext)
|
|
|
|
}
|
|
|
|
|
2023-12-22 21:26:50 +00: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 18:26:35 +00: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-13 15:14:02 +01: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 18:26:35 +00: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-13 15:14:02 +01: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 18:26:35 +00: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 21:26:50 +00: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-13 02:35:10 +01:00
|
|
|
last_id = 1;
|
2023-12-22 21:26:50 +00: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-13 02:35:10 +01:00
|
|
|
*lock = last_id - 1;
|
|
|
|
log::info!("setting id initialized to {}", last_id);
|
2023-12-22 21:26:50 +00:00
|
|
|
}
|
|
|
|
*lock += 1;
|
|
|
|
*lock
|
|
|
|
}
|