diff options
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 81 |
1 files changed, 79 insertions, 2 deletions
@@ -6,16 +6,22 @@ pub mod db; | |||
6 | pub mod error; | 6 | pub mod error; |
7 | pub mod files; | 7 | pub mod files; |
8 | 8 | ||
9 | use std::fmt::Display; | 9 | use std::{fmt::Display, fs::{File, remove_file, self}, io::{Write, Read}, time::Duration}; |
10 | 10 | ||
11 | pub use apis::*; | 11 | pub use apis::*; |
12 | use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; | ||
12 | pub use commands::*; | 13 | pub use commands::*; |
13 | use error::{ErrorType, MLError, MLE}; | 14 | use error::{ErrorType, MLError, MLE}; |
15 | use serde::{Deserialize, Serialize}; | ||
14 | 16 | ||
15 | #[derive(Debug, Clone, PartialEq, Eq)] | 17 | #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] |
16 | pub enum Modloader { | 18 | pub enum Modloader { |
19 | #[serde(rename(serialize = "fabric", deserialize = "fabric"))] | ||
17 | Fabric, | 20 | Fabric, |
21 | #[serde(rename(serialize = "forge", deserialize = "forge"))] | ||
18 | Forge, | 22 | Forge, |
23 | #[serde(rename(serialize = "quilt", deserialize = "quilt"))] | ||
24 | Quilt, | ||
19 | } | 25 | } |
20 | 26 | ||
21 | impl Modloader { | 27 | impl Modloader { |
@@ -33,6 +39,77 @@ impl Display for Modloader { | |||
33 | match self { | 39 | match self { |
34 | Modloader::Fabric => write!(f, "fabric"), | 40 | Modloader::Fabric => write!(f, "fabric"), |
35 | Modloader::Forge => write!(f, "forge"), | 41 | Modloader::Forge => write!(f, "forge"), |
42 | Modloader::Quilt => write!(f, "quilt"), | ||
43 | } | ||
44 | } | ||
45 | } | ||
46 | |||
47 | #[derive(Debug, Clone, Deserialize, Serialize)] | ||
48 | pub enum VersionLevel { | ||
49 | #[serde(rename(serialize = "release", deserialize = "release"))] | ||
50 | Release, | ||
51 | #[serde(rename(serialize = "snapshot", deserialize = "snapshot"))] | ||
52 | Snapshot, | ||
53 | Version(String) | ||
54 | } | ||
55 | |||
56 | /// Checks if update needed (time) | ||
57 | /// if yes: get versions, update | ||
58 | pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { | ||
59 | let creation_time = fs::metadata(path)?.created()?; | ||
60 | if !force && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24) { return Ok(()); } | ||
61 | print!("Update minecraft versions"); | ||
62 | std::io::stdout().flush()?; | ||
63 | let versions = get_game_versions().await; | ||
64 | remove_file(path)?; | ||
65 | let mut file = File::create(path)?; | ||
66 | file.write_all(&serde_json::to_string_pretty(&versions)?.as_bytes())?; | ||
67 | println!(" ✓"); | ||
68 | Ok(()) | ||
69 | } | ||
70 | |||
71 | /// Loads game versions from file | ||
72 | pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> { | ||
73 | let mut file = File::open(path)?; | ||
74 | let mut data = String::new(); | ||
75 | file.read_to_string(&mut data)?; | ||
76 | let versions: Vec<GameVersion> = serde_json::from_str(&data)?; | ||
77 | Ok(versions) | ||
78 | } | ||
79 | |||
80 | impl VersionLevel { | ||
81 | |||
82 | pub fn from(str: &str) -> Self { | ||
83 | match str { | ||
84 | "release" => VersionLevel::Release, | ||
85 | "snapshot" => VersionLevel::Snapshot, | ||
86 | _ => VersionLevel::Version(String::from(str)), | ||
87 | } | ||
88 | } | ||
89 | |||
90 | pub async fn get(self, versions_path: &str, force_update: bool) -> MLE<String> { | ||
91 | let path = format!("{}/versions.json", versions_path); | ||
92 | check_game_versions(&path, force_update).await?; | ||
93 | let mut versions = load_game_versions(&path)?.into_iter(); | ||
94 | |||
95 | match self { | ||
96 | VersionLevel::Release => { | ||
97 | let release = versions.find(|ver| ver.version_type == GameVersionType::release).unwrap(); | ||
98 | println!("{:?}", release); | ||
99 | Ok(release.version) | ||
100 | }, | ||
101 | VersionLevel::Snapshot => { | ||
102 | let snapshot = versions.find(|ver| ver.version_type == GameVersionType::snapshot).unwrap(); | ||
103 | println!("{:?}", snapshot); | ||
104 | Ok(snapshot.version) | ||
105 | }, | ||
106 | VersionLevel::Version(v) => { | ||
107 | if versions.find(|ver| ver.version == v).is_some() { | ||
108 | Ok(v) | ||
109 | } else { | ||
110 | Err(MLError::new(ErrorType::ConfigError, "unknown minecraft version")) | ||
111 | } | ||
112 | }, | ||
36 | } | 113 | } |
37 | } | 114 | } |
38 | } | 115 | } |