config is neater

This commit is contained in:
Alex Janka 2023-10-05 21:38:43 +11:00
parent 3349576aa1
commit d4bc05b803
2 changed files with 11 additions and 11 deletions

View file

@ -80,8 +80,10 @@ struct Configs {
config_dir: PathBuf, config_dir: PathBuf,
} }
impl Configs { static CONFIGS: OnceLock<Configs> = OnceLock::new();
fn get() -> Self {
fn access_config<'a>() -> &'a Configs {
CONFIGS.get_or_init(|| {
let config_manager = ConfigManager::get().expect("Could not open config folder"); let config_manager = ConfigManager::get().expect("Could not open config folder");
let emu_config = config_manager.load_or_create_base_config(); let emu_config = config_manager.load_or_create_base_config();
let vst_config = config_manager.load_or_create_config::<VstConfig>("vst"); let vst_config = config_manager.load_or_create_config::<VstConfig>("vst");
@ -90,12 +92,12 @@ impl Configs {
panic!("no rom provided!!"); panic!("no rom provided!!");
} }
Self { Configs {
vst_config, vst_config,
emu_config, emu_config,
config_dir: config_manager.dir(), config_dir: config_manager.dir(),
} }
} })
} }
#[derive(Default)] #[derive(Default)]
@ -116,8 +118,6 @@ type JoypadSender = Mutex<Option<Sender<JoypadInfo>>>;
const BUFFERS_PER_FRAME: usize = 1; const BUFFERS_PER_FRAME: usize = 1;
const DOWNSAMPLE_TYPE: DownsampleType = DownsampleType::Linear; const DOWNSAMPLE_TYPE: DownsampleType = DownsampleType::Linear;
static CONFIGS: OnceLock<Configs> = OnceLock::new();
impl Plugin for GameboyEmu { impl Plugin for GameboyEmu {
const NAME: &'static str = "Gameboy"; const NAME: &'static str = "Gameboy";
@ -248,7 +248,7 @@ impl Plugin for GameboyEmu {
} }
fn editor(&mut self, _: AsyncExecutor<Self>) -> Option<Box<dyn Editor>> { fn editor(&mut self, _: AsyncExecutor<Self>) -> Option<Box<dyn Editor>> {
let configs = CONFIGS.get_or_init(Configs::get); let configs = access_config();
let size = Size::new( let size = Size::new(
(WIDTH * configs.vst_config.scale_factor) as f64, (WIDTH * configs.vst_config.scale_factor) as f64,
@ -279,8 +279,7 @@ impl Plugin for GameboyEmu {
vars.emulator_core.replace_output(output); vars.emulator_core.replace_output(output);
vars.rx = rx; vars.rx = rx;
} else { } 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()); let rom_path = configs.config_dir.join(configs.vst_config.rom.clone());
if !rom_path.is_file() { if !rom_path.is_file() {
panic!("{rom_path:?} is not a file!"); panic!("{rom_path:?} is not a file!");
@ -350,6 +349,7 @@ impl Plugin for GameboyEmu {
} }
fn deactivate(&mut self) { fn deactivate(&mut self) {
eprintln!("DEACTIVATE FUNCTION");
if let Some(ref mut vars) = self.vars { if let Some(ref mut vars) = self.vars {
match vars.sender.send(EmulatorMessage::Stop) { match vars.sender.send(EmulatorMessage::Stop) {
Ok(_) => self.vars = None, Ok(_) => self.vars = None,

View file

@ -14,7 +14,7 @@ use gb_emu_lib::{
use keyboard_types::{Code, KeyState}; use keyboard_types::{Code, KeyState};
use nih_plug::prelude::*; use nih_plug::prelude::*;
use crate::{Configs, Frame, FrameReceiver, JoypadInfo, JoypadSender, CONFIGS}; use crate::{access_config, Frame, FrameReceiver, JoypadInfo, JoypadSender};
pub struct Emulator { pub struct Emulator {
frame_receiver: Arc<FrameReceiver>, frame_receiver: Arc<FrameReceiver>,
@ -63,7 +63,7 @@ impl Editor for Emulator {
#[cfg(feature = "vulkan")] #[cfg(feature = "vulkan")]
let shader_path = self.shader_path.lock().unwrap().clone(); let shader_path = self.shader_path.lock().unwrap().clone();
// let shader_path = None; // 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( let size = Size::new(
(WIDTH * scale_factor) as f64, (WIDTH * scale_factor) as f64,