Merge remote-tracking branch 'daedalus/monorepo-migration' into daedalus-add

This commit is contained in:
Jai A 2024-10-19 14:59:12 -07:00
commit 595d5362f6
No known key found for this signature in database
GPG Key ID: 9A9F9B7250E9883C
29 changed files with 5813 additions and 0 deletions

44
.github/workflows/daedalus-docker.yml vendored Normal file
View File

@ -0,0 +1,44 @@
name: daedalus-docker-build
on:
push:
branches: [ "main" ]
paths:
- .github/workflows/daedalus-docker.yml
- 'apps/daedalus/**'
pull_request:
types: [ opened, synchronize ]
paths:
- .github/workflows/daedalus-docker.yml
- 'apps/daedalus/**'
merge_group:
types: [ checks_requested ]
jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Fetch docker metadata
id: docker_meta
uses: docker/metadata-action@v3
with:
images: ghcr.io/modrinth/daedalus
-
name: Login to GitHub Images
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Build and push
id: docker_build
uses: docker/build-push-action@v2
with:
context: .
file: ./Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}

52
.github/workflows/daedalus-run.yml vendored Normal file
View File

@ -0,0 +1,52 @@
name: Run Meta
on:
schedule:
- cron: '*/5 * * * *'
workflow_dispatch:
jobs:
run-docker:
if: github.repository_owner == 'modrinth'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Pull Docker image from GHCR
run: docker pull ghcr.io/modrinth/daedalus:main
- name: Run Docker container
env:
BASE_URL: ${{ secrets.BASE_URL }}
S3_ACCESS_TOKEN: ${{ secrets.S3_ACCESS_TOKEN }}
S3_SECRET: ${{ secrets.S3_SECRET }}
S3_URL: ${{ secrets.S3_URL }}
S3_REGION: ${{ secrets.S3_REGION }}
S3_BUCKET_NAME: ${{ secrets.S3_BUCKET_NAME }}
CLOUDFLARE_INTEGRATION: ${{ secrets.CLOUDFLARE_INTEGRATION }}
CLOUDFLARE_TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }}
CLOUDFLARE_ZONE_ID: ${{ secrets.CLOUDFLARE_ZONE_ID }}
run: |
docker run \
--name daedalus \
-e RUST_LOG=warn,daedalus_client=trace \
-e BASE_URL=$BASE_URL \
-e S3_ACCESS_TOKEN=$S3_ACCESS_TOKEN \
-e S3_SECRET=$S3_SECRET \
-e S3_URL=$S3_URL \
-e S3_REGION=$S3_REGION \
-e S3_BUCKET_NAME=$S3_BUCKET_NAME \
-e CLOUDFLARE_INTEGRATION=$CLOUDFLARE_INTEGRATION \
-e CLOUDFLARE_TOKEN=$CLOUDFLARE_TOKEN \
-e CLOUDFLARE_ZONE_ID=$CLOUDFLARE_ZONE_ID \
ghcr.io/modrinth/daedalus:master

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

15
.idea/daedalus.iml generated Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/daedalus_client_new/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/apps/daedalus_client/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/packages/daedalus/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

7
.idea/discord.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="PROJECT_FILES" />
<option name="description" value="" />
</component>
</project>

26
.idea/libraries/KotlinJavaRuntime.xml generated Normal file
View File

@ -0,0 +1,26 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime" type="repository">
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.0/kotlin-stdlib-jdk8-1.8.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.8.0/kotlin-stdlib-1.8.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.0/kotlin-stdlib-common-1.8.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.8.0/kotlin-stdlib-jdk7-1.8.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.0/kotlin-stdlib-jdk8-1.8.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.8.0/kotlin-stdlib-1.8.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.0/kotlin-stdlib-common-1.8.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.8.0/kotlin-stdlib-jdk7-1.8.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.8.0/kotlin-stdlib-jdk8-1.8.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.8.0/kotlin-stdlib-1.8.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-common/1.8.0/kotlin-stdlib-common-1.8.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.8.0/kotlin-stdlib-jdk7-1.8.0-sources.jar!/" />
</SOURCES>
</library>
</component>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/daedalus.iml" filepath="$PROJECT_DIR$/.idea/daedalus.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

15
apps/daedalus_client/.env Normal file
View File

@ -0,0 +1,15 @@
RUST_LOG=warn,daedalus_client=trace
BASE_URL=http://localhost:9000/meta
CONCURRENCY_LIMIT=10
S3_ACCESS_TOKEN=none
S3_SECRET=none
S3_URL=http://localhost:9000
S3_REGION=path-style
S3_BUCKET_NAME=meta
CLOUDFLARE_INTEGRATION=false
CLOUDFLARE_TOKEN=none
CLOUDFLARE_ZONE_ID=none

View File

