Skip to main content

il2cpp_bridge_rs/structs/components/physics/
layer_mask.rs

1//! Unity LayerMask wrapper
2use crate::api::cache;
3use std::ffi::c_void;
4
5#[repr(transparent)]
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub struct LayerMask {
8    pub value: i32,
9}
10
11impl From<i32> for LayerMask {
12    fn from(value: i32) -> Self {
13        Self { value }
14    }
15}
16
17impl From<LayerMask> for i32 {
18    fn from(mask: LayerMask) -> Self {
19        mask.value
20    }
21}
22
23impl LayerMask {
24    /// Gets the LayerMask class definition
25    ///
26    /// # Returns
27    /// * `Option<Class>` - The UnityEngine.LayerMask class
28    pub fn get_class() -> Option<crate::structs::core::Class> {
29        cache::coremodule().class("LayerMask")
30    }
31
32    /// Converts a layer name to a layer index
33    ///
34    /// # Arguments
35    /// * `name` - The name of the layer
36    ///
37    /// # Returns
38    /// * `i32` - The layer index, or -1 if not found
39    pub fn name_to_layer(name: &str) -> i32 {
40        if let Some(class) = Self::get_class() {
41            if let Some(method) = class.method("NameToLayer") {
42                let name_str = crate::structs::Il2cppString::new(name);
43                let res = unsafe { method.call::<i32>(&[name_str as *mut c_void]) };
44                return res.unwrap_or(-1);
45            }
46        }
47        -1
48    }
49
50    /// Converts a layer index to a layer name
51    ///
52    /// # Arguments
53    /// * `layer` - The layer index
54    ///
55    /// # Returns
56    /// * `String` - The name of the layer
57    pub fn layer_to_name(layer: i32) -> String {
58        if let Some(class) = Self::get_class() {
59            if let Some(method) = class.method("LayerToName") {
60                let mut layer_val = layer;
61                unsafe {
62                    let ptr = method.call::<*mut crate::structs::Il2cppString>(&[&mut layer_val
63                        as *mut i32
64                        as *mut c_void]);
65                    if let Ok(ptr) = ptr {
66                        if !ptr.is_null() {
67                            return (*ptr).to_string().unwrap_or_default();
68                        }
69                    }
70                }
71            }
72        }
73        String::new()
74    }
75
76    /// Gets a mask for the specified layer names
77    ///
78    /// # Arguments
79    /// * `layer_names` - A list of layer names to include in the mask
80    ///
81    /// # Returns
82    /// * `LayerMask` - The resulting bitmask
83    pub fn get_mask(layer_names: &[&str]) -> LayerMask {
84        if let Some(class) = Self::get_class() {
85            if let Some(method) = class.method("GetMask") {
86                let array_len = layer_names.len() as u32;
87                let string_class = crate::api::cache::mscorlib()
88                    .class("System.String")
89                    .unwrap();
90                let array = crate::structs::collections::Il2cppArray::<
91                    *mut crate::structs::Il2cppString,
92                >::new(&string_class, array_len as usize);
93
94                if !array.is_null() {
95                    let array_obj = unsafe { &mut *array };
96                    for (i, name) in layer_names.iter().enumerate() {
97                        let il2cpp_string = crate::structs::Il2cppString::new(name);
98                        array_obj.set(i, il2cpp_string);
99                    }
100
101                    let res = unsafe { method.call::<i32>(&[array as *mut c_void]) };
102                    if let Ok(val) = res {
103                        return LayerMask { value: val };
104                    }
105                }
106            }
107        }
108        LayerMask { value: 0 }
109    }
110}