mirror of
https://github.com/MCCTeam/Minecraft-Console-Client
synced 2025-10-14 21:22:49 +00:00
.NET 5+ Support (#1674)
Implement changes to support .NET 5 onwards. Co-authored-by: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com> Co-authored-by: ORelio <ORelio@users.noreply.github.com>
This commit is contained in:
parent
b3cc2351ee
commit
d9f1a77ac2
117 changed files with 1028 additions and 9058 deletions
|
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
MIT License
|
||||
Copyright (c) 2019 Laurent Kempé
|
||||
https://github.com/laurentkempe/DynamicRun/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using MinecraftClient;
|
||||
|
||||
namespace DynamicRun.Builder
|
||||
{
|
||||
internal class CompileRunner
|
||||
{
|
||||
public object? Execute(byte[] compiledAssembly, string[] args, Dictionary<string, object> localVars, ChatBot apiHandler)
|
||||
{
|
||||
var assemblyLoadContextWeakRef = LoadAndExecute(compiledAssembly, args, localVars, apiHandler);
|
||||
|
||||
for (var i = 0; i < 8 && assemblyLoadContextWeakRef.Item1.IsAlive; i++)
|
||||
{
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
|
||||
ConsoleIO.WriteLogLine(assemblyLoadContextWeakRef.Item1.IsAlive ? "Script failed to clean-up" : "Script finished!");
|
||||
return assemblyLoadContextWeakRef.Item2;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private static Tuple<WeakReference, object?> LoadAndExecute(byte[] compiledAssembly, string[] args, Dictionary<string, object> localVars, ChatBot apiHandler)
|
||||
{
|
||||
using (var asm = new MemoryStream(compiledAssembly))
|
||||
{
|
||||
var assemblyLoadContext = new SimpleUnloadableAssemblyLoadContext();
|
||||
|
||||
var assembly = assemblyLoadContext.LoadFromStream(asm);
|
||||
var compiledScript = assembly.CreateInstance("ScriptLoader.Script");
|
||||
var execResult = compiledScript.GetType().GetMethod("__run").Invoke(compiledScript, new object[] { new CSharpAPI(apiHandler, localVars), args });
|
||||
|
||||
assemblyLoadContext.Unload();
|
||||
|
||||
return new (new WeakReference(assemblyLoadContext), execResult);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
90
MinecraftClient/Scripting/DynamicRun/Builder/Compiler.cs
Normal file
90
MinecraftClient/Scripting/DynamicRun/Builder/Compiler.cs
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/*
|
||||
MIT License
|
||||
Copyright (c) 2019 Laurent Kempé
|
||||
https://github.com/laurentkempe/DynamicRun/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.Text;
|
||||
using MinecraftClient;
|
||||
|
||||
namespace DynamicRun.Builder
|
||||
{
|
||||
internal class Compiler
|
||||
{
|
||||
public CompileResult Compile(string filepath, string fileName)
|
||||
{
|
||||
ConsoleIO.WriteLogLine($"Starting compilation of: '{fileName}'");
|
||||
|
||||
using (var peStream = new MemoryStream())
|
||||
{
|
||||
var result = GenerateCode(filepath, fileName).Emit(peStream);
|
||||
|
||||
if (!result.Success)
|
||||
{
|
||||
ConsoleIO.WriteLogLine("Compilation done with error.");
|
||||
|
||||
var failures = result.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error);
|
||||
|
||||
return new CompileResult() {
|
||||
Assembly = null,
|
||||
HasCompiledSucecssfully = false,
|
||||
Failures = failures.ToList()
|
||||
};
|
||||
}
|
||||
|
||||
ConsoleIO.WriteLogLine("Compilation done without any error.");
|
||||
|
||||
peStream.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
return new CompileResult() {
|
||||
Assembly = peStream.ToArray(),
|
||||
HasCompiledSucecssfully = true,
|
||||
Failures = null
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private static CSharpCompilation GenerateCode(string sourceCode, string fileName)
|
||||
{
|
||||
var codeString = SourceText.From(sourceCode);
|
||||
var options = CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.CSharp9);
|
||||
|
||||
var parsedSyntaxTree = SyntaxFactory.ParseSyntaxTree(codeString, options);
|
||||
|
||||
var references = new List<MetadataReference>
|
||||
{
|
||||
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(Console).Assembly.Location),
|
||||
MetadataReference.CreateFromFile(typeof(ChatBot).Assembly.Location)
|
||||
};
|
||||
|
||||
Assembly.GetEntryAssembly()?.GetReferencedAssemblies().ToList()
|
||||
.ForEach(a => references.Add(MetadataReference.CreateFromFile(Assembly.Load(a).Location)));
|
||||
|
||||
return CSharpCompilation.Create($"{fileName}.dll",
|
||||
new[] { parsedSyntaxTree },
|
||||
references: references,
|
||||
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
|
||||
optimizationLevel: OptimizationLevel.Release,
|
||||
assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default));
|
||||
}
|
||||
|
||||
internal struct CompileResult {
|
||||
internal byte[]? Assembly;
|
||||
internal bool HasCompiledSucecssfully;
|
||||
internal List<Diagnostic>? Failures;
|
||||
public CompileResult(bool hasCompiledSucecssfully, List<Diagnostic>? failures, byte[]? assembly) {
|
||||
HasCompiledSucecssfully = hasCompiledSucecssfully;
|
||||
Failures = failures;
|
||||
Assembly = assembly;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
MIT License
|
||||
Copyright (c) 2019 Laurent Kempé
|
||||
https://github.com/laurentkempe/DynamicRun/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
|
||||
namespace DynamicRun.Builder
|
||||
{
|
||||
internal class SimpleUnloadableAssemblyLoadContext : AssemblyLoadContext
|
||||
{
|
||||
public SimpleUnloadableAssemblyLoadContext()
|
||||
: base(true)
|
||||
{
|
||||
}
|
||||
|
||||
protected override Assembly Load(AssemblyName assemblyName)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue