123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729 |
- /**
- * Unity Plugins Version 2.2.10
- *
- * android version 2.2.9
- * iOS version 2.7.3
- * window version 1.0.0
- */
- using System.Text;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Security.Cryptography;
- using System.Text.RegularExpressions;
- #if UNITY_ANDROID || UNITY_IPHONE || UNITY_IOS
- using com.xsj.Crasheye.U3D;
- #endif
- #if UNITY_ANDROID
- using com.xsj.Crasheye.U3D.Android;
- #endif
- #if UNITY_IPHONE || UNITY_IOS
- using com.xsj.Crasheye.U3D.IOS;
- #endif
- public class Crasheye : MonoBehaviour
- {
- public delegate void FnOnCrashCallback(bool bCaptureSucceed, string cpszCrashReportFile);
-
- public string YourAppKeyForAndroid = "YourAppKeyForAndroid";
- public string YourChannelIdForAndroid = "YourChannelIdForAndroid";
- public string YourAppKeyForIOS = "YourAppKeyForIOS";
- public string YourChannelIdForIOS = "YourChannelIdForIOS";
- public string YourAppKeyForPC = "YourAppKeyForPC";
- public string YourChannelIdForPC = "YourChannelIdForPC";
- #if UNITY_ANDROID || UNITY_IPHONE || UNITY_IOS
- public static CrasheyeLib crasheyeLib = null;
- #endif
-
- #if UNITY_ANDROID || UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_WIN
- private static string YourChannelId = "NA";
- #endif
- public static void AddCustomLog(string collectFile)
- {
- #if UNITY_STANDALONE_WIN && !UNITY_EDITOR
- DumpForPC.AddCustomLog(collectFile);
- #endif
- }
- public static void AddCustomLogDirectory(string CollectLogDirectory)
- {
- #if UNITY_STANDALONE_WIN && !UNITY_EDITOR
- DumpForPC.AddCustomLogDirectory(CollectLogDirectory);
- #endif
- }
- void Start()
- {
- DontDestroyOnLoad(gameObject);
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- SetChannelID(YourChannelIdForAndroid);
- StartInitCrasheye(YourAppKeyForAndroid);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- SetChannelID(YourChannelIdForIOS);
- StartInitCrasheye(YourAppKeyForIOS);
- #endif
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- SetChannelID(YourAppKeyForPC);
- StartInitCrasheye(YourAppKeyForPC);
- #endif
- }
- }
- /// <summary>
- /// SetURL
- /// </summary>
- /// <param name="SetUrl"></param>
- public static void SetURL(string url)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.SetURL(url);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.SetURL(url);
- #endif
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- DumpForPC.SetURL(url);
- #endif
- }
- }
-
- /// <summary>
- /// 设置Dump是否强制上报(忽略玩家在DumpReport界面上的选择)
- ///
- /// </summary>
- /// <param name="isForceUpload"> isForceUpload 是否设置Dump强制上报 </param>
- public static void SetForceUpload(bool isForceUpload)
- {
- Debug.Log("SetForceUpload");
- #if UNITY_STANDALONE_WIN
- DumpForPC.SetForceUpload(isForceUpload);
- #endif
- }
- /// <summary>
- /// 启动Crasheye
- /// </summary>
- /// <param name="Your_AppKey"></param>
- public static void StartInitCrasheye(string Your_AppKey)
- {
- RegisterLogCallback();
-
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.Init(Your_AppKey, YourChannelId);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.Init(Your_AppKey, YourChannelId);
- #endif
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- DumpForPC.Init(Your_AppKey, YourChannelId);
- #endif
- }
- //bool result = Crasheye.PushLogTrace("Crasheye Demo Push Log");
- //Debug.Log("PushLogTrace:" + result);
- }
- /// <summary>
- /// 注册脚本捕获
- /// </summary>
- public static void RegisterLogCallback()
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- crasheyeLib = new LibForAndroid();
- CrasheyeForAndroid.setInternalExtraData();
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- crasheyeLib = new LibForiOS();
- #endif
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- Debug.Log("RegisterLogCallback");
- AppDomain.CurrentDomain.UnhandledException += DumpForPC.OnHandleUnresolvedException;
- SetRegisterLogFunction(DumpForPC.OnHandleLogCallback);
- Application.logMessageReceived += RegisterLogFunction;
- #endif
- }
- #if UNITY_ANDROID || UNITY_IPHONE || UNITY_IOS
- if (crasheyeLib == null)
- {
- return;
- }
- System.AppDomain.CurrentDomain.UnhandledException += new System.UnhandledExceptionEventHandler(crasheyeLib.OnHandleUnresolvedException);
- SetRegisterLogFunction(crasheyeLib.OnHandleLogCallback);
- #if UNITY_5
- Application.logMessageReceived += RegisterLogFunction;
- #else
- Application.RegisterLogCallback(RegisterLogFunction);
- #endif
- #endif
- }
- private static void RegisterLogFunction(string logString, string stackTrace, LogType type)
- {
- #if (UNITY_IPHONE || UNITY_IOS) && !UNITY_EDITOR
- if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- CrasheyeForIOS.SaveLogger(logString);
- }
- if (CrasheyeForIOS.GetLoggingLines() > 0 &&
- Application.platform == RuntimePlatform.IPhonePlayer &&
- (type == LogType.Assert || type == LogType.Exception)
- )
- {
- CrasheyeForIOS.addLog(CrasheyeForIOS.GetLogger());
- }
- #endif
- if (m_RegisterLog != null)
- {
- m_RegisterLog(logString, stackTrace, type);
- }
- #if (UNITY_IPHONE || UNITY_IOS) && !UNITY_EDITOR
- if (CrasheyeForIOS.GetLoggingLines() > 0 &&
- Application.platform == RuntimePlatform.IPhonePlayer &&
- (type == LogType.Assert || type == LogType.Exception)
- )
- {
- CrasheyeForIOS.removeLog();
- }
- #endif
- }
- public delegate void RegisterLog(string logString, string stackTrace, LogType type);
- private static RegisterLog m_RegisterLog = null;
-
- /// <summary>
- /// 设置用户的脚本回调的函数
- /// </summary>
- /// <param name="registerLogCBFun"></param>
- public static void SetRegisterLogFunction(RegisterLog registerLogCBFun)
- {
- Debug.Log("SetRegisterLogFunction");
- m_RegisterLog += registerLogCBFun;
- }
-
- /// <summary>
- /// 发送脚本异常
- /// </summary>
- /// <param name="ex">Excepiton Info</param>
- public static void SendScriptException(Exception ex)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- crasheyeLib.LibSendScriptException(ex.Message, ex.StackTrace);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- crasheyeLib.LibSendScriptException(ex.Message, ex.StackTrace);
- #endif
- }
- }
- /// <summary>
- /// 上报脚本异常
- /// </summary>
- /// <param name="errorTitle">错误的标题</param>
- /// <param name="stacktrace">错误堆栈信息</param>
- /// <param name="language">语言</param>
- public static void SendScriptException(string errorTitle, string stacktrace, string language)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- crasheyeLib.LibSendScriptException(errorTitle, stacktrace, language);
- #endif
- }
- else if(Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- crasheyeLib.LibSendScriptException(errorTitle, stacktrace);
- #endif
- } else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- DumpForPC.SendScriptException(errorTitle, stacktrace, language);
- #endif
- }
- }
-
- /// <summary>
- /// 设置渠道号
- /// </summary>
- /// <param name="yourChannelID"></param>
- public static void SetChannelID(string yourChannelID)
- {
- if (String.IsNullOrEmpty(yourChannelID))
- {
- Debug.LogError("set channel id value is null or empty!");
- return;
- }
- if (yourChannelID.Equals("YourChannelIdForAndroid") || yourChannelID.Equals("YourChannelIdForIOS"))
- {
- return;
- }
- #if UNITY_ANDROID || UNITY_IPHONE || UNITY_IOS || UNITY_STANDALONE_WIN
- YourChannelId = yourChannelID;
- #endif
- }
- /// <summary>
- /// 设置是否只在wifi下上报报告文件
- /// </summary>
- /// <param name="enabled"></param>
- public static void SetFlushOnlyOverWiFi(bool enabled)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.SetFlushOnlyOverWiFi(enabled);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.SetFlushOnlyOverWiFi(enabled);
- #endif
- }
- }
- /// <summary>
- /// 设置该版本是否为测试版本
- /// </summary>
- /// <param name="isBeta">是否为测试版本</param>
- public static void SetIsBetaVersion(bool isBeta)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.SetIsBetaVersion(isBeta);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.SetBeta(isBeta);
- #endif
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN && !UNITY_EDITOR
- DumpForPC.SetBeta();
- #endif
- }
- }
- /// <summary>
- /// 设置版本号信息
- /// </summary>
- /// <param name="yourAppVersion">App版本号</param>
- public static void SetAppVersion(string yourAppVersion)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.SetAppVersion(yourAppVersion);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.SetAppVersion(yourAppVersion);
- #endif
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- DumpForPC.SetAppVersion(yourAppVersion);
- #endif
- }
- }
- /// <summary>
- /// 设置用户信息
- /// </summary>
- /// <param name="setUserIdentifier">用户标识</param>
- public static void SetUserIdentifier(string userIdentifier)
- {
- if(string.IsNullOrEmpty(userIdentifier))
- {
- return;
- }
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.SetUserIdentifier(userIdentifier);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.SetUserIdentifier(userIdentifier);
- #endif
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- DumpForPC.SetUserIdentifier_UTF8(userIdentifier);
- #endif
- }
- }
- /// <summary>
- /// 如果启动了C#堆栈回溯可能会导致某些机型出现宕机
- /// </summary>
- public static void EnableCSharpStackTrace()
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.EnableCSharpStackTrace();
- #endif
- }
- /// <summary>
- /// 添加自定义数据
- /// </summary>
- /// <param name="key">Key.</param>
- /// <param name="value">Value.</param>
- public static void AddExtraData(string key, string value)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.AddExtraData(key, value);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.AddExtraData(key, value);
- #endif
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- DumpForPC.AddExtraData(key,value);
- #endif
- }
- }
- public static void AddExtraDataUTF8(string key, string value)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.AddExtraData(key, value);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.AddExtraData(key, value);
- #endif
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- DumpForPC.AddExtraDataUTF8(key, value);
- #endif
- }
- }
- /// <summary>
- /// 获取自定义数据
- /// </summary>
- /// <param name="key">Key</param>
- /// <returns></returns>
- public static string GetExtraData(string key)
- {
- string extraData = "";
- if (string.IsNullOrEmpty(key))
- {
- return extraData;
- }
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- extraData = CrasheyeForAndroid.GetExtraData(key);
- #endif
- }
- else
- {
- #if UNITY_IPHONE || UNITY_IOS
- extraData = CrasheyeForIOS.GetExtraData(key);
- #endif
- }
- return extraData;
- }
- /// <summary>
- /// 移除自定义值
- /// </summary>
- /// <param name="key">Key</param>
- public static void RemoveExtraData(string key)
- {
- if (string.IsNullOrEmpty(key))
- {
- return;
- }
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.RemoveExtraData(key);
- #endif
- }
- else if(Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.RemoveExtraData(key);
- #endif
- }
- }
- /// <summary>
- /// 清除自定义数据
- /// </summary>
- public static void CleanExtraData()
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.CleanExtraData();
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.ClearExtraData();
- #endif
- }
- }
- /// <summary>
- /// 设置打点数据接口
- /// </summary>
- /// <param name="breadcrumb">Breadcrumb.</param>
- public static void SetBreadCrumbType(Int32 type)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- // TODO
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- // TODO
- #endif
- }
- else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- DumpForPC.SetBreadCrumbType(type);
- #endif
- }
- }
- /// <summary>
- /// 打点数据
- /// </summary>
- /// <param name="breadcrumb">Breadcrumb.</param>
- public static void LeaveBreadcrumb(string breadcrumb)
- {
- if (string.IsNullOrEmpty(breadcrumb))
- {
- return;
- }
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.LeaveBreadcrumb(breadcrumb);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.LeaveBreadcrumb(breadcrumb);
- #endif
- } else if (Application.platform == RuntimePlatform.WindowsPlayer)
- {
- #if UNITY_STANDALONE_WIN
- DumpForPC.leaveBreadcrumb(breadcrumb);
- #endif
- }
- }
- /// <summary>
- /// 指定获取应用程序log日志的行数
- /// </summary>
- /// <param name="lines">需要获取log行数</param>
- public static void SetLogging(int lines)
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.SetLogging(lines);
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.SetLogging(lines);
- #endif
- }
- }
- /// <summary>
- /// 获取应用程序log日志关键字过滤
- /// </summary>
- /// <param name="filter">需要过滤关键字</param>
- public static void SetLogging(string filter)
- {
- if (string.IsNullOrEmpty(filter))
- {
- return;
- }
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.SetLogging(filter);
- #endif
- }
- }
- /// <summary>
- /// 获取应用程序log日志(过滤条件:关键字过滤+行数)
- /// </summary>
- /// <param name="lines">需要获取的行数</param>
- /// <param name="filter">需要过滤的关键字</param>
- public static void SetLogging(int lines, string filter)
- {
- if (string.IsNullOrEmpty(filter))
- {
- return;
- }
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- CrasheyeForAndroid.SetLogging(lines, filter);
- #endif
- }
- else if(Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- CrasheyeForIOS.SetLogging(lines, filter);
- #endif
- }
- }
- /// <summary>
- /// 获取sdk版本号
- /// </summary>
- /// <returns></returns>
- public static string GetAppVersion()
- {
- if (Application.platform == RuntimePlatform.Android)
- {
- #if UNITY_ANDROID
- return CrasheyeForAndroid.GetAppVersion();
- #endif
- }
- else if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- #if UNITY_IPHONE || UNITY_IOS
- return CrasheyeForIOS.GetAppVersion();
- #endif
- }
- return "NA";
- }
- public static bool SetBackgroundUpload(bool isBackgroundUpload)
- {
-
- #if UNITY_STANDALONE_WIN
- return DumpForPC.SetBackgroundUpload(isBackgroundUpload);
- #endif
- return false;
- }
-
- public static bool SetCrashCallback(FnOnCrashCallback pCallback)
- {
- #if UNITY_STANDALONE_WIN
- return DumpForPC.SetOnMiniDumpCreateCallBack((bCaptureSucceed, cpszCrashReportFile)=>{
- string szCrashReportFile = Marshal.PtrToStringUni(cpszCrashReportFile);
- pCallback?.Invoke(bCaptureSucceed, szCrashReportFile);
- });
- #endif
- return false;
- }
-
- public static bool PushLogTrace(string cpszMessage)
- {
- #if UNITY_STANDALONE_WIN
- return DumpForPC.PushLogTrace(cpszMessage);
- #endif
- return false;
- }
- }
|