Skip to content

Quilt (& intermediary) support #2

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions helixlauncher-meta/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ impl Display for GradleSpecifier {
}
}

impl GradleSpecifier {
pub fn to_url(&self, base_repo: &str) -> String {
format!(
"{}{}/{}/{}/{}-{}{}.{}",
base_repo,
self.group.replace(".", "/"),
self.artifact,
self.version,
self.artifact,
self.version,
self.classifier
.as_ref()
.map_or("".to_string(), |it| "-".to_string() + &it),
self.extension
)
}
}

cfg_if::cfg_if! {
if #[cfg(windows)] {
pub const CURRENT_OS: component::OsName = component::OsName::Windows;
Expand Down
151 changes: 151 additions & 0 deletions src/intermediary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use std::{collections::BTreeSet, fs, path::Path, str::FromStr};

use anyhow::Result;
use chrono::{DateTime, Utc};
use futures::{stream, StreamExt, TryStreamExt};
use helixlauncher_meta::{
component::{Component, ComponentDependency, ConditionalClasspathEntry, Download},
index::Index,
util::GradleSpecifier,
};
use reqwest::Client;
use serde::{Deserialize, Serialize};

use crate::{get_hash, get_size};

const CONCURRENT_FETCH_LIMIT: usize = 5;
pub async fn fetch(client: &Client) -> Result<()> {
let upstream_base = Path::new("upstream/intermediary");

fs::create_dir_all(&upstream_base).unwrap();

stream::iter(get_versions(client).await?)
.map(|version| async { fetch_version(version, client, &upstream_base).await })
.buffer_unordered(CONCURRENT_FETCH_LIMIT)
.try_collect::<()>()
.await?;
Ok(())
}

async fn fetch_version(version: String, client: &Client, upstream_base: &Path) -> Result<()> {
let version_path = upstream_base.join(format!("{}.json", version));
if version_path.try_exists()? {
return Ok(());
}

let library = crate::Library {
name: GradleSpecifier::from_str(&format!("net.fabricmc:intermediary:{version}")).unwrap(),
url: "https://maven.fabricmc.net/".into(),
};
let download = Download {
name: library.name.clone(),
url: library.name.to_url(&library.url),
hash: get_hash(client, &library).await?,
size: get_size(client, &library).await?.try_into().unwrap(),
};

let release_time = DateTime::parse_from_rfc2822(
// TODO: This does one more request than necessary, should get_size or get_hash be merged into this?
client
.head(library.name.to_url(&library.url))
.header("User-Agent", "helixlauncher-meta")
.send()
.await?
.headers()
.get("last-modified")
.expect("Cannot handle servers returning no last-modified")
.to_str()?,
)
.expect(&format!(
"Error parsing last-modified header of {}",
library.name.to_url(&library.url)
))
.into();

let download = DownloadWithReleaseTime {
download,
release_time,
};

fs::write(version_path, serde_json::to_string_pretty(&download)?)?;

Ok(())
}

pub fn process() -> Result<()> {
let out_base = Path::new("out/net.fabricmc.intermediary");
let upstream_base = Path::new("upstream/intermediary");
fs::create_dir_all(out_base)?;

let mut index: Index = vec![];

for version_meta in fs::read_dir(upstream_base)? {
let version_meta: DownloadWithReleaseTime =
serde_json::from_str(&fs::read_to_string(version_meta?.path())?)?;

let classpath = vec![ConditionalClasspathEntry::All(
version_meta.download.name.clone(),
)];

let component = Component {
format_version: 1,
assets: None,
conflicts: vec![],
id: "net.fabricmc.intermediary".into(),
jarmods: vec![],
natives: vec![],
release_time: version_meta.release_time,
version: version_meta.download.name.version.clone(),
traits: BTreeSet::new(),
requires: vec![ComponentDependency {
id: "net.minecraft".into(),
version: Some(version_meta.download.name.version.clone()),
}],
game_jar: None,
main_class: None,
game_arguments: vec![],
classpath,
downloads: vec![version_meta.download],
};

fs::write(
out_base.join(format!("{}.json", component.version)),
serde_json::to_string_pretty(&component)?,
)?;

index.push(component.into());
}

index.sort_by(|x, y| y.release_time.cmp(&x.release_time));

fs::write(
out_base.join("index.json"),
serde_json::to_string_pretty(&index)?,
)?;

Ok(())
}

async fn get_versions(client: &Client) -> Result<Vec<String>> {
let response: Vec<IntermediaryVersionData> = client
.get("https://meta.fabricmc.net/v2/versions/intermediary")
.header("User-Agent", "helixlauncher-meta")
.send()
.await?
.json()
.await?;
Ok(response.into_iter().map(|v| v.version).collect())
}

#[derive(Deserialize)]
struct IntermediaryVersionData {
// maven: GradleSpecifier,
version: String,
// stable: bool,
}

#[derive(Serialize, Deserialize)]
struct DownloadWithReleaseTime {
download: Download,
release_time: DateTime<Utc>,
}
49 changes: 48 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,64 @@
#![deny(rust_2018_idioms)]

use anyhow::Result;
use futures::try_join;
use helixlauncher_meta::{component::Hash, util::GradleSpecifier};
use reqwest::Client;
use serde::{Deserialize, Serialize};

mod forge;
mod intermediary;
mod mojang;
mod quilt;

#[tokio::main]
async fn main() -> Result<()> {
let client = reqwest::Client::new();

mojang::fetch(&client).await?;
try_join!(
mojang::fetch(&client),
quilt::fetch(&client),
intermediary::fetch(&client),
)?;

mojang::process()?;

// forge::process()?;

quilt::process()?;

intermediary::process()?;

Ok(())
}

pub(crate) async fn get_hash(client: &Client, coord: &Library) -> Result<Hash> {
Ok(Hash::SHA256(
client
.get(coord.name.to_url(&coord.url) + ".sha256")
.header("User-Agent", "helixlauncher-meta")
.send()
.await?
.text()
.await?,
))
}

pub(crate) async fn get_size(client: &Client, coord: &Library) -> Result<u64> {
Ok(client
.head(coord.name.to_url(&coord.url))
.header("User-Agent", "helixlauncher-meta")
.send()
.await?
.headers()
.get("content-length")
.expect("Cannot handle servers returning no content length")
.to_str()?
.parse()?)
}

#[derive(Serialize, Deserialize, Debug)]
struct Library {
name: GradleSpecifier,
url: String,
}
Loading