summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 73b5697fbe8b9d7338f677d2c124130957a71ae4 (plain) (blame)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
pub mod apis;
pub mod cache;
pub mod commands;
pub mod config;
pub mod db;
pub mod error;
pub mod files;

use std::{fmt::Display, fs::{File, remove_file, self}, io::{Write, Read}, time::Duration};

pub use apis::*;
use apis::modrinth::{get_game_versions, GameVersion, GameVersionType};
pub use commands::*;
use error::{ErrorType, MLError, MLE};
use serde::{Deserialize, Serialize, de::Visitor};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Modloader {
    Fabric,
    Forge,
}

impl Modloader {
    pub fn from(string: &str) -> MLE<Modloader> {
        match string {
            "forge" => Ok(Modloader::Forge),
            "fabric" => Ok(Modloader::Fabric),
            _ => Err(MLError::new(ErrorType::ArgumentError, "UNKNOWN_MODLOADER")),
        }
    }
}

impl Display for Modloader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Modloader::Fabric => write!(f, "fabric"),
            Modloader::Forge => write!(f, "forge"),
        }
    }
}

impl Serialize for Modloader {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer {
        match self {
            Modloader::Fabric => serializer.serialize_str("fabric"),
            Modloader::Forge => serializer.serialize_str("forge"),
        }
    }
}

impl<'de> Deserialize<'de> for Modloader {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
    D: serde::Deserializer<'de>,
    {
        struct FieldVisitor;

        impl<'de> Visitor<'de> for FieldVisitor {
            type Value = Modloader;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("`fabric`, `forge` or `quilt`")
            }

            fn visit_str<E>(self, v: &str) -> Result<Modloader, E>
                where
                    E: serde::de::Error, {
                match v {
                    "fabric" => Ok(Modloader::Fabric),
                    "forge" => Ok(Modloader::Forge),
                    _ => Err(serde::de::Error::unknown_field(v, &["fabric", "forge", "quilt"]))
                }
            }
        }

        deserializer.deserialize_identifier(FieldVisitor)
    }
}

#[derive(Debug, Clone)]
pub enum VersionLevel {
    Release,
    Snapshot,
    Version(String)
}

/// Checks if update needed (time)
/// if yes: get versions, update
pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> {
    let creation_time = fs::metadata(path)?.created()?;
    if !force && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24) { return Ok(()); }
    print!("Update minecraft versions");
    std::io::stdout().flush()?;
    let versions = get_game_versions().await;
    remove_file(path)?;
    let mut file = File::create(path)?;
    file.write_all(&serde_json::to_string_pretty(&versions)?.as_bytes())?;
    println!(" ✓");
    Ok(())
}

/// Loads game versions
pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> {
    let mut file = File::open(path)?;
    let mut data = String::new();
    file.read_to_string(&mut data)?;
    let versions: Vec<GameVersion> = serde_json::from_str(&data)?;
    Ok(versions)
}
    
impl VersionLevel {

    pub fn from(str: &str) -> Self {
        match str {
            "release" => VersionLevel::Release,
            "snapshot" => VersionLevel::Snapshot,
            _ => VersionLevel::Version(String::from(str)),
        }
    }

    pub fn get(self, versions_path: &str) -> MLE<String> {
        let path = format!("{}/versions.json", versions_path);
        let mut versions = load_game_versions(&path)?.into_iter();

        match self {
            VersionLevel::Release => {
                let release = versions.find(|ver| ver.version_type == GameVersionType::release).unwrap();
                println!("{:?}", release);
                Ok(release.version)
            },
            VersionLevel::Snapshot => {
                let snapshot = versions.find(|ver| ver.version_type == GameVersionType::snapshot).unwrap();
                println!("{:?}", snapshot);
                Ok(snapshot.version)
            },
            VersionLevel::Version(v) => {
                if versions.find(|ver| ver.version == v).is_some() {
                    Ok(v)
                } else {
                    Err(MLError::new(ErrorType::ConfigError, "unknown minecraft version"))
                }
            },
        }
    }
}

impl Serialize for VersionLevel {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer {
        match self {
            VersionLevel::Release => serializer.serialize_str("release"),
            VersionLevel::Snapshot => serializer.serialize_str("snapshot"),
            VersionLevel::Version(v) => serializer.serialize_str(v),
        }
    }
}

impl<'de> Deserialize<'de> for VersionLevel {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
    D: serde::Deserializer<'de>,
    {
        struct FieldVisitor;

        impl<'de> Visitor<'de> for FieldVisitor {
            type Value = VersionLevel;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("`fabric`, `forge` or `quilt`")
            }

            fn visit_str<E>(self, v: &str) -> Result<VersionLevel, E>
                where
                    E: serde::de::Error, {
                match v {
                    "release" => Ok(VersionLevel::Release),
                    "snapshot" => Ok(VersionLevel::Snapshot),
                    _ => Ok(VersionLevel::Version(String::from(v)))
                }
            }
        }

        deserializer.deserialize_identifier(FieldVisitor)
    }
}