From b9894ec7363fe883da8df811652ad3e2ef160d4a Mon Sep 17 00:00:00 2001 From: LynithDev <61880709+LynithDev@users.noreply.github.com> Date: Sat, 23 Mar 2024 22:04:45 +0100 Subject: [PATCH] refactor: Add macros and add helper functions --- src-tauri/src/game/client.rs | 85 +++++++++++++++++++++++++++++------ src-tauri/src/game/vanilla.rs | 25 ++++++++--- 2 files changed, 91 insertions(+), 19 deletions(-) diff --git a/src-tauri/src/game/client.rs b/src-tauri/src/game/client.rs index 8588ee34..0b629bb1 100644 --- a/src-tauri/src/game/client.rs +++ b/src-tauri/src/game/client.rs @@ -9,7 +9,18 @@ use uuid::Uuid; use crate::PolyResult; -use super::{minecraft::{MinecraftManifest, ReleaseType}, vanilla::{self, VanillaImpl}}; +use super::{minecraft::{MinecraftManifest, ReleaseType}, vanilla::{self, VanillaClientProps}}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Version { + id: String, + url: String, + #[serde(default)] + release_type: ReleaseType, + #[serde(default)] + release_time: chrono::DateTime +} #[derive(Debug, Serialize, Deserialize)] pub struct Manifest { @@ -27,21 +38,10 @@ pub struct Instance { pub client: ClientType, } -#[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct Version { - id: String, - url: String, - #[serde(default)] - release_type: ReleaseType, - #[serde(default)] - release_time: chrono::DateTime -} - #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type")] pub enum ClientType { - Vanilla(VanillaImpl) + Vanilla(VanillaClientProps) } impl ClientType { @@ -53,7 +53,15 @@ impl ClientType { } #[async_trait] -pub trait ClientTrait: Send + Sync { +pub trait ClientTrait<'a>: Send + Sync { + fn get_impl(handle: AppHandle, client: &'a ClientType, instance: &'a Instance, manifest: &'a Manifest) -> Box + 'a> where Self: Sized { + Box::new(match client { + ClientType::Vanilla(_) => vanilla::VanillaClient::new(handle, instance, manifest) + }) + } + + fn new(handle: AppHandle, instance: &'a Instance, manifest: &'a Manifest) -> Self where Self: Sized; + async fn launch(&self) -> PolyResult<()>; async fn setup(&self) -> PolyResult<()>; @@ -61,6 +69,55 @@ pub trait ClientTrait: Send + Sync { async fn install_libraries(&self) -> PolyResult; async fn install_natives(&self) -> PolyResult<()>; async fn install_assets(&self) -> PolyResult<()>; + + fn get_instance(&self) -> &'a Instance where Self: Sized; + fn get_manifest(&self) -> &'a Manifest where Self: Sized; + fn get_handle(&'a self) -> &'a AppHandle where Self: Sized; +} + +#[macro_export] +macro_rules! create_game_client_props { + ($client:ident { $($name:ident: $type:ty),* }) => { + #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] + pub struct $client { + $(pub $name: $type),* + } + }; +} + +#[macro_export] +macro_rules! create_game_client { + ($props:ident { $($props_name:ident: $props_type:ty),* } $client:ident { $($name:ident: $type:ty),* }) => { + #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] + pub struct $props { + $(pub $props_name: $props_type),* + } + + #[derive(Debug, Clone)] + pub struct $client<'a> { + pub handle: tauri::AppHandle, + pub instance: &'a crate::game::client::Instance, + pub manifest: &'a crate::game::client::Manifest, + $(pub $name: $type),* + } + }; +} + +#[macro_export] +macro_rules! impl_game_client { + () => { + fn get_instance(&self) -> &'a crate::game::client::Instance where Self: Sized { + self.instance + } + + fn get_manifest(&self) -> &'a crate::game::client::Manifest where Self: Sized { + self.manifest + } + + fn get_handle(&'a self) -> &'a tauri::AppHandle where Self: Sized { + &self.handle + } + }; } pub struct ClientManager { diff --git a/src-tauri/src/game/vanilla.rs b/src-tauri/src/game/vanilla.rs index d5b6aa41..a682c66d 100644 --- a/src-tauri/src/game/vanilla.rs +++ b/src-tauri/src/game/vanilla.rs @@ -2,14 +2,27 @@ use std::{fs, path::PathBuf}; use anyhow::anyhow; use async_trait::async_trait; use serde::{Deserialize, Serialize}; -use crate::{constants::MINECRAFT_VERSIONS_MANIFEST, utils::http, PolyResult}; -use super::{client::{ClientTrait, Version}, minecraft::MinecraftManifest}; +use crate::{constants::MINECRAFT_VERSIONS_MANIFEST, create_game_client, impl_game_client, utils::http, PolyResult}; +use super::{client::{ClientTrait, Instance, Manifest, Version}, minecraft::MinecraftManifest}; -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct VanillaImpl; +create_game_client! { + VanillaClientProps {} + + VanillaClient {} +} #[async_trait] -impl ClientTrait for VanillaImpl { +impl<'a> ClientTrait<'a> for VanillaClient<'a> { + impl_game_client!(); + + fn new(handle: tauri::AppHandle, instance: &'a Instance, manifest: &'a Manifest) -> Self { + VanillaClient { + handle, + instance, + manifest + } + } + async fn launch(&self) -> PolyResult<()> { Ok(()) } @@ -19,6 +32,8 @@ impl ClientTrait for VanillaImpl { } async fn install_game(&self) -> PolyResult<()> { + let artifact = &self.manifest.manifest.downloads.client; + Ok(()) }