use lazy static config manager

This commit is contained in:
Alex Janka 2023-11-28 12:06:44 +11:00
parent a6dbfbae89
commit 6d5530efe6
8 changed files with 64 additions and 71 deletions

1
Cargo.lock generated
View file

@ -1461,6 +1461,7 @@ dependencies = [
"directories", "directories",
"futures", "futures",
"itertools", "itertools",
"lazy_static",
"librashader", "librashader",
"librashader-common", "librashader-common",
"librashader-presets", "librashader-presets",

View file

@ -5,7 +5,7 @@ use camera::Webcam;
use clap::{ArgGroup, Parser, Subcommand, ValueEnum}; use clap::{ArgGroup, Parser, Subcommand, ValueEnum};
use frontend_common::{audio, debug::Debugger, window::winit_manager::WinitWindowManager}; use frontend_common::{audio, debug::Debugger, window::winit_manager::WinitWindowManager};
use gb_emu_lib::{ use gb_emu_lib::{
config::ConfigManager, config::{ConfigManager, CONFIG_MANAGER},
connect::{EmulatorCoreTrait, EmulatorMessage, SerialTarget, SramType, StdoutType}, connect::{EmulatorCoreTrait, EmulatorMessage, SerialTarget, SramType, StdoutType},
}; };
use std::{ use std::{
@ -130,14 +130,12 @@ fn main() {
match subcommand { match subcommand {
Commands::PrintConfig { config, current } => { Commands::PrintConfig { config, current } => {
if let Some(string) = if current { if let Some(string) = if current {
let config_manager =
ConfigManager::get().expect("Could not open config folder");
match config { match config {
ConfigType::Base => ConfigManager::get_custom_config_string( ConfigType::Base => ConfigManager::get_custom_config_string(
config_manager.load_or_create_base_config(), CONFIG_MANAGER.load_or_create_base_config(),
) )
.ok(), .ok(),
ConfigType::Standalone => config_manager ConfigType::Standalone => CONFIG_MANAGER
.load_custom_config::<frontend_common::StandaloneConfig>() .load_custom_config::<frontend_common::StandaloneConfig>()
.and_then(|v| ConfigManager::get_custom_config_string(v).ok()), .and_then(|v| ConfigManager::get_custom_config_string(v).ok()),
} }

View file

@ -4,7 +4,7 @@
use camera::Webcam; use camera::Webcam;
use gb_emu_lib::{ use gb_emu_lib::{
config::{ConfigManager, NamedConfig}, config::{NamedConfig, CONFIG_MANAGER},
connect::{ connect::{
AudioOutput, CameraWrapper, CgbRomType, EmulatorMessage, EmulatorOptions, NoCamera, Rom, AudioOutput, CameraWrapper, CgbRomType, EmulatorMessage, EmulatorOptions, NoCamera, Rom,
RomFile, SerialTarget, SramType, RomFile, SerialTarget, SramType,
@ -114,9 +114,8 @@ pub fn prepare(
options: RunOptions, options: RunOptions,
receiver: Receiver<EmulatorMessage>, receiver: Receiver<EmulatorMessage>,
) -> PreparedEmulator<NoCamera> { ) -> PreparedEmulator<NoCamera> {
let config_manager = ConfigManager::get().expect("Could not open config folder"); let config = CONFIG_MANAGER.load_or_create_base_config();
let config = config_manager.load_or_create_base_config(); let standalone_config: StandaloneConfig = CONFIG_MANAGER.load_or_create_config();
let standalone_config: StandaloneConfig = config_manager.load_or_create_config();
let rom_file = RomFile::Path(options.rom); let rom_file = RomFile::Path(options.rom);
@ -127,7 +126,7 @@ pub fn prepare(
let configs = CONFIGS.get_or_init(|| Configs { let configs = CONFIGS.get_or_init(|| Configs {
standalone_config, standalone_config,
emu_config: config.clone(), emu_config: config.clone(),
config_dir: config_manager.dir(), config_dir: CONFIG_MANAGER.dir(),
rom_title: rom.get_title().to_owned(), rom_title: rom.get_title().to_owned(),
}); });
@ -139,7 +138,7 @@ pub fn prepare(
} else { } else {
config.vulkan_config.dmg_shader_path.as_ref() 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() let resizable = shader_path.is_some()
&& if will_be_cgb { && if will_be_cgb {

View file

@ -2,7 +2,7 @@ use async_ringbuf::AsyncHeapConsumer;
use baseview::Size; use baseview::Size;
use futures::executor; use futures::executor;
use gb_emu_lib::{ use gb_emu_lib::{
config::ConfigManager, config::CONFIG_MANAGER,
connect::{ connect::{
AudioOutput, CgbRomType, DownsampleType, EmulatorCoreTrait, EmulatorMessage, AudioOutput, CgbRomType, DownsampleType, EmulatorCoreTrait, EmulatorMessage,
EmulatorOptions, NoCamera, RendererMessage, RomFile, SerialTarget, EmulatorOptions, NoCamera, RendererMessage, RomFile, SerialTarget,
@ -72,14 +72,13 @@ static IS_CGB: OnceLock<bool> = OnceLock::new();
fn access_config<'a>() -> &'a Configs { fn access_config<'a>() -> &'a Configs {
CONFIGS.get_or_init(|| { 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 emu_config = config_manager.load_or_create_base_config(); let vst_config: VstConfig = CONFIG_MANAGER.load_or_create_config();
let vst_config: VstConfig = config_manager.load_or_create_config();
Configs { Configs {
vst_config, vst_config,
emu_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 VERSION: &'static str = "0.1";
const AUDIO_IO_LAYOUTS: &'static [AudioIOLayout] = &[ const AUDIO_IO_LAYOUTS: &'static [AudioIOLayout] =
AudioIOLayout { &[
main_input_channels: None, AudioIOLayout {
main_output_channels: NonZeroU32::new(2), main_input_channels: None,
main_output_channels: NonZeroU32::new(2),
aux_input_ports: &[], aux_input_ports: &[],
aux_output_ports: &[], aux_output_ports: &[],
// Individual ports and the layout as a whole can be named here. By default these names // 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 // 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. // given the name 'Mono' based no the number of input and output channels.
names: PortNames::const_default(), names: PortNames::const_default(),
}, },
AudioIOLayout { AudioIOLayout {
main_input_channels: None, main_input_channels: None,
main_output_channels: NonZeroU32::new(1), main_output_channels: NonZeroU32::new(1),
aux_input_ports: &[], aux_input_ports: &[],
aux_output_ports: &[], aux_output_ports: &[],
// Individual ports and the layout as a whole can be named here. By default these names // 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 // 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. // given the name 'Mono' based no the number of input and output channels.
names: PortNames::const_default(), names: PortNames::const_default(),
}, },
]; ];
const MIDI_INPUT: MidiConfig = MidiConfig::MidiCCs; const MIDI_INPUT: MidiConfig = MidiConfig::MidiCCs;
const SAMPLE_ACCURATE_AUTOMATION: bool = true; const SAMPLE_ACCURATE_AUTOMATION: bool = true;

View file

@ -5,7 +5,6 @@ use cacao::{
}, },
view::ViewController, view::ViewController,
}; };
use gb_emu_lib::config::ConfigManager;
use self::{ use self::{
toolbar::PreferencesToolbar, toolbar::PreferencesToolbar,
@ -41,16 +40,11 @@ pub(crate) struct PreferencesUi {
impl PreferencesUi { impl PreferencesUi {
pub(crate) fn new() -> Self { pub(crate) fn new() -> Self {
let config_manager = ConfigManager::get().expect("Couldn't load config dir");
Self { Self {
toolbar: Toolbar::new("PreferencesToolbar", PreferencesToolbar::default()), toolbar: Toolbar::new("PreferencesToolbar", PreferencesToolbar::default()),
core_prefs: ViewController::new(CorePreferencesContentView::new( core_prefs: ViewController::new(CorePreferencesContentView::new()),
config_manager.clone(), standalone_prefs: ViewController::new(StandalonePreferencesContentView::new()),
)), vst_prefs: ViewController::new(VstPreferencesContentView::new()),
standalone_prefs: ViewController::new(StandalonePreferencesContentView::new(
config_manager.clone(),
)),
vst_prefs: ViewController::new(VstPreferencesContentView::new(config_manager)),
window: None, window: None,
} }
} }

View file

@ -5,7 +5,7 @@ use cacao::{
view::{View, ViewDelegate}, view::{View, ViewDelegate},
}; };
use frontend_common::StandaloneConfig; 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; use crate::macos::dispatch;
@ -28,7 +28,6 @@ fn make_relative_path(path: PathBuf, base_dir: PathBuf) -> String {
pub(crate) struct CorePreferencesContentView { pub(crate) struct CorePreferencesContentView {
config: Config, config: Config,
config_manager: ConfigManager,
dmg_bootrom: PathView, dmg_bootrom: PathView,
cgb_bootrom: PathView, cgb_bootrom: PathView,
show_bootrom: ToggleView, show_bootrom: ToggleView,
@ -42,10 +41,9 @@ pub(crate) struct CorePreferencesContentView {
} }
impl CorePreferencesContentView { impl CorePreferencesContentView {
pub(crate) fn new(config_manager: ConfigManager) -> Self { pub(crate) fn new() -> Self {
Self { Self {
config: config_manager.load_or_create_base_config(), config: CONFIG_MANAGER.load_or_create_base_config(),
config_manager,
dmg_bootrom: Default::default(), dmg_bootrom: Default::default(),
cgb_bootrom: Default::default(), cgb_bootrom: Default::default(),
show_bootrom: Default::default(), show_bootrom: Default::default(),
@ -102,30 +100,28 @@ impl CorePreferencesContentView {
.into(); .into();
} }
CorePreferencesUpdates::DmgBootrom(path) => { CorePreferencesUpdates::DmgBootrom(path) => {
self.config.dmg_bootrom = self.config.dmg_bootrom = path.map(|v| make_relative_path(v, CONFIG_MANAGER.dir()));
path.map(|v| make_relative_path(v, self.config_manager.dir()));
self.dmg_bootrom.update(self.config.dmg_bootrom.clone()); self.dmg_bootrom.update(self.config.dmg_bootrom.clone());
} }
CorePreferencesUpdates::CgbBootrom(path) => { CorePreferencesUpdates::CgbBootrom(path) => {
self.config.cgb_bootrom = self.config.cgb_bootrom = path.map(|v| make_relative_path(v, CONFIG_MANAGER.dir()));
path.map(|v| make_relative_path(v, self.config_manager.dir()));
self.cgb_bootrom.update(self.config.cgb_bootrom.clone()); self.cgb_bootrom.update(self.config.cgb_bootrom.clone());
} }
CorePreferencesUpdates::DmgShader(path) => { CorePreferencesUpdates::DmgShader(path) => {
self.config.vulkan_config.dmg_shader_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 self.dmg_shader
.update(self.config.vulkan_config.dmg_shader_path.clone()); .update(self.config.vulkan_config.dmg_shader_path.clone());
} }
CorePreferencesUpdates::CgbShader(path) => { CorePreferencesUpdates::CgbShader(path) => {
self.config.vulkan_config.cgb_shader_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 self.cgb_shader
.update(self.config.vulkan_config.cgb_shader_path.clone()); .update(self.config.vulkan_config.cgb_shader_path.clone());
} }
} }
self.config_manager CONFIG_MANAGER
.save_custom_config(self.config.clone()) .save_custom_config(self.config.clone())
.expect("failed to save config"); .expect("failed to save config");
} }
@ -309,7 +305,6 @@ impl ViewDelegate for CorePreferencesContentView {
pub(crate) struct StandalonePreferencesContentView { pub(crate) struct StandalonePreferencesContentView {
config: StandaloneConfig, config: StandaloneConfig,
config_manager: ConfigManager,
scale_factor: StepperView, scale_factor: StepperView,
group_screenshots_by_rom: ToggleView, group_screenshots_by_rom: ToggleView,
buffers_per_frame: StepperView, buffers_per_frame: StepperView,
@ -317,10 +312,10 @@ pub(crate) struct StandalonePreferencesContentView {
} }
impl StandalonePreferencesContentView { impl StandalonePreferencesContentView {
pub(crate) fn new(config_manager: ConfigManager) -> Self { pub(crate) fn new() -> Self {
Self { Self {
config: config_manager.load_or_create_config(), config: CONFIG_MANAGER.load_or_create_config(),
config_manager,
group_screenshots_by_rom: Default::default(), group_screenshots_by_rom: Default::default(),
scale_factor: Default::default(), scale_factor: Default::default(),
buffers_per_frame: Default::default(), buffers_per_frame: Default::default(),
@ -346,7 +341,7 @@ impl StandalonePreferencesContentView {
}); });
} }
} }
self.config_manager CONFIG_MANAGER
.save_custom_config(self.config.clone()) .save_custom_config(self.config.clone())
.expect("failed to save config"); .expect("failed to save config");
} }
@ -445,17 +440,16 @@ impl ViewDelegate for StandalonePreferencesContentView {
pub(crate) struct VstPreferencesContentView { pub(crate) struct VstPreferencesContentView {
config: twinc_emu_vst::VstConfig, config: twinc_emu_vst::VstConfig,
config_manager: ConfigManager,
scale_factor: StepperView, scale_factor: StepperView,
rom: PathView, rom: PathView,
force_skip_bootrom: ToggleView, force_skip_bootrom: ToggleView,
} }
impl VstPreferencesContentView { impl VstPreferencesContentView {
pub(crate) fn new(config_manager: ConfigManager) -> Self { pub(crate) fn new() -> Self {
Self { Self {
config: config_manager.load_or_create_config(), config: CONFIG_MANAGER.load_or_create_config(),
config_manager,
force_skip_bootrom: Default::default(), force_skip_bootrom: Default::default(),
scale_factor: Default::default(), scale_factor: Default::default(),
rom: Default::default(), rom: Default::default(),
@ -472,12 +466,12 @@ impl VstPreferencesContentView {
} }
VstPreferencesUpdates::Rom(path) => { VstPreferencesUpdates::Rom(path) => {
if let Some(path) = 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.rom.update(Some(self.config.rom.clone()));
} }
} }
self.config_manager CONFIG_MANAGER
.save_custom_config(self.config.clone()) .save_custom_config(self.config.clone())
.expect("failed to save config"); .expect("failed to save config");
} }

View file

@ -41,6 +41,7 @@ librashader-presets = { version = "0.1", optional = true }
librashader-common = { version = "0.1", optional = true } librashader-common = { version = "0.1", optional = true }
directories = { version = "5.0", optional = true } directories = { version = "5.0", optional = true }
ron = { version = "0.8", optional = true } ron = { version = "0.8", optional = true }
lazy_static = "1.4"
[build-dependencies] [build-dependencies]
naga = { version = "0.13", optional = true, features = ["wgsl-in", "spv-out"] } naga = { version = "0.13", optional = true, features = ["wgsl-in", "spv-out"] }

View file

@ -4,8 +4,14 @@ use std::{
path::PathBuf, path::PathBuf,
}; };
use lazy_static::lazy_static;
use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};
lazy_static! {
pub static ref CONFIG_MANAGER: ConfigManager =
ConfigManager::get().expect("Error loading configmanager!");
}
pub trait NamedConfig { pub trait NamedConfig {
fn name() -> String; fn name() -> String;
} }
@ -16,7 +22,7 @@ pub struct ConfigManager {
} }
impl ConfigManager { impl ConfigManager {
pub fn get() -> Option<Self> { fn get() -> Option<Self> {
directories::ProjectDirs::from("com", "alexjanka", "TWINC") directories::ProjectDirs::from("com", "alexjanka", "TWINC")
.map(|v| v.config_dir().to_path_buf()) .map(|v| v.config_dir().to_path_buf())
.map(|path| { .map(|path| {