From 7f0a2da7a6f4604e642e66ba3ea97e5330b9033c Mon Sep 17 00:00:00 2001 From: IQuant Date: Thu, 18 Jul 2024 13:15:21 +0300 Subject: [PATCH] Refactor settings system. --- Justfile | 3 ++ noita-proxy/Cargo.lock | 2 +- noita-proxy/Cargo.toml | 2 +- noita-proxy/assets/lang/en-US/main.ftl | 1 + noita-proxy/src/lib.rs | 3 ++ noita-proxy/src/net.rs | 33 +++++++++---------- noita-proxy/src/net/proxy_opt.rs | 30 +++++++++++++++++ quant.ew/files/src/net_handling.lua | 12 ++++++- quant.ew/files/src/system/debug.lua | 8 ++--- .../system/player_tether/player_tether.lua | 2 +- .../files/src/system/spawn_hooks/init.lua | 15 ++++----- quant.ew/init.lua | 6 ++-- 12 files changed, 78 insertions(+), 39 deletions(-) create mode 100644 noita-proxy/src/net/proxy_opt.rs diff --git a/Justfile b/Justfile index 35b67111..0c85ae9b 100644 --- a/Justfile +++ b/Justfile @@ -1,6 +1,9 @@ run: cd noita-proxy && NP_APPID=480 NP_SKIP_MOD_CHECK=1 cargo run +build: + cd noita-proxy && cargo build --release + run-rel: cd noita-proxy && NP_APPID=480 NP_SKIP_MOD_CHECK=1 cargo run --release diff --git a/noita-proxy/Cargo.lock b/noita-proxy/Cargo.lock index 8fe1e51e..afa80273 100644 --- a/noita-proxy/Cargo.lock +++ b/noita-proxy/Cargo.lock @@ -1962,7 +1962,7 @@ checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451" [[package]] name = "noita-proxy" -version = "0.13.8" +version = "0.14.0" dependencies = [ "argh", "bincode", diff --git a/noita-proxy/Cargo.toml b/noita-proxy/Cargo.toml index c66affb5..fb891fd5 100644 --- a/noita-proxy/Cargo.toml +++ b/noita-proxy/Cargo.toml @@ -4,7 +4,7 @@ members = ["tangled"] [package] name = "noita-proxy" description = "Noita Entangled Worlds companion app." -version = "0.13.8" +version = "0.14.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/noita-proxy/assets/lang/en-US/main.ftl b/noita-proxy/assets/lang/en-US/main.ftl index 4338c4a1..11a5aa6f 100644 --- a/noita-proxy/assets/lang/en-US/main.ftl +++ b/noita-proxy/assets/lang/en-US/main.ftl @@ -16,6 +16,7 @@ connect_settings_wsv = World sync version to use: connect_settings_player_tether = Player tether enabled connect_settings_player_tether_desc = Player tether: Teleports clients to host if they get far enough. connect_settings_player_tether_length = Tether length +connect_settings_item_dedup = Deduplicate (sync) items spawned by world generation. connect_settings_local = Local settings connect_settings_autostart = Start the game automatically diff --git a/noita-proxy/src/lib.rs b/noita-proxy/src/lib.rs index b1da53a4..970e6b7c 100644 --- a/noita-proxy/src/lib.rs +++ b/noita-proxy/src/lib.rs @@ -40,6 +40,7 @@ pub struct GameSettings { player_tether: bool, tether_length: u32, use_constant_seed: bool, + item_dedup: bool, } impl Default for GameSettings { @@ -51,6 +52,7 @@ impl Default for GameSettings { player_tether: false, tether_length: 750, use_constant_seed: false, + item_dedup: true, } } } @@ -421,6 +423,7 @@ impl App { Slider::new(&mut self.saved_state.game_settings.tether_length, 10..=5000) .text(tr("connect_settings_player_tether_length")), ); + ui.checkbox(&mut self.saved_state.game_settings.item_dedup, tr("connect_settings_item_dedup")); heading_with_underline(ui, tr("connect_settings_local")); ui.checkbox( diff --git a/noita-proxy/src/net.rs b/noita-proxy/src/net.rs index 1f7d231e..add43583 100644 --- a/noita-proxy/src/net.rs +++ b/noita-proxy/src/net.rs @@ -1,5 +1,6 @@ use messages::NetMsg; use omni::OmniPeerId; +use proxy_opt::ProxyOpt; use socket2::{Domain, Socket, Type}; use std::{ env, @@ -20,6 +21,7 @@ use tungstenite::{accept, WebSocket}; use crate::GameSettings; pub mod messages; +mod proxy_opt; pub mod steam_networking; pub mod world; @@ -30,13 +32,6 @@ pub(crate) fn ws_encode_proxy(key: &'static str, value: impl Display) -> tungste tungstenite::Message::Binary(buf) } -pub(crate) fn ws_encode_proxy_opt(key: &'static str, value: impl Display) -> tungstenite::Message { - let mut buf = Vec::new(); - buf.push(2); - write!(buf, "proxy_opt {} {}", key, value).unwrap(); - tungstenite::Message::Binary(buf) -} - pub fn ws_encode_proxy_bin(key: u8, data: &[u8]) -> tungstenite::Message { let mut buf = Vec::new(); buf.push(3); @@ -67,6 +62,13 @@ impl NetInnerState { }; } } + pub(crate) fn try_ws_write_option(&mut self, key: &str, value: impl ProxyOpt) { + let mut buf = Vec::new(); + buf.push(2); + value.write_opt(&mut buf, key); + let message = tungstenite::Message::Binary(buf); + self.try_ws_write(message); + } } pub mod omni; @@ -276,20 +278,15 @@ impl NetManager { )); if let Some(nickname) = &self.init_settings.my_nickname { info!("Chosen nickname: {}", nickname); - state.try_ws_write(ws_encode_proxy("name", nickname)); + state.try_ws_write_option("name", nickname.as_str()); } else { info!("No nickname chosen"); } - state.try_ws_write(ws_encode_proxy( - "debug", - if settings.debug_mode { "true" } else { "false" }, - )); - state.try_ws_write(ws_encode_proxy_opt( - "world_sync_version", - settings.world_sync_version, - )); - state.try_ws_write(ws_encode_proxy_opt("player_tether", settings.player_tether)); - state.try_ws_write(ws_encode_proxy_opt("tether_length", settings.tether_length)); + state.try_ws_write_option("debug", settings.debug_mode); + state.try_ws_write_option("world_sync_version", settings.world_sync_version); + state.try_ws_write_option("player_tether", settings.player_tether); + state.try_ws_write_option("tether_length", settings.tether_length); + state.try_ws_write_option("item_dedup", settings.item_dedup); state.try_ws_write(ws_encode_proxy("ready", "")); // TODO? those are currently ignored by mod diff --git a/noita-proxy/src/net/proxy_opt.rs b/noita-proxy/src/net/proxy_opt.rs new file mode 100644 index 00000000..3b8b1c28 --- /dev/null +++ b/noita-proxy/src/net/proxy_opt.rs @@ -0,0 +1,30 @@ +use std::io::Write; + +/// Trait that allows to pass value to mod as proxy options. +pub(crate) trait ProxyOpt { + fn write_opt(self, buf: &mut Vec, key: &str); +} + +impl ProxyOpt for bool { + fn write_opt(self, buf: &mut Vec, key: &str) { + write!( + buf, + "proxy_opt_bool {} {}", + key, + if self { "true" } else { "false" } + ) + .unwrap(); + } +} + +impl ProxyOpt for u32 { + fn write_opt(self, buf: &mut Vec, key: &str) { + write!(buf, "proxy_opt_num {} {}", key, self).unwrap(); + } +} + +impl ProxyOpt for &str { + fn write_opt(self, buf: &mut Vec, key: &str) { + write!(buf, "proxy_opt {} {}", key, self).unwrap(); + } +} diff --git a/quant.ew/files/src/net_handling.lua b/quant.ew/files/src/net_handling.lua index 5c733180..665ce615 100644 --- a/quant.ew/files/src/net_handling.lua +++ b/quant.ew/files/src/net_handling.lua @@ -40,10 +40,20 @@ function net_handling.proxy.name(_, value) end function net_handling.proxy.proxy_opt(_, key, value) - print("Proxy opt: "..key.." = "..value) + print("Proxy opt [str]: "..key.." = "..value) ctx.proxy_opt[key] = value end +function net_handling.proxy.proxy_opt_num(_, key, value) + print("Proxy opt [num]: "..key.." = "..value) + ctx.proxy_opt[key] = tonumber(value) +end + +function net_handling.proxy.proxy_opt_bool(_, key, value) + print("Proxy opt [bool]: "..key.." = "..value) + ctx.proxy_opt[key] = value == "true" +end + function net_handling.mod.inventory(peer_id, inventory_state) if not player_fns.peer_has_player(peer_id) then return diff --git a/quant.ew/files/src/system/debug.lua b/quant.ew/files/src/system/debug.lua index f6b2b7fc..7b900ae2 100644 --- a/quant.ew/files/src/system/debug.lua +++ b/quant.ew/files/src/system/debug.lua @@ -43,11 +43,9 @@ function module.on_local_player_spawn(my_player) end function module.on_world_update_host() - if ctx.debug then - local hp, max_hp = util.get_ent_health(ctx.my_player.entity) - if hp < max_hp / 2 then - -- util.set_ent_health(ctx.my_player.entity, {max_hp, max_hp}) - end + local hp, max_hp = util.get_ent_health(ctx.my_player.entity) + if hp < max_hp / 2 then + -- util.set_ent_health(ctx.my_player.entity, {max_hp, max_hp}) end end diff --git a/quant.ew/files/src/system/player_tether/player_tether.lua b/quant.ew/files/src/system/player_tether/player_tether.lua index c82e3358..5f4dc59a 100644 --- a/quant.ew/files/src/system/player_tether/player_tether.lua +++ b/quant.ew/files/src/system/player_tether/player_tether.lua @@ -3,7 +3,7 @@ local ctx = dofile_once("mods/quant.ew/files/src/ctx.lua") local net = dofile_once("mods/quant.ew/files/src/net.lua") local player_fns = dofile_once("mods/quant.ew/files/src/player_fns.lua") -local tether_length = tonumber(ctx.proxy_opt.tether_length) +local tether_length = ctx.proxy_opt.tether_length local tether_length_2 = tether_length + 200 local module = {} diff --git a/quant.ew/files/src/system/spawn_hooks/init.lua b/quant.ew/files/src/system/spawn_hooks/init.lua index 7dc106a5..1ff4b116 100644 --- a/quant.ew/files/src/system/spawn_hooks/init.lua +++ b/quant.ew/files/src/system/spawn_hooks/init.lua @@ -9,19 +9,16 @@ local module = {} ModLuaFileAppend("data/scripts/director_helpers.lua", "mods/quant.ew/files/src/system/spawn_hooks/append/director_helpers.lua") ModLuaFileAppend("data/scripts/item_spawnlists.lua", "mods/quant.ew/files/src/system/spawn_hooks/append/item_spawnlist.lua") -local marked = {} -- This entity needs to be synced by item_sync local function is_sync_item(ent_path) - - local start = "data/entities/items/" - if string.sub(ent_path, 1, #start) ~= start then - if marked[ent_path] == nil then - marked[ent_path] = true - print("skip "..ent_path) - end + -- No item needs to be synced when this option is off. + if not ctx.proxy_opt.item_dedup then + return false + end + local start = "data/entities/items/" + if string.sub(ent_path, 1, #start) ~= start then return false end - print(ent_path) return true end diff --git a/quant.ew/init.lua b/quant.ew/init.lua index 8886bc88..55792921 100755 --- a/quant.ew/init.lua +++ b/quant.ew/init.lua @@ -35,7 +35,7 @@ local function load_modules() ctx.dofile_and_add_hooks("mods/quant.ew/files/src/system/damage/sync.lua") ctx.dofile_and_add_hooks("mods/quant.ew/files/src/system/nickname.lua") - if ctx.debug then + if ctx.proxy_opt.debug then ctx.dofile_and_add_hooks("mods/quant.ew/files/src/system/debug.lua") end @@ -43,7 +43,7 @@ local function load_modules() ctx.dofile_and_add_hooks("mods/quant.ew/files/src/system/weather_sync.lua") ctx.dofile_and_add_hooks("mods/quant.ew/files/src/system/polymorph/sync.lua") - if ctx.proxy_opt.world_sync_version == "1" then + if ctx.proxy_opt.world_sync_version == 1 then ctx.dofile_and_add_hooks("mods/quant.ew/files/src/system/world_sync_v1.lua") else ctx.dofile_and_add_hooks("mods/quant.ew/files/src/system/world_sync_v2.lua") @@ -54,7 +54,7 @@ local function load_modules() ctx.dofile_and_add_hooks("mods/quant.ew/files/src/system/proxy_info.lua") ctx.dofile_and_add_hooks("mods/quant.ew/files/src/system/perk_patches/init.lua") - if ctx.proxy_opt.player_tether == "true" then + if ctx.proxy_opt.player_tether then ctx.dofile_and_add_hooks("mods/quant.ew/files/src/system/player_tether/player_tether.lua") end