Skip to content

Commit 343e89a

Browse files
committed
* support Exception 'turn around'~
1 parent 3220760 commit 343e89a

12 files changed

Lines changed: 98 additions & 66 deletions

File tree

BindGenerater/Generater/CSharp/DelegateGenerater.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public override void Gen()
128128
eventFuncDeclear += ")";
129129

130130
CS.Writer.Start(eventFuncDeclear);
131+
CS.Writer.WriteLine("Exception __e = null");
131132
CS.Writer.Start("try");
132133
//_logMessageReceived(unbox(arg0), unbox(arg1), unbox(arg2));
133134
var callCmd = $"_{name}(";
@@ -163,10 +164,12 @@ public override void Gen()
163164
}
164165
CS.Writer.End();//try
165166
CS.Writer.Start("catch(Exception e)");
166-
CS.Writer.WriteLine("ScriptEngine.OnException(e.ToString())");
167+
CS.Writer.WriteLine("__e = e");
168+
CS.Writer.End();//catch
169+
CS.Writer.WriteLine("if(__e != null)", false);
170+
CS.Writer.WriteLine("ScriptEngine.OnException(__e.ToString())");
167171
if (returnType != null)
168172
CS.Writer.WriteLine($"return default({returnTypeName})");
169-
CS.Writer.End();//catch
170173

171174
CS.Writer.End();//method
172175

@@ -188,6 +191,7 @@ public override void Gen()
188191
CS.Writer.WriteLine(Utils.BindMethodName(method, false, false) + $"({targetHandle}{res})");
189192
//var value_p = Marshal.GetFunctionPointerForDelegate(logMessageReceivedAction);
190193
//MonoBind.UnityEngine_Application_add_logMessageReceived(value_p);
194+
CS.Writer.WriteLine("ScriptEngine.CheckException()");
191195
CS.Writer.End(); //if(attach)
192196
CS.Writer.End(); // add
193197
}
@@ -199,6 +203,7 @@ public override void Gen()
199203
CS.Writer.Start($"if(_{name} == null)");
200204
var res = TypeResolver.Resolve(type).Box($"{name}Action");
201205
CS.Writer.WriteLine(Utils.BindMethodName(removeMethod, false, false) + $"({targetHandle}{res})");
206+
CS.Writer.WriteLine("ScriptEngine.CheckException()");
202207
CS.Writer.End(); //if(attach)
203208
CS.Writer.End(); // remove
204209
}

BindGenerater/Generater/CSharp/GenerateBindings.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,23 @@ public static void Gen()
116116

117117
CS.Writer.WriteLine($"[MonoPInvokeCallback(typeof({methodName}_Type))]", false);
118118
CS.Writer.Start($"static {MethodResolver.Resolve(method).ReturnType()} {methodName} {Utils.BindMethodParamDefine(method, true)}");
119+
120+
CS.Writer.WriteLine("Exception __e = null");
119121
CS.Writer.Start("try");
120122

121123
var reName = MethodResolver.Resolve(method).Implement("_value");
122124
if (!string.IsNullOrEmpty(reName))
123125
CS.Writer.WriteLine($"return {reName}");
124126
CS.Writer.End();//try
125127
CS.Writer.Start("catch(Exception e)");
126-
CS.Writer.WriteLine("ScriptEngine.OnException(e.ToString())");
128+
CS.Writer.WriteLine("__e = e");
129+
CS.Writer.End();//catch
130+
131+
CS.Writer.WriteLine("if(__e != null)", false);
132+
CS.Writer.WriteLine("ScriptEngine.OnException(__e.ToString())");
127133
if (!string.IsNullOrEmpty(reName))
128134
CS.Writer.WriteLine($"return default({MethodResolver.Resolve(method).ReturnType()})");
129-
CS.Writer.End();//catch
135+
130136
CS.Writer.End();//method
131137

132138
}

BindGenerater/Generater/CSharp/MethodGenerater.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ void GenGeter()
6262
}
6363
writer.Start("get");
6464
var res = MethodResolver.Resolve(genMethod).Call("res");
65+
writer.WriteLine("ScriptEngine.CheckException()");
6566
writer.WriteLine($"return {res}");
6667
writer.End();
6768
}
@@ -76,20 +77,23 @@ void GenSeter()
7677

