Use WorkshopId instead and minor cleanup
This commit is contained in:
@@ -3278,7 +3278,7 @@ namespace Barotrauma
|
|||||||
const string CMD_CL_CS = "cl_cs";
|
const string CMD_CL_CS = "cl_cs";
|
||||||
commands.Add(new Command(CMD_CL_CS, $"{CMD_CL_CS}: runs a string on the client", (string[] args) =>
|
commands.Add(new Command(CMD_CL_CS, $"{CMD_CL_CS}: runs a string on the client", (string[] args) =>
|
||||||
{
|
{
|
||||||
if (LuaCsSetup.GetPackage("CsForBarotrauma", false, true) == null) { return; }
|
if (LuaCsSetup.GetPackage(LuaCsSetup.CsForBarotraumaId, false, true) == null) { return; }
|
||||||
|
|
||||||
if (GameMain.Client != null && !GameMain.Client.HasPermission(ClientPermissions.ConsoleCommands))
|
if (GameMain.Client != null && !GameMain.Client.HasPermission(ClientPermissions.ConsoleCommands))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ namespace Barotrauma
|
|||||||
{
|
{
|
||||||
if (!File.Exists(LuaCsSetup.VersionFile)) { return; }
|
if (!File.Exists(LuaCsSetup.VersionFile)) { return; }
|
||||||
|
|
||||||
ContentPackage luaPackage = LuaCsSetup.GetPackage("Lua For Barotrauma");
|
ContentPackage luaPackage = LuaCsSetup.GetPackage(LuaCsSetup.LuaForBarotraumaId);
|
||||||
string luaCsPath = Path.GetDirectoryName(luaPackage.Path);
|
string luaCsPath = Path.GetDirectoryName(luaPackage.Path);
|
||||||
|
|
||||||
if (luaPackage == null) { return; }
|
if (luaPackage == null) { return; }
|
||||||
@@ -69,7 +69,7 @@ namespace Barotrauma
|
|||||||
|
|
||||||
if (clientVersion == workshopVersion) { return; }
|
if (clientVersion == workshopVersion) { return; }
|
||||||
|
|
||||||
string additional = LuaCsSetup.GetPackage("CsForBarotrauma", false, true) == null ? "" : "Cs";
|
string additional = LuaCsSetup.GetPackage(LuaCsSetup.CsForBarotraumaId, false, true) == null ? "" : "Cs";
|
||||||
|
|
||||||
var msg = new GUIMessageBox($"Lua{additional} Update", $"Your Lua{additional} client version is different from the version found in the Lua{additional}ForBarotrauma workshop files. Do you want to update?\n\n Client Version: {clientVersion}\n Workshop Version: {workshopVersion}",
|
var msg = new GUIMessageBox($"Lua{additional} Update", $"Your Lua{additional} client version is different from the version found in the Lua{additional}ForBarotrauma workshop files. Do you want to update?\n\n Client Version: {clientVersion}\n Workshop Version: {workshopVersion}",
|
||||||
new LocalizedString[2] { TextManager.Get("Yes"), TextManager.Get("Cancel") });
|
new LocalizedString[2] { TextManager.Get("Yes"), TextManager.Get("Cancel") });
|
||||||
|
|||||||
@@ -400,7 +400,7 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
string additional = LuaCsSetup.GetPackage("CsForBarotrauma", false, true) == null ? "" : "Cs";
|
string additional = LuaCsSetup.GetPackage(LuaCsSetup.CsForBarotraumaId, false, true) == null ? "" : "Cs";
|
||||||
|
|
||||||
new GUIButton(new RectTransform(new Point(300, 30), Frame.RectTransform, Anchor.TopLeft) { AbsoluteOffset = new Point(20, 50) },
|
new GUIButton(new RectTransform(new Point(300, 30), Frame.RectTransform, Anchor.TopLeft) { AbsoluteOffset = new Point(20, 50) },
|
||||||
$"Remove Client-Side Lua{additional}", style: "MainMenuGUIButton", color: GUIStyle.Red)
|
$"Remove Client-Side Lua{additional}", style: "MainMenuGUIButton", color: GUIStyle.Red)
|
||||||
|
|||||||
@@ -1264,7 +1264,7 @@ namespace Barotrauma
|
|||||||
}));
|
}));
|
||||||
commands.Add(new Command("cs", "cs: Runs a string", (string[] args) =>
|
commands.Add(new Command("cs", "cs: Runs a string", (string[] args) =>
|
||||||
{
|
{
|
||||||
if (LuaCsSetup.GetPackage("CsForBarotrauma", false, true) == null) { return; }
|
if (LuaCsSetup.GetPackage(LuaCsSetup.CsForBarotraumaId, false, true) == null) { return; }
|
||||||
|
|
||||||
GameMain.LuaCs.CsScript.Run(string.Join(" ", args));
|
GameMain.LuaCs.CsScript.Run(string.Join(" ", args));
|
||||||
GameMain.LuaCs.RecreateCsScript();
|
GameMain.LuaCs.RecreateCsScript();
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace Barotrauma
|
|||||||
{
|
{
|
||||||
public static void Install()
|
public static void Install()
|
||||||
{
|
{
|
||||||
ContentPackage luaPackage = LuaCsSetup.GetPackage("Lua For Barotrauma");
|
ContentPackage luaPackage = LuaCsSetup.GetPackage(LuaCsSetup.LuaForBarotraumaId);
|
||||||
|
|
||||||
if (luaPackage == null)
|
if (luaPackage == null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -38,6 +38,20 @@ require("DefaultHook")
|
|||||||
Descriptors = LuaSetup.LuaUserData.Descriptors
|
Descriptors = LuaSetup.LuaUserData.Descriptors
|
||||||
LuaUserData = LuaSetup.LuaUserData
|
LuaUserData = LuaSetup.LuaUserData
|
||||||
|
|
||||||
|
if CSActive then
|
||||||
|
LuaUserData.RegisterType = function (typeName)
|
||||||
|
local descriptor = Descriptors[typeName]
|
||||||
|
|
||||||
|
print(typeName)
|
||||||
|
|
||||||
|
if descriptor == nil then
|
||||||
|
error("Type '" .. typeName .. "' can't be registered", 2)
|
||||||
|
else
|
||||||
|
return descriptor
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
LuaSetup = nil
|
LuaSetup = nil
|
||||||
|
|
||||||
require("ModLoader")
|
require("ModLoader")
|
||||||
@@ -516,7 +516,7 @@ namespace Barotrauma
|
|||||||
{
|
{
|
||||||
"Barotrauma.Lua",
|
"Barotrauma.Lua",
|
||||||
"Barotrauma.Cs",
|
"Barotrauma.Cs",
|
||||||
"ContentPackageManager",
|
"Barotrauma.ContentPackageManager",
|
||||||
};
|
};
|
||||||
|
|
||||||
private static void ValidatePatchTarget(MethodBase method)
|
private static void ValidatePatchTarget(MethodBase method)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
@@ -18,12 +18,12 @@ using LuaCsCompatPatchFunc = Barotrauma.LuaCsPatch;
|
|||||||
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.CsOneTimeScriptAssembly, AllInternalsVisible = true)]
|
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.CsOneTimeScriptAssembly, AllInternalsVisible = true)]
|
||||||
namespace Barotrauma
|
namespace Barotrauma
|
||||||
{
|
{
|
||||||
class LuaCsSetupConfig
|
class LuaCsSetupConfig
|
||||||
{
|
{
|
||||||
public bool FirstTimeCsWarning = true;
|
public bool FirstTimeCsWarning = true;
|
||||||
|
|
||||||
public LuaCsSetupConfig() { }
|
public LuaCsSetupConfig() { }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal delegate void LuaCsMessageLogger(string prefix, object o);
|
internal delegate void LuaCsMessageLogger(string prefix, object o);
|
||||||
|
|
||||||
@@ -37,120 +37,123 @@ namespace Barotrauma
|
|||||||
CSharpMod,
|
CSharpMod,
|
||||||
}
|
}
|
||||||
|
|
||||||
partial class LuaCsSetup
|
partial class LuaCsSetup
|
||||||
{
|
{
|
||||||
public const string LuaSetupFile = "Lua/LuaSetup.lua";
|
public const string LuaSetupFile = "Lua/LuaSetup.lua";
|
||||||
public const string VersionFile = "luacsversion.txt";
|
public const string VersionFile = "luacsversion.txt";
|
||||||
|
public static ContentPackageId LuaForBarotraumaId = new SteamWorkshopId(2559634234);
|
||||||
|
public static ContentPackageId CsForBarotraumaId = new SteamWorkshopId(2795927223);
|
||||||
|
|
||||||
private const string configFileName = "LuaCsSetupConfig.xml";
|
|
||||||
|
private const string configFileName = "LuaCsSetupConfig.xml";
|
||||||
|
|
||||||
#if SERVER
|
#if SERVER
|
||||||
public const bool IsServer = true;
|
public const bool IsServer = true;
|
||||||
public const bool IsClient = false;
|
public const bool IsClient = false;
|
||||||
#else
|
#else
|
||||||
public const bool IsServer = false;
|
public const bool IsServer = false;
|
||||||
public const bool IsClient = true;
|
public const bool IsClient = true;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private static int executionNumber = 0;
|
private static int executionNumber = 0;
|
||||||
|
|
||||||
private Script lua;
|
private Script lua;
|
||||||
public Script Lua
|
public Script Lua
|
||||||
{
|
{
|
||||||
get { return lua; }
|
get { return lua; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public CsScriptRunner CsScript { get; private set; }
|
public CsScriptRunner CsScript { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// due to there's a race on the process and the unloaded AssemblyLoadContexts,
|
/// due to there's a race on the process and the unloaded AssemblyLoadContexts,
|
||||||
/// should recreate runner after the script runs
|
/// should recreate runner after the script runs
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void RecreateCsScript()
|
public void RecreateCsScript()
|
||||||
{
|
{
|
||||||
CsScript = new CsScriptRunner(CsScript.setup);
|
CsScript = new CsScriptRunner(CsScript.setup);
|
||||||
lua.Globals["CsScript"] = CsScript;
|
lua.Globals["CsScript"] = CsScript;
|
||||||
}
|
}
|
||||||
|
|
||||||
public LuaScriptLoader LuaScriptLoader { get; private set; }
|
public LuaScriptLoader LuaScriptLoader { get; private set; }
|
||||||
|
|
||||||
public LuaGame Game { get; private set; }
|
public LuaGame Game { get; private set; }
|
||||||
public LuaCsHook Hook { get; private set; }
|
public LuaCsHook Hook { get; private set; }
|
||||||
public LuaCsTimer Timer { get; private set; }
|
public LuaCsTimer Timer { get; private set; }
|
||||||
public LuaCsNetworking Networking { get; private set; }
|
public LuaCsNetworking Networking { get; private set; }
|
||||||
public LuaCsSteam Steam { get; private set; }
|
public LuaCsSteam Steam { get; private set; }
|
||||||
public LuaCsPerformanceCounter PerformanceCounter { get; private set; }
|
public LuaCsPerformanceCounter PerformanceCounter { get; private set; }
|
||||||
|
|
||||||
public LuaCsModStore ModStore { get; private set; }
|
public LuaCsModStore ModStore { get; private set; }
|
||||||
private LuaRequire require { get; set; }
|
private LuaRequire require { get; set; }
|
||||||
|
|
||||||
public CsScriptLoader CsScriptLoader { get; private set; }
|
public CsScriptLoader CsScriptLoader { get; private set; }
|
||||||
public LuaCsSetupConfig Config { get; private set; }
|
public LuaCsSetupConfig Config { get; private set; }
|
||||||
|
|
||||||
public LuaCsSetup()
|
public LuaCsSetup()
|
||||||
{
|
{
|
||||||
MessageLogger = DefaultMessageLogger;
|
MessageLogger = DefaultMessageLogger;
|
||||||
ExceptionHandler = DefaultExceptionHandler;
|
ExceptionHandler = DefaultExceptionHandler;
|
||||||
|
|
||||||
Hook = new LuaCsHook(this);
|
Hook = new LuaCsHook(this);
|
||||||
ModStore = new LuaCsModStore();
|
ModStore = new LuaCsModStore();
|
||||||
|
|
||||||
Game = new LuaGame();
|
Game = new LuaGame();
|
||||||
Networking = new LuaCsNetworking();
|
Networking = new LuaCsNetworking();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void UpdateConfig()
|
public void UpdateConfig()
|
||||||
{
|
{
|
||||||
FileStream file;
|
FileStream file;
|
||||||
if (!File.Exists(configFileName)) file = File.Create(configFileName);
|
if (!File.Exists(configFileName)) file = File.Create(configFileName);
|
||||||
else file = File.Open(configFileName, FileMode.Truncate, FileAccess.Write);
|
else file = File.Open(configFileName, FileMode.Truncate, FileAccess.Write);
|
||||||
LuaCsConfig.Save(file, Config);
|
LuaCsConfig.Save(file, Config);
|
||||||
file.Close();
|
file.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static ContentPackage GetPackage(Identifier name, bool fallbackToAll = true, bool useBackup = false)
|
public static ContentPackage GetPackage(ContentPackageId id, bool fallbackToAll = true, bool useBackup = false)
|
||||||
{
|
{
|
||||||
foreach (ContentPackage package in ContentPackageManager.EnabledPackages.All)
|
foreach (ContentPackage package in ContentPackageManager.EnabledPackages.All)
|
||||||
{
|
|
||||||
if (package.NameMatches(name))
|
|
||||||
{
|
|
||||||
return package;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fallbackToAll)
|
|
||||||
{
|
|
||||||
foreach (ContentPackage package in ContentPackageManager.LocalPackages)
|
|
||||||
{
|
|
||||||
if (package.NameMatches(name))
|
|
||||||
{
|
|
||||||
return package;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (ContentPackage package in ContentPackageManager.AllPackages)
|
|
||||||
{
|
|
||||||
if (package.NameMatches(name))
|
|
||||||
{
|
|
||||||
return package;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (useBackup && ContentPackageManager.EnabledPackages.BackupPackages.Regular != null)
|
|
||||||
{
|
{
|
||||||
foreach (ContentPackage package in ContentPackageManager.EnabledPackages.BackupPackages.Regular.Value)
|
if (package.UgcId.ValueEquals(id))
|
||||||
{
|
{
|
||||||
if (package.NameMatches(name))
|
return package;
|
||||||
{
|
}
|
||||||
return package;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
if (fallbackToAll)
|
||||||
}
|
{
|
||||||
|
foreach (ContentPackage package in ContentPackageManager.LocalPackages)
|
||||||
|
{
|
||||||
|
if (package.UgcId.ValueEquals(id))
|
||||||
|
{
|
||||||
|
return package;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (ContentPackage package in ContentPackageManager.AllPackages)
|
||||||
|
{
|
||||||
|
if (package.UgcId.ValueEquals(id))
|
||||||
|
{
|
||||||
|
return package;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (useBackup && ContentPackageManager.EnabledPackages.BackupPackages.Regular != null)
|
||||||
|
{
|
||||||
|
foreach (ContentPackage package in ContentPackageManager.EnabledPackages.BackupPackages.Regular.Value)
|
||||||
|
{
|
||||||
|
if (package.UgcId.ValueEquals(id))
|
||||||
|
{
|
||||||
|
return package;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private void DefaultExceptionHandler(Exception ex, LuaCsMessageOrigin origin)
|
private void DefaultExceptionHandler(Exception ex, LuaCsMessageOrigin origin)
|
||||||
{
|
{
|
||||||
@@ -288,271 +291,271 @@ namespace Barotrauma
|
|||||||
// TODO: deprecate this (in an effort to get rid of as much global state as possible)
|
// TODO: deprecate this (in an effort to get rid of as much global state as possible)
|
||||||
public static void PrintCsMessage(object message) => GameMain.LuaCs.PrintMessage(message);
|
public static void PrintCsMessage(object message) => GameMain.LuaCs.PrintMessage(message);
|
||||||
|
|
||||||
private DynValue DoFile(string file, Table globalContext = null, string codeStringFriendly = null)
|
private DynValue DoFile(string file, Table globalContext = null, string codeStringFriendly = null)
|
||||||
{
|
{
|
||||||
if (!LuaCsFile.CanReadFromPath(file))
|
if (!LuaCsFile.CanReadFromPath(file))
|
||||||
{
|
|
||||||
throw new ScriptRuntimeException($"dofile: File access to {file} not allowed.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!LuaCsFile.Exists(file))
|
|
||||||
{
|
|
||||||
throw new ScriptRuntimeException($"dofile: File {file} not found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return lua.DoFile(file, globalContext, codeStringFriendly);
|
|
||||||
}
|
|
||||||
|
|
||||||
private DynValue LoadFile(string file, Table globalContext = null, string codeStringFriendly = null)
|
|
||||||
{
|
|
||||||
if (!LuaCsFile.CanReadFromPath(file))
|
|
||||||
{
|
{
|
||||||
throw new ScriptRuntimeException($"loadfile: File access to {file} not allowed.");
|
throw new ScriptRuntimeException($"dofile: File access to {file} not allowed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!LuaCsFile.Exists(file))
|
if (!LuaCsFile.Exists(file))
|
||||||
{
|
{
|
||||||
throw new ScriptRuntimeException($"loadfile: File {file} not found.");
|
throw new ScriptRuntimeException($"dofile: File {file} not found.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return lua.LoadFile(file, globalContext, codeStringFriendly);
|
return lua.DoFile(file, globalContext, codeStringFriendly);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DynValue CallLuaFunction(object function, params object[] args)
|
private DynValue LoadFile(string file, Table globalContext = null, string codeStringFriendly = null)
|
||||||
{
|
{
|
||||||
|
if (!LuaCsFile.CanReadFromPath(file))
|
||||||
|
{
|
||||||
|
throw new ScriptRuntimeException($"loadfile: File access to {file} not allowed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!LuaCsFile.Exists(file))
|
||||||
|
{
|
||||||
|
throw new ScriptRuntimeException($"loadfile: File {file} not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return lua.LoadFile(file, globalContext, codeStringFriendly);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DynValue CallLuaFunction(object function, params object[] args)
|
||||||
|
{
|
||||||
// XXX: `lua` might be null if `LuaCsSetup.Stop()` is called while
|
// XXX: `lua` might be null if `LuaCsSetup.Stop()` is called while
|
||||||
// a patched function is still running.
|
// a patched function is still running.
|
||||||
if (lua == null) return null;
|
if (lua == null) return null;
|
||||||
|
|
||||||
lock (lua)
|
lock (lua)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
return lua.Call(function, args);
|
return lua.Call(function, args);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
HandleException(e, LuaCsMessageOrigin.LuaMod);
|
HandleException(e, LuaCsMessageOrigin.LuaMod);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetModulePaths(string[] str)
|
private void SetModulePaths(string[] str)
|
||||||
{
|
{
|
||||||
LuaScriptLoader.ModulePaths = str;
|
LuaScriptLoader.ModulePaths = str;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update()
|
public void Update()
|
||||||
{
|
{
|
||||||
Hook?.Update();
|
Hook?.Update();
|
||||||
Timer?.Update();
|
Timer?.Update();
|
||||||
Steam?.Update();
|
Steam?.Update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
foreach (var type in AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == CsScriptBase.CsScriptAssembly).SelectMany(assembly => assembly.GetTypes()))
|
foreach (var type in AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == CsScriptBase.CsScriptAssembly).SelectMany(assembly => assembly.GetTypes()))
|
||||||
{
|
{
|
||||||
UserData.UnregisterType(type, true);
|
UserData.UnregisterType(type, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var mod in ACsMod.LoadedMods.ToArray())
|
foreach (var mod in ACsMod.LoadedMods.ToArray())
|
||||||
{
|
{
|
||||||
mod.Dispose();
|
mod.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
ACsMod.LoadedMods.Clear();
|
ACsMod.LoadedMods.Clear();
|
||||||
|
|
||||||
if (Thread.CurrentThread == GameMain.MainThread)
|
if (Thread.CurrentThread == GameMain.MainThread)
|
||||||
{
|
{
|
||||||
Hook?.Call("stop");
|
Hook?.Call("stop");
|
||||||
}
|
}
|
||||||
|
|
||||||
Game?.Stop();
|
Game?.Stop();
|
||||||
|
|
||||||
Hook.Clear();
|
Hook.Clear();
|
||||||
ModStore.Clear();
|
ModStore.Clear();
|
||||||
Game = new LuaGame();
|
Game = new LuaGame();
|
||||||
Networking = new LuaCsNetworking();
|
Networking = new LuaCsNetworking();
|
||||||
Timer = new LuaCsTimer();
|
Timer = new LuaCsTimer();
|
||||||
Steam = new LuaCsSteam();
|
Steam = new LuaCsSteam();
|
||||||
PerformanceCounter = new LuaCsPerformanceCounter();
|
PerformanceCounter = new LuaCsPerformanceCounter();
|
||||||
LuaScriptLoader = null;
|
LuaScriptLoader = null;
|
||||||
lua = null;
|
lua = null;
|
||||||
CsScript = null;
|
CsScript = null;
|
||||||
Config = null;
|
Config = null;
|
||||||
|
|
||||||
if (CsScriptLoader != null)
|
if (CsScriptLoader != null)
|
||||||
{
|
{
|
||||||
CsScriptLoader.Clear();
|
CsScriptLoader.Clear();
|
||||||
CsScriptLoader.Unload();
|
CsScriptLoader.Unload();
|
||||||
CsScriptLoader = null;
|
CsScriptLoader = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Initialize()
|
public void Initialize()
|
||||||
{
|
{
|
||||||
Stop();
|
Stop();
|
||||||
|
|
||||||
PrintMessage("Lua! Version " + AssemblyInfo.GitRevision);
|
PrintMessage("Lua! Version " + AssemblyInfo.GitRevision);
|
||||||
|
|
||||||
|
|
||||||
if (File.Exists(configFileName))
|
if (File.Exists(configFileName))
|
||||||
{
|
{
|
||||||
using (var file = File.Open(configFileName, FileMode.Open, FileAccess.Read))
|
using (var file = File.Open(configFileName, FileMode.Open, FileAccess.Read))
|
||||||
Config = LuaCsConfig.Load<LuaCsSetupConfig>(file);
|
Config = LuaCsConfig.Load<LuaCsSetupConfig>(file);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Config = new LuaCsSetupConfig();
|
Config = new LuaCsSetupConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool csActive = GetPackage("CsForBarotrauma", false, true) != null;
|
bool csActive = GetPackage(CsForBarotraumaId, false, true) != null;
|
||||||
|
|
||||||
LuaScriptLoader = new LuaScriptLoader();
|
LuaScriptLoader = new LuaScriptLoader();
|
||||||
LuaScriptLoader.ModulePaths = new string[] { };
|
LuaScriptLoader.ModulePaths = new string[] { };
|
||||||
|
|
||||||
RegisterLuaConverters();
|
RegisterLuaConverters();
|
||||||
|
|
||||||
lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.Debug);
|
lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.Debug);
|
||||||
lua.Options.DebugPrint = PrintMessage;
|
lua.Options.DebugPrint = PrintMessage;
|
||||||
lua.Options.ScriptLoader = LuaScriptLoader;
|
lua.Options.ScriptLoader = LuaScriptLoader;
|
||||||
lua.Options.CheckThreadAccess = false;
|
lua.Options.CheckThreadAccess = false;
|
||||||
CsScript = new CsScriptRunner(this);
|
CsScript = new CsScriptRunner(this);
|
||||||
|
|
||||||
require = new LuaRequire(lua);
|
require = new LuaRequire(lua);
|
||||||
|
|
||||||
Game = new LuaGame();
|
Game = new LuaGame();
|
||||||
Networking = new LuaCsNetworking();
|
Networking = new LuaCsNetworking();
|
||||||
Timer = new LuaCsTimer();
|
Timer = new LuaCsTimer();
|
||||||
Steam = new LuaCsSteam();
|
Steam = new LuaCsSteam();
|
||||||
PerformanceCounter = new LuaCsPerformanceCounter();
|
PerformanceCounter = new LuaCsPerformanceCounter();
|
||||||
Hook.Initialize();
|
Hook.Initialize();
|
||||||
ModStore.Initialize();
|
ModStore.Initialize();
|
||||||
|
|
||||||
UserData.RegisterType<LuaCsConfig>();
|
UserData.RegisterType<LuaCsConfig>();
|
||||||
UserData.RegisterType<LuaCsAction>();
|
UserData.RegisterType<LuaCsAction>();
|
||||||
UserData.RegisterType<LuaCsFile>();
|
UserData.RegisterType<LuaCsFile>();
|
||||||
UserData.RegisterType<LuaCsCompatPatchFunc>();
|
UserData.RegisterType<LuaCsCompatPatchFunc>();
|
||||||
UserData.RegisterType<LuaCsPatchFunc>();
|
UserData.RegisterType<LuaCsPatchFunc>();
|
||||||
UserData.RegisterType<LuaCsConfig>();
|
UserData.RegisterType<LuaCsConfig>();
|
||||||
UserData.RegisterType<CsScriptRunner>();
|
UserData.RegisterType<CsScriptRunner>();
|
||||||
UserData.RegisterType<LuaGame>();
|
UserData.RegisterType<LuaGame>();
|
||||||
UserData.RegisterType<LuaCsTimer>();
|
UserData.RegisterType<LuaCsTimer>();
|
||||||
UserData.RegisterType<LuaCsFile>();
|
UserData.RegisterType<LuaCsFile>();
|
||||||
UserData.RegisterType<LuaCsNetworking>();
|
UserData.RegisterType<LuaCsNetworking>();
|
||||||
UserData.RegisterType<LuaCsSteam>();
|
UserData.RegisterType<LuaCsSteam>();
|
||||||
UserData.RegisterType<LuaUserData>();
|
UserData.RegisterType<LuaUserData>();
|
||||||
UserData.RegisterType<LuaCsPerformanceCounter>();
|
UserData.RegisterType<LuaCsPerformanceCounter>();
|
||||||
UserData.RegisterType<IUserDataDescriptor>();
|
UserData.RegisterType<IUserDataDescriptor>();
|
||||||
|
|
||||||
lua.Globals["printerror"] = (Action<object>)PrintLuaError;
|
lua.Globals["printerror"] = (Action<object>)PrintLuaError;
|
||||||
|
|
||||||
lua.Globals["setmodulepaths"] = (Action<string[]>)SetModulePaths;
|
lua.Globals["setmodulepaths"] = (Action<string[]>)SetModulePaths;
|
||||||
|
|
||||||
lua.Globals["dofile"] = (Func<string, Table, string, DynValue>)DoFile;
|
lua.Globals["dofile"] = (Func<string, Table, string, DynValue>)DoFile;
|
||||||
lua.Globals["loadfile"] = (Func<string, Table, string, DynValue>)LoadFile;
|
lua.Globals["loadfile"] = (Func<string, Table, string, DynValue>)LoadFile;
|
||||||
lua.Globals["require"] = (Func<string, Table, DynValue>)require.Require;
|
lua.Globals["require"] = (Func<string, Table, DynValue>)require.Require;
|
||||||
|
|
||||||
lua.Globals["dostring"] = (Func<string, Table, string, DynValue>)lua.DoString;
|
lua.Globals["dostring"] = (Func<string, Table, string, DynValue>)lua.DoString;
|
||||||
lua.Globals["load"] = (Func<string, Table, string, DynValue>)lua.LoadString;
|
lua.Globals["load"] = (Func<string, Table, string, DynValue>)lua.LoadString;
|
||||||
|
|
||||||
lua.Globals["CsScript"] = CsScript;
|
lua.Globals["CsScript"] = CsScript;
|
||||||
lua.Globals["LuaUserData"] = UserData.CreateStatic<LuaUserData>();
|
lua.Globals["LuaUserData"] = UserData.CreateStatic<LuaUserData>();
|
||||||
lua.Globals["Game"] = Game;
|
lua.Globals["Game"] = Game;
|
||||||
lua.Globals["Hook"] = Hook;
|
lua.Globals["Hook"] = Hook;
|
||||||
lua.Globals["ModStore"] = ModStore;
|
lua.Globals["ModStore"] = ModStore;
|
||||||
lua.Globals["Timer"] = Timer;
|
lua.Globals["Timer"] = Timer;
|
||||||
lua.Globals["File"] = UserData.CreateStatic<LuaCsFile>();
|
lua.Globals["File"] = UserData.CreateStatic<LuaCsFile>();
|
||||||
lua.Globals["Networking"] = Networking;
|
lua.Globals["Networking"] = Networking;
|
||||||
lua.Globals["Steam"] = Steam;
|
lua.Globals["Steam"] = Steam;
|
||||||
lua.Globals["PerformanceCounter"] = PerformanceCounter;
|
lua.Globals["PerformanceCounter"] = PerformanceCounter;
|
||||||
|
|
||||||
lua.Globals["ExecutionNumber"] = executionNumber;
|
lua.Globals["ExecutionNumber"] = executionNumber;
|
||||||
lua.Globals["CSActive"] = csActive;
|
lua.Globals["CSActive"] = csActive;
|
||||||
|
|
||||||
lua.Globals["SERVER"] = IsServer;
|
lua.Globals["SERVER"] = IsServer;
|
||||||
lua.Globals["CLIENT"] = IsClient;
|
lua.Globals["CLIENT"] = IsClient;
|
||||||
|
|
||||||
if (csActive)
|
if (csActive)
|
||||||
{
|
{
|
||||||
PrintMessage("Cs! Version " + AssemblyInfo.GitRevision);
|
PrintMessage("Cs! Version " + AssemblyInfo.GitRevision);
|
||||||
|
|
||||||
if (Config.FirstTimeCsWarning)
|
if (Config.FirstTimeCsWarning)
|
||||||
{
|
{
|
||||||
Config.FirstTimeCsWarning = false;
|
Config.FirstTimeCsWarning = false;
|
||||||
UpdateConfig();
|
UpdateConfig();
|
||||||
|
|
||||||
DebugConsole.AddWarning("Cs package active! Cs mods are NOT sandboxed, use it at your own risk!");
|
DebugConsole.AddWarning("Cs package active! Cs mods are NOT sandboxed, use it at your own risk!");
|
||||||
}
|
}
|
||||||
|
|
||||||
CsScriptLoader = new CsScriptLoader();
|
CsScriptLoader = new CsScriptLoader();
|
||||||
CsScriptLoader.SearchFolders();
|
CsScriptLoader.SearchFolders();
|
||||||
if (CsScriptLoader.HasSources)
|
if (CsScriptLoader.HasSources)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var modTypes = CsScriptLoader.Compile();
|
var modTypes = CsScriptLoader.Compile();
|
||||||
modTypes.ForEach(t =>
|
modTypes.ForEach(t =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
|
||||||
t.GetConstructor(new Type[] { })?.Invoke(null);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
HandleException(ex, LuaCsMessageOrigin.CSharpMod);
|
t.GetConstructor(new Type[] { })?.Invoke(null);
|
||||||
}
|
}
|
||||||
});
|
catch (Exception ex)
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
HandleException(ex, LuaCsMessageOrigin.CSharpMod);
|
||||||
{
|
}
|
||||||
HandleException(ex, LuaCsMessageOrigin.CSharpMod);
|
});
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
HandleException(ex, LuaCsMessageOrigin.CSharpMod);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ContentPackage luaPackage = GetPackage("Lua For Barotrauma");
|
ContentPackage luaPackage = GetPackage(LuaForBarotraumaId);
|
||||||
|
|
||||||
if (File.Exists(LuaSetupFile))
|
if (File.Exists(LuaSetupFile))
|
||||||
{
|
{
|
||||||
PrintMessage("Using LuaSetup.lua from the Barotrauma Lua/ folder.");
|
PrintMessage("Using LuaSetup.lua from the Barotrauma Lua/ folder.");
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
DynValue function = lua.LoadFile(LuaSetupFile);
|
DynValue function = lua.LoadFile(LuaSetupFile);
|
||||||
CallLuaFunction(function, Path.GetDirectoryName(Path.GetFullPath(LuaSetupFile)));
|
CallLuaFunction(function, Path.GetDirectoryName(Path.GetFullPath(LuaSetupFile)));
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
HandleException(e, LuaCsMessageOrigin.LuaMod);
|
HandleException(e, LuaCsMessageOrigin.LuaMod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (luaPackage != null)
|
else if (luaPackage != null)
|
||||||
{
|
{
|
||||||
PrintMessage("Using LuaSetup.lua from the content package.");
|
PrintMessage("Using LuaSetup.lua from the content package.");
|
||||||
|
|
||||||
string path = Path.GetDirectoryName(luaPackage.Path);
|
string path = Path.GetDirectoryName(luaPackage.Path);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string luaPath = Path.Combine(path, "Binary/Lua/LuaSetup.lua");
|
string luaPath = Path.Combine(path, "Binary/Lua/LuaSetup.lua");
|
||||||
CallLuaFunction(lua.LoadFile(luaPath), Path.GetDirectoryName(Path.GetFullPath(luaPath)));
|
CallLuaFunction(lua.LoadFile(luaPath), Path.GetDirectoryName(Path.GetFullPath(luaPath)));
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
HandleException(e, LuaCsMessageOrigin.LuaMod);
|
HandleException(e, LuaCsMessageOrigin.LuaMod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
PrintLuaError("LuaSetup.lua not found! Lua/LuaSetup.lua, no Lua scripts will be executed or work.");
|
PrintLuaError("LuaSetup.lua not found! Lua/LuaSetup.lua, no Lua scripts will be executed or work.");
|
||||||
}
|
}
|
||||||
|
|
||||||
executionNumber++;
|
executionNumber++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,15 +55,14 @@ namespace Barotrauma
|
|||||||
|
|
||||||
bool pathStartsWith(string prefix) => path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase);
|
bool pathStartsWith(string prefix) => path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
if (pathStartsWith(getFullPath(ContentPackage.LocalModsDir + "Lua For Barotrauma")))
|
foreach (var package in ContentPackageManager.AllPackages)
|
||||||
return false;
|
{
|
||||||
|
if (package.UgcId.ValueEquals(LuaCsSetup.LuaForBarotraumaId) && pathStartsWith(getFullPath(package.Path)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (pathStartsWith(getFullPath(ContentPackage.WorkshopModsDir + "Lua For Barotrauma")))
|
|
||||||
return false;
|
|
||||||
#if CLIENT
|
|
||||||
if (pathStartsWith(getFullPath(ModReceiver.DownloadFolder + "Lua For Barotrauma")))
|
|
||||||
return false;
|
|
||||||
#endif
|
|
||||||
if (pathStartsWith(getFullPath(SaveUtil.SaveFolder)))
|
if (pathStartsWith(getFullPath(SaveUtil.SaveFolder)))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user