summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorfx <[email protected]>2023-05-29 18:02:08 +0200
committerfx <[email protected]>2023-05-29 18:02:08 +0200
commitd3870a2efa74e68c643dfb4aef32edc2536503b0 (patch)
tree116075aaa57c35afca2749719d450c3cb473ab3e /src/lib.rs
parent5a2ea0755b29a8811aeeec1c73679c5783082628 (diff)
parentc7ecf3019a75dc0ab1a0aefeb9b880899fc8a231 (diff)
downloadmodlist-d3870a2efa74e68c643dfb4aef32edc2536503b0.tar
modlist-d3870a2efa74e68c643dfb4aef32edc2536503b0.tar.gz
modlist-d3870a2efa74e68c643dfb4aef32edc2536503b0.zip
Merge pull request 'multithreaded' (#6) from multithreaded into master
Reviewed-on: http://raspberrypi.fritz.box:7920/fx/modlist/pulls/6
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs72
1 files changed, 52 insertions, 20 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 1c40ceb..f77befc 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,14 +6,28 @@ pub mod db;
6pub mod error; 6pub mod error;
7pub mod files; 7pub mod files;
8 8
9use std::{fmt::Display, fs::{File, remove_file, self}, io::{Write, Read}, time::Duration}; 9use std::{
10 fmt::Display,
11 fs::{self, remove_file, File},
12 io::{Read, Write},
13 time::Duration,
14};
10 15
11pub use apis::*;
12use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; 16use apis::modrinth::{get_game_versions, GameVersion, GameVersionType};
17pub use apis::*;
13pub use commands::*; 18pub use commands::*;
14use error::{ErrorType, MLError, MLE}; 19use error::{ErrorType, MLError, MLE};
20use indicatif::{ProgressBar, ProgressStyle};
15use serde::{Deserialize, Serialize}; 21use serde::{Deserialize, Serialize};
16 22
23pub static STYLE_BAR_BYTE: &str =
24 "{spinner:.green}{wide_msg}{bytes}/{total_bytes} [{bar:.green/lime}]";
25pub static STYLE_BAR_POS: &str = " {wide_msg}{pos}/{len} [{bar:.green/lime}]";
26pub static STYLE_SPINNER: &str = "{spinner:.green}{wide_msg}";
27pub static STYLE_OPERATION: &str = " {wide_msg}";
28pub static STYLE_MESSAGE: &str = "{wide_msg}";
29pub static PROGRESS_CHARS: &str = "#>-";
30
17#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] 31#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
18pub enum Modloader { 32pub enum Modloader {
19 #[serde(rename(serialize = "fabric", deserialize = "fabric"))] 33 #[serde(rename(serialize = "fabric", deserialize = "fabric"))]
@@ -30,7 +44,9 @@ impl Modloader {
30 "forge" => Ok(Modloader::Forge), 44 "forge" => Ok(Modloader::Forge),
31 "fabric" => Ok(Modloader::Fabric), 45 "fabric" => Ok(Modloader::Fabric),
32 "quilt" => Ok(Modloader::Quilt), 46 "quilt" => Ok(Modloader::Quilt),
33 _ => Err(MLError::new(ErrorType::ArgumentError, "UNKNOWN_MODLOADER")), 47 _ => {
48 Err(MLError::new(ErrorType::ArgumentError, "UNKNOWN_MODLOADER"))
49 }
34 } 50 }
35 } 51 }
36} 52}
@@ -51,21 +67,29 @@ pub enum VersionLevel {
51 Release, 67 Release,
52 #[serde(rename(serialize = "snapshot", deserialize = "snapshot"))] 68 #[serde(rename(serialize = "snapshot", deserialize = "snapshot"))]
53 Snapshot, 69 Snapshot,
54 Version(String) 70 Version(String),
55} 71}
56 72
57/// Checks if update needed (time) 73/// Checks if update needed (time)
58/// if yes: get versions, update 74/// if yes: get versions, update
59pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> { 75pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> {
76 let p = ProgressBar::new(1);
77 p.set_style(ProgressStyle::with_template(STYLE_MESSAGE).unwrap());
78 p.set_message("Update minecraft versions");
79
60 let creation_time = fs::metadata(path)?.created()?; 80 let creation_time = fs::metadata(path)?.created()?;
61 if !force && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24) { return Ok(()); } 81 if !force
62 print!("Update minecraft versions"); 82 && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24)
63 std::io::stdout().flush()?; 83 {
84 return Ok(());
85 }
86
64 let versions = get_game_versions().await; 87 let versions = get_game_versions().await;
65 remove_file(path)?; 88 remove_file(path)?;
66 let mut file = File::create(path)?; 89 let mut file = File::create(path)?;
67 file.write_all(&serde_json::to_string_pretty(&versions)?.as_bytes())?; 90 file.write_all(serde_json::to_string_pretty(&versions)?.as_bytes())?;
68 println!(" ✓"); 91
92 p.finish_with_message("Updated minecraft versions");
69 Ok(()) 93 Ok(())
70} 94}
71 95
@@ -79,7 +103,6 @@ pub fn load_game_versions(path: &str) -> MLE<Vec<GameVersion>> {
79} 103}
80 104
81impl VersionLevel { 105impl VersionLevel {
82
83 pub fn from(str: &str) -> Self { 106 pub fn from(str: &str) -> Self {
84 match str { 107 match str {
85 "release" => VersionLevel::Release, 108 "release" => VersionLevel::Release,
@@ -88,29 +111,38 @@ impl VersionLevel {
88 } 111 }
89 } 112 }
90 113
91 pub async fn get(self, versions_path: &str, force_update: bool) -> MLE<String> { 114 pub async fn get(
115 self,
116 versions_path: &str,
117 force_update: bool,
118 ) -> MLE<String> {
92 let path = format!("{}/versions.json", versions_path); 119 let path = format!("{}/versions.json", versions_path);
93 check_game_versions(&path, force_update).await?; 120 check_game_versions(&path, force_update).await?;
94 let mut versions = load_game_versions(&path)?.into_iter(); 121 let mut versions = load_game_versions(&path)?.into_iter();
95 122
96 match self { 123 match self {
97 VersionLevel::Release => { 124 VersionLevel::Release => {
98 let release = versions.find(|ver| ver.version_type == GameVersionType::release).unwrap(); 125 let release = versions
99 println!("{:?}", release); 126 .find(|ver| ver.version_type == GameVersionType::release)
127 .unwrap();
100 Ok(release.version) 128 Ok(release.version)
101 }, 129 }
102 VersionLevel::Snapshot => { 130 VersionLevel::Snapshot => {
103 let snapshot = versions.find(|ver| ver.version_type == GameVersionType::snapshot).unwrap(); 131 let snapshot = versions
104 println!("{:?}", snapshot); 132 .find(|ver| ver.version_type == GameVersionType::snapshot)
133 .unwrap();
105 Ok(snapshot.version) 134 Ok(snapshot.version)
106 }, 135 }
107 VersionLevel::Version(v) => { 136 VersionLevel::Version(v) => {
108 if versions.find(|ver| ver.version == v).is_some() { 137 if versions.any(|ver| ver.version == v) {
109 Ok(v) 138 Ok(v)
110 } else { 139 } else {
111 Err(MLError::new(ErrorType::ConfigError, "unknown minecraft version")) 140 Err(MLError::new(
141 ErrorType::ConfigError,
142 "unknown minecraft version",
143 ))
112 } 144 }
113 }, 145 }
114 } 146 }
115 } 147 }
116} 148}