@@ -3,7 +3,7 @@ use std::collections::HashMap;
33use std:: fmt:: Debug ;
44use std:: os:: raw:: c_ulonglong;
55use std:: rc:: Rc ;
6- use std:: sync:: OnceLock ;
6+ use std:: sync:: { Mutex , OnceLock } ;
77use unity_native_plugin_sys:: * ;
88
99#[ derive( Default , Copy , Clone , Eq , PartialEq , Hash ) ]
@@ -22,7 +22,7 @@ pub trait UnityInterfaceID {
2222}
2323
2424pub struct TesterContextInterfaces {
25- map : HashMap < InfKey , Rc < dyn UnityInterfaceBase > > ,
25+ map : Mutex < HashMap < InfKey , Rc < dyn UnityInterfaceBase > > > ,
2626 interfaces : IUnityInterfaces ,
2727}
2828
@@ -40,7 +40,7 @@ unsafe impl Sync for TesterContextInterfaces {}
4040impl TesterContextInterfaces {
4141 pub fn new ( ) -> Self {
4242 TesterContextInterfaces {
43- map : HashMap :: < InfKey , Rc < dyn UnityInterfaceBase > > :: new ( ) ,
43+ map : Mutex :: new ( HashMap :: < InfKey , Rc < dyn UnityInterfaceBase > > :: new ( ) ) ,
4444 interfaces : IUnityInterfaces {
4545 GetInterface : Some ( get_interface) ,
4646 RegisterInterface : Some ( register_interface) ,
@@ -54,16 +54,16 @@ impl TesterContextInterfaces {
5454 unsafe { std:: mem:: transmute :: < _ , _ > ( & self . interfaces ) }
5555 }
5656
57- pub fn get_interface ( & self , guid : UnityInterfaceGUID ) -> Option < & Rc < dyn UnityInterfaceBase > > {
57+ pub fn get_interface ( & self , guid : UnityInterfaceGUID ) -> Option < Rc < dyn UnityInterfaceBase > > {
5858 self . get_interface_split ( guid. m_GUIDHigh , guid. m_GUIDLow )
5959 }
6060
6161 pub fn get_interface_split (
6262 & self ,
6363 high : :: std:: os:: raw:: c_ulonglong ,
6464 low : :: std:: os:: raw:: c_ulonglong ,
65- ) -> Option < & Rc < dyn UnityInterfaceBase > > {
66- self . map . get ( & InfKey { high, low } )
65+ ) -> Option < Rc < dyn UnityInterfaceBase > > {
66+ self . map . lock ( ) . unwrap ( ) . get ( & InfKey { high, low } ) . cloned ( )
6767 }
6868
6969 pub fn register_interface < T : UnityInterfaceBase + UnityInterfaceID > (
@@ -81,9 +81,9 @@ impl TesterContextInterfaces {
8181 interface : Option < Rc < dyn UnityInterfaceBase > > ,
8282 ) {
8383 if let Some ( i) = interface {
84- self . map . insert ( InfKey { high, low } , i) ;
84+ self . map . lock ( ) . unwrap ( ) . insert ( InfKey { high, low } , i) ;
8585 } else {
86- self . map . remove ( & InfKey { high, low } ) ;
86+ self . map . lock ( ) . unwrap ( ) . remove ( & InfKey { high, low } ) ;
8787 }
8888 }
8989}
@@ -132,14 +132,21 @@ pub unsafe fn get_unity_interfaces() -> &'static TesterContextInterfaces {
132132 }
133133}
134134
135- pub unsafe fn get_unity_interface < T : UnityInterfaceBase + UnityInterfaceID > ( ) -> & ' static T {
135+ pub unsafe fn get_unity_interface < T : UnityInterfaceBase + UnityInterfaceID + ' static > ( ) -> Option < Rc < T > >
136+ {
136137 unsafe {
137- get_unity_interfaces ( )
138- . get_interface ( T :: get_interface_guid ( ) )
139- . unwrap ( )
140- . as_any ( )
141- . downcast_ref :: < T > ( )
142- . unwrap ( )
138+ let interface_rc = get_unity_interfaces ( )
139+ . get_interface ( T :: get_interface_guid ( ) ) ?;
140+
141+ // Rcの中身をダウンキャストして新しいRcを作成
142+ let any_ref = interface_rc. as_any ( ) ;
143+ if any_ref. is :: < T > ( ) {
144+ // unsafeなポインタ操作を使ってRc<T>を作成
145+ let ptr = Rc :: as_ptr ( & interface_rc) as * const T ;
146+ Some ( Rc :: from_raw ( ptr) )
147+ } else {
148+ None
149+ }
143150 }
144151}
145152
0 commit comments