| 1 | //! Application state management |
| 2 | |
| 3 | use wanda_core::{ |
| 4 | config::WandaConfig, |
| 5 | prefix::PrefixManager, |
| 6 | steam::{ProtonManager, SteamInstallation}, |
| 7 | }; |
| 8 | |
| 9 | /// Shared application state |
| 10 | pub struct AppState { |
| 11 | /// Loaded configuration |
| 12 | pub config: Option<WandaConfig>, |
| 13 | /// Steam installation (cached) |
| 14 | pub steam: Option<SteamInstallation>, |
| 15 | /// Proton manager (cached) |
| 16 | pub proton: Option<ProtonManager>, |
| 17 | /// Prefix manager |
| 18 | pub prefix_manager: Option<PrefixManager>, |
| 19 | /// Whether WANDA is initialized |
| 20 | pub initialized: bool, |
| 21 | } |
| 22 | |
| 23 | impl AppState { |
| 24 | pub fn new() -> Self { |
| 25 | Self { |
| 26 | config: None, |
| 27 | steam: None, |
| 28 | proton: None, |
| 29 | prefix_manager: None, |
| 30 | initialized: false, |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// Load or reload state from disk |
| 35 | pub fn load(&mut self) -> Result<(), String> { |
| 36 | // Load config |
| 37 | let config = WandaConfig::load().map_err(|e| e.to_string())?; |
| 38 | |
| 39 | // Discover Steam |
| 40 | let steam = SteamInstallation::discover(&config).map_err(|e| e.to_string())?; |
| 41 | |
| 42 | // Discover Proton |
| 43 | let proton = ProtonManager::discover(&steam, &config).map_err(|e| e.to_string())?; |
| 44 | |
| 45 | // Load prefix manager |
| 46 | let mut prefix_manager = PrefixManager::new(&config); |
| 47 | prefix_manager.load().map_err(|e| e.to_string())?; |
| 48 | |
| 49 | // Check if initialized (has default prefix with WeMod) |
| 50 | let initialized = prefix_manager |
| 51 | .get("default") |
| 52 | .map(|p| p.wemod_installed) |
| 53 | .unwrap_or(false); |
| 54 | |
| 55 | self.config = Some(config); |
| 56 | self.steam = Some(steam); |
| 57 | self.proton = Some(proton); |
| 58 | self.prefix_manager = Some(prefix_manager); |
| 59 | self.initialized = initialized; |
| 60 | |
| 61 | Ok(()) |
| 62 | } |
| 63 | |
| 64 | /// Ensure state is loaded |
| 65 | pub fn ensure_loaded(&mut self) -> Result<(), String> { |
| 66 | if self.config.is_none() { |
| 67 | self.load()?; |
| 68 | } |
| 69 | Ok(()) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | impl Default for AppState { |
| 74 | fn default() -> Self { |
| 75 | Self::new() |
| 76 | } |
| 77 | } |
| 78 |