fix: switch default profile to terminal and fixed some comments

This commit is contained in:
objz committed 2026-04-18 19:44:59 +02:00
1 parent 971375bf1f
commit 52787b57c7
71 files changed
+671 -188

No files matched your search

+5
View File
@@ -1,3 +1,6 @@
// fabric mod loader: fetches loader metadata and downloads libraries
// from fabric's maven. structurally very similar to quilt (they forked).
use std::path::Path;
use serde::{Deserialize, Serialize};
@@ -74,6 +77,8 @@ pub async fn fetch_fabric_profile(
client.get_json(&url).await
}
// each fabric library entry has a maven coordinate and a base url.
// the coordinate gets resolved to a path and combined with the url to download.
pub async fn download_fabric_libraries(
client: &HttpClient,
profile: &FabricProfile,
+9 -12
View File
@@ -1,10 +1,14 @@
// forge mod loader: version discovery via promotions API, then download
// and run the installer jar. forge doesn't have a nice metadata API like
// fabric/quilt, so it shells out to java to run their installer.
use std::collections::HashMap;
use std::path::Path;
use serde::Deserialize;
use crate::instance::loader::GameVersion;
use crate::net::{download_file, HttpClient, NetError};
use crate::net::{HttpClient, NetError, download_file};
use crate::tui::progress::set_action;
const FORGE_PROMOTIONS_URL: &str =
@@ -16,10 +20,8 @@ struct ForgePromotions {
promos: HashMap<String, String>,
}
/// Fetch available Forge loader versions for a given Minecraft game version.
///
/// Queries the Forge promotions JSON and returns unique Forge version strings
/// matching the requested game version (e.g. `"47.2.20"` for game version `"1.20.1"`).
// forge promotions use keys like "1.20.1-recommended", "1.20.1-latest"
// so this filters by game version prefix and extracts the forge version values
pub async fn fetch_forge_versions(
client: &HttpClient,
game_version: &str,
@@ -39,6 +41,8 @@ pub async fn fetch_forge_versions(
Ok(versions)
}
// extracts unique game versions from the promotion keys by splitting off
// the "-recommended"/"-latest" suffix
pub async fn fetch_forge_game_versions(client: &HttpClient) -> Result<Vec<GameVersion>, NetError> {
let promos: ForgePromotions = client.get_json(FORGE_PROMOTIONS_URL).await?;
@@ -60,9 +64,6 @@ pub async fn fetch_forge_game_versions(client: &HttpClient) -> Result<Vec<GameVe
.collect())
}
/// Download the Forge installer JAR for the given game + forge version combo.
///
/// The installer JAR is saved to `dest`. Progress is reported via the TUI progress system.
pub async fn download_forge_installer(
client: &HttpClient,
game_version: &str,
@@ -87,10 +88,6 @@ pub async fn download_forge_installer(
.await
}
/// Run the Forge installer JAR to install Forge into the instance directory.
///
/// Requires a Java runtime. The installer is invoked with `--installClient` from
/// the instance directory.
pub async fn run_forge_installer(
installer_path: &Path,
instance_dir: &Path,
+15 -4
View File
@@ -1,8 +1,11 @@
// networking layer: http client, file downloads, and shared utilities
// for fetching game assets from mojang, mod loaders, and modrinth.
pub mod fabric;
pub mod forge;
pub mod modrinth;
pub mod mojang;
pub mod neoforge;
pub mod modrinth;
pub mod quilt;
use reqwest::Client;
@@ -40,7 +43,10 @@ impl Default for HttpClient {
impl HttpClient {
pub fn new() -> Self {
let client = Client::builder()
.user_agent(format!("mcl/{} (Minecraft Launcher)", env!("CARGO_PKG_VERSION")))
.user_agent(format!(
"mcl/{} (Minecraft Launcher)",
env!("CARGO_PKG_VERSION")
))
.timeout(std::time::Duration::from_secs(30))
.build()
.unwrap_or_else(|_| Client::new());
@@ -67,8 +73,9 @@ impl HttpClient {
}
}
/// Download a file from `url` to `dest`, calling `progress_cb(bytes_downloaded, total_bytes)` as chunks arrive.
/// `total_bytes` is 0 if Content-Length is unknown.
// streams a file to disk in chunks, calling progress_cb(downloaded, total) along the way.
// total will be 0 if the server doesn't send content-length, so callers
// should handle that gracefully.
pub async fn download_file(
client: &HttpClient,
url: &str,
@@ -97,6 +104,7 @@ pub async fn download_file(
Ok(())
}
// tries JAVA_HOME first, then PATH, then just yolos "java" and hopes for the best
#[must_use]
pub fn detect_java_path() -> String {
if let Ok(java_home) = std::env::var("JAVA_HOME") {
@@ -111,6 +119,9 @@ pub fn detect_java_path() -> String {
.unwrap_or_else(|_| "java".to_string())
}
// converts maven coordinates like "org.example:artifact:1.0" into a
// filesystem path like "org/example/artifact/1.0/artifact-1.0.jar".
// supports optional classifier as a 4th component.
#[must_use]
pub fn maven_coord_to_path(coord: &str) -> Option<String> {
let parts: Vec<&str> = coord.split(':').collect();
+13
View File
@@ -1,3 +1,6 @@
// modrinth modpack support: parses URLs/slugs, fetches project metadata,
// downloads .mrpack files, and extracts loader info from pack manifests.
use serde::Deserialize;
use std::collections::HashMap;
@@ -55,6 +58,9 @@ pub enum ModrinthInput {
LocalFile(String),
}
// figures out what the user gave us: a modrinth URL, a local .mrpack file,
// or just a project slug. accepts a pretty wide range of inputs so users
// don't have to think about it.
pub fn parse_input(input: &str) -> ModrinthInput {
let input = input.trim();
@@ -85,6 +91,8 @@ pub fn parse_input(input: &str) -> ModrinthInput {
use crate::instance::models::ModLoader;
// mrpack dependencies use keys like "fabric-loader", "forge", etc.
// checks in priority order and returns the first match.
pub fn loader_from_dependencies(deps: &HashMap<String, String>) -> (Option<ModLoader>, Option<String>) {
let loaders = [
("fabric-loader", ModLoader::Fabric),
@@ -106,6 +114,8 @@ pub fn game_version_from_dependencies(deps: &HashMap<String, String>) -> Option<
const API_BASE: &str = "https://api.modrinth.com/v2";
// hand-rolled percent encoding because pulling in a crate for RFC 3986
// unreserved chars felt like overkill
fn url_encode(s: &str) -> String {
use std::fmt::Write;
let mut encoded = String::with_capacity(s.len());
@@ -149,6 +159,8 @@ pub async fn fetch_version(
client.get_json(&url).await
}
// grabs the primary file from a version, falling back to the first file
// if none is marked primary (some projects are sloppy about that)
pub async fn download_mrpack(
client: &crate::net::HttpClient,
version: &VersionInfo,
@@ -166,6 +178,7 @@ pub async fn download_mrpack(
Ok(mrpack_path)
}
// .mrpack is just a zip with modrinth.index.json at the root
pub fn parse_mrpack(path: &std::path::Path) -> Result<MrpackIndex, String> {
let file = std::fs::File::open(path).map_err(|e| format!("Cannot open .mrpack: {e}"))?;
let mut archive = zip::ZipArchive::new(file).map_err(|e| format!("Invalid ZIP: {e}"))?;
+19 -3
View File
@@ -1,16 +1,20 @@
// handles all downloads from mojang's servers: version manifests,
// client jars, libraries, and asset objects. this is the core of
// getting vanilla minecraft onto disk.
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::{
atomic::{AtomicU64, Ordering},
Arc,
atomic::{AtomicU64, Ordering},
},
};
use serde::{Deserialize, Serialize};
use tokio::task::JoinSet;
use super::{download_file, HttpClient, NetError};
use super::{HttpClient, NetError, download_file};
use crate::tui::progress::{clear, set_action, set_progress, set_sub_action};
const MANIFEST_URL: &str = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json";
@@ -239,11 +243,16 @@ pub async fn download_assets(
}
}
// assets are stored by hash with the first 2 chars as a directory prefix,
// e.g. "ab/ab1234..." - same layout mojang uses on their CDN
let mut downloads = Vec::new();
for object in asset_index.objects.values() {
if object.hash.len() < 2 {
clear();
return Err(NetError::Parse(format!("Invalid asset hash: {}", object.hash)));
return Err(NetError::Parse(format!(
"Invalid asset hash: {}",
object.hash
)));
}
let prefix = &object.hash[..2];
@@ -272,6 +281,9 @@ pub async fn download_assets(
result
}
// mojang's rule system is a bit quirky: no rules means "allow everywhere",
// and rules are evaluated in order where the last matching rule wins.
// a "disallow" for the current OS is an immediate reject though.
fn library_allowed_for_current_os(library: &Library) -> bool {
let rules = match &library.rules {
Some(rules) => rules,
@@ -306,6 +318,7 @@ fn library_allowed_for_current_os(library: &Library) -> bool {
allowed
}
// mojang calls macOS "osx" because apparently it's still 2012
fn mojang_os_name() -> &'static str {
match std::env::consts::OS {
"macos" => "osx",
@@ -313,6 +326,9 @@ fn mojang_os_name() -> &'static str {
}
}
// bounded parallel downloader. spawns up to MAX_CONCURRENT_DOWNLOADS tasks
// and feeds new ones in as each completes. collects errors but keeps going
// so it downloads as much as possible before reporting the first failure.
async fn run_parallel_downloads(
client: &HttpClient,
downloads: Vec<(String, PathBuf, String)>,
+20 -14
View File
@@ -1,9 +1,14 @@
// neoforge: the forge fork that broke away after 1.20.1.
// uses a different versioning scheme where neoforge versions map to
// minecraft versions by dropping the "1." prefix (e.g. MC 1.21 = NF 21.x).
// like forge, installation requires running their installer jar.
use std::path::Path;
use serde::Deserialize;
use crate::instance::loader::GameVersion;
use crate::net::{download_file, HttpClient, NetError};
use crate::net::{HttpClient, NetError, download_file};
use crate::tui::progress::set_action;
const NEOFORGE_MAVEN_BASE: &str = "https://maven.neoforged.net/releases/net/neoforged/neoforge";
@@ -15,10 +20,8 @@ struct NeoForgeMavenVersions {
versions: Vec<String>,
}
/// Map a Minecraft game version like "1.21" or "1.20.4" to the NeoForge version
/// prefix (e.g. "21.0." or "20.4."). NeoForge versions encode the MC version as
/// `MAJOR.MINOR.` where MAJOR is the MC major-minor (20 for 1.20.x, 21 for 1.21.x)
/// and MINOR is the MC patch (0 if absent, e.g. "1.21" → "21.0.").
// neoforge dropped the "1." from minecraft versions in their scheme,
// so "1.20.4" becomes prefix "20.4." and "1.21" becomes "21.0."
fn game_version_to_neoforge_prefix(game_version: &str) -> Option<String> {
let parts: Vec<&str> = game_version.split('.').collect();
match parts.as_slice() {
@@ -55,6 +58,8 @@ pub async fn fetch_neoforge_versions(
Ok(versions)
}
// reverse-engineers minecraft versions from neoforge version numbers.
// e.g. neoforge "21.0.x" means MC 1.21, "20.4.x" means MC 1.20.4
pub async fn fetch_neoforge_game_versions(
client: &HttpClient,
) -> Result<Vec<GameVersion>, NetError> {
@@ -64,17 +69,18 @@ pub async fn fetch_neoforge_game_versions(
for version in &maven.versions {
let parts: Vec<&str> = version.split('.').collect();
if parts.len() >= 2
&& let (Ok(major), Ok(minor)) = (parts[0].parse::<u32>(), parts[1].parse::<u32>()) {
let mc_version = if minor == 0 {
format!("1.{}", major)
} else {
format!("1.{}.{}", major, minor)
};
&& let (Ok(major), Ok(minor)) = (parts[0].parse::<u32>(), parts[1].parse::<u32>())
{
let mc_version = if minor == 0 {
format!("1.{}", major)
} else {
format!("1.{}.{}", major, minor)
};
if !game_versions.contains(&mc_version) {
game_versions.push(mc_version);
}
if !game_versions.contains(&mc_version) {
game_versions.push(mc_version);
}
}
}
game_versions.reverse();
+4 -2
View File
@@ -1,9 +1,12 @@
// quilt mod loader: fabric fork with a nearly identical metadata API.
// if you're getting deja vu reading this after fabric.rs, that's why.
use std::path::Path;
use serde::{Deserialize, Serialize};
use crate::instance::loader::GameVersion;
use crate::net::{download_file, HttpClient, NetError};
use crate::net::{HttpClient, NetError, download_file};
use crate::tui::progress::set_sub_action;
const QUILT_META_BASE: &str = "https://meta.quiltmc.org/v3";
@@ -140,5 +143,4 @@ mod tests {
Err(e) => panic!("fetch_quilt_game_versions failed: {}", e),
}
}
}