1use hao_il2cpp_runtime::{element, Il2CppValue, TypeInfo};
4
5macro_rules! value_type {
6 ($(#[$doc:meta])* $name:ident = $namespace:literal $class:literal { $($(#[$field_doc:meta])* $field:ident: $t:ty),* }) => {
7 $(#[$doc])*
8 #[repr(C)]
9 #[derive(Debug, Clone, Copy, PartialEq, Default)]
10 pub struct $name {
11 $($(#[$field_doc])* pub $field: $t,)*
12 }
13
14 unsafe impl Il2CppValue for $name {
15 fn matches(ty: TypeInfo) -> bool {
16 ty.is_value_type($namespace, $class)
17 }
18 }
19 };
20}
21
22value_type!(
23 Vector2 = "UnityEngine" "Vector2" {
25 x: f32,
27 y: f32
29 }
30);
31value_type!(
32 Vector3 = "UnityEngine" "Vector3" {
34 x: f32,
36 y: f32,
38 z: f32
40 }
41);
42value_type!(
43 Color = "UnityEngine" "Color" {
45 r: f32,
47 g: f32,
49 b: f32,
51 a: f32
53 }
54);
55value_type!(
56 Quaternion = "UnityEngine" "Quaternion" {
58 x: f32,
60 y: f32,
62 z: f32,
64 w: f32
66 }
67);
68
69impl Vector2 {
70 pub const ZERO: Self = Self::new(0.0, 0.0);
72 pub const ONE: Self = Self::new(1.0, 1.0);
74
75 pub const fn new(x: f32, y: f32) -> Self {
77 Self { x, y }
78 }
79
80 pub fn magnitude(self) -> f32 {
82 (self.x * self.x + self.y * self.y).sqrt()
83 }
84
85 pub fn normalized(self) -> Self {
87 let m = self.magnitude();
88 if m > 0.0 {
89 Self::new(self.x / m, self.y / m)
90 } else {
91 Self::ZERO
92 }
93 }
94}
95
96impl std::ops::Add for Vector2 {
97 type Output = Self;
98 fn add(self, o: Self) -> Self {
99 Self::new(self.x + o.x, self.y + o.y)
100 }
101}
102
103impl std::ops::Sub for Vector2 {
104 type Output = Self;
105 fn sub(self, o: Self) -> Self {
106 Self::new(self.x - o.x, self.y - o.y)
107 }
108}
109
110impl std::ops::Mul<f32> for Vector2 {
111 type Output = Self;
112 fn mul(self, s: f32) -> Self {
113 Self::new(self.x * s, self.y * s)
114 }
115}
116
117impl Vector3 {
118 pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);
120 pub const ONE: Self = Self::new(1.0, 1.0, 1.0);
122 pub const UP: Self = Self::new(0.0, 1.0, 0.0);
124 pub const FORWARD: Self = Self::new(0.0, 0.0, 1.0);
126
127 pub const fn new(x: f32, y: f32, z: f32) -> Self {
129 Self { x, y, z }
130 }
131
132 pub fn dot(self, o: Self) -> f32 {
134 self.x * o.x + self.y * o.y + self.z * o.z
135 }
136
137 pub fn magnitude(self) -> f32 {
139 self.dot(self).sqrt()
140 }
141
142 pub fn normalized(self) -> Self {
144 let m = self.magnitude();
145 if m > 0.0 {
146 self * (1.0 / m)
147 } else {
148 Self::ZERO
149 }
150 }
151
152 pub fn distance(self, o: Self) -> f32 {
154 (self - o).magnitude()
155 }
156}
157
158impl std::ops::Add for Vector3 {
159 type Output = Self;
160 fn add(self, o: Self) -> Self {
161 Self::new(self.x + o.x, self.y + o.y, self.z + o.z)
162 }
163}
164
165impl std::ops::Sub for Vector3 {
166 type Output = Self;
167 fn sub(self, o: Self) -> Self {
168 Self::new(self.x - o.x, self.y - o.y, self.z - o.z)
169 }
170}
171
172impl std::ops::Mul<f32> for Vector3 {
173 type Output = Self;
174 fn mul(self, s: f32) -> Self {
175 Self::new(self.x * s, self.y * s, self.z * s)
176 }
177}
178
179impl Color {
180 pub const WHITE: Self = Self::rgb(1.0, 1.0, 1.0);
182 pub const BLACK: Self = Self::rgb(0.0, 0.0, 0.0);
184 pub const RED: Self = Self::rgb(1.0, 0.0, 0.0);
186 pub const GREEN: Self = Self::rgb(0.0, 1.0, 0.0);
188 pub const BLUE: Self = Self::rgb(0.0, 0.0, 1.0);
190 pub const YELLOW: Self = Self::rgb(1.0, 0.92, 0.016);
192 pub const CLEAR: Self = Self::rgba(0.0, 0.0, 0.0, 0.0);
194
195 pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
197 Self::rgba(r, g, b, 1.0)
198 }
199
200 pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
202 Self { r, g, b, a }
203 }
204
205 pub const fn with_alpha(self, a: f32) -> Self {
207 Self {
208 r: self.r,
209 g: self.g,
210 b: self.b,
211 a,
212 }
213 }
214}
215
216macro_rules! int_enum {
217 ($(#[$doc:meta])* $name:ident = $namespace:literal $class:literal { $($(#[$variant_doc:meta])* $variant:ident = $value:literal),* }) => {
218 $(#[$doc])*
219 #[repr(transparent)]
220 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
221 pub struct $name(
222 #[doc = concat!("The raw `", $class, "` value.")]
223 pub i32,
224 );
225
226 impl $name {
227 $(
228 $(#[$variant_doc])*
229 #[doc = ""]
230 #[doc = concat!("`", $class, "` value `", stringify!($value), "`.")]
231 pub const $variant: Self = Self($value);
232 )*
233 }
234
235 unsafe impl Il2CppValue for $name {
236 fn matches(ty: TypeInfo) -> bool {
237 !ty.byref && ty.is_enum && ty.element == element::I4 && ty.namespace == $namespace && ty.name == $class
238 }
239 }
240 };
241}
242
243int_enum!(
244 LoadSceneMode = "UnityEngine.SceneManagement" "LoadSceneMode" {
246 SINGLE = 0,
248 ADDITIVE = 1
250 }
251);
252int_enum!(
253 RenderMode = "UnityEngine" "RenderMode" {
255 SCREEN_SPACE_OVERLAY = 0,
257 SCREEN_SPACE_CAMERA = 1,
259 WORLD_SPACE = 2
261 }
262);
263int_enum!(
264 TextureFormat = "UnityEngine" "TextureFormat" {
266 ALPHA8 = 1,
268 RGBA32 = 4,
270 ARGB32 = 5,
272 RGB24 = 3
274 }
275);
276int_enum!(
277 FilterMode = "UnityEngine" "FilterMode" {
280 POINT = 0,
282 BILINEAR = 1,
284 TRILINEAR = 2
286 }
287);
288int_enum!(
289 HideFlags = "UnityEngine" "HideFlags" {
292 NONE = 0,
294 DONT_UNLOAD_UNUSED_ASSET = 32,
296 HIDE_AND_DONT_SAVE = 61
298 }
299);
300int_enum!(
301 KeyCode = "UnityEngine" "KeyCode" {
304 BACKSPACE = 8, TAB = 9, RETURN = 13, ESCAPE = 27, SPACE = 32, DELETE = 127,
305 LEFT = 276, RIGHT = 275, UP = 273, DOWN = 274,
306 INSERT = 277, HOME = 278, END = 279, PAGE_UP = 280, PAGE_DOWN = 281,
307 ALPHA0 = 48, ALPHA1 = 49, ALPHA2 = 50, ALPHA3 = 51,
308 A = 97, B = 98, C = 99, D = 100, E = 101, F = 102, G = 103, H = 104,
309 I = 105, J = 106, K = 107, L = 108, M = 109, N = 110, O = 111, P = 112,
310 Q = 113, R = 114, S = 115, T = 116, U = 117, V = 118, W = 119, X = 120,
311 Y = 121, Z = 122,
312 F1 = 282, F2 = 283, F3 = 284, F4 = 285, F5 = 286, F6 = 287,
313 RIGHT_SHIFT = 303, LEFT_SHIFT = 304, RIGHT_CONTROL = 305, LEFT_CONTROL = 306,
314 RIGHT_ALT = 307, LEFT_ALT = 308
315 }
316);