1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use proc_macro::TokenStream;
use syn::{AttributeArgs, DeriveInput, parse_macro_input};
use quote::quote;
#[proc_macro_derive(SteamMsg)]
pub fn steammsg_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_steammsg_macro(&ast)
}
fn impl_steammsg_macro(ast: &DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl SerializableBytes for #name {
fn to_bytes(&self) -> Vec<u8> {
bincode::serialize(&self).unwrap()
}
}
impl DeserializableBytes for #name {
fn from_bytes(packet_data: &[u8]) -> Self {
let decoded: Self = bincode::deserialize(packet_data).unwrap();
decoded
}
}
impl MessageBodyExt for #name {
fn split_from_bytes(data: &[u8]) -> (&[u8], &[u8]) {
let size = std::mem::size_of::<Self>();
(&data[..size], &data[size..])
}
}
};
gen.into()
}
#[proc_macro_derive(MsgHeader)]
pub fn header_derive(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
impl_header_macro(&ast)
}
fn impl_header_macro(ast: &DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl SerializableBytes for #name {
fn to_bytes(&self) -> Vec<u8> {
bincode::serialize(&self).unwrap()
}
}
impl DeserializableBytes for #name {
fn from_bytes(packet_data: &[u8]) -> Self {
let decoded: Self = bincode::deserialize(packet_data).unwrap();
decoded
}
}
impl MessageHeaderExt for #name {
fn split_from_bytes(data: &[u8]) -> (&[u8], &[u8]) {
let size = std::mem::size_of::<Self>();
(&data[..size], &data[size..])
}
fn create() -> Self {
Self::new()
}
}
impl MessageHeader for #name {
fn set_target(&mut self, new_target: u64) {
self.target_job_id = new_target;
}
fn set_source(&mut self, new_source: u64) {
self.source_job_id = new_source;
}
fn target(&self) -> u64 {
self.target_job_id
}
fn source(&self) -> u64 {
self.source_job_id
}
}
};
gen.into()
}
#[proc_macro_attribute]
pub fn linked_emsg(attr: TokenStream, item: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(item).unwrap();
let name = &ast.ident;
let mut args: AttributeArgs = parse_macro_input!(attr as AttributeArgs);
let attribute = args.pop().unwrap();
let tokens = quote! {
#ast
impl HasEMsg for #name {
fn emsg() -> EMsg {
#attribute
}
fn create() -> Self {
Self::new()
}
}
};
tokens.into()
}