Update remote debugger

Support remote mobie device debugging
pull/13/head
hevinci 2022-06-15 16:52:54 +08:00
parent 33387e8d26
commit 8deb239450
20 changed files with 475 additions and 157 deletions

View File

@ -1,9 +1,12 @@
#if UNITY_2019_4_OR_NEWER
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using UnityEditor.Networking.PlayerConnection;
using UnityEngine.Networking.PlayerConnection;
namespace YooAsset.Editor
{
@ -37,12 +40,15 @@ namespace YooAsset.Editor
BundleView,
}
private readonly Dictionary<int, RemotePlayerSession> _playerSessions = new Dictionary<int, RemotePlayerSession>();
private ToolbarMenu _viewModeMenu;
private DebuggerAssetListViewer _assetListViewer;
private DebuggerBundleListViewer _bundleListViewer;
private EViewMode _viewMode;
private readonly DebugReport _debugReport = new DebugReport();
private DebugReport _debugReport;
private string _searchKeyWord;
@ -91,18 +97,84 @@ namespace YooAsset.Editor
Debug.LogError(e.ToString());
}
}
public void OnDestroy()
{
}
private void OnEnable()
{
EditorConnection.instance.Initialize();
EditorConnection.instance.RegisterConnection(OnHandleConnectionEvent);
EditorConnection.instance.RegisterDisconnection(OnHandleDisconnectionEvent);
EditorConnection.instance.Register(RemoteDebuggerDefine.kMsgSendPlayerToEditor, OnHandlePlayerMessage);
RemoteDebuggerInRuntime.EditorHandleDebugReportCallback = OnHandleDebugReport;
}
private void OnDisable()
{
EditorConnection.instance.Unregister(RemoteDebuggerDefine.kMsgSendPlayerToEditor, OnHandlePlayerMessage);
_playerSessions.Clear();
}
private void OnHandleConnectionEvent(int playerId)
{
Debug.Log($"Game player connection : {playerId}");
}
private void OnHandleDisconnectionEvent(int playerId)
{
Debug.Log($"Game player disconnection : {playerId}");
RemovePlayerSession(playerId);
}
private void OnHandlePlayerMessage(MessageEventArgs args)
{
var debugReport = DebugReport.Deserialize(args.data);
OnHandleDebugReport(args.playerId, debugReport);
}
private void OnHandleDebugReport(int playerId, DebugReport debugReport)
{
var playerSession = GetOrCreatePlayerSession(playerId);
playerSession.AddDebugReport(debugReport);
_debugReport = debugReport;
_assetListViewer.FillViewData(debugReport, _searchKeyWord);
_bundleListViewer.FillViewData(debugReport, _searchKeyWord);
}
private RemotePlayerSession GetOrCreatePlayerSession(int playerId)
{
if (_playerSessions.TryGetValue(playerId, out RemotePlayerSession session))
{
return session;
}
else
{
RemotePlayerSession newSession = new RemotePlayerSession(playerId);
_playerSessions.Add(playerId, newSession);
return newSession;
}
}
private void RemovePlayerSession(int playerId)
{
if (_playerSessions.ContainsKey(playerId))
_playerSessions.Remove(playerId);
}
private void SampleBtn_onClick()
{
YooAssets.GetDebugReport(_debugReport);
_assetListViewer.FillViewData(_debugReport, _searchKeyWord);
_bundleListViewer.FillViewData(_debugReport, _searchKeyWord);
// 发送采集数据的命令
RemoteCommand command = new RemoteCommand();
command.CommandType = (int)ERemoteCommand.SampleOnce;
command.CommandParam = string.Empty;
byte[] data = RemoteCommand.Serialize(command);
EditorConnection.instance.Send(RemoteDebuggerDefine.kMsgSendEditorToPlayer, data);
RemoteDebuggerInRuntime.EditorRequestDebugReport();
}
private void OnSearchKeyWordChange(ChangeEvent<string> e)
{
_searchKeyWord = e.newValue;
if (_debugReport != null)
{
_assetListViewer.FillViewData(_debugReport, _searchKeyWord);
_bundleListViewer.FillViewData(_debugReport, _searchKeyWord);
}
}
private void ViewModeMenuAction1(DropdownMenuAction action)
{
if (_viewMode != EViewMode.AssetView)

View File

@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset.Editor
{
internal class RemotePlayerSession
{
private readonly List<DebugReport> _reportList = new List<DebugReport>();
/// <summary>
/// 用户ID
/// </summary>
public int PlayerId { private set; get; }
/// <summary>
/// 保存的报告最大数量
/// </summary>
public int MaxReportCount { private set; get; }
public RemotePlayerSession(int playerId, int maxReportCount = 1000)
{
PlayerId = playerId;
MaxReportCount = maxReportCount;
}
/// <summary>
/// 添加一个调试报告
/// </summary>
public void AddDebugReport(DebugReport report)
{
if (report == null)
Debug.LogWarning("Invalid debug report data !");
if (_reportList.Count >= MaxReportCount)
_reportList.RemoveAt(0);
_reportList.Add(report);
}
/// <summary>
/// 获取最近一次的报告
/// </summary>
public DebugReport GetLatestReport()
{
if (_reportList.Count == 0)
return null;
return _reportList[_reportList.Count - 1];
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 89b4ea90b9f7e474d9e80077656ef6c4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -171,7 +171,7 @@ namespace YooAsset.Editor
// Status
StyleColor textColor;
if (providerInfo.Status == ProviderBase.EStatus.Fail)
if (providerInfo.Status == (int)ProviderBase.EStatus.Fail)
textColor = new StyleColor(Color.yellow);
else
textColor = label1.style.color;

View File

@ -147,7 +147,7 @@ namespace YooAsset.Editor
// Status
StyleColor textColor;
if (bundleInfo.Status == AssetBundleLoaderBase.EStatus.Failed)
if (bundleInfo.Status == (int)AssetBundleLoaderBase.EStatus.Failed)
textColor = new StyleColor(Color.yellow);
else
textColor = label1.style.color;

View File

@ -321,9 +321,10 @@ namespace YooAsset
}
#region 调试专属方法
internal static void GetDebugReport(DebugReport report)
internal static DebugReport GetDebugReport()
{
report.ClearAll();
DebugReport report = new DebugReport();
report.FrameCount = Time.frameCount;
report.BundleCount = _loaders.Count;
report.AssetCount = _providers.Count;
@ -334,8 +335,8 @@ namespace YooAsset
providerInfo.SpawnScene = provider.SpawnScene;
providerInfo.SpawnTime = provider.SpawnTime;
providerInfo.RefCount = provider.RefCount;
providerInfo.Status = provider.Status;
providerInfo.BundleInfos.Clear();
providerInfo.Status = (int)provider.Status;
providerInfo.BundleInfos = new List<DebugBundleInfo>();
report.ProviderInfos.Add(providerInfo);
if (provider is BundledProvider)
@ -347,6 +348,7 @@ namespace YooAsset
// 重新排序
report.ProviderInfos.Sort();
return report;
}
#endregion
}

View File

@ -185,7 +185,7 @@ namespace YooAsset
if (_isShowWaitForAsyncError == false)
{
_isShowWaitForAsyncError = true;
YooLogger.Error($"WaitForAsyncComplete failed ! BundleName : {MainBundleInfo.BundleName} States : {Status}");
YooLogger.Error($"WaitForAsyncComplete failed ! Try load bundle : {MainBundleInfo.BundleName} from remote with sync load method !");
}
break;
}

View File

@ -20,6 +20,7 @@ namespace YooAsset
private ESteps _steps = ESteps.None;
private float _tryTimer = 0;
private string _webURL;
private bool _isShowWaitForAsyncError = false;
private UnityWebRequest _webRequest;
@ -118,7 +119,11 @@ namespace YooAsset
/// </summary>
public override void WaitForAsyncComplete()
{
throw new System.NotImplementedException($"WebGL platform not support {nameof(WaitForAsyncComplete)}");
if (_isShowWaitForAsyncError == false)
{
_isShowWaitForAsyncError = true;
YooLogger.Error($"WebGL platform not support {nameof(WaitForAsyncComplete)} ! Use the async load method instead of the sync load method !");
}
}
}
}

