fix CsScriptRunner
This commit is contained in:
@@ -17,8 +17,14 @@ namespace Barotrauma
|
||||
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)
|
||||
public CsScriptRunner(LuaCsSetup setup) : base(isCollectible: true)
|
||||
{
|
||||
this.setup = setup;
|
||||
|
||||
@@ -34,17 +40,10 @@ namespace Barotrauma
|
||||
|
||||
private static string ToOneTimeScript(string code)
|
||||
{
|
||||
var prefix = @"
|
||||
public class NetOneTimeScriptRunner {
|
||||
public NetOneTimeScriptRunner() { }
|
||||
public object Run() {
|
||||
|
||||
";
|
||||
var postfix = @"
|
||||
return null;
|
||||
}
|
||||
}
|
||||
";
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -54,7 +53,8 @@ public class NetOneTimeScriptRunner {
|
||||
|
||||
try
|
||||
{
|
||||
var syntaxTree = SyntaxFactory.ParseSyntaxTree(ToOneTimeScript(code), CSharpParseOptions.Default);
|
||||
code = ToOneTimeScript(code);
|
||||
var syntaxTree = SyntaxFactory.ParseSyntaxTree(code, CSharpParseOptions.Default);
|
||||
var compilation = CSharpCompilation.Create("NetOneTimeScriptAssembly", new[] { syntaxTree }, defaultReferences, compileOptions);
|
||||
|
||||
Assembly assembly = null;
|
||||
@@ -66,43 +66,39 @@ public class NetOneTimeScriptRunner {
|
||||
IEnumerable<Diagnostic> failures = result.Diagnostics.Where(d => d.IsWarningAsError || d.Severity == DiagnosticSeverity.Error);
|
||||
|
||||
string errStr = "Script cmopilation errors:";
|
||||
var lineErr = new SortedDictionary<int, (string, string)>();
|
||||
foreach (Diagnostic diagnostic in failures)
|
||||
errStr = $"\n{diagnostic}";
|
||||
{
|
||||
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{idx}: {err}";
|
||||
}
|
||||
LuaCsSetup.PrintCsError(errStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
var metaReader = new PEReader(mem).GetMetadataReader();
|
||||
var errStr = CsScriptFilter.FilterMetadata(metaReader);
|
||||
if (errStr == null)
|
||||
{
|
||||
foreach (var handle in metaReader.TypeDefinitions)
|
||||
{
|
||||
var typeDef = metaReader.GetTypeDefinition(handle);
|
||||
var typeName = $"{metaReader.GetString(typeDef.Namespace)}.{metaReader.GetString(typeDef.Name)}";
|
||||
if (typeName != ".NetOneTimeScriptRunner")
|
||||
{
|
||||
errStr = "Script Error - malformed assembly";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var errStr = CsScriptFilter.FilterOneTimeMetadata(new PEReader(mem).GetMetadataReader());
|
||||
if (errStr == null)
|
||||
{
|
||||
mem.Seek(0, SeekOrigin.Begin);
|
||||
assembly = LoadFromStream(mem);
|
||||
var runner = assembly.CreateInstance("NetOneTimeScriptRunner");
|
||||
var runner = assembly.CreateInstance("NetOneTimeScript.NetOneTimeScriptRunner");
|
||||
if (runner != null)
|
||||
{
|
||||
if (runner.GetType().GetMethods().Count() > 1) LuaCsSetup.PrintCsError("Script Error - malformed runner class");
|
||||
else
|
||||
{
|
||||
var method = runner.GetType().GetMethod("Run", BindingFlags.Public | BindingFlags.Instance);
|
||||
if (method != null) scriptResilt = method.Invoke(runner, null);
|
||||
else LuaCsSetup.PrintCsError("Script Error - no run method detected");
|
||||
}
|
||||
var method = runner.GetType().GetMethod("Run", BindingFlags.Public | BindingFlags.Instance);
|
||||
if (method != null) scriptResilt = method.Invoke(runner, null);
|
||||
else LuaCsSetup.PrintCsError("Script Error - no run method detected");
|
||||
}
|
||||
else LuaCsSetup.PrintCsError("Script Error - no runner class detected");
|
||||
}
|
||||
@@ -111,15 +107,6 @@ public class NetOneTimeScriptRunner {
|
||||
}
|
||||
Unload();
|
||||
}
|
||||
catch (CompilationErrorException ex)
|
||||
{
|
||||
string errStr = "Script Cmopilation Error:";
|
||||
foreach (var diag in ex.Diagnostics)
|
||||
{
|
||||
errStr += "\n" + diag.ToString();
|
||||
}
|
||||
LuaCsSetup.PrintCsError(errStr);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LuaCsSetup.PrintCsError("Error running script:\n" + ex.Message + "\n" + ex.StackTrace);
|
||||
|
||||
Reference in New Issue
Block a user