refactor: share loader library downloads and validate settings
This commit is contained in:
1 parent
783fb76f06
commit
7e7e5c0c31
7 files changed
+126
-89
No files matched your search
+2
-1
@@ -3,6 +3,7 @@
|
||||
|
||||
use clap::ArgMatches;
|
||||
|
||||
use super::utils::required_arg;
|
||||
use crate::instance::InstanceManager;
|
||||
use crate::instance::import::{ImportInput, parse_import_input};
|
||||
use crate::net::modrinth;
|
||||
@@ -10,7 +11,7 @@ use crate::net::modrinth;
|
||||
type CliResult = Result<(), Box<dyn std::error::Error>>;
|
||||
|
||||
pub async fn handle_import(matches: &ArgMatches) -> CliResult {
|
||||
let input = matches.get_one::<String>("source").unwrap();
|
||||
let input = required_arg(matches, "source")?;
|
||||
let override_name = matches.get_one::<String>("name");
|
||||
let override_version = matches.get_one::<String>("version");
|
||||
|
||||
|
||||
@@ -353,9 +353,9 @@ fn backup_user_data(
|
||||
},
|
||||
)?;
|
||||
if config_file.exists() {
|
||||
let destination = partial.join("config").join("config.toml");
|
||||
fs::create_dir_all(destination.parent().unwrap())?;
|
||||
let bytes = fs::copy(config_file, destination)?;
|
||||
let config_backup = partial.join("config");
|
||||
fs::create_dir_all(&config_backup)?;
|
||||
let bytes = fs::copy(config_file, config_backup.join("config.toml"))?;
|
||||
copied = copied.saturating_add(bytes);
|
||||
report(copied, total, config_file);
|
||||
}
|
||||
|
||||
+10
-32
@@ -5,9 +5,8 @@ use std::path::Path;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::feedback::progress::set_sub_action;
|
||||
use crate::instance::loader::GameVersion;
|
||||
use crate::net::{HttpClient, NetError, download_file};
|
||||
use crate::net::{HttpClient, NetError, download_loader_libraries};
|
||||
|
||||
const FABRIC_META_BASE: &str = "https://meta.fabricmc.net/v2";
|
||||
|
||||
@@ -150,40 +149,19 @@ pub async fn download_fabric_libraries(
|
||||
profile: &FabricProfile,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let libraries_dir = crate::storage::MetadataPaths::new(meta_dir).libraries();
|
||||
tracing::debug!(
|
||||
"Resolving {} Fabric libraries into {}",
|
||||
profile.libraries.len(),
|
||||
libraries_dir.display()
|
||||
crate::storage::MetadataPaths::new(meta_dir)
|
||||
.libraries()
|
||||
.display()
|
||||
);
|
||||
|
||||
for lib in &profile.libraries {
|
||||
let maven_path = match crate::instance::loader::maven::maven_coord_to_path(&lib.name) {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
return Err(NetError::Parse(format!(
|
||||
"Invalid Maven coordinate: {}",
|
||||
lib.name
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
let dest = libraries_dir.join(&maven_path);
|
||||
|
||||
if dest.exists() {
|
||||
tracing::debug!("Fabric library already exists: {}", lib.name);
|
||||
continue;
|
||||
}
|
||||
|
||||
let base_url = lib.url.trim_end_matches('/');
|
||||
let download_url = format!("{}/{}", base_url, maven_path);
|
||||
|
||||
set_sub_action(&lib.name);
|
||||
tracing::info!("Downloading Fabric library: {}", lib.name);
|
||||
tracing::trace!("Fabric library destination: {}", dest.display());
|
||||
|
||||
download_file(client, &download_url, &dest, |_, _| {}).await?;
|
||||
}
|
||||
let libraries: Vec<_> = profile
|
||||
.libraries
|
||||
.iter()
|
||||
.map(|lib| (lib.name.as_str(), lib.url.as_str()))
|
||||
.collect();
|
||||
download_loader_libraries(client, libraries, meta_dir, "Fabric").await?;
|
||||
|
||||
tracing::debug!("Fabric library resolution complete for {}", profile.id);
|
||||
Ok(())
|
||||
|
||||
@@ -257,6 +257,31 @@ pub async fn download_file(
|
||||
result
|
||||
}
|
||||
|
||||
pub(crate) async fn download_loader_libraries<'a>(
|
||||
client: &HttpClient,
|
||||
libraries: impl IntoIterator<Item = (&'a str, &'a str)>,
|
||||
meta_dir: &Path,
|
||||
loader: &str,
|
||||
) -> Result<(), NetError> {
|
||||
let libraries_dir = crate::storage::MetadataPaths::new(meta_dir).libraries();
|
||||
for (name, url) in libraries {
|
||||
let maven_path = crate::instance::loader::maven::maven_coord_to_path(name)
|
||||
.ok_or_else(|| NetError::Parse(format!("Invalid Maven coordinate: {name}")))?;
|
||||
let dest = libraries_dir.join(&maven_path);
|
||||
if dest.exists() {
|
||||
tracing::debug!("{loader} library already exists: {name}");
|
||||
continue;
|
||||
}
|
||||
|
||||
let download_url = format!("{}/{}", url.trim_end_matches('/'), maven_path);
|
||||
crate::feedback::progress::set_sub_action(name);
|
||||
tracing::info!("Downloading {loader} library: {name}");
|
||||
tracing::trace!("{loader} library destination: {}", dest.display());
|
||||
download_file(client, &download_url, &dest, |_, _| {}).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn download_file_once(
|
||||
client: &HttpClient,
|
||||
url: &str,
|
||||
|
||||
+10
-32
@@ -5,9 +5,8 @@ use std::path::Path;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::feedback::progress::set_sub_action;
|
||||
use crate::instance::loader::GameVersion;
|
||||
use crate::net::{HttpClient, NetError, download_file};
|
||||
use crate::net::{HttpClient, NetError, download_loader_libraries};
|
||||
|
||||
const QUILT_META_BASE: &str = "https://meta.quiltmc.org/v3";
|
||||
|
||||
@@ -147,40 +146,19 @@ pub async fn download_quilt_libraries(
|
||||
profile: &QuiltProfile,
|
||||
meta_dir: &Path,
|
||||
) -> Result<(), NetError> {
|
||||
let libraries_dir = crate::storage::MetadataPaths::new(meta_dir).libraries();
|
||||
tracing::debug!(
|
||||
"Resolving {} Quilt libraries into {}",
|
||||
profile.libraries.len(),
|
||||
libraries_dir.display()
|
||||
crate::storage::MetadataPaths::new(meta_dir)
|
||||
.libraries()
|
||||
.display()
|
||||
);
|
||||
|
||||
for lib in &profile.libraries {
|
||||
let maven_path = match crate::instance::loader::maven::maven_coord_to_path(&lib.name) {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
return Err(NetError::Parse(format!(
|
||||
"Invalid Maven coordinate: {}",
|
||||
lib.name
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
let dest = libraries_dir.join(&maven_path);
|
||||
|
||||
if dest.exists() {
|
||||
tracing::debug!("Quilt library already exists: {}", lib.name);
|
||||
continue;
|
||||
}
|
||||
|
||||
let base_url = lib.url.trim_end_matches('/');
|
||||
let download_url = format!("{}/{}", base_url, maven_path);
|
||||
|
||||
set_sub_action(&lib.name);
|
||||
tracing::info!("Downloading Quilt library: {}", lib.name);
|
||||
tracing::trace!("Quilt library destination: {}", dest.display());
|
||||
|
||||
download_file(client, &download_url, &dest, |_, _| {}).await?;
|
||||
}
|
||||
let libraries: Vec<_> = profile
|
||||
.libraries
|
||||
.iter()
|
||||
.map(|lib| (lib.name.as_str(), lib.url.as_str()))
|
||||
.collect();
|
||||
download_loader_libraries(client, libraries, meta_dir, "Quilt").await?;
|
||||
|
||||
tracing::debug!("Quilt library resolution complete for {}", profile.id);
|
||||
Ok(())
|
||||
|
||||
@@ -193,26 +193,26 @@ impl State {
|
||||
state.editing = Some(settings_text_area(editor.lines().to_vec()));
|
||||
};
|
||||
match self.selected {
|
||||
3 | 4 if normalize_memory_value(value).is_none() => invalid(
|
||||
self,
|
||||
"Use a positive memory value ending in K, M, or G.".to_owned(),
|
||||
),
|
||||
3 => {
|
||||
let value = normalize_memory_value(value).unwrap();
|
||||
self.config.defaults.memory_min = value.clone();
|
||||
if memory_kib(&value) > memory_kib(&self.config.defaults.memory_max) {
|
||||
self.config.defaults.memory_max = value;
|
||||
3 | 4 => match normalize_memory_value(value) {
|
||||
Some(value) => {
|
||||
if self.selected == 3 {
|
||||
self.config.defaults.memory_min = value.clone();
|
||||
if memory_kib(&value) > memory_kib(&self.config.defaults.memory_max) {
|
||||
self.config.defaults.memory_max = value;
|
||||
}
|
||||
} else {
|
||||
self.config.defaults.memory_max = value.clone();
|
||||
if memory_kib(&value) < memory_kib(&self.config.defaults.memory_min) {
|
||||
self.config.defaults.memory_min = value;
|
||||
}
|
||||
}
|
||||
self.save_pending = true;
|
||||
}
|
||||
self.save_pending = true;
|
||||
}
|
||||
4 => {
|
||||
let value = normalize_memory_value(value).unwrap();
|
||||
self.config.defaults.memory_max = value.clone();
|
||||
if memory_kib(&value) < memory_kib(&self.config.defaults.memory_min) {
|
||||
self.config.defaults.memory_min = value;
|
||||
}
|
||||
self.save_pending = true;
|
||||
}
|
||||
None => invalid(
|
||||
self,
|
||||
"Use a positive memory value ending in K, M, or G.".to_owned(),
|
||||
),
|
||||
},
|
||||
5 => {
|
||||
self.config.paths.java_path = (!value.is_empty()).then(|| value.to_owned());
|
||||
self.save_pending = true;
|
||||
|
||||
Reference in new issue
Block a user