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

View File

@@ -9,6 +9,7 @@ using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Text;
using System.Threading;
using Barotrauma.Extensions;
using Barotrauma.LuaCs;
@@ -255,8 +256,7 @@ public sealed class AssemblyLoader : AssemblyLoadContext, IAssemblyLoaderService
}
}
public FluentResults.Result<Assembly> CompileScriptAssembly(
[NotNull] string assemblyName,
public Result<Assembly> CompileScriptAssembly([NotNull] string assemblyName,
bool compileWithInternalAccess,
ImmutableArray<SyntaxTree> syntaxTrees,
ImmutableArray<MetadataReference> metadataReferences,
@@ -290,7 +290,8 @@ public sealed class AssemblyLoader : AssemblyLoadContext, IAssemblyLoaderService
outputKind: OutputKind.DynamicallyLinkedLibrary,
optimizationLevel: OptimizationLevel.Release,
concurrentBuild: true,
reportSuppressedDiagnostics: true,
reportSuppressedDiagnostics: false,
warningLevel: 0,
allowUnsafe: true);
if (!compileWithInternalAccess)
@@ -306,12 +307,22 @@ public sealed class AssemblyLoader : AssemblyLoadContext, IAssemblyLoaderService
using var asmMemoryStream = new MemoryStream();
var result = CSharpCompilation
.Create(compilationAssemblyName, syntaxTrees, metadataReferences, compilationOptions)
.Create(compilationAssemblyName, syntaxTrees,
metadataReferences, compilationOptions)
.Emit(asmMemoryStream);
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(
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.RootObject, syntaxTrees));
var failuresDiag =

View File

@@ -6,6 +6,7 @@ using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using Barotrauma.LuaCs;
using FluentResults;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@@ -74,20 +75,19 @@ public interface IAssemblyLoaderService : IService
/// AssemblyManager class.
/// </summary>
/// <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
/// publicized assemblies are not used (InternalsVisibleTo Attrib).</para>
/// Must be supplied for in-memory assemblies.
/// <para>Must be unique to all other assemblies explicitly loaded using this context.</para></param>
/// <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>
/// Must be supplied for in-memory assemblies.
/// <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>
/// <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="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
/// images not managed by the AssemblyManager class.</param>
/// [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>
/// <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>
public FluentResults.Result<Assembly> CompileScriptAssembly(
[NotNull] string assemblyName,
public Result<Assembly> CompileScriptAssembly([NotNull] string assemblyName,
bool compileWithInternalAccess,
ImmutableArray<SyntaxTree> syntaxTrees,
ImmutableArray<MetadataReference> metadataReferences,

View File

@@ -588,7 +588,7 @@ public class PluginManagementService : IAssemblyManagementService
syntaxTreesBuilder.Add(SyntaxFactory.ParseSyntaxTree(
text: sourceCode,
options: ScriptParseOptions,
path: null,
path: resourcePath.FullPath,
encoding: Encoding.Default,
cancellationToken: token
));