/*
* This file is part of sdbclient
* Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy::prelude::*;
use bevy_console::PrintConsoleLine;
use crate::{cfg, util};
/// This plugin will handle config related tasks, like saving and loading
pub struct ConfigPlugin;
impl Plugin for ConfigPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<Events<LoadEvent>>()
.init_resource::<Events<SaveEvent>>()
.add_system(handle_load_events)
.add_system(handle_save_events);
}
}
pub enum LoadEventValue {
Settings,
User,
Dev,
}
pub struct LoadEvent {
pub value: LoadEventValue,
}
fn handle_load_events(
mut events: EventReader<LoadEvent>,
mut cfg_settings: ResMut<cfg::CfgSettings>,
mut cfg_user: ResMut<cfg::CfgUser>,
mut cfg_dev: ResMut<cfg::CfgDev>,
cfg_dirs: ResMut<cfg::CfgDirs>,
mut console: EventWriter<PrintConsoleLine>,
) {
for event in events.iter() {
match &event.value {
LoadEventValue::Settings => {
*cfg_settings = util::sl::load(
cfg_dirs.0.data_local_dir().to_str().unwrap(),
cfg::FILE_CFG_SETTINGS,
&mut console,
);
}
LoadEventValue::User => {
*cfg_user = util::sl::load(
cfg_dirs.0.data_local_dir().to_str().unwrap(),
cfg::FILE_CFG_USER,
&mut console,
);
}
LoadEventValue::Dev => {
*cfg_dev = util::sl::load(
cfg_dirs.0.data_local_dir().to_str().unwrap(),
cfg::FILE_CFG_DEV,
&mut console,
);
}
}
}
}
pub enum SaveEventValue {
Settings(cfg::CfgSettings),
User(cfg::CfgUser),
Dev(cfg::CfgDev),
}
pub struct SaveEvent {
pub value: SaveEventValue,
}
fn handle_save_events(
mut events: EventReader<SaveEvent>,
cfg_dirs: Res<cfg::CfgDirs>,
mut console: EventWriter<PrintConsoleLine>,
) {
for event in events.iter() {
match &event.value {
SaveEventValue::Settings(value) => {
util::sl::save(
cfg_dirs.0.data_local_dir().to_str().unwrap(),
cfg::FILE_CFG_SETTINGS,
&value,
&mut console,
);
}
SaveEventValue::User(value) => {
util::sl::save(
cfg_dirs.0.data_local_dir().to_str().unwrap(),
cfg::FILE_CFG_USER,
&value,
&mut console,
);
}
SaveEventValue::Dev(value) => {
util::sl::save(
cfg_dirs.0.data_local_dir().to_str().unwrap(),
cfg::FILE_CFG_DEV,
&value,
&mut console,
);
}
}
}
}