View File

@ -104,7 +104,7 @@ namespace YooAsset
var bundleInfo = new DebugBundleInfo();
bundleInfo.BundleName = loader.MainBundleInfo.BundleName;
bundleInfo.RefCount = loader.RefCount;
bundleInfo.Status = loader.Status;
bundleInfo.Status = (int)loader.Status;
output.Add(bundleInfo);
}
}

View File

@ -43,7 +43,7 @@ namespace YooAsset
var bundleInfo = new DebugBundleInfo();
bundleInfo.BundleName = OwnerBundle.MainBundleInfo.BundleName;
bundleInfo.RefCount = OwnerBundle.RefCount;
bundleInfo.Status = OwnerBundle.Status;
bundleInfo.Status = (int)OwnerBundle.Status;
output.Add(bundleInfo);
DependBundleGroup.GetBundleDebugInfos(output);

View File

@ -1,21 +1,23 @@
using System;
namespace YooAsset
{
[Serializable]
internal class DebugBundleInfo
{
/// <summary>
/// 资源包名称
/// </summary>
public string BundleName { set; get; }
public string BundleName;
/// <summary>
/// 引用计数
/// </summary>
public int RefCount { set; get; }
public int RefCount;
/// <summary>
/// 加载状态
/// </summary>
public AssetBundleLoaderBase.EStatus Status { set; get; }
public int Status;
}
}

