mirror of
https://github.com/alterware/alterware-launcher.git
synced 2025-12-06 00:07:48 +00:00
create cache module
This commit is contained in:
34
src/cache.rs
Normal file
34
src/cache.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use crate::structs::{Cache, StoredGameData};
|
||||
use std::{fs, path::Path};
|
||||
|
||||
pub fn get_cache(dir: &Path) -> Cache {
|
||||
let cache_path = dir.join("awcache.json");
|
||||
let cache_content = fs::read_to_string(cache_path).unwrap_or_default();
|
||||
if cache_content.trim().is_empty() {
|
||||
Cache::default()
|
||||
} else {
|
||||
serde_json::from_str(&cache_content).unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_cache(dir: &Path, cache: Cache) {
|
||||
let cache_path = dir.join("awcache.json");
|
||||
let cache_serialized = serde_json::to_string_pretty(&cache).unwrap();
|
||||
fs::write(cache_path, cache_serialized).unwrap_or_else(|e| {
|
||||
error!("Failed to save cache: {}", e);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn get_stored_data() -> Option<StoredGameData> {
|
||||
let dir = std::env::current_dir().ok()?;
|
||||
let cache = get_cache(&dir);
|
||||
cache.stored_data
|
||||
}
|
||||
|
||||
pub fn store_game_data(data: &StoredGameData) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let dir = std::env::current_dir()?;
|
||||
let mut cache = get_cache(&dir);
|
||||
cache.stored_data = Some((*data).clone());
|
||||
save_cache(&dir, cache);
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user