diff options
author | metamuffin <metamuffin@disroot.org> | 2024-07-08 18:52:39 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-07-08 18:52:39 +0200 |
commit | c987d11babad2d8115e7fae6627669baee5df289 (patch) | |
tree | 4f41e6ad4fba71e7eb9cdbbef9a13877c8935d9e | |
parent | 90f42ec7a3f28fb3a87c233a81e8f65de832983e (diff) | |
download | hurrycurry-c987d11babad2d8115e7fae6627669baee5df289.tar hurrycurry-c987d11babad2d8115e7fae6627669baee5df289.tar.bz2 hurrycurry-c987d11babad2d8115e7fae6627669baee5df289.tar.zst |
add replay system to client and server tool
-rw-r--r-- | Cargo.lock | 203 | ||||
-rw-r--r-- | client/game.gd | 7 | ||||
-rw-r--r-- | client/multiplayer.gd | 8 | ||||
-rw-r--r-- | server/examples/client.rs | 4 | ||||
-rw-r--r-- | server/protocol/src/lib.rs | 7 | ||||
-rw-r--r-- | server/replaytool/Cargo.toml | 3 | ||||
-rw-r--r-- | server/replaytool/src/main.rs | 135 | ||||
-rw-r--r-- | server/src/game.rs | 1 |
8 files changed, 360 insertions, 8 deletions
@@ -188,6 +188,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" [[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + +[[package]] name = "cpufeatures" version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -258,6 +274,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] name = "fake" version = "2.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -268,12 +294,33 @@ dependencies = [ ] [[package]] +name = "fastrand" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" + +[[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] name = "futures-core" version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -407,6 +454,7 @@ name = "hurrycurry-replaytool" version = "0.1.0" dependencies = [ "anyhow", + "clap", "env_logger", "futures-util", "hurrycurry-protocol", @@ -468,6 +516,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + +[[package]] name = "lock_api" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -510,6 +564,23 @@ dependencies = [ ] [[package]] +name = "native-tls" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8614eb2c83d59d1c8cc974dd3f920198647674a0a035e1af1fa58707e317466" +dependencies = [ + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] name = "num_cpus" version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -529,6 +600,56 @@ dependencies = [ ] [[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "openssl" +version = "0.10.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95a0481286a310808298130d22dd1fef0fa571e05a8f44ec801801e84b216b1f" +dependencies = [ + "bitflags", + "cfg-if", + "foreign-types", + "libc", + "once_cell", + "openssl-macros", + "openssl-sys", +] + +[[package]] +name = "openssl-macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a948666b637a0f465e8564c73e89d4dde00d72d4d473cc972f390fc3dcee7d9c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] +name = "openssl-sys" +version = "0.9.102" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] name = "parking_lot" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -564,6 +685,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] +name = "pkg-config" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" + +[[package]] name = "pollster" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -699,18 +826,63 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] +name = "rustix" +version = "0.38.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] name = "ryu" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] +name = "schannel" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] name = "scopeguard" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] +name = "security-framework" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c627723fd09706bacdb5cf41499e95098555af3c3c29d014dc3c458ef6be11c0" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317936bbbd05227752583946b9e66d7ce3b489f84e11a94a510b4437fef407d7" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] name = "serde" version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -823,6 +995,18 @@ dependencies = [ ] [[package]] +name = "tempfile" +version = "3.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" +dependencies = [ + "cfg-if", + "fastrand", + "rustix", + "windows-sys 0.52.0", +] + +[[package]] name = "thiserror" version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -873,6 +1057,16 @@ dependencies = [ ] [[package]] +name = "tokio-native-tls" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbae76ab933c85776efabc971569dd6119c580d8f5d448769dec1764bf796ef2" +dependencies = [ + "native-tls", + "tokio", +] + +[[package]] name = "tokio-tungstenite" version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -880,7 +1074,9 @@ checksum = "c6989540ced10490aaf14e6bad2e3d33728a2813310a0c71d1574304c49631cd" dependencies = [ "futures-util", "log", + "native-tls", "tokio", + "tokio-native-tls", "tungstenite", ] @@ -896,6 +1092,7 @@ dependencies = [ "http", "httparse", "log", + "native-tls", "rand 0.8.5", "sha1", "thiserror", @@ -933,6 +1130,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/client/game.gd b/client/game.gd index e760186b..a31109cb 100644 --- a/client/game.gd +++ b/client/game.gd @@ -32,6 +32,7 @@ var tile_collide: Array = [] var tile_interact: Array = [] var map_names: Array = [] var in_lobby := false +var is_replay := false var marker_target = Vector3(0,0,0) var players := {} @@ -50,6 +51,7 @@ func _ready(): if !Global.on_vulkan(): environment.environment.tonemap_exposure = 0.5 + mp.replay_start.connect(func(): is_replay = true) mp.connection_closed.connect(func(reason: String): Global.error_message = reason; get_parent().replace_menu("res://menu/error.tscn") @@ -241,11 +243,12 @@ func _process(delta): marker.position = Global.interpolate(marker.position, marker_target, delta * 30.) update_center() + if is_replay: mp.send_replay_tick(delta) + if Global.get_setting("debug_info"): debug_label.show() debug_label.text = "%d FPS" % Engine.get_frames_per_second() - else: - debug_label.hide() + else: debug_label.hide() func get_tile_collision(pos: Vector2i) -> bool: var t = map.get_tile_name(pos) diff --git a/client/multiplayer.gd b/client/multiplayer.gd index 4d071933..709a4502 100644 --- a/client/multiplayer.gd +++ b/client/multiplayer.gd @@ -51,6 +51,7 @@ signal set_ingame(state: bool, lobby: bool) signal score(demands_failed: int, demands_completed: int, points: int, time_remaining: float) signal hide_score() signal server_message(text: String) +signal replay_start() signal connection_closed(reason: String) @@ -242,6 +243,7 @@ func handle_packet(bytes: PackedByteArray): "server_message": var text = decoded["text"] server_message.emit(text) + "replay_start": replay_start.emit() _: push_error("Unrecognized packet type: %s" % packet_type) @@ -279,6 +281,12 @@ func send_chat(message: String): } }) +func send_replay_tick(dt: float): + send_packet({ + "type": "replay_tick", + "dt": dt + }) + func send_packet(packet): var json = JSON.stringify(packet) socket.send_text(json) diff --git a/server/examples/client.rs b/server/examples/client.rs index 80ba8ffa..70b1bb00 100644 --- a/server/examples/client.rs +++ b/server/examples/client.rs @@ -15,15 +15,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ +use hurrycurry_protocol::{glam::Vec2, PacketC, PacketS}; use std::{ io::{stdin, BufRead, BufReader, Write}, net::TcpStream, thread, }; -use glam::Vec2; -use hurrycurry_protocol::{PacketC, PacketS}; - fn main() { let mut sock = TcpStream::connect("127.0.0.1:27031").unwrap(); diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs index 49ff6e1d..36a496a0 100644 --- a/server/protocol/src/lib.rs +++ b/server/protocol/src/lib.rs @@ -79,6 +79,10 @@ pub enum PacketS { ReplaceHand { item: Option<ItemIndex>, }, + /// For use in replay sessions only + ReplayTick { + dt: f64, + }, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -156,6 +160,9 @@ pub enum PacketC { Error { message: String, }, + + /// For use in replay sessions only + ReplayStart, } #[derive(Debug, Clone, Serialize, Deserialize, Copy)] diff --git a/server/replaytool/Cargo.toml b/server/replaytool/Cargo.toml index 16f8377a..e6c1cc23 100644 --- a/server/replaytool/Cargo.toml +++ b/server/replaytool/Cargo.toml @@ -10,8 +10,9 @@ anyhow = "1.0.86" serde = { version = "1.0.204", features = ["derive"] } tokio = { version = "1.38.0", features = ["full"] } serde_json = "1.0.120" -tokio-tungstenite = "0.23.1" +tokio-tungstenite = { version = "0.23.1", features = ["native-tls"] } futures-util = "0.3.30" rand = "0.9.0-alpha.1" +clap = { version = "4.5.8", features = ["derive"] } hurrycurry-protocol = { path = "../protocol" } diff --git a/server/replaytool/src/main.rs b/server/replaytool/src/main.rs index efdc36e0..d2fcf26c 100644 --- a/server/replaytool/src/main.rs +++ b/server/replaytool/src/main.rs @@ -1,5 +1,136 @@ +use anyhow::anyhow; +use clap::Parser; +use futures_util::{SinkExt, StreamExt}; +use hurrycurry_protocol::{PacketC, PacketS}; +use log::{debug, info, warn, LevelFilter}; +use serde::{Deserialize, Serialize}; +use std::{path::PathBuf, time::Instant}; +use tokio::{ + fs::File, + io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter}, + net::TcpListener, +}; +use tokio_tungstenite::tungstenite::Message; + +#[derive(Parser)] +enum Args { + Record { url: String, output: PathBuf }, + Replay { input: PathBuf }, +} + +#[derive(Serialize, Deserialize)] +struct Event { + ts: f64, + packet: PacketC, +} #[tokio::main] -async fn main() { - +async fn main() -> anyhow::Result<()> { + env_logger::builder() + .filter_level(LevelFilter::Info) + .parse_env("LOG") + .init(); + + let args = Args::parse(); + + match args { + Args::Record { url, output } => { + let mut file = BufWriter::new(File::create(&output).await?); + info!("connecting to {url:?}..."); + let (mut sock, _) = tokio_tungstenite::connect_async(url).await?; + info!("starting recording."); + let start = Instant::now(); + + while let Some(Ok(message)) = sock.next().await { + match message { + Message::Text(line) => { + let packet: PacketC = match serde_json::from_str(&line) { + Ok(p) => p, + Err(e) => { + warn!("invalid packet: {e}"); + break; + } + }; + debug!("<- {packet:?}"); + file.write_all( + format!( + "{}\n", + serde_json::to_string(&Event { + ts: start.elapsed().as_secs_f64(), + packet: packet + }) + .unwrap() + ) + .as_bytes(), + ) + .await? + } + Message::Close(_) => break, + _ => (), + } + } + } + Args::Replay { input } => { + let ws_listener = TcpListener::bind("0.0.0.0:27032").await?; + info!("listening for websockets on {}", ws_listener.local_addr()?); + + loop { + let mut file = BufReader::new(File::open(&input).await?).lines(); + let mut next_event = + serde_json::from_str::<Event>(&file.next_line().await?.ok_or(anyhow!("eof"))?)?; + let mut time = 0.; + + info!("ready"); + let (sock, addr) = ws_listener.accept().await?; + let Ok(mut sock) = tokio_tungstenite::accept_async(sock).await else { + warn!("invalid ws handshake"); + continue; + }; + info!("{addr} connected via ws"); + + sock.send(tokio_tungstenite::tungstenite::Message::Text( + serde_json::to_string(&PacketC::ReplayStart).unwrap(), + )) + .await?; + while let Some(Ok(message)) = sock.next().await { + match message { + Message::Text(line) => { + let packet: PacketS = match serde_json::from_str(&line) { + Ok(p) => p, + Err(e) => { + warn!("invalid packet: {e}"); + break; + } + }; + debug!("<- {packet:?}"); + + match packet { + PacketS::ReplayTick { dt } => { + time += dt; + while next_event.ts < time { + debug!("<- {:?}", next_event.packet); + sock.send(tokio_tungstenite::tungstenite::Message::Text( + serde_json::to_string(&next_event.packet).unwrap(), + )) + .await?; + + let Some(next) = &file.next_line().await? else { + info!("reached end"); + break; + }; + next_event = serde_json::from_str::<Event>(next)?; + } + } + _ => (), + } + } + Message::Close(_) => break, + _ => (), + } + } + info!("{addr} left"); + } + } + } + Ok(()) } diff --git a/server/src/game.rs b/server/src/game.rs index 74bc9d45..32fb7ac9 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -414,6 +414,7 @@ impl Game { item, }) } + PacketS::ReplayTick { .. } => bail!("packet not supported in this session"), } if self.points != points_before { |