diff --git a/Barotrauma/BarotraumaClient/LinuxClient.csproj b/Barotrauma/BarotraumaClient/LinuxClient.csproj index 6c3780e94..041fa9654 100644 --- a/Barotrauma/BarotraumaClient/LinuxClient.csproj +++ b/Barotrauma/BarotraumaClient/LinuxClient.csproj @@ -121,12 +121,18 @@ - + + + + ..\..\Libraries\0Harmony.dll + + + diff --git a/Barotrauma/BarotraumaClient/MacClient.csproj b/Barotrauma/BarotraumaClient/MacClient.csproj index 6640759d6..3ef501bc4 100644 --- a/Barotrauma/BarotraumaClient/MacClient.csproj +++ b/Barotrauma/BarotraumaClient/MacClient.csproj @@ -122,7 +122,7 @@ - + @@ -135,6 +135,11 @@ PreserveNewest + + + ..\..\Libraries\0Harmony.dll + + PreserveNewest diff --git a/Barotrauma/BarotraumaClient/WindowsClient.csproj b/Barotrauma/BarotraumaClient/WindowsClient.csproj index 0a16bfdca..b54d4721f 100644 --- a/Barotrauma/BarotraumaClient/WindowsClient.csproj +++ b/Barotrauma/BarotraumaClient/WindowsClient.csproj @@ -125,12 +125,18 @@ - + + + + ..\..\Libraries\0Harmony.dll + + + Never diff --git a/Barotrauma/BarotraumaServer/LinuxServer.csproj b/Barotrauma/BarotraumaServer/LinuxServer.csproj index c25ba6e32..bc8f50286 100644 --- a/Barotrauma/BarotraumaServer/LinuxServer.csproj +++ b/Barotrauma/BarotraumaServer/LinuxServer.csproj @@ -74,9 +74,14 @@ - + + + + ..\..\Libraries\0Harmony.dll + + diff --git a/Barotrauma/BarotraumaServer/MacServer.csproj b/Barotrauma/BarotraumaServer/MacServer.csproj index 248219178..a2d5ddbc8 100644 --- a/Barotrauma/BarotraumaServer/MacServer.csproj +++ b/Barotrauma/BarotraumaServer/MacServer.csproj @@ -89,9 +89,14 @@ - + + + + ..\..\Libraries\0Harmony.dll + + diff --git a/Barotrauma/BarotraumaServer/WindowsServer.csproj b/Barotrauma/BarotraumaServer/WindowsServer.csproj index 5a0f09221..9398e9cc8 100644 --- a/Barotrauma/BarotraumaServer/WindowsServer.csproj +++ b/Barotrauma/BarotraumaServer/WindowsServer.csproj @@ -83,9 +83,15 @@ - + + + + + ..\..\Libraries\0Harmony.dll + + diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultLib.lua b/Barotrauma/BarotraumaShared/Lua/DefaultLib.lua index acd20433e..ec24efded 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultLib.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultLib.lua @@ -53,4 +53,4 @@ elseif CLIENT then defaultLib["PlayerInput"] = CreateStatic("PlayerInput") end -return defaultLib \ No newline at end of file +return defaultLib diff --git a/Barotrauma/BarotraumaShared/Lua/DefaultRegister.lua b/Barotrauma/BarotraumaShared/Lua/DefaultRegister.lua index 887a1ba01..5f86c37b6 100644 --- a/Barotrauma/BarotraumaShared/Lua/DefaultRegister.lua +++ b/Barotrauma/BarotraumaShared/Lua/DefaultRegister.lua @@ -4,6 +4,8 @@ end local AddCallMetaMember = LuaUserData.AddCallMetaMember +LuaUserData.RegisterType("System.TimeSpan") + RegisterBarotrauma("CauseOfDeathType") RegisterBarotrauma("Level+InterestingPosition") RegisterBarotrauma("Level+PositionType") @@ -88,7 +90,10 @@ RegisterBarotrauma("GameSession") RegisterBarotrauma("CampaignMode") local descriptor = RegisterBarotrauma("NetLobbyScreen") -LuaUserData.MakeFieldAccessible(descriptor, "subs") + +if SERVER then + LuaUserData.MakeFieldAccessible(descriptor, "subs") +end RegisterBarotrauma("Networking.IWriteMessage") RegisterBarotrauma("Networking.IReadMessage") diff --git a/Barotrauma/BarotraumaShared/Lua/LuaSetup.lua b/Barotrauma/BarotraumaShared/Lua/LuaSetup.lua index cd83b391e..cd058681c 100644 --- a/Barotrauma/BarotraumaShared/Lua/LuaSetup.lua +++ b/Barotrauma/BarotraumaShared/Lua/LuaSetup.lua @@ -1,15 +1,18 @@ -- Config - local runDisabledMods = false local modulePaths = {"Lua/?.lua"} setmodulepaths(modulePaths) +-- Setup Libraries + local defaultLib = require("DefaultLib") for key, value in pairs(defaultLib) do _G[key] = value end +-- Execute Mods + if SERVER and Game.IsDedicated then runDisabledMods = true @@ -17,9 +20,9 @@ if SERVER and Game.IsDedicated then end if runDisabledMods then - print("LUA LOADER: Mods will be executed regardless being enabled or not. Lua/MoonsharpSetup.lua") + print("LUA LOADER: Mods will be executed regardless being enabled or not. Lua/LuaSetup.lua") else - print("LUA LOADER: Only enabled mods will be executed. Lua/MoonsharpSetup.lua") + print("LUA LOADER: Only enabled mods will be executed. Lua/LuaSetup.lua") end local enabledPackages = Game.GetEnabledContentPackages() diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs index 3ea2b743e..2f71c8f2c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs @@ -15,6 +15,7 @@ using System.Reflection; using HarmonyLib; using MoonSharp.Interpreter.Interop; using System.Diagnostics; +using System.Reflection.Emit; namespace Barotrauma { @@ -714,31 +715,62 @@ namespace Barotrauma Before, After } - static void HookLuaPatchPrefix(MethodBase __originalMethod) + static bool HookLuaPatchPrefix(MethodBase __originalMethod, object[] __params) { - luaSetup.hook.Call(methodNameToHookName[__originalMethod.Name]); + var result = new LuaResult(luaSetup.hook.Call(methodNameToHookName[__originalMethod.Name], __params)); + + if (!result.IsNull()) + { + return false; + } + + return true; } - static void HookLuaPatchPostfix(MethodBase __originalMethod) + static bool HookLuaPatchRetPrefix(MethodBase __originalMethod, object[] __params, ref object __result) { - luaSetup.hook.Call(methodNameToHookName[__originalMethod.Name]); + var result = new LuaResult(luaSetup.hook.Call(methodNameToHookName[__originalMethod.Name], __params)); + + if (!result.IsNull()) + { + __result = result.Object(); + return false; + } + + return true; } - // not very useful until i find a way to use the transpiler to inject arguments into the call + static void HookLuaPatchPostfix(MethodBase __originalMethod, object[] __params) + { + var result = new LuaResult(luaSetup.hook.Call(methodNameToHookName[__originalMethod.Name], __params)); + } + + static void HookLuaPatchRetPostfix(MethodBase __originalMethod, object[] __params, ref object __result) + { + var result = new LuaResult(luaSetup.hook.Call(methodNameToHookName[__originalMethod.Name], __params)); + + if (!result.IsNull()) + __result = result.Object(); + } public void HookMethod(string className, string methodName, string hookName, HookMethodType hookMethodType = HookMethodType.Before) { var classType = Type.GetType(className); var methodInfos = classType.GetMethods(); HarmonyMethod harmonyMethod = new HarmonyMethod(); + HarmonyMethod harmonyMethodRet = new HarmonyMethod(); if (hookMethodType == HookMethodType.Before) { harmonyMethod = new HarmonyMethod(GetType().GetMethod("HookLuaPatchPrefix", BindingFlags.NonPublic | BindingFlags.Static)); + + harmonyMethodRet = new HarmonyMethod(GetType().GetMethod("HookLuaPatchRetPrefix", BindingFlags.NonPublic | BindingFlags.Static)); } else if (hookMethodType == HookMethodType.After) { harmonyMethod = new HarmonyMethod(GetType().GetMethod("HookLuaPatchPostfix", BindingFlags.NonPublic | BindingFlags.Static)); + + harmonyMethodRet = new HarmonyMethod(GetType().GetMethod("HookLuaPatchRetPrefix", BindingFlags.NonPublic | BindingFlags.Static)); } var foundAny = false; @@ -748,9 +780,17 @@ namespace Barotrauma if(methodInfo.Name == methodName) { if (hookMethodType == HookMethodType.Before) - env.harmony.Patch(methodInfo, harmonyMethod); + if (methodInfo.ReturnType == typeof(void)) + env.harmony.Patch(methodInfo, harmonyMethod); + else + env.harmony.Patch(methodInfo, harmonyMethodRet); + + else if (hookMethodType == HookMethodType.After) - env.harmony.Patch(methodInfo, postfix: harmonyMethod); + if (methodInfo.ReturnType == typeof(void)) + env.harmony.Patch(methodInfo, postfix: harmonyMethod); + else + env.harmony.Patch(methodInfo, postfix: harmonyMethodRet); foundAny = true; } @@ -767,7 +807,6 @@ namespace Barotrauma if (!hookFunctions.ContainsKey(name)) hookFunctions.Add(name, new Dictionary()); - hookFunctions[name][hookName] = new HookFunction(name, hookName, function); } @@ -874,5 +913,15 @@ namespace Barotrauma return 0f; } + + public object Object() + { + if(result is DynValue dynValue) + { + return dynValue.ToObject(); + } + + return null; + } } } \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs index 35afb201b..9686b150b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaSetup.cs @@ -320,6 +320,7 @@ namespace Barotrauma UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType(); + UserData.RegisterType(); UserData.RegisterType(); UserData.RegisterType>(); @@ -330,6 +331,11 @@ namespace Barotrauma UserData.RegisterType>(); +#if SERVER + +#elif CLIENT + UserData.RegisterType(); +#endif lua.Globals["setmodulepaths"] = (Action)SetModulePaths; diff --git a/Libraries/0Harmony.dll b/Libraries/0Harmony.dll new file mode 100644 index 000000000..730fdb783 Binary files /dev/null and b/Libraries/0Harmony.dll differ