DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: a4137f9834a85dc03723d8842524505bb499ae90 deck-builder/client/src/plugins/config/mod.rs -rw-r--r-- 3.0 KiB
a4137f98Jonni Liljamo feat(client, server, shared): new login/register 1 year, 9 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
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * 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 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,
                );
            }
        }
    }
}