From d6434a201c12149a6aa002fa76102a976334471f Mon Sep 17 00:00:00 2001 From: Observer KRypt0n_ Date: Sun, 16 Apr 2023 10:20:27 +0200 Subject: [PATCH] feat: added `sandbox` config --- Cargo.toml | 3 +- README.md | 1 + src/config/schema_blanks/mod.rs | 6 ++++ src/config/schema_blanks/sandbox.rs | 49 +++++++++++++++++++++++++++++ src/genshin/config/mod.rs | 2 +- src/genshin/config/schema/mod.rs | 11 +++++++ src/honkai/config/mod.rs | 2 +- src/honkai/config/schema/mod.rs | 6 ++++ 8 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/config/schema_blanks/sandbox.rs diff --git a/Cargo.toml b/Cargo.toml index e82c813..5bc4996 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,10 +37,11 @@ config = ["dep:serde", "dep:serde_json", "dep:enum-ordinalize"] components = ["dep:wincompatlib", "dep:lazy_static"] game = ["components", "config"] discord-rpc = ["dep:discord-rich-presence"] +sandbox = [] # Genshin-specific features environment-emulation = [] fps-unlocker = ["dep:md-5"] -all = ["states", "config", "components", "game", "discord-rpc", "environment-emulation", "fps-unlocker"] +all = ["states", "config", "components", "game", "discord-rpc", "sandbox", "environment-emulation", "fps-unlocker"] default = ["all"] diff --git a/README.md b/README.md index 1734b9b..db667e1 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ SDK based on [anime-game-core](https://github.com/an-anime-team/anime-game-core) | Manage components (list wine/dxvk versions, etc) | `components` | | Run the game | `game` | | Use Discord RPC when the game is running | `discord-rpc` | +| Run the game in `bwrap` sandbox | `sandbox` | ## Anime Game specific features diff --git a/src/config/schema_blanks/mod.rs b/src/config/schema_blanks/mod.rs index 90dc727..6e83ffa 100644 --- a/src/config/schema_blanks/mod.rs +++ b/src/config/schema_blanks/mod.rs @@ -9,6 +9,9 @@ pub mod dxvk; pub mod wine; pub mod gamescope; +#[cfg(feature = "sandbox")] +pub mod sandbox; + pub mod prelude { pub use super::resolution::Resolution; pub use super::repairer::Repairer; @@ -19,4 +22,7 @@ pub mod prelude { pub use super::wine::prelude::*; pub use super::gamescope::prelude::*; + + #[cfg(feature = "sandbox")] + pub use super::sandbox::Sandbox; } diff --git a/src/config/schema_blanks/sandbox.rs b/src/config/schema_blanks/sandbox.rs new file mode 100644 index 0000000..e2a7ed3 --- /dev/null +++ b/src/config/schema_blanks/sandbox.rs @@ -0,0 +1,49 @@ +use serde::{Serialize, Deserialize}; +use serde_json::Value as JsonValue; + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Sandbox { + pub enabled: bool, + pub private: Vec +} + +impl Default for Sandbox { + #[inline] + fn default() -> Self { + Self { + enabled: true, + private: vec![] + } + } +} + +impl From<&JsonValue> for Sandbox { + fn from(value: &JsonValue) -> Self { + let default = Self::default(); + + Self { + enabled: match value.get("enabled") { + Some(value) => value.as_bool().unwrap_or(default.enabled), + None => default.enabled + }, + + private: match value.get("private") { + Some(value) => match value.as_array() { + Some(values) => { + let mut private = Vec::new(); + + for value in values { + if let Some(server) = value.as_str() { + private.push(server.to_string()); + } + } + + private + }, + None => default.private + }, + None => default.private + } + } + } +} diff --git a/src/genshin/config/mod.rs b/src/genshin/config/mod.rs index dc95aa2..45c90c3 100644 --- a/src/genshin/config/mod.rs +++ b/src/genshin/config/mod.rs @@ -31,7 +31,7 @@ impl ConfigExt for Config { #[inline] fn deserialize_schema>(schema: T) -> anyhow::Result { - Ok(serde_json::from_str(schema.as_ref())?) + Ok(Self::Schema::from(&serde_json::from_str(schema.as_ref())?)) } #[inline] diff --git a/src/genshin/config/schema/mod.rs b/src/genshin/config/schema/mod.rs index c56aec1..56d50c1 100644 --- a/src/genshin/config/schema/mod.rs +++ b/src/genshin/config/schema/mod.rs @@ -1,6 +1,9 @@ use serde::{Serialize, Deserialize}; use serde_json::Value as JsonValue; +#[cfg(feature = "sandbox")] +use crate::config::schema_blanks::sandbox::Sandbox; + #[cfg(feature = "components")] use crate::components::wine::Version as WineVersion; @@ -33,6 +36,9 @@ pub struct Schema { pub launcher: Launcher, pub game: Game, + #[cfg(feature = "sandbox")] + pub sandbox: Sandbox, + #[cfg(feature = "components")] pub components: Components, @@ -54,6 +60,11 @@ impl From<&JsonValue> for Schema { None => default.game }, + sandbox: match value.get("sandbox") { + Some(value) => Sandbox::from(value), + None => default.sandbox + }, + components: match value.get("components") { Some(value) => Components::from(value), None => default.components diff --git a/src/honkai/config/mod.rs b/src/honkai/config/mod.rs index c919457..1c9695f 100644 --- a/src/honkai/config/mod.rs +++ b/src/honkai/config/mod.rs @@ -31,7 +31,7 @@ impl ConfigExt for Config { #[inline] fn deserialize_schema>(schema: T) -> anyhow::Result { - Ok(serde_json::from_str(schema.as_ref())?) + Ok(Self::Schema::from(&serde_json::from_str(schema.as_ref())?)) } #[inline] diff --git a/src/honkai/config/schema/mod.rs b/src/honkai/config/schema/mod.rs index c56aec1..153af67 100644 --- a/src/honkai/config/schema/mod.rs +++ b/src/honkai/config/schema/mod.rs @@ -1,6 +1,9 @@ use serde::{Serialize, Deserialize}; use serde_json::Value as JsonValue; +#[cfg(feature = "sandbox")] +use crate::config::schema_blanks::sandbox::Sandbox; + #[cfg(feature = "components")] use crate::components::wine::Version as WineVersion; @@ -33,6 +36,9 @@ pub struct Schema { pub launcher: Launcher, pub game: Game, + #[cfg(feature = "sandbox")] + pub sandbox: Sandbox, + #[cfg(feature = "components")] pub components: Components,