From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- Client/Assets/Scripts/Tss/BugtraceAgent.cs | 147 +++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 Client/Assets/Scripts/Tss/BugtraceAgent.cs (limited to 'Client/Assets/Scripts/Tss/BugtraceAgent.cs') diff --git a/Client/Assets/Scripts/Tss/BugtraceAgent.cs b/Client/Assets/Scripts/Tss/BugtraceAgent.cs new file mode 100644 index 00000000..55dea179 --- /dev/null +++ b/Client/Assets/Scripts/Tss/BugtraceAgent.cs @@ -0,0 +1,147 @@ +using UnityEngine; +using System.Collections; +using System; +using System.Reflection; +using System.Runtime.InteropServices; + +public sealed class BugtraceAgent { + private const string SDK_PACKAGE = "com.tencent.tp.bugtrace"; + +#if UNITY_4_6 + private static Application.LogCallback s_oldLogCallback; + public static Application.LogCallback GetCurrentLogCallback(){ + Type t = typeof(Application); + + // Instance Field + BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public; + + // Static Field + flag = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public; + FieldInfo fieldInfo = t.GetField ("s_LogCallback", flag); + + if (fieldInfo != null && fieldInfo.IsPrivate && fieldInfo.IsStatic) { + object callback = fieldInfo.GetValue (null); + if (callback != null){ + return (Application.LogCallback)callback; + + } + } + return null; + } +#endif + + private static bool _isInitialized = false; + public static void EnableExceptionHandler(){ + if (_isInitialized){ + return; + } + RegisterExceptionHandler(); + _isInitialized = true; + } + + public static void DisableExceptionHandler(){ + if (!_isInitialized){ + return; + } + UnregisterExceptionHandler(); + _isInitialized = false; + } + + private static void RegisterExceptionHandler(){ +#if UNITY_4_6 + AppDomain.CurrentDomain.UnhandledException += UncaughtExceptionHandler; + s_oldLogCallback = GetCurrentLogCallback (); + Application.RegisterLogCallback (LogCallbackHandler); +#endif + +#if (UNITY_5_0 || UNITY_5_1 || UNITY_5_2) + Application.logMessageReceived += LogCallbackHandler; +#endif + + } + + private static void UnregisterExceptionHandler(){ +#if UNITY_4_6 + AppDomain.CurrentDomain.UnhandledException -= UncaughtExceptionHandler; + Application.RegisterLogCallback (s_oldLogCallback); +#endif + +#if (UNITY_5_0 || UNITY_5_1 || UNITY_5_2) + Application.logMessageReceived -= LogCallbackHandler; +#endif + } + + private static void LogCallbackHandler(string condition, string stack, LogType type){ + if (type == LogType.Exception) { + //FileLog.Instance.Log (" reason:" + condition); + //FileLog.Instance.Log (" stack:" + stack); + HandleException (condition, stack); + } +#if UNITY_4_6 + if (s_oldLogCallback != null) { + s_oldLogCallback(condition, stack, type); + } +#endif + + } + + private static void UncaughtExceptionHandler(object sender, System.UnhandledExceptionEventArgs args){ + Exception e = (Exception)args.ExceptionObject; + if (e != null){ + HandleException (e.Message, e.StackTrace); + } + + } + + //private static void ReportException(string reason, string stack){ + //FileLog.Instance.Log (" reason:" + reason); + //FileLog.Instance.Log (" stack:" + stack); + //HandleException (reason, stack); + //} + +#if (UNITY_EDITOR || UNITY_STANDALONE) + private static void HandleException(string reason, string stack){ + + } + + + + + +#elif UNITY_ANDROID + private static readonly string CLASS_UNITYAGENT = "com.tencent.tp.bugtrace.BugtraceAgent"; + private static AndroidJavaObject _unityAgent; + public static AndroidJavaObject UnityAgent{ + get{ + if (_unityAgent == null){ + using (AndroidJavaClass clazz = new AndroidJavaClass(CLASS_UNITYAGENT)) { + _unityAgent = clazz.CallStatic ("getInstance"); + } + } + return _unityAgent; + } + } + private static void HandleException(string reason, string stack){ + string cmd = "crash-reportcsharpexception|"; + cmd += "cause:" + reason; + cmd += "stack:" + stack; + //FileLog.Instance.Log("Android HandleException"); + try{ + AndroidJavaClass agent = new AndroidJavaClass("com.tencent.tp.TssJavaMethod"); + if (agent != null){ + agent.CallStatic("sendCmd", cmd); + } + }catch{ + //FileLog.Instance.Log("Android HandleException catch"); + } + } + +#elif (UNITY_IOS || UNITY_IPHONE) + [DllImport("__Internal")] + private static extern void ReportCSharpException (string reason, string stack); + private static void HandleException(string reason, string stack){ + ReportCSharpException(reason, stack); + } + +#endif +} // end of class BugtraceAgent -- cgit v1.1-26-g67d0