Skip to content

Commit a48c4e7

Browse files
committed
refactor subscription logic to leverage existing hooks, code cleanup
1 parent f7f955f commit a48c4e7

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

Unity-MCP-Plugin/Assets/root/Editor/Scripts/Startup.Editor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,28 @@ static void OnApplicationUnloading()
3333
if (UnityMcpPlugin.IsLogActive(LogLevel.Debug))
3434
Debug.Log($"{DebugName} OnApplicationUnloading triggered");
3535
Disconnect();
36+
LogUtils.SaveToFile();
3637
}
3738
static void OnApplicationQuitting()
3839
{
3940
if (UnityMcpPlugin.IsLogActive(LogLevel.Debug))
4041
Debug.Log($"{DebugName} OnApplicationQuitting triggered");
4142
Disconnect();
43+
LogUtils.SaveToFile();
4244
}
4345
static void OnBeforeAssemblyReload()
4446
{
4547
if (UnityMcpPlugin.IsLogActive(LogLevel.Debug))
4648
Debug.Log($"{DebugName} OnBeforeAssemblyReload triggered");
4749
Disconnect();
50+
LogUtils.SaveToFile();
4851
}
4952
static void OnAfterAssemblyReload()
5053
{
5154
if (UnityMcpPlugin.IsLogActive(LogLevel.Debug))
5255
Debug.Log($"{DebugName} OnAfterReload triggered - BuildAndStart with openConnection: {!EnvironmentUtils.IsCi()}");
5356
UnityMcpPlugin.BuildAndStart(openConnectionIfNeeded: !EnvironmentUtils.IsCi());
57+
LogUtils.LoadFromFile();
5458
}
5559

5660
static void OnPlayModeStateChanged(PlayModeStateChange state)

Unity-MCP-Plugin/Assets/root/Runtime/Unity/Logs/LogCache.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77
│ See the LICENSE file in the project root for more information. │
88
└──────────────────────────────────────────────────────────────────┘
99
*/
10-
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
10+
#nullable enable
1111
using System;
1212
using System.Collections;
1313
using System.Collections.Concurrent;
1414
using System.IO;
15-
using System.Runtime.Serialization.Formatters.Binary;
1615
using System.Threading;
1716
using System.Threading.Tasks;
1817
using com.IvanMurzak.ReflectorNet.Utils;
19-
using Microsoft.CodeAnalysis.CSharp.Syntax;
20-
using Microsoft.CodeAnalysis.Diagnostics;
2118
using R3;
2219
using UnityEngine;
2320

@@ -40,18 +37,20 @@ public static class LogCache
4037
public static void Initialize()
4138
{
4239
if (_initialized) return;
40+
4341
var subscription = Observable.Timer(
4442
TimeSpan.FromSeconds(1),
4543
TimeSpan.FromSeconds(1)
4644
)
4745
.Subscribe(x =>
4846
{
49-
Task.Run(() => LogCache.HandleLogCache());
47+
Task.Run(HandleLogCache);
5048
});
49+
5150
_initialized = true;
5251
}
5352

54-
public static async void HandleLogCache()
53+
public static async Task HandleLogCache()
5554
{
5655
if (LogUtils.LogEntries > 0)
5756
{

Unity-MCP-Plugin/Assets/root/Runtime/Unity/Logs/LogUtils.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
│ See the LICENSE file in the project root for more information. │
88
└──────────────────────────────────────────────────────────────────┘
99
*/
10-
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
10+
#nullable enable
1111
using System.Collections.Concurrent;
12+
using System.Diagnostics.CodeAnalysis;
13+
using System.Threading;
14+
using System.Threading.Tasks;
1215
using com.IvanMurzak.ReflectorNet.Utils;
1316
using UnityEngine;
1417

@@ -40,6 +43,16 @@ public static void ClearLogs()
4043
}
4144
}
4245

46+
public static void SaveToFile()
47+
{
48+
Task.Run(async () => await LogCache.CacheLogEntriesAsync(_logEntries.ToArray()));
49+
}
50+
51+
public static void LoadFromFile()
52+
{
53+
Task.Run(async () => _logEntries = await LogCache.GetCachedLogEntriesAsync());
54+
}
55+
4356
public static LogEntry[] GetAllLogs()
4457
{
4558
lock (_lockObject)
@@ -55,7 +68,7 @@ static LogUtils()
5568

5669
public static void EnsureSubscribed()
5770
{
58-
MainThread.Instance.RunAsync(async () =>
71+
MainThread.Instance.RunAsync(() =>
5972
{
6073
lock (_lockObject)
6174
{
@@ -66,7 +79,6 @@ public static void EnsureSubscribed()
6679
_isSubscribed = true;
6780
}
6881
}
69-
_logEntries = await LogCache.GetCachedLogEntriesAsync();
7082
});
7183
}
7284

Unity-MCP-Plugin/Assets/root/Runtime/Unity/Logs/LogWrapper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
│ See the LICENSE file in the project root for more information. │
88
└──────────────────────────────────────────────────────────────────┘
99
*/
10-
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
10+
#nullable enable
1111

1212
namespace com.IvanMurzak.Unity.MCP
1313
{
1414
[System.Serializable]
1515
internal class LogWrapper
1616
{
17-
public LogEntry[] entries;
17+
public LogEntry[]? entries;
1818
}
1919
}

Unity-MCP-Plugin/Assets/root/Tests/Editor/Tool/Console/TestToolConsoleIntegration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ public IEnumerator GetLogs_Validate_ConsoleLogRetention()
270270
}
271271
LogUtils.ClearLogs();
272272
Assert.AreEqual(0, LogUtils.LogEntries, "Log entries and Log Cache count should be empty.");
273-
LogUtils.EnsureSubscribed();
273+
LogUtils.LoadFromFile();
274274
for (int i = 0; i < 10000; i++)
275275
{
276276
yield return null;

0 commit comments

Comments
 (0)