@@ -365,6 +365,7 @@ public static void lua_pushstdcallcfunction(IntPtr luaState, LuaCSFunction func)
365365 private static CSharpMethodHooker tolua_unref_hooker ;
366366#endif
367367 private static bool m_hooked = false ;
368+ private static object m_Lock = 1 ;
368369 public static void Install ( )
369370 {
370371#if TOLUA || XLUA || SLUA
@@ -501,53 +502,74 @@ private static CSharpMethodHooker BindHook(Type oldType, Type replaceType, strin
501502
502503 public static IntPtr luaL_newstate_replace ( )
503504 {
504- IntPtr intPtr = LuaDLL . luaL_newstate ( ) ;
505- LuaProfiler . mainL = intPtr ;
506- MikuLuaProfilerLuaProfilerWrap . __Register ( intPtr ) ;
507- return intPtr ;
505+ lock ( m_Lock )
506+ {
507+ IntPtr intPtr = LuaDLL . luaL_newstate ( ) ;
508+ LuaProfiler . mainL = intPtr ;
509+ MikuLuaProfilerLuaProfilerWrap . __Register ( intPtr ) ;
510+ return intPtr ;
511+ }
508512 }
509513
510514 public static void lua_close_replace ( IntPtr luaState )
511515 {
512- if ( LuaProfiler . mainL == luaState )
516+ lock ( m_Lock )
513517 {
514- LuaProfiler . mainL = IntPtr . Zero ;
518+ if ( LuaProfiler . mainL == luaState )
519+ {
520+ LuaProfiler . mainL = IntPtr . Zero ;
521+ }
522+ LuaDLL . lua_close ( luaState ) ;
515523 }
516- LuaDLL . lua_close ( luaState ) ;
517524 }
518525
519526 public static int luaL_loadbuffer_replace ( IntPtr luaState , byte [ ] buff , int size , string name )
520527 {
521528#if TOLUA || XLUA || SLUA
522- buff = LuaHook . Hookloadbuffer ( luaState , buff , name ) ;
523- return LuaDLL . luaL_loadbuffer ( luaState , buff , ( IntPtr ) ( buff . Length ) , name ) ;
529+ lock ( m_Lock )
530+ {
531+ buff = LuaHook . Hookloadbuffer ( luaState , buff , name ) ;
532+ return LuaDLL . luaL_loadbuffer ( luaState , buff , ( IntPtr ) ( buff . Length ) , name ) ;
533+ }
524534#else
525535 return 1 ;
526536#endif
527537 }
528538
529539 public static int luaL_ref_replace ( IntPtr luaState , int t )
530540 {
531- int num = LuaDLL . luaL_ref ( luaState , t ) ;
532- LuaHook . HookRef ( luaState , num ) ;
533- return num ;
541+ lock ( m_Lock )
542+ {
543+ int num = LuaDLL . luaL_ref ( luaState , t ) ;
544+ LuaHook . HookRef ( luaState , num ) ;
545+ return num ;
546+ }
534547 }
535548
536549 public static void luaL_unref_replace ( IntPtr luaState , int registryIndex , int reference )
537550 {
538- LuaHook . HookUnRef ( luaState , reference ) ;
539- LuaDLL . luaL_unref ( luaState , registryIndex , reference ) ;
551+ lock ( m_Lock )
552+ {
553+ LuaHook . HookUnRef ( luaState , reference ) ;
554+ LuaDLL . luaL_unref ( luaState , registryIndex , reference ) ;
555+ }
540556 }
541557
542558#if TOLUA
543559 public static int toluaL_ref_replace ( IntPtr L )
544560 {
545- return OldLuaDLL . luaL_ref ( L , - 10000 ) ;
561+ lock ( m_Lock )
562+ {
563+ return OldLuaDLL . luaL_ref ( L , - 10000 ) ;
564+ }
546565 }
547566
548567 public static void toluaL_unref_replace ( IntPtr L , int reference )
549568 {
550- OldLuaDLL . luaL_unref ( L , - 10000 , reference ) ;
569+ lock ( m_Lock )
570+ {
571+ OldLuaDLL . luaL_unref ( L , - 10000 , reference ) ;
572+ }
551573 }
552574#endif
553575
0 commit comments