fix all the red wiggly wiggles

This commit is contained in:
leocth
2022-02-20 22:47:44 +08:00
parent 14e8e92f46
commit 28d1267afa
8 changed files with 56 additions and 59 deletions

1
Cargo.lock generated
View File

@@ -1191,6 +1191,7 @@ dependencies = [
"futures",
"json5",
"log",
"once_cell",
"path-clean",
"regex",
"reqwest",

View File

@@ -34,6 +34,7 @@ sys-info = "0.9.0"
log = "0.4.14"
const_format = "0.2.22"
once_cell = "1.9.0"
[dev-dependencies]
argh = "0.1.6"

View File

@@ -1,4 +1,6 @@
use crate::data::DataError;
use std::path::Path;
use crate::{data::DataError, LAUNCHER_WORK_DIR};
use once_cell::sync;
use serde::{Deserialize, Serialize};
use tokio::sync::{RwLock, RwLockReadGuard};
@@ -17,13 +19,11 @@ pub struct Metadata {
impl Metadata {
pub async fn init() -> Result<(), DataError> {
let meta_path = crate::LAUNCHER_WORK_DIR.join(META_FILE);
let meta_path = Path::new(LAUNCHER_WORK_DIR).join(META_FILE);
if meta_path.exists() {
let meta_data = std::fs::read_to_string(meta_path)
.map(|x| serde_json::from_str::<Metadata>(&*x).ok())
.ok()
.flatten();
let meta_data = std::fs::read_to_string(meta_path).ok()
.and_then(|x| serde_json::from_str::<Metadata>(&x).ok());
if let Some(metadata) = meta_data {
METADATA.get_or_init(|| RwLock::new(metadata));
@@ -36,8 +36,8 @@ impl Metadata {
let new = Self::fetch().await?;
std::fs::write(
crate::LAUNCHER_WORK_DIR.join(META_FILE),
&*serde_json::to_string(&new)?,
Path::new(LAUNCHER_WORK_DIR).join(META_FILE),
&serde_json::to_string(&new)?,
)?;
if let Some(metadata) = METADATA.get() {

View File

@@ -1,4 +1,6 @@
use crate::data::DataError;
use std::path::Path;
use crate::{data::DataError, LAUNCHER_WORK_DIR};
use once_cell::sync;
use serde::{Deserialize, Serialize};
use tokio::sync::{RwLock, RwLockReadGuard};
@@ -32,7 +34,7 @@ impl Default for Settings {
impl Settings {
pub async fn init() -> Result<(), DataError> {
let settings_path = crate::LAUNCHER_WORK_DIR.join(SETTINGS_FILE);
let settings_path = Path::new(LAUNCHER_WORK_DIR).join(SETTINGS_FILE);
if settings_path.exists() {
let settings_data = std::fs::read_to_string(settings_path)
@@ -49,7 +51,7 @@ impl Settings {
let new = Self::default();
std::fs::write(
crate::LAUNCHER_WORK_DIR.join(SETTINGS_FILE),
Path::new(LAUNCHER_WORK_DIR).join(SETTINGS_FILE),
&*serde_json::to_string(&new)?,
)?;
@@ -60,8 +62,8 @@ impl Settings {
}
pub async fn load() -> Result<(), DataError> {
let new = serde_json::from_str::<Settings>(&*std::fs::read_to_string(
crate::LAUNCHER_WORK_DIR.join(SETTINGS_FILE),
let new = serde_json::from_str::<Settings>(&std::fs::read_to_string(
Path::new(LAUNCHER_WORK_DIR).join(SETTINGS_FILE),
)?)?;
let write = &mut *SETTINGS
@@ -79,8 +81,8 @@ impl Settings {
let settings = Self::get().await?;
std::fs::write(
crate::LAUNCHER_WORK_DIR.join(SETTINGS_FILE),
&*serde_json::to_string(&*settings)?,
Path::new(LAUNCHER_WORK_DIR).join(SETTINGS_FILE),
&serde_json::to_string(&*settings)?,
)?;
Ok(())

View File

@@ -21,21 +21,19 @@ pub fn get_class_paths(
libraries: &[Library],
client_path: &Path,
) -> Result<String, LauncherError> {
let mut class_paths = Vec::new();
for library in libraries {
let mut class_paths = libraries.iter().filter_map(|library| {
if let Some(rules) = &library.rules {
if !super::rules::parse_rules(rules.as_slice()) {
continue;
return None;
}
}
if !library.include_in_classpath {
continue;
return None;
}
class_paths.push(get_lib_path(libraries_path, &library.name)?);
}
Some(get_lib_path(libraries_path, &library.name))
}).collect::<Result<Vec<_>, _>>()?;
class_paths.push(
crate::util::absolute_path(&client_path)
@@ -56,19 +54,14 @@ pub fn get_class_paths_jar<T: AsRef<str>>(
libraries_path: &Path,
libraries: &[T],
) -> Result<String, LauncherError> {
let mut class_paths = Vec::new();
let class_paths = libraries.iter().map(|library| {
get_lib_path(libraries_path, library.as_ref())
}).collect::<Result<Vec<_>, _>>()?;
for library in libraries {
class_paths.push(get_lib_path(libraries_path, library)?)
}
Ok(class_paths.join(match super::download::get_os() {
Os::Osx | Os::Linux | Os::Unknown => ":",
Os::Windows => ";",
}))
Ok(class_paths.join(get_cp_separator()))
}
pub fn get_lib_path<T: AsRef<str>>(libraries_path: &Path, lib: T) -> Result<String, LauncherError> {
pub fn get_lib_path(libraries_path: &Path, lib: &str) -> Result<String, LauncherError> {
let mut path = libraries_path.to_path_buf();
path.push(get_path_from_artifact(lib.as_ref())?);
@@ -105,7 +98,7 @@ pub fn get_jvm_arguments(
if let Some(args) = arguments {
parse_arguments(args, &mut parsed_arguments, |arg| {
parse_jvm_argument(
arg,
arg.to_string(),
natives_path,
libraries_path,
class_paths,
@@ -138,18 +131,17 @@ pub fn get_jvm_arguments(
}
fn parse_jvm_argument(
argument: &str,
mut argument: String,
natives_path: &Path,
libraries_path: &Path,
class_paths: &str,
version_name: &str,
) -> Result<String, LauncherError> {
let mut argument = argument.to_string();
argument.retain(|c| !c.is_whitespace());
Ok(argument
.replace(
"${natives_directory}",
&*crate::util::absolute_path(natives_path)
&crate::util::absolute_path(natives_path)
.map_err(|_| {
LauncherError::InvalidInput(format!(
"Specified natives path {} does not exist",
@@ -196,8 +188,8 @@ pub fn get_minecraft_arguments(
parse_arguments(arguments, &mut parsed_arguments, |arg| {
parse_minecraft_argument(
arg,
&*credentials.access_token,
&*credentials.username,
&credentials.access_token,
&credentials.username,
&credentials.id,
version,
asset_index_name,
@@ -212,8 +204,8 @@ pub fn get_minecraft_arguments(
} else if let Some(legacy_arguments) = legacy_arguments {
Ok(parse_minecraft_argument(
legacy_arguments,
&*credentials.access_token,
&*credentials.username,
&credentials.access_token,
&credentials.username,
&credentials.id,
version,
asset_index_name,

View File

@@ -53,7 +53,7 @@ fn get_loader_version(loader: ModLoader, version: &str) -> ModpackResult<String>
}?;
let manifest = futures::executor::block_on(daedalus::modded::fetch_manifest(&source))?;
Ok(manifest
let version = manifest
.game_versions
.iter()
.find(|&it| it.id == version)
@@ -63,8 +63,8 @@ fn get_loader_version(loader: ModLoader, version: &str) -> ModpackResult<String>
ModpackError::VersionError(format!(
"No versions of modloader {loader:?} exist for Minecraft {version}",
))
})?
.id)
})?;
Ok(version.id.clone())
}
impl TryFrom<pack::Modpack> for Manifest {
@@ -79,20 +79,20 @@ impl TryFrom<pack::Modpack> for Manifest {
files,
} = pack;
let game = match game {
let game_name = match &game {
ModpackGame::Minecraft(..) => "minecraft".into(),
};
let files: Vec<_> = pack.files.into_iter().map(ManifestFile::from).collect();
let files: Vec<_> = files.into_iter().map(ManifestFile::from).collect();
Ok(Manifest {
format_version: DEFAULT_FORMAT_VERSION,
game,
game: game_name,
version_id: version,
name,
summary,
files,
dependencies: ManifestDeps::try_from(pack.game)?,
dependencies: ManifestDeps::try_from(game)?,
})
}
}
@@ -240,12 +240,12 @@ impl TryFrom<pack::ModpackGame> for ManifestDeps {
Ok(match game {
Minecraft(minecraft, ModLoader::Vanilla) => Self::MinecraftVanilla { minecraft },
Minecraft(minecraft, ModLoader::Fabric) => Self::MinecraftFabric {
minecraft,
fabric_loader: get_loader_version(ModLoader::Fabric, &minecraft)?,
minecraft,
},
Minecraft(minecraft, ModLoader::Forge) => Self::MinecraftForge {
minecraft,
forge: get_loader_version(ModLoader::Fabric, &minecraft)?,
minecraft,
},
})
}

View File

@@ -33,6 +33,16 @@ pub struct Modpack {
}
impl Modpack {
pub fn new(game: ModpackGame, version: &str, name: &str, summary: Option<&str>) -> Self {
Self {
game,
version: String::from(version),
name: String::from(name),
summary: summary.map(String::from),
files: HashSet::new(),
}
}
/// Download a modpack's files for a given side to a given destination
/// Assumes the destination exists and is a directory
pub async fn download_files(&self, dest: &Path, side: ModpackSide) -> ModpackResult<()> {
@@ -49,16 +59,6 @@ impl Modpack {
Ok(())
}
pub fn new(game: ModpackGame, version: &str, name: &str, summary: Option<&str>) -> Self {
Self {
game,
version: String::from(version),
name: String::from(name),
summary: summary.map(String::from),
files: HashSet::new(),
}
}
pub async fn add_project(
&mut self,
project: &str,

View File

@@ -3,6 +3,7 @@ use std::{env, io};
use path_clean::PathClean;
// https://stackoverflow.com/a/54817755
pub fn absolute_path(path: impl AsRef<Path>) -> io::Result<PathBuf> {
let path = path.as_ref();