Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.

Commit 573fc14

Browse files
committed
create cache module
1 parent b0c2c8d commit 573fc14

File tree

5 files changed

+52
-50
lines changed

5 files changed

+52
-50
lines changed

src/cache.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::structs::{Cache, StoredGameData};
2+
use std::{fs, path::Path};
3+
4+
pub fn get_cache(dir: &Path) -> Cache {
5+
let cache_path = dir.join("awcache.json");
6+
let cache_content = fs::read_to_string(cache_path).unwrap_or_default();
7+
if cache_content.trim().is_empty() {
8+
Cache::default()
9+
} else {
10+
serde_json::from_str(&cache_content).unwrap_or_default()
11+
}
12+
}
13+
14+
pub fn save_cache(dir: &Path, cache: Cache) {
15+
let cache_path = dir.join("awcache.json");
16+
let cache_serialized = serde_json::to_string_pretty(&cache).unwrap();
17+
fs::write(cache_path, cache_serialized).unwrap_or_else(|e| {
18+
error!("Failed to save cache: {}", e);
19+
});
20+
}
21+
22+
pub fn get_stored_data() -> Option<StoredGameData> {
23+
let dir = std::env::current_dir().ok()?;
24+
let cache = get_cache(&dir);
25+
cache.stored_data
26+
}
27+
28+
pub fn store_game_data(data: &StoredGameData) -> Result<(), Box<dyn std::error::Error>> {
29+
let dir = std::env::current_dir()?;
30+
let mut cache = get_cache(&dir);
31+
cache.stored_data = Some((*data).clone());
32+
save_cache(&dir, cache);
33+
Ok(())
34+
}

src/global.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::misc;
2-
use crate::structs::{PrintPrefix, StoredGameData};
1+
use crate::structs::PrintPrefix;
32
use colored::Colorize;
43
use once_cell::sync::Lazy;
54
use std::collections::HashMap;
@@ -66,17 +65,3 @@ pub async fn check_connectivity() -> bool {
6665
}
6766
}
6867
}
69-
70-
pub fn get_stored_data() -> Option<StoredGameData> {
71-
let dir = std::env::current_dir().ok()?;
72-
let cache = misc::get_cache(&dir);
73-
cache.stored_data
74-
}
75-
76-
pub fn store_game_data(data: &StoredGameData) -> Result<(), Box<dyn std::error::Error>> {
77-
let dir = std::env::current_dir()?;
78-
let mut cache = misc::get_cache(&dir);
79-
cache.stored_data = Some((*data).clone());
80-
misc::save_cache(&dir, cache);
81-
Ok(())
82-
}

