summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: be63ff8dd5619df30db4ae55d9e4dab48d341450 (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
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::{self, remove_file, File},
    io::{Read, Write},
    time::Duration,
};

use apis::modrinth::{get_game_versions, GameVersion, GameVersionType};
pub use apis::*;
pub use commands::*;
use error::{EType, MLErr, MLE};
use indicatif::{ProgressBar, ProgressStyle};
use serde::{Deserialize, Serialize};

pub static STYLE_BAR_BYTE: &str =
    "{spinner:.green}{wide_msg}{bytes}/{total_bytes} [{bar:.green/lime}]";
pub static STYLE_BAR_POS: &str = " {wide_msg}{pos}/{len} [{bar:.green/lime}]";
pub static STYLE_SPINNER: &str = "{spinner:.green}{wide_msg}";
pub static STYLE_OPERATION: &str = " {wide_msg}";
pub static STYLE_MESSAGE: &str = "{wide_msg}";
pub static PROGRESS_CHARS: &str = "#>-";

#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub enum Modloader {
    #[serde(rename(serialize = "fabric", deserialize = "fabric"))]
    Fabric,
    #[serde(rename(serialize = "forge", deserialize = "forge"))]
    Forge,
    #[serde(rename(serialize = "quilt", deserialize = "quilt"))]
    Quilt,
}

impl Modloader {
    /// # Errors
    pub fn from(string: &str) -> MLE<Modloader> {
        match string {
            "forge" => Ok(Modloader::Forge),
            "fabric" => Ok(Modloader::Fabric),
            "quilt" => Ok(Modloader::Quilt),
            _ => {
                Err(MLErr::new(EType::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"),
            Modloader::Quilt => write!(f, "quilt"),
        }
    }
}

#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum VersionLevel {
    #[serde(rename(serialize = "release", deserialize = "release"))]
    Release,
    #[serde(rename(serialize = "snapshot", deserialize = "snapshot"))]
    Snapshot,
    Version(String),
}

/// Checks if update needed (time)
/// if yes: get versions, update
/// # Errors
pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> {
    let p = ProgressBar::new(1);
    p.set_style(ProgressStyle::with_template(STYLE_MESSAGE).map_err(|_| {
        MLErr::new(EType::LibIndicatif, "template error")
    })?);
    p.set_message("Update minecraft versions");

    let creation_time = fs::metadata(path)?.created()?;
    if !force
        && creation_time.elapsed().map_err(|_| MLErr::new(EType::LibIndicatif, "SystemTimeError"))? < Duration::from_secs(60 * 60 * 24)
    {
        return Ok(());
    }

    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())?;

    p.finish_with_message("Updated minecraft versions");
    Ok(())
}

/// Loads game versions from file
/// # Errors
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 {
    #[must_use]
    pub fn from(str: &str) -> Self {
        match str {
            "release" => VersionLevel::Release,
            "snapshot" => VersionLevel::Snapshot,
            _ => VersionLevel::Version(String::from(str)),
        }
    }

    /// .
    ///
    /// Panics if .
    /// # Errors
    pub async fn get(
        self,
        versions_path: &str,
        force_update: bool,
    ) -> MLE<String> {
        let path = format!("{versions_path}/versions.json");
        check_game_versions(&path, force_update).await?;
        let mut versions = load_game_versions(&path)?.into_iter();

        match self {
            VersionLevel::Release => {
                if let Some(release) = versions
                    .find(|ver| ver.version_type == GameVersionType::release)
                {
                    Ok(release.version)
                } else {
                    Err(MLErr::new(
                        EType::Other,
                        "no minecraft release version found",
                    ))
                }
            }
            VersionLevel::Snapshot => {
                if let Some(snapshot) = versions
                    .find(|ver| ver.version_type == GameVersionType::snapshot)
                {
                    Ok(snapshot.version)
                } else {
                    Err(MLErr::new(
                        EType::Other,
                        "no minecraft snapshot version found",
                    ))
                }
            }
            VersionLevel::Version(v) => {
                if versions.any(|ver| ver.version == v) {
                    Ok(v)
                } else {
                    Err(MLErr::new(
                        EType::ConfigError,
                        "unknown minecraft version",
                    ))
                }
            }
        }
    }
}