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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Unity structs and enums, laid out as the game passes them.
use hao_il2cpp_runtime::{element, Il2CppValue, TypeInfo};
macro_rules! value_type {
($(#[$doc:meta])* $name:ident = $namespace:literal $class:literal { $($(#[$field_doc:meta])* $field:ident: $t:ty),* }) => {
$(#[$doc])*
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct $name {
$($(#[$field_doc])* pub $field: $t,)*
}
unsafe impl Il2CppValue for $name {
fn matches(ty: TypeInfo) -> bool {
ty.is_value_type($namespace, $class)
}
}
};
}
value_type!(
/// `UnityEngine.Vector2`.
Vector2 = "UnityEngine" "Vector2" {
/// Horizontal; increases to the right.
x: f32,
/// Vertical; increases upwards.
y: f32
}
);
value_type!(
/// `UnityEngine.Vector3`.
Vector3 = "UnityEngine" "Vector3" {
/// Right (positive) and left.
x: f32,
/// Up (positive) and down.
y: f32,
/// Forward (positive) and back; Unity is left-handed.
z: f32
}
);
value_type!(
/// `UnityEngine.Color`, with components from 0 to 1.
Color = "UnityEngine" "Color" {
/// Red.
r: f32,
/// Green.
g: f32,
/// Blue.
b: f32,
/// Opacity: 0 is transparent, 1 opaque.
a: f32
}
);
value_type!(
/// `UnityEngine.Quaternion`, a rotation. Use [`Quaternion::euler`] to build one.
Quaternion = "UnityEngine" "Quaternion" {
/// X of the vector part (the rotation axis scaled by `sin(angle / 2)`).
x: f32,
/// Y of the vector part.
y: f32,
/// Z of the vector part.
z: f32,
/// The scalar part, `cos(angle / 2)`; 1 with a zero vector part is no rotation.
w: f32
}
);
impl Vector2 {
/// `(0, 0)`.
pub const ZERO: Self = Self::new(0.0, 0.0);
/// `(1, 1)`.
pub const ONE: Self = Self::new(1.0, 1.0);
/// A vector from its components (usable in `const`s).
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
/// The vector's length.
pub fn magnitude(self) -> f32 {
(self.x * self.x + self.y * self.y).sqrt()
}
/// The same direction with length 1, or [`ZERO`](Self::ZERO) for a zero vector.
pub fn normalized(self) -> Self {
let m = self.magnitude();
if m > 0.0 {
Self::new(self.x / m, self.y / m)
} else {
Self::ZERO
}
}
}
impl std::ops::Add for Vector2 {
type Output = Self;
fn add(self, o: Self) -> Self {
Self::new(self.x + o.x, self.y + o.y)
}
}
impl std::ops::Sub for Vector2 {
type Output = Self;
fn sub(self, o: Self) -> Self {
Self::new(self.x - o.x, self.y - o.y)
}
}
impl std::ops::Mul<f32> for Vector2 {
type Output = Self;
fn mul(self, s: f32) -> Self {
Self::new(self.x * s, self.y * s)
}
}
impl Vector3 {
/// `(0, 0, 0)`.
pub const ZERO: Self = Self::new(0.0, 0.0, 0.0);
/// `(1, 1, 1)`, e.g. the default scale.
pub const ONE: Self = Self::new(1.0, 1.0, 1.0);
/// `(0, 1, 0)`, world up.
pub const UP: Self = Self::new(0.0, 1.0, 0.0);
/// `(0, 0, 1)`, world forward.
pub const FORWARD: Self = Self::new(0.0, 0.0, 1.0);
/// A vector from its components (usable in `const`s).
pub const fn new(x: f32, y: f32, z: f32) -> Self {
Self { x, y, z }
}
/// The dot product: the cosine of the angle between two unit vectors.
pub fn dot(self, o: Self) -> f32 {
self.x * o.x + self.y * o.y + self.z * o.z
}
/// The vector's length.
pub fn magnitude(self) -> f32 {
self.dot(self).sqrt()
}
/// The same direction with length 1, or [`ZERO`](Self::ZERO) for a zero vector.
pub fn normalized(self) -> Self {
let m = self.magnitude();
if m > 0.0 {
self * (1.0 / m)
} else {
Self::ZERO
}
}
/// The distance between two points.
pub fn distance(self, o: Self) -> f32 {
(self - o).magnitude()
}
}
impl std::ops::Add for Vector3 {
type Output = Self;
fn add(self, o: Self) -> Self {
Self::new(self.x + o.x, self.y + o.y, self.z + o.z)
}
}
impl std::ops::Sub for Vector3 {
type Output = Self;
fn sub(self, o: Self) -> Self {
Self::new(self.x - o.x, self.y - o.y, self.z - o.z)
}
}
impl std::ops::Mul<f32> for Vector3 {
type Output = Self;
fn mul(self, s: f32) -> Self {
Self::new(self.x * s, self.y * s, self.z * s)
}
}
impl Color {
/// Opaque white.
pub const WHITE: Self = Self::rgb(1.0, 1.0, 1.0);
/// Opaque black.
pub const BLACK: Self = Self::rgb(0.0, 0.0, 0.0);
/// Opaque red.
pub const RED: Self = Self::rgb(1.0, 0.0, 0.0);
/// Opaque green.
pub const GREEN: Self = Self::rgb(0.0, 1.0, 0.0);
/// Opaque blue.
pub const BLUE: Self = Self::rgb(0.0, 0.0, 1.0);
/// Unity's `Color.yellow`, which is slightly orange: `(1, 0.92, 0.016)`.
pub const YELLOW: Self = Self::rgb(1.0, 0.92, 0.016);
/// Fully transparent, e.g. to clear an overlay.
pub const CLEAR: Self = Self::rgba(0.0, 0.0, 0.0, 0.0);
/// An opaque colour, each component from 0 to 1.
pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
Self::rgba(r, g, b, 1.0)
}
/// A colour with opacity `a` (0 transparent, 1 opaque).
pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
/// The colour with its alpha replaced.
pub const fn with_alpha(self, a: f32) -> Self {
Self {
r: self.r,
g: self.g,
b: self.b,
a,
}
}
}
macro_rules! int_enum {
($(#[$doc:meta])* $name:ident = $namespace:literal $class:literal { $($(#[$variant_doc:meta])* $variant:ident = $value:literal),* }) => {
$(#[$doc])*
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct $name(
#[doc = concat!("The raw `", $class, "` value.")]
pub i32,
);
impl $name {
$(
$(#[$variant_doc])*
#[doc = ""]
#[doc = concat!("`", $class, "` value `", stringify!($value), "`.")]
pub const $variant: Self = Self($value);
)*
}
unsafe impl Il2CppValue for $name {
fn matches(ty: TypeInfo) -> bool {
!ty.byref && ty.is_enum && ty.element == element::I4 && ty.namespace == $namespace && ty.name == $class
}
}
};
}
int_enum!(
/// `UnityEngine.SceneManagement.LoadSceneMode`.
LoadSceneMode = "UnityEngine.SceneManagement" "LoadSceneMode" {
/// The scene replaced every loaded scene.
SINGLE = 0,
/// The scene was loaded alongside the ones already loaded.
ADDITIVE = 1
}
);
int_enum!(
/// `UnityEngine.RenderMode`, how a [`Canvas`](crate::Canvas) is drawn.
RenderMode = "UnityEngine" "RenderMode" {
/// Drawn over the whole screen, on top of every camera.
SCREEN_SPACE_OVERLAY = 0,
/// Drawn over the screen by a chosen camera, at a distance in front of it.
SCREEN_SPACE_CAMERA = 1,
/// A flat object placed in the scene like any other.
WORLD_SPACE = 2
}
);
int_enum!(
/// `UnityEngine.TextureFormat`.
TextureFormat = "UnityEngine" "TextureFormat" {
/// One byte per pixel: alpha only.
ALPHA8 = 1,
/// Four bytes per pixel in R, G, B, A order; the usual choice for raw pixels.
RGBA32 = 4,
/// Four bytes per pixel in A, R, G, B order.
ARGB32 = 5,
/// Three bytes per pixel, no alpha.
RGB24 = 3
}
);
int_enum!(
/// `UnityEngine.FilterMode`, how a texture is sampled. `POINT` samples the
/// nearest texel exactly (no blending with neighbours).
FilterMode = "UnityEngine" "FilterMode" {
/// Nearest texel, for crisp pixel art and glyph atlases.
POINT = 0,
/// Blends the four nearest texels.
BILINEAR = 1,
/// Bilinear, also blended between mipmap levels.
TRILINEAR = 2
}
);
int_enum!(
/// `UnityEngine.HideFlags`. `HIDE_AND_DONT_SAVE` keeps a runtime-created
/// object (e.g. a `Material`) from being destroyed on scene load.
HideFlags = "UnityEngine" "HideFlags" {
/// Normal: visible, saved, and unloaded with its scene.
NONE = 0,
/// Not unloaded by `Resources.UnloadUnusedAssets`.
DONT_UNLOAD_UNUSED_ASSET = 32,
/// Hidden, never saved, and not unloaded on scene load; the owner must destroy it.
HIDE_AND_DONT_SAVE = 61
}
);
int_enum!(
/// `UnityEngine.KeyCode`, for [`input`](crate::input). A few common keys are
/// named; any other can be made with `KeyCode(value)`.
KeyCode = "UnityEngine" "KeyCode" {
BACKSPACE = 8, TAB = 9, RETURN = 13, ESCAPE = 27, SPACE = 32, DELETE = 127,
LEFT = 276, RIGHT = 275, UP = 273, DOWN = 274,
INSERT = 277, HOME = 278, END = 279, PAGE_UP = 280, PAGE_DOWN = 281,
ALPHA0 = 48, ALPHA1 = 49, ALPHA2 = 50, ALPHA3 = 51,
A = 97, B = 98, C = 99, D = 100, E = 101, F = 102, G = 103, H = 104,
I = 105, J = 106, K = 107, L = 108, M = 109, N = 110, O = 111, P = 112,
Q = 113, R = 114, S = 115, T = 116, U = 117, V = 118, W = 119, X = 120,
Y = 121, Z = 122,
F1 = 282, F2 = 283, F3 = 284, F4 = 285, F5 = 286, F6 = 287,
RIGHT_SHIFT = 303, LEFT_SHIFT = 304, RIGHT_CONTROL = 305, LEFT_CONTROL = 306,
RIGHT_ALT = 307, LEFT_ALT = 308
}
);