TH1/Unity/Assets/Scripts/TH1_Logic/CrashSight/CrashSightManager.cs
2026-06-05 11:56:51 +08:00

147 lines
5.0 KiB
C#

/*
* @Author: 白哉
* @Description:
* @Date: 2025年07月03日 星期四 10:07:49
* @Modify:
*/
using Logic.Config;
using TH1_Logic.Net;
using UnityEngine;
namespace Logic.CrashSight
{
public class CrashSightManager
{
public static CrashSightManager Instance => new CrashSightManager();
private const string CrashSightDeviceIdFallbackKey = "TH1_CrashSightDeviceId";
private static string _runtimeFallbackDeviceId;
private static ulong _reportedSteamUserId;
private CrashSightManager() { }
public void Initialize()
{
CrashSightAgent.ConfigCrashReporter(1);
// 设置上报的目标域名,请根据项目需求进行填写。(必填)
CrashSightAgent.ConfigCrashServerUrl("pc.crashsight.qq.com");
// 设置上报所指向的APP ID, 并进行初始化。APPID可以在管理端更多->产品设置->产品信息中找到。
CrashSightAgent.InitWithAppId("01076c49ce");
var deviceId = GetCrashSightDeviceId();
CrashSightAgent.SetDeviceId(deviceId);
CrashSightAgent.SetUserValue("DeviceId", deviceId);
}
public static void UpdateSteamUserId(ulong steamUserId)
{
if (steamUserId == 0 || !CrashSightAgent.IsInitialized || _reportedSteamUserId == steamUserId) return;
try
{
var value = steamUserId.ToString();
CrashSightAgent.SetUserId(value);
CrashSightAgent.SetUserValue("SteamId", value);
CrashSightAgent.SetUserValue("MemberId", value);
_reportedSteamUserId = steamUserId;
}
catch (System.Exception e)
{
Debug.LogWarning($"CrashSight SetSteamUserId failed: {e.Message}");
}
}
public static string GetCrashSightDeviceId()
{
try
{
var deviceId = SystemInfo.deviceUniqueIdentifier;
if (!string.IsNullOrEmpty(deviceId) && deviceId != SystemInfo.unsupportedIdentifier)
return deviceId;
var cachedId = PlayerPrefs.GetString(CrashSightDeviceIdFallbackKey, "");
if (!string.IsNullOrEmpty(cachedId))
return cachedId;
cachedId = $"th1-{System.Guid.NewGuid():N}";
PlayerPrefs.SetString(CrashSightDeviceIdFallbackKey, cachedId);
PlayerPrefs.Save();
return cachedId;
}
catch
{
if (string.IsNullOrEmpty(_runtimeFallbackDeviceId))
_runtimeFallbackDeviceId = $"th1-device-id-unavailable-{System.Guid.NewGuid():N}";
return _runtimeFallbackDeviceId;
}
}
}
public class LogSystem
{
public static string Record = string.Empty;
private static string GenerateHashCode()
{
string deviceId = SystemInfo.deviceUniqueIdentifier;
string timestamp = System.DateTime.UtcNow.Ticks.ToString();
string combined = deviceId + timestamp;
using (var md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(combined);
byte[] hashBytes = md5.ComputeHash(inputBytes);
// Convert the byte array to hexadecimal string
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
return sb.ToString();
}
}
public static void LogError(string message, Object context = null)
{
#if !UNITY_EDITOR
CrashSightAgent.PrintLog(CSLogSeverity.LogError, message + $"({GetSelfMemberIdSafe()})");
# endif
if (context!=null) Debug.LogError(message, context);
else Debug.LogError(message);
}
public static void LogWarning(string message, Object context = null)
{
#if !UNITY_EDITOR
CrashSightAgent.PrintLog(CSLogSeverity.LogWarning, message);
# endif
if (context!=null) Debug.LogWarning(message, context);
else Debug.LogWarning(message);
}
public static void LogInfo(string message, Object context = null)
{
#if !UNITY_EDITOR
CrashSightAgent.PrintLog(CSLogSeverity.LogInfo, message);
# endif
if (context!=null) Debug.Log(message, context);
else Debug.Log(message);
}
private static ulong GetSelfMemberIdSafe()
{
try
{
return LobbyManager.Instance?.Lobby?.GetSelfMemberId() ?? 0UL;
}
catch
{
return 0UL;
}
}
}
}