From 534292d901668f6df9b58846d41f6d62e8a35544 Mon Sep 17 00:00:00 2001 From: Aubrey Taylor Date: Mon, 16 Dec 2024 01:28:13 -0600 Subject: [PATCH] initial commit --- .direnv/flake-profile | 1 + .direnv/flake-profile-10-link | 1 + .envrc | 1 + .gitignore | 1 + Cargo.lock | 702 ++++++++++++++++++++++++++++++++++ Cargo.toml | 14 + flake.lock | 100 +++++ flake.nix | 30 ++ src/main.rs | 38 ++ src/packet.rs | 213 +++++++++++ src/player.rs | 183 +++++++++ 11 files changed, 1284 insertions(+) create mode 120000 .direnv/flake-profile create mode 120000 .direnv/flake-profile-10-link create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 src/main.rs create mode 100644 src/packet.rs create mode 100644 src/player.rs diff --git a/.direnv/flake-profile b/.direnv/flake-profile new file mode 120000 index 0000000..b9f8d1a --- /dev/null +++ b/.direnv/flake-profile @@ -0,0 +1 @@ +flake-profile-10-link \ No newline at end of file diff --git a/.direnv/flake-profile-10-link b/.direnv/flake-profile-10-link new file mode 120000 index 0000000..9b41d40 --- /dev/null +++ b/.direnv/flake-profile-10-link @@ -0,0 +1 @@ +/nix/store/laaadi7cin70p3aj4lwnn0pgn1jwvrrq-nix-shell-env \ No newline at end of file diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..e68c668 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,702 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anyhow" +version = "1.0.94" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" + +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" + +[[package]] +name = "catty" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf0adb3cc1c06945672f8dcc827e42497ac6d0aff49f459ec918132b82a5cbc" +dependencies = [ + "spin", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "event-listener" +version = "2.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" + +[[package]] +name = "futures-core" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" + +[[package]] +name = "futures-task" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f90f7dce0722e95104fcb095585910c0977252f286e354b5e3bd38902cd99988" + +[[package]] +name = "futures-util" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "pin-utils", +] + +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + +[[package]] +name = "hash32" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" +dependencies = [ + "byteorder", +] + +[[package]] +name = "hashbrown" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289" + +[[package]] +name = "heapless" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +dependencies = [ + "hash32", + "stable_deref_trait", +] + +[[package]] +name = "indexmap" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.168" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aaeb2981e0606ca11d79718f8bb01164f1d6ed75080182d3abf017e6d244b6d" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + +[[package]] +name = "mio" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + +[[package]] +name = "newtype-enum" +version = "0.1.0" +source = "git+https://github.com/mgjm/newtype-enum#c8eda405946c2fdff5610e45c4d9b9d144492b5c" +dependencies = [ + "newtype-enum-macro", +] + +[[package]] +name = "newtype-enum-macro" +version = "0.1.0" +source = "git+https://github.com/mgjm/newtype-enum#c8eda405946c2fdff5610e45c4d9b9d144492b5c" +dependencies = [ + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.90", +] + +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + +[[package]] +name = "object" +version = "0.36.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" +dependencies = [ + "memchr", +] + +[[package]] +name = "once_cell" +version = "1.20.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" + +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] +name = "pin-project-lite" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "915a1e146535de9163f3987b8944ed8cf49a18bb0056bcebcdcece385cece4ff" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "proc-macro-crate" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919" +dependencies = [ + "once_cell", + "toml_edit", +] + +[[package]] +name = "proc-macro2" +version = "1.0.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata 0.4.9", + "regex-syntax 0.8.5", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.5", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "smallvec" +version = "1.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" + +[[package]] +name = "smo-server" +version = "0.1.0" +dependencies = [ + "anyhow", + "heapless", + "newtype-enum", + "tokio", + "tracing", + "tracing-subscriber", + "xtra", + "zerocopy", +] + +[[package]] +name = "socket2" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c970269d99b64e60ec3bd6ad27270092a5394c4e309314b18ae3fe575695fbe8" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "spin" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "tokio" +version = "1.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" +dependencies = [ + "backtrace", + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.90", +] + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" + +[[package]] +name = "toml_edit" +version = "0.19.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b5bb770da30e5cbfde35a2d7b9b8a2c4b8ef89548a7a6aeab5c9a576e3e7421" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + +[[package]] +name = "tracing" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "395ae124c09f9e6918a2310af6038fba074bcf474ac352496d5910dd59a2226d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.90", +] + +[[package]] +name = "tracing-core" +version = "0.1.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e672c95779cf947c5311f83787af4fa8fffd12fb27e4993211a84bdfd9610f9c" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8189decb5ac0fa7bc8b96b7cb9b2701d60d48805aca84a238004d665fcc4008" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" + +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "winnow" +version = "0.5.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f593a95398737aeed53e489c785df13f3618e41dbcd6718c6addbf1395aa6876" +dependencies = [ + "memchr", +] + +[[package]] +name = "xtra" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1fd2fa83096d6c2e5eae3dcd265d16bb084abbe1391c38baec036a6cc1b7c7c" +dependencies = [ + "catty", + "event-listener", + "futures-core", + "futures-util", + "pin-project-lite", + "spin", + "tokio", + "xtra-macros", +] + +[[package]] +name = "xtra-macros" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dfd68acdfa4e363b4dad688a87d5ed462cfb9498398c66520c7dab507dbc591" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "zerocopy" +version = "0.8.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67914ab451f3bfd2e69e5e9d2ef3858484e7074d63f204fd166ec391b54de21d" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7988d73a4303ca289df03316bc490e934accf371af6bc745393cf3c2c5c4f25d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.90", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a62d14b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "smo-server" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1.0.94" +heapless = "0.8.0" +newtype-enum = { git = "https://github.com/mgjm/newtype-enum", version = "0.1.0" } +tokio = { version = "1.42.0", features = ["rt", "macros", "net", "sync", "rt-multi-thread", "io-util", "io-std"] } +tracing = "0.1.41" +tracing-subscriber = { version = "0.3.19", features = ["env-filter"] } +xtra = { version = "0.6.0", features = ["macros", "tokio"] } +zerocopy = { version = "0.8.13", features = ["derive"] } diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..f0cbe6d --- /dev/null +++ b/flake.lock @@ -0,0 +1,100 @@ +{ + "nodes": { + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1734244366, + "narHash": "sha256-5sH3pvXDP9ifPGiuFu9OkUSw0hnL2BAabdkXogsn3AU=", + "owner": "nix-community", + "repo": "fenix", + "rev": "e61b6fb56bc8471ad64f8d3eb9dde5b6e0d7ffcd", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1734119587, + "narHash": "sha256-AKU6qqskl0yf2+JdRdD0cfxX4b9x3KKV5RqA6wijmPM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "3566ab7246670a43abd2ffa913cc62dad9cdf7d5", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "fenix": "fenix", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1734121833, + "narHash": "sha256-EZtwXmh9P9FnoXKfbjGxyWksjrjPB4HES2HVIV+STNg=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "fc18d263aa95f7d6de8174bd4c6663dfe865e6d5", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..e2d1263 --- /dev/null +++ b/flake.nix @@ -0,0 +1,30 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + outputs = { self, nixpkgs, flake-utils, fenix }: + flake-utils.lib.eachDefaultSystem + (system: + let + overlays = [ fenix.overlays.default ]; + pkgs = import nixpkgs { + inherit system overlays; + }; + in + with pkgs; + { + devShells.default = mkShell { + buildInputs = [ + pkgs.fenix.stable.completeToolchain + pkg-config + openssl + ]; + }; + } + ); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..7c89326 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,38 @@ +pub mod packet; +pub mod player; + +use std::{collections::HashMap, sync::LazyLock}; + +use player::PlayerActor; +use tokio::{net::TcpListener, sync::RwLock}; +use tracing::{error, info}; +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; +use xtra::Address; + +pub fn clients() -> &'static RwLock>> { + static CLIENTS: LazyLock>>> = LazyLock::new(|| RwLock::default()); + + &CLIENTS +} + +#[tokio::main] +async fn main() { + //.with(EnvFilter::from_default_env()) + tracing_subscriber::registry().with(tracing_subscriber::fmt::layer()).init(); + let tcp = TcpListener::bind("0.0.0.0:1027").await.unwrap(); + + info!("listening on port 1027"); + loop { + match tcp.accept().await { + Ok((stream, addr)) => { + PlayerActor::new_connection(stream, addr); + } + Err(error) => { + error!("failed to handle connection: {error:?}"); + } + } + } + + // let socket = Arc::new(UdpSocket::bind("0.0.0.0:1027").await.unwrap()); + // let mut packet = [0u8; 1024]; +} diff --git a/src/packet.rs b/src/packet.rs new file mode 100644 index 0000000..12b9b95 --- /dev/null +++ b/src/packet.rs @@ -0,0 +1,213 @@ +use core::str; +use std::{ + ffi::CStr, + fmt::{Debug, Display}, +}; + +use anyhow::{bail, Context}; +use newtype_enum::newtype_enum; +use zerocopy::{ + FromBytes, FromZeros, Immutable, IntoBytes, KnownLayout, Unaligned, +}; + +#[derive(Debug, Clone, Copy, FromZeros, IntoBytes, KnownLayout, Immutable)] +#[repr(C, packed)] +pub struct PacketHeader { + pub user_id: u128, + pub kind: PacketKind, + pub size: u16, +} + +#[derive(Debug, Clone, Copy, FromZeros, IntoBytes, KnownLayout, Immutable)] +#[repr(u16)] +pub enum PacketKind { + Unknown = 0, + Init = 1, + Player = 2, + Cap = 3, + Game = 4, + Tag = 5, + Connect = 6, + Disconnect = 7, + Costume = 8, + Shine = 9, + Capture = 10, + ChangeStage = 11, + Command = 12, + UdpInit = 13, + HolePunch = 14 +} + +const COSTUME_NAME_SIZE: usize = 0x20; +const CAP_ANIM_SIZE: usize = 0x30; +const STAGE_GAME_NAME_SIZE: usize = 0x40; +const STAGE_CHANGE_NAME_SIZE: usize = 0x30; +const STAGE_ID_SIZE: usize = 0x10; +const CLIENT_NAME_SIZE: usize = COSTUME_NAME_SIZE; + +#[derive(Debug)] +pub struct Packet { + pub user_id: u128, + pub data: PacketData, +} + +#[newtype_enum] +#[derive(Debug)] +pub enum PacketData { + Unknown(Vec), + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + Init { + pub max_players: u16, + }, + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + Player { + position: [f32; 3], + rotation: [f32; 4], + weights: [f32; 6], + action: u16, + subaction: u16, + }, + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + Cap { + position: [f32; 3], + rotation: [f32; 4], + out: Bool, + anim: String, + }, + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + Game { + is_2d: u8, + scenario_num: u8, + stage: String, + }, + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + Tag { + update_type: u8, + is_it: Bool, + seconds: u8, + minutes: u16, + }, + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + Connect { + kind: ConnectionKind, + max_player: u16, + client_name: String, + }, + Disconnect, + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + Costume { + body_name: String, + cap_name: String, + }, + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + Shine { + shine_id: i32, + is_grand: Bool, + }, + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + Capture { + model: String, + }, + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + ChangeStage { + stage: String, + id: String, + scenario: i8, + sub_scenario: u8, + }, + Command, + #[derive(FromZeros, IntoBytes, KnownLayout, Immutable)] + #[repr(C, packed)] + UdpInit { + port: u16 + }, + HolePunch +} + +pub enum TagUpdateBit { + Time = 0, + State = 1, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, FromZeros, IntoBytes, KnownLayout, Immutable)] +#[repr(u32)] +pub enum ConnectionKind { + New = 0, + Old = 1, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)] +#[repr(C)] +pub struct Bool(u8); + +impl Bool { + pub fn new(value: bool) -> Bool { + Bool(if value { 1 } else { 0 }) + } + pub fn get(&self) -> bool { + self.0 != 0 + } +} + +impl From for Bool { + fn from(value: bool) -> Self { + Bool::new(value) + } +} + +impl From for bool { + fn from(value: Bool) -> Self { + value.get() + } +} + +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)] +#[repr(C)] +pub struct String([u8; N]); + +impl String { + pub fn to_str(&self) -> anyhow::Result<&str> { + let cstr = CStr::from_bytes_until_nul(&self.0).context("interpreting bytes as c-string")?; + cstr.to_str().context("verifying string has utf-8") + // let str = str::from_utf8(&self.0).context("verifying string has utf-8")?; + // Ok(str.trim_end_matches('\0')) + } + pub fn assert_valid(&self) -> anyhow::Result<()> { + self.to_str().map(drop) + } +} + +impl TryFrom<&str> for String { + type Error = anyhow::Error; + fn try_from(value: &str) -> Result { + let mut buf = [0; N]; + if value.len() > N { + bail!("seggs") + } + + value.write_to_prefix(&mut buf).unwrap(); + + Ok(Self(buf)) + } +} + +impl Display for String { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Display::fmt(self.to_str().expect("failed to parse string"), f) + } +} +impl Debug for String { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Debug::fmt(self.to_str().expect("failed to parse string"), f) + } +} diff --git a/src/player.rs b/src/player.rs new file mode 100644 index 0000000..3317b95 --- /dev/null +++ b/src/player.rs @@ -0,0 +1,183 @@ +use std::{net::SocketAddr, time::Duration}; + +use anyhow::{bail, Context}; +use tokio::{ + io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}, + net::{ + tcp::{OwnedReadHalf, OwnedWriteHalf}, + TcpStream, + }, sync::mpsc, time::Instant, +}; +use tracing::{info, info_span, trace, Instrument}; +use zerocopy::{IntoBytes, TryFromBytes}; + +use crate::packet::{ + Packet, PacketData, + PacketData_variants::{Cap, Command, Connect, Disconnect, HolePunch, Init, Player}, + PacketHeader, PacketKind, String, +}; + +// #[derive(Actor)] +pub struct PlayerActor { + write_half: BufWriter, +} + +impl PlayerActor { + pub async fn read_packet(reader: &mut BufReader) -> anyhow::Result { + let mut header = [0; size_of::()]; + reader.read_exact(&mut header).await.context("reading data")?; + let Ok(header) = PacketHeader::try_read_from_bytes(&header) else { + bail!("parsing packet buffer") + }; + + macro_rules! read_data { + ($read: expr, $size: expr, $ty: ident $(, $field:ident => $assert: expr)*) => {{ + async fn read_data(reader: &mut BufReader, size: u16) -> anyhow::Result { + type T = crate::packet::PacketData_variants::$ty; + let size = size as usize; + if size < size_of::() { + bail!("buffer too small for packet: expected {}, got {size}", size_of::()) + } + let mut data = [0; u16::MAX as usize]; + reader.read_exact(&mut data[..size]).await.context("reading data")?; + let packet_data = match T::try_read_from_bytes(&data[..size_of::()]) { + Ok(data) => data, + Err(error) => { + bail!(concat!("interpreting ", stringify!($ty), ": {:?}"), error) + } + }; + + $( + { + let $field = packet_data.$field; + $assert + }?; + )* + Ok(PacketData::$ty(packet_data)) + } + type _FixIntellisense = crate::packet::PacketData_variants::$ty; + read_data($read, $size).await? + }}; + } + + let data: PacketData = match header.kind { + PacketKind::Unknown => { + let mut data = vec![0; header.size.into()]; + reader.read_exact(&mut data).await.context("reading unknown data")?; + PacketData::Unknown(data) + } + PacketKind::Init => read_data!(reader, header.size, Init), + PacketKind::Player => read_data!(reader, header.size, Player), + PacketKind::Cap => read_data!(reader, header.size, Cap, anim => anim.assert_valid()), + PacketKind::Game => read_data!(reader, header.size, Game, stage => stage.assert_valid()), + PacketKind::Tag => read_data!(reader, header.size, Tag), + PacketKind::Connect => read_data!(reader, header.size, Connect), + PacketKind::Disconnect => PacketData::Disconnect(Disconnect), + PacketKind::Costume => read_data!(reader, header.size, Costume), + PacketKind::Shine => read_data!(reader, header.size, Shine), + PacketKind::Capture => read_data!(reader, header.size, Capture), + PacketKind::ChangeStage => read_data!(reader, header.size, ChangeStage), + PacketKind::Command => PacketData::Command(Command), + PacketKind::UdpInit => read_data!(reader, header.size, UdpInit), + PacketKind::HolePunch => PacketData::HolePunch(HolePunch), + }; + + Ok(Packet { + user_id: header.user_id, + data, + }) + } + + pub async fn write_packet(writer: &mut BufWriter, id: u128, data: PacketData) -> anyhow::Result<()> { + let (kind, slice) = match &data { + PacketData::Unknown(vec) => { + if vec.len() >= (u16::MAX as usize) { + bail!("unknown packet vec too large") + } + (PacketKind::Unknown, vec.as_slice()) + } + PacketData::Init(init) => (PacketKind::Init, init.as_bytes()), + PacketData::Player(player) => (PacketKind::Player, player.as_bytes()), + PacketData::Cap(cap) => (PacketKind::Cap, cap.as_bytes()), + PacketData::Game(game) => (PacketKind::Game, game.as_bytes()), + PacketData::Tag(tag) => (PacketKind::Tag, tag.as_bytes()), + PacketData::Connect(connect) => (PacketKind::Connect, connect.as_bytes()), + PacketData::Disconnect(..) => (PacketKind::Disconnect, [].as_slice()), + PacketData::Costume(costume) => (PacketKind::Costume, costume.as_bytes()), + PacketData::Shine(shine) => (PacketKind::Shine, shine.as_bytes()), + PacketData::Capture(capture) => (PacketKind::Capture, capture.as_bytes()), + PacketData::ChangeStage(change_stage) => (PacketKind::ChangeStage, change_stage.as_bytes()), + PacketData::Command(..) => (PacketKind::Command, [].as_slice()), + PacketData::UdpInit(udp_init) => (PacketKind::UdpInit, udp_init.as_bytes()), + PacketData::HolePunch(..) => (PacketKind::HolePunch, [].as_slice()), + }; + + writer + .write_all( + PacketHeader { + kind, + size: slice.len() as u16, + user_id: id, + } + .as_bytes(), + ) + .await + .context("writing header")?; + writer.write_all(slice).await.context("writing data")?; + writer.flush().await.context("flushing writer") + } + + pub fn new_connection(stream: TcpStream, addr: SocketAddr) { + tokio::spawn(async move { + info!("accepted connection from {addr}"); + let (reader, writer) = stream.into_split(); + let mut reader = BufReader::new(reader); + let mut writer = BufWriter::new(writer); + let packet = Self::read_packet(&mut reader).await.unwrap(); + let Packet { + user_id, + data: PacketData::Connect(connect), + } = packet + else { + tracing::error!("not a valid player!"); + return; + }; + + let bot_id = user_id + 1; + Self::write_packet(&mut writer, user_id, PacketData::Init(Init { max_players: 8 })) + .await + .expect("msfrarausfhsdagsdgkog"); + Self::write_packet(&mut writer, bot_id, PacketData::Connect(Connect { kind: crate::packet::ConnectionKind::New, client_name: String::try_from("Bot").unwrap(), max_player: 8 })) + .await + .expect("msfrarausfhsdagsdgkog"); + + let span = info_span!("", player = connect.client_name.to_string()); + let (sender, mut receiver) = mpsc::unbounded_channel(); + tokio::spawn(async move { + while let Some((instant, packet)) = receiver.recv().await { + tokio::time::sleep_until(instant).await; + Self::write_packet(&mut writer, bot_id, packet).await.unwrap(); + } + }.instrument(span.clone())); + + async move { + info!("connected {user_id}"); + + loop { + let packet = Self::read_packet(&mut reader).await.unwrap(); + trace!("got packet {packet:?}"); + let sender = sender.clone(); + tokio::spawn(async move { + let _ = sender.send((Instant::now().checked_add(Duration::from_millis(200)).unwrap(), match packet.data { + PacketData::Player(player) => PacketData::Player(Player {position: [player.position[0], -20., player.position[2]], ..player}), + PacketData::Cap(cap) => PacketData::Cap(Cap {position: [cap.position[0], -20., cap.position[2]], ..cap}), + pass => pass, + })); + }); + } + } + .instrument(span) + .await; + }); + } +}