wormhole_common/boot/
integrity.rs

1use serde::{Deserialize, Serialize};
2use std::fs;
3use std::path::PathBuf;
4
5#[derive(Debug, Deserialize, Serialize)]
6pub struct Mods {
7    mods: Vec<Mod>,
8}
9
10#[derive(Debug, Deserialize, Serialize)]
11struct Mod {
12    name: String,
13    date_installed: String,
14    size: u64,
15    install_path: String,
16}
17
18pub fn check_directories() {
19    let mut dir_path = PathBuf::from(std::env::var("APPDATA").unwrap());
20    dir_path.push("Wormhole");
21
22    if !dir_path.exists() {
23        let _ = fs::create_dir_all(&dir_path);
24    }
25
26    if !&dir_path.join("instances").exists() {
27        let _ = fs::create_dir_all(dir_path.join("instances"));
28    }
29
30    if !&dir_path.join("mods").exists() {
31        let _ = fs::create_dir_all(dir_path.join("mods"));
32    }
33
34    if !&dir_path.join("cache").exists() {
35        let _ = fs::create_dir_all(dir_path.join("cache"));
36    }
37}
38
39pub fn check_files() {
40    let mut dir_path = PathBuf::from(std::env::var("APPDATA").unwrap());
41    dir_path.push("Wormhole");
42
43    if !&dir_path.join("instances").join("instances.json").exists() {
44        let _ = fs::write(dir_path.join("instances").join("instances.json"), "[]");
45    }
46
47    const MODS_TEMPLATE: &str = r#"
48    {
49      "mods": [
50        {
51          "name": "Mod 1",
52          "date_installed": "2022-03-10T10:30:00Z",
53          "size": 1000000,
54          "install_path": "C:\\Users\\username\\AppData\\Roaming\\Wormhole\\mods\\mod1"
55        },
56        {
57          "name": "Mod 2",
58          "date_installed": "2022-03-09T15:20:00Z",
59          "size": 500000,
60          "install_path": "C:\\Users\\username\\AppData\\Roaming\\Wormhole\\mods\\mod2"
61        },
62        {
63          "name": "Mod 3",
64          "date_installed": "2022-03-08T11:45:00Z",
65          "size": 2000000,
66          "install_path": "C:\\Users\\username\\AppData\\Roaming\\Wormhole\\mods\\mod3"
67        }
68      ]
69    }
70    "#;
71
72    if !&dir_path.join("mods").join("mods.json").exists() {
73        let _ = fs::write(dir_path.join("mods").join("mods.json"), MODS_TEMPLATE);
74    }
75}
76
77pub fn directory_integrity_check() {
78    check_directories();
79    check_files();
80}
81
82pub fn read_mods_file() -> Mods {
83    let mut dir_path = PathBuf::from(std::env::var("APPDATA").unwrap());
84    dir_path.push("Wormhole");
85    dir_path.push("mods");
86    dir_path.push("mods.json");
87    let mods_file_contents = fs::read_to_string(dir_path);
88    let mods: Mods = serde_json::from_str(&mods_file_contents.unwrap()).unwrap();
89    return mods;
90}
91
92// write to mods file
93pub fn write_mods_file(mods: Mods) {
94    let mut dir_path = PathBuf::from(std::env::var("APPDATA").unwrap());
95    dir_path.push("Wormhole");
96    dir_path.push("mods");
97    dir_path.push("mods.json");
98    let _mods_file_contents = fs::write(dir_path, serde_json::to_string(&mods).unwrap());
99}