summaryrefslogblamecommitdiff
path: root/src/lib.rs
blob: f59ba8994aa25cdcc1286f8caf947d26879b6a4b (plain) (tree)
1
2
3
4
5
6
7
8
9
             
              
                 
               
           
              
              
 
                                                                                          
 
                
                                                                      
                    
                                     
                                    
 

                                    
                                                              
                    
                                                                  
           
                                                                
          

                                                                
 

                
                                                 


                                              
                                            
                                                                                  
         

     
 




                                                                        
                                                   


         
 
                                               
                       
                                                                    
            
                                                                      













                                                                                                        
                                                                         



                     
                                 






                                                                  
 









                                                          
                                                                                    
                                                              
                                                        













                                                                                                           
                                                         







                                                                                          
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::{File, remove_file, self}, io::{Write, Read}, time::Duration};

pub use apis::*;
use apis::modrinth::{get_game_versions, GameVersion, GameVersionType};
pub use commands::*;
use error::{ErrorType, MLError, MLE};
use serde::{Deserialize, Serialize};

pub static TICK_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 {
    pub fn from(string: &str) -> MLE<Modloader> {
        match string {
            "forge" => Ok(Modloader::Forge),
            "fabric" => Ok(Modloader::Fabric),
            "quilt" => Ok(Modloader::Quilt),
            _ => Err(MLError::new(ErrorType::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
pub async fn check_game_versions(path: &str, force: bool) -> MLE<()> {
    let creation_time = fs::metadata(path)?.created()?;
    if !force && creation_time.elapsed().unwrap() < Duration::from_secs(60 * 60 * 24) { return Ok(()); }
    print!("Update minecraft versions");
    std::io::stdout().flush()?;
    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())?;
    println!(" ✓");
    Ok(())
}

/// Loads game versions from file
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 {

    pub fn from(str: &str) -> Self {
        match str {
            "release" => VersionLevel::Release,
            "snapshot" => VersionLevel::Snapshot,
            _ => VersionLevel::Version(String::from(str)),
        }
    }

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

        match self {
            VersionLevel::Release => {
                let release = versions.find(|ver| ver.version_type == GameVersionType::release).unwrap();
                println!("{:?}", release);
                Ok(release.version)
            },
            VersionLevel::Snapshot => {
                let snapshot = versions.find(|ver| ver.version_type == GameVersionType::snapshot).unwrap();
                println!("{:?}", snapshot);
                Ok(snapshot.version)
            },
            VersionLevel::Version(v) => {
                if versions.any(|ver| ver.version == v) {
                    Ok(v)
                } else {
                    Err(MLError::new(ErrorType::ConfigError, "unknown minecraft version"))
                }
            },
        }
    }
}