Skip to content

Commit

Permalink
Handle the case if config was regenerated but it's not our first time
Browse files Browse the repository at this point in the history
  • Loading branch information
MolotovCherry committed Jan 14, 2025
1 parent 4f297bd commit cfbe4b3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
2 changes: 1 addition & 1 deletion crates/loader/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
31 changes: 28 additions & 3 deletions crates/shared/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,33 @@ impl Default for Log {
}
}

pub fn get_config() -> Result<&'static Config> {
static CONFIG: LazyLock<Result<Config>> = 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<Result<ConfigState>> = 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())?;

if let Err(e) = fs::write(&path, json) {
error!("failed to save config: {e}");
return Err(e.into());
}

new = true;
}

let config = match fs::read_to_string(path) {
Expand All @@ -94,7 +110,16 @@ pub fn get_config() -> Result<&'static Config> {
};

match toml::from_str::<Config>(&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())
Expand Down
41 changes: 26 additions & 15 deletions crates/yabg3nml/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -58,7 +58,31 @@ pub fn init() -> Result<InitData> {

// 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 <bg3_root>/bin or <bg3_root>/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}"));
}
Expand All @@ -67,19 +91,6 @@ pub fn init() -> Result<InitData> {
// 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 <bg3_root>/bin or <bg3_root>/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:?}");
Expand Down

0 comments on commit cfbe4b3

Please sign in to comment.