Skip to main content

il2cpp_bridge_rs/structs/components/rendering/
shader.rs

1use crate::api::cache;
2use crate::structs::collections::Il2cppString;
3use crate::structs::core::Object;
4use std::ffi::c_void;
5use std::ops::Deref;
6
7#[repr(C)]
8#[derive(Debug, Clone, Copy)]
9pub struct Shader {
10    pub object: Object,
11}
12
13impl Shader {
14    pub fn from_ptr(ptr: *mut c_void) -> Self {
15        Self {
16            object: unsafe { Object::from_ptr(ptr) },
17        }
18    }
19}
20
21impl Deref for Shader {
22    type Target = Object;
23    fn deref(&self) -> &Self::Target {
24        &self.object
25    }
26}
27
28impl Shader {
29    /// Gets the Shader class definition
30    ///
31    /// # Returns
32    /// * `Option<Class>` - The UnityEngine.Shader class
33    pub fn get_class() -> Option<crate::structs::core::Class> {
34        cache::coremodule().class("UnityEngine.Shader")
35    }
36
37    /// Finds a shader with the given name.
38    ///
39    /// # Arguments
40    /// * `name` - The name of the shader to find
41    ///
42    /// # Returns
43    /// * `Option<Shader>` - The shader if found, None otherwise
44    pub fn find(name: &str) -> Option<Self> {
45        unsafe {
46            let string_new_ptr = Il2cppString::new(name);
47            Self::get_class()
48                .and_then(|cls| cls.method(("Find", 1)))
49                .and_then(|method| {
50                    method
51                        .call::<*mut c_void>(&[string_new_ptr as *mut c_void])
52                        .ok()
53                })
54                .filter(|ptr| !ptr.is_null())
55                .map(Self::from_ptr)
56        }
57    }
58
59    /// Gets the unique identifier for a shader property name.
60    ///
61    /// # Arguments
62    /// * `name` - The property name
63    ///
64    /// # Returns
65    /// * `i32` - The ID of the property
66    pub fn property_to_id(name: &str) -> i32 {
67        unsafe {
68            let string_new_ptr = Il2cppString::new(name);
69            Self::get_class()
70                .and_then(|cls| cls.method(("PropertyToID", 1)))
71                .and_then(|method| method.call::<i32>(&[string_new_ptr as *mut c_void]).ok())
72                .unwrap_or(0)
73        }
74    }
75}