Fix mixed spaces and tabs
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
@@ -14,40 +14,40 @@ using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class CsScriptBase : AssemblyLoadContext
|
||||
{
|
||||
class CsScriptBase : AssemblyLoadContext
|
||||
{
|
||||
|
||||
public const string CsOneTimeScriptAssembly = "NetOneTimeScriptAssembly";
|
||||
public const string CsScriptAssembly = "NetScriptAssembly";
|
||||
public const string CsOneTimeScriptAssembly = "NetOneTimeScriptAssembly";
|
||||
public const string CsScriptAssembly = "NetScriptAssembly";
|
||||
|
||||
public static readonly string[] LoadedAssemblyName = {
|
||||
CsScriptBase.CsScriptAssembly,
|
||||
CsScriptBase.CsOneTimeScriptAssembly
|
||||
};
|
||||
|
||||
public static Dictionary<string, object> Revision = new Dictionary<string, object>()
|
||||
{
|
||||
{ CsScriptAssembly, 0},
|
||||
{ CsOneTimeScriptAssembly, 0}
|
||||
public static readonly string[] LoadedAssemblyName = {
|
||||
CsScriptBase.CsScriptAssembly,
|
||||
CsScriptBase.CsOneTimeScriptAssembly
|
||||
};
|
||||
|
||||
public CSharpParseOptions ParseOptions { get; protected set; }
|
||||
|
||||
public CsScriptBase() : base(isCollectible: true) {
|
||||
ParseOptions = CSharpParseOptions.Default
|
||||
.WithPreprocessorSymbols(new[] { LuaCsSetup.IsServer ? "SERVER" : (LuaCsSetup.IsClient ? "CLIENT" : "UNDEFINED") });
|
||||
}
|
||||
|
||||
public static SyntaxTree AssemblyInfoSyntaxTree(string asmName = null)
|
||||
public static Dictionary<string, object> Revision = new Dictionary<string, object>()
|
||||
{
|
||||
Revision[asmName] = (int)Revision[asmName] + 1;
|
||||
var asmInfo = new StringBuilder();
|
||||
asmInfo.AppendLine("using System.Reflection;");
|
||||
asmInfo.AppendLine($"[assembly: AssemblyMetadata(\"Revision\", \"{Revision[asmName]}\")]");
|
||||
asmInfo.AppendLine($"[assembly: AssemblyVersion(\"0.0.0.{Revision[asmName]}\")]");
|
||||
return CSharpSyntaxTree.ParseText(asmInfo.ToString(), CSharpParseOptions.Default);
|
||||
}
|
||||
{ CsScriptAssembly, 0},
|
||||
{ CsOneTimeScriptAssembly, 0}
|
||||
};
|
||||
|
||||
~CsScriptBase() { }
|
||||
}
|
||||
}
|
||||
public CSharpParseOptions ParseOptions { get; protected set; }
|
||||
|
||||
public CsScriptBase() : base(isCollectible: true) {
|
||||
ParseOptions = CSharpParseOptions.Default
|
||||
.WithPreprocessorSymbols(new[] { LuaCsSetup.IsServer ? "SERVER" : (LuaCsSetup.IsClient ? "CLIENT" : "UNDEFINED") });
|
||||
}
|
||||
|
||||
public static SyntaxTree AssemblyInfoSyntaxTree(string asmName = null)
|
||||
{
|
||||
Revision[asmName] = (int)Revision[asmName] + 1;
|
||||
var asmInfo = new StringBuilder();
|
||||
asmInfo.AppendLine("using System.Reflection;");
|
||||
asmInfo.AppendLine($"[assembly: AssemblyMetadata(\"Revision\", \"{Revision[asmName]}\")]");
|
||||
asmInfo.AppendLine($"[assembly: AssemblyVersion(\"0.0.0.{Revision[asmName]}\")]");
|
||||
return CSharpSyntaxTree.ParseText(asmInfo.ToString(), CSharpParseOptions.Default);
|
||||
}
|
||||
|
||||
~CsScriptBase() { }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
@@ -14,208 +14,208 @@ using System.Xml.Linq;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class CsScriptLoader : CsScriptBase
|
||||
{
|
||||
private List<MetadataReference> defaultReferences;
|
||||
class CsScriptLoader : CsScriptBase
|
||||
{
|
||||
private List<MetadataReference> defaultReferences;
|
||||
|
||||
private Dictionary<string, List<string>> sources;
|
||||
public Assembly Assembly { get; private set; }
|
||||
private Dictionary<string, List<string>> sources;
|
||||
public Assembly Assembly { get; private set; }
|
||||
|
||||
public CsScriptLoader()
|
||||
{
|
||||
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();
|
||||
|
||||
sources = new Dictionary<string, List<string>>();
|
||||
Assembly = null;
|
||||
}
|
||||
|
||||
private enum RunType { Standard, Forced, None };
|
||||
private bool ShouldRun(ContentPackage cp, string path)
|
||||
{
|
||||
if (!Directory.Exists(path + "CSharp"))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var isEnabled = ContentPackageManager.EnabledPackages.All.Contains(cp);
|
||||
if (File.Exists(path + "CSharp/RunConfig.xml"))
|
||||
{
|
||||
Stream stream = File.Open(path + "CSharp/RunConfig.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
var doc = XDocument.Load(stream);
|
||||
var elems = doc.Root.Elements().ToArray();
|
||||
var elem = elems.FirstOrDefault(e => e.Name.LocalName.Equals(LuaCsSetup.IsServer ? "Server" : (LuaCsSetup.IsClient ? "Client" : "None"), StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (elem != null && Enum.TryParse(elem.Value, true, out RunType rtValue))
|
||||
{
|
||||
if (rtValue == RunType.Standard && isEnabled)
|
||||
{
|
||||
LuaCsSetup.PrintCsMessage($"Standard run C# of {cp.Name}");
|
||||
return true;
|
||||
}
|
||||
else if (rtValue == RunType.Forced)
|
||||
{
|
||||
LuaCsSetup.PrintCsMessage($"Forced run C# of {cp.Name}");
|
||||
return true;
|
||||
}
|
||||
else if (rtValue == RunType.None)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
if (isEnabled)
|
||||
{
|
||||
LuaCsSetup.PrintCsMessage($"Assumed run C# of {cp.Name}");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void SearchFolders()
|
||||
{
|
||||
var paths = new Dictionary<string, string>();
|
||||
foreach (var cp in ContentPackageManager.AllPackages.Concat(ContentPackageManager.EnabledPackages.All))
|
||||
{
|
||||
var path = $"{Path.GetFullPath(Path.GetDirectoryName(cp.Path)).Replace('\\', '/')}/";
|
||||
if (ShouldRun(cp, path))
|
||||
{
|
||||
if (paths.ContainsKey(cp.Name))
|
||||
{
|
||||
if (ContentPackageManager.EnabledPackages.All.Contains(cp))
|
||||
{
|
||||
paths[cp.Name] = path;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
paths.Add(cp.Name, path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ((var _, var path) in paths)
|
||||
{
|
||||
RunFolder(path);
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasSources { get => sources.Count > 0; }
|
||||
|
||||
private void AddSources(string folder)
|
||||
public CsScriptLoader()
|
||||
{
|
||||
foreach (var str in DirSearch(folder))
|
||||
{
|
||||
string s = str.Replace("\\", "/");
|
||||
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();
|
||||
|
||||
if (sources.ContainsKey(folder))
|
||||
{
|
||||
sources[folder].Add(s);
|
||||
}
|
||||
else
|
||||
{
|
||||
sources.Add(folder, new List<string> { s });
|
||||
}
|
||||
}
|
||||
}
|
||||
sources = new Dictionary<string, List<string>>();
|
||||
Assembly = null;
|
||||
}
|
||||
|
||||
private void RunFolder(string folder)
|
||||
{
|
||||
|
||||
AddSources(folder + "/CSharp/Shared");
|
||||
|
||||
#if SERVER
|
||||
AddSources(folder + "/CSharp/Server");
|
||||
#else
|
||||
AddSources(folder + "/CSharp/Client");
|
||||
#endif
|
||||
}
|
||||
|
||||
private IEnumerable<SyntaxTree> ParseSources() {
|
||||
var syntaxTrees = new List<SyntaxTree>();
|
||||
|
||||
if (sources.Count <= 0) throw new Exception("No Cs sources detected");
|
||||
syntaxTrees.Add(AssemblyInfoSyntaxTree(CsScriptAssembly));
|
||||
foreach ((var folder, var src) in sources)
|
||||
private enum RunType { Standard, Forced, None };
|
||||
private bool ShouldRun(ContentPackage cp, string path)
|
||||
{
|
||||
if (!Directory.Exists(path + "CSharp"))
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var file in src)
|
||||
{
|
||||
var tree = SyntaxFactory.ParseSyntaxTree(File.ReadAllText(file), ParseOptions, file);
|
||||
|
||||
syntaxTrees.Add(tree);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LuaCsSetup.PrintCsError("Error loading '" + folder + "':\n" + ex.Message + "\n" + ex.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
return syntaxTrees;
|
||||
}
|
||||
|
||||
public List<Type> Compile()
|
||||
{
|
||||
IEnumerable<SyntaxTree> syntaxTrees = ParseSources();
|
||||
|
||||
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
|
||||
.WithMetadataImportOptions(MetadataImportOptions.All)
|
||||
.WithOptimizationLevel(OptimizationLevel.Release)
|
||||
.WithAllowUnsafe(false);
|
||||
var compilation = CSharpCompilation.Create(CsScriptAssembly, 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 = "CS MODS NOT LOADED | Compilation errors:";
|
||||
foreach (Diagnostic diagnostic in failures)
|
||||
errStr += $"\n{diagnostic}";
|
||||
LuaCsSetup.PrintCsError(errStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
Assembly = LoadFromStream(mem);
|
||||
}
|
||||
}
|
||||
|
||||
if (Assembly != null)
|
||||
{
|
||||
return Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ACsMod))).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unable to create cs mods assembly.");
|
||||
}
|
||||
}
|
||||
|
||||
private static string[] DirSearch(string sDir)
|
||||
{
|
||||
if (!Directory.Exists(sDir))
|
||||
{
|
||||
return new string[] {};
|
||||
return false;
|
||||
}
|
||||
|
||||
return Directory.GetFiles(sDir, "*.cs", SearchOption.AllDirectories);
|
||||
}
|
||||
var isEnabled = ContentPackageManager.EnabledPackages.All.Contains(cp);
|
||||
if (File.Exists(path + "CSharp/RunConfig.xml"))
|
||||
{
|
||||
Stream stream = File.Open(path + "CSharp/RunConfig.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
var doc = XDocument.Load(stream);
|
||||
var elems = doc.Root.Elements().ToArray();
|
||||
var elem = elems.FirstOrDefault(e => e.Name.LocalName.Equals(LuaCsSetup.IsServer ? "Server" : (LuaCsSetup.IsClient ? "Client" : "None"), StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Assembly = null;
|
||||
if (elem != null && Enum.TryParse(elem.Value, true, out RunType rtValue))
|
||||
{
|
||||
if (rtValue == RunType.Standard && isEnabled)
|
||||
{
|
||||
LuaCsSetup.PrintCsMessage($"Standard run C# of {cp.Name}");
|
||||
return true;
|
||||
}
|
||||
else if (rtValue == RunType.Forced)
|
||||
{
|
||||
LuaCsSetup.PrintCsMessage($"Forced run C# of {cp.Name}");
|
||||
return true;
|
||||
}
|
||||
else if (rtValue == RunType.None)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
if (isEnabled)
|
||||
{
|
||||
LuaCsSetup.PrintCsMessage($"Assumed run C# of {cp.Name}");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SearchFolders()
|
||||
{
|
||||
var paths = new Dictionary<string, string>();
|
||||
foreach (var cp in ContentPackageManager.AllPackages.Concat(ContentPackageManager.EnabledPackages.All))
|
||||
{
|
||||
var path = $"{Path.GetFullPath(Path.GetDirectoryName(cp.Path)).Replace('\\', '/')}/";
|
||||
if (ShouldRun(cp, path))
|
||||
{
|
||||
if (paths.ContainsKey(cp.Name))
|
||||
{
|
||||
if (ContentPackageManager.EnabledPackages.All.Contains(cp))
|
||||
{
|
||||
paths[cp.Name] = path;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
paths.Add(cp.Name, path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ((var _, var path) in paths)
|
||||
{
|
||||
RunFolder(path);
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasSources { get => sources.Count > 0; }
|
||||
|
||||
private void AddSources(string folder)
|
||||
{
|
||||
foreach (var str in DirSearch(folder))
|
||||
{
|
||||
string s = str.Replace("\\", "/");
|
||||
|
||||
if (sources.ContainsKey(folder))
|
||||
{
|
||||
sources[folder].Add(s);
|
||||
}
|
||||
else
|
||||
{
|
||||
sources.Add(folder, new List<string> { s });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RunFolder(string folder)
|
||||
{
|
||||
|
||||
AddSources(folder + "/CSharp/Shared");
|
||||
|
||||
#if SERVER
|
||||
AddSources(folder + "/CSharp/Server");
|
||||
#else
|
||||
AddSources(folder + "/CSharp/Client");
|
||||
#endif
|
||||
}
|
||||
|
||||
private IEnumerable<SyntaxTree> ParseSources() {
|
||||
var syntaxTrees = new List<SyntaxTree>();
|
||||
|
||||
if (sources.Count <= 0) throw new Exception("No Cs sources detected");
|
||||
syntaxTrees.Add(AssemblyInfoSyntaxTree(CsScriptAssembly));
|
||||
foreach ((var folder, var src) in sources)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var file in src)
|
||||
{
|
||||
var tree = SyntaxFactory.ParseSyntaxTree(File.ReadAllText(file), ParseOptions, file);
|
||||
|
||||
syntaxTrees.Add(tree);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LuaCsSetup.PrintCsError("Error loading '" + folder + "':\n" + ex.Message + "\n" + ex.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
return syntaxTrees;
|
||||
}
|
||||
|
||||
public List<Type> Compile()
|
||||
{
|
||||
IEnumerable<SyntaxTree> syntaxTrees = ParseSources();
|
||||
|
||||
var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
|
||||
.WithMetadataImportOptions(MetadataImportOptions.All)
|
||||
.WithOptimizationLevel(OptimizationLevel.Release)
|
||||
.WithAllowUnsafe(false);
|
||||
var compilation = CSharpCompilation.Create(CsScriptAssembly, 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 = "CS MODS NOT LOADED | Compilation errors:";
|
||||
foreach (Diagnostic diagnostic in failures)
|
||||
errStr += $"\n{diagnostic}";
|
||||
LuaCsSetup.PrintCsError(errStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
Assembly = LoadFromStream(mem);
|
||||
}
|
||||
}
|
||||
|
||||
if (Assembly != null)
|
||||
{
|
||||
return Assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(ACsMod))).ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Unable to create cs mods assembly.");
|
||||
}
|
||||
}
|
||||
|
||||
private static string[] DirSearch(string sDir)
|
||||
{
|
||||
if (!Directory.Exists(sDir))
|
||||
{
|
||||
return new string[] {};
|
||||
}
|
||||
|
||||
return Directory.GetFiles(sDir, "*.cs", SearchOption.AllDirectories);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
Assembly = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.CodeAnalysis.Scripting;
|
||||
@@ -13,109 +13,109 @@ using MoonSharp.Interpreter;
|
||||
|
||||
namespace Barotrauma
|
||||
{
|
||||
class CsScriptRunner : CsScriptBase
|
||||
{
|
||||
public LuaCsSetup setup;
|
||||
private List<MetadataReference> defaultReferences;
|
||||
private CSharpCompilationOptions compileOptions;
|
||||
private static readonly string[] usings = {
|
||||
"System",
|
||||
"Barotrauma",
|
||||
"System.Collections.Generic",
|
||||
"System.Linq"
|
||||
};
|
||||
class CsScriptRunner : CsScriptBase
|
||||
{
|
||||
public LuaCsSetup setup;
|
||||
private List<MetadataReference> defaultReferences;
|
||||
private CSharpCompilationOptions compileOptions;
|
||||
private static readonly string[] usings = {
|
||||
"System",
|
||||
"Barotrauma",
|
||||
"System.Collections.Generic",
|
||||
"System.Linq"
|
||||
};
|
||||
|
||||
public CsScriptRunner(LuaCsSetup setup)
|
||||
{
|
||||
this.setup = setup;
|
||||
|
||||
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();
|
||||
compileOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
|
||||
.WithMetadataImportOptions(MetadataImportOptions.All)
|
||||
.WithOptimizationLevel(OptimizationLevel.Release)
|
||||
.WithAllowUnsafe(false);
|
||||
}
|
||||
|
||||
private static string ToOneTimeScript(string code)
|
||||
public CsScriptRunner(LuaCsSetup setup)
|
||||
{
|
||||
var prefix = "";
|
||||
foreach (var u in usings) prefix += $"using {u}; ";
|
||||
prefix += "namespace NetOneTimeScript { public class NetOneTimeScriptRunner { public NetOneTimeScriptRunner() { } public object Run() {\n";
|
||||
var postfix = "\nreturn null; } } }";
|
||||
return prefix + code + postfix;
|
||||
}
|
||||
this.setup = setup;
|
||||
|
||||
public object Run(string code)
|
||||
{
|
||||
object scriptResilt = null;
|
||||
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();
|
||||
compileOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
|
||||
.WithMetadataImportOptions(MetadataImportOptions.All)
|
||||
.WithOptimizationLevel(OptimizationLevel.Release)
|
||||
.WithAllowUnsafe(false);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
code = ToOneTimeScript(code);
|
||||
var syntaxTree = SyntaxFactory.ParseSyntaxTree(code, ParseOptions);
|
||||
var compilation = CSharpCompilation.Create(CsOneTimeScriptAssembly, new[] { AssemblyInfoSyntaxTree(CsOneTimeScriptAssembly), syntaxTree }, defaultReferences, compileOptions);
|
||||
private static string ToOneTimeScript(string code)
|
||||
{
|
||||
var prefix = "";
|
||||
foreach (var u in usings) prefix += $"using {u}; ";
|
||||
prefix += "namespace NetOneTimeScript { public class NetOneTimeScriptRunner { public NetOneTimeScriptRunner() { } public object Run() {\n";
|
||||
var postfix = "\nreturn null; } } }";
|
||||
return prefix + code + postfix;
|
||||
}
|
||||
|
||||
Assembly assembly = null;
|
||||
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);
|
||||
public object Run(string code)
|
||||
{
|
||||
object scriptResilt = null;
|
||||
|
||||
string errStr = "Script compilation errors:";
|
||||
var lineErr = new SortedDictionary<int, (string, string)>();
|
||||
foreach (Diagnostic diagnostic in failures)
|
||||
try
|
||||
{
|
||||
code = ToOneTimeScript(code);
|
||||
var syntaxTree = SyntaxFactory.ParseSyntaxTree(code, ParseOptions);
|
||||
var compilation = CSharpCompilation.Create(CsOneTimeScriptAssembly, new[] { AssemblyInfoSyntaxTree(CsOneTimeScriptAssembly), syntaxTree }, defaultReferences, compileOptions);
|
||||
|
||||
Assembly assembly = null;
|
||||
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 = "Script compilation errors:";
|
||||
var lineErr = new SortedDictionary<int, (string, string)>();
|
||||
foreach (Diagnostic diagnostic in failures)
|
||||
{
|
||||
var line = syntaxTree.GetLineSpan(diagnostic.Location.SourceSpan).StartLinePosition.Line;
|
||||
lineErr[line] = (diagnostic.Id, diagnostic.ToString());
|
||||
}
|
||||
var lines = code.Split('\n');
|
||||
for (var i = 1; i < lines.Length - 1; i++)
|
||||
var line = syntaxTree.GetLineSpan(diagnostic.Location.SourceSpan).StartLinePosition.Line;
|
||||
lineErr[line] = (diagnostic.Id, diagnostic.ToString());
|
||||
}
|
||||
var lines = code.Split('\n');
|
||||
for (var i = 1; i < lines.Length - 1; i++)
|
||||
{
|
||||
errStr += $"\n{i} >> {lines[i]}";
|
||||
if (lineErr.ContainsKey(i)) errStr += $" <=== {lineErr[i].Item1}";
|
||||
}
|
||||
errStr += "\n";
|
||||
foreach ((var idx, (var id, var err)) in lineErr)
|
||||
errStr += $"\n{i} >> {lines[i]}";
|
||||
if (lineErr.ContainsKey(i)) errStr += $" <=== {lineErr[i].Item1}";
|
||||
}
|
||||
errStr += "\n";
|
||||
foreach ((var idx, (var id, var err)) in lineErr)
|
||||
{
|
||||
errStr += $"\n{idx}: {err}";
|
||||
}
|
||||
LuaCsSetup.PrintCsError(errStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
assembly = LoadFromStream(mem);
|
||||
var runner = assembly.CreateInstance("NetOneTimeScript.NetOneTimeScriptRunner");
|
||||
if (runner != null)
|
||||
{
|
||||
var method = runner.GetType().GetMethod("Run", BindingFlags.Public | BindingFlags.Instance);
|
||||
if (method != null)
|
||||
{
|
||||
scriptResilt = method.Invoke(runner, null);
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
UserData.UnregisterType(type, true);
|
||||
}
|
||||
}
|
||||
else { LuaCsSetup.PrintCsError("Script Error - no run method detected"); }
|
||||
}
|
||||
else { LuaCsSetup.PrintCsError("Script Error - no runner class detected"); }
|
||||
}
|
||||
}
|
||||
Unload();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LuaCsSetup.PrintCsError("Error running script:\n" + ex.Message + "\n" + ex.StackTrace);
|
||||
}
|
||||
errStr += $"\n{idx}: {err}";
|
||||
}
|
||||
LuaCsSetup.PrintCsError(errStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
assembly = LoadFromStream(mem);
|
||||
var runner = assembly.CreateInstance("NetOneTimeScript.NetOneTimeScriptRunner");
|
||||
if (runner != null)
|
||||
{
|
||||
var method = runner.GetType().GetMethod("Run", BindingFlags.Public | BindingFlags.Instance);
|
||||
if (method != null)
|
||||
{
|
||||
scriptResilt = method.Invoke(runner, null);
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
UserData.UnregisterType(type, true);
|
||||
}
|
||||
}
|
||||
else { LuaCsSetup.PrintCsError("Script Error - no run method detected"); }
|
||||
}
|
||||
else { LuaCsSetup.PrintCsError("Script Error - no runner class detected"); }
|
||||
}
|
||||
}
|
||||
Unload();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LuaCsSetup.PrintCsError("Error running script:\n" + ex.Message + "\n" + ex.StackTrace);
|
||||
}
|
||||
|
||||
return scriptResilt;
|
||||
}
|
||||
return scriptResilt;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user