From 316e3ee06f5ee0405e4cab567e67f4315c0f2cfb Mon Sep 17 00:00:00 2001 From: xavo95 Date: Sun, 25 May 2025 00:41:42 +0700 Subject: [PATCH] Port strinova proto debug feature --- Cargo.toml | 19 ++++++ wicked-waifus-protocol-derive/Cargo.toml | 6 +- wicked-waifus-protocol/Cargo.toml | 23 ++++--- wicked-waifus-protocol/build.rs | 82 ++++++++++++++---------- 4 files changed, 84 insertions(+), 46 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 12b73cf..c554a5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,23 @@ edition = "2024" version = "0.1.0" [workspace.dependencies] +byteorder = "1.5.0" +crc32fast = "1.4.2" +prettyplease = "0.2.32" +proc-macro2 = "1.0.95" +prost = "0.13.5" +prost-build = "0.13.5" +quote = "1.0.40" # +serde = { version = "1.0.219", features = ["derive"] } +serde_json = "1.0.140" +syn = "2.0.101" # +thiserror = "2.0.12" + +# Internal wicked-waifus-protocol-derive = { path = "wicked-waifus-protocol-derive" } + +[profile.release] +strip = true # Automatically strip symbols from the binary. +lto = true # Link-time optimization. +opt-level = 3 # Optimization level 3. +codegen-units = 1 # Maximum size reduction optimizations. diff --git a/wicked-waifus-protocol-derive/Cargo.toml b/wicked-waifus-protocol-derive/Cargo.toml index 44d7fc3..04b91dc 100644 --- a/wicked-waifus-protocol-derive/Cargo.toml +++ b/wicked-waifus-protocol-derive/Cargo.toml @@ -4,9 +4,9 @@ edition.workspace = true version.workspace = true [dependencies] -syn = "2.0.100" -quote = "1.0.40" -proc-macro2 = "1.0.94" +syn.workspace = true +quote.workspace = true +proc-macro2.workspace = true [lib] proc-macro = true diff --git a/wicked-waifus-protocol/Cargo.toml b/wicked-waifus-protocol/Cargo.toml index 403b0e7..16c872c 100644 --- a/wicked-waifus-protocol/Cargo.toml +++ b/wicked-waifus-protocol/Cargo.toml @@ -3,17 +3,20 @@ name = "wicked-waifus-protocol" edition.workspace = true version.workspace = true +[features] +debug = ["dep:serde", "dep:serde_json"] + [dependencies] -byteorder = "1.5.0" -crc32fast = "1.4.2" -thiserror = "2.0.12" -prost = "0.13.5" -serde = { version = "1.0.219", features = ["derive"] } -serde_json = "1.0.140" +byteorder.workspace = true +crc32fast.workspace = true +prost.workspace = true +serde = { workspace = true, optional = true } +serde_json = { workspace = true, optional = true } +thiserror.workspace = true wicked-waifus-protocol-derive.workspace = true [build-dependencies] -prost-build = "0.13.5" -quote = "1.0.40" -syn = "2.0.100" -prettyplease = "0.2.31" +prettyplease.workspace = true +prost-build.workspace = true +quote.workspace = true +syn.workspace = true \ No newline at end of file diff --git a/wicked-waifus-protocol/build.rs b/wicked-waifus-protocol/build.rs index 5402b44..fa725cb 100644 --- a/wicked-waifus-protocol/build.rs +++ b/wicked-waifus-protocol/build.rs @@ -24,15 +24,28 @@ pub fn main() { if Path::new(&proto_file).exists() { println!("cargo:rerun-if-changed={proto_file}"); - prost_build::Config::new() - .out_dir(CODEGEN_OUT_DIR) - .default_package_filename("wicked-waifus") - .type_attribute(".", "#[derive(wicked_waifus_protocol_derive::MessageID)]") - .type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]") - .compile_protos(&[proto_file], &["wicked-waifus"]) - .unwrap(); + #[cfg(feature = "debug")] + { + prost_build::Config::new() + .out_dir(CODEGEN_OUT_DIR) + .default_package_filename("wicked-waifus") + .type_attribute(".", "#[derive(wicked_waifus_protocol_derive::MessageID)]") + .type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]") + .compile_protos(&[proto_file], &["wicked-waifus"]) + .unwrap(); + + impl_dumper(Path::new("generated/wicked-waifus.rs")).unwrap(); + } + #[cfg(not(feature = "debug"))] + { + prost_build::Config::new() + .out_dir(CODEGEN_OUT_DIR) + .default_package_filename("wicked-waifus") + .type_attribute(".", "#[derive(wicked_waifus_protocol_derive::MessageID)]") + .compile_protos(&[proto_file], &["wicked-waifus"]) + .unwrap(); + } - impl_dumper(Path::new("generated/wicked-waifus.rs")).unwrap(); impl_message_id(Path::new("generated/wicked-waifus.rs")).unwrap(); } } @@ -134,35 +147,38 @@ pub fn impl_dumper(codegen_path: &Path) -> io::Result<()> { } } - let generated_code = quote! { - pub mod proto_dumper { - use prost::Message; - use crate::*; - use crate::ai::*; - use crate::combat_message::*; - use crate::debug::*; - use crate::summon::*; + if !match_arms.is_empty() { + let generated_code = quote! { + pub mod proto_dumper { + use prost::Message; + use crate::*; + use crate::ai::*; + use crate::combat_message::*; + use crate::debug::*; + use crate::summon::*; - #[derive(thiserror::Error, Debug)] - pub enum Error { - #[error("serde_json::Error: {0}")] - Json(#[from] serde_json::Error), - #[error("serde_json::Error: {0}")] - Decode(#[from] prost::DecodeError), - } + #[derive(thiserror::Error, Debug)] + pub enum Error { + #[error("serde_json::Error: {0}")] + Json(#[from] serde_json::Error), + #[error("serde_json::Error: {0}")] + Decode(#[from] prost::DecodeError), + } - pub fn get_debug_info(id: u16, data: &[u8]) -> Result<(&str, String), Error> { - match id { - #match_arms - _ => Ok(("UnknownType", "".to_string())), + pub fn get_debug_info(id: u16, data: &[u8]) -> Result<(&str, String), Error> { + match id { + #match_arms + _ => Ok(("UnknownType", "".to_string())), + } } } - } - }; + }; + + let syntax_tree = syn::parse2(generated_code).unwrap(); + let formatted = prettyplease::unparse(&syntax_tree); + let mut file = fs::OpenOptions::new().append(true).open(codegen_path)?; + file.write(formatted.as_bytes())?; + } - let syntax_tree = syn::parse2(generated_code).unwrap(); - let formatted = prettyplease::unparse(&syntax_tree); - let mut file = fs::OpenOptions::new().append(true).open(codegen_path)?; - file.write(formatted.as_bytes())?; Ok(()) } \ No newline at end of file