launcher/
config_manager.rs1use std::{
2 fs,
3 path::{Path, PathBuf},
4};
5use supreme_config::models::{
6 DetailConfig, DisplayConfig, LanguageConfig, SoundConfig, TrainerConfig,
7};
8use supreme_fs::GameStorage;
9use supreme_music::{MusicConfig, load_and_reconcile_music_configs};
10
11pub struct ConfigManager {
12 pub storage: GameStorage,
13 pub display_config: DisplayConfig,
14 pub language_config: LanguageConfig,
15 pub detail_config: DetailConfig,
16 pub sound_config: SoundConfig,
17 pub trainer_config: TrainerConfig,
18 pub music_configs: Vec<MusicConfig>,
19}
20
21impl ConfigManager {
22 #[must_use]
23 pub fn load(storage: &GameStorage) -> Self {
24 Self {
25 display_config: DisplayConfig::load(storage, Path::new("rd_config.txt")),
26 language_config: LanguageConfig::load(storage, Path::new("Saved_Data/language.txt")),
27 detail_config: DetailConfig::load(storage, Path::new("Data/Detail_Config.txt")),
28 sound_config: SoundConfig::load(storage, Path::new("Data/Sound_Config.txt")),
29 trainer_config: TrainerConfig::load(Path::new(
30 "Display_Config_Resources/Display_Config_Helper.json",
31 )),
32 music_configs: Vec::new(),
33 storage: storage.clone(),
34 }
35 }
36
37 pub fn save_all(&self) -> Result<(), Vec<String>> {
38 let mut errors = Vec::new();
39
40 if let Err(e) = self
41 .language_config
42 .save(&self.storage, Path::new("Saved_Data/language.txt"))
43 {
44 errors.push(format!("Language config: {e}"));
45 }
46
47 if let Err(e) = self
48 .display_config
49 .save(&self.storage, Path::new("rd_config.txt"))
50 {
51 errors.push(format!("Display config: {e}"));
52 }
53
54 if let Err(e) = self
55 .detail_config
56 .save(&self.storage, Path::new("Data/Detail_Config.txt"))
57 {
58 errors.push(format!("Detail config: {e}"));
59 }
60
61 if let Err(e) = self
62 .sound_config
63 .save(&self.storage, Path::new("Data/Sound_Config.txt"))
64 {
65 errors.push(format!("Sound config: {e}"));
66 }
67
68 if let Err(e) = self.trainer_config.save(Path::new(
69 "Display_Config_Resources/Display_Config_Helper.json",
70 )) {
71 errors.push(format!("Helper config: {e}"));
72 }
73
74 if errors.is_empty() {
75 Ok(())
76 } else {
77 Err(errors)
78 }
79 }
80
81 pub fn export(&self) -> Result<PathBuf, Vec<String>> {
82 let export_path = PathBuf::from("config_export");
83 let export_storage = GameStorage::new(&export_path);
84 let mut errors = Vec::new();
85
86 if let Err(e) = fs::create_dir_all(export_path.join("Saved_Data")) {
88 errors.push(format!("Failed to create Saved_Data directory: {e}"));
89 }
90
91 if let Err(e) = fs::create_dir_all(export_path.join("Data")) {
92 errors.push(format!("Failed to create Data directory: {e}"));
93 }
94
95 if let Err(e) = fs::create_dir_all(export_path.join("Display_Config_Resources")) {
96 errors.push(format!(
97 "Failed to create Display_Config_Resources directory: {e}"
98 ));
99 }
100
101 if let Err(e) = self
103 .display_config
104 .save(&export_storage, &export_path.join("rd_config.txt"))
105 {
106 errors.push(format!("Display config: {e}"));
107 }
108
109 if let Err(e) = self.language_config.save(
110 &export_storage,
111 &export_path.join("Saved_Data/language.txt"),
112 ) {
113 errors.push(format!("Language config: {e}"));
114 }
115
116 if let Err(e) = self
117 .detail_config
118 .save(&export_storage, &export_path.join("Data/Detail_Config.txt"))
119 {
120 errors.push(format!("Detail config: {e}"));
121 }
122
123 if let Err(e) = self
124 .sound_config
125 .save(&export_storage, &export_path.join("Data/Sound_Config.txt"))
126 {
127 errors.push(format!("Sound config: {e}"));
128 }
129
130 if let Err(e) = self
131 .trainer_config
132 .save(&export_path.join("Display_Config_Resources/Display_Config_Helper.json"))
133 {
134 errors.push(format!("Helper config: {e}"));
135 }
136
137 if errors.is_empty() {
138 Ok(export_path)
139 } else {
140 Err(errors)
141 }
142 }
143
144 pub fn load_music_configs(&mut self) {
145 let game_path = self.storage.game_path();
146 let music_dir = game_path.join("data/music");
147 self.music_configs = load_and_reconcile_music_configs(&music_dir, game_path);
148 }
149}