7778
writer.Start("set");
7879
MethodResolver.Resolve(genMethod).Call("");
80+
writer.WriteLine("ScriptEngine.CheckException()");
7981
writer.End();
8082
}
8183

8284
void GenAddOn()
8385
{
8486
writer.Start("add");
8587
MethodResolver.Resolve(genMethod).Call("");
88+
writer.WriteLine("ScriptEngine.CheckException()");
8689
writer.End();
8790
}
8891

8992
void GenRemoveOn()
9093
{
9194
writer.Start("remove");
9295
MethodResolver.Resolve(genMethod).Call("");
96+
writer.WriteLine("ScriptEngine.CheckException()");
9397
writer.End();
9498
}
9599

@@ -101,17 +105,20 @@ void GenMethod()
101105
if(genMethod.DeclaringType.IsValueType)
102106
{
103107
CS.Writer.WriteLine(Utils.BindMethodName(genMethod));
108+
writer.WriteLine("ScriptEngine.CheckException()");
104109
}
105110
else
106111
{
107112
CS.Writer.WriteLine($"var h = {Utils.BindMethodName(genMethod)}");
113+
writer.WriteLine("ScriptEngine.CheckException()");
108114
CS.Writer.WriteLine($"SetHandle(h)");
109115
CS.Writer.WriteLine("ObjectStore.Store(this, h)");
110116
}
111117
}
112118
else
113119
{
114120
var res = MethodResolver.Resolve(genMethod).Call("res");
121+
writer.WriteLine("ScriptEngine.CheckException()");
115122
writer.WriteLine($"return {res}");
116123
}
117124

BindGenerater/Generater/CSharp/MethodResolver.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ public override string Implement(string name)
338338

339339
callCmd += ")";
340340
CS.Writer.WriteLine(callCmd);
341+
CS.Writer.WriteLine("ScriptEngine.CheckException()");
341342
if (returnType != null)
342343
{
343344
var res = TypeResolver.Resolve(returnType).Box("res");

BindGenerater/Tools/ScriptEngine.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public class ScriptEngine
1616

1717
[DllImport(XMONO_LIB, EntryPoint = "OnExceptionMono", CallingConvention = CallingConvention.Cdecl)]
1818
public static extern void OnException(string msg);
19+
[DllImport(XMONO_LIB, EntryPoint = "CheckExceptionIl2cpp", CallingConvention = CallingConvention.Cdecl)]
20+
public static extern void CheckException();
1921
}
2022
}
2123

DemoProject/Assets/Plugins/PureScript/ScriptEngine.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class ScriptEngine
2121

2222
[DllImport(XMONO_LIB, EntryPoint = "OnExceptionIl2cpp", CallingConvention = CallingConvention.Cdecl)]
2323
public static extern void OnException(string msg);
24+
[DllImport(XMONO_LIB, EntryPoint = "CheckExceptionMono", CallingConvention = CallingConvention.Cdecl)]
25+
public static extern void CheckException();
2426
}
2527
}
2628

-512 Bytes
Binary file not shown.

