Files
sbox-public/engine/Sandbox.Compiling/Compiler/IncrementalCompileState.cs
Lorenz Junglas 236b95e0ed Disposable diagnostic (#3693)
* Enable CA2000 in editorconfig

* Remove unused CaptureStdOut class

* Add missing dispose calls in Sandbox.Tools

* Add missing dispose calls in tests

* Add missing dispose calls in launchers

* Add missing dispose calls in Topten.RichtTextKit

* Add missing dispose calls in Engine

* Add missing dispose calls in SboxBuild

* Add nullchecks to a few dispose calls

* Fix more missing disposal calls and leaks

* Disable CA2000 for Textures

* Fix disposing too early in ImageFileTextureGenerator

* Fix disposing codec, ownership is transferred to animation

* Add missing using in ByteStream benchmark
2026-01-12 21:59:15 +01:00

96 lines
2.5 KiB
C#

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.Collections.Immutable;
using System.IO;
namespace Sandbox;
/// <summary>
/// Holds state for incremental compilation. Ask Matt or James to add comments, I don't know.
/// </summary>
class IncrementalCompileState
{
public ImmutableArray<SyntaxTree> OldSyntaxTrees;
public ImmutableArray<SyntaxTree> SyntaxTrees;
public ImmutableArray<SyntaxTree> PreHotloadSyntaxTrees;
public CSharpCompilation Compilation;
public bool HasState => Compilation is not null;
internal void Reset()
{
Compilation = null;
OldSyntaxTrees = default;
SyntaxTrees = default;
PreHotloadSyntaxTrees = default;
}
internal void Update( ImmutableArray<SyntaxTree> syntaxTrees, ImmutableArray<SyntaxTree> beforeIlHotloadProcessingTrees, CSharpCompilation compiler )
{
OldSyntaxTrees = SyntaxTrees;
SyntaxTrees = syntaxTrees;
PreHotloadSyntaxTrees = beforeIlHotloadProcessingTrees;
Compilation = compiler;
}
public Dictionary<string, object> GetChangeSummary( IEnumerable<BaseFileSystem> fileLocations = default )
{
if ( OldSyntaxTrees.IsDefaultOrEmpty || SyntaxTrees.IsDefaultOrEmpty )
{
return new Dictionary<string, object>();
}
var filePaths = OldSyntaxTrees.Select( x => x.FilePath )
.Union( SyntaxTrees.Select( x => x.FilePath ) )
.OrderBy( x => x )
.ToArray();
var result = new Dictionary<string, object>();
using var writer = new StringWriter();
foreach ( var filePath in filePaths )
{
var localPath = fileLocations?.Select( x =>
{
try
{
return x.GetRelativePath( filePath );
}
catch
{
return null;
}
} ).FirstOrDefault( x => x != null ) ?? Path.GetFileName( filePath );
var oldTree = OldSyntaxTrees.FirstOrDefault( x => x.FilePath.Equals( filePath, StringComparison.OrdinalIgnoreCase ) );
var newTree = SyntaxTrees.FirstOrDefault( x => x.FilePath.Equals( filePath, StringComparison.OrdinalIgnoreCase ) );
if ( oldTree == null && newTree != null )
{
writer.WriteLine( $"{localPath}: Added" );
continue;
}
if ( oldTree != null && newTree == null )
{
writer.WriteLine( $"{localPath}: Removed" );
continue;
}
var changes = Generator.ILHotloadProcessor.GetChanges( oldTree, newTree );
if ( string.IsNullOrEmpty( changes ) )
{
continue;
}
writer.WriteLine( $"{localPath}: Modified" );
writer.WriteLine( $" {changes.Replace( "\n", "\n " )}" );
}
result["Details"] = writer.ToString();
return result;
}
}