View File

@ -4,37 +4,38 @@ using System.Collections.Generic;
namespace YooAsset
{
[Serializable]
internal class DebugProviderInfo : IComparer<DebugProviderInfo>, IComparable<DebugProviderInfo>
{
/// <summary>
/// 资源对象路径
/// </summary>
public string AssetPath { set; get; }
public string AssetPath;
/// <summary>
/// 资源出生的场景
/// </summary>
public string SpawnScene { set; get; }
public string SpawnScene;
/// <summary>
/// 资源出生的时间
/// </summary>
public string SpawnTime { set; get; }
public string SpawnTime;
/// <summary>
/// 引用计数
/// </summary>
public int RefCount { set; get; }
public int RefCount;
/// <summary>
/// 加载状态
/// </summary>
public ProviderBase.EStatus Status { set; get; }
public int Status;
/// <summary>
/// 依赖的资源包列表
/// </summary>
public readonly List<DebugBundleInfo> BundleInfos = new List<DebugBundleInfo>();
public List<DebugBundleInfo> BundleInfos;
public int CompareTo(DebugProviderInfo other)
{

View File

@ -1,21 +1,49 @@
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
/// <summary>
/// 资源系统调试信息
/// </summary>
[Serializable]
internal class DebugReport
{
public readonly List<DebugProviderInfo> ProviderInfos = new List<DebugProviderInfo>(1000);
public int BundleCount { set; get; }
public int AssetCount { set; get; }
public List<DebugProviderInfo> ProviderInfos = new List<DebugProviderInfo>(1000);
public void ClearAll()
/// <summary>
/// 游戏帧
/// </summary>
public int FrameCount;
/// <summary>
/// 资源包总数
/// </summary>
public int BundleCount;
/// <summary>
/// 资源对象总数
/// </summary>
public int AssetCount;
/// <summary>
/// 序列化
/// </summary>
public static byte[] Serialize(DebugReport debugReport)
{
ProviderInfos.Clear();
return Encoding.UTF8.GetBytes(JsonUtility.ToJson(debugReport));
}
/// <summary>
/// 反序列化
/// </summary>
public static DebugReport Deserialize(byte[] data)
{
return JsonUtility.FromJson<DebugReport>(Encoding.UTF8.GetString(data));
}
}
}

View File

@ -0,0 +1,45 @@
using System;
using System.Text;
using UnityEngine;
namespace YooAsset
{
internal enum ERemoteCommand
{
/// <summary>
/// 采样一次
/// </summary>
SampleOnce = 0,
}
[Serializable]
internal class RemoteCommand
{
/// <summary>
/// 命令类型
/// </summary>
public int CommandType;
/// <summary>
/// 命令附加参数
/// </summary>
public string CommandParam;
/// <summary>
/// 序列化
/// </summary>
public static byte[] Serialize(RemoteCommand command)
{
return Encoding.UTF8.GetBytes(JsonUtility.ToJson(command));
}
/// <summary>
/// 反序列化
/// </summary>
public static RemoteCommand Deserialize(byte[] data)
{
return JsonUtility.FromJson<RemoteCommand>(Encoding.UTF8.GetString(data));
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e79402f4d610fb4408aa6f8a94b09cb9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,12 @@
using System;
using System.Text;
using UnityEngine;
namespace YooAsset
{
internal class RemoteDebuggerDefine
{
public static readonly Guid kMsgSendPlayerToEditor = new Guid("e34a5702dd353724aa315fb8011f08c3");
public static readonly Guid kMsgSendEditorToPlayer = new Guid("4d1926c9df5b052469a1c63448b7609a");
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3fd4f5f47edbfd54ba92427daf9ea3a4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,64 @@
using System;
using System.Text;
using UnityEngine;
using UnityEngine.Networking.PlayerConnection;
namespace YooAsset
{
internal class RemoteDebuggerInRuntime : MonoBehaviour
{
#if UNITY_EDITOR
/// <summary>
/// 编辑器下获取报告的回调
/// </summary>
public static Action<int, DebugReport> EditorHandleDebugReportCallback;
/// <summary>
/// 编辑器下请求报告数据
/// </summary>
public static void EditorRequestDebugReport()
{
if(UnityEditor.EditorApplication.isPlaying)
{
var report = AssetSystem.GetDebugReport();
EditorHandleDebugReportCallback?.Invoke(0, report);
}
}
#else
private void OnEnable()
{
PlayerConnection.instance.RegisterConnection(OnHandleConnectionEvent);
PlayerConnection.instance.RegisterDisconnection(OnHandleDisconnectionEvent);
PlayerConnection.instance.Register(RemoteDebuggerDefine.kMsgSendEditorToPlayer, OnHandleEditorMessage);
}
private void OnDisable()
{
PlayerConnection.instance.Unregister(RemoteDebuggerDefine.kMsgSendEditorToPlayer, OnHandleEditorMessage);
}
private void OnHandleConnectionEvent(int id)
{
YooLogger.Log($"Editor player connection : {id}");
}
private void OnHandleDisconnectionEvent(int id)
{
YooLogger.Log($"Editor player disconnection : {id}");
}
private void OnHandleEditorMessage(MessageEventArgs args)
{
var command = RemoteCommand.Deserialize(args.data);
YooLogger.Log($"On handle remote command : {command.CommandType} : {command.CommandParam}");
if (command.CommandType == (int)ERemoteCommand.SampleOnce)
{
var debugReport = AssetSystem.GetDebugReport();
var data = DebugReport.Serialize(debugReport);
PlayerConnection.instance.Send(RemoteDebuggerDefine.kMsgSendPlayerToEditor, data);
}
else
{
throw new NotImplementedException(command.CommandType.ToString());
}
}
#endif
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 91030c545c99c944a846f6a9a2a0756a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -117,7 +117,6 @@ namespace YooAsset
private static OfflinePlayModeImpl _offlinePlayModeImpl;
private static HostPlayModeImpl _hostPlayModeImpl;
/// <summary>
/// 异步初始化
/// </summary>
@ -143,6 +142,10 @@ namespace YooAsset
UnityEngine.GameObject driverGo = new UnityEngine.GameObject("[YooAsset]");
driverGo.AddComponent<YooAssetDriver>();
UnityEngine.Object.DontDestroyOnLoad(driverGo);
#if DEBUG
driverGo.AddComponent<RemoteDebuggerInRuntime>();
#endif
}
else
{
@ -934,17 +937,6 @@ namespace YooAsset
AssetSystem.Update();
}
/// <summary>
/// 获取调试信息
/// </summary>
internal static void GetDebugReport(DebugReport report)
{
if (report == null)
YooLogger.Error($"{nameof(DebugReport)} is null");
AssetSystem.GetDebugReport(report);
}
/// <summary>
/// 资源定位地址转换为资源完整路径
/// </summary>