Refactor and fix #56

This commit is contained in:
Evil Factory
2022-04-15 19:05:24 -03:00
parent 6eb575ea81
commit 42df433d3e
35 changed files with 144 additions and 97 deletions
@@ -2723,13 +2723,13 @@ namespace Barotrauma
{
for (int i = 0; i < CharacterList.Count; i++)
{
if (GameMain.LuaCs.game.updatePriorityCharacters.Contains(CharacterList[i])) continue;
if (GameMain.LuaCs.Game.updatePriorityCharacters.Contains(CharacterList[i])) continue;
CharacterList[i].Update(deltaTime * CharacterUpdateInterval, cam);
}
}
foreach (Character character in GameMain.LuaCs.game.updatePriorityCharacters)
foreach (Character character in GameMain.LuaCs.Game.updatePriorityCharacters)
{
if (character.Removed) continue;
@@ -302,7 +302,7 @@ namespace Barotrauma
if (Prefab is AfflictionPrefabHusk huskPrefab)
{
if (huskPrefab.ControlHusk || GameMain.LuaCs.game.enableControlHusk)
if (huskPrefab.ControlHusk || GameMain.LuaCs.Game.enableControlHusk)
{
#if SERVER
if (client != null)
@@ -74,7 +74,7 @@ namespace Barotrauma.Items.Components
#if SERVER
get
{
if(GameMain.LuaCs.game.allowWifiChat) return true;
if(GameMain.LuaCs.Game.allowWifiChat) return true;
return linkToChat;
}
@@ -47,6 +47,8 @@ namespace Barotrauma
}
}
private Harmony harmony;
private Dictionary<string, Dictionary<string, (LuaCsHookCallback, ACsMod)>> hookFunctions;
private Dictionary<long, HashSet<(string, CsPatch, ACsMod)>> hookPrefixMethods;
@@ -54,7 +56,11 @@ namespace Barotrauma
private Queue<(float, CsAction, object[])> queuedFunctionCalls;
private LuaCsHook() {
private static LuaCsHook instance;
public LuaCsHook() {
instance = this;
hookFunctions = new Dictionary<string, Dictionary<string, (LuaCsHookCallback, ACsMod)>>();
hookPrefixMethods = new Dictionary<long, HashSet<(string, CsPatch, ACsMod)>>();
@@ -63,10 +69,12 @@ namespace Barotrauma
queuedFunctionCalls = new Queue<(float, CsAction, object[])>();
}
private static LuaCsHook _inst;
static LuaCsHook() => _inst = new LuaCsHook();
public static LuaCsHook Instance { get => _inst; }
public void Initialize()
{
harmony = new Harmony("LuaCsForBarotrauma");
}
private static void _hookLuaCsPatch(MethodBase __originalMethod, object[] __args, object __instance, out object result, HookMethodType hookMethodType)
{
result = null;
@@ -81,10 +89,10 @@ namespace Barotrauma
switch (hookMethodType)
{
case HookMethodType.Before:
_inst.hookPrefixMethods.TryGetValue(funcAddr, out methodSet);
instance.hookPrefixMethods.TryGetValue(funcAddr, out methodSet);
break;
case HookMethodType.After:
_inst.hookPostfixMethods.TryGetValue(funcAddr, out methodSet);
instance.hookPostfixMethods.TryGetValue(funcAddr, out methodSet);
break;
default:
break;
@@ -196,7 +204,6 @@ namespace Barotrauma
public void HookMethod(string identifier, MethodInfo method, CsPatch patch, HookMethodType hookType = HookMethodType.Before, ACsMod owner = null)
{
Console.WriteLine($" --== '{identifier}' {method.ReflectedType.Name}.{method.Name} -> {method.ReturnType.Name} | {hookType.ToString("G")}");
if (identifier == null || method == null || patch == null) throw new ArgumentNullException("Identifier, Method and Patch arguments must not be null.");
var funcAddr = ((long)method.MethodHandle.GetFunctionPointer());
@@ -208,14 +215,14 @@ namespace Barotrauma
{
if (patches == null || patches.Prefixes == null || patches.Prefixes.Find(patch => patch.PatchMethod == _miHookLuaCsPatchRetPrefix) == null)
{
GameMain.LuaCs.harmony.Patch(method, prefix: new HarmonyMethod(_miHookLuaCsPatchRetPrefix));
harmony.Patch(method, prefix: new HarmonyMethod(_miHookLuaCsPatchRetPrefix));
}
}
else
{
if (patches == null || patches.Prefixes == null || patches.Prefixes.Find(patch => patch.PatchMethod == _miHookLuaCsPatchPrefix) == null)
{
GameMain.LuaCs.harmony.Patch(method, prefix: new HarmonyMethod(_miHookLuaCsPatchPrefix));
harmony.Patch(method, prefix: new HarmonyMethod(_miHookLuaCsPatchPrefix));
}
}
@@ -236,14 +243,14 @@ namespace Barotrauma
{
if (patches == null || patches.Postfixes == null || patches.Postfixes.Find(patch => patch.PatchMethod == _miHookLuaCsPatchRetPostfix) == null)
{
GameMain.LuaCs.harmony.Patch(method, postfix: new HarmonyMethod(_miHookLuaCsPatchRetPostfix));
harmony.Patch(method, postfix: new HarmonyMethod(_miHookLuaCsPatchRetPostfix));
}
}
else
{
if (patches == null || patches.Postfixes == null || patches.Postfixes.Find(patch => patch.PatchMethod == _miHookLuaCsPatchPostfix) == null)
{
GameMain.LuaCs.harmony.Patch(method, postfix: new HarmonyMethod(_miHookLuaCsPatchPostfix));
harmony.Patch(method, postfix: new HarmonyMethod(_miHookLuaCsPatchPostfix));
}
}
@@ -312,7 +319,6 @@ namespace Barotrauma
{
name = name.ToLower();
LuaCsSetup.PrintLogMessage($"'{name}' | '{hookName}'");
if (name == null || hookName == null || hook == null) throw new ArgumentNullException("Names and Hook must not be null");
if (!hookFunctions.ContainsKey(name))
@@ -340,7 +346,7 @@ namespace Barotrauma
queuedFunctionCalls.Clear();
GameMain.LuaCs.harmony?.UnpatchAll();
harmony?.UnpatchAll();
}
@@ -24,19 +24,18 @@ namespace Barotrauma
internal LuaCsHook Hook { get; private set; }
public LuaGame game;
public LuaCsNetworking networking;
public Harmony harmony;
public LuaGame Game;
public LuaCsNetworking Networking;
public LuaScriptLoader luaScriptLoader;
public CsScriptLoader netScriptLoader;
public LuaScriptLoader LuaScriptLoader;
public CsScriptLoader NetScriptLoader;
public LuaCsSetup()
{
Hook = LuaCsHook.Instance;
Hook = new LuaCsHook();
game = new LuaGame();
networking = new LuaCsNetworking();
Game = new LuaGame();
Networking = new LuaCsNetworking();
}
@@ -265,7 +264,7 @@ namespace Barotrauma
public void SetModulePaths(string[] str)
{
luaScriptLoader.ModulePaths = str;
LuaScriptLoader.ModulePaths = str;
}
public void Update()
@@ -279,31 +278,12 @@ namespace Barotrauma
ACsMod.LoadedMods.Clear();
Hook?.Call("stop");
game?.Stop();
//harmony?.UnpatchAll();
Game?.Stop();
//Hook = new LuaCsHook();
Hook.Clear();
game = new LuaGame();
networking = new LuaCsNetworking();
luaScriptLoader = null;
}
private void InitCs()
{
netScriptLoader = new CsScriptLoader(this);
netScriptLoader.SearchFolders();
if (netScriptLoader == null) throw new Exception("LuaCsSetup was not properly initialized.");
try
{
var modTypes = netScriptLoader.Compile();
//modTypes.ForEach(t => ACsMod.CreateInstance(t));
modTypes.ForEach(t => t.GetConstructor(new Type[] { })?.Invoke(null));
}
catch (Exception ex)
{
PrintMessage(ex);
}
Game = new LuaGame();
Networking = new LuaCsNetworking();
LuaScriptLoader = null;
}
public void Initialize()
@@ -312,32 +292,27 @@ namespace Barotrauma
PrintMessage("LuaCs! Version " + AssemblyInfo.GitRevision);
luaScriptLoader = new LuaScriptLoader();
luaScriptLoader.ModulePaths = new string[] { };
InitCs();
LuaScriptLoader = new LuaScriptLoader();
LuaScriptLoader.ModulePaths = new string[] { };
NetScriptLoader = new CsScriptLoader(this);
LuaCustomConverters.RegisterAll();
lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.Debug);
lua.Options.DebugPrint = PrintMessage;
lua.Options.ScriptLoader = luaScriptLoader;
lua.Options.ScriptLoader = LuaScriptLoader;
harmony = new Harmony("com.LuaForBarotrauma");
harmony.UnpatchAll();
Hook.Initialize();
Game = new LuaGame();
Networking = new LuaCsNetworking();
//Hook = new LuaCsHook();
game = new LuaGame();
networking = new LuaCsNetworking();
//UserData.RegisterType<LuaCsHook>();
UserData.RegisterType<LuaGame>();
UserData.RegisterType<LuaCsTimer>();
UserData.RegisterType<LuaCsFile>();
UserData.RegisterType<LuaCsNetworking>();
UserData.RegisterType<LuaUserData>();
UserData.RegisterType<IUserDataDescriptor>();
lua.Globals["printerror"] = (Action<object>)PrintError;
var hookType = UserData.RegisterType<LuaCsHook>();
var hookDesc = (StandardUserDataDescriptor)hookType;
@@ -353,6 +328,8 @@ namespace Barotrauma
}
});
lua.Globals["printerror"] = (Action<object>)PrintError;
lua.Globals["setmodulepaths"] = (Action<string[]>)SetModulePaths;
lua.Globals["dofile"] = (Func<string, Table, string, DynValue>)DoFile;
@@ -363,11 +340,11 @@ namespace Barotrauma
lua.Globals["load"] = (Func<string, Table, string, DynValue>)LoadString;
lua.Globals["LuaUserData"] = UserData.CreateStatic<LuaUserData>();
lua.Globals["Game"] = game;
lua.Globals["Game"] = Game;
lua.Globals["Hook"] = Hook;
lua.Globals["Timer"] = new LuaCsTimer();
lua.Globals["File"] = UserData.CreateStatic<LuaCsFile>();
lua.Globals["Networking"] = networking;
lua.Globals["Networking"] = Networking;
bool isServer;
@@ -382,6 +359,20 @@ namespace Barotrauma
// LuaDocs.GenerateDocsAll();
NetScriptLoader.SearchFolders();
if (NetScriptLoader == null) throw new Exception("LuaCsSetup was not properly initialized.");
try
{
var modTypes = NetScriptLoader.Compile();
modTypes.ForEach(t => t.GetConstructor(new Type[] { })?.Invoke(null));
}
catch (Exception ex)
{
PrintMessage(ex);
}
ContentPackage luaPackage = GetPackage();
if (File.Exists(LUASETUP_FILE))
@@ -411,7 +402,7 @@ namespace Barotrauma
}
else
{
PrintError("LuaCs loader not found! Lua/LuaSetup.lua, no Lua scripts will be executed or work.");
PrintError("LuaSetup.lua not found! Lua/LuaSetup.lua, no Lua scripts will be executed or work.");
}
}
@@ -596,12 +596,12 @@ namespace Barotrauma
Powered.UpdatePower(deltaTime * MapEntityUpdateInterval);
foreach (Item item in Item.ItemList)
{
if (GameMain.LuaCs.game.updatePriorityItems.Contains(item)) continue;
if (GameMain.LuaCs.Game.updatePriorityItems.Contains(item)) continue;
item.Update(deltaTime * MapEntityUpdateInterval, cam);
}
}
foreach (var item in GameMain.LuaCs.game.updatePriorityItems)
foreach (var item in GameMain.LuaCs.Game.updatePriorityItems)
{
if (item.Removed) continue;
@@ -117,7 +117,7 @@ namespace Barotrauma.Networking
foreach (Wire wire in connection.Wires)
{
#if SERVER
if (GameMain.LuaCs.game.overrideRespawnSub == false)
if (GameMain.LuaCs.Game.overrideRespawnSub == false)
{
if (wire != null) wire.Locked = true;
}
@@ -205,7 +205,7 @@ namespace Barotrauma.Networking
{
#if SERVER
if (GameMain.LuaCs.game.overrideRespawnSub)
if (GameMain.LuaCs.Game.overrideRespawnSub)
{
yield return CoroutineStatus.Success;
}