/*
* This file is part of laurelin/client
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy::prelude::*;
use crate::{cfg, util, AfterMainLoop};
/// 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_systems((handle_load_events, handle_save_events).in_set(AfterMainLoop));
}
}
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>,
) {
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,
);
}
LoadEventValue::User => {
*cfg_user = util::sl::load(
cfg_dirs.0.data_local_dir().to_str().unwrap(),
cfg::FILE_CFG_USER,
);
}
LoadEventValue::Dev => {
*cfg_dev = util::sl::load(
cfg_dirs.0.data_local_dir().to_str().unwrap(),
cfg::FILE_CFG_DEV,
);
}
}
}
}
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>) {
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,
);
}
SaveEventValue::User(value) => {
util::sl::save(
cfg_dirs.0.data_local_dir().to_str().unwrap(),
cfg::FILE_CFG_USER,
&value,
);
}
SaveEventValue::Dev(value) => {
util::sl::save(
cfg_dirs.0.data_local_dir().to_str().unwrap(),
cfg::FILE_CFG_DEV,
&value,
);
}
}
}
}