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

View File

@@ -121,12 +121,18 @@
</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="NVorbis" Version="0.8.6" />
<PackageReference Include="RestSharp" Version="106.6.10" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<!-- Sourced from https://stackoverflow.com/a/45248069 -->
<Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''">
<PropertyGroup>

View File

@@ -122,7 +122,7 @@
</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="NVorbis" Version="0.8.6" />
<PackageReference Include="RestSharp" Version="106.6.10" />
@@ -135,6 +135,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Update="libfreetype6.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

View File

@@ -125,12 +125,18 @@
</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="NVorbis" Version="0.8.6" />
<PackageReference Include="RestSharp" Version="106.6.10" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Update="..\BarotraumaShared\Content\Lights\divinghelmetlight.psd">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>

View File

@@ -74,9 +74,14 @@
<ProjectReference Include="..\..\Libraries\Lidgren.Network\Lidgren.NetStandard.csproj" AdditionalProperties="Configuration=Debug" />
</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" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<!-- Sourced from https://stackoverflow.com/a/45248069 -->
<Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''">

View File

@@ -89,9 +89,14 @@
</None>
</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" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''">
<PropertyGroup>
<!-- temp file for the git version (lives in "obj" folder)-->

View File

@@ -83,9 +83,15 @@
</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" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\..\Libraries\0Harmony.dll</HintPath>
</Reference>
</ItemGroup>
<!-- Sourced from https://stackoverflow.com/a/45248069 -->
<Target Name="GetGitRevision" BeforeTargets="WriteGitRevision" Condition="'$(BuildHash)' == ''">

View File

@@ -53,4 +53,4 @@ elseif CLIENT then
defaultLib["PlayerInput"] = CreateStatic("PlayerInput")
end
return defaultLib
return defaultLib

View File

@@ -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")

View File

@@ -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()

View File

@@ -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<string, HookFunction>());
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;
}
}
}

View File

@@ -320,6 +320,7 @@ namespace Barotrauma
UserData.RegisterType<LuaFile>();
UserData.RegisterType<LuaNetworking>();
UserData.RegisterType<LuaUserData>();
UserData.RegisterType<LuaHook.HookMethodType>();
UserData.RegisterType<IUserDataDescriptor>();
UserData.RegisterType<PrefabCollection<ItemPrefab>>();
@@ -330,6 +331,11 @@ namespace Barotrauma
UserData.RegisterType<Pair<JobPrefab, int>>();
#if SERVER
#elif CLIENT
UserData.RegisterType<LuaGUI>();
#endif
lua.Globals["setmodulepaths"] = (Action<string[]>)SetModulePaths;

BIN
Libraries/0Harmony.dll Normal file

Binary file not shown.