src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod cache;
12
mod config;
23
mod extend;
34
mod github;
@@ -376,7 +377,7 @@ async fn update(
376377
let mut cache = if force {
377378
structs::Cache::default()
378379
} else {
379-
misc::get_cache(dir)
380+
cache::get_cache(dir)
380381
};
381382

382383
if game.engine == "iw4" {
@@ -482,10 +483,10 @@ async fn update(
482483
}
483484
}
484485

485-
misc::save_cache(dir, cache);
486+
cache::save_cache(dir, cache);
486487

487488
// Store game data for offline mode
488-
let mut stored_data = global::get_stored_data().unwrap_or_default();
489+
let mut stored_data = cache::get_stored_data().unwrap_or_default();
489490
stored_data.game_path = dir.to_string_lossy().into_owned();
490491

491492
// Store available clients for this engine
@@ -494,7 +495,7 @@ async fn update(
494495
game.client.iter().map(|s| s.to_string()).collect(),
495496
);
496497

497-
if let Err(e) = global::store_game_data(&stored_data) {
498+
if let Err(e) = cache::store_game_data(&stored_data) {
498499
println!(
499500
"{} Failed to store game data: {}",
500501
PREFIXES.get("error").unwrap().formatted(),
@@ -679,7 +680,7 @@ async fn main() {
679680
let offline_mode = !global::check_connectivity().await;
680681
if offline_mode {
681682
// Check if this is a first-time run (no stored data)
682-
let stored_data = global::get_stored_data();
683+
let stored_data = cache::get_stored_data();
683684
if stored_data.is_none() {
684685
println!(
685686
"{} Internet connection is required for first-time installation.",
@@ -713,7 +714,7 @@ async fn main() {
713714
let cfg = config::load(install_path.join("alterware-launcher.json"));
714715

715716
// Try to get stored game data
716-
let stored_data = global::get_stored_data();
717+
let stored_data = cache::get_stored_data();
717718
if let Some(ref data) = stored_data {
718719
info!("Found stored game data for path: {}", data.game_path);
719720
} else {
@@ -933,7 +934,7 @@ async fn main() {
933934
}
934935

935936
// Store game data for offline mode
936-
let mut stored_data = global::get_stored_data().unwrap_or_default();
937+
let mut stored_data = cache::get_stored_data().unwrap_or_default();
937938
stored_data.game_path = install_path.to_string_lossy().into_owned();
938939

939940
// Store available clients for this engine
@@ -942,7 +943,7 @@ async fn main() {
942943
g.client.iter().map(|s| s.to_string()).collect(),
943944
);
944945

945-
if let Err(e) = global::store_game_data(&stored_data) {
946+
if let Err(e) = cache::store_game_data(&stored_data) {
946947
println!(
947948
"{} Failed to store game data: {}",
948949
PREFIXES.get("error").unwrap().formatted(),

src/misc.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{fs, path::Path};
22

33
use indicatif::{ProgressBar, ProgressStyle};
44

5-
use crate::{global, structs};
5+
use crate::global;
66

77
pub fn stdin() -> String {
88
let mut input = String::new();
@@ -140,24 +140,6 @@ pub fn prefix(tag_name: &str) -> String {
140140
.map_or_else(|| tag_name.to_string(), |tag| tag.formatted())
141141
}
142142

143-
pub fn get_cache(dir: &Path) -> structs::Cache {
144-
let cache_path = dir.join("awcache.json");
145-
let cache_content = fs::read_to_string(cache_path).unwrap_or_default();
146-
if cache_content.trim().is_empty() {
147-
structs::Cache::default()
148-
} else {
149-
serde_json::from_str(&cache_content).unwrap_or_default()
150-
}
151-
}
152-
153-
pub fn save_cache(dir: &Path, cache: structs::Cache) {
154-
let cache_path = dir.join("awcache.json");
155-
let cache_serialized = serde_json::to_string_pretty(&cache).unwrap();
156-
fs::write(cache_path, cache_serialized).unwrap_or_else(|e| {
157-
println_error!("Failed to save cache: {}", e);
158-
});
159-
}
160-
161143
pub fn random_string(length: u32) -> String {
162144
use rand::Rng;
163145
let mut rng = rand::thread_rng();

src/tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mod config {
4949
}
5050

5151
mod misc {
52-
use crate::{fs, misc, structs, Blake3Path};
52+
use crate::{cache, fs, misc, structs, Blake3Path};
5353
use std::{fs::File, io::Write, path::Path};
5454

5555
#[test]
@@ -95,7 +95,7 @@ mod misc {
9595
let cache_file = path.join("awcache.json");
9696

9797
// Test initial empty cache
98-
let initial_cache = misc::get_cache(path);
98+
let initial_cache = cache::get_cache(path);
9999
assert_eq!(initial_cache, structs::Cache::default());
100100

101101
// Test saving and loading cache
@@ -106,16 +106,16 @@ mod misc {
106106
.collect(),
107107
stored_data: None,
108108
};
109-
misc::save_cache(path, test_cache.clone());
110-
let loaded_cache = misc::get_cache(path);
109+
cache::save_cache(path, test_cache.clone());
110+
let loaded_cache = cache::get_cache(path);
111111
assert_eq!(loaded_cache, test_cache);
112112

113113
fs::remove_file(&cache_file).unwrap();
114114
}
115115
}
116116

117117
mod stored_data {
118-
use crate::{fs, global, structs::StoredGameData};
118+
use crate::{cache, fs, structs::StoredGameData};
119119
use serial_test::serial;
120120
use std::{collections::HashMap, path::Path};
121121

@@ -134,8 +134,8 @@ mod stored_data {
134134
let path = Path::new("tests_tmp");
135135
fs::create_dir_all(path).unwrap();
136136

137-
global::store_game_data(&data).unwrap();
138-
let loaded = global::get_stored_data().unwrap();
137+
cache::store_game_data(&data).unwrap();
138+
let loaded = cache::get_stored_data().unwrap();
139139

140140
assert_eq!(data.game_path, loaded.game_path);
141141
assert_eq!(data.clients, loaded.clients);

0 commit comments

Comments
 (0)