DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: c52f5b1a9de349e429a60de4a126d33cfc25d14b deck-builder/client/src/plugins/config/mod.rs -rw-r--r-- 2.7 KiB
c52f5b1aJonni Liljamo Merge remote branch 'origin/dev' 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * 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};

/// 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>,
) {
    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,
                );
            }
        }
    }
}