Removed CsScriptRunner since its useless
This commit is contained in:
@@ -3318,20 +3318,6 @@ namespace Barotrauma
|
||||
}
|
||||
}));
|
||||
|
||||
commands.Add(new Command("cl_cs", $"cl_cs: Runs a string on the client.", (string[] args) =>
|
||||
{
|
||||
if (LuaCsSetup.GetPackage(LuaCsSetup.CsForBarotraumaId, false, true) == null) { return; }
|
||||
|
||||
if (GameMain.Client != null && !GameMain.Client.HasPermission(ClientPermissions.ConsoleCommands))
|
||||
{
|
||||
ThrowError("Command not permitted.");
|
||||
return;
|
||||
}
|
||||
|
||||
GameMain.LuaCs.CsScript.Run(string.Join(" ", args));
|
||||
GameMain.LuaCs.RecreateCsScript();
|
||||
}));
|
||||
|
||||
commands.Add(new Command("cl_reloadlua|cl_reloadcs|cl_reloadluacs", "Re-initializes the LuaCs environment.", (string[] args) =>
|
||||
{
|
||||
GameMain.LuaCs.Initialize();
|
||||
|
||||
@@ -1254,13 +1254,6 @@ namespace Barotrauma
|
||||
LuaCsLogger.HandleException(ex, LuaCsMessageOrigin.LuaMod);
|
||||
}
|
||||
}));
|
||||
commands.Add(new Command("cs", "cs: Runs a string.", (string[] args) =>
|
||||
{
|
||||
if (LuaCsSetup.GetPackage(LuaCsSetup.CsForBarotraumaId, false, true) == null) { return; }
|
||||
|
||||
GameMain.LuaCs.CsScript.Run(string.Join(" ", args));
|
||||
GameMain.LuaCs.RecreateCsScript();
|
||||
}));
|
||||
|
||||
commands.Add(new Command("reloadlua|reloadcs|reloadluacs", "Re-initializes the LuaCs environment.", (string[] args) =>
|
||||
{
|
||||
|
||||
@@ -17,18 +17,15 @@ namespace Barotrauma
|
||||
class CsScriptBase : AssemblyLoadContext
|
||||
{
|
||||
|
||||
public const string CsOneTimeScriptAssembly = "NetOneTimeScriptAssembly";
|
||||
public const string CsScriptAssembly = "NetScriptAssembly";
|
||||
|
||||
public static readonly string[] LoadedAssemblyName = {
|
||||
CsScriptBase.CsScriptAssembly,
|
||||
CsScriptBase.CsOneTimeScriptAssembly
|
||||
CsScriptBase.CsScriptAssembly
|
||||
};
|
||||
|
||||
public static Dictionary<string, object> Revision = new Dictionary<string, object>()
|
||||
{
|
||||
{ CsScriptAssembly, 0},
|
||||
{ CsOneTimeScriptAssembly, 0}
|
||||
{ CsScriptAssembly, 0}
|
||||
};
|
||||
|
||||
public CSharpParseOptions ParseOptions { get; protected set; }
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
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;
|
||||
using System.Runtime.Loader;
|
||||
using System.Reflection.PortableExecutable;
|
||||
using System.Reflection.Metadata;
|
||||
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"
|
||||
};
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public object Run(string code)
|
||||
{
|
||||
object scriptResilt = null;
|
||||
|
||||
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++)
|
||||
{
|
||||
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}";
|
||||
}
|
||||
LuaCsLogger.LogError(errStr, LuaCsMessageOrigin.CSharpMod);
|
||||
}
|
||||
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 { LuaCsLogger.LogError("Script Error - no run method detected", LuaCsMessageOrigin.CSharpMod); }
|
||||
}
|
||||
else { LuaCsLogger.LogError("Script Error - no runner class detected", LuaCsMessageOrigin.CSharpMod); }
|
||||
}
|
||||
}
|
||||
Unload();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LuaCsLogger.LogError("Error running script:\n" + ex.Message + "\n" + ex.StackTrace, LuaCsMessageOrigin.CSharpMod);
|
||||
}
|
||||
|
||||
return scriptResilt;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -397,7 +397,6 @@ namespace Barotrauma
|
||||
{
|
||||
DebugConsole.FindCommand("cl_reloadluacs"),
|
||||
DebugConsole.FindCommand("cl_lua"),
|
||||
DebugConsole.FindCommand("cl_cs"),
|
||||
};
|
||||
|
||||
foreach (var consoleCommand in LuaAddedCommand.Concat(permitted.AsEnumerable()))
|
||||
|
||||
@@ -9,7 +9,6 @@ using LuaCsCompatPatchFunc = Barotrauma.LuaCsPatch;
|
||||
using System.Diagnostics;
|
||||
|
||||
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.CsScriptAssembly, AllInternalsVisible = true)]
|
||||
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.CsOneTimeScriptAssembly, AllInternalsVisible = true)]
|
||||
namespace Barotrauma
|
||||
{
|
||||
class LuaCsSetupConfig
|
||||
@@ -49,7 +48,6 @@ namespace Barotrauma
|
||||
|
||||
|
||||
public Script Lua { get; private set; }
|
||||
public CsScriptRunner CsScript { get; private set; }
|
||||
public LuaScriptLoader LuaScriptLoader { get; private set; }
|
||||
|
||||
public LuaGame Game { get; private set; }
|
||||
@@ -103,16 +101,6 @@ namespace Barotrauma
|
||||
file.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// due to there's a race on the process and the unloaded AssemblyLoadContexts,
|
||||
/// should recreate runner after the script runs
|
||||
/// </summary>
|
||||
public void RecreateCsScript()
|
||||
{
|
||||
CsScript = new CsScriptRunner(CsScript.setup);
|
||||
Lua.Globals["CsScript"] = CsScript;
|
||||
}
|
||||
|
||||
public static ContentPackage GetPackage(ContentPackageId id, bool fallbackToAll = true, bool useBackup = false)
|
||||
{
|
||||
foreach (ContentPackage package in ContentPackageManager.EnabledPackages.All)
|
||||
@@ -257,7 +245,6 @@ namespace Barotrauma
|
||||
PerformanceCounter = new LuaCsPerformanceCounter();
|
||||
LuaScriptLoader = null;
|
||||
Lua = null;
|
||||
CsScript = null;
|
||||
|
||||
if (CsScriptLoader != null)
|
||||
{
|
||||
@@ -285,7 +272,6 @@ namespace Barotrauma
|
||||
Lua.Options.ScriptLoader = LuaScriptLoader;
|
||||
Lua.Options.CheckThreadAccess = false;
|
||||
Script.GlobalOptions.ShouldPCallCatchException = (Exception ex) => { return true; };
|
||||
CsScript = new CsScriptRunner(this);
|
||||
|
||||
require = new LuaRequire(Lua);
|
||||
|
||||
@@ -304,7 +290,6 @@ namespace Barotrauma
|
||||
UserData.RegisterType<LuaCsFile>();
|
||||
UserData.RegisterType<LuaCsCompatPatchFunc>();
|
||||
UserData.RegisterType<LuaCsPatchFunc>();
|
||||
UserData.RegisterType<CsScriptRunner>();
|
||||
UserData.RegisterType<LuaGame>();
|
||||
UserData.RegisterType<LuaCsTimer>();
|
||||
UserData.RegisterType<LuaCsFile>();
|
||||
@@ -326,7 +311,6 @@ namespace Barotrauma
|
||||
Lua.Globals["load"] = (Func<string, Table, string, DynValue>)Lua.LoadString;
|
||||
|
||||
Lua.Globals["Logger"] = UserData.CreateStatic<LuaCsLogger>();
|
||||
Lua.Globals["CsScript"] = CsScript;
|
||||
Lua.Globals["LuaUserData"] = UserData.CreateStatic<LuaUserData>();
|
||||
Lua.Globals["Game"] = Game;
|
||||
Lua.Globals["Hook"] = Hook;
|
||||
|
||||
Reference in New Issue
Block a user