Skip to content

Commit da89de4

Browse files
author
ElPsyCongree
committed
错误处理
1 parent 5f1f740 commit da89de4

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

LuaProfiler/LuaProfilerClient/Core/LuaHookSetup.cs

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,25 @@ private void OnApplicationQuit()
129129
int desotryCount = 0;
130130
private void WaitDestory()
131131
{
132+
#if XLUA || TOLUA || SLUA
132133
desotryCount++;
133134
if (desotryCount > 10)
134135
{
135136
UnityEditor.EditorApplication.update -= WaitDestory;
137+
if (LuaProfiler.mainL != IntPtr.Zero)
138+
{
139+
LuaDLL.lua_close(LuaProfiler.mainL);
140+
}
141+
LuaProfiler.mainL = IntPtr.Zero;
136142
NetWorkClient.Close();
137143
desotryCount = 0;
138144
}
145+
#endif
139146
}
140147
#endif
141-
}
148+
}
142149

143-
public class Menu : MonoBehaviour
150+
public class Menu : MonoBehaviour
144151
{
145152
private static Menu m_menu;
146153
public static void EnableMenu(GameObject go)
@@ -376,61 +383,36 @@ public static void DoString(IntPtr L, string script)
376383
LuaHook.isHook = false;
377384
byte[] chunk = Encoding.UTF8.GetBytes(script);
378385
int oldTop = LuaDLL.lua_gettop(L);
379-
#if XLUA
380-
int errFunc = LuaDLL.load_error_func(L, -1);
381-
386+
lua_getglobal(L, "miku_handle_error");
382387
if (LuaLib.luaL_loadbuffer(L, chunk, chunk.Length, "chunk") == 0)
383388
{
384-
if (LuaDLL.lua_pcall(L, 0, -1, errFunc) == 0)
389+
if (LuaDLL.lua_pcall(L, 0, -1, oldTop + 1) == 0)
385390
{
386-
LuaDLL.lua_remove(L, errFunc);
391+
LuaDLL.lua_remove(L, oldTop + 1);
387392
}
388393
}
389394
else
390395
{
391396
Debug.Log(script);
392397
}
393-
#elif TOLUA
394-
if (LuaLib.luaL_loadbuffer(L, chunk, chunk.Length, "chunk") == 0)
395-
{
396-
LuaDLL.lua_call(L, 0, -1);
397-
}
398-
#elif SLUA
399-
if (LuaLib.luaL_loadbuffer(L, chunk, chunk.Length, "chunk") == 0)
400-
{
401-
LuaDLL.lua_call(L, 0, -1);
402-
}
403-
#endif
404398
LuaHook.isHook = true;
405399
LuaDLL.lua_settop(L, oldTop);
406400
}
407401

408402
public static void DoRefLuaFun(IntPtr L, string funName)
409403
{
410404
int oldTop = LuaDLL.lua_gettop(L);
411-
405+
lua_getglobal(L, "miku_handle_error");
412406
do
413407
{
414-
#if XLUA
415-
int errFunc = LuaDLL.load_error_func(L, -1);
416408
LuaLib.lua_getglobal(L, funName);
417409
if (!LuaDLL.lua_isfunction(L, -1)) break;
418-
LuaDLL.lua_pushvalue(L, -3);
419-
if (LuaDLL.lua_pcall(L, 1, 0, errFunc) == 0)
410+
LuaDLL.lua_pushvalue(L, oldTop);
411+
if (LuaDLL.lua_pcall(L, 1, 0, oldTop + 1) == 0)
420412
{
421-
LuaDLL.lua_remove(L, errFunc);
413+
LuaDLL.lua_remove(L, oldTop + 1);
422414
}
423-
#elif TOLUA
424-
LuaLib.lua_getglobal(L, funName);
425-
if (!LuaDLL.lua_isfunction(L, -1)) break;
426-
LuaDLL.lua_pushvalue(L, -2);
427-
LuaDLL.lua_call(L, 1, 0);
428-
#elif SLUA
429-
LuaLib.lua_getglobal(L, funName);
430-
if (!LuaDLL.lua_isfunction(L, -1)) break;
431-
LuaDLL.lua_pushvalue(L, -2);
432-
LuaDLL.lua_call(L, 1, 0);
433-
#endif
415+
434416
} while (false);
435417

436418
LuaDLL.lua_settop(L, oldTop);
@@ -470,6 +452,9 @@ public static void __Register(IntPtr L)
470452
LuaLib.lua_pushstdcallcfunction(L, RemoveRefFunInfo);
471453
LuaLib.lua_setglobal(L, "miku_remove_ref_fun_info");
472454

455+
LuaLib.lua_pushstdcallcfunction(L, HandleError);
456+
LuaLib.lua_setglobal(L, "miku_handle_error");
457+
473458
LuaDLL.lua_newtable(L);
474459
LuaLib.lua_setglobal(L, "MikuLuaProfilerStrTb");
475460
#if XLUA
@@ -651,6 +636,14 @@ static int RemoveRefFunInfo(IntPtr L)
651636
return 0;
652637
}
653638

639+
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
640+
static int HandleError(IntPtr L)
641+
{
642+
string error = GetRefString(L, 1);
643+
Debug.LogError(error);
644+
return 0;
645+
}
646+
654647
[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
655648
static int EndSample(IntPtr L)
656649
{

0 commit comments

Comments
 (0)