File tree Expand file tree Collapse file tree 5 files changed +27
-12
lines changed
Unity-MCP-Plugin/Assets/root
Tests/Editor/Tool/Console Expand file tree Collapse file tree 5 files changed +27
-12
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 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
1111using System ;
1212using System . Collections ;
1313using System . Collections . Concurrent ;
1414using System . IO ;
15- using System . Runtime . Serialization . Formatters . Binary ;
1615using System . Threading ;
1716using System . Threading . Tasks ;
1817using com . IvanMurzak . ReflectorNet . Utils ;
19- using Microsoft . CodeAnalysis . CSharp . Syntax ;
20- using Microsoft . CodeAnalysis . Diagnostics ;
2118using R3 ;
2219using 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 {
Original file line number Diff line number Diff line change 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
1111using System . Collections . Concurrent ;
12+ using System . Diagnostics . CodeAnalysis ;
13+ using System . Threading ;
14+ using System . Threading . Tasks ;
1215using com . IvanMurzak . ReflectorNet . Utils ;
1316using 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
Original file line number Diff line number Diff line change 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
1212namespace com . IvanMurzak . Unity . MCP
1313{
1414 [ System . Serializable ]
1515 internal class LogWrapper
1616 {
17- public LogEntry [ ] entries ;
17+ public LogEntry [ ] ? entries ;
1818 }
1919}
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments