diff --git a/src/config.rs b/src/config.rs index bdb9b03..92429d3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,24 +1,18 @@ -use crate::infos::{LauncherInfo, WadInfo}; +use crate::infos::{LauncherInfo}; +use directories; use serde_derive::{Deserialize, Serialize}; use std::path::PathBuf; use toml; -use directories; - -#[derive(Serialize, Deserialize, Debug)] -pub enum AddMode { - DoNothing, - Copy, - Move, -} #[derive(Serialize, Deserialize, Debug, Default)] pub struct Config { - pub iwads_added_folder: Option, - pub iwads_removed_folder: Option, - pub add_mode: Option, - pub iwads: Option>, - pub pwads: Option>, pub launchers: Option>, + pub iwads_folder: PathBuf, + pub iwads_old_folder: Option, + pub pwads_folder: Option, + pub pwads_old_folder: Option, + pub mods_folder: Option, + pub mods_old_folder: Option, } pub fn load_config(path: &PathBuf) -> Result> { @@ -29,18 +23,11 @@ pub fn load_config(path: &PathBuf) -> Result> Ok(toml::from_str(&content)?) } -pub fn save_config(path: &PathBuf, config: &Config) -> Result<(), Box> { - let prefix = path.parent().unwrap(); - std::fs::create_dir_all(prefix).unwrap(); - let toml_string = toml::to_string(&config).unwrap(); - Ok(std::fs::write(path, toml_string)?) -} - -pub fn default_save_filename() -> PathBuf { +pub fn default_config_filename() -> PathBuf { let project = directories::ProjectDirs::from("party", "arfy", "rdl").unwrap(); let mut full_path = PathBuf::new(); full_path.push(project.config_dir()); - full_path.push("wads_and_stuff.toml"); + full_path.push("wad_folders.toml"); full_path } @@ -49,85 +36,51 @@ mod tests { use super::*; #[test] - fn basic_iwad() { - let config_str = "[[iwads]]\n\ - name = \"DOOM2.WAD\"\n\ - path = \"/home/wads/doom.wad\"\n"; + fn basic_folder() { + let config_str = "iwads_folder = \"/home/arthurr/Games/doom_wads/iwads\""; + let config: Config = toml::from_str(&config_str).unwrap(); + assert_eq!( + config.iwads_folder, + PathBuf::from("/home/arthurr/Games/doom_wads/iwads") + ); + } + + #[test] + fn basic_launcher() { + let config_str = "iwads_folder = \"/home/arthurr/Games/doom_wads/iwads\" +[[launchers]]\n\ +name = \"GZDoom\"\n\ +path = \"/home/guy/gzdoom\"\n"; let config: Result = toml::from_str(&config_str); assert!(config.is_ok()); let config = config.unwrap(); - assert_eq!(config.iwads.unwrap().len(), 1); + assert_eq!(config.launchers.unwrap().len(), 1); } #[test] - fn basic_iwads() { - let config_str = "[[iwads]] -name = \"DOOM2.WAD\" -path = \"/home/wads/doom.wad\" + fn basic_launchers() { + let config_str = "iwads_folder = \"/home/arthurr/Games/doom_wads/iwads\" +[[launchers]] +name = \"GZDoom\" +path = \"/home/guy/gzdoom\" -[[iwads]] -name = \"DOOM1.WAD1.9\" -path = \"/home/wads/doom1.9.wad\""; +[[launchers]] +name = \"PrBoom\" +path = \"/home/guy/prboom\""; let config: Config = toml::from_str(&config_str).unwrap(); - // I'm not 100% sure why I need all of these as_ref - assert_eq!(config.iwads.as_ref().unwrap().len(), 2); - assert_eq!(config.iwads.as_ref().unwrap()[0].name, "DOOM2.WAD"); - assert_eq!(config.iwads.as_ref().unwrap()[1].name, "DOOM1.WAD1.9"); + assert_eq!(config.launchers.as_ref().unwrap().len(), 2); + assert_eq!(config.launchers.as_ref().unwrap()[0].name, "GZDoom"); + assert_eq!(config.launchers.as_ref().unwrap()[1].name, "PrBoom"); assert_eq!( - config.iwads.unwrap()[0].path, - PathBuf::from("/home/wads/doom.wad") + config.launchers.unwrap()[0].path, + PathBuf::from("/home/guy/gzdoom") ); } #[test] - fn iwads_and_pwads() { - let config_str = "[[iwads]] -name = \"DOOM2.WAD\" -path = \"/home/wads/doom.wad\" - -[[iwads]] -name = \"DOOM1.WAD1.9\" -path = \"/home/wads/doom1.9.wad\" - -[[pwads]] -name = \"Ancient Aliens\" -path = \"/home/pwads/aa.pk3\""; - - let config: Config = toml::from_str(&config_str).unwrap(); - assert_eq!(config.iwads.as_ref().unwrap().len(), 2); - assert_eq!(config.iwads.unwrap()[0].name, "DOOM2.WAD"); - assert_eq!(config.pwads.as_ref().unwrap()[0].name, "Ancient Aliens"); - assert_eq!( - config.pwads.unwrap()[0].path, - PathBuf::from("/home/pwads/aa.pk3") - ); - } - - #[test] - fn iwads_back_and_forth() { - let iwad1 = WadInfo { - path: PathBuf::from("/home/wads/doom.wad"), - name: "DOOM.WAD".to_string(), - }; - let iwad2 = WadInfo { - path: PathBuf::from("/home/wads/doom2.wad"), - name: "DOOM2.WAD".to_string(), - }; - let iwads = vec![iwad1, iwad2]; - let launcher = LauncherInfo { - path: PathBuf::from("/home/bin/gzdoom/gzdoom"), - name: "GZDoom".to_string(), - }; - let config = Config { - iwads: Some(iwads), - launchers: Some(vec![launcher]), - pwads: None, - iwads_added_folder: None, - iwads_removed_folder: None, - add_mode: None, - }; - let toml_as_str = toml::to_string(&config).unwrap(); - let new_config: Config = toml::from_str(&toml_as_str).unwrap(); - assert_eq!(config.iwads.unwrap().len(), new_config.iwads.unwrap().len()); + fn missing_iwad_folder() { + let config_str = "pwads_folder = \"/home/arthurr/Games/doom_wads/pwads\""; + let config = toml::from_str::(&config_str); + assert!(config.is_err()); } } diff --git a/src/infos.rs b/src/infos.rs index 699bea4..f93383a 100644 --- a/src/infos.rs +++ b/src/infos.rs @@ -123,7 +123,6 @@ impl CommandManager { pub struct WadInfo { pub path: PathBuf, pub name: String, - #[serde(skip_deserializing)] pub info_text: Option, } diff --git a/src/main.rs b/src/main.rs index 9b12f6f..af08851 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ -use config::{default_save_filename, load_config, save_config, Config}; -use wad::get_summary; +use config::{default_config_filename, load_config, Config}; use eframe::egui; use eframe::egui::Color32; use infos::{ @@ -44,8 +43,6 @@ struct RustDoomLauncher { add_stuff_window_displayed: bool, selected_file_type: FileType, selected_file_path: PathBuf, - save_config_window_displayed: bool, - close_application: bool, } impl Default for RustDoomLauncher { @@ -56,44 +53,30 @@ impl Default for RustDoomLauncher { pwad_manager: MultiManager::new(), command_manager: CommandManager::default(), add_name: "".to_string(), - config_filename: default_save_filename(), + config_filename: default_config_filename(), config: None, // TODO: put the config_filename in the config stuct - or something config_tried: false, display_command: false, add_stuff_window_displayed: false, selected_file_type: FileType::Pwad, selected_file_path: PathBuf::new(), - save_config_window_displayed: false, - close_application: false, } } } impl RustDoomLauncher { - // There must be a better way than this - maybe for the RustDoomLauncher to - // have a config thing fn get_config_file_stuff(&mut self) { - self.config = match load_config(&self.config_filename) { - Ok(c) => Some(c), - Err(_e) => None, + // TODO - Throw some kind of error if a config file isn't found and/or missing iwad folder + match load_config(&self.config_filename) { + Ok(c) => { + if let Some(launchers) = &c.launchers { + for launcher in launchers { + self.launcher_manager.add(&launcher); + } + } + }, + Err(_e) => {}, }; - if let Some(config) = &self.config { - if let Some(iwads) = &config.iwads { - for iwad in iwads { - self.iwad_manager.add(&iwad); - } - } - if let Some(pwads) = &config.pwads { - for pwad in pwads { - self.pwad_manager.add(&pwad.clone()); - } - } - if let Some(launchers) = &config.launchers { - for launcher in launchers { - self.launcher_manager.add(&launcher); - } - } - } } fn launch_doom(&self) { @@ -140,11 +123,6 @@ impl RustDoomLauncher { } impl eframe::App for RustDoomLauncher { - fn on_close_event(&mut self) -> bool { - self.save_config_window_displayed = true; - self.close_application - } - fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { if !self.config_tried { self.get_config_file_stuff(); @@ -371,37 +349,6 @@ impl eframe::App for RustDoomLauncher { } }); - if self.save_config_window_displayed { - egui::Window::new("Save WADs?") - .collapsible(false) - .resizable(false) - .show(ctx, |ui| { - ui.vertical(|ui| { - ui.label("Would you like to save the WADs you've added/deleted?"); - ui.horizontal(|ui| { - if ui.button("Yes").clicked() { - self.close_application = true; - let config = Config { - iwads: Some(self.iwad_manager.selectable.clone()), - pwads: Some(self.pwad_manager.selectable.clone()), - launchers: Some(self.launcher_manager.selectable.clone()), - ..Default::default() - }; - save_config(&self.config_filename, &config).unwrap(); - frame.close(); - } - if ui.button("No").clicked() { - self.close_application = true; - frame.close(); - }; - if ui.button("Don't Quit").clicked() { - self.save_config_window_displayed = false; - }; - }); - }); - }); - } - egui::Window::new("Add WAD or Launcher") .open(&mut self.add_stuff_window_displayed) .show(ctx, |ui| {