Refactor settings system.

This commit is contained in:
IQuant 2024-07-18 13:15:21 +03:00
parent 6ef4287197
commit 7f0a2da7a6
12 changed files with 78 additions and 39 deletions

View file

@ -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

View file

@ -1962,7 +1962,7 @@ checksum = "2bf50223579dc7cdcfb3bfcacf7069ff68243f8c363f62ffa99cf000a6b9c451"
[[package]]
name = "noita-proxy"
version = "0.13.8"
version = "0.14.0"
dependencies = [
"argh",
"bincode",

View file

@ -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

View file

@ -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

View file

@ -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(

View file

@ -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

View file

@ -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<u8>, key: &str);
}
impl ProxyOpt for bool {
fn write_opt(self, buf: &mut Vec<u8>, 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<u8>, key: &str) {
write!(buf, "proxy_opt_num {} {}", key, self).unwrap();
}
}
impl ProxyOpt for &str {
fn write_opt(self, buf: &mut Vec<u8>, key: &str) {
write!(buf, "proxy_opt {} {}", key, self).unwrap();
}
}

View file

@ -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

View file

@ -43,13 +43,11 @@ 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
end
end
return module

View file

@ -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 = {}

View file

@ -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

View file

@ -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