@ -0,0 +1,33 @@
[package]
name = "daedalus_client"
version = "0.2.2"
authors = ["Jai A <jai@modrinth.com>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
daedalus = { path = "../daedalus" }
tokio = { version = "1", features = ["full"] }
futures = "0.3.25"
dotenvy = "0.15.6"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde-xml-rs = "0.6.0"
lazy_static = "1.4.0"
thiserror = "1.0"
reqwest = { version = "0.12.5", features = ["stream", "json", "rustls-tls"] }
async_zip = { version = "0.0.17", features = ["full"] }
semver = "1.0"
chrono = { version = "0.4", features = ["serde"] }
bytes = "1.6.0"
rust-s3 = "0.34.0"
dashmap = "5.5.3"
sha1_smol = { version = "1.0.0", features = ["std"] }
indexmap = { version = "2.2.6", features = ["serde"]}
itertools = "0.13.0"
tracing-error = "0.2.0"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-futures = { version = "0.2.5", features = ["futures", "tokio"] }

View File

@ -0,0 +1,21 @@
FROM rust:1.79.0 as build
ENV PKG_CONFIG_ALLOW_CROSS=1
WORKDIR /usr/src/daedalus
COPY . .
RUN cargo build --release
FROM debian:bookworm-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends ca-certificates openssl \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
RUN update-ca-certificates
COPY --from=build /usr/src/daedalus/target/release/daedalus_client /daedalus/daedalus_client
WORKDIR /daedalus_client
CMD /daedalus/daedalus_client

View File

@ -0,0 +1,7 @@
Copyright © 2024 Rinth, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,12 @@
# Daedalus
Daedalus is a powerful tool which queries and generates metadata for the Minecraft (and other games in the future!) game
and mod loaders for:
- Performance (Serving static files can be easily cached and is extremely quick)
- Ease for Launcher Devs (Metadata is served in an easy to query and use format)
- Reliability (Provides a versioning system which ensures no breakage with updates)
Daedalus is currently a work in progress, but will support the original Minecraft data and reposting for all Forge and
Fabric artifacts.
Once Daedalus is done, Modrinth will provide full documentation for how to query from it and use it for your own launcher!

View File

@ -0,0 +1,17 @@
version: '3'
services:
minio:
image: quay.io/minio/minio
volumes:
- minio-data:/data
ports:
- "9000:9000"
- "9001:9001"
environment:
MINIO_ROOT_USER: minioadmin
MINIO_ROOT_PASSWORD: miniosecret
command: server /data --console-address ":9001"
volumes:
minio-data:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,10 @@
{
"name": "@modrinth/daedalus_client",
"scripts": {
"build": "cargo build --release",
"lint": "cargo fmt --check && cargo clippy --all-targets --all-features -- -D warnings",
"fix": "cargo fmt && cargo clippy --fix",
"dev": "cargo run",
"test": "cargo test"
}
}

View File

@ -0,0 +1,63 @@
use tracing_error::InstrumentError;
#[derive(thiserror::Error, Debug)]
pub enum ErrorKind {
#[error("Daedalus Error: {0}")]
Daedalus(#[from] daedalus::Error),
#[error("Invalid input: {0}")]
InvalidInput(String),
#[error("Error while managing asynchronous tasks")]
TaskError(#[from] tokio::task::JoinError),
#[error("Error while deserializing JSON: {0}")]
SerdeJSON(#[from] serde_json::Error),
#[error("Error while deserializing XML: {0}")]
SerdeXML(#[from] serde_xml_rs::Error),
#[error("Failed to validate file checksum at url {url} with hash {hash} after {tries} tries")]
ChecksumFailure {
hash: String,
url: String,
tries: u32,
},
#[error("Unable to fetch {item}")]
Fetch { inner: reqwest::Error, item: String },
#[error("Error while uploading file to S3: {file}")]
S3 {
inner: s3::error::S3Error,
file: String,
},
#[error("Error acquiring semaphore: {0}")]
Acquire(#[from] tokio::sync::AcquireError),
#[error("Tracing error: {0}")]
Tracing(#[from] tracing::subscriber::SetGlobalDefaultError),
#[error("Zip error: {0}")]
Zip(#[from] async_zip::error::ZipError),
}
#[derive(Debug)]
pub struct Error {
pub source: tracing_error::TracedError<ErrorKind>,
}
impl std::fmt::Display for Error {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(fmt, "{}", self.source)
}
}
impl<E: Into<ErrorKind>> From<E> for Error {
fn from(source: E) -> Self {
let error = Into::<ErrorKind>::into(source);
Self {
source: error.in_current_span(),
}
}
}
impl ErrorKind {
pub fn as_error(self) -> Error {
self.into()
}
}
pub type Result<T> = core::result::Result<T, Error>;

View File

@ -0,0 +1,301 @@
use crate::util::{download_file, fetch_json, format_url};
use crate::{insert_mirrored_artifact, Error, MirrorArtifact, UploadFile};
use daedalus::modded::{Manifest, PartialVersionInfo, DUMMY_REPLACE_STRING};
use dashmap::DashMap;
use serde::Deserialize;
use std::sync::Arc;
use tokio::sync::Semaphore;
#[tracing::instrument(skip(semaphore, upload_files, mirror_artifacts))]
pub async fn fetch_fabric(
semaphore: Arc<Semaphore>,
upload_files: &DashMap<String, UploadFile>,
mirror_artifacts: &DashMap<String, MirrorArtifact>,
) -> Result<(), Error> {
fetch(
daedalus::modded::CURRENT_FABRIC_FORMAT_VERSION,
"fabric",
"https://meta.fabricmc.net/v2",
"https://maven.fabricmc.net/",
&[],
semaphore,
upload_files,
mirror_artifacts,
)
.await
}
#[tracing::instrument(skip(semaphore, upload_files, mirror_artifacts))]
pub async fn fetch_quilt(
semaphore: Arc<Semaphore>,
upload_files: &DashMap<String, UploadFile>,
mirror_artifacts: &DashMap<String, MirrorArtifact>,
) -> Result<(), Error> {
fetch(
daedalus::modded::CURRENT_QUILT_FORMAT_VERSION,
"quilt",
"https://meta.quiltmc.org/v3",
"https://maven.quiltmc.org/repository/release/",
&[
// This version is broken as it contains invalid library coordinates
"0.17.5-beta.4",
],
semaphore,
upload_files,
mirror_artifacts,
)
.await
}
#[allow(clippy::too_many_arguments)]
#[tracing::instrument(skip(semaphore, upload_files, mirror_artifacts))]
async fn fetch(
format_version: usize,
mod_loader: &str,
meta_url: &str,
maven_url: &str,
skip_versions: &[&str],
semaphore: Arc<Semaphore>,
upload_files: &DashMap<String, UploadFile>,
mirror_artifacts: &DashMap<String, MirrorArtifact>,
) -> Result<(), Error> {
let modrinth_manifest = fetch_json::<Manifest>(
&format_url(&format!("{mod_loader}/v{format_version}/manifest.json",)),
&semaphore,
)
.await
.ok();
let fabric_manifest = fetch_json::<FabricVersions>(
&format!("{meta_url}/versions"),
&semaphore,
)
.await?;
// We check Modrinth's fabric version manifest and compare if the fabric version exists in Modrinth's database
// We also check intermediary versions that are newly added to query
let (fetch_fabric_versions, fetch_intermediary_versions) =
if let Some(modrinth_manifest) = modrinth_manifest {
let (mut fetch_versions, mut fetch_intermediary_versions) =
(Vec::new(), Vec::new());
for version in &fabric_manifest.loader {
if !modrinth_manifest
.game_versions
.iter()
.any(|x| x.loaders.iter().any(|x| x.id == version.version))
&& !skip_versions.contains(&&*version.version)
{
fetch_versions.push(version);
}
}
for version in &fabric_manifest.intermediary {
if !modrinth_manifest
.game_versions
.iter()
.any(|x| x.id == version.version)
&& fabric_manifest
.game
.iter()
.any(|x| x.version == version.version)
{
fetch_intermediary_versions.push(version);
}
}
(fetch_versions, fetch_intermediary_versions)
} else {
(
fabric_manifest
.loader
.iter()
.filter(|x| !skip_versions.contains(&&*x.version))
.collect(),
fabric_manifest.intermediary.iter().collect(),
)
};
const DUMMY_GAME_VERSION: &str = "1.21";
if !fetch_intermediary_versions.is_empty() {
for x in &fetch_intermediary_versions {
insert_mirrored_artifact(
&x.maven,
None,
vec![maven_url.to_string()],
false,
mirror_artifacts,
)?;
}
}
if !fetch_fabric_versions.is_empty() {
let fabric_version_manifest_urls = fetch_fabric_versions
.iter()
.map(|x| {
format!(
"{}/versions/loader/{}/{}/profile/json",
meta_url, DUMMY_GAME_VERSION, x.version
)
})
.collect::<Vec<_>>();
let fabric_version_manifests = futures::future::try_join_all(
fabric_version_manifest_urls
.iter()
.map(|x| download_file(x, None, &semaphore)),
)
.await?
.into_iter()
.map(|x| serde_json::from_slice(&x))
.collect::<Result<Vec<PartialVersionInfo>, serde_json::Error>>()?;
let patched_version_manifests = fabric_version_manifests
.into_iter()
.map(|mut version_info| {
for lib in &mut version_info.libraries {
let new_name = lib
.name
.replace(DUMMY_GAME_VERSION, DUMMY_REPLACE_STRING);
// Hard-code: This library is not present on fabric's maven, so we fetch it from MC libraries
if &*lib.name == "net.minecraft:launchwrapper:1.12" {
lib.url = Some(
"https://libraries.minecraft.net/".to_string(),
);
}
// If a library is not intermediary, we add it to mirror artifacts to be mirrored
if lib.name == new_name {
insert_mirrored_artifact(
&new_name,
None,
vec![lib
.url
.clone()
.unwrap_or_else(|| maven_url.to_string())],
false,
mirror_artifacts,
)?;
} else {
lib.name = new_name;
}
lib.url = Some(format_url("maven/"));
}
version_info.id = version_info
.id
.replace(DUMMY_GAME_VERSION, DUMMY_REPLACE_STRING);
version_info.inherits_from = version_info
.inherits_from
.replace(DUMMY_GAME_VERSION, DUMMY_REPLACE_STRING);
Ok(version_info)
})
.collect::<Result<Vec<_>, Error>>()?;
let serialized_version_manifests = patched_version_manifests
.iter()
.map(|x| serde_json::to_vec(x).map(bytes::Bytes::from))
.collect::<Result<Vec<_>, serde_json::Error>>()?;
serialized_version_manifests
.into_iter()
.enumerate()
.for_each(|(index, bytes)| {
let loader = fetch_fabric_versions[index];
let version_path = format!(
"{mod_loader}/v{format_version}/versions/{}.json",
loader.version
);
upload_files.insert(
version_path,
UploadFile {
file: bytes,
content_type: Some("application/json".to_string()),
},
);
});
}
if !fetch_fabric_versions.is_empty()
|| !fetch_intermediary_versions.is_empty()
{
let fabric_manifest_path =
format!("{mod_loader}/v{format_version}/manifest.json",);
let loader_versions = daedalus::modded::Version {
id: DUMMY_REPLACE_STRING.to_string(),
stable: true,
loaders: fabric_manifest
.loader
.into_iter()
.map(|x| {
let version_path = format!(
"{mod_loader}/v{format_version}/versions/{}.json",
x.version,
);
daedalus::modded::LoaderVersion {
id: x.version,
url: format_url(&version_path),
stable: x.stable,
}
})
.collect(),
};
let manifest = daedalus::modded::Manifest {
game_versions: std::iter::once(loader_versions)
.chain(fabric_manifest.game.into_iter().map(|x| {
daedalus::modded::Version {
id: x.version,
stable: x.stable,
loaders: vec![],
}
}))
.collect(),
};
upload_files.insert(
fabric_manifest_path,
UploadFile {
file: bytes::Bytes::from(serde_json::to_vec(&manifest)?),
content_type: Some("application/json".to_string()),
},
);
}
Ok(())
}
#[derive(Deserialize, Debug, Clone)]
struct FabricVersions {
pub loader: Vec<FabricLoaderVersion>,
pub game: Vec<FabricGameVersion>,
#[serde(alias = "hashed")]
pub intermediary: Vec<FabricIntermediaryVersion>,
}
#[derive(Deserialize, Debug, Clone)]
struct FabricLoaderVersion {
// pub separator: String,
// pub build: u32,
// pub maven: String,
pub version: String,
#[serde(default)]
pub stable: bool,
}
#[derive(Deserialize, Debug, Clone)]
struct FabricIntermediaryVersion {
pub maven: String,
pub version: String,
}
#[derive(Deserialize, Debug, Clone)]
struct FabricGameVersion {
pub version: String,
pub stable: bool,
}

View File

@ -0,0 +1,793 @@
use crate::util::{
download_file, fetch_json, fetch_xml, format_url,
};
use crate::{insert_mirrored_artifact, Error, MirrorArtifact, UploadFile};
use chrono::{DateTime, Utc};
use daedalus::get_path_from_artifact;
use daedalus::modded::PartialVersionInfo;
use dashmap::DashMap;
use futures::io::Cursor;
use indexmap::IndexMap;
use itertools::Itertools;
use serde::de::DeserializeOwned;
use serde::Deserialize;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Semaphore;
#[tracing::instrument(skip(semaphore, upload_files, mirror_artifacts))]
pub async fn fetch_forge(
semaphore: Arc<Semaphore>,
upload_files: &DashMap<String, UploadFile>,
mirror_artifacts: &DashMap<String, MirrorArtifact>,
) -> Result<(), Error> {
let forge_manifest = fetch_json::<IndexMap<String, Vec<String>>>(
"https://files.minecraftforge.net/net/minecraftforge/forge/maven-metadata.json",
&semaphore,
)
.await?;
let mut format_version = 0;
let forge_versions = forge_manifest.into_iter().flat_map(|(game_version, versions)| versions.into_iter().map(|loader_version| {
// Forge versions can be in these specific formats:
// 1.10.2-12.18.1.2016-failtests
// 1.9-12.16.0.1886
// 1.9-12.16.0.1880-1.9
// 1.14.4-28.1.30
// This parses them to get the actual Forge version. Ex: 1.15.2-31.1.87 -> 31.1.87
let version_split = loader_version.split('-').nth(1).unwrap_or(&loader_version).to_string();
// Forge has 3 installer formats:
// - Format 0 (Unsupported ATM): Forge Legacy (pre-1.5.2). Uses Binary Patch method to install
// To install: Download patch, download minecraft client JAR. Combine patch and client JAR and delete META-INF/.
// (pre-1.3-2) Client URL: https://maven.minecraftforge.net/net/minecraftforge/forge/{version}/forge-{version}-client.zip
// (pre-1.3-2) Server URL: https://maven.minecraftforge.net/net/minecraftforge/forge/{version}/forge-{version}-server.zip
// (1.3-2-onwards) Universal URL: https://maven.minecraftforge.net/net/minecraftforge/forge/{version}/forge-{version}-universal.zip
// - Format 1: Forge Installer Legacy (1.5.2-1.12.2ish)
// To install: Extract install_profile.json from archive. "versionInfo" is the profile's version info. Convert it to the modern format
// Extract forge library from archive. Path is at "install"."path".
// - Format 2: Forge Installer Modern
// To install: Extract install_profile.json from archive. Extract version.json from archive. Combine the two and extract all libraries
// which are embedded into the installer JAR.
// Then upload. The launcher will need to run processors!
if format_version != 1 && &*version_split == "7.8.0.684" {
format_version = 1;
} else if format_version != 2 && &*version_split == "14.23.5.2851" {
format_version = 2;
}
ForgeVersion {
format_version,
installer_url: format!("https://maven.minecraftforge.net/net/minecraftforge/forge/{0}/forge-{0}-installer.jar", loader_version),
raw: loader_version,
loader_version: version_split,
game_version: game_version.clone(),
}
})
.collect::<Vec<_>>())
// TODO: support format version 0 (see above)
.filter(|x| x.format_version != 0)
.filter(|x| {
// These following Forge versions are broken and cannot be installed
const BLACKLIST : &[&str] = &[
// Not supported due to `data` field being `[]` even though the type is a map
"1.12.2-14.23.5.2851",
// Malformed Archives
"1.6.1-8.9.0.749",
"1.6.1-8.9.0.751",
"1.6.4-9.11.1.960",
"1.6.4-9.11.1.961",
"1.6.4-9.11.1.963",
"1.6.4-9.11.1.964",
];
!BLACKLIST.contains(&&*x.raw)
})
.collect::<Vec<_>>();
fetch(
daedalus::modded::CURRENT_FORGE_FORMAT_VERSION,
"forge",
"https://maven.minecraftforge.net/",
forge_versions,
semaphore,
upload_files,
mirror_artifacts,
)
.await
}
#[tracing::instrument(skip(semaphore, upload_files, mirror_artifacts))]
pub async fn fetch_neo(
semaphore: Arc<Semaphore>,
upload_files: &DashMap<String, UploadFile>,
mirror_artifacts: &DashMap<String, MirrorArtifact>,
) -> Result<(), Error> {
#[derive(Debug, Deserialize)]
struct Metadata {
versioning: Versioning,
}
#[derive(Debug, Deserialize)]
struct Versioning {
versions: Versions,
}
#[derive(Debug, Deserialize)]
struct Versions {
version: Vec<String>,
}
let forge_versions = fetch_xml::<Metadata>(
"https://maven.neoforged.net/net/neoforged/forge/maven-metadata.xml",
&semaphore,
)
.await?;
let neo_versions = fetch_xml::<Metadata>(
"https://maven.neoforged.net/net/neoforged/neoforge/maven-metadata.xml",
&semaphore,
)
.await?;
let parsed_versions = forge_versions.versioning.versions.version.into_iter().map(|loader_version| {
// NeoForge Forge versions can be in these specific formats:
// 1.20.1-47.1.74
// 47.1.82
// This parses them to get the actual Forge version. Ex: 1.20.1-47.1.74 -> 47.1.74
let version_split = loader_version.split('-').nth(1).unwrap_or(&loader_version).to_string();
Ok(ForgeVersion {
format_version: 2,
installer_url: format!("https://maven.neoforged.net/net/neoforged/forge/{0}/forge-{0}-installer.jar", loader_version),
raw: loader_version,
loader_version: version_split,
game_version: "1.20.1".to_string(), // All NeoForge Forge versions are for 1.20.1
})
}).chain(neo_versions.versioning.versions.version.into_iter().map(|loader_version| {
let mut parts = loader_version.split('.');
// NeoForge Forge versions are in this format: 20.2.29-beta, 20.6.119
// Where the first number is the major MC version, the second is the minor MC version, and the third is the NeoForge version
let major = parts.next().ok_or_else(
|| crate::ErrorKind::InvalidInput(format!("Unable to find major game version for NeoForge {loader_version}"))
)?;
let minor = parts.next().ok_or_else(
|| crate::ErrorKind::InvalidInput(format!("Unable to find minor game version for NeoForge {loader_version}"))
)?;
let game_version = if minor == "0" {
format!("1.{major}")
} else {
format!("1.{major}.{minor}")
};
Ok(ForgeVersion {
format_version: 2,
installer_url: format!("https://maven.neoforged.net/net/neoforged/neoforge/{0}/neoforge-{0}-installer.jar", loader_version),
loader_version: loader_version.clone(),
raw: loader_version,
game_version,
})
}))
.collect::<Result<Vec<_>, Error>>()?
.into_iter()
.filter(|x| {
// These following Forge versions are broken and cannot be installed
const BLACKLIST : &[&str] = &[
// Unreachable / 404
"1.20.1-47.1.7",
"47.1.82",
];
!BLACKLIST.contains(&&*x.raw)
}).collect();
fetch(
daedalus::modded::CURRENT_NEOFORGE_FORMAT_VERSION,
"neo",
"https://maven.neoforged.net/",
parsed_versions,
semaphore,
upload_files,
mirror_artifacts,
)
.await
}
#[tracing::instrument(skip(
forge_versions,
semaphore,
upload_files,
mirror_artifacts
))]
async fn fetch(
format_version: usize,
mod_loader: &str,
maven_url: &str,
forge_versions: Vec<ForgeVersion>,
semaphore: Arc<Semaphore>,
upload_files: &DashMap<String, UploadFile>,
mirror_artifacts: &DashMap<String, MirrorArtifact>,
) -> Result<(), Error> {
let modrinth_manifest = fetch_json::<daedalus::modded::Manifest>(
&format_url(&format!("{mod_loader}/v{format_version}/manifest.json",)),
&semaphore,
)
.await
.ok();
let fetch_versions = if let Some(modrinth_manifest) = modrinth_manifest {
let mut fetch_versions = Vec::new();
for version in &forge_versions {
if !modrinth_manifest.game_versions.iter().any(|x| {
x.id == version.game_version
&& x.loaders.iter().any(|x| x.id == version.loader_version)
}) {
fetch_versions.push(version);
}
}
fetch_versions
} else {
forge_versions.iter().collect()
};
if !fetch_versions.is_empty() {
let forge_installers = futures::future::try_join_all(
fetch_versions
.iter()
.map(|x| download_file(&x.installer_url, None, &semaphore)),
)
.await?;
#[tracing::instrument(skip(raw, upload_files, mirror_artifacts))]
async fn read_forge_installer(
raw: bytes::Bytes,
loader: &ForgeVersion,
maven_url: &str,
mod_loader: &str,
upload_files: &DashMap<String, UploadFile>,
mirror_artifacts: &DashMap<String, MirrorArtifact>,
) -> Result<PartialVersionInfo, Error> {
tracing::trace!(
"Reading forge installer for {}",
loader.loader_version
);
type ZipFileReader = async_zip::base::read::seek::ZipFileReader<
Cursor<bytes::Bytes>,
>;
let cursor = Cursor::new(raw);
let mut zip = ZipFileReader::new(cursor).await?;
#[tracing::instrument(skip(zip))]
async fn read_file(
zip: &mut ZipFileReader,
file_name: &str,
) -> Result<Option<Vec<u8>>, Error> {
let zip_index_option =
zip.file().entries().iter().position(|f| {
f.filename().as_str().unwrap_or_default() == file_name
});
if let Some(zip_index) = zip_index_option {
let mut buffer = Vec::new();
let mut reader = zip.reader_with_entry(zip_index).await?;
reader.read_to_end_checked(&mut buffer).await?;
Ok(Some(buffer))
} else {
Ok(None)
}
}
#[tracing::instrument(skip(zip))]
async fn read_json<T: DeserializeOwned>(
zip: &mut ZipFileReader,
file_name: &str,
) -> Result<Option<T>, Error> {
if let Some(file) = read_file(zip, file_name).await? {
Ok(Some(serde_json::from_slice(&file)?))
} else {
Ok(None)
}
}
if loader.format_version == 1 {
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ForgeInstallerProfileInstallDataV1 {
// pub mirror_list: String,
// pub target: String,
/// Path to the Forge universal library
pub file_path: String,
// pub logo: String,
// pub welcome: String,
// pub version: String,
/// Maven coordinates of the Forge universal library
pub path: String,
// pub profile_name: String,
pub minecraft: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ForgeInstallerProfileManifestV1 {
pub id: String,
pub libraries: Vec<daedalus::minecraft::Library>,
pub main_class: Option<String>,
pub minecraft_arguments: Option<String>,
pub release_time: DateTime<Utc>,
pub time: DateTime<Utc>,
pub type_: daedalus::minecraft::VersionType,
// pub assets: Option<String>,
// pub inherits_from: Option<String>,
// pub jar: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ForgeInstallerProfileV1 {
pub install: ForgeInstallerProfileInstallDataV1,
pub version_info: ForgeInstallerProfileManifestV1,
}
let install_profile = read_json::<ForgeInstallerProfileV1>(
&mut zip,
"install_profile.json",
)
.await?
.ok_or_else(|| {
crate::ErrorKind::InvalidInput(format!(
"No install_profile.json present for loader {}",
loader.installer_url
))
})?;
let forge_library =
read_file(&mut zip, &install_profile.install.file_path)
.await?
.ok_or_else(|| {
crate::ErrorKind::InvalidInput(format!(
"No forge library present for loader {}",
loader.installer_url
))
})?;
upload_files.insert(
format!(
"maven/{}",
get_path_from_artifact(&install_profile.install.path)?
),
UploadFile {
file: bytes::Bytes::from(forge_library),
content_type: None,
},
);
Ok(PartialVersionInfo {
id: install_profile.version_info.id,
inherits_from: install_profile.install.minecraft,
release_time: install_profile.version_info.release_time,
time: install_profile.version_info.time,
main_class: install_profile.version_info.main_class,
minecraft_arguments: install_profile
.version_info
.minecraft_arguments
.clone(),
arguments: install_profile
.version_info
.minecraft_arguments
.map(|x| {
[(
daedalus::minecraft::ArgumentType::Game,
x.split(' ')
.map(|x| {
daedalus::minecraft::Argument::Normal(
x.to_string(),
)
})
.collect(),
)]
.iter()
.cloned()
.collect()
}),
libraries: install_profile
.version_info
.libraries
.into_iter()
.map(|mut lib| {
// For all libraries besides the forge lib extracted, we mirror them from maven servers
// unless the URL is empty/null or available on Minecraft's servers
if let Some(ref url) = lib.url {
if lib.name == install_profile.install.path {
lib.url = Some(format_url("maven/"));
} else if !url.is_empty()
&& !url.contains(
"https://libraries.minecraft.net/",
)
{
insert_mirrored_artifact(
&lib.name,
None,
vec![
url.clone(),
"https://maven.creeperhost.net/"
.to_string(),
maven_url.to_string(),
],
false,
mirror_artifacts,
)?;
lib.url = Some(format_url("maven/"));
}
}
Ok(lib)
})
.collect::<Result<Vec<_>, Error>>()?,
type_: install_profile.version_info.type_,
data: None,
processors: None,
})
} else if loader.format_version == 2 {
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct ForgeInstallerProfileV2 {
// pub spec: i32,
// pub profile: String,
// pub version: String,
// pub json: String,
// pub path: Option<String>,
// pub minecraft: String,
pub data: HashMap<String, daedalus::modded::SidedDataEntry>,
pub libraries: Vec<daedalus::minecraft::Library>,
pub processors: Vec<daedalus::modded::Processor>,
}
let install_profile = read_json::<ForgeInstallerProfileV2>(
&mut zip,
"install_profile.json",
)
.await?
.ok_or_else(|| {
crate::ErrorKind::InvalidInput(format!(
"No install_profile.json present for loader {}",
loader.installer_url
))
})?;
let mut version_info =
read_json::<PartialVersionInfo>(&mut zip, "version.json")
.await?
.ok_or_else(|| {
crate::ErrorKind::InvalidInput(format!(
"No version.json present for loader {}",
loader.installer_url
))
})?;
version_info.processors = Some(install_profile.processors);
version_info.libraries.extend(
install_profile.libraries.into_iter().map(|mut x| {
x.include_in_classpath = false;
x
}),
);
async fn mirror_forge_library(
mut zip: ZipFileReader,
mut lib: daedalus::minecraft::Library,
maven_url: &str,
upload_files: &DashMap<String, UploadFile>,
mirror_artifacts: &DashMap<String, MirrorArtifact>,
) -> Result<daedalus::minecraft::Library, Error>
{
let artifact_path = get_path_from_artifact(&lib.name)?;
if let Some(ref mut artifact) =
lib.downloads.as_mut().and_then(|x| x.artifact.as_mut())
{
if !artifact.url.is_empty() {
insert_mirrored_artifact(
&lib.name,
Some(artifact.sha1.clone()),
vec![artifact.url.clone()],
true,
mirror_artifacts,
)?;
artifact.url =
format_url(&format!("maven/{}", artifact_path));
return Ok(lib);
}
} else if let Some(url) = &lib.url {
if !url.is_empty() {
insert_mirrored_artifact(
&lib.name,
None,
vec![
url.clone(),
"https://libraries.minecraft.net/"
.to_string(),
"https://maven.creeperhost.net/"
.to_string(),
maven_url.to_string(),
],
false,
mirror_artifacts,
)?;
lib.url = Some(format_url("maven/"));
return Ok(lib);
}
}
// Other libraries are generally available in the "maven" directory of the installer. If they are
// not present here, they will be generated by Forge processors.
let extract_path = format!("maven/{artifact_path}");
if let Some(file) =
read_file(&mut zip, &extract_path).await?
{
upload_files.insert(
extract_path,
UploadFile {
file: bytes::Bytes::from(file),
content_type: None,
},
);
lib.url = Some(format_url("maven/"));
} else {
lib.downloadable = false;
}
Ok(lib)
}
version_info.libraries = futures::future::try_join_all(
version_info.libraries.into_iter().map(|lib| {
mirror_forge_library(
zip.clone(),
lib,
maven_url,
upload_files,
mirror_artifacts,
)
}),
)
.await?;
// In Minecraft Forge modern installers, processors are run during the install process. Some processors
// are extracted from the installer JAR. This function finds these files, extracts them, and uploads them
// and registers them as libraries instead.
// Ex:
// "BINPATCH": {
// "client": "/data/client.lzma",
// "server": "/data/server.lzma"
// },
// Becomes:
// "BINPATCH": {
// "client": "[net.minecraftforge:forge:1.20.3-49.0.1:shim:client@lzma]",
// "server": "[net.minecraftforge:forge:1.20.3-49.0.1:shim:server@lzma]"
// },
// And the resulting library is added to the profile's libraries
let mut new_data = HashMap::new();
for (key, entry) in install_profile.data {
async fn extract_data(
zip: &mut ZipFileReader,
key: &str,
value: &str,
upload_files: &DashMap<String, UploadFile>,
libs: &mut Vec<daedalus::minecraft::Library>,
mod_loader: &str,
version: &ForgeVersion,
) -> Result<String, Error> {
let extract_file =
read_file(zip, &value[1..value.len()])
.await?
.ok_or_else(|| {
crate::ErrorKind::InvalidInput(format!(
"Unable reading data key {key} at path {value}",
))
})?;
let file_name = value.split('/').last()
.ok_or_else(|| {
crate::ErrorKind::InvalidInput(format!(
"Unable reading filename for data key {key} at path {value}",
))
})?;
let mut file = file_name.split('.');
let file_name = file.next()
.ok_or_else(|| {
crate::ErrorKind::InvalidInput(format!(
"Unable reading filename only for data key {key} at path {value}",
))
})?;
let ext = file.next()
.ok_or_else(|| {
crate::ErrorKind::InvalidInput(format!(
"Unable reading extension only for data key {key} at path {value}",
))
})?;
let path = format!(
"com.modrinth.daedalus:{}-installer-extracts:{}:{}@{}",
mod_loader,
version.raw,
file_name,
ext
);
upload_files.insert(
format!("maven/{}", get_path_from_artifact(&path)?),
UploadFile {
file: bytes::Bytes::from(extract_file),
content_type: None,
},
);
libs.push(daedalus::minecraft::Library {
downloads: None,
extract: None,
name: path.clone(),
url: Some(format_url("maven/")),
natives: None,
rules: None,
checksums: None,
include_in_classpath: false,
downloadable: true,
});
Ok(format!("[{path}]"))
}
let client = if entry.client.starts_with('/') {
extract_data(
&mut zip,
&key,
&entry.client,
upload_files,
&mut version_info.libraries,
mod_loader,
loader,
)
.await?
} else {
entry.client.clone()
};
let server = if entry.server.starts_with('/') {
extract_data(
&mut zip,
&key,
&entry.server,
upload_files,
&mut version_info.libraries,
mod_loader,
loader,
)
.await?
} else {
entry.server.clone()
};
new_data.insert(
key.clone(),
daedalus::modded::SidedDataEntry { client, server },
);
}
version_info.data = Some(new_data);
Ok(version_info)
} else {
Err(crate::ErrorKind::InvalidInput(format!(
"Unknown format version {} for loader {}",
loader.format_version, loader.installer_url
))
.into())
}
}
let forge_version_infos = futures::future::try_join_all(
forge_installers
.into_iter()
.enumerate()
.map(|(index, raw)| {
let loader = fetch_versions[index];
read_forge_installer(
raw,
loader,
maven_url,
mod_loader,
upload_files,
mirror_artifacts,
)
}),
)
.await?;
let serialized_version_manifests = forge_version_infos
.iter()
.map(|x| serde_json::to_vec(x).map(bytes::Bytes::from))
.collect::<Result<Vec<_>, serde_json::Error>>()?;
serialized_version_manifests
.into_iter()
.enumerate()
.for_each(|(index, bytes)| {
let loader = fetch_versions[index];
let version_path = format!(
"{mod_loader}/v{format_version}/versions/{}.json",
loader.loader_version
);
upload_files.insert(
version_path,
UploadFile {
file: bytes,
content_type: Some("application/json".to_string()),
},
);
});
let forge_manifest_path =
format!("{mod_loader}/v{format_version}/manifest.json",);
let manifest = daedalus::modded::Manifest {
game_versions: forge_versions
.into_iter()
.rev()
.chunk_by(|x| x.game_version.clone())
.into_iter()
.map(|(game_version, loaders)| daedalus::modded::Version {
id: game_version,
stable: true,
loaders: loaders
.map(|x| daedalus::modded::LoaderVersion {
url: format_url(&format!(
"{mod_loader}/v{format_version}/versions/{}.json",
x.loader_version
)),
id: x.loader_version,
stable: false,
})
.collect(),
})
.collect(),
};
upload_files.insert(
forge_manifest_path,
UploadFile {
file: bytes::Bytes::from(serde_json::to_vec(&manifest)?),
content_type: Some("application/json".to_string()),
},
);
}
Ok(())
}
#[derive(Debug)]
struct ForgeVersion {
pub format_version: usize,
pub raw: String,
pub loader_version: String,
pub game_version: String,
pub installer_url: String,
}

View File

@ -0,0 +1,218 @@
use crate::util::{
format_url, upload_file_to_bucket, upload_url_to_bucket_mirrors,
REQWEST_CLIENT,
};
use daedalus::get_path_from_artifact;
use dashmap::{DashMap, DashSet};
use std::sync::Arc;
use tokio::sync::Semaphore;
use tracing_error::ErrorLayer;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
mod error;
mod fabric;
mod forge;
mod minecraft;
pub mod util;
pub use error::{Error, ErrorKind, Result};
#[tokio::main]
async fn main() -> Result<()> {
dotenvy::dotenv().ok();
let subscriber = tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.with(ErrorLayer::default());
tracing::subscriber::set_global_default(subscriber)?;
tracing::info!("Initialized tracing. Starting Daedalus!");
if check_env_vars() {
tracing::error!("Some environment variables are missing!");
return Ok(());
}
let semaphore = Arc::new(Semaphore::new(
dotenvy::var("CONCURRENCY_LIMIT")
.ok()
.and_then(|x| x.parse().ok())
.unwrap_or(10),
));
// path, upload file
let upload_files: DashMap<String, UploadFile> = DashMap::new();
// path, mirror artifact
let mirror_artifacts: DashMap<String, MirrorArtifact> = DashMap::new();
minecraft::fetch(semaphore.clone(), &upload_files, &mirror_artifacts)
.await?;
fabric::fetch_fabric(semaphore.clone(), &upload_files, &mirror_artifacts)
.await?;
fabric::fetch_quilt(semaphore.clone(), &upload_files, &mirror_artifacts)
.await?;
forge::fetch_neo(semaphore.clone(), &upload_files, &mirror_artifacts)
.await?;
forge::fetch_forge(semaphore.clone(), &upload_files, &mirror_artifacts)
.await?;
futures::future::try_join_all(upload_files.iter().map(|x| {
upload_file_to_bucket(
x.key().clone(),
x.value().file.clone(),
x.value().content_type.clone(),
&semaphore,
)
}))
.await?;
futures::future::try_join_all(mirror_artifacts.iter().map(|x| {
upload_url_to_bucket_mirrors(
format!("maven/{}", x.key()),
x.value()
.mirrors
.iter()
.map(|mirror| {
if mirror.entire_url {
mirror.path.clone()
} else {
format!("{}{}", mirror.path, x.key())
}
})
.collect(),
x.sha1.clone(),
&semaphore,
)
}))
.await?;
if dotenvy::var("CLOUDFLARE_INTEGRATION")
.ok()
.and_then(|x| x.parse::<bool>().ok())
.unwrap_or(false)
{
if let Ok(token) = dotenvy::var("CLOUDFLARE_TOKEN") {
if let Ok(zone_id) = dotenvy::var("CLOUDFLARE_ZONE_ID") {
let cache_clears = upload_files
.into_iter()
.map(|x| format_url(&x.0))
.chain(
mirror_artifacts
.into_iter()
.map(|x| format_url(&format!("maven/{}", x.0))),
)
.collect::<Vec<_>>();
// Cloudflare ratelimits cache clears to 500 files per request
for chunk in cache_clears.chunks(500) {
REQWEST_CLIENT.post(format!("https://api.cloudflare.com/client/v4/zones/{zone_id}/purge_cache"))
.bearer_auth(&token)
.json(&serde_json::json!({
"files": chunk
}))
.send()
.await
.map_err(|err| {
ErrorKind::Fetch {
inner: err,
item: "cloudflare clear cache".to_string(),
}
})?
.error_for_status()
.map_err(|err| {
ErrorKind::Fetch {
inner: err,
item: "cloudflare clear cache".to_string(),
}
})?;
}
}
}
}
Ok(())
}
pub struct UploadFile {
file: bytes::Bytes,
content_type: Option<String>,
}
pub struct MirrorArtifact {
pub sha1: Option<String>,
pub mirrors: DashSet<Mirror>,
}
#[derive(Eq, PartialEq, Hash)]
pub struct Mirror {
path: String,
entire_url: bool,
}
#[tracing::instrument(skip(mirror_artifacts))]
pub fn insert_mirrored_artifact(
artifact: &str,
sha1: Option<String>,
mirrors: Vec<String>,
entire_url: bool,
mirror_artifacts: &DashMap<String, MirrorArtifact>,
) -> Result<()> {
let val = mirror_artifacts
.entry(get_path_from_artifact(artifact)?)
.or_insert(MirrorArtifact {
sha1,
mirrors: DashSet::new(),
});
for mirror in mirrors {
val.mirrors.insert(Mirror {
path: mirror,
entire_url,
});
}
Ok(())
}
fn check_env_vars() -> bool {
let mut failed = false;
fn check_var<T: std::str::FromStr>(var: &str) -> bool {
if dotenvy::var(var)
.ok()
.and_then(|s| s.parse::<T>().ok())
.is_none()
{
tracing::warn!(
"Variable `{}` missing in dotenvy or not of type `{}`",
var,
std::any::type_name::<T>()
);
true
} else {
false
}
}
failed |= check_var::<String>("BASE_URL");
failed |= check_var::<String>("S3_ACCESS_TOKEN");
failed |= check_var::<String>("S3_SECRET");
failed |= check_var::<String>("S3_URL");
failed |= check_var::<String>("S3_REGION");
failed |= check_var::<String>("S3_BUCKET_NAME");
if dotenvy::var("CLOUDFLARE_INTEGRATION")
.ok()
.and_then(|x| x.parse::<bool>().ok())
.unwrap_or(false)
{
failed |= check_var::<String>("CLOUDFLARE_TOKEN");
failed |= check_var::<String>("CLOUDFLARE_ZONE_ID");
}
failed
}

View File

@ -0,0 +1,230 @@
use crate::util::fetch_json;
use crate::{
util::download_file, util::format_url, util::sha1_async, Error,
MirrorArtifact, UploadFile,
};
use daedalus::minecraft::{
merge_partial_library, Library, PartialLibrary, VersionInfo,
VersionManifest, VERSION_MANIFEST_URL,
};
use dashmap::DashMap;
use serde::Deserialize;
use std::sync::Arc;
use tokio::sync::Semaphore;
#[tracing::instrument(skip(semaphore, upload_files, _mirror_artifacts))]
pub async fn fetch(
semaphore: Arc<Semaphore>,
upload_files: &DashMap<String, UploadFile>,
_mirror_artifacts: &DashMap<String, MirrorArtifact>,
) -> Result<(), Error> {
let modrinth_manifest = fetch_json::<VersionManifest>(
&format_url(&format!(
"minecraft/v{}/manifest.json",
daedalus::minecraft::CURRENT_FORMAT_VERSION
)),
&semaphore,
)
.await
.ok();
let mojang_manifest =
fetch_json::<VersionManifest>(VERSION_MANIFEST_URL, &semaphore).await?;
// TODO: experimental snapshots: https://github.com/PrismLauncher/meta/blob/main/meta/common/mojang-minecraft-experiments.json
// TODO: old snapshots: https://github.com/PrismLauncher/meta/blob/main/meta/common/mojang-minecraft-old-snapshots.json
// We check Modrinth's version manifest and compare if the version 1) exists in Modrinth's database and 2) is unchanged
// If they are not, we will fetch them
let (fetch_versions, existing_versions) =
if let Some(mut modrinth_manifest) = modrinth_manifest {
let (mut fetch_versions, mut existing_versions) =
(Vec::new(), Vec::new());
for version in mojang_manifest.versions {
if let Some(index) = modrinth_manifest
.versions
.iter()
.position(|x| x.id == version.id)
{
let modrinth_version =
modrinth_manifest.versions.remove(index);
if modrinth_version
.original_sha1
.as_ref()
.map(|x| x == &version.sha1)
.unwrap_or(false)
{
existing_versions.push(modrinth_version);
} else {
fetch_versions.push(version);
}
} else {
fetch_versions.push(version);
}
}
(fetch_versions, existing_versions)
} else {
(mojang_manifest.versions, Vec::new())
};
if !fetch_versions.is_empty() {
let version_manifests = futures::future::try_join_all(
fetch_versions
.iter()
.map(|x| download_file(&x.url, Some(&x.sha1), &semaphore)),
)
.await?
.into_iter()
.map(|x| serde_json::from_slice(&x))
.collect::<Result<Vec<VersionInfo>, serde_json::Error>>()?;
// Patch libraries of Minecraft versions for M-series Mac Support, Better Linux Compatibility, etc
let library_patches = fetch_library_patches()?;
let patched_version_manifests = version_manifests
.into_iter()
.map(|mut x| {
if !library_patches.is_empty() {
let mut new_libraries = Vec::new();
for library in x.libraries {
let mut libs = patch_library(&library_patches, library);
new_libraries.append(&mut libs)
}
x.libraries = new_libraries
}
x
})
.collect::<Vec<_>>();
// serialize + compute hashes
let serialized_version_manifests = patched_version_manifests
.iter()
.map(|x| serde_json::to_vec(x).map(bytes::Bytes::from))
.collect::<Result<Vec<_>, serde_json::Error>>()?;
let hashes_version_manifests = futures::future::try_join_all(
serialized_version_manifests
.iter()
.map(|x| sha1_async(x.clone())),
)
.await?;
// We upload the new version manifests and add them to the versions list
let mut new_versions = patched_version_manifests
.into_iter()
.zip(serialized_version_manifests.into_iter())
.zip(hashes_version_manifests.into_iter())
.map(|((version, bytes), hash)| {
let version_path = format!(
"minecraft/v{}/versions/{}.json",
daedalus::minecraft::CURRENT_FORMAT_VERSION,
version.id
);
let url = format_url(&version_path);
upload_files.insert(
version_path,
UploadFile {
file: bytes,
content_type: Some("application/json".to_string()),
},
);
daedalus::minecraft::Version {
original_sha1: fetch_versions
.iter()
.find(|x| x.id == version.id)
.map(|x| x.sha1.clone()),
id: version.id,
type_: version.type_,
url,
time: version.time,
release_time: version.release_time,
sha1: hash,
compliance_level: 1,
}
})
.chain(existing_versions.into_iter())
.collect::<Vec<_>>();
new_versions.sort_by(|a, b| b.release_time.cmp(&a.release_time));
// create and upload the new manifest
let version_manifest_path = format!(
"minecraft/v{}/manifest.json",
daedalus::minecraft::CURRENT_FORMAT_VERSION
);
let new_manifest = VersionManifest {
latest: mojang_manifest.latest,
versions: new_versions,
};
upload_files.insert(
version_manifest_path,
UploadFile {
file: bytes::Bytes::from(serde_json::to_vec(&new_manifest)?),
content_type: Some("application/json".to_string()),
},
);
}
Ok(())
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct LibraryPatch {
#[serde(rename = "_comment")]
pub _comment: String,
#[serde(rename = "match")]
pub match_: Vec<String>,
pub additional_libraries: Option<Vec<Library>>,
#[serde(rename = "override")]
pub override_: Option<PartialLibrary>,
pub patch_additional_libraries: Option<bool>,
}
fn fetch_library_patches() -> Result<Vec<LibraryPatch>, Error> {
let patches = include_bytes!("../library-patches.json");
Ok(serde_json::from_slice(patches)?)
}
pub fn patch_library(
patches: &Vec<LibraryPatch>,
mut library: Library,
) -> Vec<Library> {
let mut val = Vec::new();
let actual_patches = patches
.iter()
.filter(|x| x.match_.contains(&library.name))
.collect::<Vec<_>>();
if !actual_patches.is_empty() {
for patch in actual_patches {
if let Some(override_) = &patch.override_ {
library = merge_partial_library(override_.clone(), library);
}
if let Some(additional_libraries) = &patch.additional_libraries {
for additional_library in additional_libraries {
if patch.patch_additional_libraries.unwrap_or(false) {
let mut libs =
patch_library(patches, additional_library.clone());
val.append(&mut libs)
} else {
val.push(additional_library.clone());
}
}
}
}
val.push(library);
} else {
val.push(library);
}
val
}

View File

@ -0,0 +1,234 @@
use crate::{Error, ErrorKind};
use bytes::Bytes;
use s3::creds::Credentials;
use s3::{Bucket, Region};
use serde::de::DeserializeOwned;
use std::sync::Arc;
use tokio::sync::Semaphore;
lazy_static::lazy_static! {
static ref BUCKET : Bucket = {
let region = dotenvy::var("S3_REGION").unwrap();
let b = Bucket::new(
&dotenvy::var("S3_BUCKET_NAME").unwrap(),
if &*region == "r2" {
Region::R2 {
account_id: dotenvy::var("S3_URL").unwrap(),
}
} else {
Region::Custom {
region: region.clone(),
endpoint: dotenvy::var("S3_URL").unwrap(),
}
},
Credentials::new(
Some(&*dotenvy::var("S3_ACCESS_TOKEN").unwrap()),
Some(&*dotenvy::var("S3_SECRET").unwrap()),
None,
None,
None,
).unwrap(),
).unwrap();
if region == "path-style" {
b.with_path_style()
} else {
b
}
};
}
lazy_static::lazy_static! {
pub static ref REQWEST_CLIENT: reqwest::Client = {
let mut headers = reqwest::header::HeaderMap::new();
if let Ok(header) = reqwest::header::HeaderValue::from_str(&format!(
"modrinth/daedalus/{} (support@modrinth.com)",
env!("CARGO_PKG_VERSION")
)) {
headers.insert(reqwest::header::USER_AGENT, header);
}
reqwest::Client::builder()
.tcp_keepalive(Some(std::time::Duration::from_secs(10)))
.timeout(std::time::Duration::from_secs(15))
.default_headers(headers)
.build()
.unwrap()
};
}
#[tracing::instrument(skip(bytes, semaphore))]
pub async fn upload_file_to_bucket(
path: String,
bytes: Bytes,
content_type: Option<String>,
semaphore: &Arc<Semaphore>,
) -> Result<(), Error> {
let _permit = semaphore.acquire().await?;
let key = path.clone();
const RETRIES: i32 = 3;
for attempt in 1..=(RETRIES + 1) {
tracing::trace!("Attempting file upload, attempt {attempt}");
let result = if let Some(ref content_type) = content_type {
BUCKET
.put_object_with_content_type(key.clone(), &bytes, content_type)
.await
} else {
BUCKET.put_object(key.clone(), &bytes).await
}
.map_err(|err| ErrorKind::S3 {
inner: err,
file: path.clone(),
});
match result {
Ok(_) => return Ok(()),
Err(_) if attempt <= RETRIES => continue,
Err(_) => {
result?;
}
}
}
unreachable!()
}
pub async fn upload_url_to_bucket_mirrors(
upload_path: String,
mirrors: Vec<String>,
sha1: Option<String>,
semaphore: &Arc<Semaphore>,
) -> Result<(), Error> {
if mirrors.is_empty() {
return Err(ErrorKind::InvalidInput(
"No mirrors provided!".to_string(),
)
.into());
}
for (index, mirror) in mirrors.iter().enumerate() {
let result = upload_url_to_bucket(
upload_path.clone(),
mirror.clone(),
sha1.clone(),
semaphore,
)
.await;
if result.is_ok() || (result.is_err() && index == (mirrors.len() - 1)) {
return result;
}
}
unreachable!()
}
#[tracing::instrument(skip(semaphore))]
pub async fn upload_url_to_bucket(
path: String,
url: String,
sha1: Option<String>,
semaphore: &Arc<Semaphore>,
) -> Result<(), Error> {
let data = download_file(&url, sha1.as_deref(), semaphore).await?;
upload_file_to_bucket(path, data, None, semaphore).await?;
Ok(())
}
#[tracing::instrument(skip(bytes))]
pub async fn sha1_async(bytes: Bytes) -> Result<String, Error> {
let hash = tokio::task::spawn_blocking(move || {
sha1_smol::Sha1::from(bytes).hexdigest()
})
.await?;
Ok(hash)
}
#[tracing::instrument(skip(semaphore))]
pub async fn download_file(
url: &str,
sha1: Option<&str>,
semaphore: &Arc<Semaphore>,
) -> Result<bytes::Bytes, crate::Error> {
let _permit = semaphore.acquire().await?;
tracing::trace!("Starting file download");
const RETRIES: u32 = 10;
for attempt in 1..=(RETRIES + 1) {
let result = REQWEST_CLIENT
.get(url.replace("http://", "https://"))
.send()
.await
.and_then(|x| x.error_for_status());
match result {
Ok(x) => {
let bytes = x.bytes().await;
if let Ok(bytes) = bytes {
if let Some(sha1) = sha1 {
if &*sha1_async(bytes.clone()).await? != sha1 {
if attempt <= 3 {
continue;
} else {
return Err(
crate::ErrorKind::ChecksumFailure {
hash: sha1.to_string(),
url: url.to_string(),
tries: attempt,
}
.into(),
);
}
}
}
return Ok(bytes);
} else if attempt <= RETRIES {
continue;
} else if let Err(err) = bytes {
return Err(crate::ErrorKind::Fetch {
inner: err,
item: url.to_string(),
}
.into());
}
}
Err(_) if attempt <= RETRIES => continue,
Err(err) => {
return Err(crate::ErrorKind::Fetch {
inner: err,
item: url.to_string(),
}
.into())
}
}
}
unreachable!()
}
pub async fn fetch_json<T: DeserializeOwned>(
url: &str,
semaphore: &Arc<Semaphore>,
) -> Result<T, Error> {
Ok(serde_json::from_slice(
&download_file(url, None, semaphore).await?,
)?)
}
pub async fn fetch_xml<T: DeserializeOwned>(
url: &str,
semaphore: &Arc<Semaphore>,
) -> Result<T, Error> {
Ok(serde_xml_rs::from_reader(
&*download_file(url, None, semaphore).await?,
)?)
}
pub fn format_url(path: &str) -> String {
format!("{}/{}", &*dotenvy::var("BASE_URL").unwrap(), path)
}

View File

@ -0,0 +1,21 @@
[package]
name = "daedalus"
version = "0.2.3"
authors = ["Jai A <jai@modrinth.com>"]
edition = "2021"
license = "MIT"
description = "Utilities for querying and parsing Minecraft metadata"
repository = "https://github.com/modrinth/daedalus/"
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
keywords = ["minecraft", "launcher"]
categories = ["game-development", "api-bindings"]
readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
chrono = { version = "0.4", features = ["serde"] }
bytes = "1"
thiserror = "1.0"

View File

@ -0,0 +1,7 @@
Copyright © 2024 Rinth, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1,4 @@
# Daedalus
Daedalus (the rust library) is a library providing model structs and methods for requesting and parsing things
from Minecraft and other mod loaders meta APIs.

View File

@ -0,0 +1,10 @@
{
"name": "@modrinth/daedalus_client",
"scripts": {
"build": "cargo build --release",
"lint": "cargo fmt --check && cargo clippy --all-targets --all-features -- -D warnings",
"fix": "cargo fmt && cargo clippy --fix",
"dev": "cargo run",
"test": "cargo test"
}
}

View File

@ -0,0 +1,102 @@
//! # Daedalus
//!
//! Daedalus is a library which provides models and methods to fetch metadata about games
#![warn(missing_docs, unused_import_braces, missing_debug_implementations)]
/// Models and methods for fetching metadata for Minecraft
pub mod minecraft;
/// Models and methods for fetching metadata for Minecraft mod loaders
pub mod modded;
#[derive(thiserror::Error, Debug)]
/// An error type representing possible errors when fetching metadata
pub enum Error {
/// Error while parsing input
#[error("{0}")]
ParseError(String),
}
/// Converts a maven artifact to a path
pub fn get_path_from_artifact(artifact: &str) -> Result<String, Error> {
let name_items = artifact.split(':').collect::<Vec<&str>>();
let package = name_items.first().ok_or_else(|| {
Error::ParseError(format!(
"Unable to find package for library {}",
&artifact
))
})?;
let name = name_items.get(1).ok_or_else(|| {
Error::ParseError(format!(
"Unable to find name for library {}",
&artifact
))
})?;
if name_items.len() == 3 {
let version_ext = name_items
.get(2)
.ok_or_else(|| {
Error::ParseError(format!(
"Unable to find version for library {}",
&artifact
))
})?
.split('@')
.collect::<Vec<&str>>();
let version = version_ext.first().ok_or_else(|| {
Error::ParseError(format!(
"Unable to find version for library {}",
&artifact
))
})?;
let ext = version_ext.get(1);
Ok(format!(
"{}/{}/{}/{}-{}.{}",
package.replace('.', "/"),
name,
version,
name,
version,
ext.unwrap_or(&"jar")
))
} else {
let version = name_items.get(2).ok_or_else(|| {
Error::ParseError(format!(
"Unable to find version for library {}",
&artifact
))
})?;
let data_ext = name_items
.get(3)
.ok_or_else(|| {
Error::ParseError(format!(
"Unable to find data for library {}",
&artifact
))
})?
.split('@')
.collect::<Vec<&str>>();
let data = data_ext.first().ok_or_else(|| {
Error::ParseError(format!(
"Unable to find data for library {}",
&artifact
))
})?;
let ext = data_ext.get(1);
Ok(format!(
"{}/{}/{}/{}-{}-{}.{}",
package.replace('.', "/"),
name,
version,
name,
version,
data,
ext.unwrap_or(&"jar")
))
}
}

View File

@ -0,0 +1,461 @@
use crate::modded::{Processor, SidedDataEntry};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// The latest version of the format the model structs deserialize to
pub const CURRENT_FORMAT_VERSION: usize = 0;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
/// The version type
pub enum VersionType {
/// A major version, which is stable for all players to use
Release,
/// An experimental version, which is unstable and used for feature previews and beta testing
Snapshot,
/// The oldest versions before the game was released
OldAlpha,
/// Early versions of the game
OldBeta,
}
impl VersionType {
/// Converts the version type to a string
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")]
/// A game version of Minecraft
pub struct Version {
/// A unique identifier of the version
pub id: String,
#[serde(rename = "type")]
/// The release type of the version
pub type_: VersionType,
/// A link to additional information about the version
pub url: String,
/// The latest time a file in this version was updated
pub time: DateTime<Utc>,
/// The time this version was released
pub release_time: DateTime<Utc>,
/// The SHA1 hash of the additional information about the version
pub sha1: String,
/// Whether the version supports the latest player safety features
pub compliance_level: u32,
#[serde(skip_serializing_if = "Option::is_none")]
/// (Modrinth Provided) The SHA1 hash of the original unmodified Minecraft versions JSON
pub original_sha1: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// The latest snapshot and release of the game
pub struct LatestVersion {
/// The version id of the latest release
pub release: String,
/// The version id of the latest snapshot
pub snapshot: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Data of all game versions of Minecraft
pub struct VersionManifest {
/// A struct containing the latest snapshot and release of the game
pub latest: LatestVersion,
/// A list of game versions of Minecraft
pub versions: Vec<Version>,
}
/// The URL to the version manifest
pub const VERSION_MANIFEST_URL: &str =
"https://piston-meta.mojang.com/mc/game/version_manifest_v2.json";
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
/// Information about the assets of the game
pub struct AssetIndex {
/// The game version ID the assets are for
pub id: String,
/// The SHA1 hash of the assets index
pub sha1: String,
/// The size of the assets index
pub size: u32,
/// The size of the game version's assets
pub total_size: u32,
/// A URL to a file which contains information about the version's assets
pub url: String,
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash)]
#[serde(rename_all = "snake_case")]
/// The type of download
pub enum DownloadType {
/// The download is for the game client
Client,
/// The download is mappings for the game
ClientMappings,
/// The download is for the game server
Server,
/// The download is mappings for the game server
ServerMappings,
/// The download is for the windows server
WindowsServer,
}
#[derive(Serialize, Deserialize, Debug)]
/// Download information of a file
pub struct Download {
/// The SHA1 hash of the file
pub sha1: String,
/// The size of the file
pub size: u32,
/// The URL where the file can be downloaded
pub url: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Download information of a library
pub struct LibraryDownload {
#[serde(skip_serializing_if = "Option::is_none")]
/// The path that the library should be saved to
pub path: Option<String>,
/// The SHA1 hash of the library
pub sha1: String,
/// The size of the library
pub size: u32,
/// The URL where the library can be downloaded
pub url: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A list of files that should be downloaded for libraries
pub struct LibraryDownloads {
#[serde(skip_serializing_if = "Option::is_none")]
/// The primary library artifact
pub artifact: Option<LibraryDownload>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Conditional files that may be needed to be downloaded alongside the library
/// The HashMap key specifies a classifier as additional information for downloading files
pub classifiers: Option<HashMap<String, LibraryDownload>>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "snake_case")]
/// The action a rule can follow
pub enum RuleAction {
/// The rule's status allows something to be done
Allow,
/// The rule's status disallows something to be done
Disallow,
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Clone)]
#[serde(rename_all = "kebab-case")]
/// An enum representing the different types of operating systems
pub enum Os {
/// MacOS (x86)
Osx,
/// M1-Based Macs
OsxArm64,
/// Windows (x86)
Windows,
/// Windows ARM
WindowsArm64,
/// Linux (x86) and its derivatives
Linux,
/// Linux ARM 64
LinuxArm64,
/// Linux ARM 32
LinuxArm32,
/// The OS is unknown
Unknown,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A rule which depends on what OS the user is on
pub struct OsRule {
#[serde(skip_serializing_if = "Option::is_none")]
/// The name of the OS
pub name: Option<Os>,
#[serde(skip_serializing_if = "Option::is_none")]
/// The version of the OS. This is normally a RegEx
pub version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
/// The architecture of the OS
pub arch: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A rule which depends on the toggled features of the launcher
pub struct FeatureRule {
#[serde(skip_serializing_if = "Option::is_none")]
/// Whether the user is in demo mode
pub is_demo_user: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Whether the user is using a custom resolution
pub has_custom_resolution: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Whether the launcher has quick plays support
pub has_quick_plays_support: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Whether the instance is being launched to a single-player world
pub is_quick_play_singleplayer: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Whether the instance is being launched to a multi-player world
pub is_quick_play_multiplayer: Option<bool>,
/// Whether the instance is being launched to a realms world
pub is_quick_play_realms: Option<bool>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A rule deciding whether a file is downloaded, an argument is used, etc.
pub struct Rule {
/// The action the rule takes
pub action: RuleAction,
#[serde(skip_serializing_if = "Option::is_none")]
/// The OS rule
pub os: Option<OsRule>,
#[serde(skip_serializing_if = "Option::is_none")]
/// The feature rule
pub features: Option<FeatureRule>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// Information delegating the extraction of the library
pub struct LibraryExtract {
#[serde(skip_serializing_if = "Option::is_none")]
/// Files/Folders to be excluded from the extraction of the library
pub exclude: Option<Vec<String>>,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
/// Information about the java version the game needs
pub struct JavaVersion {
/// The component needed for the Java installation
pub component: String,
/// The major Java version number
pub major_version: u32,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A library which the game relies on to run
pub struct Library {
#[serde(skip_serializing_if = "Option::is_none")]
/// The files the library has
pub downloads: Option<LibraryDownloads>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Rules of the extraction of the file
pub extract: Option<LibraryExtract>,
/// The maven name of the library. The format is `groupId:artifactId:version`
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
/// The URL to the repository where the library can be downloaded
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Native files that the library relies on
pub natives: Option<HashMap<Os, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Rules deciding whether the library should be downloaded or not
pub rules: Option<Vec<Rule>>,
#[serde(skip_serializing_if = "Option::is_none")]
/// SHA1 Checksums for validating the library's integrity. Only present for forge libraries
pub checksums: Option<Vec<String>>,
#[serde(default = "default_include_in_classpath")]
/// Whether the library should be included in the classpath at the game's launch
pub include_in_classpath: bool,
#[serde(default = "default_downloadable")]
/// Whether the library should be downloaded
pub downloadable: bool,
}
#[derive(Deserialize, Debug, Clone)]
/// A partial library which should be merged with a full library
pub struct PartialLibrary {
/// The files the library has
pub downloads: Option<LibraryDownloads>,
/// Rules of the extraction of the file
pub extract: Option<LibraryExtract>,
/// The maven name of the library. The format is `groupId:artifactId:version`
pub name: Option<String>,
/// The URL to the repository where the library can be downloaded
pub url: Option<String>,
/// Native files that the library relies on
pub natives: Option<HashMap<Os, String>>,
/// Rules deciding whether the library should be downloaded or not
pub rules: Option<Vec<Rule>>,
/// SHA1 Checksums for validating the library's integrity. Only present for forge libraries
pub checksums: Option<Vec<String>>,
/// Whether the library should be included in the classpath at the game's launch
pub include_in_classpath: Option<bool>,
}
/// Merges a partial library to make a complete library
pub fn merge_partial_library(
partial: PartialLibrary,
mut merge: Library,
) -> Library {
if let Some(downloads) = partial.downloads {
if let Some(merge_downloads) = &mut merge.downloads {
if let Some(artifact) = downloads.artifact {
merge_downloads.artifact = Some(artifact);
}
if let Some(classifiers) = downloads.classifiers {
if let Some(merge_classifiers) =
&mut merge_downloads.classifiers
{
for classifier in classifiers {
merge_classifiers.insert(classifier.0, classifier.1);
}
} else {
merge_downloads.classifiers = Some(classifiers);
}
}
} else {
merge.downloads = Some(downloads)
}
}
if let Some(extract) = partial.extract {
merge.extract = Some(extract)
}
if let Some(name) = partial.name {
merge.name = name
}
if let Some(url) = partial.url {
merge.url = Some(url)
}
if let Some(natives) = partial.natives {
if let Some(merge_natives) = &mut merge.natives {
for native in natives {
merge_natives.insert(native.0, native.1);
}
} else {
merge.natives = Some(natives);
}
}
if let Some(rules) = partial.rules {
if let Some(merge_rules) = &mut merge.rules {
for rule in rules {
merge_rules.push(rule);
}
} else {
merge.rules = Some(rules)
}
}
if let Some(checksums) = partial.checksums {
merge.checksums = Some(checksums)
}
if let Some(include_in_classpath) = partial.include_in_classpath {
merge.include_in_classpath = include_in_classpath
}
merge
}
fn default_include_in_classpath() -> bool {
true
}
fn default_downloadable() -> bool {
true
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// A container for an argument or multiple arguments
pub enum ArgumentValue {
/// The container has one argument
Single(String),
/// The container has multiple arguments
Many(Vec<String>),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
/// A command line argument passed to a program
pub enum Argument {
/// An argument which is applied no matter what
Normal(String),
/// An argument which is only applied if certain conditions are met
Ruled {
/// The rules deciding whether the argument(s) is used or not
rules: Vec<Rule>,
/// The container of the argument(s) that should be applied accordingly
value: ArgumentValue,
},
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Hash, Clone, Copy)]
#[serde(rename_all = "snake_case")]
/// The type of argument
pub enum ArgumentType {
/// The argument is passed to the game
Game,
/// The argument is passed to the JVM
Jvm,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
/// Information about a version
pub struct VersionInfo {
#[serde(skip_serializing_if = "Option::is_none")]
/// Arguments passed to the game or JVM
pub arguments: Option<HashMap<ArgumentType, Vec<Argument>>>,
/// Assets for the game
pub asset_index: AssetIndex,
/// The version ID of the assets
pub assets: String,
/// Game downloads of the version
pub downloads: HashMap<DownloadType, Download>,
/// The version ID of the version
pub id: String,
/// The Java version this version supports
pub java_version: Option<JavaVersion>,
/// Libraries that the version depends on
pub libraries: Vec<Library>,
/// The classpath to the main class to launch the game
pub main_class: String,
#[serde(skip_serializing_if = "Option::is_none")]
/// (Legacy) Arguments passed to the game
pub minecraft_arguments: Option<String>,
/// The minimum version of the Minecraft Launcher that can run this version of the game
pub minimum_launcher_version: u32,
/// The time that the version was released
pub release_time: DateTime<Utc>,
/// The latest time a file in this version was updated
pub time: DateTime<Utc>,
#[serde(rename = "type")]
/// The type of version
pub type_: VersionType,
#[serde(skip_serializing_if = "Option::is_none")]
/// (Forge-only)
pub data: Option<HashMap<String, SidedDataEntry>>,
#[serde(skip_serializing_if = "Option::is_none")]
/// (Forge-only) The list of processors to run after downloading the files
pub processors: Option<Vec<Processor>>,
}
#[derive(Serialize, Deserialize, Debug)]
/// An asset of the game
pub struct Asset {
/// The SHA1 hash of the asset file
pub hash: String,
/// The size of the asset file
pub size: u32,
}
#[derive(Serialize, Deserialize, Debug)]
/// An index containing all assets the game needs
pub struct AssetsIndex {
/// A hashmap containing the filename (key) and asset (value)
pub objects: HashMap<String, Asset>,
}

View File

@ -0,0 +1,208 @@
use crate::minecraft::{
Argument, ArgumentType, Library, VersionInfo, VersionType,
};
use chrono::{DateTime, TimeZone, Utc};
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::HashMap;
/// The latest version of the format the fabric model structs deserialize to
pub const CURRENT_FABRIC_FORMAT_VERSION: usize = 0;
/// The latest version of the format the fabric model structs deserialize to
pub const CURRENT_FORGE_FORMAT_VERSION: usize = 0;
/// The latest version of the format the quilt model structs deserialize to
pub const CURRENT_QUILT_FORMAT_VERSION: usize = 0;
/// The latest version of the format the neoforge model structs deserialize to
pub const CURRENT_NEOFORGE_FORMAT_VERSION: usize = 0;
/// The dummy replace string library names, inheritsFrom, and version names should be replaced with
pub const DUMMY_REPLACE_STRING: &str = "${modrinth.gameVersion}";
/// A data variable entry that depends on the side of the installation
#[derive(Serialize, Deserialize, Debug)]
pub struct SidedDataEntry {
/// The value on the client
pub client: String,
/// The value on the server
pub server: String,
}
fn deserialize_date<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
serde_json::from_str::<DateTime<Utc>>(&format!("\"{s}\""))
.or_else(|_| Utc.datetime_from_str(&s, "%Y-%m-%dT%H:%M:%S%.9f"))
.map_err(serde::de::Error::custom)
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
/// A partial version returned by fabric meta
pub struct PartialVersionInfo {
/// The version ID of the version
pub id: String,
/// The version ID this partial version inherits from
pub inherits_from: String,
/// The time that the version was released
#[serde(deserialize_with = "deserialize_date")]
pub release_time: DateTime<Utc>,
/// The latest time a file in this version was updated
#[serde(deserialize_with = "deserialize_date")]
pub time: DateTime<Utc>,
#[serde(skip_serializing_if = "Option::is_none")]
/// The classpath to the main class to launch the game
pub main_class: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
/// (Legacy) Arguments passed to the game
pub minecraft_arguments: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Arguments passed to the game or JVM
pub arguments: Option<HashMap<ArgumentType, Vec<Argument>>>,
/// Libraries that the version depends on
pub libraries: Vec<Library>,
#[serde(rename = "type")]
/// The type of version
pub type_: VersionType,
#[serde(skip_serializing_if = "Option::is_none")]
/// (Forge-only)
pub data: Option<HashMap<String, SidedDataEntry>>,
#[serde(skip_serializing_if = "Option::is_none")]
/// (Forge-only) The list of processors to run after downloading the files
pub processors: Option<Vec<Processor>>,
}
/// A processor to be ran after downloading the files
#[derive(Serialize, Deserialize, Debug)]
pub struct Processor {
/// Maven coordinates for the JAR library of this processor.
pub jar: String,
/// Maven coordinates for all the libraries that must be included in classpath when running this processor.
pub classpath: Vec<String>,
/// Arguments for this processor.
pub args: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Represents a map of outputs. Keys and values can be data values
pub outputs: Option<HashMap<String, String>>,
#[serde(skip_serializing_if = "Option::is_none")]
/// Which sides this processor shall be ran on.
/// Valid values: client, server, extract
pub sides: Option<Vec<String>>,
}
/// Merges a partial version into a complete one
pub fn merge_partial_version(
partial: PartialVersionInfo,
merge: VersionInfo,
) -> VersionInfo {
let merge_id = merge.id.clone();
let mut libraries = vec![];
// We skip duplicate libraries that exist already in the partial version
for mut lib in merge.libraries {
let lib_artifact = lib.name.rsplit_once(':').map(|x| x.0);
if let Some(lib_artifact) = lib_artifact {
if !partial.libraries.iter().any(|x| {
let target_artifact = x.name.rsplit_once(':').map(|x| x.0);
target_artifact == Some(lib_artifact) && x.include_in_classpath
}) {
libraries.push(lib);
} else {
lib.include_in_classpath = false;
}
} else {
libraries.push(lib);
}
}
VersionInfo {
arguments: if let Some(partial_args) = partial.arguments {
if let Some(merge_args) = merge.arguments {
let mut new_map = HashMap::new();
fn add_keys(
new_map: &mut HashMap<ArgumentType, Vec<Argument>>,
args: HashMap<ArgumentType, Vec<Argument>>,
) {
for (type_, arguments) in args {
for arg in arguments {
if let Some(vec) = new_map.get_mut(&type_) {
vec.push(arg);
} else {
new_map.insert(type_, vec![arg]);
}
}
}
}
add_keys(&mut new_map, merge_args);
add_keys(&mut new_map, partial_args);
Some(new_map)
} else {
Some(partial_args)
}
} else {
merge.arguments
},
asset_index: merge.asset_index,
assets: merge.assets,
downloads: merge.downloads,
id: partial.id.replace(DUMMY_REPLACE_STRING, &merge_id),
java_version: merge.java_version,
libraries: libraries.into_iter()
.chain(partial.libraries)
.map(|mut x| {
x.name = x.name.replace(DUMMY_REPLACE_STRING, &merge_id);
x
})
.collect::<Vec<_>>(),
main_class: if let Some(main_class) = partial.main_class {
main_class
} else {
merge.main_class
},
minecraft_arguments: partial.minecraft_arguments,
minimum_launcher_version: merge.minimum_launcher_version,
release_time: partial.release_time,
time: partial.time,
type_: partial.type_,
data: partial.data,
processors: partial.processors,
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
/// A manifest containing information about a mod loader's versions
pub struct Manifest {
/// The game versions the mod loader supports
pub game_versions: Vec<Version>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A game version of Minecraft
pub struct Version {
/// The minecraft version ID
pub id: String,
/// Whether the release is stable or not
pub stable: bool,
/// A map that contains loader versions for the game version
pub loaders: Vec<LoaderVersion>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
/// A version of a Minecraft mod loader
pub struct LoaderVersion {
/// The version ID of the loader
pub id: String,
/// The URL of the version's manifest
pub url: String,
/// Whether the loader is stable or not
pub stable: bool,
}