2017-08-03 109 views
1

我正在开发一个Unity项目,并处理WebGL插件,它被Unity称为“jslib”。从直接c#和jslib回调调用事件时会调用不同的函数签名c#

虽然统一推荐使用UnitySendMessage当jslib与c#交互时,我更喜欢使用委托作为回调。在一个小测试代码中,它工作。但是当它运行到触发事件代码时,会发生奇怪的错误。

C#:

public class Test : MonoBehaviour { 

    public void Start() 
    { 
     UserLoginDispatcher.GetInstance().onLoginOver += onLoginOver; 
    } 

    public void OnDestroy() 
    { 
     UserLoginDispatcher.GetInstance().onLoginOver -= onLoginOver; 
    } 

    //[MonoPInvokeCallback(typeof(System.Action<string, string, SDKError>))] 
    public static void onLoginOver(string context, string pluginId, SDKError err) 
    { 
     string errmsg = err == null ? "" : err.errmsg; 
     LogFormat ("Login Over with context " + context + ", pluginId " + pluginId + ", errmsg:" + errmsg); 
    } 

    static void LogFormat(string format, params object[] param) 
    { 
     string log = string.Format (format, param); 
     Debug.Log (log); 
    } 
} 

public class UserLoginDispatcher { 
    private static UserLoginDispatcher s_Instance = new UserLoginDispatcher(); 
    public static UserLoginDispatcher GetInstance() 
    { 
     return s_Instance; 
    } 

    public delegate void UserLoginCallback(string context, string pluginId, SDKError error); 

    public event UserLoginCallback onLoginOver; 

    public void ProcessLoginOver(string context, string pluginId, SDKError error) 
    { 
     onLoginOver (context, pluginId, error); 
    } 
} 

public class WebGLPluginManager 
{ 
    [DllImport("__Internal")] 
    private static extern int Plugin_addLoginOverCB (LoginCBDelegate nativeCb); 
    private delegate void LoginCBDelegate(string context, string pluginId, int errno, int channelerrno, string errmsg); 

    [MonoPInvokeCallback(typeof(LoginCBDelegate))] 
    private static void OnLoginOver(string context, string pluginId, int errno, int channelerrno, string errmsg) 
    { 
     SDK.GetInstance().loginDispatcher.ProcessLoginOver (context, pluginId, new SDKError (errno, channelerrno, errmsg)); 
    } 

    public void Init() 
    { 
     Plugin_addLoginOverCB(OnLoginOver); 
    } 
} 

在jslib:

...//login succeed: 
Runtime.dynCall("viiiii", .....); 

虽然ProcessLoginOver在UserLoginDispatcher被调用时,onLoginOver在测试抛出一个错误:

无效的函数指针调用署名“VIIII ”。也许这是一个无效的值(例如通过在NULL指针上调用虚拟方法造成的)?或者调用一个错误类型的函数,这会失败? (这是值得建立你的源文件与-Werror(警告是错误),因为警告可能表明未定义的行为,可能会导致此)

但如果我尝试直接在C#代码中调用此事件,它可以被成功触发。

错误调用堆栈:

Uncaught abort(68) at Error 
at jsStackTrace (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23794:12) 
at stackTrace (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23808:11) 
at abort (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:464638:43) 
at nullFunc_viiii (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:50912:2) 
at Array.b68 (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:455121:2) 
at ftCall_viiii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:6602:49) 
at mftCall_viiii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:732323:2) 
at _UserLoginCallback_Invoke_m2599958513 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:533179:4) 
at _UserLoginCallback_Invoke_m2599958513 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:533170:14) 
at _UserLoginDispatcher_ProcessLoginOver_m2254139378 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:562185:2) 
at _WebGLPluginManager_OnLoginOver_m1528063286 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:548824:2) 
at Array.asm._WebGLPluginManager_OnLoginOver_m1528063286 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:921460:59) 
.... 

直接C#调用

