summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-05-04 14:33:23 +0200
committerfx <[email protected]>2023-05-04 14:33:23 +0200
commit0c7ba29d3e17c47e5fc9cffe78c28a0019d453b7 (patch)
tree1815f82eaa9e84f8f73683a84ed99063e4e0d904 /src/lib.rs
parent2b180daf1e2687436046b853c59e0abe12c50f57 (diff)
parent59d539f756375719f7ff71822c72afa00c3bd3c4 (diff)
downloadmodlist-0c7ba29d3e17c47e5fc9cffe78c28a0019d453b7.tar
modlist-0c7ba29d3e17c47e5fc9cffe78c28a0019d453b7.tar.gz
modlist-0c7ba29d3e17c47e5fc9cffe78c28a0019d453b7.zip
Merge pull request 'config' (#4) from config into master
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/4
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs81
1 files changed, 79 insertions, 2 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 185edd7..f18bdfb 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,16 +6,22 @@ pub mod db;
6pub mod error; 6pub mod error;
7pub mod files; 7pub mod files;
8 8
9use std::fmt::Display; 9use std::{fmt::Display, fs::{File, remove_file, self}, io::{Write, Read}, time::Duration};
10 10
11pub use apis::*; 11pub use apis::*;
12use apis::modrinth::{get_game_versions, GameVersion, GameVersionType};
12pub use commands::*; 13pub use commands::*;
13use error::{ErrorType, MLError, MLE}; 14use error::{ErrorType, MLError, MLE};
15use serde::{Deserialize, Serialize};
14 16
15#[derive(Debug, Clone, PartialEq, Eq)] 17#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
16pub enum Modloader { 18pub 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
21impl Modloader { 27impl 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)]
48pub 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
58pub 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
72pub 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
80impl 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}