-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #55 from Berrysoft/dev/dcp-settings
Decouple settings manager from runtime.
- Loading branch information
Showing
8 changed files
with
164 additions
and
95 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use ayaka_runtime::{ | ||
anyhow::{self, Result}, | ||
settings::*, | ||
}; | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
use std::path::{Path, PathBuf}; | ||
use tauri::PathResolver; | ||
use tryiterator::TryIteratorExt; | ||
|
||
#[derive(Default)] | ||
pub struct FileSettingsManager { | ||
local_data_dir: PathBuf, | ||
config_dir: PathBuf, | ||
} | ||
|
||
impl FileSettingsManager { | ||
pub fn new(resolver: &PathResolver) -> Self { | ||
Self { | ||
local_data_dir: resolver.app_local_data_dir().unwrap(), | ||
config_dir: resolver.app_config_dir().unwrap(), | ||
} | ||
} | ||
|
||
fn records_path_root(&self, game: &str) -> PathBuf { | ||
self.local_data_dir.join("save").join(game) | ||
} | ||
} | ||
|
||
impl SettingsManager for FileSettingsManager { | ||
fn load_file<T: DeserializeOwned>(&self, path: impl AsRef<Path>) -> Result<T> { | ||
let buffer = std::fs::read(path)?; | ||
Ok(serde_json::from_slice(&buffer)?) | ||
} | ||
|
||
fn save_file<T: Serialize>( | ||
&self, | ||
path: impl AsRef<Path>, | ||
data: &T, | ||
pretty: bool, | ||
) -> Result<()> { | ||
let path = path.as_ref(); | ||
if let Some(parent) = path.parent() { | ||
std::fs::create_dir_all(parent)?; | ||
} | ||
let buffer = if pretty { | ||
serde_json::to_vec_pretty(data) | ||
} else { | ||
serde_json::to_vec(data) | ||
}?; | ||
std::fs::write(path, buffer)?; | ||
Ok(()) | ||
} | ||
|
||
fn settings_path(&self) -> Result<PathBuf> { | ||
Ok(self.config_dir.join("settings.json")) | ||
} | ||
|
||
fn global_record_path(&self, game: &str) -> Result<PathBuf> { | ||
Ok(self.records_path_root(game).join("global.json")) | ||
} | ||
|
||
type RecordsPathIter = impl Iterator<Item = Result<PathBuf>>; | ||
|
||
fn records_path(&self, game: &str) -> Result<Self::RecordsPathIter> { | ||
let ctx_path = self.records_path_root(game); | ||
Ok(std::fs::read_dir(ctx_path)? | ||
.map_err(anyhow::Error::from) | ||
.try_filter_map(|entry| { | ||
let p = entry.path(); | ||
if p.is_file() && p.file_name().unwrap_or_default() != "global.json" { | ||
Ok(Some(p)) | ||
} else { | ||
Ok(None) | ||
} | ||
})) | ||
} | ||
|
||
fn record_path(&self, game: &str, i: usize) -> Result<PathBuf> { | ||
Ok(self | ||
.records_path_root(game) | ||
.join(i.to_string()) | ||
.with_extension("json")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use crate::{ | ||
plugin::{LoadStatus, Runtime}, | ||
settings::*, | ||
*, | ||
}; | ||
use anyhow::{anyhow, bail, Result}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.