Login Over with context a test session 12:13:28, pluginId DefaultUserLogin, errmsg: 
at __Z13GetStacktracei [GetStacktrace(int)] (blob:http://localhost:60912/6467252d-2f87-4a2c-9967-31868825bff3:2275897:2) 
at __Z17DebugStringToFileRK21DebugStringToFileData [DebugStringToFile(DebugStringToFileData const&)] (blob:http://localhost:60912/6467252d-2f87-4a2c-9967-31868825bff3:1247009:5) 
at __Z17DebugStringToFilePKciS0_i15LogMessageFlagsiiPFvRK11CppLogEntryE [DebugStringToFile(char const*, int, char const*, int, LogMessageFlags, int, int, void (*)(CppLogEntry const&))] (blob:http://localhost:60912/6467252d-2f87-4a2c-9967-31868825bff3:2562904:2) 
at __Z35DebugLogHandler_CUSTOM_Internal_Log7LogTypeP12Il2CppStringP12Il2CppObject [DebugLogHandler_CUSTOM_Internal_Log(LogType, Il2CppString*, Il2CppObject*)] (blob:http://localhost:60912/6467252d-2f87-4a2c-9967-31868825bff3:2126846:2) 
at Array.UnityLoader.d9fd6255ccad354c9cc5ad72abf13dcc.asm.__Z35DebugLogHandler_CUSTOM_Internal_Log7LogTypeP12Il2CppStringP12Il2CppObject [DebugLogHandler_CUSTOM_Internal_Log(LogType, Il2CppString*, Il2CppObject*)] (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:145057:93) 
at ftCall_viii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:7197:48) 
at mftCall_viii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:747533:2) 
at _DebugLogHandler_Internal_Log_m1116757358 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:719622:2) 
at Array._DebugLogHandler_LogFormat_m2613962716 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:620933:2) 
at mftCall_viiiiii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:708880:40) 
at __ZN23InterfaceActionInvoker4IiP18Object_t3753624189P8String_tP24ObjectU5BU5D_t2405707486E6InvokeEjP11Il2CppClassP12Il2CppObjectiS1_S3_S5_ [InterfaceActionInvoker4<int, Object_t3753624189*, String_t*, ObjectU5BU5D_t2405707486*>::Invoke(unsigned int, Il2CppClass*, Il2CppObject*, int, Object_t3753624189*, String_t*, ObjectU5BU5D_t2405707486*)] (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:394923:2) 
at Array._Logger_Log_m314997607 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:564686:3) 
at mftCall_viiii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:732320:39) 
at __ZN23InterfaceActionInvoker2IiP12Il2CppObjectE6InvokeEjP11Il2CppClassS1_iS1_ [InterfaceActionInvoker2<int, Il2CppObject*>::Invoke(unsigned int, Il2CppClass*, Il2CppObject*, int, Il2CppObject*)] (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:412747:2) 
at _Debug_Log_m2324883804 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:619913:2) 
at _Test_LogFormat_m983122927 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:579589:2) 
at Array._Test_onLoginOver_m2512123873 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:383768:2) 
at mftCall_viiiii (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:720078:40) 
at _UserLoginCallback_Invoke_m2599958513 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:533182:4) 
at _UserLoginDispatcher_ProcessLoginOver_m2254139378 (eval at loadDynamicLibrary (blob:http://localhost:60912/134eecaa-92a2-4615-b5b2-0a2ab6d2ed73:23235:19), <anonymous>:562185:2) 
.... 

我注意到,在直接C#调用,dynCall在jslib通话使用mftCall_viiiii,同时,该堆栈dynCall使用mftCall_viiii,可能这就是为什么函数调用失败。这很奇怪,为什么会发生这种情况?

回答

0

自己修复!

事实上,这是我的一个小错误:我只是忘记初始化loginDispatcher,所以当输入loginDispatcher时这个ptr是0,那么这个行为很混乱。

当“this”是0时,代码没有抛出任何异常,因为它是从c代码翻译的。它继续运行,直到事件调用函数,最后函数表没有这个函数并且抱怨,所以我看到了非常混乱的错误报告!

ps:在asm.js中的c#委托的行为如下:如果此ptr为0,则委托被视为静态函数,如果此ptr不为0,则委托被视为成员函数。这就是为什么当这个ptr为0时,调用mftCall_viiii而不是mftCall_viiiii