From d4bc05b803a9cdc983d875cc9b27a671162f5dfb Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Thu, 5 Oct 2023 21:38:43 +1100 Subject: [PATCH] config is neater --- gb-vst/src/lib.rs | 18 +++++++++--------- gb-vst/src/ui.rs | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/gb-vst/src/lib.rs b/gb-vst/src/lib.rs index 3d4facd..54fcba9 100644 --- a/gb-vst/src/lib.rs +++ b/gb-vst/src/lib.rs @@ -80,8 +80,10 @@ struct Configs { config_dir: PathBuf, } -impl Configs { - fn get() -> Self { +static CONFIGS: OnceLock = OnceLock::new(); + +fn access_config<'a>() -> &'a Configs { + CONFIGS.get_or_init(|| { let config_manager = ConfigManager::get().expect("Could not open config folder"); let emu_config = config_manager.load_or_create_base_config(); let vst_config = config_manager.load_or_create_config::("vst"); @@ -90,12 +92,12 @@ impl Configs { panic!("no rom provided!!"); } - Self { + Configs { vst_config, emu_config, config_dir: config_manager.dir(), } - } + }) } #[derive(Default)] @@ -116,8 +118,6 @@ type JoypadSender = Mutex>>; const BUFFERS_PER_FRAME: usize = 1; const DOWNSAMPLE_TYPE: DownsampleType = DownsampleType::Linear; -static CONFIGS: OnceLock = OnceLock::new(); - impl Plugin for GameboyEmu { const NAME: &'static str = "Gameboy"; @@ -248,7 +248,7 @@ impl Plugin for GameboyEmu { } fn editor(&mut self, _: AsyncExecutor) -> Option> { - let configs = CONFIGS.get_or_init(Configs::get); + let configs = access_config(); let size = Size::new( (WIDTH * configs.vst_config.scale_factor) as f64, @@ -279,8 +279,7 @@ impl Plugin for GameboyEmu { vars.emulator_core.replace_output(output); vars.rx = rx; } else { - let configs = CONFIGS.get_or_init(Configs::get); - + let configs = access_config(); let rom_path = configs.config_dir.join(configs.vst_config.rom.clone()); if !rom_path.is_file() { panic!("{rom_path:?} is not a file!"); @@ -350,6 +349,7 @@ impl Plugin for GameboyEmu { } fn deactivate(&mut self) { + eprintln!("DEACTIVATE FUNCTION"); if let Some(ref mut vars) = self.vars { match vars.sender.send(EmulatorMessage::Stop) { Ok(_) => self.vars = None, diff --git a/gb-vst/src/ui.rs b/gb-vst/src/ui.rs index 44718a1..82cc6be 100644 --- a/gb-vst/src/ui.rs +++ b/gb-vst/src/ui.rs @@ -14,7 +14,7 @@ use gb_emu_lib::{ use keyboard_types::{Code, KeyState}; use nih_plug::prelude::*; -use crate::{Configs, Frame, FrameReceiver, JoypadInfo, JoypadSender, CONFIGS}; +use crate::{access_config, Frame, FrameReceiver, JoypadInfo, JoypadSender}; pub struct Emulator { frame_receiver: Arc, @@ -63,7 +63,7 @@ impl Editor for Emulator { #[cfg(feature = "vulkan")] let shader_path = self.shader_path.lock().unwrap().clone(); // let shader_path = None; - let scale_factor = CONFIGS.get_or_init(Configs::get).vst_config.scale_factor; + let scale_factor = access_config().vst_config.scale_factor; let size = Size::new( (WIDTH * scale_factor) as f64,