From cfbe4b374780f3f05f16a60dce7ad243ad055de5 Mon Sep 17 00:00:00 2001 From: Cherry <13651622+MolotovCherry@users.noreply.github.com> Date: Tue, 14 Jan 2025 09:51:20 -0800 Subject: [PATCH] Handle the case if config was regenerated but it's not our first time --- crates/loader/src/loader.rs | 2 +- crates/shared/src/config.rs | 31 ++++++++++++++++++++++++--- crates/yabg3nml/src/setup.rs | 41 +++++++++++++++++++++++------------- 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/crates/loader/src/loader.rs b/crates/loader/src/loader.rs index 031525c..1fb7e2d 100644 --- a/crates/loader/src/loader.rs +++ b/crates/loader/src/loader.rs @@ -23,7 +23,7 @@ pub fn load_plugins() -> Result<()> { // This function is safe because we upheld this requirement let plugins_dir = get_bg3_plugins_dir()?; - let config = get_config()?; + let config = get_config()?.get(); if !config.core.enabled { info!("Plugins are globally disabled. If you want to re-enable them, set [core]enabled in config.toml to true"); diff --git a/crates/shared/src/config.rs b/crates/shared/src/config.rs index 36e4170..db854c2 100644 --- a/crates/shared/src/config.rs +++ b/crates/shared/src/config.rs @@ -72,10 +72,24 @@ impl Default for Log { } } -pub fn get_config() -> Result<&'static Config> { - static CONFIG: LazyLock> = LazyLock::new(|| { +pub enum ConfigState { + Exists(Config), + New(Config), +} + +impl ConfigState { + pub fn get(&self) -> &Config { + match self { + Self::Exists(config) | Self::New(config) => config, + } + } +} + +pub fn get_config() -> Result<&'static ConfigState> { + static CONFIG: LazyLock> = LazyLock::new(|| { let path = get_bg3_plugins_dir()?.join("config.toml"); + let mut new = false; if !path.exists() { let json = toml::to_string_pretty(&Config::default())?; @@ -83,6 +97,8 @@ pub fn get_config() -> Result<&'static Config> { error!("failed to save config: {e}"); return Err(e.into()); } + + new = true; } let config = match fs::read_to_string(path) { @@ -94,7 +110,16 @@ pub fn get_config() -> Result<&'static Config> { }; match toml::from_str::(&config) { - Ok(v) => Ok(v), + Ok(v) => { + let state = if new { + ConfigState::New(v) + } else { + ConfigState::Exists(v) + }; + + Ok(state) + } + Err(e) => { error!("failed to deserialize config: {e}"); Err(e.into()) diff --git a/crates/yabg3nml/src/setup.rs b/crates/yabg3nml/src/setup.rs index 8bd848f..d94a3a9 100644 --- a/crates/yabg3nml/src/setup.rs +++ b/crates/yabg3nml/src/setup.rs @@ -2,7 +2,7 @@ use std::{process, thread}; use eyre::{Context as _, Result}; use shared::{ - config::{get_config, Config}, + config::{get_config, Config, ConfigState}, paths::{get_bg3_local_dir, get_bg3_plugins_dir}, popup::{display_popup, fatal_popup, MessageBoxIcon}, }; @@ -58,7 +58,31 @@ pub fn init() -> Result { // get/create config let config = match get_config() { - Ok(v) => v, + Ok(ConfigState::Exists(c)) => c, + + Ok(ConfigState::New(_)) if first_time => { + display_popup( + "Finish Setup", + format!( + "The plugins folder was just created at\n{}\n\nTo install plugins, place the plugin dll files inside the plugins folder.\n\nPlease also double-check `config.toml` in the plugins folder. install_root in the config likely needs to be adjusted to the correct path. If the tools are placed in /bin or /bin/subfolder, the tools will automatically detect the correct root path and do not require install_root to be configured, otherwise you need to configure install_root", + plugins_dir.display() + ), + MessageBoxIcon::Info, + ); + + process::exit(0); + } + + Ok(ConfigState::New(_)) => { + display_popup( + "Recreated Config", + "`config.toml` was recreated from scratch because it was missing. Please double-check it to ensure the configuration is correct.", + MessageBoxIcon::Info, + ); + + process::exit(0); + } + Err(e) => { fatal_popup("Error reading config", format!("Failed to get config file. Most likely either it failed to read the file, or your config file is malformed.\n\nError: {e}")); } @@ -67,19 +91,6 @@ pub fn init() -> Result { // start logger let worker_guard = setup_logs(config, &plugins_dir).context("Failed to set up logs")?; - if first_time { - display_popup( - "Finish Setup", - format!( - "The plugins folder was just created at\n{}\n\nTo install plugins, place the plugin dll files inside the plugins folder.\n\nPlease also double-check `config.toml` in the plugins folder. install_root in the config likely needs to be adjusted to the correct path. If the tools are placed in /bin or /bin/subfolder, the tools will automatically detect the correct root path and do not require install_root to be configured, otherwise you need to configure install_root", - plugins_dir.display() - ), - MessageBoxIcon::Info, - ); - - process::exit(0); - } - let loader = init_loader()?; trace!("Got config: {config:?}");