Partially done removing config parts

This commit is contained in:
2023-10-18 23:38:48 +01:00
parent 603b65ada6
commit 6122f99ff7
3 changed files with 55 additions and 156 deletions

View File

@@ -1,24 +1,18 @@
use crate::infos::{LauncherInfo, WadInfo}; use crate::infos::{LauncherInfo};
use directories;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::path::PathBuf; use std::path::PathBuf;
use toml; use toml;
use directories;
#[derive(Serialize, Deserialize, Debug)]
pub enum AddMode {
DoNothing,
Copy,
Move,
}
#[derive(Serialize, Deserialize, Debug, Default)] #[derive(Serialize, Deserialize, Debug, Default)]
pub struct Config { pub struct Config {
pub iwads_added_folder: Option<PathBuf>,
pub iwads_removed_folder: Option<PathBuf>,
pub add_mode: Option<AddMode>,
pub iwads: Option<Vec<WadInfo>>,
pub pwads: Option<Vec<WadInfo>>,
pub launchers: Option<Vec<LauncherInfo>>, pub launchers: Option<Vec<LauncherInfo>>,
pub iwads_folder: PathBuf,
pub iwads_old_folder: Option<PathBuf>,
pub pwads_folder: Option<PathBuf>,
pub pwads_old_folder: Option<PathBuf>,
pub mods_folder: Option<PathBuf>,
pub mods_old_folder: Option<PathBuf>,
} }
pub fn load_config(path: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> { pub fn load_config(path: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
@@ -29,18 +23,11 @@ pub fn load_config(path: &PathBuf) -> Result<Config, Box<dyn std::error::Error>>
Ok(toml::from_str(&content)?) Ok(toml::from_str(&content)?)
} }
pub fn save_config(path: &PathBuf, config: &Config) -> Result<(), Box<dyn std::error::Error>> { pub fn default_config_filename() -> PathBuf {
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 {
let project = directories::ProjectDirs::from("party", "arfy", "rdl").unwrap(); let project = directories::ProjectDirs::from("party", "arfy", "rdl").unwrap();
let mut full_path = PathBuf::new(); let mut full_path = PathBuf::new();
full_path.push(project.config_dir()); full_path.push(project.config_dir());
full_path.push("wads_and_stuff.toml"); full_path.push("wad_folders.toml");
full_path full_path
} }
@@ -49,85 +36,51 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn basic_iwad() { fn basic_folder() {
let config_str = "[[iwads]]\n\ let config_str = "iwads_folder = \"/home/arthurr/Games/doom_wads/iwads\"";
name = \"DOOM2.WAD\"\n\ let config: Config = toml::from_str(&config_str).unwrap();
path = \"/home/wads/doom.wad\"\n"; 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<Config, _> = toml::from_str(&config_str); let config: Result<Config, _> = toml::from_str(&config_str);
assert!(config.is_ok()); assert!(config.is_ok());
let config = config.unwrap(); let config = config.unwrap();
assert_eq!(config.iwads.unwrap().len(), 1); assert_eq!(config.launchers.unwrap().len(), 1);
} }
#[test] #[test]
fn basic_iwads() { fn basic_launchers() {
let config_str = "[[iwads]] let config_str = "iwads_folder = \"/home/arthurr/Games/doom_wads/iwads\"
name = \"DOOM2.WAD\" [[launchers]]
path = \"/home/wads/doom.wad\" name = \"GZDoom\"
path = \"/home/guy/gzdoom\"
[[iwads]] [[launchers]]
name = \"DOOM1.WAD1.9\" name = \"PrBoom\"
path = \"/home/wads/doom1.9.wad\""; path = \"/home/guy/prboom\"";
let config: Config = toml::from_str(&config_str).unwrap(); 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.launchers.as_ref().unwrap().len(), 2);
assert_eq!(config.iwads.as_ref().unwrap().len(), 2); assert_eq!(config.launchers.as_ref().unwrap()[0].name, "GZDoom");
assert_eq!(config.iwads.as_ref().unwrap()[0].name, "DOOM2.WAD"); assert_eq!(config.launchers.as_ref().unwrap()[1].name, "PrBoom");
assert_eq!(config.iwads.as_ref().unwrap()[1].name, "DOOM1.WAD1.9");
assert_eq!( assert_eq!(
config.iwads.unwrap()[0].path, config.launchers.unwrap()[0].path,
PathBuf::from("/home/wads/doom.wad") PathBuf::from("/home/guy/gzdoom")
); );
} }
#[test] #[test]
fn iwads_and_pwads() { fn missing_iwad_folder() {
let config_str = "[[iwads]] let config_str = "pwads_folder = \"/home/arthurr/Games/doom_wads/pwads\"";
name = \"DOOM2.WAD\" let config = toml::from_str::<Config>(&config_str);
path = \"/home/wads/doom.wad\" assert!(config.is_err());
[[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());
} }
} }

View File

@@ -123,7 +123,6 @@ impl CommandManager {
pub struct WadInfo { pub struct WadInfo {
pub path: PathBuf, pub path: PathBuf,
pub name: String, pub name: String,
#[serde(skip_deserializing)]
pub info_text: Option<String>, pub info_text: Option<String>,
} }

View File

@@ -1,5 +1,4 @@
use config::{default_save_filename, load_config, save_config, Config}; use config::{default_config_filename, load_config, Config};
use wad::get_summary;
use eframe::egui; use eframe::egui;
use eframe::egui::Color32; use eframe::egui::Color32;
use infos::{ use infos::{
@@ -44,8 +43,6 @@ struct RustDoomLauncher {
add_stuff_window_displayed: bool, add_stuff_window_displayed: bool,
selected_file_type: FileType, selected_file_type: FileType,
selected_file_path: PathBuf, selected_file_path: PathBuf,
save_config_window_displayed: bool,
close_application: bool,
} }
impl Default for RustDoomLauncher { impl Default for RustDoomLauncher {
@@ -56,44 +53,30 @@ impl Default for RustDoomLauncher {
pwad_manager: MultiManager::new(), pwad_manager: MultiManager::new(),
command_manager: CommandManager::default(), command_manager: CommandManager::default(),
add_name: "".to_string(), 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: None, // TODO: put the config_filename in the config stuct - or something
config_tried: false, config_tried: false,
display_command: false, display_command: false,
add_stuff_window_displayed: false, add_stuff_window_displayed: false,
selected_file_type: FileType::Pwad, selected_file_type: FileType::Pwad,
selected_file_path: PathBuf::new(), selected_file_path: PathBuf::new(),
save_config_window_displayed: false,
close_application: false,
} }
} }
} }
impl RustDoomLauncher { 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) { fn get_config_file_stuff(&mut self) {
self.config = match load_config(&self.config_filename) { // TODO - Throw some kind of error if a config file isn't found and/or missing iwad folder
Ok(c) => Some(c), match load_config(&self.config_filename) {
Err(_e) => None, 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) { fn launch_doom(&self) {
@@ -140,11 +123,6 @@ impl RustDoomLauncher {
} }
impl eframe::App for 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) { fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
if !self.config_tried { if !self.config_tried {
self.get_config_file_stuff(); 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") egui::Window::new("Add WAD or Launcher")
.open(&mut self.add_stuff_window_displayed) .open(&mut self.add_stuff_window_displayed)
.show(ctx, |ui| { .show(ctx, |ui| {