1use std::fmt::{Debug, Display, Formatter};
2use std::ops::*;
3use crate::misc::hclient::{HClient, CUR_CLIENT};
4use super::packetvariable::PacketVariable;
5
6#[derive(PartialEq, Eq, Hash, Clone, Copy, Default)]
11pub struct LegacyId(pub i64);
12
13#[derive(PartialEq, Eq, Hash, Clone, Copy, Default)]
18pub struct LegacyLength(pub i32);
19
20#[derive(PartialEq, Eq, Hash, Clone, Copy, Default)]
26pub struct LegacyStringId(pub i64);
27
28#[derive(PartialEq, Clone, Copy, Default)]
34pub struct LegacyDouble(pub f64);
35
36macro_rules! impl_op {
37 ($legacy_name:ident, $legacy_ty:ident => $($op_name:ident, $op_ass_name:ident, $function_name:ident, $function_ass_name:ident, $op:expr, $($ty:ident) +); +;) => ($(
38 impl $op_name for $legacy_name {
39 type Output = Self;
40
41 fn $function_name(self, rhs: Self) -> Self::Output {
42 Self(($op)(self.0, rhs.0))
43 }
44 }
45
46 impl $op_ass_name for $legacy_name {
47 fn $function_ass_name(&mut self, rhs: Self) {
48 *self = Self(($op)(self.0, rhs.0))
49 }
50 }
51
52 $(
53 impl $op_name<$ty> for $legacy_name {
54 type Output = Self;
55
56 fn $function_name(self, rhs: $ty) -> Self::Output {
57 Self(($op)(self.0, rhs as $legacy_ty))
58 }
59 }
60
61 impl $op_ass_name<$ty> for $legacy_name {
62 fn $function_ass_name(&mut self, rhs: $ty) {
63 *self = Self(($op)(self.0, rhs as $legacy_ty))
64 }
65 }
66 )+
67 )+)
68}
69
70macro_rules! impl_legacy {
71 ($($name:ident, $ty_max:ident, $ty_flash:ident, $ty_unity:ident); +;) => ($(
72 impl Deref for $name {
73 type Target = $ty_max;
74
75 fn deref(&self) -> &Self::Target {
76 &self.0
77 }
78 }
79
80 impl DerefMut for $name {
81 fn deref_mut(&mut self) -> &mut Self::Target {
82 &mut self.0
83 }
84 }
85
86 impl Debug for $name {
87 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
88 write!(f, "{}", self.0)
89 }
90 }
91
92 impl Display for $name {
93 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
94 write!(f, "{}", self.0)
95 }
96 }
97
98 impl Neg for $name {
99 type Output = Self;
100
101 fn neg(self) -> Self::Output {
102 Self(-(self.0))
103 }
104 }
105
106 impl_op! {
107 $name, $ty_max =>
108 Add, AddAssign, add, add_assign, | a, b | a + b, $ty_flash $ty_unity;
109 Div, DivAssign, div, div_assign, | a, b | a / b, $ty_flash $ty_unity;
110 Mul, MulAssign, mul, mul_assign, | a, b | a * b, $ty_flash $ty_unity;
111 Rem, RemAssign, rem, rem_assign, | a, b | a % b, $ty_flash $ty_unity;
112 Sub, SubAssign, sub, sub_assign, | a, b | a - b, $ty_flash $ty_unity;
113 }
114 )+)
115}
116
117macro_rules! impl_legacy_bitwise {
118 ($($name:ident, $ty_max:ident, $ty_flash:ident, $ty_unity:ident); +;) => ($(
119 impl Not for $name {
120 type Output = Self;
121
122 fn not(self) -> Self::Output {
123 Self(!self.0)
124 }
125 }
126
127 impl_op! {
128 $name, $ty_max =>
129 BitAnd, BitAndAssign, bitand, bitand_assign, | a, b | a & b, $ty_flash $ty_unity;
130 BitOr, BitOrAssign, bitor, bitor_assign, | a, b | a | b, $ty_flash $ty_unity;
131 BitXor, BitXorAssign, bitxor, bitxor_assign, | a, b | a ^ b, $ty_flash $ty_unity;
132 Shl, ShlAssign, shl, shl_assign, | a, b | a << b, $ty_flash $ty_unity;
133 Shr, ShrAssign, shr, shr_assign, | a, b | a >> b, $ty_flash $ty_unity;
134 }
135 )+)
136}
137
138impl_legacy! {
139 LegacyId, i64, i32, i64;
140 LegacyLength, i32, i32, i16;
141 LegacyStringId, i64, i32, i64;
142 LegacyDouble, f64, f32, f64;
143}
144
145impl_legacy_bitwise! {
146 LegacyId, i64, i32, i64;
147 LegacyLength, i32, i32, i16;
148 LegacyStringId, i64, i32, i64;
149}
150
151impl PacketVariable for LegacyId {
152 fn from_packet(bytes: Vec<u8>) -> (Self, usize) where Self: Sized {
153 if *CUR_CLIENT.lock().unwrap() == HClient::Unity {
154 (Self(i64::from_packet(bytes).0), 8)
155 } else {
156 (Self(i32::from_packet(bytes).0 as i64), 4)
157 }
158 }
159
160 fn to_packet(&self) -> Vec<u8> {
161 if *CUR_CLIENT.lock().unwrap() == HClient::Unity {
162 (self.0).to_packet()
163 } else {
164 (self.0 as i32).to_packet()
165 }
166 }
167}
168
169impl PacketVariable for LegacyLength {
170 fn from_packet(bytes: Vec<u8>) -> (Self, usize) where Self: Sized {
171 if *CUR_CLIENT.lock().unwrap() == HClient::Unity {
172 (Self(i16::from_packet(bytes).0 as i32), 8)
173 } else {
174 (Self(i32::from_packet(bytes).0), 4)
175 }
176 }
177
178 fn to_packet(&self) -> Vec<u8> {
179 if *CUR_CLIENT.lock().unwrap() == HClient::Unity {
180 (self.0 as i16).to_packet()
181 } else {
182 (self.0).to_packet()
183 }
184 }
185}
186
187impl PacketVariable for LegacyStringId {
188 fn from_packet(bytes: Vec<u8>) -> (Self, usize) where Self: Sized {
189 if *CUR_CLIENT.lock().unwrap() == HClient::Unity {
190 (Self(i64::from_packet(bytes).0), 8)
191 } else {
192 let (s, size) = String::from_packet(bytes);
193 (Self(s.parse::<i64>().unwrap()), size)
194 }
195 }
196
197 fn to_packet(&self) -> Vec<u8> {
198 if *CUR_CLIENT.lock().unwrap() == HClient::Unity {
199 (self.0).to_packet()
200 } else {
201 let res = (self.0.to_string()).to_packet();
202 res
203 }
204 }
205}
206
207impl PacketVariable for LegacyDouble {
208 fn from_packet(bytes: Vec<u8>) -> (Self, usize) where Self: Sized {
209 if *CUR_CLIENT.lock().unwrap() == HClient::Unity {
210 (Self(f64::from_packet(bytes).0), 8)
211 } else {
212 let (s, size) = String::from_packet(bytes);
213 (Self(s.parse::<f64>().unwrap()), size)
214 }
215 }
216
217 fn to_packet(&self) -> Vec<u8> {
218 if *CUR_CLIENT.lock().unwrap() == HClient::Unity {
219 (self.0).to_packet()
220 } else {
221 let res = (self.0.to_string()).to_packet();
222 res
223 }
224 }
225}