Added verbose cs compilation.

This commit is contained in:
Maplewheels
2026-03-29 00:30:18 -04:00
parent bcec409618
commit f1808d9231
3 changed files with 26 additions and 15 deletions
@@ -9,6 +9,7 @@ using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Loader; using System.Runtime.Loader;
using System.Text;
using System.Threading; using System.Threading;
using Barotrauma.Extensions; using Barotrauma.Extensions;
using Barotrauma.LuaCs; using Barotrauma.LuaCs;
@@ -255,8 +256,7 @@ public sealed class AssemblyLoader : AssemblyLoadContext, IAssemblyLoaderService
} }
} }
public FluentResults.Result<Assembly> CompileScriptAssembly( public Result<Assembly> CompileScriptAssembly([NotNull] string assemblyName,
[NotNull] string assemblyName,
bool compileWithInternalAccess, bool compileWithInternalAccess,
ImmutableArray<SyntaxTree> syntaxTrees, ImmutableArray<SyntaxTree> syntaxTrees,
ImmutableArray<MetadataReference> metadataReferences, ImmutableArray<MetadataReference> metadataReferences,
@@ -290,7 +290,8 @@ public sealed class AssemblyLoader : AssemblyLoadContext, IAssemblyLoaderService
outputKind: OutputKind.DynamicallyLinkedLibrary, outputKind: OutputKind.DynamicallyLinkedLibrary,
optimizationLevel: OptimizationLevel.Release, optimizationLevel: OptimizationLevel.Release,
concurrentBuild: true, concurrentBuild: true,
reportSuppressedDiagnostics: true, reportSuppressedDiagnostics: false,
warningLevel: 0,
allowUnsafe: true); allowUnsafe: true);
if (!compileWithInternalAccess) if (!compileWithInternalAccess)
@@ -306,12 +307,22 @@ public sealed class AssemblyLoader : AssemblyLoadContext, IAssemblyLoaderService
using var asmMemoryStream = new MemoryStream(); using var asmMemoryStream = new MemoryStream();
var result = CSharpCompilation var result = CSharpCompilation
.Create(compilationAssemblyName, syntaxTrees, metadataReferences, compilationOptions) .Create(compilationAssemblyName, syntaxTrees,
metadataReferences, compilationOptions)
.Emit(asmMemoryStream); .Emit(asmMemoryStream);
if (!result.Success) if (!result.Success)
{ {
StringBuilder sb = new StringBuilder();
foreach (var resultDiagnostic in result.Diagnostics)
{
if (resultDiagnostic.Severity != DiagnosticSeverity.Error)
{
continue;
}
sb.AppendLine($">>> {resultDiagnostic.GetMessage()} | Location: {resultDiagnostic.Location.SourceTree?.GetLineSpan(resultDiagnostic.Location.SourceSpan)} ");
}
var res = new FluentResults.Result().WithError( var res = new FluentResults.Result().WithError(
new Error($"Compilation failed for assembly {assemblyName}!") new Error($"Package Error: {OwnerPackage.Name}: Compilation failed for assembly {assemblyName}! {sb.ToString()}\n")
.WithMetadata(MetadataType.ExceptionObject, this) .WithMetadata(MetadataType.ExceptionObject, this)
.WithMetadata(MetadataType.RootObject, syntaxTrees)); .WithMetadata(MetadataType.RootObject, syntaxTrees));
var failuresDiag = var failuresDiag =
@@ -6,6 +6,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using Barotrauma.LuaCs; using Barotrauma.LuaCs;
using FluentResults;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp;
@@ -74,20 +75,19 @@ public interface IAssemblyLoaderService : IService
/// AssemblyManager class. /// AssemblyManager class.
/// </summary> /// </summary>
/// <param name="assemblyName"><c>[NotNull]</c>Name reference of the assembly. /// <param name="assemblyName"><c>[NotNull]</c>Name reference of the assembly.
/// <para><b>[IMPORTANT]</b> This is used to reference this assembly as the true name will be forced if /// <para><b>[IMPORTANT]</b> This is used to reference this assembly as the true name will be forced if
/// publicized assemblies are not used (InternalsVisibleTo Attrib).</para> /// publicized assemblies are not used (InternalsVisibleTo Attrib).</para>
/// Must be supplied for in-memory assemblies. /// Must be supplied for in-memory assemblies.
/// <para>Must be unique to all other assemblies explicitly loaded using this context.</para></param> /// <para>Must be unique to all other assemblies explicitly loaded using this context.</para></param>
/// <param name="compileWithInternalAccess">Forces the assembly name to <see cref="InternalsAccessAssemblyName"/> and grants access to <c>internal</c>.</param> /// <param name="compileWithInternalAccess">Forces the assembly name to <see cref="InternalsAccessAssemblyName"/> and grants access to <c>internal</c>.</param>
/// <para><b>[IMPORTANT]</b>Cannot be null or empty if <see cref="compileWithInternalAccess"/> is false.</para></param>
/// <param name="syntaxTrees"><c>[NotNull]</c>Syntax trees to compile into the assembly.</param> /// <param name="syntaxTrees"><c>[NotNull]</c>Syntax trees to compile into the assembly.</param>
/// <param name="metadataReferences">All <c>MetadataReference<c/>s to be used for compilation. /// <param name="metadataReferences">All <c>MetadataReference<c/>s to be used for compilation.
/// [IMPORTANT] This method builds metadata from loaded assemblies, only supply your own if you have in-memory /// [IMPORTANT] This method builds metadata from loaded assemblies, only supply your own if you have in-memory
/// images not managed by the AssemblyManager class.</param> /// images not managed by the AssemblyManager class.</param>
/// <param name="compilationOptions"><c>[NotNull]</c>CSharp compilation options. This method automatically adds the 'IgnoreAccessChecks' property for compilation.</param> /// <param name="compilationOptions"><c>[NotNull]</c>CSharp compilation options. This method automatically adds the 'IgnoreAccessChecks' property for compilation.</param>
/// <para><b>[IMPORTANT]</b>Cannot be null or empty if <see cref="compileWithInternalAccess"/> is false.</para></param>
/// <returns>Success state of the operation.</returns> /// <returns>Success state of the operation.</returns>
public FluentResults.Result<Assembly> CompileScriptAssembly( public Result<Assembly> CompileScriptAssembly([NotNull] string assemblyName,
[NotNull] string assemblyName,
bool compileWithInternalAccess, bool compileWithInternalAccess,
ImmutableArray<SyntaxTree> syntaxTrees, ImmutableArray<SyntaxTree> syntaxTrees,
ImmutableArray<MetadataReference> metadataReferences, ImmutableArray<MetadataReference> metadataReferences,
@@ -588,7 +588,7 @@ public class PluginManagementService : IAssemblyManagementService
syntaxTreesBuilder.Add(SyntaxFactory.ParseSyntaxTree( syntaxTreesBuilder.Add(SyntaxFactory.ParseSyntaxTree(
text: sourceCode, text: sourceCode,
options: ScriptParseOptions, options: ScriptParseOptions,
path: null, path: resourcePath.FullPath,
encoding: Encoding.Default, encoding: Encoding.Default,
cancellationToken: token cancellationToken: token
)); ));