1
0
Fork 0
mirror of https://codeberg.org/demostf/parser.git synced 2026-06-03 18:24:05 +02:00

reduce the size of the generated code

This commit is contained in:
Robin Appelman 2019-09-03 23:14:24 +02:00
commit b761a0aa1a
5 changed files with 1319 additions and 4261 deletions

78
codegen/Cargo.lock generated
View file

@ -84,21 +84,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "bitstream_reader" name = "bitstream_reader"
version = "0.5.1" version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"bitstream_reader_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "bitstream_reader_derive 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "bitstream_reader_derive" name = "bitstream_reader_derive"
version = "0.5.0" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)", "syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)",
"syn_util 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -257,6 +258,11 @@ dependencies = [
"synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", "synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "fs_extra"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "fuchsia-cprng" name = "fuchsia-cprng"
version = "0.1.1" version = "0.1.1"
@ -277,6 +283,25 @@ name = "itoa"
version = "0.4.4" version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "jemalloc-sys"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cc 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
"fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "jemallocator"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"jemalloc-sys 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.3.0" version = "1.3.0"
@ -345,6 +370,29 @@ dependencies = [
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "parse-display"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"parse-display-derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "parse-display-derive"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.5" version = "0.2.5"
@ -634,6 +682,16 @@ dependencies = [
"unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "syn_util"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "synom" name = "synom"
version = "0.11.3" version = "0.11.3"
@ -679,12 +737,14 @@ name = "tf-demo-parser"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"better-panic 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "better-panic 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"bitstream_reader 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "bitstream_reader 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
"enum-primitive-derive 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "enum-primitive-derive 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"enumflags2 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "enumflags2 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
"enumflags2_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "enumflags2_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"err-derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "err-derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
"jemallocator 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", "num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"parse-display 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_repr 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde_repr 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
@ -799,8 +859,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e" "checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
"checksum better-panic 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "64714970eb2081691f85a923b78a490fd7c3e256dcc83c7a5177314586563356" "checksum better-panic 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "64714970eb2081691f85a923b78a490fd7c3e256dcc83c7a5177314586563356"
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" "checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd"
"checksum bitstream_reader 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ce18d8bbe27f4f79e0221775535a640c40760888431a63c22d92e926c5839c77" "checksum bitstream_reader 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b4ddd36b9f93bc10fb8c9ca3dc7bfd3212cc197244dcada7efd250fc3d7834c0"
"checksum bitstream_reader_derive 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7ae1ff36caa3f111504ee053686356bf98bbc1cfe9a5b71e19bbc8294b04bdb1" "checksum bitstream_reader_derive 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "14e4573210a0cc968772847e1db9705e546f60cc1525c5cbfbf470dfa1fa391c"
"checksum blake2b_simd 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bf775a81bb2d464e20ff170ac20316c7b08a43d11dbc72f0f82e8e8d3d6d0499" "checksum blake2b_simd 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bf775a81bb2d464e20ff170ac20316c7b08a43d11dbc72f0f82e8e8d3d6d0499"
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
"checksum c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7d64d04786e0f528460fc884753cf8dddcc466be308f6026f8e355c41a0e4101" "checksum c2-chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7d64d04786e0f528460fc884753cf8dddcc466be308f6026f8e355c41a0e4101"
@ -819,9 +879,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum err-derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3d8ff65eb6c2fc68e76557239d16f5698fd56603925b89856d3f0f7105fd4543" "checksum err-derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3d8ff65eb6c2fc68e76557239d16f5698fd56603925b89856d3f0f7105fd4543"
"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"
"checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1"
"checksum fs_extra 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5f2a4a2034423744d2cc7ca2068453168dcdb82c438419e639a26bd87839c674"
"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
"checksum getrandom 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "6171a6cc63fbabbe27c2b5ee268e8b7fe5dc1eb0dd2dfad537c1dfed6f69117e" "checksum getrandom 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "6171a6cc63fbabbe27c2b5ee268e8b7fe5dc1eb0dd2dfad537c1dfed6f69117e"
"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f" "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
"checksum jemalloc-sys 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0d3b9f3f5c9b31aa0f5ed3260385ac205db665baa41d49bb8338008ae94ede45"
"checksum jemallocator 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "43ae63fcfc45e99ab3d1b29a46782ad679e98436c3169d15a167a1108a724b69"
"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba"
"checksum lock_api 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f8912e782533a93a167888781b836336a6ca5da6175c05944c86cf28c31104dc" "checksum lock_api 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f8912e782533a93a167888781b836336a6ca5da6175c05944c86cf28c31104dc"
@ -831,6 +894,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32" "checksum num-traits 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "6ba9a427cfca2be13aa6f6403b0b7e7368fe982bfa16fccc450ce74c46cd9b32"
"checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252" "checksum parking_lot 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f842b1982eb6c2fe34036a4fbfb06dd185a3f5c8edfaacdf7d1ea10b07de6252"
"checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b" "checksum parking_lot_core 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b876b1b9e7ac6e1a74a6da34d25c42e17e8862aa409cbbbdcfc8d86c6f3bc62b"
"checksum parse-display 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "718b422bc6b056b6374f7ffc3b2d9b55180a4af59a089835df1963994676d8b6"
"checksum parse-display-derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7cf2deb364a60cc0f633c1ffe619b42463993c91352ae367010b8420e442655"
"checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b" "checksum ppv-lite86 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e3cbf9f658cdb5000fcf6f362b8ea2ba154b9f146a61c7a20d647034c6b6561b"
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
"checksum proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c5c2380ae88876faae57698be9e9775e3544decad214599c3a6266cca6ac802" "checksum proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4c5c2380ae88876faae57698be9e9775e3544decad214599c3a6266cca6ac802"
@ -867,6 +932,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
"checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" "checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5"
"checksum syn 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c65d951ab12d976b61a41cf9ed4531fc19735c6e6d84a4bb1453711e762ec731" "checksum syn 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c65d951ab12d976b61a41cf9ed4531fc19735c6e6d84a4bb1453711e762ec731"
"checksum syn_util 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "50518d8ba51dbea1b0981d7bff56cecc2a20a02f22bf6fcf71a924bc241ee4a0"
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
"checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f" "checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f"
"checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"

View file

@ -1,15 +1,13 @@
extern crate proc_macro; extern crate proc_macro;
use inflector::Inflector;
use lazy_static::lazy_static;
use proc_macro2::{Ident, Literal, Span, TokenStream};
use quote::quote;
use tf_demo_parser::demo::gamevent::{GameEventDefinition, GameEventValueType}; use tf_demo_parser::demo::gamevent::{GameEventDefinition, GameEventValueType};
use tf_demo_parser::demo::message::Message;
use tf_demo_parser::demo::packet::stringtable::StringTableEntry;
use tf_demo_parser::demo::parser::MessageHandler; use tf_demo_parser::demo::parser::MessageHandler;
use tf_demo_parser::{Demo, ParserState}; use tf_demo_parser::{Demo, ParserState};
use tf_demo_parser::{DemoParser, MessageType}; use tf_demo_parser::{DemoParser, MessageType};
use inflector::Inflector;
use lazy_static::lazy_static;
use quote::quote;
use proc_macro2::{Span, Ident, TokenStream, Literal};
struct GameEventAnalyser; struct GameEventAnalyser;
@ -20,16 +18,8 @@ impl MessageHandler for GameEventAnalyser {
false false
} }
fn handle_message(&mut self, _message: Message, _tick: u32) {}
fn handle_string_entry(&mut self, _table: &String, _index: usize, _entry: &StringTableEntry) {}
fn get_output(self, state: ParserState) -> Self::Output { fn get_output(self, state: ParserState) -> Self::Output {
state state.event_definitions
.event_definitions
.into_iter()
.map(|(_, definition)| definition)
.collect()
} }
} }
@ -216,7 +206,7 @@ fn get_event_name(name: &str) -> String {
} }
pub fn generate_game_events(demo: Demo) -> TokenStream { pub fn generate_game_events(demo: Demo) -> TokenStream {
let (_, mut events) = let (_, mut events) =
DemoParser::parse_with_analyser(demo.get_stream(), GameEventAnalyser).unwrap(); DemoParser::parse_with_analyser(demo.get_stream(), GameEventAnalyser).unwrap();
events.sort(); events.sort();
@ -244,19 +234,11 @@ pub fn generate_game_events(demo: Demo) -> TokenStream {
let ty = Ident::new(get_type_name(entry.kind), span); let ty = Ident::new(get_type_name(entry.kind), span);
quote!( quote!(
let #name = match iter.next() { #name: #ty::from_value(iter.next(), #name_str)?,
Some(value) => #ty::from_value(value, #name_str)?,
None => #ty::default()
};
) )
}); });
let field_names = event.entries.iter().map(|entry| { let iter = if event.entries.len() > 0 {
let name = Ident::new(&get_entry_name(&entry.name), span);
quote!(#name,)
});
let iter = if event.entries.len() > 0 {
quote!( quote!(
let mut iter = values.into_iter(); let mut iter = values.into_iter();
) )
@ -264,7 +246,7 @@ pub fn generate_game_events(demo: Demo) -> TokenStream {
quote!() quote!()
}; };
let param_name = if event.entries.len() > 0 { let param_name = if event.entries.len() > 0 {
quote!(values) quote!(values)
} else { } else {
quote!(_values) quote!(_values)
@ -279,10 +261,9 @@ pub fn generate_game_events(demo: Demo) -> TokenStream {
impl FromRawGameEvent for #name { impl FromRawGameEvent for #name {
fn from_raw_event(#param_name: Vec<GameEventValue>) -> Result<Self> { fn from_raw_event(#param_name: Vec<GameEventValue>) -> Result<Self> {
#iter #iter
#(#entry_constructors)*
Ok(#name { Ok(#name {
#(#field_names)* #(#entry_constructors)*
}) })
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -81,14 +81,15 @@ impl GameEventValue {
} }
pub trait FromGameEventValue: Sized { pub trait FromGameEventValue: Sized {
fn from_value(value: GameEventValue, name: &'static str) -> Result<Self>; fn from_value(value: Option<GameEventValue>, name: &'static str) -> Result<Self>;
} }
impl FromGameEventValue for String { impl FromGameEventValue for String {
fn from_value(value: GameEventValue, name: &'static str) -> Result<Self> { fn from_value(value: Option<GameEventValue>, name: &'static str) -> Result<Self> {
match value { match value {
GameEventValue::String(val) => Ok(val), Some(GameEventValue::String(val)) => Ok(val),
_ => Err(MalformedDemoError::InvalidGameEvent { None => Ok(String::default()),
Some(value) => Err(MalformedDemoError::InvalidGameEvent {
expected_type: GameEventValueType::String, expected_type: GameEventValueType::String,
name, name,
found_type: value.get_type(), found_type: value.get_type(),
@ -99,10 +100,11 @@ impl FromGameEventValue for String {
} }
impl FromGameEventValue for f32 { impl FromGameEventValue for f32 {
fn from_value(value: GameEventValue, name: &'static str) -> Result<Self> { fn from_value(value: Option<GameEventValue>, name: &'static str) -> Result<Self> {
match value { match value {
GameEventValue::Float(val) => Ok(val), Some(GameEventValue::Float(val)) => Ok(val),
_ => Err(MalformedDemoError::InvalidGameEvent { None => Ok(f32::default()),
Some(value) => Err(MalformedDemoError::InvalidGameEvent {
expected_type: GameEventValueType::Float, expected_type: GameEventValueType::Float,
name, name,
found_type: value.get_type(), found_type: value.get_type(),
@ -113,10 +115,11 @@ impl FromGameEventValue for f32 {
} }
impl FromGameEventValue for u32 { impl FromGameEventValue for u32 {
fn from_value(value: GameEventValue, name: &'static str) -> Result<Self> { fn from_value(value: Option<GameEventValue>, name: &'static str) -> Result<Self> {
match value { match value {
GameEventValue::Long(val) => Ok(val), Some(GameEventValue::Long(val)) => Ok(val),
_ => Err(MalformedDemoError::InvalidGameEvent { None => Ok(u32::default()),
Some(value) => Err(MalformedDemoError::InvalidGameEvent {
expected_type: GameEventValueType::Long, expected_type: GameEventValueType::Long,
name, name,
found_type: value.get_type(), found_type: value.get_type(),
@ -127,10 +130,11 @@ impl FromGameEventValue for u32 {
} }
impl FromGameEventValue for u16 { impl FromGameEventValue for u16 {
fn from_value(value: GameEventValue, name: &'static str) -> Result<Self> { fn from_value(value: Option<GameEventValue>, name: &'static str) -> Result<Self> {
match value { match value {
GameEventValue::Short(val) => Ok(val), Some(GameEventValue::Short(val)) => Ok(val),
_ => Err(MalformedDemoError::InvalidGameEvent { None => Ok(u16::default()),
Some(value) => Err(MalformedDemoError::InvalidGameEvent {
expected_type: GameEventValueType::Short, expected_type: GameEventValueType::Short,
name, name,
found_type: value.get_type(), found_type: value.get_type(),
@ -141,10 +145,11 @@ impl FromGameEventValue for u16 {
} }
impl FromGameEventValue for u8 { impl FromGameEventValue for u8 {
fn from_value(value: GameEventValue, name: &'static str) -> Result<Self> { fn from_value(value: Option<GameEventValue>, name: &'static str) -> Result<Self> {
match value { match value {
GameEventValue::Byte(val) => Ok(val), Some(GameEventValue::Byte(val)) => Ok(val),
_ => Err(MalformedDemoError::InvalidGameEvent { None => Ok(u8::default()),
Some(value) => Err(MalformedDemoError::InvalidGameEvent {
expected_type: GameEventValueType::Byte, expected_type: GameEventValueType::Byte,
name, name,
found_type: value.get_type(), found_type: value.get_type(),
@ -155,10 +160,11 @@ impl FromGameEventValue for u8 {
} }
impl FromGameEventValue for bool { impl FromGameEventValue for bool {
fn from_value(value: GameEventValue, name: &'static str) -> Result<Self> { fn from_value(value: Option<GameEventValue>, name: &'static str) -> Result<Self> {
match value { match value {
GameEventValue::Boolean(val) => Ok(val), Some(GameEventValue::Boolean(val)) => Ok(val),
_ => Err(MalformedDemoError::InvalidGameEvent { None => Ok(bool::default()),
Some(value) => Err(MalformedDemoError::InvalidGameEvent {
expected_type: GameEventValueType::Boolean, expected_type: GameEventValueType::Boolean,
name, name,
found_type: value.get_type(), found_type: value.get_type(),
@ -169,10 +175,10 @@ impl FromGameEventValue for bool {
} }
impl FromGameEventValue for () { impl FromGameEventValue for () {
fn from_value(value: GameEventValue, name: &'static str) -> Result<Self> { fn from_value(value: Option<GameEventValue>, name: &'static str) -> Result<Self> {
match value { match value {
GameEventValue::Local => Ok(()), Some(GameEventValue::Local) | None => Ok(()),
_ => Err(MalformedDemoError::InvalidGameEvent { Some(value) => Err(MalformedDemoError::InvalidGameEvent {
expected_type: GameEventValueType::Local, expected_type: GameEventValueType::Local,
name, name,
found_type: value.get_type(), found_type: value.get_type(),

View file

@ -75,6 +75,12 @@ impl From<GameEventTypeId> for usize {
} }
} }
impl From<GameEventTypeId> for u16 {
fn from(id: GameEventTypeId) -> Self {
id.0
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct GameEventListMessage { pub struct GameEventListMessage {
pub event_list: Vec<GameEventDefinition>, pub event_list: Vec<GameEventDefinition>,