Skip to main content

unity_native_plugin/
lib.rs

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