mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
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:
parent
dee085686f
commit
023cc2e2d4
2 changed files with 19 additions and 19 deletions
|
|
@ -24,7 +24,7 @@ namespace DynamicRun.Builder
|
||||||
GC.WaitForPendingFinalizers();
|
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;
|
return assemblyLoadContextWeakRef.Item2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ using System.IO;
|
||||||
using System.IO.MemoryMappedFiles;
|
using System.IO.MemoryMappedFiles;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.CodeAnalysis.CSharp;
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
using Microsoft.CodeAnalysis.Text;
|
using Microsoft.CodeAnalysis.Text;
|
||||||
|
|
@ -22,7 +23,7 @@ namespace DynamicRun.Builder
|
||||||
{
|
{
|
||||||
public CompileResult Compile(string filepath, string fileName)
|
public CompileResult Compile(string filepath, string fileName)
|
||||||
{
|
{
|
||||||
ConsoleIO.WriteLogLine($"Starting compilation of: '{fileName}'");
|
ConsoleIO.WriteLogLine($"Starting compilation...");
|
||||||
|
|
||||||
using var peStream = new MemoryStream();
|
using var peStream = new MemoryStream();
|
||||||
var result = GenerateCode(filepath, fileName).Emit(peStream);
|
var result = GenerateCode(filepath, fileName).Emit(peStream);
|
||||||
|
|
@ -62,26 +63,21 @@ namespace DynamicRun.Builder
|
||||||
|
|
||||||
var mods = Assembly.GetEntryAssembly()!.GetModules();
|
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>
|
#pragma warning disable IL3000 // We determine if we are in a self-contained binary by checking specifically if the Assembly file path is null.
|
||||||
{
|
|
||||||
MetadataReference.CreateFromFile(A),
|
|
||||||
MetadataReference.CreateFromFile(B)
|
|
||||||
};
|
|
||||||
|
|
||||||
// We're on a Single File Application, so we need to extract the executable to get the assembly.
|
var SystemPrivateCoreLib = typeof(object).Assembly.Location; // System.Private.CoreLib
|
||||||
if (string.IsNullOrEmpty(C))
|
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.
|
// Create a temporary file to copy the executable to.
|
||||||
var executableDir = System.AppContext.BaseDirectory;
|
var executableDir = AppContext.BaseDirectory;
|
||||||
var executablePath = Path.Combine(executableDir, "MinecraftClient.exe");
|
var executablePath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? Path.Combine(executableDir, "MinecraftClient.exe") : Path.Combine(executableDir, "MinecraftClient");
|
||||||
var tempFileName = Path.GetTempFileName();
|
var tempFileName = Path.GetTempFileName();
|
||||||
if (File.Exists(executablePath))
|
if (File.Exists(executablePath))
|
||||||
{
|
{
|
||||||
|
|
@ -99,6 +95,7 @@ namespace DynamicRun.Builder
|
||||||
|
|
||||||
var assemblyrefs = Assembly.GetEntryAssembly()?.GetReferencedAssemblies().ToList()!;
|
var assemblyrefs = Assembly.GetEntryAssembly()?.GetReferencedAssemblies().ToList()!;
|
||||||
assemblyrefs.Add(new("MinecraftClient"));
|
assemblyrefs.Add(new("MinecraftClient"));
|
||||||
|
assemblyrefs.Add(new("System.Private.CoreLib"));
|
||||||
|
|
||||||
foreach (var refs in assemblyrefs)
|
foreach (var refs in assemblyrefs)
|
||||||
{
|
{
|
||||||
|
|
@ -133,10 +130,13 @@ namespace DynamicRun.Builder
|
||||||
}
|
}
|
||||||
else
|
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)));
|
Assembly.GetEntryAssembly()?.GetReferencedAssemblies().ToList().ForEach(a => references.Add(MetadataReference.CreateFromFile(Assembly.Load(a).Location)));
|
||||||
}
|
}
|
||||||
#pragma warning restore IL3000
|
#pragma warning restore IL3000
|
||||||
|
|
||||||
return CSharpCompilation.Create($"{fileName}.dll",
|
return CSharpCompilation.Create($"{fileName}.dll",
|
||||||
new[] { parsedSyntaxTree },
|
new[] { parsedSyntaxTree },
|
||||||
references: references,
|
references: references,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue