DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

ref: 343934c69a20354b4f27ed5b68cfa05976c1580a deck-builder/client/src/util/sl.rs -rw-r--r-- 2.7 KiB
343934c6Jonni Liljamo chore(client): remove unused init_networking 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
/*
 * 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.
 */

// TODO: We should encode the configs as base64

use bevy::prelude::*;
use bevy_console::PrintConsoleLine;

pub fn load<T: Default + for<'a> serde::Deserialize<'a>>(
    target_dir: &str,
    file_name: &str,
    console: &mut EventWriter<PrintConsoleLine>,
) -> T {
    let file_path: String = format!("{}/{}", target_dir, file_name);

    match std::path::Path::new(&file_path).exists() {
        true => {
            console.send(PrintConsoleLine::new(
                format!("sl::load found '{}'", file_path).into(),
            ));
            let json: Vec<u8> = std::fs::read(file_path.clone()).unwrap();
            serde_json::from_str(std::str::from_utf8(&json).unwrap()).expect(&format!(
                "sl::load couldn't deserialize the config at '{}'",
                file_path
            ))
        }
        false => {
            console.send(PrintConsoleLine::new(
                format!("sl::load couldn't find '{}', using defaults", file_path).into(),
            ));
            T::default()
        }
    }
}

pub fn save<T>(
    target_dir: &str,
    file_name: &str,
    value: &T,
    console: &mut EventWriter<PrintConsoleLine>,
) -> bool
where
    T: ?Sized + serde::Serialize,
{
    let file_path: String = format!("{}/{}", target_dir, file_name);
    let json: String = serde_json::to_string(&value).unwrap();

    match std::path::Path::new(target_dir).exists() {
        true => {}
        false => {
            console.send(PrintConsoleLine::new(
                format!("sl::save creating target dir '{}'", target_dir).into(),
            ));
            match std::fs::create_dir_all(target_dir) {
                Ok(_) => {}
                Err(_) => {
                    console.send(PrintConsoleLine::new(
                        format!("sl::save couldn't create target dir '{}'", target_dir).into(),
                    ));
                    console.send(PrintConsoleLine::new(
                        format!("sl::save nothing will be saved!").into(),
                    ));
                    return false;
                }
            }
        }
    }

    match std::fs::write(&file_path, json) {
        Ok(_) => {
            console.send(PrintConsoleLine::new(
                format!("sl::save wrote file '{}'", file_path).into(),
            ));
            true
        }
        Err(_) => {
            console.send(PrintConsoleLine::new(
                format!("sl::save couldn't write file '{}'", file_path).into(),
            ));
            false
        }
    }
}