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
use bevy::{
    prelude::*,
    tasks::{futures_lite::future, IoTaskPool, Task},
};
use de_core::fs::conf_dir;
use de_core::state::AppState;
use de_gui::ToastEvent;
use iyes_progress::prelude::*;
use tracing::error;

use crate::macros::ConfigLoadError;
use crate::Configuration;

pub(super) struct ConfPlugin;

impl Plugin for ConfPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(OnEnter(AppState::AppLoading), start_loading)
            .add_systems(OnExit(AppState::AppLoading), cleanup)
            .add_systems(
                Update,
                poll_conf
                    .track_progress()
                    .run_if(in_state(AppState::AppLoading)),
            );
    }
}

#[derive(Resource)]
struct LoadingTask(Task<Result<Configuration, ConfigLoadError>>);

fn cleanup(mut commands: Commands) {
    commands.remove_resource::<LoadingTask>();
}

fn start_loading(mut commands: Commands) {
    let task = IoTaskPool::get().spawn(async {
        let path = conf_dir().map_err(ConfigLoadError::from)?.join("conf.yaml");
        Configuration::load(path.as_path()).await
    });
    commands.insert_resource(LoadingTask(task));
}

fn poll_conf(
    mut commands: Commands,
    task: Option<ResMut<LoadingTask>>,
    conf: Option<Res<Configuration>>,
    mut toasts: EventWriter<ToastEvent>,
) -> Progress {
    if conf.is_some() {
        return true.into();
    }

    match task {
        Some(mut task) => match future::block_on(future::poll_once(&mut task.0)) {
            Some(result) => match result {
                Ok(configuration) => {
                    commands.insert_resource(configuration);
                    true.into()
                }
                Err(err) => {
                    error!("{err}");
                    toasts.send(ToastEvent::new("Configuration loading failed."));
                    commands.init_resource::<Configuration>();
                    true.into()
                }
            },
            None => false.into(),
        },
        None => false.into(),
    }
}