feat: add shared meta dir for downloads, skip cached
This commit is contained in:
1 parent
6ff8ae64d5
commit
abacb830bc
8 files changed
+148
-35
No files matched your search
@@ -18,6 +18,8 @@ impl Default for General {
|
||||
pub struct Paths {
|
||||
#[serde(default = "default_instances_dir")]
|
||||
pub instances_dir: String,
|
||||
#[serde(default = "default_meta_dir")]
|
||||
pub meta_dir: String,
|
||||
pub java_path: Option<String>,
|
||||
}
|
||||
|
||||
@@ -25,10 +27,15 @@ fn default_instances_dir() -> String {
|
||||
"~/.local/share/mcl/instances".to_string()
|
||||
}
|
||||
|
||||
fn default_meta_dir() -> String {
|
||||
"~/.local/share/mcl/meta".to_string()
|
||||
}
|
||||
|
||||
impl Default for Paths {
|
||||
fn default() -> Self {
|
||||
Paths {
|
||||
instances_dir: default_instances_dir(),
|
||||
meta_dir: default_meta_dir(),
|
||||
java_path: None,
|
||||
}
|
||||
}
|
||||
@@ -51,6 +58,23 @@ impl Paths {
|
||||
}
|
||||
std::path::PathBuf::from(raw)
|
||||
}
|
||||
|
||||
pub fn resolve_meta_dir(&self) -> std::path::PathBuf {
|
||||
let raw = &self.meta_dir;
|
||||
if let Some(stripped) = raw.strip_prefix("~/") {
|
||||
return match dirs_next::home_dir() {
|
||||
Some(home) => home.join(stripped),
|
||||
None => std::path::PathBuf::from(raw),
|
||||
};
|
||||
}
|
||||
if raw == "~" {
|
||||
return match dirs_next::home_dir() {
|
||||
Some(home) => home,
|
||||
None => std::path::PathBuf::from(raw),
|
||||
};
|
||||
}
|
||||
std::path::PathBuf::from(raw)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
|
||||
+12
-5
@@ -30,6 +30,7 @@ pub trait ModLoaderInstaller: Send + Sync {
|
||||
game_version: &str,
|
||||
loader_version: &str,
|
||||
instance_dir: &Path,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError>;
|
||||
}
|
||||
|
||||
@@ -55,6 +56,7 @@ impl ModLoaderInstaller for VanillaInstaller {
|
||||
_game_version: &str,
|
||||
_loader_version: &str,
|
||||
_instance_dir: &Path,
|
||||
_meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
// Vanilla: no loader to install; Minecraft files downloaded by Mojang API separately
|
||||
Ok(())
|
||||
@@ -91,7 +93,8 @@ impl ModLoaderInstaller for FabricInstaller {
|
||||
client: &HttpClient,
|
||||
game_version: &str,
|
||||
loader_version: &str,
|
||||
instance_dir: &Path,
|
||||
_instance_dir: &Path,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let profile =
|
||||
match fabric::fetch_fabric_profile(client, game_version, loader_version).await {
|
||||
@@ -101,7 +104,7 @@ impl ModLoaderInstaller for FabricInstaller {
|
||||
}
|
||||
};
|
||||
|
||||
match fabric::download_fabric_libraries(client, &profile, instance_dir).await {
|
||||
match fabric::download_fabric_libraries(client, &profile, meta_dir).await {
|
||||
Ok(()) => {}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
@@ -139,6 +142,7 @@ impl ModLoaderInstaller for ForgeInstaller {
|
||||
game_version: &str,
|
||||
loader_version: &str,
|
||||
instance_dir: &Path,
|
||||
_meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let installer_jar = instance_dir.join(".minecraft").join("forge-installer.jar");
|
||||
|
||||
@@ -202,7 +206,8 @@ impl ModLoaderInstaller for QuiltInstaller {
|
||||
client: &HttpClient,
|
||||
game_version: &str,
|
||||
loader_version: &str,
|
||||
instance_dir: &Path,
|
||||
_instance_dir: &Path,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let profile =
|
||||
match quilt::fetch_quilt_profile(client, game_version, loader_version).await {
|
||||
@@ -212,7 +217,7 @@ impl ModLoaderInstaller for QuiltInstaller {
|
||||
}
|
||||
};
|
||||
|
||||
match quilt::download_quilt_libraries(client, &profile, instance_dir).await {
|
||||
match quilt::download_quilt_libraries(client, &profile, meta_dir).await {
|
||||
Ok(()) => {}
|
||||
Err(e) => {
|
||||
return Err(e);
|
||||
@@ -250,6 +255,7 @@ impl ModLoaderInstaller for NeoForgeInstaller {
|
||||
_game_version: &str,
|
||||
loader_version: &str,
|
||||
instance_dir: &Path,
|
||||
_meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let installer_jar = instance_dir
|
||||
.join(".minecraft")
|
||||
@@ -321,7 +327,8 @@ mod tests {
|
||||
let client = HttpClient::new();
|
||||
let installer = VanillaInstaller;
|
||||
let tmp = std::env::temp_dir().join("mcl_test_vanilla_install");
|
||||
match installer.install(&client, "1.20.1", "vanilla", &tmp).await {
|
||||
let meta = std::env::temp_dir().join("mcl_test_meta");
|
||||
match installer.install(&client, "1.20.1", "vanilla", &tmp, &meta).await {
|
||||
Ok(()) => {}
|
||||
Err(e) => assert!(false, "install failed: {}", e),
|
||||
}
|
||||
|
||||
+31
-6
@@ -23,13 +23,15 @@ pub enum InstanceError {
|
||||
|
||||
pub struct InstanceManager {
|
||||
pub instances_dir: PathBuf,
|
||||
pub meta_dir: PathBuf,
|
||||
client: crate::net::HttpClient,
|
||||
}
|
||||
|
||||
impl InstanceManager {
|
||||
pub fn new(instances_dir: impl Into<PathBuf>) -> Self {
|
||||
pub fn new(instances_dir: impl Into<PathBuf>, meta_dir: impl Into<PathBuf>) -> Self {
|
||||
InstanceManager {
|
||||
instances_dir: instances_dir.into(),
|
||||
meta_dir: meta_dir.into(),
|
||||
client: crate::net::HttpClient::new(),
|
||||
}
|
||||
}
|
||||
@@ -67,6 +69,21 @@ impl InstanceManager {
|
||||
}
|
||||
}
|
||||
|
||||
for meta_subdir in &[
|
||||
self.meta_dir.join("versions"),
|
||||
self.meta_dir.join("libraries"),
|
||||
self.meta_dir.join("assets").join("objects"),
|
||||
self.meta_dir.join("assets").join("indexes"),
|
||||
] {
|
||||
match std::fs::create_dir_all(meta_subdir) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to create meta directory: {}", e);
|
||||
return Err(InstanceError::Io(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let manifest = match crate::net::mojang::fetch_version_manifest(&self.client).await {
|
||||
Ok(m) => m,
|
||||
Err(e) => {
|
||||
@@ -90,21 +107,21 @@ impl InstanceManager {
|
||||
}
|
||||
};
|
||||
|
||||
match crate::net::mojang::download_client_jar(&self.client, &version_meta, &instance_dir).await {
|
||||
match crate::net::mojang::download_client_jar(&self.client, &version_meta, &self.meta_dir).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
return Err(InstanceError::Download(e));
|
||||
}
|
||||
}
|
||||
|
||||
match crate::net::mojang::download_libraries(&self.client, &version_meta, &instance_dir).await {
|
||||
match crate::net::mojang::download_libraries(&self.client, &version_meta, &self.meta_dir).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
return Err(InstanceError::Download(e));
|
||||
}
|
||||
}
|
||||
|
||||
match crate::net::mojang::download_assets(&self.client, &version_meta, &instance_dir).await {
|
||||
match crate::net::mojang::download_assets(&self.client, &version_meta, &self.meta_dir).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
return Err(InstanceError::Download(e));
|
||||
@@ -124,7 +141,13 @@ impl InstanceManager {
|
||||
}
|
||||
};
|
||||
match installer
|
||||
.install(&self.client, game_version, effective_loader_version, &instance_dir)
|
||||
.install(
|
||||
&self.client,
|
||||
game_version,
|
||||
effective_loader_version,
|
||||
&instance_dir,
|
||||
&self.meta_dir,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
@@ -291,8 +314,10 @@ mod tests {
|
||||
|
||||
fn test_manager() -> (InstanceManager, PathBuf) {
|
||||
let tmp = std::env::temp_dir().join(format!("mcl_test_{}", uuid_like()));
|
||||
let meta = std::env::temp_dir().join(format!("mcl_meta_test_{}", uuid_like()));
|
||||
std::fs::create_dir_all(&tmp).ok();
|
||||
(InstanceManager::new(tmp.clone()), tmp)
|
||||
std::fs::create_dir_all(&meta).ok();
|
||||
(InstanceManager::new(tmp.clone(), meta), tmp)
|
||||
}
|
||||
|
||||
fn uuid_like() -> String {
|
||||
|
||||
+2
-2
@@ -111,9 +111,9 @@ pub async fn fetch_fabric_profile(
|
||||
pub async fn download_fabric_libraries(
|
||||
client: &HttpClient,
|
||||
profile: &FabricProfile,
|
||||
instance_dir: &Path,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let libraries_dir = instance_dir.join(".minecraft").join("libraries");
|
||||
let libraries_dir = meta_dir.join("libraries");
|
||||
|
||||
for lib in &profile.libraries {
|
||||
let maven_path = match crate::net::maven_coord_to_path(&lib.name) {
|
||||
|
||||
+65
-17
@@ -7,7 +7,7 @@ use std::{
|
||||
},
|
||||
};
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::task::JoinSet;
|
||||
|
||||
use super::{download_file, HttpClient, NetError};
|
||||
@@ -105,12 +105,12 @@ pub struct JavaVersion {
|
||||
pub major_version: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct AssetIndexContent {
|
||||
pub objects: HashMap<String, AssetObject>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct AssetObject {
|
||||
pub hash: String,
|
||||
pub size: u64,
|
||||
@@ -130,16 +130,20 @@ pub async fn fetch_version_meta(
|
||||
pub async fn download_client_jar(
|
||||
client: &HttpClient,
|
||||
meta: &VersionMeta,
|
||||
instance_dir: &Path,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
set_action("Downloading Minecraft client...");
|
||||
|
||||
let jar_path = instance_dir
|
||||
.join(".minecraft")
|
||||
let jar_path = meta_dir
|
||||
.join("versions")
|
||||
.join(&meta.id)
|
||||
.join(format!("{}.jar", meta.id));
|
||||
|
||||
if jar_path.exists() {
|
||||
tracing::info!("Client JAR already cached: {}", meta.id);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
set_action(format!("Downloading Minecraft {}...", meta.id));
|
||||
|
||||
let result = download_file(client, &meta.downloads.client.url, &jar_path, |current, total| {
|
||||
set_progress(current, total);
|
||||
})
|
||||
@@ -152,7 +156,7 @@ pub async fn download_client_jar(
|
||||
pub async fn download_libraries(
|
||||
client: &HttpClient,
|
||||
meta: &VersionMeta,
|
||||
instance_dir: &Path,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
set_action("Downloading libraries...");
|
||||
|
||||
@@ -167,14 +171,21 @@ pub async fn download_libraries(
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let destination = instance_dir
|
||||
.join(".minecraft")
|
||||
.join("libraries")
|
||||
.join(&artifact.path);
|
||||
let destination = meta_dir.join("libraries").join(&artifact.path);
|
||||
|
||||
if destination.exists() {
|
||||
continue;
|
||||
}
|
||||
|
||||
downloads.push((artifact.url.clone(), destination, artifact.path.clone()));
|
||||
}
|
||||
|
||||
if downloads.is_empty() {
|
||||
tracing::info!("All libraries already cached");
|
||||
clear();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let result = run_parallel_downloads(client, downloads, false).await;
|
||||
clear();
|
||||
result
|
||||
@@ -183,7 +194,7 @@ pub async fn download_libraries(
|
||||
pub async fn download_assets(
|
||||
client: &HttpClient,
|
||||
meta: &VersionMeta,
|
||||
instance_dir: &Path,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
set_action("Downloading assets...");
|
||||
|
||||
@@ -195,10 +206,38 @@ pub async fn download_assets(
|
||||
}
|
||||
};
|
||||
|
||||
let index_path = meta_dir
|
||||
.join("assets")
|
||||
.join("indexes")
|
||||
.join(format!("{}.json", meta.asset_index.id));
|
||||
if !index_path.exists() {
|
||||
match serde_json::to_string(&asset_index) {
|
||||
Ok(json) => {
|
||||
if let Some(parent) = index_path.parent() {
|
||||
match tokio::fs::create_dir_all(parent).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to create asset index dir: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
match tokio::fs::write(&index_path, json).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to write asset index: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to serialize asset index: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut downloads = Vec::new();
|
||||
for object in asset_index.objects.values() {
|
||||
if object.hash.len() < 2 {
|
||||
let message = format!("Invalid asset hash for version {}: {}", meta.id, object.hash);
|
||||
let message = format!("Invalid asset hash: {}", object.hash);
|
||||
tracing::error!("{}", message);
|
||||
clear();
|
||||
return Err(NetError::Parse(message));
|
||||
@@ -206,16 +245,25 @@ pub async fn download_assets(
|
||||
|
||||
let prefix = &object.hash[..2];
|
||||
let url = format!("{}/{}/{}", ASSETS_BASE_URL, prefix, object.hash);
|
||||
let destination = instance_dir
|
||||
.join(".minecraft")
|
||||
let destination = meta_dir
|
||||
.join("assets")
|
||||
.join("objects")
|
||||
.join(prefix)
|
||||
.join(&object.hash);
|
||||
|
||||
if destination.exists() {
|
||||
continue;
|
||||
}
|
||||
|
||||
downloads.push((url, destination, object.hash.clone()));
|
||||
}
|
||||
|
||||
if downloads.is_empty() {
|
||||
tracing::info!("All assets already cached");
|
||||
clear();
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let result = run_parallel_downloads(client, downloads, true).await;
|
||||
clear();
|
||||
result
|
||||
|
||||
+2
-2
@@ -108,9 +108,9 @@ pub async fn fetch_quilt_profile(
|
||||
pub async fn download_quilt_libraries(
|
||||
client: &HttpClient,
|
||||
profile: &QuiltProfile,
|
||||
instance_dir: &Path,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let libraries_dir = instance_dir.join(".minecraft").join("libraries");
|
||||
let libraries_dir = meta_dir.join("libraries");
|
||||
|
||||
for lib in &profile.libraries {
|
||||
let maven_path = match crate::net::maven_coord_to_path(&lib.name) {
|
||||
|
||||
+11
-3
@@ -48,6 +48,7 @@ pub enum FocusedArea {
|
||||
impl Default for App {
|
||||
fn default() -> Self {
|
||||
let instances_dir = crate::config::SETTINGS.paths.resolve_instances_dir();
|
||||
let meta_dir = crate::config::SETTINGS.paths.resolve_meta_dir();
|
||||
|
||||
match std::fs::create_dir_all(&instances_dir) {
|
||||
Ok(_) => {}
|
||||
@@ -56,7 +57,14 @@ impl Default for App {
|
||||
}
|
||||
}
|
||||
|
||||
let manager = InstanceManager::new(instances_dir);
|
||||
match std::fs::create_dir_all(&meta_dir) {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to create meta dir: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
let manager = InstanceManager::new(instances_dir, meta_dir);
|
||||
let instances = manager.load_all();
|
||||
let profiles_state = profiles::State::with_instances(instances);
|
||||
|
||||
@@ -254,13 +262,14 @@ impl App {
|
||||
|
||||
fn spawn_create(&self, params: new_instance::WizardParams) {
|
||||
let instances_dir = self.instance_manager.instances_dir.clone();
|
||||
let meta_dir = crate::config::SETTINGS.paths.resolve_meta_dir();
|
||||
let pending_instances = PENDING_INSTANCES.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
progress::set_action(format!("Creating instance '{}'...", params.name));
|
||||
progress::set_sub_action(format!("{} {}", params.game_version, params.loader));
|
||||
|
||||
let manager = InstanceManager::new(instances_dir);
|
||||
let manager = InstanceManager::new(instances_dir, meta_dir);
|
||||
match manager
|
||||
.create(
|
||||
¶ms.name,
|
||||
@@ -376,4 +385,3 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user