replace harmony with a custom one that includes __params, some fixes in the lua setup and hook method is now actually useful

This commit is contained in:
Evil Factory
2021-11-05 20:40:50 -03:00
parent a8a0c96d57
commit 72fff4ab73
12 changed files with 115 additions and 19 deletions
@@ -121,12 +121,18 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.1.1" /> <PackageReference Include="MonoMod.Common" Version="21.10.22.1" />
<PackageReference Include="MoonSharp" Version="2.0.0" /> <PackageReference Include="MoonSharp" Version="2.0.0" />
<PackageReference Include="NVorbis" Version="0.8.6" /> <PackageReference Include="NVorbis" Version="0.8.6" />
<PackageReference Include="RestSharp" Version="106.6.10" /> <PackageReference Include="RestSharp" Version="106.6.10" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<!-- Sourced from https://stackoverflow.com/a/45248069 --> <!-- Sourced from https://stackoverflow.com/a/45248069 -->
<Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''"> <Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''">
<PropertyGroup> <PropertyGroup>
+6 -1
View File
@@ -122,7 +122,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.1.1" /> <PackageReference Include="MonoMod.Common" Version="21.10.22.1" />
<PackageReference Include="MoonSharp" Version="2.0.0" /> <PackageReference Include="MoonSharp" Version="2.0.0" />
<PackageReference Include="NVorbis" Version="0.8.6" /> <PackageReference Include="NVorbis" Version="0.8.6" />
<PackageReference Include="RestSharp" Version="106.6.10" /> <PackageReference Include="RestSharp" Version="106.6.10" />
@@ -135,6 +135,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="libfreetype6.dylib"> <None Update="libfreetype6.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@@ -125,12 +125,18 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.1.1" /> <PackageReference Include="MonoMod.Common" Version="21.10.22.1" />
<PackageReference Include="MoonSharp" Version="2.0.0" /> <PackageReference Include="MoonSharp" Version="2.0.0" />
<PackageReference Include="NVorbis" Version="0.8.6" /> <PackageReference Include="NVorbis" Version="0.8.6" />
<PackageReference Include="RestSharp" Version="106.6.10" /> <PackageReference Include="RestSharp" Version="106.6.10" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<Content Update="..\BarotraumaShared\Content\Lights\divinghelmetlight.psd"> <Content Update="..\BarotraumaShared\Content\Lights\divinghelmetlight.psd">
<CopyToOutputDirectory>Never</CopyToOutputDirectory> <CopyToOutputDirectory>Never</CopyToOutputDirectory>
@@ -74,9 +74,14 @@
<ProjectReference Include="..\..\Libraries\Lidgren.Network\Lidgren.NetStandard.csproj" AdditionalProperties="Configuration=Debug" /> <ProjectReference Include="..\..\Libraries\Lidgren.Network\Lidgren.NetStandard.csproj" AdditionalProperties="Configuration=Debug" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.1.1" /> <PackageReference Include="MonoMod.Common" Version="21.10.22.1" />
<PackageReference Include="MoonSharp" Version="2.0.0" /> <PackageReference Include="MoonSharp" Version="2.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<!-- Sourced from https://stackoverflow.com/a/45248069 --> <!-- Sourced from https://stackoverflow.com/a/45248069 -->
<Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''"> <Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''">
+6 -1
View File
@@ -89,9 +89,14 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.1.1" /> <PackageReference Include="MonoMod.Common" Version="21.10.22.1" />
<PackageReference Include="MoonSharp" Version="2.0.0" /> <PackageReference Include="MoonSharp" Version="2.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''"> <Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''">
<PropertyGroup> <PropertyGroup>
<!-- temp file for the git version (lives in "obj" folder)--> <!-- temp file for the git version (lives in "obj" folder)-->
@@ -83,9 +83,15 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Lib.Harmony" Version="2.1.1" /> <PackageReference Include="MonoMod.Common" Version="21.10.22.1" />
<PackageReference Include="MoonSharp" Version="2.0.0" /> <PackageReference Include="MoonSharp" Version="2.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<!-- Sourced from https://stackoverflow.com/a/45248069 --> <!-- Sourced from https://stackoverflow.com/a/45248069 -->
<Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''"> <Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''">
@@ -53,4 +53,4 @@ elseif CLIENT then
defaultLib["PlayerInput"] = CreateStatic("PlayerInput") defaultLib["PlayerInput"] = CreateStatic("PlayerInput")
end end
return defaultLib return defaultLib
@@ -4,6 +4,8 @@ end
local AddCallMetaMember = LuaUserData.AddCallMetaMember local AddCallMetaMember = LuaUserData.AddCallMetaMember
LuaUserData.RegisterType("System.TimeSpan")
RegisterBarotrauma("CauseOfDeathType") RegisterBarotrauma("CauseOfDeathType")
RegisterBarotrauma("Level+InterestingPosition") RegisterBarotrauma("Level+InterestingPosition")
RegisterBarotrauma("Level+PositionType") RegisterBarotrauma("Level+PositionType")
@@ -88,7 +90,10 @@ RegisterBarotrauma("GameSession")
RegisterBarotrauma("CampaignMode") RegisterBarotrauma("CampaignMode")
local descriptor = RegisterBarotrauma("NetLobbyScreen") local descriptor = RegisterBarotrauma("NetLobbyScreen")
LuaUserData.MakeFieldAccessible(descriptor, "subs")
if SERVER then
LuaUserData.MakeFieldAccessible(descriptor, "subs")
end
RegisterBarotrauma("Networking.IWriteMessage") RegisterBarotrauma("Networking.IWriteMessage")
RegisterBarotrauma("Networking.IReadMessage") RegisterBarotrauma("Networking.IReadMessage")
+6 -3
View File
@@ -1,15 +1,18 @@
-- Config -- Config
local runDisabledMods = false local runDisabledMods = false
local modulePaths = {"Lua/?.lua"} local modulePaths = {"Lua/?.lua"}
setmodulepaths(modulePaths) setmodulepaths(modulePaths)
-- Setup Libraries
local defaultLib = require("DefaultLib") local defaultLib = require("DefaultLib")
for key, value in pairs(defaultLib) do for key, value in pairs(defaultLib) do
_G[key] = value _G[key] = value
end end
-- Execute Mods
if SERVER and Game.IsDedicated then if SERVER and Game.IsDedicated then
runDisabledMods = true runDisabledMods = true
@@ -17,9 +20,9 @@ if SERVER and Game.IsDedicated then
end end
if runDisabledMods then 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 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 end
local enabledPackages = Game.GetEnabledContentPackages() local enabledPackages = Game.GetEnabledContentPackages()
@@ -15,6 +15,7 @@ using System.Reflection;
using HarmonyLib; using HarmonyLib;
using MoonSharp.Interpreter.Interop; using MoonSharp.Interpreter.Interop;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection.Emit;
namespace Barotrauma namespace Barotrauma
{ {
@@ -714,31 +715,62 @@ namespace Barotrauma
Before, After 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) public void HookMethod(string className, string methodName, string hookName, HookMethodType hookMethodType = HookMethodType.Before)
{ {
var classType = Type.GetType(className); var classType = Type.GetType(className);
var methodInfos = classType.GetMethods(); var methodInfos = classType.GetMethods();
HarmonyMethod harmonyMethod = new HarmonyMethod(); HarmonyMethod harmonyMethod = new HarmonyMethod();
HarmonyMethod harmonyMethodRet = new HarmonyMethod();
if (hookMethodType == HookMethodType.Before) if (hookMethodType == HookMethodType.Before)
{ {
harmonyMethod = new HarmonyMethod(GetType().GetMethod("HookLuaPatchPrefix", BindingFlags.NonPublic | BindingFlags.Static)); 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) else if (hookMethodType == HookMethodType.After)
{ {
harmonyMethod = new HarmonyMethod(GetType().GetMethod("HookLuaPatchPostfix", BindingFlags.NonPublic | BindingFlags.Static)); harmonyMethod = new HarmonyMethod(GetType().GetMethod("HookLuaPatchPostfix", BindingFlags.NonPublic | BindingFlags.Static));
harmonyMethodRet = new HarmonyMethod(GetType().GetMethod("HookLuaPatchRetPrefix", BindingFlags.NonPublic | BindingFlags.Static));
} }
var foundAny = false; var foundAny = false;
@@ -748,9 +780,17 @@ namespace Barotrauma
if(methodInfo.Name == methodName) if(methodInfo.Name == methodName)
{ {
if (hookMethodType == HookMethodType.Before) 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) 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; foundAny = true;
} }
@@ -767,7 +807,6 @@ namespace Barotrauma
if (!hookFunctions.ContainsKey(name)) if (!hookFunctions.ContainsKey(name))
hookFunctions.Add(name, new Dictionary<string, HookFunction>()); hookFunctions.Add(name, new Dictionary<string, HookFunction>());
hookFunctions[name][hookName] = new HookFunction(name, hookName, function); hookFunctions[name][hookName] = new HookFunction(name, hookName, function);
} }
@@ -874,5 +913,15 @@ namespace Barotrauma
return 0f; return 0f;
} }
public object Object()
{
if(result is DynValue dynValue)
{
return dynValue.ToObject();
}
return null;
}
} }
} }
@@ -320,6 +320,7 @@ namespace Barotrauma
UserData.RegisterType<LuaFile>(); UserData.RegisterType<LuaFile>();
UserData.RegisterType<LuaNetworking>(); UserData.RegisterType<LuaNetworking>();
UserData.RegisterType<LuaUserData>(); UserData.RegisterType<LuaUserData>();
UserData.RegisterType<LuaHook.HookMethodType>();
UserData.RegisterType<IUserDataDescriptor>(); UserData.RegisterType<IUserDataDescriptor>();
UserData.RegisterType<PrefabCollection<ItemPrefab>>(); UserData.RegisterType<PrefabCollection<ItemPrefab>>();
@@ -330,6 +331,11 @@ namespace Barotrauma
UserData.RegisterType<Pair<JobPrefab, int>>(); UserData.RegisterType<Pair<JobPrefab, int>>();
#if SERVER
#elif CLIENT
UserData.RegisterType<LuaGUI>();
#endif
lua.Globals["setmodulepaths"] = (Action<string[]>)SetModulePaths; lua.Globals["setmodulepaths"] = (Action<string[]>)SetModulePaths;
Binary file not shown.