use crate::modded::{Processor, SidedDataEntry};
use crate::{download_file, Error};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub const CURRENT_FORMAT_VERSION: usize = 0;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum VersionType {
Release,
Snapshot,
OldAlpha,
OldBeta,
}
impl VersionType {
pub fn as_str(&self) -> &'static str {
match self {
VersionType::Release => "release",
VersionType::Snapshot => "snapshot",
VersionType::OldAlpha => "old_alpha",
VersionType::OldBeta => "old_beta",
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Version {
pub id: String,
#[serde(rename = "type")]
pub type_: VersionType,
pub url: String,
pub time: DateTime<Utc>,
pub release_time: DateTime<Utc>,
pub sha1: String,
pub compliance_level: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub assets_index_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub assets_index_sha1: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LatestVersion {
pub release: String,
pub snapshot: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VersionManifest {
pub latest: LatestVersion,
pub versions: Vec<Version>,
}
pub const VERSION_MANIFEST_URL: &str =
"https://launchermeta.mojang.com/mc/game/version_manifest_v2.json";
pub async fn fetch_version_manifest(url: Option<&str>) -> Result<VersionManifest, Error> {
Ok(serde_json::from_slice(
&download_file(url.unwrap_or(VERSION_MANIFEST_URL), None).await?,
)?)
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct AssetIndex {
pub id: String,
pub sha1: String,
pub size: u32,
pub total_size: u32,
pub url: String,
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash)]
#[serde(rename_all = "snake_case")]
pub enum DownloadType {
Client,
ClientMappings,
Server,
ServerMappings,
WindowsServer,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Download {
pub sha1: String,
pub size: u32,
pub url: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LibraryDownload {
pub path: String,
pub sha1: String,
pub size: u32,
pub url: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LibraryDownloads {
#[serde(skip_serializing_if = "Option::is_none")]
pub artifact: Option<LibraryDownload>,
#[serde(skip_serializing_if = "Option::is_none")]
pub classifiers: Option<HashMap<String, LibraryDownload>>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
pub enum RuleAction {
Allow,
Disallow,
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Clone)]
#[serde(rename_all = "snake_case")]
pub enum Os {
Osx,
Windows,
Linux,
Unknown,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OsRule {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<Os>,
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub arch: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FeatureRule {
#[serde(skip_serializing_if = "Option::is_none")]
pub is_demo_user: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_demo_resolution: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Rule {
pub action: RuleAction,
#[serde(skip_serializing_if = "Option::is_none")]
pub os: Option<OsRule>,
#[serde(skip_serializing_if = "Option::is_none")]
pub features: Option<FeatureRule>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct LibraryExtract {
#[serde(skip_serializing_if = "Option::is_none")]
pub exclude: Option<Vec<String>>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Library {
#[serde(skip_serializing_if = "Option::is_none")]
pub downloads: Option<LibraryDownloads>,
#[serde(skip_serializing_if = "Option::is_none")]
pub extract: Option<LibraryExtract>,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub natives: Option<HashMap<Os, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rules: Option<Vec<Rule>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub checksums: Option<Vec<String>>,
#[serde(default = "default_include_in_classpath")]
pub include_in_classpath: bool,
}
fn default_include_in_classpath() -> bool {
true
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum ArgumentValue {
Single(String),
Many(Vec<String>),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum Argument {
Normal(String),
Ruled {
rules: Vec<Rule>,
value: ArgumentValue,
},
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum ArgumentType {
Game,
Jvm,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct VersionInfo {
#[serde(skip_serializing_if = "Option::is_none")]
pub arguments: Option<HashMap<ArgumentType, Vec<Argument>>>,
pub asset_index: AssetIndex,
pub assets: String,
pub downloads: HashMap<DownloadType, Download>,
pub id: String,
pub libraries: Vec<Library>,
pub main_class: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub minecraft_arguments: Option<String>,
pub minimum_launcher_version: u32,
pub release_time: DateTime<Utc>,
pub time: DateTime<Utc>,
#[serde(rename = "type")]
pub type_: VersionType,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<HashMap<String, SidedDataEntry>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub processors: Option<Vec<Processor>>,
}
pub async fn fetch_version_info(version: &Version) -> Result<VersionInfo, Error> {
Ok(serde_json::from_slice(
&download_file(&version.url, Some(&version.sha1)).await?,
)?)
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Asset {
pub hash: String,
pub size: u32,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AssetsIndex {
pub objects: HashMap<String, Asset>,
}
pub async fn fetch_assets_index(version: &VersionInfo) -> Result<AssetsIndex, Error> {
Ok(serde_json::from_slice(
&download_file(&version.asset_index.url, Some(&version.asset_index.sha1)).await?,
)?)
}