Fix Linux issues with the scripting system

This fixes bugs in the scripting system in which scripts does not compile and run on Linux.

This commit also changes wording around the logging in the scripting system to avoid confusion between script errors and regular script compilation.
This commit is contained in:
breadbyte 2022-10-14 02:31:31 +08:00
parent dee085686f
commit 023cc2e2d4
No known key found for this signature in database
GPG key ID: 81897669486FFAFC
2 changed files with 19 additions and 19 deletions

View file

@ -24,7 +24,7 @@ namespace DynamicRun.Builder
GC.WaitForPendingFinalizers();
}
ConsoleIO.WriteLogLine(assemblyLoadContextWeakRef.Item1.IsAlive ? "Script failed to clean-up" : "Script finished!");
ConsoleIO.WriteLogLine(assemblyLoadContextWeakRef.Item1.IsAlive ? "Script continues to run." : "Script finished!");
return assemblyLoadContextWeakRef.Item2;
}

View file

@ -10,6 +10,7 @@ using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
@ -22,7 +23,7 @@ namespace DynamicRun.Builder
{
public CompileResult Compile(string filepath, string fileName)
{
ConsoleIO.WriteLogLine($"Starting compilation of: '{fileName}'");
ConsoleIO.WriteLogLine($"Starting compilation...");
using var peStream = new MemoryStream();
var result = GenerateCode(filepath, fileName).Emit(peStream);
@ -62,26 +63,21 @@ namespace DynamicRun.Builder
var mods = Assembly.GetEntryAssembly()!.GetModules();
#pragma warning disable IL3000
// System.Private.CoreLib
var A = typeof(object).Assembly.Location;
// System.Console
var B = typeof(Console).Assembly.Location;
// The path to MinecraftClient.dll
var C = typeof(Program).Assembly.Location;
var references = new List<MetadataReference>
{
MetadataReference.CreateFromFile(A),
MetadataReference.CreateFromFile(B)
};
#pragma warning disable IL3000 // We determine if we are in a self-contained binary by checking specifically if the Assembly file path is null.
// We're on a Single File Application, so we need to extract the executable to get the assembly.
if (string.IsNullOrEmpty(C))
var SystemPrivateCoreLib = typeof(object).Assembly.Location; // System.Private.CoreLib
var SystemConsole = typeof(Console).Assembly.Location; // System.Console
var MinecraftClientDll = typeof(Program).Assembly.Location; // The path to MinecraftClient.dll
var references = new List<MetadataReference>();
// We're on a self-contained binary, so we need to extract the executable to get the assemblies.
if (string.IsNullOrEmpty(MinecraftClientDll))
{
// Create a temporary file to copy the executable to.
var executableDir = System.AppContext.BaseDirectory;
var executablePath = Path.Combine(executableDir, "MinecraftClient.exe");
var executableDir = AppContext.BaseDirectory;
var executablePath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Path.Combine(executableDir, "MinecraftClient.exe") : Path.Combine(executableDir, "MinecraftClient");
var tempFileName = Path.GetTempFileName();
if (File.Exists(executablePath))
{
@ -99,6 +95,7 @@ namespace DynamicRun.Builder
var assemblyrefs = Assembly.GetEntryAssembly()?.GetReferencedAssemblies().ToList()!;
assemblyrefs.Add(new("MinecraftClient"));
assemblyrefs.Add(new("System.Private.CoreLib"));
foreach (var refs in assemblyrefs)
{
@ -133,10 +130,13 @@ namespace DynamicRun.Builder
}
else
{
references.Add(MetadataReference.CreateFromFile(C));
references.Add(MetadataReference.CreateFromFile(SystemPrivateCoreLib));
references.Add(MetadataReference.CreateFromFile(SystemConsole));
references.Add(MetadataReference.CreateFromFile(MinecraftClientDll));
Assembly.GetEntryAssembly()?.GetReferencedAssemblies().ToList().ForEach(a => references.Add(MetadataReference.CreateFromFile(Assembly.Load(a).Location)));
}
#pragma warning restore IL3000
return CSharpCompilation.Create($"{fileName}.dll",
new[] { parsedSyntaxTree },
references: references,