From 6d5530efe6bf46670632c8dcc89dc1d6be5c982a Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Tue, 28 Nov 2023 12:06:44 +1100 Subject: [PATCH] use lazy static config manager --- Cargo.lock | 1 + cli/src/main.rs | 8 ++--- frontend-common/src/lib.rs | 11 +++--- gb-vst/src/plugin.rs | 54 +++++++++++++++--------------- gui/src/macos/preferences.rs | 12 ++----- gui/src/macos/preferences/views.rs | 40 ++++++++++------------ lib/Cargo.toml | 1 + lib/src/config/mod.rs | 8 ++++- 8 files changed, 64 insertions(+), 71 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index edb91a9..8b7e0fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1461,6 +1461,7 @@ dependencies = [ "directories", "futures", "itertools", + "lazy_static", "librashader", "librashader-common", "librashader-presets", diff --git a/cli/src/main.rs b/cli/src/main.rs index 41b4959..ffd4a65 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -5,7 +5,7 @@ use camera::Webcam; use clap::{ArgGroup, Parser, Subcommand, ValueEnum}; use frontend_common::{audio, debug::Debugger, window::winit_manager::WinitWindowManager}; use gb_emu_lib::{ - config::ConfigManager, + config::{ConfigManager, CONFIG_MANAGER}, connect::{EmulatorCoreTrait, EmulatorMessage, SerialTarget, SramType, StdoutType}, }; use std::{ @@ -130,14 +130,12 @@ fn main() { match subcommand { Commands::PrintConfig { config, current } => { if let Some(string) = if current { - let config_manager = - ConfigManager::get().expect("Could not open config folder"); match config { ConfigType::Base => ConfigManager::get_custom_config_string( - config_manager.load_or_create_base_config(), + CONFIG_MANAGER.load_or_create_base_config(), ) .ok(), - ConfigType::Standalone => config_manager + ConfigType::Standalone => CONFIG_MANAGER .load_custom_config::() .and_then(|v| ConfigManager::get_custom_config_string(v).ok()), } diff --git a/frontend-common/src/lib.rs b/frontend-common/src/lib.rs index a7b70a0..0771ebe 100644 --- a/frontend-common/src/lib.rs +++ b/frontend-common/src/lib.rs @@ -4,7 +4,7 @@ use camera::Webcam; use gb_emu_lib::{ - config::{ConfigManager, NamedConfig}, + config::{NamedConfig, CONFIG_MANAGER}, connect::{ AudioOutput, CameraWrapper, CgbRomType, EmulatorMessage, EmulatorOptions, NoCamera, Rom, RomFile, SerialTarget, SramType, @@ -114,9 +114,8 @@ pub fn prepare( options: RunOptions, receiver: Receiver, ) -> PreparedEmulator { - let config_manager = ConfigManager::get().expect("Could not open config folder"); - let config = config_manager.load_or_create_base_config(); - let standalone_config: StandaloneConfig = config_manager.load_or_create_config(); + let config = CONFIG_MANAGER.load_or_create_base_config(); + let standalone_config: StandaloneConfig = CONFIG_MANAGER.load_or_create_config(); let rom_file = RomFile::Path(options.rom); @@ -127,7 +126,7 @@ pub fn prepare( let configs = CONFIGS.get_or_init(|| Configs { standalone_config, emu_config: config.clone(), - config_dir: config_manager.dir(), + config_dir: CONFIG_MANAGER.dir(), rom_title: rom.get_title().to_owned(), }); @@ -139,7 +138,7 @@ pub fn prepare( } else { config.vulkan_config.dmg_shader_path.as_ref() } - .map(|v| config_manager.dir().join(v)); + .map(|v| CONFIG_MANAGER.dir().join(v)); let resizable = shader_path.is_some() && if will_be_cgb { diff --git a/gb-vst/src/plugin.rs b/gb-vst/src/plugin.rs index 6e7d5dc..ff5e011 100644 --- a/gb-vst/src/plugin.rs +++ b/gb-vst/src/plugin.rs @@ -2,7 +2,7 @@ use async_ringbuf::AsyncHeapConsumer; use baseview::Size; use futures::executor; use gb_emu_lib::{ - config::ConfigManager, + config::CONFIG_MANAGER, connect::{ AudioOutput, CgbRomType, DownsampleType, EmulatorCoreTrait, EmulatorMessage, EmulatorOptions, NoCamera, RendererMessage, RomFile, SerialTarget, @@ -72,14 +72,13 @@ static IS_CGB: 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: VstConfig = config_manager.load_or_create_config(); + let emu_config = CONFIG_MANAGER.load_or_create_base_config(); + let vst_config: VstConfig = CONFIG_MANAGER.load_or_create_config(); Configs { vst_config, emu_config, - config_dir: config_manager.dir(), + config_dir: CONFIG_MANAGER.dir(), } }) } @@ -105,32 +104,33 @@ impl Plugin for GameboyEmu { const VERSION: &'static str = "0.1"; - const AUDIO_IO_LAYOUTS: &'static [AudioIOLayout] = &[ - AudioIOLayout { - main_input_channels: None, - main_output_channels: NonZeroU32::new(2), + const AUDIO_IO_LAYOUTS: &'static [AudioIOLayout] = + &[ + AudioIOLayout { + main_input_channels: None, + main_output_channels: NonZeroU32::new(2), - aux_input_ports: &[], - aux_output_ports: &[], + aux_input_ports: &[], + aux_output_ports: &[], - // Individual ports and the layout as a whole can be named here. By default these names - // are generated as needed. This layout will be called 'Stereo', while the other one is - // given the name 'Mono' based no the number of input and output channels. - names: PortNames::const_default(), - }, - AudioIOLayout { - main_input_channels: None, - main_output_channels: NonZeroU32::new(1), + // Individual ports and the layout as a whole can be named here. By default these names + // are generated as needed. This layout will be called 'Stereo', while the other one is + // given the name 'Mono' based no the number of input and output channels. + names: PortNames::const_default(), + }, + AudioIOLayout { + main_input_channels: None, + main_output_channels: NonZeroU32::new(1), - aux_input_ports: &[], - aux_output_ports: &[], + aux_input_ports: &[], + aux_output_ports: &[], - // Individual ports and the layout as a whole can be named here. By default these names - // are generated as needed. This layout will be called 'Stereo', while the other one is - // given the name 'Mono' based no the number of input and output channels. - names: PortNames::const_default(), - }, - ]; + // Individual ports and the layout as a whole can be named here. By default these names + // are generated as needed. This layout will be called 'Stereo', while the other one is + // given the name 'Mono' based no the number of input and output channels. + names: PortNames::const_default(), + }, + ]; const MIDI_INPUT: MidiConfig = MidiConfig::MidiCCs; const SAMPLE_ACCURATE_AUTOMATION: bool = true; diff --git a/gui/src/macos/preferences.rs b/gui/src/macos/preferences.rs index 9af2d7a..b08d47d 100644 --- a/gui/src/macos/preferences.rs +++ b/gui/src/macos/preferences.rs @@ -5,7 +5,6 @@ use cacao::{ }, view::ViewController, }; -use gb_emu_lib::config::ConfigManager; use self::{ toolbar::PreferencesToolbar, @@ -41,16 +40,11 @@ pub(crate) struct PreferencesUi { impl PreferencesUi { pub(crate) fn new() -> Self { - let config_manager = ConfigManager::get().expect("Couldn't load config dir"); Self { toolbar: Toolbar::new("PreferencesToolbar", PreferencesToolbar::default()), - core_prefs: ViewController::new(CorePreferencesContentView::new( - config_manager.clone(), - )), - standalone_prefs: ViewController::new(StandalonePreferencesContentView::new( - config_manager.clone(), - )), - vst_prefs: ViewController::new(VstPreferencesContentView::new(config_manager)), + core_prefs: ViewController::new(CorePreferencesContentView::new()), + standalone_prefs: ViewController::new(StandalonePreferencesContentView::new()), + vst_prefs: ViewController::new(VstPreferencesContentView::new()), window: None, } } diff --git a/gui/src/macos/preferences/views.rs b/gui/src/macos/preferences/views.rs index 88f5798..f620e68 100644 --- a/gui/src/macos/preferences/views.rs +++ b/gui/src/macos/preferences/views.rs @@ -5,7 +5,7 @@ use cacao::{ view::{View, ViewDelegate}, }; use frontend_common::StandaloneConfig; -use gb_emu_lib::config::{Config, ConfigManager, ResolutionOverride}; +use gb_emu_lib::config::{Config, ResolutionOverride, CONFIG_MANAGER}; use crate::macos::dispatch; @@ -28,7 +28,6 @@ fn make_relative_path(path: PathBuf, base_dir: PathBuf) -> String { pub(crate) struct CorePreferencesContentView { config: Config, - config_manager: ConfigManager, dmg_bootrom: PathView, cgb_bootrom: PathView, show_bootrom: ToggleView, @@ -42,10 +41,9 @@ pub(crate) struct CorePreferencesContentView { } impl CorePreferencesContentView { - pub(crate) fn new(config_manager: ConfigManager) -> Self { + pub(crate) fn new() -> Self { Self { - config: config_manager.load_or_create_base_config(), - config_manager, + config: CONFIG_MANAGER.load_or_create_base_config(), dmg_bootrom: Default::default(), cgb_bootrom: Default::default(), show_bootrom: Default::default(), @@ -102,30 +100,28 @@ impl CorePreferencesContentView { .into(); } CorePreferencesUpdates::DmgBootrom(path) => { - self.config.dmg_bootrom = - path.map(|v| make_relative_path(v, self.config_manager.dir())); + self.config.dmg_bootrom = path.map(|v| make_relative_path(v, CONFIG_MANAGER.dir())); self.dmg_bootrom.update(self.config.dmg_bootrom.clone()); } CorePreferencesUpdates::CgbBootrom(path) => { - self.config.cgb_bootrom = - path.map(|v| make_relative_path(v, self.config_manager.dir())); + self.config.cgb_bootrom = path.map(|v| make_relative_path(v, CONFIG_MANAGER.dir())); self.cgb_bootrom.update(self.config.cgb_bootrom.clone()); } CorePreferencesUpdates::DmgShader(path) => { self.config.vulkan_config.dmg_shader_path = - path.map(|v| make_relative_path(v, self.config_manager.dir())); + path.map(|v| make_relative_path(v, CONFIG_MANAGER.dir())); self.dmg_shader .update(self.config.vulkan_config.dmg_shader_path.clone()); } CorePreferencesUpdates::CgbShader(path) => { self.config.vulkan_config.cgb_shader_path = - path.map(|v| make_relative_path(v, self.config_manager.dir())); + path.map(|v| make_relative_path(v, CONFIG_MANAGER.dir())); self.cgb_shader .update(self.config.vulkan_config.cgb_shader_path.clone()); } } - self.config_manager + CONFIG_MANAGER .save_custom_config(self.config.clone()) .expect("failed to save config"); } @@ -309,7 +305,6 @@ impl ViewDelegate for CorePreferencesContentView { pub(crate) struct StandalonePreferencesContentView { config: StandaloneConfig, - config_manager: ConfigManager, scale_factor: StepperView, group_screenshots_by_rom: ToggleView, buffers_per_frame: StepperView, @@ -317,10 +312,10 @@ pub(crate) struct StandalonePreferencesContentView { } impl StandalonePreferencesContentView { - pub(crate) fn new(config_manager: ConfigManager) -> Self { + pub(crate) fn new() -> Self { Self { - config: config_manager.load_or_create_config(), - config_manager, + config: CONFIG_MANAGER.load_or_create_config(), + group_screenshots_by_rom: Default::default(), scale_factor: Default::default(), buffers_per_frame: Default::default(), @@ -346,7 +341,7 @@ impl StandalonePreferencesContentView { }); } } - self.config_manager + CONFIG_MANAGER .save_custom_config(self.config.clone()) .expect("failed to save config"); } @@ -445,17 +440,16 @@ impl ViewDelegate for StandalonePreferencesContentView { pub(crate) struct VstPreferencesContentView { config: twinc_emu_vst::VstConfig, - config_manager: ConfigManager, scale_factor: StepperView, rom: PathView, force_skip_bootrom: ToggleView, } impl VstPreferencesContentView { - pub(crate) fn new(config_manager: ConfigManager) -> Self { + pub(crate) fn new() -> Self { Self { - config: config_manager.load_or_create_config(), - config_manager, + config: CONFIG_MANAGER.load_or_create_config(), + force_skip_bootrom: Default::default(), scale_factor: Default::default(), rom: Default::default(), @@ -472,12 +466,12 @@ impl VstPreferencesContentView { } VstPreferencesUpdates::Rom(path) => { if let Some(path) = path { - self.config.rom = make_relative_path(path, self.config_manager.dir()); + self.config.rom = make_relative_path(path, CONFIG_MANAGER.dir()); } self.rom.update(Some(self.config.rom.clone())); } } - self.config_manager + CONFIG_MANAGER .save_custom_config(self.config.clone()) .expect("failed to save config"); } diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 4145811..33baf9b 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -41,6 +41,7 @@ librashader-presets = { version = "0.1", optional = true } librashader-common = { version = "0.1", optional = true } directories = { version = "5.0", optional = true } ron = { version = "0.8", optional = true } +lazy_static = "1.4" [build-dependencies] naga = { version = "0.13", optional = true, features = ["wgsl-in", "spv-out"] } diff --git a/lib/src/config/mod.rs b/lib/src/config/mod.rs index 531f120..07e970f 100644 --- a/lib/src/config/mod.rs +++ b/lib/src/config/mod.rs @@ -4,8 +4,14 @@ use std::{ path::PathBuf, }; +use lazy_static::lazy_static; use serde::{de::DeserializeOwned, Deserialize, Serialize}; +lazy_static! { + pub static ref CONFIG_MANAGER: ConfigManager = + ConfigManager::get().expect("Error loading configmanager!"); +} + pub trait NamedConfig { fn name() -> String; } @@ -16,7 +22,7 @@ pub struct ConfigManager { } impl ConfigManager { - pub fn get() -> Option { + fn get() -> Option { directories::ProjectDirs::from("com", "alexjanka", "TWINC") .map(|v| v.config_dir().to_path_buf()) .map(|path| {