Skip to main content

hao_il2cpp_unity/
values.rs

1//! Unity structs and enums, laid out as the game passes them.
2
3use 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    /// `UnityEngine.Vector2`.
24    Vector2 = "UnityEngine" "Vector2" {
25        /// Horizontal; increases to the right.
26        x: f32,
27        /// Vertical; increases upwards.
28        y: f32
29    }
30);
31value_type!(
32    /// `UnityEngine.Vector3`.
33    Vector3 = "UnityEngine" "Vector3" {
34        /// Right (positive) and left.
35        x: f32,
36        /// Up (positive) and down.
37        y: f32,
38        /// Forward (positive) and back; Unity is left-handed.
39        z: f32
40    }
41);
42value_type!(
43    /// `UnityEngine.Color`, with components from 0 to 1.
44    Color = "UnityEngine" "Color" {
45        /// Red.
46        r: f32,
47        /// Green.
48        g: f32,
49        /// Blue.
50        b: f32,
51        /// Opacity: 0 is transparent, 1 opaque.
52        a: f32
53    }
54);
55value_type!(
56    /// `UnityEngine.Quaternion`, a rotation. Use [`Quaternion::euler`] to build one.
57    Quaternion = "UnityEngine" "Quaternion" {
58        /// X of the vector part (the rotation axis scaled by `sin(angle / 2)`).
59        x: f32,
60        /// Y of the vector part.
61        y: f32,
62        /// Z of the vector part.
63        z: f32,
64        /// The scalar part, `cos(angle / 2)`; 1 with a zero vector part is no rotation.
65        w: f32
66    }
67);
68
69impl Vector2 {
70    /// `(0, 0)`.
71    pub const ZERO: Self = Self::new(0.0, 0.0);
72    /// `(1, 1)`.
73    pub const ONE: Self = Self::new(1.0, 1.0);
74
75    /// A vector from its components (usable in `const`s).
76    pub const fn new(x: f32, y: f32) -> Self {
77        Self { x, y }
78    }
79
80    /// The vector's length.
81    pub fn magnitude(self) -> f32 {
82        (self.x * self.x + self.y * self.y).sqrt()
83    }
84
85    /// The same direction with length 1, or [`ZERO`](Self::ZERO) for a zero vector.
86    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    /// `(0, 0, 0)`.
119    pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);
120    /// `(1, 1, 1)`, e.g. the default scale.
121    pub const ONE: Self = Self::new(1.0, 1.0, 1.0);
122    /// `(0, 1, 0)`, world up.
123    pub const UP: Self = Self::new(0.0, 1.0, 0.0);
124    /// `(0, 0, 1)`, world forward.
125    pub const FORWARD: Self = Self::new(0.0, 0.0, 1.0);
126
127    /// A vector from its components (usable in `const`s).
128    pub const fn new(x: f32, y: f32, z: f32) -> Self {
129        Self { x, y, z }
130    }
131
132    /// The dot product: the cosine of the angle between two unit vectors.
133    pub fn dot(self, o: Self) -> f32 {
134        self.x * o.x + self.y * o.y + self.z * o.z
135    }
136
137    /// The vector's length.
138    pub fn magnitude(self) -> f32 {
139        self.dot(self).sqrt()
140    }
141
142    /// The same direction with length 1, or [`ZERO`](Self::ZERO) for a zero vector.
143    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    /// The distance between two points.
153    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    /// Opaque white.
181    pub const WHITE: Self = Self::rgb(1.0, 1.0, 1.0);
182    /// Opaque black.
183    pub const BLACK: Self = Self::rgb(0.0, 0.0, 0.0);
184    /// Opaque red.
185    pub const RED: Self = Self::rgb(1.0, 0.0, 0.0);
186    /// Opaque green.
187    pub const GREEN: Self = Self::rgb(0.0, 1.0, 0.0);
188    /// Opaque blue.
189    pub const BLUE: Self = Self::rgb(0.0, 0.0, 1.0);
190    /// Unity's `Color.yellow`, which is slightly orange: `(1, 0.92, 0.016)`.
191    pub const YELLOW: Self = Self::rgb(1.0, 0.92, 0.016);
192    /// Fully transparent, e.g. to clear an overlay.
193    pub const CLEAR: Self = Self::rgba(0.0, 0.0, 0.0, 0.0);
194
195    /// An opaque colour, each component from 0 to 1.
196    pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
197        Self::rgba(r, g, b, 1.0)
198    }
199
200    /// A colour with opacity `a` (0 transparent, 1 opaque).
201    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
202        Self { r, g, b, a }
203    }
204
205    /// The colour with its alpha replaced.
206    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    /// `UnityEngine.SceneManagement.LoadSceneMode`.
245    LoadSceneMode = "UnityEngine.SceneManagement" "LoadSceneMode" {
246        /// The scene replaced every loaded scene.
247        SINGLE = 0,
248        /// The scene was loaded alongside the ones already loaded.
249        ADDITIVE = 1
250    }
251);
252int_enum!(
253    /// `UnityEngine.RenderMode`, how a [`Canvas`](crate::Canvas) is drawn.
254    RenderMode = "UnityEngine" "RenderMode" {
255        /// Drawn over the whole screen, on top of every camera.
256        SCREEN_SPACE_OVERLAY = 0,
257        /// Drawn over the screen by a chosen camera, at a distance in front of it.
258        SCREEN_SPACE_CAMERA = 1,
259        /// A flat object placed in the scene like any other.
260        WORLD_SPACE = 2
261    }
262);
263int_enum!(
264    /// `UnityEngine.TextureFormat`.
265    TextureFormat = "UnityEngine" "TextureFormat" {
266        /// One byte per pixel: alpha only.
267        ALPHA8 = 1,
268        /// Four bytes per pixel in R, G, B, A order; the usual choice for raw pixels.
269        RGBA32 = 4,
270        /// Four bytes per pixel in A, R, G, B order.
271        ARGB32 = 5,
272        /// Three bytes per pixel, no alpha.
273        RGB24 = 3
274    }
275);
276int_enum!(
277    /// `UnityEngine.FilterMode`, how a texture is sampled. `POINT` samples the
278    /// nearest texel exactly (no blending with neighbours).
279    FilterMode = "UnityEngine" "FilterMode" {
280        /// Nearest texel, for crisp pixel art and glyph atlases.
281        POINT = 0,
282        /// Blends the four nearest texels.
283        BILINEAR = 1,
284        /// Bilinear, also blended between mipmap levels.
285        TRILINEAR = 2
286    }
287);
288int_enum!(
289    /// `UnityEngine.HideFlags`. `HIDE_AND_DONT_SAVE` keeps a runtime-created
290    /// object (e.g. a `Material`) from being destroyed on scene load.
291    HideFlags = "UnityEngine" "HideFlags" {
292        /// Normal: visible, saved, and unloaded with its scene.
293        NONE = 0,
294        /// Not unloaded by `Resources.UnloadUnusedAssets`.
295        DONT_UNLOAD_UNUSED_ASSET = 32,
296        /// Hidden, never saved, and not unloaded on scene load; the owner must destroy it.
297        HIDE_AND_DONT_SAVE = 61
298    }
299);
300int_enum!(
301    /// `UnityEngine.KeyCode`, for [`input`](crate::input). A few common keys are
302    /// named; any other can be made with `KeyCode(value)`.
303    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);