From e607dfe6b64fb9d7f85ac3749ecf6fe193544549 Mon Sep 17 00:00:00 2001 From: LynithDev <61880709+LynithDev@users.noreply.github.com> Date: Thu, 28 Mar 2024 20:32:16 +0100 Subject: [PATCH] feat: Fixes --- Cargo.toml | 5 +- onelauncher/Cargo.toml | 12 -- onelauncher/src/error.rs | 8 - onelauncher/src/game/minecraft.rs | 238 ++++++++++++++++-------------- src-tauri/Cargo.toml | 2 +- src-tauri/src/api/auth.rs | 2 +- src-tauri/src/api/game.rs | 2 +- src-tauri/src/api/mod.rs | 2 +- src-tauri/src/error.rs | 2 +- src-tauri/src/main.rs | 8 +- 10 files changed, 139 insertions(+), 142 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 14f3fb2b..bcab4e32 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,7 +30,10 @@ tracing-subscriber = { version = "0.3", features = [ tracing-error = { version = "0.2" } tracing-appender = { version = "0.2" } -uuid = { version = "1.8", features = [ "serde" ] } +uuid = { version = "1.8", features = [ + "serde", + "v4" +] } tokio = { version = "1", features = [ "full" ] } serde = { version = "1", features = [ "derive" ] } chrono = { version = "0.4", features = [ "serde" ] } diff --git a/onelauncher/Cargo.toml b/onelauncher/Cargo.toml index b522b5c5..1527adf7 100644 --- a/onelauncher/Cargo.toml +++ b/onelauncher/Cargo.toml @@ -10,14 +10,7 @@ readme = { workspace = true } homepage = { workspace = true } authors = { workspace = true } -[features] -tauri = [ "dep:tauri" ] -cli = [ "dep:indicatif" ] - [dependencies] -tauri = { workspace = true, optional = true } -indicatif = { workspace = true, optional = true } - tracing = { workspace = true } tracing-subscriber = { workspace = true } tracing-error = { workspace = true } @@ -39,8 +32,3 @@ dirs = { workspace = true } open = { workspace = true } sha1 = { workspace = true } discord-rich-presence = { workspace = true } -notify = { workspace = true } -notify-debouncer-mini = { workspace = true } - -[target.'cfg(windows)'.dependencies] -winreg = { workspace = true } diff --git a/onelauncher/src/error.rs b/onelauncher/src/error.rs index 829d6247..bf77a290 100644 --- a/onelauncher/src/error.rs +++ b/onelauncher/src/error.rs @@ -15,10 +15,6 @@ pub enum ErrorKind { #[error("tokio join handle error: {0}")] JoinError(#[from] tokio::task::JoinError), - /// Wrapper around [`notify::Error`] to handle file watching errors. - #[error("failed to watch file: {0}")] - NotifyError(#[from] notify::Error), - /// Wrapper around [`flate2::CompressError`] to handle flate compression errors. #[error("failed to compress a file with flate: {0}")] FlateError(#[from] flate2::CompressError), @@ -70,10 +66,6 @@ pub enum ErrorKind { /// Wrapper around [`crate::game::client::ClientManagerError`] to handle directory errors. #[error("failed to manage clients: {0}")] ClientManagerError(#[from] crate::game::client::ClientManagerError), - - #[cfg(feature = "tauri")] - #[error("failed to launch tauri: {0}")] - TauriError(#[from] tauri::Error), } #[derive(Debug)] diff --git a/onelauncher/src/game/minecraft.rs b/onelauncher/src/game/minecraft.rs index 217059f0..079f23d6 100644 --- a/onelauncher/src/game/minecraft.rs +++ b/onelauncher/src/game/minecraft.rs @@ -1,209 +1,223 @@ -use serde::{Deserialize, Serialize}; -use serde_json::Value; +use core::fmt; +use std::{collections::HashMap, marker::PhantomData}; + +use serde::{de::{self, SeqAccess, Visitor}, Deserialize, Deserializer, Serialize}; #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct MinecraftManifest { - pub asset_index: AssetIndex, - pub downloads: Downloads, - #[serde(rename = "id")] - pub version: String, - pub java_version: JavaVersion, - pub libraries: Vec, - pub logging: Logging, - pub main_class: String, - pub arguments: Arguments, - pub release_time: String, - #[serde(rename = "type")] - pub release_type: ReleaseType, + pub asset_index: AssetIndex, + pub downloads: Downloads, + pub java_version: JavaVersion, + pub libraries: Vec, + pub logging: Logging, + pub main_class: String, + pub release_time: String, + + #[serde(rename = "id")] + pub version: String, + + #[serde(rename = "type")] + pub release_type: ReleaseType, + + #[serde(skip_serializing_if = "Option::is_none")] + pub minecraft_arguments: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + pub arguments: Option, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] -#[serde(untagged)] -pub enum Arguments { - // TODO: https://serde.rs/string-or-struct.html - MinecraftArguments(String), - Arguments { - game: Vec, - jvm: Vec, - }, -} - -impl Default for Arguments { - fn default() -> Self { - Arguments::MinecraftArguments(String::new()) - } +pub struct ModernArguments { + game: Vec, + jvm: Vec, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] #[serde(untagged)] pub enum ModernArgumentsItem { - Simple(String), - Rule { - rules: Vec, - value: ArgumentRuleValue, - }, + Simple(String), + Rule(ArgumentRule), } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] -#[serde(untagged)] -pub enum ArgumentRuleValue { - String(String), - List(Vec), +pub struct ArgumentRule { + #[serde(skip_serializing_if = "Vec::is_empty")] + rules: Vec, + + #[serde(skip_serializing_if = "Vec::is_empty")] + #[serde(deserialize_with = "string_or_seq")] + value: Vec, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum ReleaseType { - Snapshot, - Release, - OldBeta, - OldAlpha, + Snapshot, + Release, + OldBeta, + OldAlpha, } impl Default for ReleaseType { - fn default() -> Self { - ReleaseType::Release - } + fn default() -> Self { ReleaseType::Release } } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct AssetIndex { - pub id: String, - pub sha1: String, - pub url: String, + pub id: String, + pub sha1: String, + pub url: String, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Downloads { - pub client: Client, + pub client: Client, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Client { - pub sha1: String, - pub url: String, + pub sha1: String, + pub url: String, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct JavaVersion { - pub major_version: u8, + pub major_version: u8, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Library { - pub downloads: Downloads2, - pub name: String, - #[serde(default)] - pub rules: Vec, - #[serde(skip_serializing_if = "Option::is_none")] - pub natives: Option, + pub downloads: LibraryDownload, + pub name: String, + #[serde(skip_serializing_if = "Vec::is_empty", default)] + pub rules: Vec, + #[serde(skip_serializing_if = "Option::is_none")] + pub natives: Option>, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] -pub struct Downloads2 { - #[serde(skip_serializing_if = "Option::is_none")] - pub artifact: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub classifiers: Option, +pub struct LibraryDownload { + #[serde(skip_serializing_if = "Option::is_none")] + pub artifact: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub classifiers: Option, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Artifact { - pub path: String, - pub sha1: String, - pub url: String, + pub path: String, + pub sha1: String, + pub url: String, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Classifiers { - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(rename = "natives-osx")] - pub natives_osx: Option, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(rename = "natives-osx")] + pub natives_osx: Option, + + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(rename = "natives-linux")] + pub natives_linux: Option, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(rename = "natives-linux")] - pub natives_linux: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(rename = "natives-windows")] - pub natives_windows: Option, -} - -#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct Native { - pub path: String, - pub sha1: String, - pub url: String, + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(rename = "natives-windows")] + pub natives_windows: Option, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Rule { - pub action: RuleAction, + pub action: RuleAction, + + #[serde(skip_serializing_if = "Option::is_none")] + pub os: Option, - #[serde(skip_serializing_if = "Option::is_none")] - pub os: Option, - - #[serde(skip_serializing_if = "Option::is_none")] - pub features: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub features: Option>, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -#[serde(untagged)] pub enum RuleAction { - Allow, - Disallow, + #[serde(rename = "allow")] + Allow, + #[serde(rename = "disallow")] + Disallow, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Os { - pub name: Option, - pub arch: Option, - pub version: Option, -} - -#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct Natives { - pub windows: Option, - pub linux: Option, - pub osx: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub name: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub arch: Option, + #[serde(skip_serializing_if = "Option::is_none")] + pub version: Option, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Logging { - pub client: Client2, + pub client: Client2, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct Client2 { - pub argument: String, - pub file: File, - #[serde(rename = "type")] - pub type_field: String, + pub argument: String, + pub file: File, + #[serde(rename = "type")] + pub type_field: String, } #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct File { - pub id: String, - pub sha1: String, - pub url: String, + pub id: String, + pub sha1: String, + pub url: String, } + +fn string_or_seq<'de, D>(deserializer: D) -> Result, D::Error> +where + D: Deserializer<'de>, +{ + struct StringOrVec(PhantomData>); + + impl<'de> Visitor<'de> for StringOrVec { + type Value = Vec; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("string or list of strings") + } + + fn visit_str(self, value: &str) -> Result + where + E: de::Error, + { + Ok(vec![value.into()]) + } + + fn visit_seq(self, seq: S) -> Result + where + S: SeqAccess<'de>, + { + Deserialize::deserialize(de::value::SeqAccessDeserializer::new(seq)) + } + } + + deserializer.deserialize_any(StringOrVec(PhantomData)) +} \ No newline at end of file diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 2bb64d9d..732f0018 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -14,7 +14,7 @@ authors = { workspace = true } tauri-build = { workspace = true } [dependencies] -onelauncher = { path = "../onelauncher", features = [ "tauri" ] } +onelauncher = { path = "../onelauncher" } tauri = { workspace = true } tauri-plugin-shell = { workspace = true } diff --git a/src-tauri/src/api/auth.rs b/src-tauri/src/api/auth.rs index 67e81ca7..55814eb7 100644 --- a/src-tauri/src/api/auth.rs +++ b/src-tauri/src/api/auth.rs @@ -1,4 +1,4 @@ -use launcher_core::auth::{ +use onelauncher::auth::{ microsoft_auth::MicrosoftAuthenticationMethod, Account, AuthenticationMethod, }; use tauri::Manager; diff --git a/src-tauri/src/api/game.rs b/src-tauri/src/api/game.rs index 48820062..41eebbaf 100644 --- a/src-tauri/src/api/game.rs +++ b/src-tauri/src/api/game.rs @@ -1,4 +1,4 @@ -use launcher_core::game::client::{ClientType, Instance, Manifest}; +use onelauncher::game::client::{ClientType, Instance, Manifest}; use tauri::State; use tokio::sync::Mutex; use uuid::Uuid; diff --git a/src-tauri/src/api/mod.rs b/src-tauri/src/api/mod.rs index de534859..ecbe4aeb 100644 --- a/src-tauri/src/api/mod.rs +++ b/src-tauri/src/api/mod.rs @@ -1,4 +1,4 @@ -use launcher_core::game::client::ClientManager; +use onelauncher::game::client::ClientManager; use tauri::{plugin::TauriPlugin, Manager, State}; use tokio::sync::Mutex; diff --git a/src-tauri/src/error.rs b/src-tauri/src/error.rs index be05d925..0964d4fd 100644 --- a/src-tauri/src/error.rs +++ b/src-tauri/src/error.rs @@ -1,6 +1,6 @@ use tracing_error::ExtractSpanTrace; -pub fn display_tracing_error(err: &launcher_core::Error) { +pub fn display_tracing_error(err: &onelauncher::Error) { match get_span_trace(err) { Some(span_trace) => { tracing::error!(error = %err, span_trace = %span_trace); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 82f9f15b..0218fda6 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,8 +3,8 @@ use std::error::Error; -use polyfrost_launcher::api; -use tauri::{menu::Menu, App}; +use onelauncher_gui::api; +use tauri::{menu::Menu, App, Manager}; #[derive(Clone, serde::Serialize)] struct Payload { @@ -14,7 +14,7 @@ struct Payload { #[tokio::main] async fn main() { - let _log_guard = launcher_core::start_logger(); + let _log_guard = onelauncher::start_logger(); tracing::info!("initialized tracing subscriber. loading OneLauncher"); tauri::Builder::default() @@ -22,7 +22,7 @@ async fn main() { .plugin(tauri_plugin_single_instance::init(|app, argv, cwd| { println!("{}, {argv:?}, {cwd}", app.package_info().name); - app.emit_all("single-instance", Payload { args: argv, cwd }) + app.emit("single-instance", Payload { args: argv, cwd }) .unwrap(); })) .plugin(tauri_plugin_window_state::Builder::default().build())