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",
"futures",
"itertools",
"lazy_static",
"librashader",
"librashader-common",
"librashader-presets",

View file

@ -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::<frontend_common::StandaloneConfig>()
.and_then(|v| ConfigManager::get_custom_config_string(v).ok()),
}

View file

@ -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<EmulatorMessage>,
) -> PreparedEmulator<NoCamera> {
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 {

View file

@ -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<bool> = 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,7 +104,8 @@ impl Plugin for GameboyEmu {
const VERSION: &'static str = "0.1";
const AUDIO_IO_LAYOUTS: &'static [AudioIOLayout] = &[
const AUDIO_IO_LAYOUTS: &'static [AudioIOLayout] =
&[
AudioIOLayout {
main_input_channels: None,
main_output_channels: NonZeroU32::new(2),

View file

@ -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,
}
}

View file

@ -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");
}

View file

@ -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"] }

View file

@ -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<Self> {
fn get() -> Option<Self> {
directories::ProjectDirs::from("com", "alexjanka", "TWINC")
.map(|v| v.config_dir().to_path_buf())
.map(|path| {