unity_native_plugin/
lib.rs

1#[cfg(feature = "d3d11")]
2pub mod d3d11;
3
4#[cfg(feature = "d3d12")]
5pub mod d3d12;
6
7#[cfg(feature = "profiler")]
8pub mod profiler;
9
10#[cfg(feature = "profiler_callbacks")]
11pub mod profiler_callbacks;
12
13pub mod enums;
14pub mod graphics;
15pub mod interface;
16pub mod log;
17pub mod memory_manager;
18mod bitflag;
19
20pub type IUnityInterfaces = unity_native_plugin_sys::IUnityInterfaces;
21
22#[macro_export]
23macro_rules! unity_native_plugin_entry_point {
24    {fn $method_load:ident($p:ident : $t:ty) $body_load:block
25     fn $method_unload:ident() $body_unload:block} => {
26        #[allow(unused_variables)]
27        fn $method_load($p: $t) $body_load
28        fn $method_unload() $body_unload
29
30        #[no_mangle]
31        #[allow(non_snake_case)]
32        extern "system" fn UnityPluginLoad(
33            interfaces: *mut unity_native_plugin::IUnityInterfaces,
34        ) {
35            unity_native_plugin::interface::UnityInterfaces::set_native_unity_interfaces(interfaces);
36            $method_load(unity_native_plugin::interface::UnityInterfaces::get());
37        }
38
39        #[no_mangle]
40        #[allow(non_snake_case)]
41        extern "system" fn UnityPluginUnload() {
42            $method_unload();
43            unity_native_plugin::interface::UnityInterfaces::set_native_unity_interfaces(std::ptr::null_mut());
44        }
45    }
46}
47
48#[macro_export]
49macro_rules! define_unity_interface {
50    ($s:ident, $intf:ty, $guid_high:expr, $guid_low:expr) => {
51        #[derive(Clone, Copy)]
52        pub struct $s {
53            interface: *const $intf,
54        }
55
56        // unity plugin interface should be thread-safe
57        unsafe impl Send for $s {}
58        unsafe impl Sync for $s {}
59
60        impl UnityInterface for $s {
61            fn get_interface_guid() -> unity_native_plugin_sys::UnityInterfaceGUID {
62                unity_native_plugin_sys::UnityInterfaceGUID::new($guid_high, $guid_low)
63            }
64
65            fn new(interface: *const unity_native_plugin_sys::IUnityInterface) -> Self {
66                $s {
67                    interface: interface as *const $intf,
68                }
69            }
70        }
71
72        impl $s {
73            #[allow(dead_code)]
74            #[inline]
75            fn interface(&self) -> &$intf {
76                unsafe { &*self.interface }
77            }
78        }
79    };
80}