1use hao_il2cpp_runtime::{Il2CppValue, Object, Result, TypeInfo};
4
5use crate::{
6 bindings::*,
7 il2cpp,
8 values::{Color, Quaternion, RenderMode, Vector2, Vector3},
9};
10
11macro_rules! object_type {
12 ($(#[$doc:meta])* $name:ident = $assembly:literal $namespace:literal $class:literal) => {
13 $(#[$doc])*
14 #[repr(transparent)]
15 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16 pub struct $name(pub Object);
17
18 unsafe impl Il2CppValue for $name {
19 fn is_reference() -> bool {
20 true
21 }
22
23 fn matches(ty: TypeInfo) -> bool {
24 <Object as Il2CppValue>::matches(ty) && ty.namespace == $namespace && ty.name == $class
25 }
26 }
27
28 impl $name {
29 pub const ASSEMBLY: &'static str = $assembly;
31 pub const NAMESPACE: &'static str = $namespace;
33 pub const NAME: &'static str = $class;
35
36 pub fn object(&self) -> Object {
38 self.0
39 }
40
41 pub fn is_null(&self) -> bool {
44 self.0.is_null()
45 }
46
47 pub fn name(&self) -> Result<String> {
49 let name = OBJECT_GET_NAME.call((UnityObject(self.0),))?;
50 Ok(name.to_rust().unwrap_or_default())
51 }
52
53 pub fn set_name(&self, name: &str) -> Result<()> {
55 OBJECT_SET_NAME.call((UnityObject(self.0), il2cpp()?.string(name)?))
56 }
57
58 pub fn dont_destroy_on_load(&self) -> Result<()> {
61 OBJECT_DONT_DESTROY_ON_LOAD.call((UnityObject(self.0),))
62 }
63
64 pub fn set_hide_flags(&self, flags: crate::HideFlags) -> Result<()> {
67 OBJECT_SET_HIDE_FLAGS.call((UnityObject(self.0), flags))
68 }
69
70 pub fn is_alive(&self) -> Result<bool> {
73 OBJECT_IS_ALIVE.call((UnityObject(self.0),))
74 }
75
76 pub fn destroy(&self) -> Result<()> {
78 OBJECT_DESTROY.call((UnityObject(self.0),))
79 }
80 }
81 };
82}
83
84pub trait ComponentType: Il2CppValue {
89 const ASSEMBLY: &'static str;
91 const NAMESPACE: &'static str;
93 const NAME: &'static str;
95
96 fn from_object(object: Object) -> Self;
98 fn object(&self) -> Object;
100
101 fn game_object(&self) -> Result<GameObject> {
103 COMPONENT_GAME_OBJECT.call((Component(self.object()),))
104 }
105
106 fn transform(&self) -> Result<Transform> {
108 COMPONENT_TRANSFORM.call((Component(self.object()),))
109 }
110
111 fn get_component<U: ComponentType>(&self) -> Result<Option<U>> {
113 let found =
114 COMPONENT_GET_COMPONENT.call((Component(self.object()), component_type::<U>()?))?;
115 Ok((!found.is_null()).then(|| U::from_object(found.0)))
116 }
117
118 fn is_enabled(&self) -> Result<bool> {
121 BEHAVIOUR_GET_ENABLED.call((Component(self.object()),))
122 }
123
124 fn set_enabled(&self, enabled: bool) -> Result<()> {
127 BEHAVIOUR_SET_ENABLED.call((Component(self.object()), enabled))
128 }
129}
130
131fn component_type<T: ComponentType>() -> Result<Object> {
133 Ok(il2cpp()?
134 .class(T::ASSEMBLY, T::NAMESPACE, T::NAME)?
135 .type_object())
136}
137
138fn read_object_array<T: ComponentType>(array: Object) -> Vec<T> {
140 if array.is_null() {
141 return Vec::new();
142 }
143 let Some(il2cpp) = il2cpp().ok() else {
144 return Vec::new();
145 };
146 let length = unsafe { (il2cpp.api().il2cpp_array_length)(array.as_ptr()) };
147 let base = 4 * std::mem::size_of::<usize>();
149 (0..length)
150 .map(|i| {
151 T::from_object(unsafe {
152 hao_il2cpp_runtime::read_field(array, base + i * std::mem::size_of::<usize>())
153 })
154 })
155 .collect()
156}
157
158pub fn find_objects_of_type<T: ComponentType>() -> Result<Vec<T>> {
163 let array = OBJECT_FIND_OBJECTS_OF_TYPE.call((component_type::<T>()?,))?;
164 Ok(read_object_array(array))
165}
166
167pub fn find_object_of_type<T: ComponentType>() -> Result<Option<T>> {
169 Ok(find_objects_of_type::<T>()?.into_iter().next())
170}
171
172macro_rules! component_type {
173 ($(#[$doc:meta])* $name:ident = $assembly:literal $namespace:literal $class:literal) => {
174 object_type!($(#[$doc])* $name = $assembly $namespace $class);
175
176 impl ComponentType for $name {
177 const ASSEMBLY: &'static str = $assembly;
178 const NAMESPACE: &'static str = $namespace;
179 const NAME: &'static str = $class;
180
181 fn from_object(object: Object) -> Self {
182 Self(object)
183 }
184
185 fn object(&self) -> Object {
186 self.0
187 }
188 }
189 };
190}
191
192object_type!(
193 UnityObject = "UnityEngine.CoreModule" "UnityEngine" "Object"
195);
196object_type!(
197 GameObject = "UnityEngine.CoreModule" "UnityEngine" "GameObject"
213);
214component_type!(
215 Component = "UnityEngine.CoreModule" "UnityEngine" "Component"
217);
218component_type!(
219 Transform = "UnityEngine.CoreModule" "UnityEngine" "Transform"
221);
222component_type!(
223 RectTransform = "UnityEngine.CoreModule" "UnityEngine" "RectTransform"
225);
226component_type!(
227 Canvas = "UnityEngine.UIModule" "UnityEngine" "Canvas"
229);
230component_type!(
231 TextMeshPro = "Unity.TextMeshPro" "TMPro" "TextMeshProUGUI"
233);
234component_type!(
235 Camera = "UnityEngine.CoreModule" "UnityEngine" "Camera"
237);
238object_type!(
239 Shader = "UnityEngine.CoreModule" "UnityEngine" "Shader"
241);
242object_type!(
243 Material = "UnityEngine.CoreModule" "UnityEngine" "Material"
245);
246
247impl Shader {
248 pub fn find(name: &str) -> Result<Option<Shader>> {
250 let shader = SHADER_FIND.call((il2cpp()?.string(name)?,))?;
251 Ok((!shader.is_null()).then_some(shader))
252 }
253}
254
255impl Material {
256 pub fn new(shader: Shader) -> Result<Material> {
258 let object = Material(
259 il2cpp()?
260 .class(Self::ASSEMBLY, Self::NAMESPACE, Self::NAME)?
261 .new_object(),
262 );
263 MATERIAL_CTOR.call((object, shader))?;
264 Ok(object)
265 }
266
267 pub fn set_pass(&self, pass: i32) -> Result<bool> {
270 MATERIAL_SET_PASS.call((*self, pass))
271 }
272
273 pub fn set_color(&self, color: Color) -> Result<()> {
275 MATERIAL_SET_COLOR.call((*self, color))
276 }
277
278 pub fn set_main_texture(&self, texture: Texture2D) -> Result<()> {
280 MATERIAL_SET_MAIN_TEXTURE.call((*self, texture.0))
281 }
282}
283
284object_type!(
285 Texture2D = "UnityEngine.CoreModule" "UnityEngine" "Texture2D"
287);
288
289impl Texture2D {
290 pub fn new(width: i32, height: i32, format: crate::TextureFormat) -> Result<Texture2D> {
292 let object = Texture2D(
293 il2cpp()?
294 .class(Self::ASSEMBLY, Self::NAMESPACE, Self::NAME)?
295 .new_object(),
296 );
297 TEXTURE2D_CTOR.call((object, width, height, format, false))?;
298 Ok(object)
299 }
300
301 pub fn load_raw(&self, bytes: &[u8]) -> Result<()> {
303 TEXTURE2D_LOAD_RAW.call((*self, il2cpp()?.byte_array(bytes)?))?;
304 TEXTURE2D_APPLY.call((*self,))
305 }
306
307 pub fn set_filter_mode(&self, mode: crate::FilterMode) -> Result<()> {
309 TEXTURE_SET_FILTER_MODE.call((*self, mode))
310 }
311}
312
313object_type!(
314 RenderTexture = "UnityEngine.CoreModule" "UnityEngine" "RenderTexture"
316);
317
318impl RenderTexture {
319 pub fn new(width: i32, height: i32, depth: i32) -> Result<RenderTexture> {
322 let object = RenderTexture(
323 il2cpp()?
324 .class(Self::ASSEMBLY, Self::NAMESPACE, Self::NAME)?
325 .new_object(),
326 );
327 RENDER_TEXTURE_CTOR.call((object, width, height, depth))?;
328 Ok(object)
329 }
330
331 pub fn active() -> Result<Option<RenderTexture>> {
333 let rt = RENDER_TEXTURE_GET_ACTIVE.call(())?;
334 Ok((!rt.is_null()).then_some(rt))
335 }
336
337 pub fn set_active(target: Option<RenderTexture>) -> Result<()> {
339 RENDER_TEXTURE_SET_ACTIVE.call((target.unwrap_or(RenderTexture(Object::NULL)),))
340 }
341
342 pub fn set_filter_mode(&self, mode: crate::FilterMode) -> Result<()> {
344 TEXTURE_SET_FILTER_MODE.call((Texture2D(self.0), mode))
345 }
346}
347
348component_type!(
349 RawImage = "UnityEngine.UI" "UnityEngine.UI" "RawImage"
352);
353
354impl RawImage {
355 pub fn set_texture(&self, texture: Object) -> Result<()> {
357 RAW_IMAGE_SET_TEXTURE.call((*self, texture))
358 }
359}
360
361impl GameObject {
362 pub fn new(name: &str) -> Result<Self> {
364 let il2cpp = il2cpp()?;
365 let object = GameObject(
366 il2cpp
367 .class(Self::ASSEMBLY, Self::NAMESPACE, Self::NAME)?
368 .new_object(),
369 );
370 GAME_OBJECT_CTOR.call((object, il2cpp.string(name)?))?;
371 Ok(object)
372 }
373
374 pub fn find(name: &str) -> Result<Option<Self>> {
376 let found = GAME_OBJECT_FIND.call((il2cpp()?.string(name)?,))?;
377 Ok((!found.is_null()).then_some(found))
378 }
379
380 pub fn add_component<T: ComponentType>(&self) -> Result<T> {
382 let class = il2cpp()?.class(T::ASSEMBLY, T::NAMESPACE, T::NAME)?;
383 let component = GAME_OBJECT_ADD_COMPONENT.call((*self, class.type_object()))?;
384 Ok(T::from_object(component.0))
385 }
386
387 pub fn get_component<T: ComponentType>(&self) -> Result<Option<T>> {
389 let found = GAME_OBJECT_GET_COMPONENT.call((*self, component_type::<T>()?))?;
390 Ok((!found.is_null()).then(|| T::from_object(found.0)))
391 }
392
393 pub fn transform(&self) -> Result<Transform> {
395 GAME_OBJECT_TRANSFORM.call((*self,))
396 }
397
398 pub fn rect_transform(&self) -> Result<Option<RectTransform>> {
400 let transform = self.transform()?;
401 let class = unsafe { il2cpp()?.class_of(transform.0) };
402 Ok((class.name() == RectTransform::NAME).then_some(RectTransform(transform.0)))
403 }
404
405 pub fn set_active(&self, active: bool) -> Result<()> {
408 GAME_OBJECT_SET_ACTIVE.call((*self, active))
409 }
410
411 pub fn is_active(&self) -> Result<bool> {
413 GAME_OBJECT_ACTIVE_SELF.call((*self,))
414 }
415
416 pub fn is_active_in_hierarchy(&self) -> Result<bool> {
418 GAME_OBJECT_ACTIVE_IN_HIERARCHY.call((*self,))
419 }
420
421 pub fn tag(&self) -> Result<String> {
423 Ok(GAME_OBJECT_GET_TAG
424 .call((*self,))?
425 .to_rust()
426 .unwrap_or_default())
427 }
428
429 pub fn set_tag(&self, tag: &str) -> Result<()> {
431 GAME_OBJECT_SET_TAG.call((*self, il2cpp()?.string(tag)?))
432 }
433
434 pub fn compare_tag(&self, tag: &str) -> Result<bool> {
436 GAME_OBJECT_COMPARE_TAG.call((*self, il2cpp()?.string(tag)?))
437 }
438
439 pub fn layer(&self) -> Result<i32> {
441 GAME_OBJECT_GET_LAYER.call((*self,))
442 }
443
444 pub fn set_layer(&self, layer: i32) -> Result<()> {
446 GAME_OBJECT_SET_LAYER.call((*self, layer))
447 }
448}
449
450impl Transform {
451 pub fn set_parent(&self, parent: Transform, world_position_stays: bool) -> Result<()> {
455 TRANSFORM_SET_PARENT.call((*self, parent, world_position_stays))
456 }
457
458 pub fn set_local_scale(&self, scale: Vector3) -> Result<()> {
460 TRANSFORM_SET_LOCAL_SCALE.call((*self, scale))
461 }
462
463 pub fn position(&self) -> Result<Vector3> {
465 TRANSFORM_GET_POSITION.call((*self,))
466 }
467
468 pub fn set_position(&self, position: Vector3) -> Result<()> {
470 TRANSFORM_SET_POSITION.call((*self, position))
471 }
472
473 pub fn local_position(&self) -> Result<Vector3> {
475 TRANSFORM_GET_LOCAL_POSITION.call((*self,))
476 }
477
478 pub fn set_local_position(&self, position: Vector3) -> Result<()> {
480 TRANSFORM_SET_LOCAL_POSITION.call((*self, position))
481 }
482
483 pub fn euler_angles(&self) -> Result<Vector3> {
485 TRANSFORM_GET_EULER_ANGLES.call((*self,))
486 }
487
488 pub fn rotation(&self) -> Result<Quaternion> {
490 TRANSFORM_GET_ROTATION.call((*self,))
491 }
492
493 pub fn set_rotation(&self, rotation: Quaternion) -> Result<()> {
495 TRANSFORM_SET_ROTATION.call((*self, rotation))
496 }
497
498 pub fn parent(&self) -> Result<Option<Transform>> {
500 let parent = TRANSFORM_GET_PARENT.call((*self,))?;
501 Ok((!parent.is_null()).then_some(parent))
502 }
503
504 pub fn child_count(&self) -> Result<i32> {
506 TRANSFORM_GET_CHILD_COUNT.call((*self,))
507 }
508
509 pub fn child(&self, index: i32) -> Result<Transform> {
511 TRANSFORM_GET_CHILD.call((*self, index))
512 }
513
514 pub fn find(&self, path: &str) -> Result<Option<Transform>> {
516 let found = TRANSFORM_FIND.call((*self, il2cpp()?.string(path)?))?;
517 Ok((!found.is_null()).then_some(found))
518 }
519}
520
521impl RectTransform {
522 pub fn as_transform(&self) -> Transform {
525 Transform(self.0)
526 }
527
528 pub fn set_anchors(&self, min: Vector2, max: Vector2, pivot: Vector2) -> Result<()> {
530 RECT_SET_ANCHOR_MIN.call((*self, min))?;
531 RECT_SET_ANCHOR_MAX.call((*self, max))?;
532 RECT_SET_PIVOT.call((*self, pivot))
533 }
534
535 pub fn set_anchored_position(&self, position: Vector2) -> Result<()> {
537 RECT_SET_ANCHORED_POSITION.call((*self, position))
538 }
539
540 pub fn set_size(&self, size: Vector2) -> Result<()> {
543 RECT_SET_SIZE_DELTA.call((*self, size))
544 }
545}
546
547impl Canvas {
548 pub fn set_render_mode(&self, mode: RenderMode) -> Result<()> {
550 CANVAS_SET_RENDER_MODE.call((*self, mode))
551 }
552
553 pub fn set_sorting_order(&self, order: i32) -> Result<()> {
555 CANVAS_SET_SORTING_ORDER.call((*self, order))
556 }
557}
558
559impl TextMeshPro {
560 pub fn set_text(&self, text: &str) -> Result<()> {
562 TMP_SET_TEXT.call((*self, il2cpp()?.string(text)?))
563 }
564
565 pub fn set_font_size(&self, size: f32) -> Result<()> {
567 TMP_SET_FONT_SIZE.call((*self, size))
568 }
569
570 pub fn set_color(&self, color: Color) -> Result<()> {
572 TMP_SET_COLOR.call((*self, color))
573 }
574}