Added "remembering" launchers and wads added
This commit is contained in:
@@ -6,6 +6,7 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
directories = "5.0.1"
|
||||
eframe = "0.21.3"
|
||||
rfd = "0.11.4"
|
||||
serde = "1.0.163"
|
||||
|
||||
@@ -1,25 +1,48 @@
|
||||
use toml;
|
||||
use crate::infos::{LauncherInfo, WadInfo};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use crate::infos::{WadInfo, LauncherInfo};
|
||||
use std::path::PathBuf;
|
||||
use toml;
|
||||
use directories;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct Config {
|
||||
iwads: Option<Vec<WadInfo>>,
|
||||
pwads: Option<Vec<WadInfo>>,
|
||||
launchers: Option<Vec<LauncherInfo>>,
|
||||
pub iwads: Option<Vec<WadInfo>>,
|
||||
pub pwads: Option<Vec<WadInfo>>,
|
||||
pub launchers: Option<Vec<LauncherInfo>>,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Config {
|
||||
iwads: None,
|
||||
pwads: None,
|
||||
launchers: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load_config(path: &PathBuf) -> Result<Config, Box<dyn std::error::Error>> {
|
||||
if !path.exists() {
|
||||
return Ok(Config::default());
|
||||
}
|
||||
let content = std::fs::read_to_string(&path)?;
|
||||
Ok(toml::from_str(&content)?)
|
||||
}
|
||||
|
||||
pub fn save_config(path: &PathBuf, config: &Config) -> Result<(), Box<dyn std::error::Error>> {
|
||||
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 mut full_path = PathBuf::new();
|
||||
full_path.push(project.config_dir());
|
||||
full_path.push("wads_and_stuff.toml");
|
||||
full_path
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@@ -50,7 +73,10 @@ path = \"/home/wads/doom1.9.wad\"";
|
||||
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.iwads.unwrap()[0].path, PathBuf::from("/home/wads/doom.wad"));
|
||||
assert_eq!(
|
||||
config.iwads.unwrap()[0].path,
|
||||
PathBuf::from("/home/wads/doom.wad")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -71,7 +97,10 @@ path = \"/home/pwads/aa.pk3\"";
|
||||
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"));
|
||||
assert_eq!(
|
||||
config.pwads.unwrap()[0].path,
|
||||
PathBuf::from("/home/pwads/aa.pk3")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -98,5 +127,4 @@ path = \"/home/pwads/aa.pk3\"";
|
||||
let new_config: Config = toml::from_str(&toml_as_str).unwrap();
|
||||
assert_eq!(config.iwads.unwrap().len(), new_config.iwads.unwrap().len());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,13 +6,13 @@ use std::path::PathBuf;
|
||||
// and perhaps the path to string conversion too could be
|
||||
// removed from the GUI part
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct WadInfo {
|
||||
pub path: PathBuf,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct LauncherInfo {
|
||||
pub path: PathBuf,
|
||||
pub name: String,
|
||||
|
||||
47
src/main.rs
47
src/main.rs
@@ -1,18 +1,18 @@
|
||||
use eframe::egui;
|
||||
use std::path::PathBuf;
|
||||
use config::{default_save_filename, save_config, load_config, Config};
|
||||
use infos::{WadInfo, LauncherInfo};
|
||||
|
||||
pub mod config;
|
||||
pub mod infos;
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
println!("Hello, world!");
|
||||
let options = eframe::NativeOptions {
|
||||
let gui_options = eframe::NativeOptions {
|
||||
..Default::default()
|
||||
};
|
||||
eframe::run_native(
|
||||
"Rust Doom Launcher",
|
||||
options,
|
||||
gui_options,
|
||||
Box::new(|_cc| Box::<RustDoomLauncher>::default()),
|
||||
)
|
||||
}
|
||||
@@ -25,6 +25,8 @@ struct RustDoomLauncher {
|
||||
selected_iwad: Option<usize>,
|
||||
selected_pwads: Vec<usize>,
|
||||
name: String,
|
||||
config_filename: PathBuf,
|
||||
config_file_loaded: bool,
|
||||
}
|
||||
|
||||
impl Default for RustDoomLauncher {
|
||||
@@ -37,12 +39,51 @@ impl Default for RustDoomLauncher {
|
||||
selected_iwad: None,
|
||||
selected_pwads: Vec::new(),
|
||||
name: "".to_string(),
|
||||
config_filename: default_save_filename(),
|
||||
config_file_loaded: 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) -> () {
|
||||
let config = load_config(&self.config_filename).unwrap();
|
||||
if let Some(iwads) = config.iwads {
|
||||
for iwad in iwads {
|
||||
self.all_iwads.push(iwad.clone());
|
||||
}
|
||||
}
|
||||
if let Some(pwads) = config.pwads {
|
||||
for pwad in pwads {
|
||||
self.all_pwads.push(pwad.clone());
|
||||
}
|
||||
}
|
||||
if let Some(launchers) = config.launchers {
|
||||
for launcher in launchers {
|
||||
self.all_launchers.push(launcher.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for RustDoomLauncher {
|
||||
fn on_close_event(&mut self) -> bool {
|
||||
let config = Config {
|
||||
iwads: Some(self.all_iwads.clone()),
|
||||
pwads: Some(self.all_pwads.clone()),
|
||||
launchers: Some(self.all_launchers.clone()),
|
||||
};
|
||||
save_config(&self.config_filename, &config);
|
||||
true
|
||||
}
|
||||
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
if self.config_file_loaded == false {
|
||||
self.get_config_file_stuff();
|
||||
self.config_file_loaded = true;
|
||||
}
|
||||
egui::containers::panel::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("RustDoomLauncher");
|
||||
ui.horizontal_wrapped(|ui| {
|
||||
|
||||
Reference in New Issue
Block a user