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:ident: $t:ty),* }) => {
7        $(#[$doc])*
8        #[repr(C)]
9        #[derive(Debug, Clone, Copy, PartialEq, Default)]
10        pub struct $name {
11            $(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" { x: f32, y: f32 }
25);
26value_type!(
27    /// `UnityEngine.Vector3`.
28    Vector3 = "UnityEngine" "Vector3" { x: f32, y: f32, z: f32 }
29);
30value_type!(
31    /// `UnityEngine.Color`, with components from 0 to 1.
32    Color = "UnityEngine" "Color" { r: f32, g: f32, b: f32, a: f32 }
33);
34value_type!(
35    /// `UnityEngine.Quaternion`, a rotation. Use [`Quaternion::euler`] to build one.
36    Quaternion = "UnityEngine" "Quaternion" { x: f32, y: f32, z: f32, w: f32 }
37);
38
39impl Vector2 {
40    pub const ZERO: Self = Self::new(0.0, 0.0);
41    pub const ONE: Self = Self::new(1.0, 1.0);
42
43    pub const fn new(x: f32, y: f32) -> Self {
44        Self { x, y }
45    }
46
47    pub fn magnitude(self) -> f32 {
48        (self.x * self.x + self.y * self.y).sqrt()
49    }
50
51    pub fn normalized(self) -> Self {
52        let m = self.magnitude();
53        if m > 0.0 {
54            Self::new(self.x / m, self.y / m)
55        } else {
56            Self::ZERO
57        }
58    }
59}
60
61impl std::ops::Add for Vector2 {
62    type Output = Self;
63    fn add(self, o: Self) -> Self {
64        Self::new(self.x + o.x, self.y + o.y)
65    }
66}
67
68impl std::ops::Sub for Vector2 {
69    type Output = Self;
70    fn sub(self, o: Self) -> Self {
71        Self::new(self.x - o.x, self.y - o.y)
72    }
73}
74
75impl std::ops::Mul<f32> for Vector2 {
76    type Output = Self;
77    fn mul(self, s: f32) -> Self {
78        Self::new(self.x * s, self.y * s)
79    }
80}
81
82impl Vector3 {
83    pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);
84    pub const ONE: Self = Self::new(1.0, 1.0, 1.0);
85    pub const UP: Self = Self::new(0.0, 1.0, 0.0);
86    pub const FORWARD: Self = Self::new(0.0, 0.0, 1.0);
87
88    pub const fn new(x: f32, y: f32, z: f32) -> Self {
89        Self { x, y, z }
90    }
91
92    pub fn dot(self, o: Self) -> f32 {
93        self.x * o.x + self.y * o.y + self.z * o.z
94    }
95
96    pub fn magnitude(self) -> f32 {
97        self.dot(self).sqrt()
98    }
99
100    pub fn normalized(self) -> Self {
101        let m = self.magnitude();
102        if m > 0.0 {
103            self * (1.0 / m)
104        } else {
105            Self::ZERO
106        }
107    }
108
109    pub fn distance(self, o: Self) -> f32 {
110        (self - o).magnitude()
111    }
112}
113
114impl std::ops::Add for Vector3 {
115    type Output = Self;
116    fn add(self, o: Self) -> Self {
117        Self::new(self.x + o.x, self.y + o.y, self.z + o.z)
118    }
119}
120
121impl std::ops::Sub for Vector3 {
122    type Output = Self;
123    fn sub(self, o: Self) -> Self {
124        Self::new(self.x - o.x, self.y - o.y, self.z - o.z)
125    }
126}
127
128impl std::ops::Mul<f32> for Vector3 {
129    type Output = Self;
130    fn mul(self, s: f32) -> Self {
131        Self::new(self.x * s, self.y * s, self.z * s)
132    }
133}
134
135impl Color {
136    pub const WHITE: Self = Self::rgb(1.0, 1.0, 1.0);
137    pub const BLACK: Self = Self::rgb(0.0, 0.0, 0.0);
138    pub const RED: Self = Self::rgb(1.0, 0.0, 0.0);
139    pub const GREEN: Self = Self::rgb(0.0, 1.0, 0.0);
140    pub const BLUE: Self = Self::rgb(0.0, 0.0, 1.0);
141    pub const YELLOW: Self = Self::rgb(1.0, 0.92, 0.016);
142    pub const CLEAR: Self = Self::rgba(0.0, 0.0, 0.0, 0.0);
143
144    pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
145        Self::rgba(r, g, b, 1.0)
146    }
147
148    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
149        Self { r, g, b, a }
150    }
151
152    /// The colour with its alpha replaced.
153    pub const fn with_alpha(self, a: f32) -> Self {
154        Self {
155            r: self.r,
156            g: self.g,
157            b: self.b,
158            a,
159        }
160    }
161}
162
163macro_rules! int_enum {
164    ($(#[$doc:meta])* $name:ident = $namespace:literal $class:literal { $($variant:ident = $value:literal),* }) => {
165        $(#[$doc])*
166        #[repr(transparent)]
167        #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
168        pub struct $name(pub i32);
169
170        impl $name {
171            $(pub const $variant: Self = Self($value);)*
172        }
173
174        unsafe impl Il2CppValue for $name {
175            fn matches(ty: TypeInfo) -> bool {
176                !ty.byref && ty.is_enum && ty.element == element::I4 && ty.namespace == $namespace && ty.name == $class
177            }
178        }
179    };
180}
181
182int_enum!(
183    /// `UnityEngine.SceneManagement.LoadSceneMode`.
184    LoadSceneMode = "UnityEngine.SceneManagement" "LoadSceneMode" { SINGLE = 0, ADDITIVE = 1 }
185);
186int_enum!(
187    /// `UnityEngine.RenderMode`, how a [`Canvas`](crate::Canvas) is drawn.
188    RenderMode = "UnityEngine" "RenderMode" { SCREEN_SPACE_OVERLAY = 0, SCREEN_SPACE_CAMERA = 1, WORLD_SPACE = 2 }
189);
190int_enum!(
191    /// `UnityEngine.TextureFormat`.
192    TextureFormat = "UnityEngine" "TextureFormat" { ALPHA8 = 1, RGBA32 = 4, ARGB32 = 5, RGB24 = 3 }
193);
194int_enum!(
195    /// `UnityEngine.FilterMode`, how a texture is sampled. `POINT` samples the
196    /// nearest texel exactly (no blending with neighbours).
197    FilterMode = "UnityEngine" "FilterMode" { POINT = 0, BILINEAR = 1, TRILINEAR = 2 }
198);
199int_enum!(
200    /// `UnityEngine.HideFlags`. `HIDE_AND_DONT_SAVE` keeps a runtime-created
201    /// object (e.g. a `Material`) from being destroyed on scene load.
202    HideFlags = "UnityEngine" "HideFlags" { NONE = 0, DONT_UNLOAD_UNUSED_ASSET = 32, HIDE_AND_DONT_SAVE = 61 }
203);
204int_enum!(
205    /// `UnityEngine.KeyCode`, for [`input`](crate::input). A few common keys are
206    /// named; any other can be made with `KeyCode(value)`.
207    KeyCode = "UnityEngine" "KeyCode" {
208        BACKSPACE = 8, TAB = 9, RETURN = 13, ESCAPE = 27, SPACE = 32, DELETE = 127,
209        LEFT = 276, RIGHT = 275, UP = 273, DOWN = 274,
210        INSERT = 277, HOME = 278, END = 279, PAGE_UP = 280, PAGE_DOWN = 281,
211        ALPHA0 = 48, ALPHA1 = 49, ALPHA2 = 50, ALPHA3 = 51,
212        A = 97, B = 98, C = 99, D = 100, E = 101, F = 102, G = 103, H = 104,
213        I = 105, J = 106, K = 107, L = 108, M = 109, N = 110, O = 111, P = 112,
214        Q = 113, R = 114, S = 115, T = 116, U = 117, V = 118, W = 119, X = 120,
215        Y = 121, Z = 122,
216        F1 = 282, F2 = 283, F3 = 284, F4 = 285, F5 = 286, F6 = 287,
217        RIGHT_SHIFT = 303, LEFT_SHIFT = 304, RIGHT_CONTROL = 305, LEFT_CONTROL = 306,
218        RIGHT_ALT = 307, LEFT_ALT = 308
219    }
220);