From 11e64fc7560de3cd0def718edf68c31e3dc8be72 Mon Sep 17 00:00:00 2001 From: fxqnlr Date: Wed, 4 Sep 2024 15:08:55 +0200 Subject: move stuff around, remove lib.rs for overview --- src/data/gameversion.rs | 118 +++++++++++++++++++++++++++++++++++++++++++++++ src/data/list.rs | 19 ++++++++ src/data/modification.rs | 11 +++++ src/data/modloader.rs | 39 ++++++++++++++++ src/data/project.rs | 12 +++++ 5 files changed, 199 insertions(+) create mode 100644 src/data/gameversion.rs create mode 100644 src/data/list.rs create mode 100644 src/data/modification.rs create mode 100644 src/data/modloader.rs create mode 100644 src/data/project.rs (limited to 'src/data') diff --git a/src/data/gameversion.rs b/src/data/gameversion.rs new file mode 100644 index 0000000..3868502 --- /dev/null +++ b/src/data/gameversion.rs @@ -0,0 +1,118 @@ +use std::{ + fs::{self, remove_file, File}, + io::{Read, Write}, + time::Duration, +}; + +use apis::modrinth::{get_game_versions, GameVersion, GameVersionType}; +use error::{EType, MLErr, MLE}; +use indicatif::{ProgressBar, ProgressStyle}; +use serde::{Deserialize, Serialize}; + +use crate::{apis, error, STYLE_MESSAGE}; + +#[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> { + let mut file = File::open(path)?; + let mut data = String::new(); + file.read_to_string(&mut data)?; + let versions: Vec = 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)), + } + } + + /// . + /// + /// Panics if . + /// # Errors + pub async fn get( + self, + versions_path: &str, + force_update: bool, + ) -> MLE { + 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", + )) + } + } + } + } +} diff --git a/src/data/list.rs b/src/data/list.rs new file mode 100644 index 0000000..0045b7a --- /dev/null +++ b/src/data/list.rs @@ -0,0 +1,19 @@ +use crate::{config::Cfg, db::{config_get_current_list, lists_get}, error::MLE}; + +use super::modloader::Modloader; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct List { + pub id: String, + pub mc_version: String, + pub modloader: Modloader, + pub download_folder: String, +} + +impl List { + /// # Errors + pub fn get_current_list(config: &Cfg) -> MLE { + let id = config_get_current_list(config)?; + lists_get(config, &id) + } +} diff --git a/src/data/modification.rs b/src/data/modification.rs new file mode 100644 index 0000000..84047ff --- /dev/null +++ b/src/data/modification.rs @@ -0,0 +1,11 @@ +#[derive(Debug)] +pub struct AddMod { + pub id: IDSelector, + pub set_version: bool, +} + +#[derive(Debug, PartialEq, Eq)] +pub enum IDSelector { + ModificationID(String), + VersionID(String), +} diff --git a/src/data/modloader.rs b/src/data/modloader.rs new file mode 100644 index 0000000..050213f --- /dev/null +++ b/src/data/modloader.rs @@ -0,0 +1,39 @@ +use std::fmt::Display; + +use serde::{Deserialize, Serialize}; + +use crate::error::{EType, MLErr, MLE}; + +#[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 { + 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"), + } + } +} diff --git a/src/data/project.rs b/src/data/project.rs new file mode 100644 index 0000000..9807867 --- /dev/null +++ b/src/data/project.rs @@ -0,0 +1,12 @@ +use crate::apis::modrinth::Version; + +#[derive(Debug, Clone)] +pub struct ProjectInfo { + pub mod_id: String, + pub slug: String, + pub title: String, + pub current_version: Option, + pub applicable_versions: Vec, + pub download_link: String, + pub set_version: bool, +} -- cgit v1.2.3