DemoProject/MonoTest/Demo/TestException.cs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,34 @@
55
using System.Threading.Tasks;
66
using UnityEngine;
77
using PureScript;
8+
using System.Runtime.InteropServices;
9+
810
public class TestException : MonoBehaviour
911
{
1012
ExceptionTest other;
1113

14+
[DllImport("ScriptEngine", EntryPoint = "OnExceptionIl2cpp", CallingConvention = CallingConvention.Cdecl)]
15+
public static extern void RaiseIl2cppException(string msg);
16+
1217
private void Start()
1318
{
1419
Debug.LogError("##Start 1");
1520
other = new ExceptionTest();
1621
Debug.LogError("##Start 2");
17-
other.callback = OnCallback;
22+
other.callback = TestCallbackException;
1823
Debug.LogError("##Start 3");
24+
//Application.focusChanged += OnFocusNullPointException;
1925
Application.focusChanged += OnFocusCallbackException;
2026
Debug.LogError("##Start 4");
2127
}
2228

2329
/*
2430
Exception: System.NullReferenceException: Object reference not set to an instance of an object.
25-
* at PureScript.ExceptionTest.NullPointException () [0x00000] in <00000000000000000000000000000000>:0
31+
* at PureScript.ExceptionTest.NullPointException () [0x00000] in <00000000000000000000000000000000>:0
2632
at UnityBind.PureScript_ExceptionTest_NullPointException (System.Int32 thiz_h) [0x00000] in <00000000000000000000000000000000>:0
27-
at (wrapper managed-to-native) System.Object.wrapper_native_00007FFA513268C0(int)
28-
* at PureScript.ExceptionTest.NullPointException () [0x0000c] in <8365f2ae61e94f83a867dc955d743bc6>:0
29-
at TestException.OnFocusNullPointException (System.Boolean focus) [0x00012] in <7bba8e115a884a978ebee1a64957c99e>:0
33+
at (wrapper managed-to-native) PureScript.ScriptEngine.CheckException()
34+
* at PureScript.ExceptionTest.NullPointException () [0x00012] in <32aba121fea14339b39ae3ef7a81ab85>:0
35+
at TestException.OnFocusNullPointException (System.Boolean focus) [0x00012] in <a78fd91ac283481f9b729fcc794fb0b0>:0
3036
at UnityEngine.Application.InvokeFocusChanged (System.Boolean focus) [0x0000b] in <cf7fb9a754e74776ae71c25165ca083c>:0
3137
at (wrapper native-to-managed) UnityEngine.Application.InvokeFocusChanged(bool,System.Exception&)
3238
*/
@@ -40,28 +46,39 @@ void OnFocusNullPointException(bool focus)
4046
}
4147
}
4248

43-
49+
/*
50+
Exception: System.Exception: System.Exception: This exception turn around ~~
51+
at TestException.TestCallbackException () [0x0000c] in <ae28305b9ad94cbf995b02f06babebda>:0
52+
at PureScript.ExceptionTest.Oncallback (System.Int32 arg0_h) [0x0000b] in <32aba121fea14339b39ae3ef7a81ab85>:0
53+
at PureScript.ScriptEngine.CheckException () [0x00000] in <00000000000000000000000000000000>:0
54+
at UnityBind.OnExceptionTest_callback (PureScript.ExceptionTest arg0) [0x00000] in <00000000000000000000000000000000>:0
55+
at System.Action.Invoke () [0x00000] in <00000000000000000000000000000000>:0
56+
* at PureScript.ExceptionTest.TestCallBack () [0x00000] in <00000000000000000000000000000000>:0
57+
at UnityBind.PureScript_ExceptionTest_TestCallBack (System.Int32 thiz_h) [0x00000] in <00000000000000000000000000000000>:0
58+
at (wrapper managed-to-native) PureScript.ScriptEngine.CheckException()
59+
* at PureScript.ExceptionTest.TestCallBack () [0x00012] in <32aba121fea14339b39ae3ef7a81ab85>:0
60+
at TestException.OnFocusCallbackException (System.Boolean focus) [0x00012] in <ae28305b9ad94cbf995b02f06babebda>:0
61+
at UnityEngine.Application.InvokeFocusChanged (System.Boolean focus) [0x0000b] in <cf7fb9a754e74776ae71c25165ca083c>:0
62+
at (wrapper native-to-managed) UnityEngine.Application.InvokeFocusChanged(bool,System.Exception&)
63+
*/
4464
void OnFocusCallbackException(bool focus)
4565
{
4666
if(focus)
4767
{
48-
try
49-
{
50-
Debug.LogError("##OnFocus 1");
51-
other.TestCallBack();
52-
Debug.LogError("##OnFocus 2");
53-
}catch(Exception e)
54-
{
55-
Debug.LogError(e.ToString());
56-
}
57-
68+
//RaiseMonoException("testException");
69+
//return;
70+
71+
Debug.LogError("##OnFocus 1");
72+
other.TestCallBack();
73+
Debug.LogError("##OnFocus 2");
74+
return;
5875
}
5976
}
6077

