feat!: Refactor, Update to version 2.5.x, Add Battle Scepter
This commit is contained in:
@@ -6,6 +6,7 @@ version.workspace = true
|
||||
[dependencies]
|
||||
prost.workspace = true
|
||||
prost-types.workspace = true
|
||||
proto-derive.workspace = true
|
||||
|
||||
[build-dependencies]
|
||||
prost-build.workspace = true
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
use std::{
|
||||
fs,
|
||||
io::{self, BufRead},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
pub fn main() {
|
||||
let proto_file = "StarRail.proto";
|
||||
if std::path::Path::new(proto_file).exists() {
|
||||
@@ -5,7 +11,38 @@ pub fn main() {
|
||||
|
||||
prost_build::Config::new()
|
||||
.out_dir("out/")
|
||||
.type_attribute(".", "#[derive(proto_derive::CmdID)]")
|
||||
.compile_protos(&[proto_file], &["."])
|
||||
.unwrap();
|
||||
|
||||
impl_message_id(Path::new("out/_.rs")).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn impl_message_id(path: &Path) -> io::Result<()> {
|
||||
let file = fs::File::open(path)?;
|
||||
let reader = io::BufReader::new(file);
|
||||
let mut output = Vec::new();
|
||||
|
||||
let mut attr = None;
|
||||
for line in reader.lines() {
|
||||
let line = line?;
|
||||
|
||||
if line.contains("CmdID:") {
|
||||
attr = Some(make_message_id_attr(&line).unwrap());
|
||||
} else {
|
||||
output.push(line);
|
||||
if let Some(attr) = attr.take() {
|
||||
output.push(attr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fs::write(path, output.join("\n").as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn make_message_id_attr(line: &str) -> Option<String> {
|
||||
let id = line.trim_start().split(' ').nth(2)?.parse::<u16>().ok()?;
|
||||
Some(format!("#[cmdid({id})]"))
|
||||
}
|
||||
|
||||
+50967
-11845
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,12 @@
|
||||
[package]
|
||||
name = "proto-derive"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
syn = "2.0.53"
|
||||
quote = "1.0.35"
|
||||
proc-macro2 = "1.0.79"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
@@ -0,0 +1,27 @@
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{quote, ToTokens};
|
||||
use syn::{parse_macro_input, DeriveInput, Meta, MetaList};
|
||||
|
||||
#[proc_macro_derive(CmdID, attributes(cmdid))]
|
||||
pub fn message_id_derive(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
let struct_name = input.ident;
|
||||
|
||||
let id = match input
|
||||
.attrs
|
||||
.iter()
|
||||
.find(|attr| attr.path().is_ident("cmdid"))
|
||||
{
|
||||
Some(attr) => match attr.meta {
|
||||
Meta::List(MetaList { ref tokens, .. }) => tokens.into_token_stream(),
|
||||
_ => panic!("Invalid cmdid attribute value"),
|
||||
},
|
||||
_ => 0u16.into_token_stream(),
|
||||
};
|
||||
|
||||
TokenStream::from(quote! {
|
||||
impl crate::CmdID for #struct_name {
|
||||
const CMD_ID: u16 = #id;
|
||||
}
|
||||
})
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
+11
-3
@@ -1,4 +1,12 @@
|
||||
mod cmd_types;
|
||||
pub use cmd_types::*;
|
||||
|
||||
include!("../out/_.rs");
|
||||
|
||||
pub use prost::DecodeError as ProtobufDecodeError;
|
||||
pub use prost::Message as Protobuf;
|
||||
|
||||
pub trait CmdID {
|
||||
const CMD_ID: u16;
|
||||
|
||||
fn get_cmd_id(&self) -> u16 {
|
||||
Self::CMD_ID
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user