From c38fc3206a4ffa2aa1c6517a8570ea0cd92daecb Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Thu, 6 Apr 2023 22:06:53 +0200 Subject: [PATCH] 0.5.9 - added environment emulation feature - updated core library to 1.5.1 --- Cargo.toml | 7 ++-- src/config/launcher/mod.rs | 19 +++++++++- src/discord_rpc.rs | 2 + src/env_emulation.rs | 77 ++++++++++++++++++++++++++++++++++++++ src/game.rs | 13 +++++++ src/lib.rs | 16 ++++---- 6 files changed, 122 insertions(+), 12 deletions(-) create mode 100644 src/env_emulation.rs diff --git a/Cargo.toml b/Cargo.toml index 8afaa39..b55542c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "anime-launcher-sdk" -version = "0.5.8" +version = "0.5.9" authors = ["Nikita Podvirnyy "] license = "GPL-3.0" readme = "README.md" @@ -8,7 +8,7 @@ edition = "2021" [dependencies.anime-game-core] git = "https://github.com/an-anime-team/anime-game-core" -tag = "1.5.0" +tag = "1.5.1" features = ["genshin", "all"] # path = "../anime-game-core" # ! for dev purposes only @@ -34,6 +34,7 @@ components = ["dep:wincompatlib", "dep:lazy_static"] game = ["components", "config"] fps-unlocker = ["dep:md-5"] discord-rpc = ["dep:discord-rich-presence"] +environment-emulation = [] -all = ["states", "config", "components", "game", "fps-unlocker", "discord-rpc"] +all = ["states", "config", "components", "game", "fps-unlocker", "discord-rpc", "environment-emulation"] default = ["all"] diff --git a/src/config/launcher/mod.rs b/src/config/launcher/mod.rs index c8163ba..fe0f233 100644 --- a/src/config/launcher/mod.rs +++ b/src/config/launcher/mod.rs @@ -9,6 +9,9 @@ use anime_game_core::genshin::consts::GameEdition as CoreGameEdition; use crate::consts::launcher_dir; +#[cfg(feature = "environment-emulation")] +use crate::env_emulation::Environment; + pub mod repairer; #[cfg(feature = "discord-rpc")] @@ -87,7 +90,10 @@ pub struct Launcher { pub style: LauncherStyle, #[cfg(feature = "discord-rpc")] - pub discord_rpc: DiscordRpc + pub discord_rpc: DiscordRpc, + + #[cfg(feature = "environment-emulation")] + pub environment: Environment } impl Default for Launcher { @@ -100,7 +106,10 @@ impl Default for Launcher { style: LauncherStyle::default(), #[cfg(feature = "discord-rpc")] - discord_rpc: DiscordRpc::default() + discord_rpc: DiscordRpc::default(), + + #[cfg(feature = "environment-emulation")] + environment: Environment::default() } } } @@ -148,6 +157,12 @@ impl From<&JsonValue> for Launcher { discord_rpc: match value.get("discord_rpc") { Some(value) => DiscordRpc::from(value), None => default.discord_rpc + }, + + #[cfg(feature = "environment-emulation")] + environment: match value.get("environment") { + Some(value) => serde_json::from_value(value.clone()).unwrap_or(default.environment), + None => default.environment } } } diff --git a/src/discord_rpc.rs b/src/discord_rpc.rs index e77b79c..d278a05 100644 --- a/src/discord_rpc.rs +++ b/src/discord_rpc.rs @@ -95,12 +95,14 @@ impl DiscordRpc { .assets(Assets::new().large_image(&config.icon)) } + #[inline] pub fn update(&self, update: RpcUpdates) -> Result<(), SendError> { self.sender.send(update) } } impl Drop for DiscordRpc { + #[inline] #[allow(unused_must_use)] fn drop(&mut self) { self.update(RpcUpdates::Disconnect); diff --git a/src/env_emulation.rs b/src/env_emulation.rs new file mode 100644 index 0000000..f355ad3 --- /dev/null +++ b/src/env_emulation.rs @@ -0,0 +1,77 @@ +use serde::{Serialize, Deserialize}; +use enum_ordinalize::Ordinalize; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Ordinalize)] +pub enum Environment { + /// `config.ini` format: + /// + /// ```ini + /// [General] + /// channel=1 + /// cps=mihoyo + /// game_version=[game version] + /// sub_channel=0 + /// ``` + PC, + + /// `config.ini` format: + /// + /// ```ini + /// [General] + /// channel=1 + /// cps=pcseaepic + /// game_version=[game version] + /// # plugin_sdk_version=2.14.2 (??? not used now) + /// sub_channel=3 + /// ``` + Epic, + + /// `config.ini` format: + /// + /// ```ini + /// [General] + /// channel=1 + /// cps=pcgoogle + /// game_version=[game version] + /// sub_channel=6 + /// ``` + Android +} + +impl Default for Environment { + #[inline] + fn default() -> Self { + Self::PC + } +} + +impl Environment { + /// Generate `config.ini`'s content + pub fn generate_config(&self, game_version: impl AsRef) -> String { + match self { + Self::PC => [ + "[General]", + "channel=1", + "cps=mihoyo", + &format!("game_version={}", game_version.as_ref()), + "sub_channel=0" + ].join("\n"), + + Self::Epic => [ + "[General]", + "channel=1", + "cps=pcseaepic", + &format!("game_version={}", game_version.as_ref()), + "sub_channel=3" + ].join("\n"), + + Self::Android => [ + "[General]", + "channel=1", + "cps=pcgoogle", + &format!("game_version={}", game_version.as_ref()), + "sub_channel=6" + ].join("\n") + } + } +} diff --git a/src/game.rs b/src/game.rs index fad896f..89fa9c9 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,6 +1,8 @@ use std::process::{Command, Stdio}; +use anime_game_core::prelude::*; use anime_game_core::genshin::telemetry; +use anime_game_core::genshin::game::Game; use super::consts; use super::config; @@ -91,6 +93,17 @@ pub fn run() -> anyhow::Result<()> { .replace("start YuanShen.exe %*", &format!("start YuanShen.exe %*\n\nZ:\ncd \"{}\"\nstart unlocker.exe", unlocker.dir().to_string_lossy())))?; } + // Generate `config.ini` if environment emulation feature is presented + + #[cfg(feature = "environment-emulation")] { + let game = Game::new(&config.game.path); + + std::fs::write( + config.game.path.join("config.ini"), + config.launcher.environment.generate_config(game.get_version()?.to_string()) + )?; + } + // Prepare bash -c '' let mut bash_command = String::new(); diff --git a/src/lib.rs b/src/lib.rs index 8e24cde..de8259d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,9 @@ pub mod fps_unlocker; #[cfg(feature = "discord-rpc")] pub mod discord_rpc; +#[cfg(feature = "environment-emulation")] +pub mod env_emulation; + pub const VERSION: &str = env!("CARGO_PKG_VERSION"); /// Check if specified binary is available @@ -35,12 +38,11 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION"); pub fn is_available(binary: &str) -> bool { tracing::trace!("Checking package availability"); - match Command::new(binary).stdout(Stdio::null()).stderr(Stdio::null()).spawn() { - Ok(mut child) => { - child.kill(); + let Ok(mut child) = Command::new(binary).stdout(Stdio::null()).stderr(Stdio::null()).spawn() else { + return false; + }; - true - }, - Err(_) => false - } + child.kill(); + + true }