61-
void OnCallback()
78+
void TestCallbackException()
6279
{
6380
Debug.LogError("##OnCallback 1");
64-
throw new NullReferenceException("This exception turn around ~~");
81+
throw new Exception("This exception turn around ~~");
6582
Debug.LogError("##OnCallback 2");
6683
}
6784
}

DemoProject/MonoTest/EngineTest.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ public class EngineTest
1010

1111
public static void Main(string[] args)
1212
{
13-
StartTest();
13+
14+
try
15+
{
16+
StartTest();
17+
}catch(Exception e)
18+
{
19+
Debug.LogError(e.ToString());
20+
}
21+
1422
}
1523

1624
static void StartTest()
@@ -31,7 +39,6 @@ static void StartTest()
3139
//obj.AddComponent<TestLoader>();
3240
//obj.AddComponent<TestBehaviourScript>();
3341
obj.AddComponent<TestException>();
34-
3542
}
3643

3744
static void TestCube()

ScriptEngine/ScriptEngine.c

Lines changed: 21 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include "main/runtime.h"
55
#include "main/Mediator.h"
6+
#include <mono/metadata/exception.h>
67

78
// Macro to put before functions that need to be exposed to C#
89
#ifdef _WIN32
@@ -11,7 +12,8 @@
1112
#define DLLEXPORT
1213
#endif
1314

14-
void* funcPtr = 0;
15+
const char* il2cpp_exception = NULL;
16+
const char* mono_exception = NULL;
1517

1618
DLLEXPORT void SetupMono(char* bundleDir, const char* dllName)
1719
{
@@ -35,50 +37,31 @@ DLLEXPORT void* GetFuncPointer()
3537

3638
DLLEXPORT void OnExceptionIl2cpp(const char* msg)
3739
{
38-
raise_mono_exception_runtime(msg);
40+
//raise_mono_exception_runtime(msg);
41+
il2cpp_exception = msg;
42+
}
43+
DLLEXPORT void CheckExceptionIl2cpp()
44+
{
45+
if (il2cpp_exception)
46+
{
47+
MonoException* exc = mono_exception_from_name_msg(mono_get_corlib(), "System", "Exception", il2cpp_exception);
48+
il2cpp_exception = NULL;
49+
mono_raise_exception(exc);
50+
}
3951
}
4052

4153
DLLEXPORT void OnExceptionMono(const char* msg)
4254
{
43-
raise_il2cpp_exception_runtime(msg);
55+
//raise_il2cpp_exception_runtime(msg);
56+
mono_exception = msg;
4457
}
4558

46-
/*
47-
#include <iostream>
48-
#include <windows.h>
49-
#include <direct.h>
50-
51-
void SetupMono()
59+
DLLEXPORT void CheckExceptionMono()
5260
{
53-
typedef void(*funcptr)(char*, const char*);
54-
55-
funcptr func;
56-
57-
char path[1024];
58-
char* bundle_path = (char *)malloc(MAX_PATH);
59-
memset(bundle_path, 0, MAX_PATH);
60-
GetModuleFileName(NULL, bundle_path, MAX_PATH); // 得到当前执行文件的文件名(包含路径)
61-
*(strrchr(bundle_path, '\\')) = '\0'; // 删除文件名,只留下目录
62-
63-
const char* dllName = "MonoLib.dll";
64-
const char* funcName = "SetupMono";
65-
HMODULE hDLL = LoadLibrary(dllName);
66-
if (hDLL != NULL)
67-
{
68-
func = (funcptr)GetProcAddress(hDLL, funcName);
69-
if (func == NULL)
70-
{
71-
std::cout << "Cannot Find Function " << funcName << std::endl;
72-
return;
73-
}
74-
}
75-
else
61+
if (mono_exception)
7662
{
77-
std::cout << "Cannot Find " << dllName << std::endl;
78-
return;
63+
Il2CppException* exc = il2cpp_exception_from_name_msg(il2cpp_get_corlib(), "System", "Exception", mono_exception);
64+
mono_exception = NULL;
65+
il2cpp_raise_exception(exc);
7966
}
80-
81-
(*func)(bundle_path, "test.exe");
8267
}
83-
84-
*/

0 commit comments

Comments
 (0)