Some initial CSharp scripting functionality

This commit is contained in:
Oiltanker
2022-04-09 20:47:55 +03:00
parent 86a88c6e20
commit 429557ad7d
12 changed files with 466 additions and 1 deletions
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
namespace Barotrauma
{
public abstract class ANetMod : IDisposable
{
private static List<ANetMod> mods = new List<ANetMod>();
public static List<ANetMod> LoadedMods
{
get => mods;
}
public ANetMod()
{
LoadedMods.Add(this);
}
public abstract void Dispose();
// TODO: some hooks
}
}
@@ -0,0 +1,96 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Scripting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
partial class NetScript
{
public class NetScriptFilter
{
private const bool useWhitelist = false;
private static string[] classesPermited = new string[] {};
private static string[] classesProhibited = new string[] { };
public static bool IsClassAllowed(string usingName)
{
if (useWhitelist && !classesPermited.Any(u => u.Equals(usingName))) return false;
if (classesProhibited.Any(u => u.Equals(usingName))) return false;
return true;
}
public static string FilterSyntaxTree(CSharpSyntaxTree tree)
{
if (tree == null) throw new ArgumentNullException("Syntax tree must not be null.");
{ // Disallow top-level statements
var nodeCheck = tree.GetRoot().DescendantNodes();
var tlStatements = nodeCheck.Where(n => n is GlobalStatementSyntax).ToList();
if (tlStatements.Count > 0)
{
string errStr = "Cmopilation Error:";
foreach (var tls in tlStatements) tls.GetDiagnostics().ToList().ForEach(d => errStr += "\n" + d.ToString());
return errStr;
}
}
var compRoot = tree.GetCompilationUnitRoot();
var refDirs = compRoot.GetReferenceDirectives().ToList();
Console.WriteLine($"Reference Directives [{refDirs.Count}]:");
refDirs.ForEach(d => Console.WriteLine(d.ToFullString()));
List<string> allUsedTypes = new List<string>();
{ // Find all used types
}
List<string> allResolvedTypes = new List<string>();
{ // Resolve all types
}
{ // Check all used types
}
if (!Directory.Exists("./SyntaxTrees")) Directory.CreateDirectory("./SyntaxTrees");
string fileName = "./SyntaxTrees/" + tree.FilePath.Replace("/", "--") + ".txt";
if (File.Exists(fileName)) File.Delete(fileName);
var fileWriter = File.CreateText(fileName);
var nodes = new Queue<(SyntaxNode, int)>();
nodes.Enqueue((tree.GetRoot(), 0));
while (nodes.Count > 0)
{
var nodeElem = nodes.Dequeue();
var node = nodeElem.Item1;
var indent = nodeElem.Item2;
node.ChildNodes().ToList().ForEach(n => {
if (n.ChildNodes().Count() > 0) nodes.Enqueue((n, indent + 1));
if (!(
n is MemberAccessExpressionSyntax ||
n is UsingDirectiveSyntax ||
n is BaseTypeSyntax ||
n is TypeSyntax
)) return;
//Console.WriteLine(new String(' ', indent * 2) + n.GetType().Name + " | " + n.GetText()?.ToString() ?? "null");
fileWriter.WriteLine(new String(' ', indent * 2) + n.GetType().Name + " | " + n.GetText()?.ToString() ?? "null");
});
node.DescendantNodes().ToList().ForEach(n => {
if (n.DescendantNodes().Count() > 0 && !nodes.Contains((n, indent + 1))) nodes.Enqueue((n, indent + 1));
if (!(
n is MemberAccessExpressionSyntax ||
n is UsingDirectiveSyntax ||
n is BaseTypeSyntax ||
n is TypeSyntax
)) return;
//Console.WriteLine(new String(' ', indent * 2) + n.GetType().Name + " | " + n.GetText()?.ToString() ?? "null");
fileWriter.WriteLine(new String(' ', indent * 2) + n.GetType().Name + " | " + n.GetText()?.ToString() ?? "null");
});
}
fileWriter.Close();
return null;
}
}
}
@@ -0,0 +1,175 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.CodeAnalysis.Scripting;
using System.Reflection;
using Microsoft.CodeAnalysis.CSharp;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis;
using System.Collections.Immutable;
using System.Runtime.Loader;
using static NetScript;
using Microsoft.CodeAnalysis.Emit;
using System.Reflection.PortableExecutable;
using System.Reflection.Metadata;
namespace Barotrauma
{
partial class NetSetup
{
public class NetScriptLoader : AssemblyLoadContext
{
public NetSetup net;
private List<MetadataReference> defaultReferences;
private List<SyntaxTree> syntaxTrees;
public Assembly Assembly { get; private set; }
public NetScriptLoader(NetSetup net)
{
this.net = net;
defaultReferences = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => !(a.IsDynamic || string.IsNullOrEmpty(a.Location) || a.Location.Contains("xunit")))
.Select(a => MetadataReference.CreateFromFile(a.Location) as MetadataReference)
.ToList();
syntaxTrees = new List<SyntaxTree>();
Assembly = null;
}
public void SearchFolders()
{
foreach(ContentPackage cp in GameMain.Config.AllEnabledPackages)
{
var path = Path.GetDirectoryName(cp.Path);
RunFolder(path);
}
}
public void RunFolder(string folder)
{
var scriptFiles = new List<string>();
foreach (var str in DirSearch(folder))
{
var s = str.Replace("\\", "/");
if (s.EndsWith(".cs"))
{
NetSetup.PrintMessage(s);
scriptFiles.Add(s);
}
}
try
{
if (scriptFiles.Count <= 0) return;
var mainFile = scriptFiles.Find(s => s.EndsWith("Main.cs"));
if (mainFile == null) throw new Exception("Mod folder has no Main.cs file");
scriptFiles.Remove(mainFile);
scriptFiles.Add(mainFile);
// Check file content for prohibited stuff
foreach (var file in scriptFiles)
{
var tree = SyntaxFactory.ParseSyntaxTree(File.ReadAllText(file), CSharpParseOptions.Default, file);
var error = NetScriptFilter.FilterSyntaxTree(tree as CSharpSyntaxTree);
if (error != null) throw new Exception(error);
syntaxTrees.Add(tree);
}
}
catch (CompilationErrorException ex)
{
string errStr = "Cmopilation Error in '" + folder + "':";
foreach (var diag in ex.Diagnostics)
{
errStr += "\n" + diag.ToString();
}
NetSetup.PrintMessage(errStr);
}
catch (Exception ex)
{
NetSetup.PrintMessage("Error loading '" + folder + "':\n" + ex.Message + "\n" + ex.StackTrace);
}
}
public List<Type> Compile()
{
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
.WithMetadataImportOptions(MetadataImportOptions.All)
.WithOptimizationLevel(OptimizationLevel.Release)
.WithAllowUnsafe(false);
var compilation = CSharpCompilation.Create("NetScriptAssembly",syntaxTrees, defaultReferences, options);
using (var mem = new MemoryStream())
{
var result = compilation.Emit(mem);
if (!result.Success)
{
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(d => d.IsWarningAsError || d.Severity == DiagnosticSeverity.Error);
string errStr = "NET MODS NOT LOADED | Mod cmopilation errors:";
foreach (Diagnostic diagnostic in failures)
{
errStr = $"\n{diagnostic}";
}
NetSetup.PrintMessage(errStr);
}
else
{
mem.Seek(0, SeekOrigin.Begin);
var reader = new PEReader(mem);
var mdReader = reader.GetMetadataReader();
mdReader.AssemblyReferences.ToList().ForEach(a =>
{
var aRef = mdReader.GetAssemblyReference(a);
Console.WriteLine(aRef.GetAssemblyName() + " " + aRef.Version);
});
Console.WriteLine();
mdReader.TypeReferences.ToList().ForEach(t =>
{
var tRef = mdReader.GetTypeReference(t);
Console.WriteLine(mdReader.GetString(tRef.Namespace) + " - " + mdReader.GetString(tRef.Name));
});
mem.Seek(0, SeekOrigin.Begin);
Assembly = LoadFromStream(mem);
}
}
syntaxTrees.Clear();
if (Assembly != null)
return Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ANetMod))).ToList();
else
throw new Exception("Unable to create net mods assembly.");
}
static string[] DirSearch(string sDir)
{
List<string> files = new List<string>();
try
{
foreach (string f in Directory.GetFiles(sDir))
{
files.Add(f);
}
foreach (string d in Directory.GetDirectories(sDir))
{
files.AddRange(DirSearch(d));
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
return files.ToArray();
}
}
}
}
@@ -0,0 +1,134 @@
using System;
using Barotrauma.Networking;
using Microsoft.Xna.Framework;
using HarmonyLib;
using System.Runtime.CompilerServices;
//using System.Linq;
//using System.Collections.Generic;
//using Microsoft.CodeAnalysis;
//using Microsoft.CodeAnalysis.CSharp;
[assembly: InternalsVisibleTo("NetScriptAssembly", AllInternalsVisible = true)]
namespace Barotrauma
{
partial class NetSetup
{
public NetScriptLoader Loader { get; private set; }
public Harmony harmony;
public LuaHook hook;
public NetSetup() => Initialize();
public void Reload()
{
Stop();
Initialize();
Execute();
}
public void Initialize()
{
hook = new LuaHook();
Loader = new NetScriptLoader(this);
Loader.SearchFolders();
//var baseType = typeof(CSharpSyntaxNode);
//var typeDict = new Dictionary<Type, object>() { { baseType, new Dictionary<Type, object>() } };
//var sytaxTypes = AppDomain.CurrentDomain
// .GetAssemblies()
// .Where(a => !(a.IsDynamic || string.IsNullOrEmpty(a.Location) || a.Location.Contains("xunit")))
// .Select(a => a.GetTypes().Where(t => t.IsSubclassOf(baseType)))
// .Aggregate((t1, t2) => t1.Concat(t2))
// .ToList();
//var q = new Queue<(Type, Dictionary<Type, object>)>();
//q.Enqueue((baseType, typeDict[baseType] as Dictionary<Type, object>));
//while (q.Count > 0)
//{
// var entry = q.Dequeue();
// var type = entry.Item1;
// var dict = entry.Item2;
// sytaxTypes.Where(t => t.BaseType == type).ToList().ForEach(t =>
// {
// var newDict = new Dictionary<Type, object>();
// dict.Add(t, newDict);
// q.Enqueue((t, newDict));
// });
//}
//Action<int, KeyValuePair<Type, object>> rprint = null;
//rprint = (indent, kv) =>
//{
// string idnt = "";
// for (int i = 0; i < indent - 1; i++) idnt += "│ ";
// if (indent > 0) idnt += "├─";
// Console.WriteLine(idnt + kv.Key.Name);
// foreach (var _kv in kv.Value as Dictionary<Type, object>)
// rprint(indent + 1, _kv);
//};
//foreach (var kv in typeDict) rprint(0, kv);
}
public void Execute()
{
if (Loader == null) throw new Exception("NetSetup was not properly initialized.");
try
{
var modTypes = Loader.Compile();
modTypes.ForEach(t => t.GetConstructor(new Type[] { }).Invoke(null));
}
catch (Exception ex)
{
PrintMessage(ex);
}
}
public void Stop()
{
harmony?.UnpatchAll();
hook?.Call("stop", new object[] { });
hook = null;
Loader = null;
ANetMod.LoadedMods.ForEach(m => m.Dispose());
ANetMod.LoadedMods.Clear();
Loader.Unload();
}
public void Update()
{
hook?.Update();
}
private static bool wasPublicized = false;
public static void PrintMessage(object message)
{
if (message == null) { message = "null"; }
string str = message.ToString();
#if SERVER
if (GameMain.Server != null)
{
for (int i = 0; i < str.Length; i += 1024)
{
string subStr = str.Substring(i, Math.Min(1024, str.Length - i));
foreach (var c in GameMain.Server.ConnectedClients)
{
GameMain.Server.SendDirectChatMessage(ChatMessage.Create("", subStr, ChatMessageType.Console, null, textColor: Color.MediumPurple), c);
}
GameServer.Log("[NET] " + subStr, ServerLog.MessageType.ServerMessage);
}
}
else
{
DebugConsole.NewMessage("[NET]" + message.ToString(), Color.MediumPurple);
}
#else
DebugConsole.NewMessage(message.ToString(), Color.Purple);
#endif
}
}
}