removed backend
@@ -1,12 +1,4 @@
|
||||
**/target
|
||||
**/*.rs.bk
|
||||
**/target
|
||||
backend.log
|
||||
*.so
|
||||
.vscode
|
||||
|
||||
|
||||
# Python
|
||||
.tox
|
||||
.pytest_cache
|
||||
*.spec
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
16:45:15 [ INFO] Found interfaces: [NetworkInterface { name: "eno1", index: 2, mac: Some(00:d8:61:54:8e:1e), ips: [V4(Ipv4Network { addr: 192.168.1.119, prefix: 24 }), V6(Ipv6Network { addr: fe80::eeaa:7e11:fdae:3789, prefix: 64 })], flags: 69699 }]
|
||||
16:45:15 [ INFO] Listening to network packets...
|
||||
@@ -1,715 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.6.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "arrayref"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee"
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4f67931368edf3a9a51d29886d245f1c3db2f1ef0dcc9e35ff70341b78c10d23"
|
||||
|
||||
[[package]]
|
||||
name = "blake2b_simd"
|
||||
version = "0.5.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a"
|
||||
dependencies = [
|
||||
"arrayref",
|
||||
"arrayvec",
|
||||
"constant_time_eq",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "0.4.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"iovec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.2.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9213f7cd7c27e95c2b57c49f0e69b1ea65b27138da84a170133fd21b07659c00"
|
||||
dependencies = [
|
||||
"num",
|
||||
"time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31850b4a4d6bae316f7a09e691c944c28299298837edc0a03f755618c23cbc01"
|
||||
dependencies = [
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "constant_time_eq"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
|
||||
|
||||
[[package]]
|
||||
name = "cpython"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85532c648315aeb0829ad216a6a29aa3212cf9319bc7f6daf1404aa0bdd1485f"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"num-traits",
|
||||
"python3-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce446db02cdc3165b94ae73111e570793400d0794e46125cc4056c81cbb039f4"
|
||||
dependencies = [
|
||||
"autocfg 0.1.7",
|
||||
"cfg-if",
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive_more"
|
||||
version = "0.99.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2159be042979966de68315bce7034bb000c775f22e3e834e1c52ff78f041cae8"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"redox_users",
|
||||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fake_clock"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a121cefff0e603c3a3477370e0fc965059d848bdab6b56554c1aa2b5b75bb320"
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "glob"
|
||||
version = "0.2.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
|
||||
|
||||
[[package]]
|
||||
name = "iovec"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ipnetwork"
|
||||
version = "0.15.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a69dd5e3613374e74da81c251750153abe3bd0ad17641ea63d43d1e21d0dbd4d"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e"
|
||||
|
||||
[[package]]
|
||||
name = "kernel32-sys"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
||||
dependencies = [
|
||||
"winapi 0.2.8",
|
||||
"winapi-build",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libaostats"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"chrono 0.4.10",
|
||||
"cpython",
|
||||
"derive_more",
|
||||
"fake_clock",
|
||||
"lazy_static",
|
||||
"log 0.4.8",
|
||||
"packet_sniffer",
|
||||
"photon_decode",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"simplelog",
|
||||
"timer",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b"
|
||||
dependencies = [
|
||||
"log 0.4.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3197e20c7edb283f87c071ddfc7a2cca8f8e0b888c242959846a6fce03c72223"
|
||||
|
||||
[[package]]
|
||||
name = "num"
|
||||
version = "0.1.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4703ad64153382334aa8db57c637364c322d3372e097840c72000dabdcf6156e"
|
||||
dependencies = [
|
||||
"num-integer",
|
||||
"num-iter",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba"
|
||||
dependencies = [
|
||||
"autocfg 1.0.0",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-iter"
|
||||
version = "0.1.40"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dfb0800a0291891dd9f4fe7bd9c19384f98f7fbe0cd0f39a2c6b88b9868bbc00"
|
||||
dependencies = [
|
||||
"autocfg 1.0.0",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
|
||||
dependencies = [
|
||||
"autocfg 1.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "packet_sniffer"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"log 0.4.8",
|
||||
"pnet",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "photon_decode"
|
||||
version = "0.2.1"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pnet"
|
||||
version = "0.25.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c08c2c6c26481fcbe49dc4405baedf47151f859c5a45d3f254c2ff74ce51cf0"
|
||||
dependencies = [
|
||||
"ipnetwork",
|
||||
"pnet_base",
|
||||
"pnet_datalink",
|
||||
"pnet_packet",
|
||||
"pnet_sys",
|
||||
"pnet_transport",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pnet_base"
|
||||
version = "0.22.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4df28acf2fcc77436dd2b91a9a0c2bb617f9ca5f2acefee1a4135058b9f9801f"
|
||||
|
||||
[[package]]
|
||||
name = "pnet_datalink"
|
||||
version = "0.25.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "545f8df67cbc53438f37f56e68ae5ca49beb3990e9fd7e9e214c8ffd36c0e0ea"
|
||||
dependencies = [
|
||||
"ipnetwork",
|
||||
"libc",
|
||||
"pnet_base",
|
||||
"pnet_sys",
|
||||
"winapi 0.2.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pnet_macros"
|
||||
version = "0.25.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf402424ca7281aa234b726c32bce5a8e2278c72f5863305e291ac3de08e16f8"
|
||||
dependencies = [
|
||||
"regex",
|
||||
"syntex",
|
||||
"syntex_syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pnet_macros_support"
|
||||
version = "0.25.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e586854ba703c15f74c486e1a46624566b47f1f61cc8a6b02c6bbe5e34a383b"
|
||||
dependencies = [
|
||||
"pnet_base",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pnet_packet"
|
||||
version = "0.25.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c44c075c6d4f2e814dba621a999838e4a4f749f6117024f52b05b3c559a4fd17"
|
||||
dependencies = [
|
||||
"glob",
|
||||
"pnet_base",
|
||||
"pnet_macros",
|
||||
"pnet_macros_support",
|
||||
"syntex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pnet_sys"
|
||||
version = "0.25.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "82f881a6d75ac98c5541db6144682d1773bb14c6fc50c6ebac7086c8f7f23c29"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi 0.2.8",
|
||||
"ws2_32-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pnet_transport"
|
||||
version = "0.25.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b75ccaee7b5daba9f9a7d47bceeb73cc32edde9952dc5409460d6621ec667b6"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"pnet_base",
|
||||
"pnet_packet",
|
||||
"pnet_sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3acb317c6ff86a4e579dfa00fc5e6cca91ecbb4e7eb2df0468805b674eb88548"
|
||||
dependencies = [
|
||||
"unicode-xid 0.2.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "python3-sys"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3a8ebed3f1201fda179f3960609dbbc10cd8c75e9f2afcb03788278f367d8ea"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.56"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84"
|
||||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.3.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"redox_syscall",
|
||||
"rust-argon2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee84f70c8c08744ea9641a731c7fadb475bf2ecc52d7f627feb833e0b3990467"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
"thread_local",
|
||||
"utf8-ranges",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e734e891f5b408a29efbf8309e656876276f49ab6a6ac208600b4419bd893d90"
|
||||
|
||||
[[package]]
|
||||
name = "rust-argon2"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"blake2b_simd",
|
||||
"constant_time_eq",
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-serialize"
|
||||
version = "0.3.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bfa8506c1de11c9c4e4c38863ccbe02a305c8188e85a05a784c9e11e1c3910c8"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "414115f25f818d7dfccec8ee535d76949ae78584fc4f79a6f45a904bf8ab4449"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.104"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "128f9e303a5a29922045a830221b8f78ec74a5f544944f3d5984f8ec3895ef64"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eab8f15f15d6c41a154c1b128a22f2dfabe350ef53c40953d84e36155c91192b"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "simplelog"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa9c948a5a26cd38340ddbeaa557a8c8a5ce4442408eb60453bee2bb3c84a3fb"
|
||||
dependencies = [
|
||||
"chrono 0.4.10",
|
||||
"log 0.4.8",
|
||||
"term 0.5.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af6f3550d8dff9ef7dc34d384ac6f107e5d31c8f57d9f28e0081503f547ac8f5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid 0.2.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syntex"
|
||||
version = "0.42.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0a30b08a6b383a22e5f6edc127d169670d48f905bb00ca79a00ea3e442ebe317"
|
||||
dependencies = [
|
||||
"syntex_errors",
|
||||
"syntex_syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syntex_errors"
|
||||
version = "0.42.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04c48f32867b6114449155b2a82114b86d4b09e1bddb21c47ff104ab9172b646"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"log 0.3.9",
|
||||
"rustc-serialize",
|
||||
"syntex_pos",
|
||||
"term 0.4.6",
|
||||
"unicode-xid 0.0.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syntex_pos"
|
||||
version = "0.42.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3fd49988e52451813c61fecbe9abb5cfd4e1b7bb6cdbb980a6fbcbab859171a6"
|
||||
dependencies = [
|
||||
"rustc-serialize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syntex_syntax"
|
||||
version = "0.42.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7628a0506e8f9666fdabb5f265d0059b059edac9a3f810bda077abb5d826bd8d"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"libc",
|
||||
"log 0.3.9",
|
||||
"rustc-serialize",
|
||||
"syntex_errors",
|
||||
"syntex_pos",
|
||||
"term 0.4.6",
|
||||
"unicode-xid 0.0.3",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "term"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa63644f74ce96fbeb9b794f66aff2a52d601cbd5e80f4b97123e3899f4570f1"
|
||||
dependencies = [
|
||||
"kernel32-sys",
|
||||
"winapi 0.2.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "term"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"dirs",
|
||||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.42"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "timer"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9522a9ec40055e2f9e514e38d2415a496e81dbfc1ece15d98d2fe55c44946b3"
|
||||
dependencies = [
|
||||
"chrono 0.2.25",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36dff09cafb4ec7c8cf0023eb0b686cb6ce65499116a12201c9e11840ca01beb"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
|
||||
|
||||
[[package]]
|
||||
name = "utf8-ranges"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.9.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-build"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
|
||||
|
||||
[[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 = "ws2_32-sys"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e"
|
||||
dependencies = [
|
||||
"winapi 0.2.8",
|
||||
"winapi-build",
|
||||
]
|
||||
@@ -1,33 +0,0 @@
|
||||
[package]
|
||||
name = "libaostats"
|
||||
version = "0.1.0"
|
||||
authors = ["Wiktor Mazur <wiktormazur1@gmail.com>"]
|
||||
edition = "2018"
|
||||
build = "build.rs"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[lib]
|
||||
name = "libaostats"
|
||||
path = "src/lib_python.rs"
|
||||
crate-type = ["dylib"]
|
||||
|
||||
[dependencies.cpython]
|
||||
version = "0.3"
|
||||
|
||||
[dependencies]
|
||||
packet_sniffer = { path = "./lib/packet-sniffer" }
|
||||
photon_decode = { path = "./lib/photon-decode" }
|
||||
bytes = "0.4"
|
||||
simplelog = "0.6"
|
||||
timer = "0.1"
|
||||
chrono = "0.4"
|
||||
log = { version = "0.4", features = ["std"] }
|
||||
lazy_static = "1.2"
|
||||
derive_more = "0.99"
|
||||
|
||||
[build-dependencies]
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
|
||||
[dev-dependencies]
|
||||
fake_clock = "0.3.0"
|
||||
@@ -1 +0,0 @@
|
||||
from libaostats import * # type: ignore
|
||||
@@ -1,138 +0,0 @@
|
||||
macro_rules! decode_number {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::Short(v) => Some(*v as usize),
|
||||
Value::Integer(v) => Some(*v as usize),
|
||||
Value::Byte(v) => Some(*v as usize),
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error!("Index {} not found in {}", $index, $name);
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! decode_string {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::String(v) => Some(v.clone()),
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(unused_macros)]
|
||||
macro_rules! decode_string_vec {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::Array(arr) => {
|
||||
let mut ret = vec![];
|
||||
for v in arr {
|
||||
if let Value::String(s) = v {
|
||||
ret.push(s.clone());
|
||||
}
|
||||
}
|
||||
|
||||
Some(ret)
|
||||
}
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! decode_number_vec {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::Array(arr) => {
|
||||
let mut ret = vec![];
|
||||
for v in arr {
|
||||
match v {
|
||||
Value::Short(v) => {
|
||||
ret.push(*v as u32);
|
||||
},
|
||||
Value::Byte(v) => {
|
||||
ret.push(*v as u32);
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Some(ret)
|
||||
},
|
||||
Value::ByteArray(v) => {
|
||||
Some(v.iter().map(|b| *b as u32).collect::<Vec<u32>>())
|
||||
},
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! decode_float {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::Float(v) => Some(*v as f32),
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
macro_rules! decode_vec_of_number_vec {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::Array(v) => {
|
||||
let mut ret = vec![];
|
||||
for i in v {
|
||||
let mut params = Parameters::new();
|
||||
params.insert(0, i.clone());
|
||||
let item = decode_number_vec!(params, 0, $name)?;
|
||||
ret.push(item);
|
||||
}
|
||||
|
||||
Some(ret)
|
||||
},
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,319 +0,0 @@
|
||||
{
|
||||
"events": [
|
||||
{
|
||||
"name": "Leave",
|
||||
"code": 1,
|
||||
"params": [
|
||||
{
|
||||
"name": "source",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "HealthUpdate",
|
||||
"code": 6,
|
||||
"params": [
|
||||
{
|
||||
"name": "source",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "target",
|
||||
"id": 6,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "value",
|
||||
"id": 2,
|
||||
"param_type": "Float"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "RegenerationHealthChanged",
|
||||
"code": 81,
|
||||
"params": [
|
||||
{
|
||||
"name": "source",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "health",
|
||||
"id": 2,
|
||||
"param_type": "Float"
|
||||
},
|
||||
{
|
||||
"name": "max_health",
|
||||
"id": 3,
|
||||
"param_type": "Float"
|
||||
},
|
||||
{
|
||||
"name": "regeneration_rate",
|
||||
"id": 4,
|
||||
"param_type": "Float",
|
||||
"optional": true
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "KnockedDown",
|
||||
"code": 154,
|
||||
"params": [
|
||||
{
|
||||
"name": "source",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "target",
|
||||
"id": 3,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "target_name",
|
||||
"id": 4,
|
||||
"param_type": "String"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "NewCharacter",
|
||||
"code": 25,
|
||||
"params": [
|
||||
{
|
||||
"name": "source",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "character_name",
|
||||
"id": 1,
|
||||
"param_type": "String"
|
||||
},
|
||||
{
|
||||
"name": "health",
|
||||
"id": 18,
|
||||
"param_type": "Float"
|
||||
},
|
||||
{
|
||||
"name": "max_health",
|
||||
"id": 19,
|
||||
"param_type": "Float"
|
||||
},
|
||||
{
|
||||
"name": "energy",
|
||||
"id": 22,
|
||||
"param_type": "Float"
|
||||
},
|
||||
{
|
||||
"name": "max_energy",
|
||||
"id": 23,
|
||||
"param_type": "Float"
|
||||
},
|
||||
{
|
||||
"name": "items",
|
||||
"id": 33,
|
||||
"param_type": "Items"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "UpdateFame",
|
||||
"code": 73,
|
||||
"params": [
|
||||
{
|
||||
"name": "source",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "fame",
|
||||
"id": 2,
|
||||
"param_type": "Number"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "CharacterEquipmentChanged",
|
||||
"code": 80,
|
||||
"params": [
|
||||
{
|
||||
"name": "source",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "items",
|
||||
"id": 2,
|
||||
"param_type": "Items"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyInvitation",
|
||||
"code": 212,
|
||||
"params": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyJoined",
|
||||
"code": 213,
|
||||
"params": [
|
||||
{
|
||||
"name": "party_id",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "party_structures",
|
||||
"id": 4,
|
||||
"param_type": "ListOfNumberList"
|
||||
},
|
||||
{
|
||||
"name": "character_names",
|
||||
"id": 5,
|
||||
"param_type": "StringList"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyDisbanded",
|
||||
"code": 214,
|
||||
"params": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyPlayerJoined",
|
||||
"code": 215,
|
||||
"params": [
|
||||
{
|
||||
"name": "party_id",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "party_structure",
|
||||
"id": 1,
|
||||
"param_type": "NumberList"
|
||||
},
|
||||
{
|
||||
"name": "name",
|
||||
"id": 2,
|
||||
"param_type": "String"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyChangedOrder",
|
||||
"code": 216,
|
||||
"params": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyPlayerLeft",
|
||||
"code": 217,
|
||||
"params": [
|
||||
{
|
||||
"name": "party_id",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "party_structure",
|
||||
"id": 1,
|
||||
"param_type": "NumberList"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyLeaderChanged",
|
||||
"code": 218,
|
||||
"params": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyLootSettingChangedPlayer",
|
||||
"code": 219,
|
||||
"params": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartySilverGained",
|
||||
"code": 220,
|
||||
"params": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyPlayerUpdated",
|
||||
"code": 221,
|
||||
"params": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyInvitationPlayerBusy",
|
||||
"code": 222,
|
||||
"params": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyMarkedObjectsUpdated",
|
||||
"code": 223,
|
||||
"params": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartyOnClusterPartyJoined",
|
||||
"code": 224,
|
||||
"params": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "PartySetRoleFlag",
|
||||
"code": 225,
|
||||
"params": [
|
||||
]
|
||||
}
|
||||
],
|
||||
"responses": [
|
||||
{
|
||||
"name": "Join",
|
||||
"code": 2,
|
||||
"params": [
|
||||
{
|
||||
"name": "source",
|
||||
"id": 0,
|
||||
"param_type": "Number"
|
||||
},
|
||||
{
|
||||
"name": "character_name",
|
||||
"id": 2,
|
||||
"param_type": "String"
|
||||
},
|
||||
{
|
||||
"name": "health",
|
||||
"id": 11,
|
||||
"param_type": "Float"
|
||||
},
|
||||
{
|
||||
"name": "max_health",
|
||||
"id": 12,
|
||||
"param_type": "Float"
|
||||
},
|
||||
{
|
||||
"name": "energy",
|
||||
"id": 15,
|
||||
"param_type": "Float"
|
||||
},
|
||||
{
|
||||
"name": "max_energy",
|
||||
"id": 16,
|
||||
"param_type": "Float"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,220 +0,0 @@
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
use serde::{Deserialize};
|
||||
|
||||
#[derive(PartialEq, Clone, Deserialize)]
|
||||
enum ParamType
|
||||
{
|
||||
Number,
|
||||
Float,
|
||||
String,
|
||||
Items,
|
||||
StringList,
|
||||
NumberList,
|
||||
ListOfNumberList,
|
||||
}
|
||||
|
||||
impl ParamType {
|
||||
fn rust_type(&self) -> &'static str {
|
||||
match self {
|
||||
ParamType::Number => "usize",
|
||||
ParamType::Float => "f32",
|
||||
ParamType::String => "String",
|
||||
ParamType::Items => "Items",
|
||||
ParamType::StringList => "Vec<String>",
|
||||
ParamType::NumberList => "Vec<u32>",
|
||||
ParamType::ListOfNumberList => "Vec<Vec<u32>>",
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_macro(&self) -> &'static str {
|
||||
match self {
|
||||
ParamType::Number => "decode_number!",
|
||||
ParamType::Float => "decode_float!",
|
||||
ParamType::String => "decode_string!",
|
||||
ParamType::Items => "decode_number_vec!",
|
||||
ParamType::StringList => "decode_string_vec!",
|
||||
ParamType::NumberList => "decode_number_vec!",
|
||||
ParamType::ListOfNumberList => "decode_vec_of_number_vec!",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
struct Param
|
||||
{
|
||||
name: String,
|
||||
id: u32,
|
||||
param_type: ParamType,
|
||||
optional: Option<bool>
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
struct Message
|
||||
{
|
||||
name: String,
|
||||
code: u32,
|
||||
params: Vec<Param>
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
struct Messages
|
||||
{
|
||||
events: Vec<Message>,
|
||||
responses: Vec<Message>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
save_file("itemdb.rs", &generate_itemdb());
|
||||
save_file("messages.rs", &generate_messages());
|
||||
}
|
||||
|
||||
fn generate_messages() -> String {
|
||||
let mut out = String::new();
|
||||
|
||||
out.push_str(r"use log::*;
|
||||
use crate::photon_messages::Items;
|
||||
use photon_decode::Parameters;
|
||||
use photon_decode::Value;
|
||||
|
||||
");
|
||||
|
||||
out.push_str(include_str!("assets/decode_macros.rs"));
|
||||
let messages: Messages = serde_json::from_str(include_str!("assets/messages.json")).expect("Missing assets/messages.json");
|
||||
|
||||
for msg in &[&messages.events[..], &messages.responses[..]].concat() {
|
||||
let mut struct_params = String::new();
|
||||
for param in &msg.params {
|
||||
if param.optional.is_some() {
|
||||
struct_params.push_str(&format!(" pub {}: Option<{}>,\n", param.name, param.param_type.rust_type()));
|
||||
} else {
|
||||
struct_params.push_str(&format!(" pub {}: {},\n", param.name, param.param_type.rust_type()));
|
||||
}
|
||||
}
|
||||
|
||||
out.push_str(&format!(r###"
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct {} {{
|
||||
{}
|
||||
}}
|
||||
"###, msg.name, struct_params));
|
||||
|
||||
|
||||
let mut parse_body = String::new();
|
||||
for param in &msg.params {
|
||||
if param.param_type == ParamType::Items {
|
||||
parse_body.push_str(&format!(" let {} = {}(val, {}, \"{}::{0}\")?;\n", "item_array", param.param_type.parse_macro(), param.id, msg.name));
|
||||
parse_body.push_str(&format!(" let items = item_array.into();\n"));
|
||||
} else {
|
||||
if param.optional.is_some() {
|
||||
parse_body.push_str(&format!(" let {} = {}(val, {}, \"{}::{0}\");\n", param.name, param.param_type.parse_macro(), param.id, msg.name));
|
||||
} else {
|
||||
parse_body.push_str(&format!(" let {} = {}(val, {}, \"{}::{0}\")?;\n", param.name, param.param_type.parse_macro(), param.id, msg.name));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
let mut param_names = String::new();
|
||||
for param in &msg.params {
|
||||
param_names.push_str(&format!("{}, ", param.name));
|
||||
}
|
||||
|
||||
out.push_str(&format!(r###"
|
||||
impl {0} {{
|
||||
pub fn parse(val: Parameters) -> Option<Message> {{
|
||||
info!("{0} parameters: {{:?}}", val);
|
||||
{1}
|
||||
|
||||
Some(Message::{0}({0} {{ {2} }}))
|
||||
}}
|
||||
}}
|
||||
"###, msg.name, parse_body, param_names));
|
||||
}
|
||||
|
||||
out.push_str("\n#[derive(Debug, Clone, PartialEq)]\n");
|
||||
out.push_str("pub enum Message {\n");
|
||||
for msg in &[&messages.events[..], &messages.responses[..]].concat() {
|
||||
out.push_str(&format!(" {0}({0}),\n", msg.name));
|
||||
}
|
||||
out.push_str("}");
|
||||
|
||||
let mut event_matches = String::new();
|
||||
let mut responses_matches = String::new();
|
||||
|
||||
for msg in &messages.events {
|
||||
event_matches.push_str(&format!(" Some(photon_decode::Value::Short({})) => {}::parse(parameters),\n", msg.code, msg.name));
|
||||
}
|
||||
|
||||
for msg in &messages.responses {
|
||||
responses_matches.push_str(&format!(" Some(photon_decode::Value::Short({})) => {}::parse(parameters),\n", msg.code, msg.name));
|
||||
}
|
||||
|
||||
out.push_str(&format!(r###"
|
||||
|
||||
pub fn into_game_message(photon_message: photon_decode::Message) -> Option<Message> {{
|
||||
debug!("Raw photon : {{:?}}", photon_message);
|
||||
match photon_message {{
|
||||
photon_decode::Message::Event(photon_decode::EventData{{
|
||||
code: 1,
|
||||
parameters
|
||||
}}) => {{
|
||||
match parameters.get(&252u8) {{
|
||||
{}
|
||||
_ => None
|
||||
}}
|
||||
}},
|
||||
photon_decode::Message::Request(photon_decode::OperationRequest{{
|
||||
code: 1,
|
||||
parameters
|
||||
}}) => {{
|
||||
match parameters.get(&253u8) {{
|
||||
_ => None
|
||||
}}
|
||||
}},
|
||||
photon_decode::Message::Response(photon_decode::OperationResponse{{
|
||||
code: 1,
|
||||
parameters,
|
||||
return_code: _,
|
||||
debug_message: _
|
||||
}}) => {{
|
||||
match parameters.get(&253u8) {{
|
||||
{}
|
||||
_ => None
|
||||
}}
|
||||
}},
|
||||
_ => None
|
||||
}}
|
||||
}}
|
||||
"###, event_matches, responses_matches));
|
||||
out
|
||||
}
|
||||
|
||||
fn generate_itemdb() -> String {
|
||||
let mut out = String::new();
|
||||
|
||||
out.push_str("use std::collections::HashMap;\n\n");
|
||||
out.push_str("lazy_static! {\n");
|
||||
out.push_str(" pub static ref ITEMDB: HashMap<u32, &'static str> = {[\n");
|
||||
|
||||
include_str!("assets/item_ids.txt").split('\n').filter_map(|line| {
|
||||
let v: Vec<&str> = line.split(',').collect();
|
||||
let id : u32 = v.get(0)?.parse().ok()?;
|
||||
let item = v.get(1)?.to_owned();
|
||||
Some((id, item))
|
||||
}).for_each(|(id, item)| {
|
||||
out.push_str(&format!(" ({}, \"{}\"),\n", id, item))
|
||||
});
|
||||
out.push_str(" ].iter().cloned().collect()};\n");
|
||||
out.push_str("}");
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
fn save_file(file_name: &str, content: &str) {
|
||||
let dest_path = Path::new(&"src/photon_messages").join(file_name);
|
||||
let mut f = File::create(&dest_path).unwrap();
|
||||
f.write_all(content.as_bytes()).unwrap();
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
[package]
|
||||
name = "packet_sniffer"
|
||||
version = "0.1.0"
|
||||
authors = ["Wiktor Mazur <wiktormazur1@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
pnet = "0.25.0"
|
||||
log = { version = "0.4", features = ["std"] }
|
||||
|
||||
[profile.dev]
|
||||
panic = "unwind"
|
||||
|
||||
[profile.release]
|
||||
panic = "unwind"
|
||||
@@ -1,209 +0,0 @@
|
||||
extern crate pnet;
|
||||
|
||||
use std::net::IpAddr;
|
||||
use std::panic;
|
||||
use std::sync::mpsc::Sender;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
|
||||
use pnet::datalink::{self, NetworkInterface};
|
||||
use pnet::packet::ethernet::{EtherTypes, EthernetPacket, MutableEthernetPacket};
|
||||
use pnet::packet::ip::{IpNextHeaderProtocol, IpNextHeaderProtocols};
|
||||
use pnet::packet::ipv4::Ipv4Packet;
|
||||
use pnet::packet::ipv6::Ipv6Packet;
|
||||
use pnet::packet::udp;
|
||||
use pnet::packet::Packet;
|
||||
use pnet::util::MacAddr;
|
||||
|
||||
use log::*;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct UdpPacket {
|
||||
pub interface_name: String,
|
||||
pub source_address: IpAddr,
|
||||
pub source_port: u16,
|
||||
pub destination_address: IpAddr,
|
||||
pub destination_port: u16,
|
||||
pub length: u16,
|
||||
pub payload: Vec<u8>,
|
||||
}
|
||||
|
||||
pub type NetworkInterfaces = Vec<pnet::datalink::NetworkInterface>;
|
||||
|
||||
pub fn network_interfaces() -> Result<NetworkInterfaces, &'static str> {
|
||||
let result = panic::catch_unwind(|| {
|
||||
datalink::interfaces()
|
||||
.into_iter()
|
||||
.filter(|i| !i.is_loopback())
|
||||
.collect::<NetworkInterfaces>()
|
||||
});
|
||||
|
||||
match result {
|
||||
Ok(interfaces) => Ok(interfaces),
|
||||
Err(_) => Err("Unable to get interface list"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn receive(interfaces: NetworkInterfaces, tx: Sender<UdpPacket>) {
|
||||
let shared_tx = Arc::new(Mutex::new(tx));
|
||||
info!("Found interfaces: {:?}", interfaces);
|
||||
|
||||
for interface in interfaces {
|
||||
let tx = shared_tx.clone();
|
||||
let config = pnet::datalink::Config {
|
||||
write_buffer_size: 65536,
|
||||
read_buffer_size: 65536,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Create a channel to receive on
|
||||
let (_, mut rx) = match datalink::channel(&interface, config) {
|
||||
Ok(pnet::datalink::Channel::Ethernet(tx, rx)) => (tx, rx),
|
||||
Ok(_) => {
|
||||
warn!("packetdump: unhandled channel type");
|
||||
continue;
|
||||
}
|
||||
Err(e) => {
|
||||
warn!("packetdump: unable to create channel: {}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
thread::spawn(move || {
|
||||
loop {
|
||||
let mut buf: [u8; 1600] = [0u8; 1600];
|
||||
let mut fake_ethernet_frame = MutableEthernetPacket::new(&mut buf[..]).unwrap();
|
||||
match rx.next() {
|
||||
Ok(packet) => {
|
||||
if cfg!(target_os = "macos")
|
||||
&& interface.is_up()
|
||||
&& !interface.is_broadcast()
|
||||
&& !interface.is_loopback()
|
||||
&& interface.is_point_to_point()
|
||||
{
|
||||
// Maybe is TUN interface
|
||||
let version = Ipv4Packet::new(&packet).unwrap().get_version();
|
||||
if version == 4 {
|
||||
fake_ethernet_frame.set_destination(MacAddr(0, 0, 0, 0, 0, 0));
|
||||
fake_ethernet_frame.set_source(MacAddr(0, 0, 0, 0, 0, 0));
|
||||
fake_ethernet_frame.set_ethertype(EtherTypes::Ipv4);
|
||||
fake_ethernet_frame.set_payload(&packet);
|
||||
handle_ethernet_frame(
|
||||
&interface,
|
||||
&fake_ethernet_frame.to_immutable(),
|
||||
&tx,
|
||||
);
|
||||
continue;
|
||||
} else if version == 6 {
|
||||
fake_ethernet_frame.set_destination(MacAddr(0, 0, 0, 0, 0, 0));
|
||||
fake_ethernet_frame.set_source(MacAddr(0, 0, 0, 0, 0, 0));
|
||||
fake_ethernet_frame.set_ethertype(EtherTypes::Ipv6);
|
||||
fake_ethernet_frame.set_payload(&packet);
|
||||
handle_ethernet_frame(
|
||||
&interface,
|
||||
&fake_ethernet_frame.to_immutable(),
|
||||
&tx,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
handle_ethernet_frame(
|
||||
&interface,
|
||||
&EthernetPacket::new(packet).unwrap(),
|
||||
&tx,
|
||||
);
|
||||
}
|
||||
Err(e) => warn!("packetdump: unable to receive packet: {}", e),
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_ethernet_frame(
|
||||
interface: &NetworkInterface,
|
||||
ethernet: &EthernetPacket,
|
||||
tx: &Arc<Mutex<Sender<UdpPacket>>>,
|
||||
) {
|
||||
let interface_name = &interface.name[..];
|
||||
match ethernet.get_ethertype() {
|
||||
EtherTypes::Ipv4 => handle_ipv4_packet(interface_name, ethernet, &tx),
|
||||
EtherTypes::Ipv6 => handle_ipv6_packet(interface_name, ethernet, &tx),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_ipv4_packet(
|
||||
interface_name: &str,
|
||||
ethernet: &EthernetPacket,
|
||||
tx: &Arc<Mutex<Sender<UdpPacket>>>,
|
||||
) {
|
||||
let header = Ipv4Packet::new(ethernet.payload());
|
||||
if let Some(header) = header {
|
||||
handle_transport_protocol(
|
||||
interface_name,
|
||||
IpAddr::V4(header.get_source()),
|
||||
IpAddr::V4(header.get_destination()),
|
||||
header.get_next_level_protocol(),
|
||||
header.payload(),
|
||||
&tx,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_ipv6_packet(
|
||||
interface_name: &str,
|
||||
ethernet: &EthernetPacket,
|
||||
tx: &Arc<Mutex<Sender<UdpPacket>>>,
|
||||
) {
|
||||
let header = Ipv6Packet::new(ethernet.payload());
|
||||
if let Some(header) = header {
|
||||
handle_transport_protocol(
|
||||
interface_name,
|
||||
IpAddr::V6(header.get_source()),
|
||||
IpAddr::V6(header.get_destination()),
|
||||
header.get_next_header(),
|
||||
header.payload(),
|
||||
&tx,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_transport_protocol(
|
||||
interface_name: &str,
|
||||
source: IpAddr,
|
||||
destination: IpAddr,
|
||||
protocol: IpNextHeaderProtocol,
|
||||
packet: &[u8],
|
||||
tx: &Arc<Mutex<Sender<UdpPacket>>>,
|
||||
) {
|
||||
match protocol {
|
||||
IpNextHeaderProtocols::Udp => {
|
||||
handle_udp_packet(interface_name, source, destination, packet, &tx)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_udp_packet(
|
||||
interface_name: &str,
|
||||
source: IpAddr,
|
||||
destination: IpAddr,
|
||||
packet: &[u8],
|
||||
tx: &Arc<Mutex<Sender<UdpPacket>>>,
|
||||
) {
|
||||
let udp = udp::UdpPacket::new(packet);
|
||||
let tx = tx.lock().unwrap();
|
||||
if let Some(udp) = udp {
|
||||
tx.send(UdpPacket {
|
||||
interface_name: String::from(interface_name),
|
||||
source_address: source,
|
||||
source_port: udp.get_source(),
|
||||
destination_address: destination,
|
||||
destination_port: udp.get_destination(),
|
||||
length: udp.get_length(),
|
||||
payload: Vec::from(udp.payload()),
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
&.rs.bk
|
||||
/target
|
||||
*.so
|
||||
.vscode
|
||||
@@ -1,48 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "0.4.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iovec"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.60"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "photon_decode"
|
||||
version = "0.2.1"
|
||||
dependencies = [
|
||||
"bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[metadata]
|
||||
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
|
||||
"checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c"
|
||||
"checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08"
|
||||
"checksum libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "d44e80633f007889c7eff624b709ab43c92d708caad982295768a7b13ca3b5eb"
|
||||
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
|
||||
@@ -1,15 +0,0 @@
|
||||
[package]
|
||||
name = "photon_decode"
|
||||
version = "0.2.1"
|
||||
authors = ["Wiktor Mazur <wiktormazur1@gmail.com>"]
|
||||
edition = "2018"
|
||||
description = "Set of utilities for decoding messages generated by photon framework."
|
||||
repository = "https://github.com/mazurwiktor/photon_decode"
|
||||
license = "MIT/Apache-2.0"
|
||||
readme = "README.md"
|
||||
documentation = "https://docs.rs/photon_decode/*/photon_decode/"
|
||||
categories = ["network-programming", "encoding"]
|
||||
keywords = ["games", "multiplayer", "photon", "unity", "unrealengine"]
|
||||
|
||||
[dependencies]
|
||||
bytes = "0.4"
|
||||
@@ -1,59 +0,0 @@
|
||||
Photon decode
|
||||
=============
|
||||
|
||||
Set of utilities for decoding messages generated by *photon framework.
|
||||
|
||||
*Photon is a development framework for multiplayer games. More on https://www.photonengine.com/.
|
||||
|
||||
[](https://docs.rs/photon_decode/*/photon_decode/)
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
To use `photon_decode`, first add this to your `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
photon_decode = "0.1"
|
||||
```
|
||||
|
||||
|
||||
```rust
|
||||
use photon_decode::{Photon, Message};
|
||||
|
||||
let mut photon = Photon::new();
|
||||
let photon_packet = vec![
|
||||
0x00, 0x01, // PeerID
|
||||
0x01, // CrcEnabled
|
||||
0x00, // CommandCount
|
||||
0x00, 0x00, 0x00, 0x01, // Timestamp
|
||||
0x00, 0x00, 0x00, 0x01, // Challenge
|
||||
];
|
||||
|
||||
for message in photon.decode(&photon_packet).iter() {
|
||||
match message {
|
||||
Message::Event(_) => {
|
||||
// use event
|
||||
},
|
||||
Message::Request(_) => {
|
||||
// use request
|
||||
},
|
||||
Message::Response(_) => {
|
||||
// use response
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
# Projects using photon decode
|
||||
|
||||
https://github.com/mazurwiktor/albion-online-stats - Albion online stats is an extension to MMORPG game - Albion Online. It tracks network traffic and displays various statistics, such as damage and DPS meter calculated from in-game actions.
|
||||
|
||||
# License
|
||||
Licensed under either of
|
||||
|
||||
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
||||
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||
|
||||
at your option.
|
||||
@@ -1,472 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io::{Cursor, Read};
|
||||
use std::mem::size_of;
|
||||
|
||||
use bytes::Buf;
|
||||
|
||||
use crate::error::*;
|
||||
use crate::layout::*;
|
||||
|
||||
pub type PhotonCursor<'a> = Cursor<&'a [u8]>;
|
||||
|
||||
pub trait Decode<T> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<T>;
|
||||
}
|
||||
|
||||
trait TypedDecode {
|
||||
fn typed_decode(&mut self, type_code: u8) -> PhotonDecodeResult<Value>;
|
||||
}
|
||||
|
||||
macro_rules! impl_decode {
|
||||
($type:ty, $decode_func:ident, $bytes_to_consume:expr) => {
|
||||
impl Decode<$type> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<$type> {
|
||||
let v = if self.remaining() >= $bytes_to_consume {
|
||||
self.$decode_func()
|
||||
} else {
|
||||
return Err(PhotonDecodeError::from(concat!(
|
||||
"Failed to decode ",
|
||||
stringify!($type),
|
||||
", not enough bytes"
|
||||
)));
|
||||
};
|
||||
Ok(v)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_decode!(u8, get_u8, 1);
|
||||
impl_decode!(f32, get_f32_be, 4);
|
||||
impl_decode!(u32, get_u32_be, 4);
|
||||
impl_decode!(i64, get_i64_be, 8);
|
||||
impl_decode!(i16, get_i16_be, 2);
|
||||
impl_decode!(f64, get_f64_be, 8);
|
||||
|
||||
impl Decode<bool> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<bool> {
|
||||
let v = if self.remaining() >= 1 {
|
||||
self.get_u8()
|
||||
} else {
|
||||
return Err(PhotonDecodeError::from(
|
||||
"Failed to decode bool, not enough bytes",
|
||||
));
|
||||
};
|
||||
Ok(v != 0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<String> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<String> {
|
||||
let size: i16 = self.decode()?;
|
||||
if size < 0 {
|
||||
return Err(PhotonDecodeError::from(
|
||||
"Failed to decode String, unreasonable size",
|
||||
));
|
||||
}
|
||||
|
||||
let mut local_buffer = vec![0; size as usize];
|
||||
if let Ok(_) = self.read_exact(&mut local_buffer[..]) {
|
||||
if let Ok(s) = String::from_utf8(local_buffer) {
|
||||
return Ok(s);
|
||||
}
|
||||
}
|
||||
|
||||
Err(PhotonDecodeError::from(
|
||||
"Failed to decode String, not enough bytes",
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<Vec<String>> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<Vec<String>> {
|
||||
let size: i16 = self.decode()?;
|
||||
if size < 0 {
|
||||
return Err(PhotonDecodeError::from(
|
||||
"Failed to decode String, unreasonable size",
|
||||
));
|
||||
}
|
||||
let mut value: Vec<String> = vec![];
|
||||
for _ in 0..size {
|
||||
value.push(self.decode()?);
|
||||
}
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<Vec<u8>> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<Vec<u8>> {
|
||||
let size: u32 = self.decode()?;
|
||||
let mut value: Vec<u8> = vec![];
|
||||
for _ in 0..size {
|
||||
value.push(self.decode()?);
|
||||
}
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<Vec<Value>> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<Vec<Value>> {
|
||||
let size: i16 = self.decode()?;
|
||||
if size < 0 {
|
||||
return Err(PhotonDecodeError::from(
|
||||
"Failed to decode Vec<Value>, unreasonable size",
|
||||
));
|
||||
}
|
||||
let type_code: u8 = self.decode()?;
|
||||
|
||||
let mut value: Vec<Value> = vec![];
|
||||
for _ in 0..size {
|
||||
if let Ok(v) = self.typed_decode(type_code) {
|
||||
value.push(v);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<HashMap<String, Value>> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<HashMap<String, Value>> {
|
||||
let key_type_code: u8 = self.decode()?;
|
||||
let value_type_code: u8 = self.decode()?;
|
||||
let size: i16 = self.decode()?;
|
||||
if size < 0 {
|
||||
return Err(PhotonDecodeError::from(
|
||||
"Failed to decode HashMap<String, Value>, unreasonable size",
|
||||
));
|
||||
}
|
||||
|
||||
let mut value: HashMap<String, Value> = HashMap::new();
|
||||
for _ in 0..size {
|
||||
let key_code: u8 = if key_type_code == 0 || key_type_code == 42 {
|
||||
self.decode()?
|
||||
} else {
|
||||
key_type_code
|
||||
};
|
||||
|
||||
let key = self.typed_decode(key_code);
|
||||
|
||||
let value_code: u8 = if value_type_code == 0 || value_type_code == 42 {
|
||||
self.decode()?
|
||||
} else {
|
||||
value_type_code
|
||||
};
|
||||
let val = self.typed_decode(value_code);
|
||||
if let (Ok(key), Ok(val)) = (key, val) {
|
||||
value.insert(format!("{}", key), val);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<HashMap<u8, Value>> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<HashMap<u8, Value>> {
|
||||
let size: i16 = self.decode()?;
|
||||
if size < 0 {
|
||||
return Err(PhotonDecodeError::from(
|
||||
"Failed to decode HashMap<u8, Value>, unreasonable size",
|
||||
));
|
||||
}
|
||||
|
||||
let mut value: HashMap<u8, Value> = HashMap::new();
|
||||
for _ in 0..size {
|
||||
let key_type_code: u8 = if let Ok(v) = self.decode() {
|
||||
v
|
||||
} else {
|
||||
break;
|
||||
};
|
||||
let val: Value = if let Ok(v) = self.decode() {
|
||||
v
|
||||
} else {
|
||||
break;
|
||||
};
|
||||
value.insert(key_type_code, val);
|
||||
}
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<EventData> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<EventData> {
|
||||
let code: u8 = self.decode()?;
|
||||
let parameters: HashMap<u8, Value> = self.decode()?;
|
||||
Ok(EventData { code, parameters })
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<OperationResponse> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<OperationResponse> {
|
||||
let code: u8 = self.decode()?;
|
||||
let return_code: i16 = self.decode()?;
|
||||
let maybe_debug_message: Value = self.decode()?;
|
||||
let debug_message = if let Value::String(s) = maybe_debug_message {
|
||||
s
|
||||
} else {
|
||||
"None".to_owned()
|
||||
};
|
||||
let parameters: HashMap<u8, Value> = self.decode()?;
|
||||
|
||||
Ok(OperationResponse {
|
||||
code,
|
||||
return_code,
|
||||
debug_message,
|
||||
parameters,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<OperationRequest> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<OperationRequest> {
|
||||
let code: u8 = self.decode()?;
|
||||
let parameters: HashMap<u8, Value> = self.decode()?;
|
||||
Ok(OperationRequest { code, parameters })
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<Vec<Box<Value>>> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<Vec<Box<Value>>> {
|
||||
let size: i16 = self.decode()?;
|
||||
if size < 0 {
|
||||
return Err(PhotonDecodeError::from(
|
||||
"Failed to decode Vec<Box<Value>>, unreasonable size",
|
||||
));
|
||||
}
|
||||
let mut value = vec![];
|
||||
for _ in 0..size {
|
||||
let type_code: u8 = if let Ok(v) = self.decode() {
|
||||
v
|
||||
} else {
|
||||
break;
|
||||
};
|
||||
if let Ok(val) = self.typed_decode(type_code) {
|
||||
value.push(Box::new(val));
|
||||
}
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<Vec<bool>> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<Vec<bool>> {
|
||||
let size: i16 = self.decode()?;
|
||||
if size < 0 {
|
||||
return Err(PhotonDecodeError::from(
|
||||
"Failed to decode Vec<bool>, unreasonable size",
|
||||
));
|
||||
}
|
||||
let mut value = vec![];
|
||||
for _ in 0..size {
|
||||
value.push(self.decode()?);
|
||||
}
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl TypedDecode for PhotonCursor<'_> {
|
||||
fn typed_decode(&mut self, type_code: u8) -> PhotonDecodeResult<Value> {
|
||||
match TypeCode::from(type_code) {
|
||||
TypeCode::None => Ok(Value::None),
|
||||
TypeCode::Null => Ok(Value::None),
|
||||
TypeCode::Boolean => Ok(Value::Boolean(self.decode()?)),
|
||||
TypeCode::Byte => Ok(Value::Byte(self.decode()?)),
|
||||
TypeCode::Double => Ok(Value::Double(self.decode()?)),
|
||||
TypeCode::Float => Ok(Value::Float(self.decode()?)),
|
||||
TypeCode::Integer => Ok(Value::Integer(self.decode()?)),
|
||||
TypeCode::Long => Ok(Value::Long(self.decode()?)),
|
||||
TypeCode::Short => Ok(Value::Short(self.decode()?)),
|
||||
TypeCode::String => Ok(Value::String(self.decode()?)),
|
||||
TypeCode::StringArray => Ok(Value::StringArray(self.decode()?)),
|
||||
TypeCode::ByteArray => Ok(Value::ByteArray(self.decode()?)),
|
||||
TypeCode::Dictionary => Ok(Value::Dictionary(self.decode()?)),
|
||||
TypeCode::EventData => Ok(Value::EventData(self.decode()?)),
|
||||
TypeCode::OperationRequest => Ok(Value::OperationRequest(self.decode()?)),
|
||||
TypeCode::OperationResponse => Ok(Value::OperationResponse(self.decode()?)),
|
||||
TypeCode::BooleanArray => Ok(Value::BooleanArray(self.decode()?)),
|
||||
TypeCode::Array => Ok(Value::Array(self.decode()?)),
|
||||
TypeCode::ObjectArray => Ok(Value::ObjectArray(self.decode()?)),
|
||||
_ => Err(PhotonDecodeError::from(format!(
|
||||
"Failed to decode Value, unknown type code ({:#X})",
|
||||
type_code
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<Value> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<Value> {
|
||||
let type_code: u8 = self.decode()?;
|
||||
self.typed_decode(type_code)
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<PhotonHeader> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<PhotonHeader> {
|
||||
let peer_id = self.decode()?;
|
||||
let crc_enabled = self.decode()?;
|
||||
let command_count = self.decode()?;
|
||||
let timestamp = self.decode()?;
|
||||
let challenge = self.decode()?;
|
||||
|
||||
Ok(PhotonHeader {
|
||||
peer_id,
|
||||
crc_enabled,
|
||||
command_count,
|
||||
timestamp,
|
||||
challenge,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<ReliableCommand> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<ReliableCommand> {
|
||||
let channel_id = self.decode()?;
|
||||
let flags = self.decode()?;
|
||||
let reserved_byte = self.decode()?;
|
||||
let length: u32 = self.decode()?;
|
||||
let reliable_sequence_number = self.decode()?;
|
||||
let msg_len = length.checked_sub(size_of::<ReliableCommand>() as u32)
|
||||
.map_or(Err(PhotonDecodeError::from("Invalid ReliableCommand length")), |v| Ok(v))?;
|
||||
Ok(ReliableCommand {
|
||||
channel_id,
|
||||
flags,
|
||||
reserved_byte,
|
||||
msg_len,
|
||||
reliable_sequence_number,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<UnreliableCommand> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<UnreliableCommand> {
|
||||
let mut reliable_command: ReliableCommand = self.decode()?;
|
||||
let unknown = self.decode()?;
|
||||
reliable_command.msg_len = reliable_command.msg_len.checked_sub(size_of::<u32>() as u32)
|
||||
.map_or(Err(PhotonDecodeError::from("Invalid UnreliableCommand length")), |v| Ok(v))?;
|
||||
Ok(UnreliableCommand {
|
||||
reliable_command,
|
||||
unknown,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<ReliableFragment> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<ReliableFragment> {
|
||||
let mut reliable_command: ReliableCommand = self.decode()?;
|
||||
let sequence_number = self.decode()?;
|
||||
let fragment_count = self.decode()?;
|
||||
let fragment_number = self.decode()?;
|
||||
let total_length = self.decode()?;
|
||||
let operation_length = self.decode()?;
|
||||
|
||||
reliable_command.msg_len = reliable_command.msg_len.checked_sub((size_of::<u32>() * 5) as u32)
|
||||
.map_or(Err(PhotonDecodeError::from("Invalid ReliableFragment length")), |v| Ok(v))?;
|
||||
let mut payload = vec![0u8; reliable_command.msg_len as usize];
|
||||
self.read_exact(&mut payload)
|
||||
.map_err(|e| PhotonDecodeError::from(format!("{}", e)))?;
|
||||
|
||||
Ok(ReliableFragment {
|
||||
reliable_command,
|
||||
sequence_number,
|
||||
fragment_count,
|
||||
fragment_number,
|
||||
total_length,
|
||||
operation_length,
|
||||
payload,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode<Command> for PhotonCursor<'_> {
|
||||
fn decode(&mut self) -> PhotonDecodeResult<Command> {
|
||||
let cmd_type_id: u8 = self.decode()?;
|
||||
match cmd_type_id {
|
||||
4 => Ok(Command::LogOut),
|
||||
6 => Ok(Command::SendReliable(self.decode()?)),
|
||||
7 => Ok(Command::SendUnreliable(
|
||||
Decode::<UnreliableCommand>::decode(self)?.reliable_command,
|
||||
)),
|
||||
8 => Ok(Command::SendReliableFragment(self.decode()?)),
|
||||
_ => Ok(Command::SendReliable(self.decode()?)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum TypeCode {
|
||||
None = 0x00,
|
||||
Null = 0x2A,
|
||||
Dictionary = 0x44,
|
||||
StringArray = 0x61,
|
||||
Byte = 0x62,
|
||||
Double = 0x64,
|
||||
EventData = 0x65,
|
||||
Float = 0x66,
|
||||
Integer = 0x69,
|
||||
Short = 0x6B,
|
||||
Long = 0x6C,
|
||||
BooleanArray = 0x6E,
|
||||
Boolean = 0x6F,
|
||||
OperationResponse = 0x70,
|
||||
OperationRequest = 0x71,
|
||||
String = 0x73,
|
||||
ByteArray = 0x78,
|
||||
Array = 0x79,
|
||||
ObjectArray = 0x7A,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl From<u8> for TypeCode {
|
||||
fn from(v: u8) -> Self {
|
||||
match v {
|
||||
0x00 => TypeCode::None,
|
||||
0x2A => TypeCode::Null,
|
||||
0x44 => TypeCode::Dictionary,
|
||||
0x61 => TypeCode::StringArray,
|
||||
0x62 => TypeCode::Byte,
|
||||
0x64 => TypeCode::Double,
|
||||
0x65 => TypeCode::EventData,
|
||||
0x66 => TypeCode::Float,
|
||||
0x69 => TypeCode::Integer,
|
||||
0x6B => TypeCode::Short,
|
||||
0x6C => TypeCode::Long,
|
||||
0x6E => TypeCode::BooleanArray,
|
||||
0x6F => TypeCode::Boolean,
|
||||
0x70 => TypeCode::OperationResponse,
|
||||
0x71 => TypeCode::OperationRequest,
|
||||
0x73 => TypeCode::String,
|
||||
0x78 => TypeCode::ByteArray,
|
||||
0x79 => TypeCode::Array,
|
||||
0x7A => TypeCode::ObjectArray,
|
||||
_ => TypeCode::Unknown,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Index<usize> for Value {
|
||||
type Output = Value;
|
||||
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
match self {
|
||||
Value::Array(v) => &v[index],
|
||||
_ => panic!("Non indexable type"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Value {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Value::String(v) => write!(f, "{}", v),
|
||||
Value::Byte(v) => write!(f, "{}", v),
|
||||
Value::Integer(v) => write!(f, "{}", v),
|
||||
v => write!(f, "{:?}", v),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
use core::fmt::{self, Display};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PhotonDecodeError {
|
||||
cause: String,
|
||||
}
|
||||
|
||||
impl PhotonDecodeError {
|
||||
pub fn extend(self, cause: String) -> Self {
|
||||
Self {
|
||||
cause: format!("{} {}", cause, self.cause),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type PhotonDecodeResult<T> = std::result::Result<T, PhotonDecodeError>;
|
||||
|
||||
impl Display for PhotonDecodeError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
Display::fmt(&self.cause, f)
|
||||
}
|
||||
}
|
||||
|
||||
// Allows writing `PhotonDecodeError::from("oops"))?`
|
||||
impl From<&'static str> for PhotonDecodeError {
|
||||
fn from(msg: &'static str) -> PhotonDecodeError {
|
||||
PhotonDecodeError { cause: msg.into() }
|
||||
}
|
||||
}
|
||||
|
||||
// Allows writing `PhotonDecodeError::from("oops".into()))?`
|
||||
impl From<String> for PhotonDecodeError {
|
||||
fn from(msg: String) -> PhotonDecodeError {
|
||||
PhotonDecodeError { cause: msg }
|
||||
}
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Value {
|
||||
None,
|
||||
Dictionary(HashMap<String, Value>),
|
||||
StringArray(Vec<String>),
|
||||
Byte(u8),
|
||||
Double(f64),
|
||||
EventData(EventData),
|
||||
Float(f32),
|
||||
Integer(u32),
|
||||
Short(i16),
|
||||
Long(i64),
|
||||
BooleanArray(Vec<bool>),
|
||||
Boolean(bool),
|
||||
OperationResponse(OperationResponse),
|
||||
OperationRequest(OperationRequest),
|
||||
String(String),
|
||||
ByteArray(Vec<u8>),
|
||||
Array(Vec<Value>),
|
||||
Object(Box<Value>),
|
||||
ObjectArray(Vec<Box<Value>>),
|
||||
}
|
||||
|
||||
pub type Parameters = HashMap<u8, Value>;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct EventData {
|
||||
pub code: u8,
|
||||
pub parameters: Parameters,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct OperationResponse {
|
||||
pub code: u8,
|
||||
pub return_code: i16,
|
||||
pub debug_message: String,
|
||||
pub parameters: Parameters,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct OperationRequest {
|
||||
pub code: u8,
|
||||
pub parameters: Parameters,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Message {
|
||||
Request(OperationRequest),
|
||||
Response(OperationResponse),
|
||||
Event(EventData),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct PhotonHeader {
|
||||
pub peer_id: i16,
|
||||
pub crc_enabled: bool,
|
||||
pub command_count: u8,
|
||||
pub timestamp: u32,
|
||||
pub challenge: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ReliableCommand {
|
||||
pub channel_id: u8,
|
||||
pub flags: u8,
|
||||
pub reserved_byte: u8,
|
||||
pub msg_len: u32,
|
||||
pub reliable_sequence_number: u32,
|
||||
}
|
||||
|
||||
pub struct UnreliableCommand {
|
||||
pub reliable_command: ReliableCommand,
|
||||
pub unknown: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ReliableFragment {
|
||||
pub reliable_command: ReliableCommand,
|
||||
pub sequence_number: u32,
|
||||
pub fragment_count: u32,
|
||||
pub fragment_number: u32,
|
||||
pub total_length: u32,
|
||||
pub operation_length: u32,
|
||||
pub payload: Vec<u8>,
|
||||
}
|
||||
|
||||
pub enum Command {
|
||||
LogOut,
|
||||
SendUnreliable(ReliableCommand),
|
||||
SendReliable(ReliableCommand),
|
||||
SendReliableFragment(ReliableFragment),
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
mod decode;
|
||||
mod error;
|
||||
mod layout;
|
||||
|
||||
pub use decode::*;
|
||||
use error::*;
|
||||
pub use layout::*;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryFrom;
|
||||
use std::io::Cursor;
|
||||
|
||||
impl TryFrom<(&ReliableCommand, &mut Cursor<&[u8]>)> for Message {
|
||||
type Error = PhotonDecodeError;
|
||||
|
||||
fn try_from(message: (&ReliableCommand, &mut Cursor<&[u8]>)) -> Result<Self, Self::Error> {
|
||||
let (command, cursor) = message;
|
||||
let _: u8 = cursor.decode()?;
|
||||
let msg_type: u8 = cursor.decode()?;
|
||||
match msg_type {
|
||||
2 => {
|
||||
let v = cursor.decode().map_err(|e| e.extend("Request".into()))?;
|
||||
Ok(Message::Request(v))
|
||||
}
|
||||
3 => {
|
||||
let v = cursor.decode().map_err(|e| e.extend("Response".into()))?;
|
||||
Ok(Message::Response(v))
|
||||
}
|
||||
4 => {
|
||||
let v = cursor.decode().map_err(|e| e.extend("Event".into()))?;
|
||||
Ok(Message::Event(v))
|
||||
}
|
||||
_ => {
|
||||
let msg_header_len = 2;
|
||||
cursor.set_position(cursor.position() + command.msg_len as u64 - msg_header_len);
|
||||
Err(PhotonDecodeError::from(format!(
|
||||
"Unknown message ({:#X})",
|
||||
msg_type
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Photon {
|
||||
fragments: HashMap<u32, Vec<ReliableFragment>>,
|
||||
}
|
||||
|
||||
impl Photon {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
fragments: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_decode(
|
||||
&mut self,
|
||||
payload: &[u8],
|
||||
) -> PhotonDecodeResult<Vec<PhotonDecodeResult<Message>>> {
|
||||
let mut cursor = Cursor::new(payload);
|
||||
Ok(
|
||||
(0..Decode::<PhotonHeader>::decode(&mut cursor)?.command_count)
|
||||
.into_iter()
|
||||
.map(|_| {
|
||||
Decode::<Command>::decode(&mut cursor).map_or_else(
|
||||
|e| Some(Err(e)),
|
||||
|command| match command {
|
||||
Command::SendReliable(c) | Command::SendUnreliable(c) => Some(
|
||||
Message::try_from((&c, &mut cursor))
|
||||
.map_err(|e| e.extend("SendReliable".into())),
|
||||
),
|
||||
Command::SendReliableFragment(fragment) => {
|
||||
self.fragments
|
||||
.entry(fragment.sequence_number)
|
||||
.or_insert(vec![])
|
||||
.push(fragment.clone());
|
||||
self.decode_reliable_fragment(&fragment).map(|msg| {
|
||||
self.fragments.remove(&fragment.sequence_number).unwrap();
|
||||
msg.map_err(|e| e.extend("Fragment".into()))
|
||||
})
|
||||
}
|
||||
_ => None,
|
||||
},
|
||||
)
|
||||
})
|
||||
.filter_map(|v| v)
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn decode(&mut self, payload: &[u8]) -> Vec<Message> {
|
||||
if let Ok(messages) = self.try_decode(payload) {
|
||||
return messages.into_iter().filter_map(Result::ok).collect();
|
||||
}
|
||||
vec![]
|
||||
}
|
||||
|
||||
fn decode_reliable_fragment(
|
||||
&mut self,
|
||||
fragment: &ReliableFragment,
|
||||
) -> Option<PhotonDecodeResult<Message>> {
|
||||
if let Some(fragments) = self.fragments.get(&fragment.sequence_number) {
|
||||
if fragments.len() == fragment.fragment_count as usize {
|
||||
let mut buf = Vec::<u8>::new();
|
||||
for fragment in fragments {
|
||||
buf.extend(fragment.payload.iter());
|
||||
}
|
||||
let mut c = Cursor::new(&buf[..]);
|
||||
return Some(Message::try_from((&fragment.reliable_command, &mut c)));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -1,361 +0,0 @@
|
||||
use photon_decode::*;
|
||||
|
||||
use std::io::Cursor;
|
||||
|
||||
#[test]
|
||||
fn deserialize_dictionary() {
|
||||
let value = vec![
|
||||
68, 115, 115, 0, 2, 0, 8, 116, 101, 115, 116, 75, 101, 121, 49, 0, 10, 116, 101, 115,
|
||||
116, 86, 97, 108, 117, 101, 49, 0, 8, 116, 101, 115, 116, 75, 101, 121, 50, 0, 10, 116,
|
||||
101, 115, 116, 86, 97, 108, 117, 101, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Dictionary(v) => {
|
||||
return assert_eq!(
|
||||
v.get("testKey1"),
|
||||
Some(&Value::String("testValue1".to_owned()))
|
||||
)
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_string_array() {
|
||||
let value = vec![
|
||||
121, 0, 2, 115, 0, 5, 116, 101, 115, 116, 49, 0, 5, 116, 101, 115, 116, 50, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
|
||||
match value {
|
||||
Value::Array(v) => {
|
||||
if let Value::String(val) = &v[0] {
|
||||
assert_eq!(val, &"test1".to_owned());
|
||||
}
|
||||
if let Value::String(val) = &v[1] {
|
||||
assert_eq!(val, &"test2".to_owned());
|
||||
}
|
||||
return;
|
||||
}
|
||||
Value::StringArray(v) => {
|
||||
return assert_eq!(vec!["test1".to_owned(), "test2".to_owned()], v)
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_deserialize_byte() {
|
||||
let value = vec![98, 6];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Byte(v) => return assert_eq!(v, 6),
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_double() {
|
||||
let value = vec![100, 64, 147, 74, 51, 51, 51, 51, 51, 0, 0, 0, 0, 0, 0, 0];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Double(v) => return assert_eq!(v, 1234.55),
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_event_data() {
|
||||
let value = vec![
|
||||
101, 100, 0, 2, 0, 115, 0, 5, 116, 101, 115, 116, 49, 1, 115, 0, 5, 116, 101, 115, 116,
|
||||
50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::EventData(v) => {
|
||||
assert_eq!(v.code, 100);
|
||||
assert_eq!(
|
||||
v.parameters.get(&0).unwrap(),
|
||||
&Value::String("test1".to_owned())
|
||||
);
|
||||
return;
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_float() {
|
||||
let value = vec![102, 68, 154, 81, 154, 0, 0, 0];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Float(v) => return assert_eq!(v, 1234.55),
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_integer() {
|
||||
let value = vec![105, 0, 0, 4, 210, 0, 0, 0];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Integer(v) => return assert_eq!(v, 1234),
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_short() {
|
||||
let value = vec![107, 4, 210, 0];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Short(v) => return assert_eq!(v, 1234),
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_long() {
|
||||
let value = vec![108, 0, 0, 0, 0, 0, 0, 4, 210, 0, 0, 0, 0, 0, 0, 0];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Long(v) => return assert_eq!(v, 1234),
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_integer_array() {
|
||||
let value = vec![121, 0, 2, 105, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Array(v) => {
|
||||
if let Value::Integer(val) = v[0] {
|
||||
assert_eq!(val, 0);
|
||||
}
|
||||
if let Value::Integer(val) = v[1] {
|
||||
assert_eq!(val, 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_boolean() {
|
||||
let value = vec![111, 1];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Boolean(v) => return assert_eq!(v, true),
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_operation_response() {
|
||||
let value = vec![
|
||||
112, 100, 0, 100, 42, 0, 2, 0, 115, 0, 5, 116, 101, 115, 116, 49, 1, 115, 0, 5, 116,
|
||||
101, 115, 116, 50, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::OperationResponse(v) => {
|
||||
assert_eq!(v.code, 100);
|
||||
assert_eq!(v.return_code, 100);
|
||||
assert_eq!(
|
||||
v.parameters.get(&1).unwrap(),
|
||||
&Value::String("test2".to_owned())
|
||||
);
|
||||
return;
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_operation_request() {
|
||||
let value = vec![
|
||||
113, 100, 0, 2, 0, 115, 0, 5, 116, 101, 115, 116, 49, 1, 115, 0, 5, 116, 101, 115, 116,
|
||||
50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::OperationRequest(v) => {
|
||||
assert_eq!(v.code, 100);
|
||||
assert_eq!(
|
||||
v.parameters.get(&1).unwrap(),
|
||||
&Value::String("test2".to_owned())
|
||||
);
|
||||
return;
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_string() {
|
||||
let value = vec![
|
||||
115, 0, 12, 116, 101, 115, 116, 95, 109, 101, 115, 115, 97, 103, 101, 0,
|
||||
];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::String(v) => return assert_eq!(&v, "test_message"),
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_byte_array() {
|
||||
let value = vec![120, 0, 0, 0, 2, 6, 7, 0];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::ByteArray(v) => {
|
||||
assert_eq!(&v[0], &6);
|
||||
assert_eq!(&v[1], &7);
|
||||
return;
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_array_dictionary() {
|
||||
let value = vec![
|
||||
121, 0, 1, 68, 105, 115, 0, 1, 0, 0, 0, 0, 0, 5, 116, 101, 115, 116, 49, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Array(v) => {
|
||||
if let Value::Dictionary(val) = &v[0] {
|
||||
assert_eq!(val.get("0").unwrap(), &Value::String("test1".to_owned()));
|
||||
}
|
||||
return;
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_array_byte_array() {
|
||||
let value = vec![121, 0, 1, 120, 0, 0, 0, 4, 0, 2, 4, 8, 0, 0, 0, 0];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
|
||||
match value {
|
||||
Value::Array(v) => {
|
||||
if let Value::ByteArray(val) = &v[0] {
|
||||
assert_eq!(val[0], 0);
|
||||
assert_eq!(val[1], 2);
|
||||
assert_eq!(val[2], 4);
|
||||
assert_eq!(val[3], 8);
|
||||
return;
|
||||
}
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_array_array() {
|
||||
let value = vec![
|
||||
121, 0, 1, 121, 0, 3, 105, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0,
|
||||
];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
match value {
|
||||
Value::Array(v) => {
|
||||
if let Value::Integer(val) = &v[0][0] {
|
||||
assert_eq!(val, &1);
|
||||
}
|
||||
if let Value::Integer(val) = &v[0][1] {
|
||||
assert_eq!(val, &2);
|
||||
}
|
||||
if let Value::Integer(val) = &v[0][2] {
|
||||
assert_eq!(val, &3);
|
||||
}
|
||||
return;
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserialize_object_array() {
|
||||
let value = vec![
|
||||
122, 0, 2, 115, 0, 5, 116, 101, 115, 116, 49, 115, 0, 5, 116, 101, 115, 116, 50, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
];
|
||||
let mut buf = Cursor::new(&value[..]);
|
||||
let result = buf.decode();
|
||||
assert!(result.is_ok(), "Unimplemented!");
|
||||
let value = result.unwrap();
|
||||
|
||||
match value {
|
||||
Value::ObjectArray(v) => {
|
||||
if let Value::Object(val) = &*v[0] {
|
||||
assert_eq!(**val, Value::String("test1".to_owned()));
|
||||
}
|
||||
if let Value::Object(val) = &*v[1] {
|
||||
assert_eq!(**val, Value::String("test2".to_owned()));
|
||||
}
|
||||
}
|
||||
_ => assert!(false),
|
||||
};
|
||||
}
|
||||
@@ -1,311 +0,0 @@
|
||||
use photon_decode;
|
||||
|
||||
use photon_decode::Message;
|
||||
use photon_decode::EventData;
|
||||
use photon_decode::OperationRequest;
|
||||
use photon_decode::OperationResponse;
|
||||
use photon_decode::Value;
|
||||
use photon_decode::Parameters;
|
||||
|
||||
#[test]
|
||||
fn test_single_event_message()
|
||||
{
|
||||
let header = vec![
|
||||
0x00, 0x01, // PeerID
|
||||
0x01, // CrcEnabled
|
||||
0x01, // CommandCount
|
||||
0x00, 0x00, 0x00, 0x01, // Timestamp
|
||||
0x00, 0x00, 0x00, 0x01, // Challenge
|
||||
];
|
||||
|
||||
let photon_command = vec![
|
||||
0x06, // Type
|
||||
0x01, // ChannelID
|
||||
0x01, // Flags
|
||||
0x04, // ReservedByte
|
||||
0x00, 0x00, 0x00, 0x1a, // Length
|
||||
0x00, 0x00, 0x00, 0x01, // ReliableSequenceNumber
|
||||
];
|
||||
|
||||
let message = vec![
|
||||
0x01, // ??
|
||||
0x04, // MsgType (Event)
|
||||
// Payload
|
||||
0x64, 0x00, 0x02, // Key
|
||||
0x00, 0x73, // String type
|
||||
0x00, 0x05, // String length
|
||||
0x74, 0x65, 0x73, 0x74, 0x31 //test1
|
||||
];
|
||||
|
||||
let mut photon = photon_decode::Photon::new();
|
||||
|
||||
let mut decoded = photon.try_decode(
|
||||
&[&header[..], &photon_command[..], &message[..]].concat()).expect("Shold works").into_iter();
|
||||
let msg = decoded.next().expect("Shold contain one msg").expect("Should decode event");
|
||||
|
||||
let parameters: Parameters =
|
||||
[(0, Value::String("test1".to_owned()))]
|
||||
.iter().cloned().collect();
|
||||
assert_eq!(msg, Message::Event(EventData{ code: 100, parameters}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_fragmented_event_message()
|
||||
{
|
||||
let header = vec![
|
||||
0x00, 0x01, // PeerID
|
||||
0x01, // CrcEnabled
|
||||
0x01, // CommandCount
|
||||
0x00, 0x00, 0x00, 0x01, // Timestamp
|
||||
0x00, 0x00, 0x00, 0x01, // Challenge
|
||||
];
|
||||
|
||||
let mut photon = photon_decode::Photon::new();
|
||||
|
||||
let photon_command = vec![
|
||||
0x08, // Type
|
||||
0x01, // ChannelID
|
||||
0x01, // Flags
|
||||
0x04, // ReservedByte
|
||||
0x00, 0x00, 0x00, 0x22, // Length
|
||||
0x00, 0x00, 0x00, 0x01, // ReliableSequenceNumber
|
||||
0x00, 0x00, 0x00, 0x01, // SequenceNumber
|
||||
0x00, 0x00, 0x00, 0x03, // FragmentCount
|
||||
0x00, 0x00, 0x00, 0x01, // FragmentNumber
|
||||
0x00, 0x00, 0x00, 0x1a, // TotalLength
|
||||
0x00, 0x00, 0x00, 0x01, // OperationLength
|
||||
];
|
||||
|
||||
let message = vec![
|
||||
0x01, // ??
|
||||
0x04, // MsgType (Event)
|
||||
// Payload
|
||||
];
|
||||
|
||||
let decoded = photon.decode(
|
||||
&[&header[..], &photon_command[..], &message[..]].concat());
|
||||
assert!(decoded.len() == 0);
|
||||
|
||||
let photon_command = vec![
|
||||
0x08, // Type
|
||||
0x01, // ChannelID
|
||||
0x01, // Flags
|
||||
0x04, // ReservedByte
|
||||
0x00, 0x00, 0x00, 0x27, // Length
|
||||
0x00, 0x00, 0x00, 0x01, // ReliableSequenceNumber
|
||||
0x00, 0x00, 0x00, 0x01, // SequenceNumber
|
||||
0x00, 0x00, 0x00, 0x03, // FragmentCount
|
||||
0x00, 0x00, 0x00, 0x02, // FragmentNumber
|
||||
0x00, 0x00, 0x00, 0x1a, // TotalLength
|
||||
0x00, 0x00, 0x00, 0x01, // OperationLength
|
||||
];
|
||||
|
||||
let message = vec![
|
||||
// Payload
|
||||
0x64, 0x00, 0x02, // Key
|
||||
0x00, 0x73, // String type
|
||||
0x00, 0x05, // String length
|
||||
];
|
||||
|
||||
let decoded = photon.decode(
|
||||
&[&header[..], &photon_command[..], &message[..]].concat());
|
||||
assert!(decoded.len() == 0);
|
||||
|
||||
let photon_command = vec![
|
||||
0x08, // Type
|
||||
0x01, // ChannelID
|
||||
0x01, // Flags
|
||||
0x04, // ReservedByte
|
||||
0x00, 0x00, 0x00, 0x25, // Length
|
||||
0x00, 0x00, 0x00, 0x01, // ReliableSequenceNumber
|
||||
0x00, 0x00, 0x00, 0x01, // SequenceNumber
|
||||
0x00, 0x00, 0x00, 0x03, // FragmentCount
|
||||
0x00, 0x00, 0x00, 0x03, // FragmentNumber
|
||||
0x00, 0x00, 0x00, 0x1a, // TotalLength
|
||||
0x00, 0x00, 0x00, 0x01, // OperationLength
|
||||
];
|
||||
|
||||
let message = vec![
|
||||
0x74, 0x65, 0x73, 0x74, 0x31 //test1
|
||||
];
|
||||
|
||||
let decoded = photon.decode(
|
||||
&[&header[..], &photon_command[..], &message[..]].concat());
|
||||
assert_eq!(decoded.len(), 1);
|
||||
|
||||
let parameters: Parameters =
|
||||
[(0, Value::String("test1".to_owned()))]
|
||||
.iter().cloned().collect();
|
||||
assert_eq!(decoded[0], Message::Event(EventData{ code: 100, parameters}));
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_two_event_messages()
|
||||
{
|
||||
let payload = vec![
|
||||
0x00, 0x00,
|
||||
0x00,
|
||||
0x02,
|
||||
0x72, 0xe9, 0xe2, 0x84,
|
||||
0x11, 0x63, 0x88, 0x88,
|
||||
|
||||
0x07,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00, 0x00, 0x00, 0x45,
|
||||
0x00, 0x00, 0x16, 0x8e,
|
||||
0x00, 0x00, 0x45, 0x2b,
|
||||
0xf3, 0x04, 0x02, 0x00, 0x02, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa, 0xb7, 0x36, 0x01, 0x78, 0x00, 0x00, 0x00, 0x20, 0x03, 0x6b, 0xc9, 0x04, 0xc1, 0xe2, 0x62, 0xd7, 0x08, 0x6b, 0xb0, 0x91, 0xc1, 0x2a, 0x80, 0x1b, 0xc2, 0x26, 0x00, 0x00, 0xb0, 0x40, 0xd8, 0xe6, 0x53, 0xc1, 0x8f, 0xcd, 0xc, 0xc2, 0x00, 0x00,
|
||||
|
||||
0x06,
|
||||
0x00,
|
||||
0x01,
|
||||
0x00,
|
||||
0x00, 0x00, 0x00, 0x1b,
|
||||
0x00, 0x00, 0x16, 0x8f,
|
||||
0xf3, 0x04, 0x01, 0x00, 0x02, 0x00, 0x69, 0x00, 0xa, 0xf6, 0xdf, 0xfc, 0x6b, 0x00, 0x01
|
||||
];
|
||||
|
||||
let mut photon = photon_decode::Photon::new();
|
||||
|
||||
let decoded = photon.decode(&payload);
|
||||
|
||||
assert!(decoded.len() == 2);
|
||||
let parameters: Parameters =
|
||||
[
|
||||
(0x01, Value::ByteArray(vec![
|
||||
3, 107, 201, 4, 193, 226, 98, 215, 8, 107, 176, 145, 193,
|
||||
42, 128, 27, 194, 38, 0, 0, 176, 64, 216, 230, 83, 193,
|
||||
143, 205, 12, 194, 0, 0])),
|
||||
(0x00, Value::Long(702262))
|
||||
]
|
||||
.iter().cloned().collect();
|
||||
assert_eq!(decoded[0], Message::Event(EventData{ code: 2, parameters}));
|
||||
|
||||
let parameters: Parameters =
|
||||
[
|
||||
(0x00, Value::Integer(718559)),
|
||||
(0xfc, Value::Short(1))
|
||||
]
|
||||
.iter().cloned().collect();
|
||||
assert_eq!(decoded[1], Message::Event(EventData{ code: 1, parameters}));
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_request_message()
|
||||
{
|
||||
let payload = vec![
|
||||
0x7d, 0xd3,
|
||||
0x00,
|
||||
0x01,
|
||||
0x00, 0x04, 0xcc, 0xb6,
|
||||
0x11, 0x63, 0x88, 0x88,
|
||||
|
||||
0x06,
|
||||
0x00,
|
||||
0x01,
|
||||
0x04,
|
||||
0x00, 0x00, 0x00, 0x2a,
|
||||
0x00, 0x00, 0x00, 0x19,
|
||||
0xf3, 0x02, 0x01, 0x00, 0x03, 0x00,
|
||||
0x79, 0x00, 0x02, 0x66, 0xc1, 0xd3, 0x61, 0x20, 0xc2, 0x27, 0xc, 0xcd, 0x01, 0x62,
|
||||
0x00, 0xfd, // key
|
||||
0x73, // String type
|
||||
0x00, 0x05, // String length
|
||||
0x74, 0x65, 0x73, 0x74, 0x31 // test1
|
||||
];
|
||||
|
||||
let mut photon = photon_decode::Photon::new();
|
||||
let decoded = photon.decode(&payload);
|
||||
|
||||
assert!(decoded.len() == 1);
|
||||
let parameters: Parameters =
|
||||
[
|
||||
(0x00, Value::Array(vec![Value::Float(-26.422424), Value::Float(-41.7625)])),
|
||||
(0x01, Value::Byte(0)),
|
||||
(0xfd, Value::String("test1".to_owned()))
|
||||
|
||||
]
|
||||
.iter().cloned().collect();
|
||||
assert_eq!(decoded[0], Message::Request(OperationRequest { code: 1, parameters}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_response_message()
|
||||
{
|
||||
let payload = vec![
|
||||
0x00, 0x00,
|
||||
0x00,
|
||||
0x02,
|
||||
0x72, 0xea, 0x1d, 0x6a,
|
||||
0x11, 0x63, 0x88, 0x88,
|
||||
|
||||
0x01,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00,
|
||||
0x00, 0x00, 0x00, 0x14,
|
||||
0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x24, 0x00, 0x04, 0xd7, 0x1a,
|
||||
|
||||
0x06,
|
||||
0x00,
|
||||
0x01,
|
||||
0x00,
|
||||
0x00, 0x00, 0x00, 0xb2,
|
||||
0x00, 0x00, 0x17, 0x71,
|
||||
0xf3, 0x03, 0x01, 0x00, 0x00, 0x2a, 0x00, 0xa, 0x00, 0x69, 0x18, 0xf2, 0x79, 0x68, 0x01, 0x73, 0x00, 0x5d, 0x40, 0x49, 0x53, 0x4c, 0x41, 0x4e, 0x44, 0x40, 0x65, 0x66, 0x33, 0x37, 0x39, 0x34, 0x64, 0x61, 0x2d, 0x38, 0x66, 0x38, 0x33, 0x2d, 0x34, 0x33, 0x34, 0x30, 0x2d, 0x62, 0x66, 0x35, 0x32, 0x2d, 0x36, 0x38, 0x31, 0x31, 0x37, 0x61, 0x30, 0x36, 0x37, 0x32, 0x65, 0x34, 0x7c, 0x57, 0x65, 0x64, 0x72, 0x6f, 0x77, 0x79, 0x63, 0x7a, 0x4a, 0x61, 0x6b, 0x75, 0x62, 0x7c, 0x36, 0x33, 0x37, 0x30, 0x37, 0x35, 0x33, 0x37, 0x38, 0x38, 0x30, 0x33, 0x31, 0x38, 0x33, 0x39, 0x33, 0x35, 0x7c, 0x57, 0x65, 0x64, 0x72, 0x6f, 0x77, 0x79, 0x63, 0x7a, 0x4a, 0x61, 0x6b, 0x75, 0x62, 0x02, 0x79, 0x00, 0x00, 0x78, 0x03, 0x79, 0x00, 0x00, 0x73, 0x04, 0x79, 0x00, 0x00, 0x73, 0x05, 0x78, 0x00, 0x00, 0x00, 0x00, 0x06, 0x79, 0x00, 0x01, 0x79, 0x00, 0x01, 0x69, 0xff, 0xff, 0xff, 0xff, 0x07, 0x79, 0x00, 0x01, 0x79, 0x00, 0x01, 0x69, 0xff, 0xff, 0xff, 0xff, 0x08, 0x78, 0x00, 0x00, 0x00, 0x00, 0xfd, 0x6b, 0x00, 0xa6
|
||||
];
|
||||
|
||||
let mut photon = photon_decode::Photon::new();
|
||||
|
||||
let decoded = photon.decode(&payload);
|
||||
|
||||
assert_eq!(decoded.len(), 1);
|
||||
let parameters: Parameters =
|
||||
[
|
||||
(0x00, Value::Integer(418543976)),
|
||||
(0x01, Value::String("@ISLAND@ef3794da-8f83-4340-bf52-68117a0672e4|WedrowyczJakub|637075378803183935|WedrowyczJakub".to_owned())),
|
||||
(0x02, Value::Array(vec![])),
|
||||
(0x03, Value::Array(vec![])),
|
||||
(0x04, Value::Array(vec![])),
|
||||
(0x05, Value::ByteArray(vec![])),
|
||||
(0x06, Value::Array(vec![Value::Array(vec![Value::Integer(4294967295)])])),
|
||||
(0x07, Value::Array(vec![Value::Array(vec![Value::Integer(4294967295)])])),
|
||||
(0x08, Value::ByteArray(vec![])),
|
||||
(0xFD, Value::Short(166)),
|
||||
]
|
||||
.iter().cloned().collect();
|
||||
assert_eq!(decoded[0], Message::Response(OperationResponse { code: 1, return_code: 0, debug_message: "None".to_owned(), parameters}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_example(){
|
||||
use photon_decode::{Photon, Message};
|
||||
|
||||
let mut photon = Photon::new();
|
||||
let photon_packet = vec![
|
||||
0x00, 0x01, // PeerID
|
||||
0x01, // CrcEnabled
|
||||
0x00, // CommandCount
|
||||
0x00, 0x00, 0x00, 0x01, // Timestamp
|
||||
0x00, 0x00, 0x00, 0x01, // Challenge
|
||||
];
|
||||
|
||||
for message in photon.decode(&photon_packet).iter() {
|
||||
match message {
|
||||
Message::Event(_) => {
|
||||
// use event
|
||||
},
|
||||
Message::Request(_) => {
|
||||
// use request
|
||||
},
|
||||
Message::Response(_) => {
|
||||
// use response
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
import sys
|
||||
|
||||
|
||||
from setuptools import setup
|
||||
|
||||
try:
|
||||
from setuptools_rust import RustExtension, Binding
|
||||
except ImportError:
|
||||
import subprocess
|
||||
|
||||
errno = subprocess.call([sys.executable, "-m", "pip", "install", "setuptools-rust"])
|
||||
if errno:
|
||||
print("Please install setuptools-rust package")
|
||||
raise SystemExit(errno)
|
||||
else:
|
||||
from setuptools_rust import RustExtension, Binding
|
||||
|
||||
setup_requires = ["setuptools-rust>=0.10.1", "wheel"]
|
||||
install_requires = []
|
||||
|
||||
setup(
|
||||
name="aostats",
|
||||
packages=["aostats"],
|
||||
rust_extensions=[RustExtension("libaostats", 'Cargo.toml', binding=Binding.RustCPython)],
|
||||
install_requires=install_requires,
|
||||
setup_requires=setup_requires,
|
||||
include_package_data=True,
|
||||
version="6.6.6",
|
||||
zip_safe=False,
|
||||
)
|
||||
@@ -1,81 +0,0 @@
|
||||
use log::*;
|
||||
use simplelog::*;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
use std::thread;
|
||||
|
||||
use packet_sniffer::UdpPacket;
|
||||
use photon_decode::Photon;
|
||||
|
||||
use crate::game::World;
|
||||
use crate::publisher::Publisher;
|
||||
|
||||
pub use crate::publisher::Subscribers;
|
||||
|
||||
use crate::translate::udp_packet_to_game_events;
|
||||
|
||||
pub enum InitializationError {
|
||||
NetworkInterfaceListMissing,
|
||||
}
|
||||
|
||||
pub fn initialize(subscribers: Subscribers) -> Result<(), InitializationError> {
|
||||
initialize_logging();
|
||||
if let Ok(interfaces) = packet_sniffer::network_interfaces() {
|
||||
thread::spawn(move || {
|
||||
let (tx, rx): (Sender<UdpPacket>, Receiver<UdpPacket>) = channel();
|
||||
|
||||
let mut photon = Photon::new();
|
||||
let mut world = World::new();
|
||||
let mut publisher = Publisher::new(subscribers);
|
||||
|
||||
packet_sniffer::receive(interfaces, tx);
|
||||
info!("Listening to network packets...");
|
||||
loop {
|
||||
if let Ok(packet) = rx.recv() {
|
||||
udp_packet_to_game_events(&mut world, &mut photon, &packet)
|
||||
.into_iter()
|
||||
.for_each(|e| {
|
||||
publisher.publish(&e);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return Err(InitializationError::NetworkInterfaceListMissing);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn initialize_logging() {
|
||||
let logging_level = get_logging_level();
|
||||
|
||||
CombinedLogger::init(vec![WriteLogger::new(
|
||||
logging_level,
|
||||
Config::default(),
|
||||
File::create("backend.log").unwrap(),
|
||||
)])
|
||||
.unwrap();
|
||||
|
||||
match logging_level {
|
||||
LevelFilter::Debug => {
|
||||
info!("Logging level set to debug");
|
||||
},
|
||||
_ => {
|
||||
|
||||
info!("Logging level set to info");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_logging_level() -> LevelFilter {
|
||||
match env::var("BACKEND_LOG_LEVEL")
|
||||
.unwrap_or("INFO".into())
|
||||
.to_lowercase()
|
||||
.as_str()
|
||||
{
|
||||
"debug" => LevelFilter::Debug,
|
||||
_ => LevelFilter::Info
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
pub mod python;
|
||||
@@ -1,157 +0,0 @@
|
||||
use cpython::PyObject;
|
||||
use cpython::Python;
|
||||
use cpython::ToPyObject;
|
||||
use cpython::PythonObject;
|
||||
use cpython::PyDict;
|
||||
|
||||
use crate::game::Event;
|
||||
use crate::game::events;
|
||||
use crate::game::StaticId;
|
||||
use crate::photon_messages;
|
||||
|
||||
impl ToPyObject for Event {
|
||||
type ObjectType = PyObject;
|
||||
fn to_py_object(&self, py: Python) -> Self::ObjectType {
|
||||
let event = PyDict::new(py);
|
||||
|
||||
match self {
|
||||
Event::MainPlayerAppeared(e) => {
|
||||
event.set_item(py, "name", "MainPlayerAppeared".into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", e.into_py_object(py)).unwrap_or(());
|
||||
},
|
||||
Event::PlayerAppeared(e) => {
|
||||
event.set_item(py, "name", "PlayerAppeared".into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", e.into_py_object(py)).unwrap_or(());
|
||||
},
|
||||
Event::ZoneChange => {
|
||||
event.set_item(py, "name", "ZoneChange".into_py_object(py)).unwrap_or(());
|
||||
},
|
||||
Event::UpdateItems(e) => {
|
||||
event.set_item(py, "name", "UpdateItems".into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", e.into_py_object(py)).unwrap_or(());
|
||||
}
|
||||
Event::DamageDone(e) => {
|
||||
event.set_item(py, "name", "DamageDone".into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", e.into_py_object(py)).unwrap_or(());
|
||||
}
|
||||
Event::EnterCombat(e) => {
|
||||
event.set_item(py, "name", "EnterCombat".into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", e.into_py_object(py)).unwrap_or(());
|
||||
}
|
||||
Event::LeaveCombat(e) => {
|
||||
event.set_item(py, "name", "LeaveCombat".into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", e.into_py_object(py)).unwrap_or(());
|
||||
}
|
||||
Event::UpdateFame(e) => {
|
||||
event.set_item(py, "name", "UpdateFame".into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", e.into_py_object(py)).unwrap_or(());
|
||||
}
|
||||
Event::HealthReceived(e) => {
|
||||
event.set_item(py, "name", "HealthReceived".into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", e.into_py_object(py)).unwrap_or(());
|
||||
},
|
||||
Event::UpdateParty(e) => {
|
||||
event.set_item(py, "name", "UpdateParty".into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", e.into_py_object(py)).unwrap_or(());
|
||||
}
|
||||
}
|
||||
|
||||
event.into_object()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyObject for StaticId {
|
||||
type ObjectType = PyObject;
|
||||
fn to_py_object(&self, py: Python) -> Self::ObjectType {
|
||||
self.inner().into_py_object(py).into_object()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl ToPyObject for events::Player {
|
||||
type ObjectType = PyObject;
|
||||
fn to_py_object(&self, py: Python) -> Self::ObjectType {
|
||||
let event = PyDict::new(py);
|
||||
|
||||
event.set_item(py, "id", self.id.into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "name", self.name.clone().into_py_object(py)).unwrap_or(());
|
||||
event.into_object()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyObject for events::Damage {
|
||||
type ObjectType = PyObject;
|
||||
fn to_py_object(&self, py: Python) -> Self::ObjectType {
|
||||
let event = PyDict::new(py);
|
||||
|
||||
event.set_item(py, "source", self.source.into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "target", self.target.into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", self.value.into_py_object(py).into_object()).unwrap_or(());
|
||||
event.into_object()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyObject for events::Fame {
|
||||
type ObjectType = PyObject;
|
||||
fn to_py_object(&self, py: Python) -> Self::ObjectType {
|
||||
let event = PyDict::new(py);
|
||||
|
||||
event.set_item(py, "source", self.source.into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", self.value.into_py_object(py).into_object()).unwrap_or(());
|
||||
event.into_object()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyObject for events::Items {
|
||||
type ObjectType = PyObject;
|
||||
fn to_py_object(&self, py: Python) -> Self::ObjectType {
|
||||
let event = PyDict::new(py);
|
||||
|
||||
event.set_item(py, "source", self.source.into_py_object(py)).unwrap_or(());
|
||||
event.set_item(py, "value", self.value.clone().into_py_object(py)).unwrap_or(());
|
||||
event.into_object()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToPyObject for events::Party {
|
||||
type ObjectType = PyObject;
|
||||
fn to_py_object(&self, py: Python) -> Self::ObjectType {
|
||||
let event = PyDict::new(py);
|
||||
|
||||
event.set_item(py, "player_names", self.player_names.clone().into_py_object(py)).unwrap_or(());
|
||||
event.into_object()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
macro_rules! set_dict_item {
|
||||
($py:ident, $dict:ident, $from:ident, $field_name:ident) => {
|
||||
if let Ok(_) = $dict.set_item($py, stringify!($field_name), $from.$field_name.to_py_object($py))
|
||||
{}
|
||||
};
|
||||
}
|
||||
|
||||
impl ToPyObject for photon_messages::Items {
|
||||
type ObjectType = PyDict;
|
||||
|
||||
fn into_py_object(self, py: Python) -> Self::ObjectType {
|
||||
self.to_py_object(py)
|
||||
}
|
||||
|
||||
fn to_py_object(&self, py: Python) -> Self::ObjectType {
|
||||
let items = PyDict::new(py);
|
||||
|
||||
set_dict_item!(py, items, self, weapon);
|
||||
set_dict_item!(py, items, self, offhand);
|
||||
set_dict_item!(py, items, self, helmet);
|
||||
set_dict_item!(py, items, self, armor);
|
||||
set_dict_item!(py, items, self, boots);
|
||||
set_dict_item!(py, items, self, bag);
|
||||
set_dict_item!(py, items, self, cape);
|
||||
set_dict_item!(py, items, self, mount);
|
||||
set_dict_item!(py, items, self, potion);
|
||||
set_dict_item!(py, items, self, food);
|
||||
|
||||
items
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
pub mod convert;
|
||||
@@ -1,170 +0,0 @@
|
||||
use std::convert::From;
|
||||
|
||||
use crate::photon_messages::messages;
|
||||
|
||||
use super::events;
|
||||
use super::id_cache::{DynIdToStaticId, StaticIdToName};
|
||||
use super::player::DynamicId;
|
||||
|
||||
pub struct EventIntermediate<Msg> {
|
||||
dyn_id_to_static_id: DynIdToStaticId,
|
||||
static_id_to_name: StaticIdToName,
|
||||
message: Msg,
|
||||
}
|
||||
|
||||
impl<Msg> EventIntermediate<Msg> {
|
||||
pub fn new(
|
||||
dyn_id_to_static_id: DynIdToStaticId,
|
||||
static_id_to_name: StaticIdToName,
|
||||
message: Msg,
|
||||
) -> Self {
|
||||
Self {
|
||||
dyn_id_to_static_id,
|
||||
static_id_to_name,
|
||||
message,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct EventList {
|
||||
value: Vec<events::Event>,
|
||||
}
|
||||
|
||||
impl EventList {
|
||||
pub fn new(list: Vec<events::Event>) -> Self {
|
||||
Self { value: list }
|
||||
}
|
||||
|
||||
pub fn values(self) -> Option<Vec<events::Event>> {
|
||||
Some(self.value)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! try_from_dynamic {
|
||||
($field_name:ident, $intermediate:expr) => {
|
||||
if let Some(static_id) = $intermediate
|
||||
.dyn_id_to_static_id
|
||||
.get(&DynamicId::from($intermediate.message.$field_name as u32))
|
||||
{
|
||||
*static_id
|
||||
} else {
|
||||
return Self::new(vec![]);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! try_from_dynamic_with_name {
|
||||
($field_name:ident, $intermediate:expr) => {
|
||||
if let Some(static_id) = $intermediate
|
||||
.dyn_id_to_static_id
|
||||
.get(&DynamicId::from($intermediate.message.$field_name as u32))
|
||||
{
|
||||
if let Some(player_name) = $intermediate.static_id_to_name.get(&static_id) {
|
||||
(*static_id, player_name.to_string())
|
||||
} else {
|
||||
return Self::new(vec![]);
|
||||
}
|
||||
} else {
|
||||
return Self::new(vec![]);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl From<EventIntermediate<messages::NewCharacter>> for EventList {
|
||||
fn from(intermediate: EventIntermediate<messages::NewCharacter>) -> Self {
|
||||
let static_id = try_from_dynamic!(source, intermediate);
|
||||
Self::new(vec![
|
||||
events::Event::PlayerAppeared(events::Player {
|
||||
id: static_id,
|
||||
name: intermediate.message.character_name,
|
||||
}),
|
||||
events::Event::UpdateItems(events::Items {
|
||||
source: static_id,
|
||||
value: intermediate.message.items,
|
||||
}),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EventIntermediate<messages::Join>> for EventList {
|
||||
fn from(intermediate: EventIntermediate<messages::Join>) -> Self {
|
||||
let static_id = try_from_dynamic!(source, intermediate);
|
||||
Self::new(vec![events::Event::MainPlayerAppeared(events::Player {
|
||||
id: static_id,
|
||||
name: intermediate.message.character_name,
|
||||
})])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EventIntermediate<messages::HealthUpdate>> for EventList {
|
||||
fn from(intermediate: EventIntermediate<messages::HealthUpdate>) -> Self {
|
||||
let target_static_id = try_from_dynamic!(target, intermediate);
|
||||
|
||||
if intermediate.message.value < 0.0 {
|
||||
return Self::new(vec![events::Event::DamageDone(events::Damage {
|
||||
source: target_static_id,
|
||||
target: intermediate
|
||||
.dyn_id_to_static_id
|
||||
.get(&DynamicId::from(intermediate.message.source as u32))
|
||||
.map(|v| *v),
|
||||
value: f32::abs(intermediate.message.value),
|
||||
})]);
|
||||
}
|
||||
|
||||
Self::new(vec![events::Event::HealthReceived(events::Damage {
|
||||
source: target_static_id,
|
||||
target: intermediate
|
||||
.dyn_id_to_static_id
|
||||
.get(&DynamicId::from(intermediate.message.source as u32))
|
||||
.map(|v| *v),
|
||||
value: intermediate.message.value,
|
||||
})])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EventIntermediate<messages::RegenerationHealthChanged>> for EventList {
|
||||
fn from(intermediate: EventIntermediate<messages::RegenerationHealthChanged>) -> Self {
|
||||
let (static_id, name) = try_from_dynamic_with_name!(source, intermediate);
|
||||
|
||||
match intermediate.message.regeneration_rate {
|
||||
Some(_) => Self::new(vec![events::Event::LeaveCombat(events::Player {
|
||||
id: static_id,
|
||||
name: name,
|
||||
})]),
|
||||
None => Self::new(vec![events::Event::EnterCombat(events::Player {
|
||||
id: static_id,
|
||||
name: name,
|
||||
})]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EventIntermediate<messages::KnockedDown>> for EventList {
|
||||
fn from(intermediate: EventIntermediate<messages::KnockedDown>) -> Self {
|
||||
let (static_id, name) = try_from_dynamic_with_name!(source, intermediate);
|
||||
Self::new(vec![events::Event::LeaveCombat(events::Player {
|
||||
id: static_id,
|
||||
name: name,
|
||||
})])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EventIntermediate<messages::UpdateFame>> for EventList {
|
||||
fn from(intermediate: EventIntermediate<messages::UpdateFame>) -> Self {
|
||||
let source_static_id = try_from_dynamic!(source, intermediate);
|
||||
Self::new(vec![events::Event::UpdateFame(events::Fame {
|
||||
source: source_static_id,
|
||||
value: intermediate.message.fame as f32 / 10000.0,
|
||||
})])
|
||||
}
|
||||
}
|
||||
|
||||
impl From<EventIntermediate<messages::CharacterEquipmentChanged>> for EventList {
|
||||
fn from(intermediate: EventIntermediate<messages::CharacterEquipmentChanged>) -> Self {
|
||||
let source_static_id = try_from_dynamic!(source, intermediate);
|
||||
Self::new(vec![events::Event::UpdateItems(events::Items {
|
||||
source: source_static_id,
|
||||
value: intermediate.message.items,
|
||||
})])
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use crate::photon_messages;
|
||||
|
||||
use super::player::StaticId;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Player {
|
||||
pub id: StaticId,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Damage {
|
||||
pub source: StaticId,
|
||||
pub target: Option<StaticId>,
|
||||
pub value: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Fame {
|
||||
pub source: StaticId,
|
||||
pub value: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Items {
|
||||
pub source: StaticId,
|
||||
pub value: photon_messages::Items,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Party {
|
||||
pub player_names: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Event {
|
||||
MainPlayerAppeared(Player),
|
||||
PlayerAppeared(Player),
|
||||
DamageDone(Damage),
|
||||
HealthReceived(Damage),
|
||||
ZoneChange,
|
||||
EnterCombat(Player),
|
||||
LeaveCombat(Player),
|
||||
UpdateFame(Fame),
|
||||
UpdateItems(Items),
|
||||
UpdateParty(Party),
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
#![allow(dead_code)]
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::player::{DynamicId, PlayerName, StaticId};
|
||||
|
||||
pub type DynIdToStaticId = HashMap<DynamicId, StaticId>;
|
||||
pub type StaticIdToName = HashMap<StaticId, PlayerName>;
|
||||
|
||||
#[derive(Debug, PartialEq, Default)]
|
||||
pub struct IdCache {
|
||||
dyn_id_to_static_id: DynIdToStaticId,
|
||||
static_id_to_name: StaticIdToName,
|
||||
last_id: u32,
|
||||
}
|
||||
|
||||
impl IdCache {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&mut self, dynamic_id: DynamicId, name: &str) {
|
||||
let player_name: PlayerName = name.to_owned().into();
|
||||
if let Some(static_id) = self.find_static_id(&player_name) {
|
||||
if let Some(dyn_id) = self.find_dynamic_id(&static_id) {
|
||||
self.dyn_id_to_static_id.remove(&dyn_id);
|
||||
}
|
||||
self.dyn_id_to_static_id.insert(dynamic_id, static_id);
|
||||
} else {
|
||||
let static_id: StaticId = self.last_id.into();
|
||||
self.last_id += 1;
|
||||
|
||||
self.dyn_id_to_static_id.insert(dynamic_id, static_id);
|
||||
self.static_id_to_name.insert(static_id, player_name);
|
||||
}
|
||||
}
|
||||
|
||||
fn find_static_id(&self, name: &PlayerName) -> Option<StaticId> {
|
||||
self.static_id_to_name
|
||||
.iter()
|
||||
.find(|(_k, v)| v == &name)
|
||||
.map(|(k, _v)| *k)
|
||||
}
|
||||
|
||||
fn find_dynamic_id(&self, static_id: &StaticId) -> Option<DynamicId> {
|
||||
self.dyn_id_to_static_id
|
||||
.iter()
|
||||
.find(|(_k, v)| v == &static_id)
|
||||
.map(|(k, _v)| *k)
|
||||
}
|
||||
|
||||
pub fn get_dyn_id_to_static_id_map(&self) -> DynIdToStaticId {
|
||||
self.dyn_id_to_static_id.clone()
|
||||
}
|
||||
|
||||
pub fn get_static_id_to_name_map(&self) -> StaticIdToName {
|
||||
self.static_id_to_name.clone()
|
||||
}
|
||||
|
||||
pub fn get_static_id(&self, dynamic_id: DynamicId) -> Option<StaticId> {
|
||||
self.dyn_id_to_static_id.get(&dynamic_id).map(|i| *i)
|
||||
}
|
||||
|
||||
pub fn get_name(&self, static_id: StaticId) -> Option<PlayerName> {
|
||||
self.static_id_to_name.get(&static_id).map(|i| i.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_id_cache() {
|
||||
let mut cache = IdCache::new();
|
||||
|
||||
assert!(cache.get_static_id(DynamicId::from(1)).is_none());
|
||||
|
||||
cache.save(DynamicId::from(1), "test");
|
||||
assert!(cache.get_static_id(DynamicId::from(1)).is_some());
|
||||
assert_eq!(
|
||||
cache.get_static_id(DynamicId::from(1)),
|
||||
Some(StaticId::from(0))
|
||||
);
|
||||
|
||||
cache.save(DynamicId::from(2), "test");
|
||||
assert!(cache.get_static_id(DynamicId::from(1)).is_none());
|
||||
assert_eq!(
|
||||
cache.get_static_id(DynamicId::from(2)),
|
||||
Some(StaticId::from(0))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_if_id_is_unique_for_a_player() {
|
||||
let mut cache = IdCache::new();
|
||||
|
||||
cache.save(DynamicId::from(1), "test");
|
||||
assert_eq!(
|
||||
cache.get_static_id(DynamicId::from(1)),
|
||||
Some(StaticId::from(0))
|
||||
);
|
||||
|
||||
cache.save(DynamicId::from(2), "test2");
|
||||
assert_eq!(
|
||||
cache.get_static_id(DynamicId::from(2)),
|
||||
Some(StaticId::from(1))
|
||||
);
|
||||
|
||||
cache.save(DynamicId::from(12345), "test3");
|
||||
assert_eq!(
|
||||
cache.get_static_id(DynamicId::from(12345)),
|
||||
Some(StaticId::from(2))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_finding_player_name() {
|
||||
let mut cache = IdCache::new();
|
||||
|
||||
assert!(cache.get_name(StaticId::from(1)).is_none());
|
||||
|
||||
cache.save(DynamicId::from(1), "test");
|
||||
assert_eq!(
|
||||
cache.get_static_id(DynamicId::from(1)),
|
||||
Some(StaticId::from(0))
|
||||
);
|
||||
assert_eq!(
|
||||
cache.get_name(StaticId::from(0)),
|
||||
Some(PlayerName::from("test".to_owned()))
|
||||
);
|
||||
|
||||
cache.save(DynamicId::from(2), "test2");
|
||||
assert_eq!(
|
||||
cache.get_static_id(DynamicId::from(2)),
|
||||
Some(StaticId::from(1))
|
||||
);
|
||||
assert_eq!(
|
||||
cache.get_name(StaticId::from(1)),
|
||||
Some(PlayerName::from("test2".to_owned()))
|
||||
);
|
||||
assert_eq!(
|
||||
cache.get_name(StaticId::from(0)),
|
||||
Some(PlayerName::from("test".to_owned()))
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
pub mod events;
|
||||
pub mod convert;
|
||||
pub mod world;
|
||||
pub mod id_cache;
|
||||
|
||||
mod party;
|
||||
mod unconsumed_messages;
|
||||
mod player;
|
||||
|
||||
pub use player::StaticId;
|
||||
pub use events::*;
|
||||
pub use world::World;
|
||||
@@ -1,75 +0,0 @@
|
||||
use crate::photon_messages::messages;
|
||||
|
||||
use super::events;
|
||||
|
||||
use log::*;
|
||||
|
||||
#[derive(Debug, PartialEq, Default)]
|
||||
struct Item {
|
||||
name: String,
|
||||
id: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Default)]
|
||||
pub struct Party {
|
||||
main_player_name: Option<String>,
|
||||
items: Vec<Item>,
|
||||
}
|
||||
|
||||
impl Party {
|
||||
pub fn set_main_player_name(&mut self, name: &str) {
|
||||
self.main_player_name = Some(name.to_owned());
|
||||
}
|
||||
|
||||
pub fn player_left(&mut self, left: messages::PartyPlayerLeft) -> Option<events::Event> {
|
||||
let main_player_name = self.main_player_name.as_ref()?;
|
||||
let party_item = self.items.iter().find(|i| i.id == into_id(&left.party_structure))?;
|
||||
if &party_item.name == main_player_name {
|
||||
return self.disbanded();
|
||||
}
|
||||
|
||||
let index = self.items.iter().position(|i| i.id == into_id(&left.party_structure))?;
|
||||
self.items.remove(index);
|
||||
|
||||
self.game_event()
|
||||
}
|
||||
|
||||
pub fn single_player_joined(&mut self, joined: messages::PartyPlayerJoined) -> Option<events::Event> {
|
||||
self.items.push(Item{name: joined.name.clone(), id: into_id(&joined.party_structure)});
|
||||
|
||||
self.game_event()
|
||||
}
|
||||
|
||||
pub fn joined(&mut self, joined: messages::PartyJoined) -> Option<events::Event> {
|
||||
let structures_and_names = joined
|
||||
.party_structures
|
||||
.iter()
|
||||
.zip(joined.character_names.iter());
|
||||
|
||||
self.items = structures_and_names
|
||||
.map(|(structure, name)| Item {
|
||||
name: name.clone(),
|
||||
id: into_id(&structure),
|
||||
})
|
||||
.collect();
|
||||
|
||||
self.game_event()
|
||||
}
|
||||
|
||||
pub fn disbanded(&mut self) -> Option<events::Event> {
|
||||
self.items = vec![];
|
||||
self.game_event()
|
||||
}
|
||||
|
||||
fn game_event(&self) -> Option<events::Event> {
|
||||
let names : Vec<String> = self.items.iter().map(|i| i.name.clone()).collect();
|
||||
let evt = Some(events::Event::UpdateParty(events::Party{player_names: names}));
|
||||
|
||||
info!("Party changed: {:?}", evt);
|
||||
evt
|
||||
}
|
||||
}
|
||||
|
||||
fn into_id(party_structure: &[u32]) -> u32 {
|
||||
party_structure.into_iter().fold(0, |acc, x| acc + x)
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
use derive_more::{From, Into};
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, From, Into, Default, Hash)]
|
||||
pub struct DynamicId(u32);
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, From, Into, Hash)]
|
||||
pub struct StaticId(u32);
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, From, Into, Default, Hash)]
|
||||
pub struct PlayerName(String);
|
||||
|
||||
impl StaticId {
|
||||
pub fn inner(&self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl PlayerName {
|
||||
pub fn to_string(&self) -> String {
|
||||
self.0.clone()
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
use super::player::{DynamicId};
|
||||
use crate::photon_messages::Message;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, PartialEq, Default)]
|
||||
pub struct UnconsumedMessages{
|
||||
messages: HashMap<u32, Vec<Message>>
|
||||
}
|
||||
|
||||
impl UnconsumedMessages {
|
||||
pub fn add(&mut self, msg: Message, id: DynamicId) {
|
||||
self.messages.entry(id.into()).or_insert(vec![]).push(msg);
|
||||
}
|
||||
|
||||
pub fn get_for_id(&mut self, id: DynamicId) -> Option<Vec<Message>> {
|
||||
let messages = self.messages.get(&id.into())?;
|
||||
let result = messages.clone();
|
||||
self.messages.remove(&id.into());
|
||||
|
||||
return Some(result);
|
||||
}
|
||||
}
|
||||
@@ -1,338 +0,0 @@
|
||||
//! Holds state of the game gathered from photon decoded packets.
|
||||
//! Note: This module is responsible for resolving all inconsistency between photon events and required game events.
|
||||
//!
|
||||
//! Inconsistency list:
|
||||
//! - player id is different in each zone
|
||||
|
||||
use crate::photon_messages;
|
||||
|
||||
use super::convert;
|
||||
use super::convert::EventList;
|
||||
use super::events;
|
||||
use super::id_cache;
|
||||
use super::party::Party;
|
||||
use super::player::{DynamicId, StaticId};
|
||||
use super::unconsumed_messages::UnconsumedMessages;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct World {
|
||||
cache: id_cache::IdCache,
|
||||
unconsumed_messages: UnconsumedMessages,
|
||||
main_player_id: Option<StaticId>,
|
||||
party: Party,
|
||||
}
|
||||
|
||||
impl World {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Transforms inconsistent game message into corresponding list of game events
|
||||
pub fn transform(&mut self, message: photon_messages::Message) -> Option<Vec<events::Event>> {
|
||||
match message {
|
||||
photon_messages::Message::NewCharacter(msg) => {
|
||||
let mut result = vec![];
|
||||
let dynamic_id = DynamicId::from(msg.source as u32);
|
||||
self.assign_dynamic_id(dynamic_id, &msg.character_name);
|
||||
|
||||
result.append(&mut EventList::from(self.get_intermediate(msg)?).values()?);
|
||||
|
||||
if let Some(messages) = self.unconsumed_messages.get_for_id(dynamic_id) {
|
||||
for message in messages {
|
||||
result.append(&mut self.transform(message).unwrap_or(vec![]));
|
||||
}
|
||||
}
|
||||
|
||||
Some(result)
|
||||
}
|
||||
photon_messages::Message::Join(msg) => {
|
||||
let mut result = vec![];
|
||||
let dynamic_id = DynamicId::from(msg.source as u32);
|
||||
|
||||
self.assign_dynamic_id(dynamic_id, &msg.character_name);
|
||||
let static_id = self.get_static_id(msg.source)?;
|
||||
|
||||
if self.main_player_id.is_none() {
|
||||
result.push(events::Event::ZoneChange)
|
||||
}
|
||||
self.party.set_main_player_name(&msg.character_name);
|
||||
result.append(&mut EventList::from(self.get_intermediate(msg)?).values()?);
|
||||
|
||||
self.main_player_id = Some(static_id);
|
||||
|
||||
if let Some(messages) = self.unconsumed_messages.get_for_id(dynamic_id) {
|
||||
for message in messages {
|
||||
result.append(&mut self.transform(message).unwrap_or(vec![]));
|
||||
}
|
||||
}
|
||||
|
||||
Some(result)
|
||||
}
|
||||
photon_messages::Message::Leave(msg) => {
|
||||
let static_id = self.get_static_id(msg.source)?;
|
||||
if let Some(main_player_id) = self.main_player_id {
|
||||
if main_player_id == static_id {
|
||||
return Some(vec![events::Event::ZoneChange]);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
photon_messages::Message::HealthUpdate(msg) => {
|
||||
EventList::from(self.get_intermediate(msg)?).values()
|
||||
}
|
||||
photon_messages::Message::RegenerationHealthChanged(msg) => {
|
||||
EventList::from(self.get_intermediate(msg)?).values()
|
||||
}
|
||||
photon_messages::Message::KnockedDown(msg) => {
|
||||
EventList::from(self.get_intermediate(msg)?).values()
|
||||
}
|
||||
photon_messages::Message::UpdateFame(msg) => {
|
||||
EventList::from(self.get_intermediate(msg)?).values()
|
||||
}
|
||||
photon_messages::Message::CharacterEquipmentChanged(msg) => {
|
||||
if let Some(_) = self.get_static_id(msg.source) {
|
||||
return EventList::from(self.get_intermediate(msg)?).values();
|
||||
}
|
||||
let id = DynamicId::from(msg.source as u32);
|
||||
self.unconsumed_messages.add(
|
||||
photon_messages::messages::Message::CharacterEquipmentChanged(msg),
|
||||
id,
|
||||
);
|
||||
None
|
||||
}
|
||||
photon_messages::Message::PartyInvitation(_) => None,
|
||||
photon_messages::Message::PartyJoined(msg) => {
|
||||
let evt = self.party.joined(msg)?;
|
||||
Some(vec![evt])
|
||||
}
|
||||
photon_messages::Message::PartyDisbanded(_) => {
|
||||
let evt = self.party.disbanded()?;
|
||||
Some(vec![evt])
|
||||
}
|
||||
photon_messages::Message::PartyPlayerJoined(msg) => {
|
||||
let evt = self.party.single_player_joined(msg)?;
|
||||
Some(vec![evt])
|
||||
}
|
||||
photon_messages::Message::PartyChangedOrder(_) => None,
|
||||
photon_messages::Message::PartyPlayerLeft(msg) => {
|
||||
let evt = self.party.player_left(msg)?;
|
||||
Some(vec![evt])
|
||||
}
|
||||
photon_messages::Message::PartyLeaderChanged(_) => None,
|
||||
photon_messages::Message::PartyLootSettingChangedPlayer(_) => None,
|
||||
photon_messages::Message::PartySilverGained(_) => None,
|
||||
photon_messages::Message::PartyPlayerUpdated(_) => None,
|
||||
photon_messages::Message::PartyInvitationPlayerBusy(_) => None,
|
||||
photon_messages::Message::PartyMarkedObjectsUpdated(_) => None,
|
||||
photon_messages::Message::PartyOnClusterPartyJoined(_) => None,
|
||||
photon_messages::Message::PartySetRoleFlag(_) => None,
|
||||
}
|
||||
}
|
||||
fn assign_dynamic_id(&mut self, id: DynamicId, name: &str) {
|
||||
self.cache.save(id, name);
|
||||
}
|
||||
|
||||
fn get_static_id(&self, id: usize) -> Option<StaticId> {
|
||||
let dynamic_id = DynamicId::from(id as u32);
|
||||
self.cache.get_static_id(dynamic_id) // queue message if static id isn't known
|
||||
}
|
||||
|
||||
fn get_intermediate<Msg>(&self, msg: Msg) -> Option<convert::EventIntermediate<Msg>> {
|
||||
Some(convert::EventIntermediate::new(
|
||||
self.cache.get_dyn_id_to_static_id_map(),
|
||||
self.cache.get_static_id_to_name_map(),
|
||||
msg,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
macro_rules! assert_contains {
|
||||
($container:expr, $value:expr) => {
|
||||
assert!(
|
||||
format!("{:?}", $container).contains($value),
|
||||
format!("{:?} does not contains {}", $container, $value)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! simulate_new_player {
|
||||
($id:expr, $name:expr, $msg:ident) => {
|
||||
photon_messages::Message::$msg(photon_messages::messages::$msg {
|
||||
source: $id,
|
||||
character_name: $name.to_string(),
|
||||
..Default::default()
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// photon_messages::NewCharacter -> events::Event::PlayerAppeared
|
||||
fn test_player_appeared() {
|
||||
let mut world = World::new();
|
||||
|
||||
let game_message = simulate_new_player!(1, "TestCharacter", NewCharacter);
|
||||
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
assert_contains!(world.transform(game_message.clone()), "PlayerAppeared");
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// photon_messages::Join -> Event::PlayerAppeared
|
||||
fn test_main_player_appeared() {
|
||||
let mut world = World::new();
|
||||
|
||||
let game_message = simulate_new_player!(1, "TestCharacter", Join);
|
||||
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
assert_contains!(world.transform(game_message.clone()), "PlayerAppeared");
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// photon_messages::HealthUpdate -> Event::DamageDone | Event::HealthReceived
|
||||
fn test_damage_done() {
|
||||
let mut world = World::new();
|
||||
|
||||
let game_message = simulate_new_player!(1, "TestCharacter", Join);
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
assert_contains!(world.transform(game_message.clone()), "PlayerAppeared");
|
||||
let game_message = simulate_new_player!(2, "TestCharacter2", NewCharacter);
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
|
||||
let target = 1;
|
||||
let game_message =
|
||||
photon_messages::Message::HealthUpdate(photon_messages::messages::HealthUpdate {
|
||||
source: 2,
|
||||
target,
|
||||
value: -666.0,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
assert_contains!(world.transform(game_message.clone()), "DamageDone");
|
||||
assert_contains!(world.transform(game_message.clone()), "666");
|
||||
|
||||
let target = 1;
|
||||
let game_message =
|
||||
photon_messages::Message::HealthUpdate(photon_messages::messages::HealthUpdate {
|
||||
source: 2,
|
||||
target,
|
||||
value: 666.0,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
assert_contains!(world.transform(game_message.clone()), "HealthReceived");
|
||||
assert_contains!(world.transform(game_message.clone()), "666");
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// photon_messages::Leave -> Event::ZoneChange
|
||||
fn test_zone_change() {
|
||||
let mut world = World::new();
|
||||
|
||||
let game_message = photon_messages::Message::Leave(photon_messages::messages::Leave {
|
||||
source: 1,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert!(world.transform(game_message.clone()).is_none());
|
||||
|
||||
let game_message = simulate_new_player!(1, "TestCharacter", Join);
|
||||
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
assert_contains!(world.transform(game_message.clone()), "PlayerAppeared");
|
||||
|
||||
let game_message = photon_messages::Message::Leave(photon_messages::messages::Leave {
|
||||
source: 1,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_contains!(world.transform(game_message.clone()), "ZoneChange");
|
||||
|
||||
let game_message = simulate_new_player!(2, "TestCharacter", NewCharacter);
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
let game_message = photon_messages::Message::Leave(photon_messages::messages::Leave {
|
||||
source: 1,
|
||||
..Default::default()
|
||||
});
|
||||
assert!(world.transform(game_message.clone()).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// photon_messages::RegenerationHealthChanged.regeneration_rate -> Event::LeaveCombat
|
||||
fn test_combat_leave_via_regeneration_change() {
|
||||
let mut world = World::new();
|
||||
|
||||
let game_message = simulate_new_player!(1, "TestCharacter", Join);
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
|
||||
let game_message = photon_messages::Message::RegenerationHealthChanged(
|
||||
photon_messages::messages::RegenerationHealthChanged {
|
||||
source: 1,
|
||||
regeneration_rate: Some(1.0),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
assert_contains!(world.transform(game_message.clone()), "LeaveCombat");
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// photon_messages::RegenerationHealthChanged.regeneration_rate -> Event::EnterCombat
|
||||
fn test_combat_enter_via_regeneration_change() {
|
||||
let mut world = World::new();
|
||||
|
||||
let game_message = simulate_new_player!(1, "TestCharacter", Join);
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
|
||||
let game_message = photon_messages::Message::RegenerationHealthChanged(
|
||||
photon_messages::messages::RegenerationHealthChanged {
|
||||
source: 1,
|
||||
regeneration_rate: None,
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
|
||||
assert_contains!(world.transform(game_message.clone()), "EnterCombat");
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// photon_messages::KnockedDown -> Event::EnterCombat
|
||||
fn test_combat_enter_via_knockout() {
|
||||
let mut world = World::new();
|
||||
|
||||
let game_message = simulate_new_player!(1, "TestCharacter", Join);
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
|
||||
let game_message =
|
||||
photon_messages::Message::KnockedDown(photon_messages::messages::KnockedDown {
|
||||
source: 1,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_contains!(world.transform(game_message.clone()), "LeaveCombat");
|
||||
}
|
||||
|
||||
#[test]
|
||||
/// photon_messages::UpdateFame -> Event::FameUpdate
|
||||
fn test_fame_update() {
|
||||
let mut world = World::new();
|
||||
|
||||
let game_message = simulate_new_player!(1, "TestCharacter", Join);
|
||||
assert!(world.transform(game_message.clone()).is_some());
|
||||
|
||||
let game_message =
|
||||
photon_messages::Message::UpdateFame(photon_messages::messages::UpdateFame {
|
||||
source: 1,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
assert_contains!(world.transform(game_message.clone()), "UpdateFame");
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
#[macro_use]
|
||||
extern crate cpython;
|
||||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
mod photon_messages;
|
||||
mod game;
|
||||
mod translate;
|
||||
mod publisher;
|
||||
mod crosslang;
|
||||
mod api;
|
||||
|
||||
use std::sync::Mutex;
|
||||
use log::*;
|
||||
|
||||
use cpython::PyResult;
|
||||
use cpython::Python;
|
||||
use cpython::ToPyObject;
|
||||
use cpython::PythonObject;
|
||||
use cpython::ObjectProtocol;
|
||||
use cpython::PyTuple;
|
||||
|
||||
lazy_static! {
|
||||
static ref PY_CALLBACKS: Mutex<Vec<cpython::PyObject>> = Mutex::new(Vec::new());
|
||||
}
|
||||
|
||||
fn python_callbacks_subscriber(event: game::Event) {
|
||||
if let Ok(ref mut py_callbacks) = &mut PY_CALLBACKS.lock() {
|
||||
let gil = Python::acquire_gil();
|
||||
let py = gil.python();
|
||||
|
||||
for py_callback in py_callbacks.iter() {
|
||||
let py_args = [event.clone().into_py_object(py).into_object()];
|
||||
let args = PyTuple::new(py, &py_args[..]);
|
||||
|
||||
if let Some(error) = py_callback.call(py, args, None).err() {
|
||||
error!("{:?}", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn initialize(_py: Python) -> PyResult<u32> {
|
||||
api::initialize(vec![
|
||||
Box::new(python_callbacks_subscriber)
|
||||
]).map_or(Ok(2), |_| {Ok(0)})
|
||||
}
|
||||
|
||||
fn subscribe(_py: Python, callable: cpython::PyObject) -> PyResult<u32> {
|
||||
if let Ok(ref mut py_callbacks) = &mut PY_CALLBACKS.lock() {
|
||||
py_callbacks.push(callable);
|
||||
}
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
|
||||
py_module_initializer!(libaostats, initlibaostats, PyInit_libaostats, |py, m| {
|
||||
m.add(py, "__doc__", "This module is implemented in Rust")?;
|
||||
m.add(py, "initialize", py_fn!(py, initialize()))?;
|
||||
m.add(py, "subscribe", py_fn!(py, subscribe(callable: cpython::PyObject)))?;
|
||||
Ok(())
|
||||
});
|
||||
@@ -1,608 +0,0 @@
|
||||
use log::*;
|
||||
use crate::photon_messages::Items;
|
||||
use photon_decode::Parameters;
|
||||
use photon_decode::Value;
|
||||
|
||||
macro_rules! decode_number {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::Short(v) => Some(*v as usize),
|
||||
Value::Integer(v) => Some(*v as usize),
|
||||
Value::Byte(v) => Some(*v as usize),
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
error!("Index {} not found in {}", $index, $name);
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! decode_string {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::String(v) => Some(v.clone()),
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[allow(unused_macros)]
|
||||
macro_rules! decode_string_vec {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::Array(arr) => {
|
||||
let mut ret = vec![];
|
||||
for v in arr {
|
||||
if let Value::String(s) = v {
|
||||
ret.push(s.clone());
|
||||
}
|
||||
}
|
||||
|
||||
Some(ret)
|
||||
}
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! decode_number_vec {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::Array(arr) => {
|
||||
let mut ret = vec![];
|
||||
for v in arr {
|
||||
match v {
|
||||
Value::Short(v) => {
|
||||
ret.push(*v as u32);
|
||||
},
|
||||
Value::Byte(v) => {
|
||||
ret.push(*v as u32);
|
||||
},
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Some(ret)
|
||||
},
|
||||
Value::ByteArray(v) => {
|
||||
Some(v.iter().map(|b| *b as u32).collect::<Vec<u32>>())
|
||||
},
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! decode_float {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::Float(v) => Some(*v as f32),
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
macro_rules! decode_vec_of_number_vec {
|
||||
($val:expr, $index:expr, $name:expr) => {
|
||||
if let Some(p) = $val.get(&$index) {
|
||||
match p {
|
||||
Value::Array(v) => {
|
||||
let mut ret = vec![];
|
||||
for i in v {
|
||||
let mut params = Parameters::new();
|
||||
params.insert(0, i.clone());
|
||||
let item = decode_number_vec!(params, 0, $name)?;
|
||||
ret.push(item);
|
||||
}
|
||||
|
||||
Some(ret)
|
||||
},
|
||||
_ => {
|
||||
error!("Failed to decode {}", $name);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct Leave {
|
||||
pub source: usize,
|
||||
|
||||
}
|
||||
|
||||
impl Leave {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("Leave parameters: {:?}", val);
|
||||
let source = decode_number!(val, 0, "Leave::source")?;
|
||||
|
||||
|
||||
Some(Message::Leave(Leave { source, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct HealthUpdate {
|
||||
pub source: usize,
|
||||
pub target: usize,
|
||||
pub value: f32,
|
||||
|
||||
}
|
||||
|
||||
impl HealthUpdate {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("HealthUpdate parameters: {:?}", val);
|
||||
let source = decode_number!(val, 0, "HealthUpdate::source")?;
|
||||
let target = decode_number!(val, 6, "HealthUpdate::target")?;
|
||||
let value = decode_float!(val, 2, "HealthUpdate::value")?;
|
||||
|
||||
|
||||
Some(Message::HealthUpdate(HealthUpdate { source, target, value, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct RegenerationHealthChanged {
|
||||
pub source: usize,
|
||||
pub health: f32,
|
||||
pub max_health: f32,
|
||||
pub regeneration_rate: Option<f32>,
|
||||
|
||||
}
|
||||
|
||||
impl RegenerationHealthChanged {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("RegenerationHealthChanged parameters: {:?}", val);
|
||||
let source = decode_number!(val, 0, "RegenerationHealthChanged::source")?;
|
||||
let health = decode_float!(val, 2, "RegenerationHealthChanged::health")?;
|
||||
let max_health = decode_float!(val, 3, "RegenerationHealthChanged::max_health")?;
|
||||
let regeneration_rate = decode_float!(val, 4, "RegenerationHealthChanged::regeneration_rate");
|
||||
|
||||
|
||||
Some(Message::RegenerationHealthChanged(RegenerationHealthChanged { source, health, max_health, regeneration_rate, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct KnockedDown {
|
||||
pub source: usize,
|
||||
pub target: usize,
|
||||
pub target_name: String,
|
||||
|
||||
}
|
||||
|
||||
impl KnockedDown {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("KnockedDown parameters: {:?}", val);
|
||||
let source = decode_number!(val, 0, "KnockedDown::source")?;
|
||||
let target = decode_number!(val, 3, "KnockedDown::target")?;
|
||||
let target_name = decode_string!(val, 4, "KnockedDown::target_name")?;
|
||||
|
||||
|
||||
Some(Message::KnockedDown(KnockedDown { source, target, target_name, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct NewCharacter {
|
||||
pub source: usize,
|
||||
pub character_name: String,
|
||||
pub health: f32,
|
||||
pub max_health: f32,
|
||||
pub energy: f32,
|
||||
pub max_energy: f32,
|
||||
pub items: Items,
|
||||
|
||||
}
|
||||
|
||||
impl NewCharacter {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("NewCharacter parameters: {:?}", val);
|
||||
let source = decode_number!(val, 0, "NewCharacter::source")?;
|
||||
let character_name = decode_string!(val, 1, "NewCharacter::character_name")?;
|
||||
let health = decode_float!(val, 18, "NewCharacter::health")?;
|
||||
let max_health = decode_float!(val, 19, "NewCharacter::max_health")?;
|
||||
let energy = decode_float!(val, 22, "NewCharacter::energy")?;
|
||||
let max_energy = decode_float!(val, 23, "NewCharacter::max_energy")?;
|
||||
let item_array = decode_number_vec!(val, 33, "NewCharacter::item_array")?;
|
||||
let items = item_array.into();
|
||||
|
||||
|
||||
Some(Message::NewCharacter(NewCharacter { source, character_name, health, max_health, energy, max_energy, items, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct UpdateFame {
|
||||
pub source: usize,
|
||||
pub fame: usize,
|
||||
|
||||
}
|
||||
|
||||
impl UpdateFame {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("UpdateFame parameters: {:?}", val);
|
||||
let source = decode_number!(val, 0, "UpdateFame::source")?;
|
||||
let fame = decode_number!(val, 2, "UpdateFame::fame")?;
|
||||
|
||||
|
||||
Some(Message::UpdateFame(UpdateFame { source, fame, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct CharacterEquipmentChanged {
|
||||
pub source: usize,
|
||||
pub items: Items,
|
||||
|
||||
}
|
||||
|
||||
impl CharacterEquipmentChanged {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("CharacterEquipmentChanged parameters: {:?}", val);
|
||||
let source = decode_number!(val, 0, "CharacterEquipmentChanged::source")?;
|
||||
let item_array = decode_number_vec!(val, 2, "CharacterEquipmentChanged::item_array")?;
|
||||
let items = item_array.into();
|
||||
|
||||
|
||||
Some(Message::CharacterEquipmentChanged(CharacterEquipmentChanged { source, items, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyInvitation {
|
||||
|
||||
}
|
||||
|
||||
impl PartyInvitation {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyInvitation parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartyInvitation(PartyInvitation { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyJoined {
|
||||
pub party_id: usize,
|
||||
pub party_structures: Vec<Vec<u32>>,
|
||||
pub character_names: Vec<String>,
|
||||
|
||||
}
|
||||
|
||||
impl PartyJoined {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyJoined parameters: {:?}", val);
|
||||
let party_id = decode_number!(val, 0, "PartyJoined::party_id")?;
|
||||
let party_structures = decode_vec_of_number_vec!(val, 4, "PartyJoined::party_structures")?;
|
||||
let character_names = decode_string_vec!(val, 5, "PartyJoined::character_names")?;
|
||||
|
||||
|
||||
Some(Message::PartyJoined(PartyJoined { party_id, party_structures, character_names, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyDisbanded {
|
||||
|
||||
}
|
||||
|
||||
impl PartyDisbanded {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyDisbanded parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartyDisbanded(PartyDisbanded { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyPlayerJoined {
|
||||
pub party_id: usize,
|
||||
pub party_structure: Vec<u32>,
|
||||
pub name: String,
|
||||
|
||||
}
|
||||
|
||||
impl PartyPlayerJoined {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyPlayerJoined parameters: {:?}", val);
|
||||
let party_id = decode_number!(val, 0, "PartyPlayerJoined::party_id")?;
|
||||
let party_structure = decode_number_vec!(val, 1, "PartyPlayerJoined::party_structure")?;
|
||||
let name = decode_string!(val, 2, "PartyPlayerJoined::name")?;
|
||||
|
||||
|
||||
Some(Message::PartyPlayerJoined(PartyPlayerJoined { party_id, party_structure, name, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyChangedOrder {
|
||||
|
||||
}
|
||||
|
||||
impl PartyChangedOrder {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyChangedOrder parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartyChangedOrder(PartyChangedOrder { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyPlayerLeft {
|
||||
pub party_id: usize,
|
||||
pub party_structure: Vec<u32>,
|
||||
|
||||
}
|
||||
|
||||
impl PartyPlayerLeft {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyPlayerLeft parameters: {:?}", val);
|
||||
let party_id = decode_number!(val, 0, "PartyPlayerLeft::party_id")?;
|
||||
let party_structure = decode_number_vec!(val, 1, "PartyPlayerLeft::party_structure")?;
|
||||
|
||||
|
||||
Some(Message::PartyPlayerLeft(PartyPlayerLeft { party_id, party_structure, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyLeaderChanged {
|
||||
|
||||
}
|
||||
|
||||
impl PartyLeaderChanged {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyLeaderChanged parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartyLeaderChanged(PartyLeaderChanged { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyLootSettingChangedPlayer {
|
||||
|
||||
}
|
||||
|
||||
impl PartyLootSettingChangedPlayer {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyLootSettingChangedPlayer parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartyLootSettingChangedPlayer(PartyLootSettingChangedPlayer { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartySilverGained {
|
||||
|
||||
}
|
||||
|
||||
impl PartySilverGained {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartySilverGained parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartySilverGained(PartySilverGained { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyPlayerUpdated {
|
||||
|
||||
}
|
||||
|
||||
impl PartyPlayerUpdated {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyPlayerUpdated parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartyPlayerUpdated(PartyPlayerUpdated { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyInvitationPlayerBusy {
|
||||
|
||||
}
|
||||
|
||||
impl PartyInvitationPlayerBusy {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyInvitationPlayerBusy parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartyInvitationPlayerBusy(PartyInvitationPlayerBusy { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyMarkedObjectsUpdated {
|
||||
|
||||
}
|
||||
|
||||
impl PartyMarkedObjectsUpdated {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyMarkedObjectsUpdated parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartyMarkedObjectsUpdated(PartyMarkedObjectsUpdated { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartyOnClusterPartyJoined {
|
||||
|
||||
}
|
||||
|
||||
impl PartyOnClusterPartyJoined {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartyOnClusterPartyJoined parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartyOnClusterPartyJoined(PartyOnClusterPartyJoined { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct PartySetRoleFlag {
|
||||
|
||||
}
|
||||
|
||||
impl PartySetRoleFlag {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("PartySetRoleFlag parameters: {:?}", val);
|
||||
|
||||
|
||||
Some(Message::PartySetRoleFlag(PartySetRoleFlag { }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct Join {
|
||||
pub source: usize,
|
||||
pub character_name: String,
|
||||
pub health: f32,
|
||||
pub max_health: f32,
|
||||
pub energy: f32,
|
||||
pub max_energy: f32,
|
||||
|
||||
}
|
||||
|
||||
impl Join {
|
||||
pub fn parse(val: Parameters) -> Option<Message> {
|
||||
info!("Join parameters: {:?}", val);
|
||||
let source = decode_number!(val, 0, "Join::source")?;
|
||||
let character_name = decode_string!(val, 2, "Join::character_name")?;
|
||||
let health = decode_float!(val, 11, "Join::health")?;
|
||||
let max_health = decode_float!(val, 12, "Join::max_health")?;
|
||||
let energy = decode_float!(val, 15, "Join::energy")?;
|
||||
let max_energy = decode_float!(val, 16, "Join::max_energy")?;
|
||||
|
||||
|
||||
Some(Message::Join(Join { source, character_name, health, max_health, energy, max_energy, }))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Message {
|
||||
Leave(Leave),
|
||||
HealthUpdate(HealthUpdate),
|
||||
RegenerationHealthChanged(RegenerationHealthChanged),
|
||||
KnockedDown(KnockedDown),
|
||||
NewCharacter(NewCharacter),
|
||||
UpdateFame(UpdateFame),
|
||||
CharacterEquipmentChanged(CharacterEquipmentChanged),
|
||||
PartyInvitation(PartyInvitation),
|
||||
PartyJoined(PartyJoined),
|
||||
PartyDisbanded(PartyDisbanded),
|
||||
PartyPlayerJoined(PartyPlayerJoined),
|
||||
PartyChangedOrder(PartyChangedOrder),
|
||||
PartyPlayerLeft(PartyPlayerLeft),
|
||||
PartyLeaderChanged(PartyLeaderChanged),
|
||||
PartyLootSettingChangedPlayer(PartyLootSettingChangedPlayer),
|
||||
PartySilverGained(PartySilverGained),
|
||||
PartyPlayerUpdated(PartyPlayerUpdated),
|
||||
PartyInvitationPlayerBusy(PartyInvitationPlayerBusy),
|
||||
PartyMarkedObjectsUpdated(PartyMarkedObjectsUpdated),
|
||||
PartyOnClusterPartyJoined(PartyOnClusterPartyJoined),
|
||||
PartySetRoleFlag(PartySetRoleFlag),
|
||||
Join(Join),
|
||||
}
|
||||
|
||||
pub fn into_game_message(photon_message: photon_decode::Message) -> Option<Message> {
|
||||
debug!("Raw photon : {:?}", photon_message);
|
||||
match photon_message {
|
||||
photon_decode::Message::Event(photon_decode::EventData{
|
||||
code: 1,
|
||||
parameters
|
||||
}) => {
|
||||
match parameters.get(&252u8) {
|
||||
Some(photon_decode::Value::Short(1)) => Leave::parse(parameters),
|
||||
Some(photon_decode::Value::Short(6)) => HealthUpdate::parse(parameters),
|
||||
Some(photon_decode::Value::Short(81)) => RegenerationHealthChanged::parse(parameters),
|
||||
Some(photon_decode::Value::Short(154)) => KnockedDown::parse(parameters),
|
||||
Some(photon_decode::Value::Short(25)) => NewCharacter::parse(parameters),
|
||||
Some(photon_decode::Value::Short(73)) => UpdateFame::parse(parameters),
|
||||
Some(photon_decode::Value::Short(80)) => CharacterEquipmentChanged::parse(parameters),
|
||||
Some(photon_decode::Value::Short(212)) => PartyInvitation::parse(parameters),
|
||||
Some(photon_decode::Value::Short(213)) => PartyJoined::parse(parameters),
|
||||
Some(photon_decode::Value::Short(214)) => PartyDisbanded::parse(parameters),
|
||||
Some(photon_decode::Value::Short(215)) => PartyPlayerJoined::parse(parameters),
|
||||
Some(photon_decode::Value::Short(216)) => PartyChangedOrder::parse(parameters),
|
||||
Some(photon_decode::Value::Short(217)) => PartyPlayerLeft::parse(parameters),
|
||||
Some(photon_decode::Value::Short(218)) => PartyLeaderChanged::parse(parameters),
|
||||
Some(photon_decode::Value::Short(219)) => PartyLootSettingChangedPlayer::parse(parameters),
|
||||
Some(photon_decode::Value::Short(220)) => PartySilverGained::parse(parameters),
|
||||
Some(photon_decode::Value::Short(221)) => PartyPlayerUpdated::parse(parameters),
|
||||
Some(photon_decode::Value::Short(222)) => PartyInvitationPlayerBusy::parse(parameters),
|
||||
Some(photon_decode::Value::Short(223)) => PartyMarkedObjectsUpdated::parse(parameters),
|
||||
Some(photon_decode::Value::Short(224)) => PartyOnClusterPartyJoined::parse(parameters),
|
||||
Some(photon_decode::Value::Short(225)) => PartySetRoleFlag::parse(parameters),
|
||||
|
||||
_ => None
|
||||
}
|
||||
},
|
||||
photon_decode::Message::Request(photon_decode::OperationRequest{
|
||||
code: 1,
|
||||
parameters
|
||||
}) => {
|
||||
match parameters.get(&253u8) {
|
||||
_ => None
|
||||
}
|
||||
},
|
||||
photon_decode::Message::Response(photon_decode::OperationResponse{
|
||||
code: 1,
|
||||
parameters,
|
||||
return_code: _,
|
||||
debug_message: _
|
||||
}) => {
|
||||
match parameters.get(&253u8) {
|
||||
Some(photon_decode::Value::Short(2)) => Join::parse(parameters),
|
||||
|
||||
_ => None
|
||||
}
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
@@ -1,65 +0,0 @@
|
||||
//! Translates photon packets into game messages
|
||||
|
||||
//! Generated messages based on assets/messages.json
|
||||
//! See build.rs
|
||||
pub mod messages;
|
||||
/// generated from assets/item_ids.txt
|
||||
/// See build.rs
|
||||
mod itemdb;
|
||||
|
||||
use itemdb::ITEMDB;
|
||||
|
||||
use std::convert::From;
|
||||
|
||||
pub use messages::Message;
|
||||
pub use messages::into_game_message;
|
||||
|
||||
/// Player inventory items
|
||||
#[derive(Debug, Clone, PartialEq, Default)]
|
||||
pub struct Items {
|
||||
pub weapon: Option<String>,
|
||||
pub offhand: Option<String>,
|
||||
pub helmet: Option<String>,
|
||||
pub armor: Option<String>,
|
||||
pub boots: Option<String>,
|
||||
pub bag: Option<String>,
|
||||
pub cape: Option<String>,
|
||||
pub mount: Option<String>,
|
||||
pub potion: Option<String>,
|
||||
pub food: Option<String>,
|
||||
}
|
||||
|
||||
impl From<std::vec::Vec<u32>> for Items {
|
||||
fn from(item_array: std::vec::Vec<u32>) -> Self {
|
||||
macro_rules! extract {
|
||||
($id:expr) => {
|
||||
item_array
|
||||
.get($id)
|
||||
.iter()
|
||||
.filter(|id| **id != &0u32)
|
||||
.map(|code| ITEMDB.get(code).map(|s| s.to_string()))
|
||||
.filter_map(|o| o)
|
||||
.next()
|
||||
};
|
||||
}
|
||||
|
||||
Self{
|
||||
weapon: extract!(0),
|
||||
offhand: extract!(1),
|
||||
helmet: extract!(2),
|
||||
armor: extract!(3),
|
||||
boots: extract!(4),
|
||||
bag: extract!(5),
|
||||
cape: extract!(6),
|
||||
mount: extract!(7),
|
||||
potion: extract!(8),
|
||||
food: extract!(9),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_itemdb_generation() {
|
||||
assert_eq!(ITEMDB.get(&0), Some(&"UNIQUE_HIDEOUT"));
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
use crate::game::Event;
|
||||
|
||||
pub type Subscriber = dyn FnMut(Event) + Send;
|
||||
pub type Subscribers = Vec<Box<Subscriber>>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Publisher {
|
||||
subscribers: Subscribers
|
||||
}
|
||||
|
||||
impl Publisher {
|
||||
pub fn new(subscribers: Subscribers) -> Self {
|
||||
Self {subscribers, ..Default::default()}
|
||||
}
|
||||
|
||||
pub fn publish(&mut self, event: &Event) {
|
||||
for subscriber in &mut self.subscribers {
|
||||
subscriber(event.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use std::thread;
|
||||
|
||||
#[test]
|
||||
fn test_simple_subscription() {
|
||||
let subscriber = |_| {};
|
||||
|
||||
let mut publisher = Publisher::new(vec![Box::new(subscriber)]);
|
||||
publisher.publish(&Event::ZoneChange);
|
||||
}
|
||||
|
||||
pub fn test_call_in_the_thread(subscribers: Subscribers)
|
||||
{
|
||||
thread::spawn(move || {
|
||||
let _ = Publisher::new(subscribers);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_threads() {
|
||||
test_call_in_the_thread(vec![]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
use packet_sniffer::UdpPacket;
|
||||
use photon_decode::Photon;
|
||||
|
||||
use crate::game::Event;
|
||||
use crate::photon_messages::into_game_message;
|
||||
use crate::photon_messages::Message;
|
||||
use crate::game::World;
|
||||
|
||||
use log::*;
|
||||
|
||||
static GAME_PORT : u16 = 5056;
|
||||
|
||||
pub fn udp_packet_to_game_events(game_world: &mut World, photon: &mut Photon, packet: &UdpPacket) -> Vec<Event> {
|
||||
if ! is_packet_valid(packet) {
|
||||
return vec![]
|
||||
}
|
||||
|
||||
debug!("Raw payload: {:?}", &packet.payload);
|
||||
|
||||
raw_to_photon_messages(photon, &packet.payload)
|
||||
.into_iter()
|
||||
.map(|message| game_world.transform(message))
|
||||
.flatten()
|
||||
.flatten()
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn raw_to_photon_messages(photon: &mut Photon, packet_payload: &[u8]) -> Vec<Message> {
|
||||
return photon
|
||||
.decode(packet_payload)
|
||||
.into_iter()
|
||||
.filter_map(into_game_message)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn is_packet_valid(packet: &UdpPacket) -> bool {
|
||||
return packet.destination_port == GAME_PORT || packet.source_port == GAME_PORT;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
}
|
||||
@@ -3,4 +3,4 @@ pyinstaller
|
||||
toml~=0.10
|
||||
requests~=2.22
|
||||
Pillow~=6.2
|
||||
./backend
|
||||
pyaoaddons~=0.1
|
||||
|
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
|
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 57 KiB |
|
Before Width: | Height: | Size: 785 B After Width: | Height: | Size: 785 B |
|
Before Width: | Height: | Size: 835 B After Width: | Height: | Size: 835 B |
|
Before Width: | Height: | Size: 648 B After Width: | Height: | Size: 648 B |