58 lines
1.9 KiB
C#
Raw Normal View History

using Aki.Reflection.Patching;
using BepInEx;
using EFT.UI;
using System.Linq;
using System.Reflection;
using DynamicInteraction = GClass2816;
using ItemContext = GClass2623;
using ItemInfoInteractions = GClass2817<EFT.InventoryLogic.EItemInfoButton>;
2024-03-05 22:48:21 +02:00
namespace IcyClawz.CustomInteractions;
[BepInPlugin("com.IcyClawz.CustomInteractions", "IcyClawz.CustomInteractions", "1.3.1")]
public class Plugin : BaseUnityPlugin
{
2024-03-05 22:48:21 +02:00
private void Awake()
{
2024-03-05 22:48:21 +02:00
new ItemUiContextPatch().Enable();
new InteractionButtonsContainerPatch().Enable();
}
2024-03-05 22:48:21 +02:00
}
2024-03-05 22:48:21 +02:00
internal class ItemUiContextPatch : ModulePatch
{
protected override MethodBase GetTargetMethod() =>
typeof(ItemUiContext).GetMethod("GetItemContextInteractions", BindingFlags.Public | BindingFlags.Instance);
2024-03-05 22:48:21 +02:00
[PatchPostfix]
private static void Postfix(ref ItemInfoInteractions __result, ref ItemUiContext __instance, ItemContext itemContext)
{
foreach (var provider in CustomInteractionsManager.Providers.OfType<IItemCustomInteractionsProvider>())
{
2024-03-05 22:48:21 +02:00
var interactions = provider.GetCustomInteractions(__instance, itemContext.ViewType, itemContext.Item);
if (interactions is null)
continue;
foreach (CustomInteraction interaction in interactions)
__result.AddCustomInteraction(interaction);
}
}
2024-03-05 22:48:21 +02:00
}
2024-03-05 22:48:21 +02:00
internal class InteractionButtonsContainerPatch : ModulePatch
{
protected override MethodBase GetTargetMethod() =>
typeof(InteractionButtonsContainer).GetMethod("method_3", BindingFlags.NonPublic | BindingFlags.Instance);
2024-03-05 22:48:21 +02:00
[PatchPrefix]
private static bool Prefix(ref InteractionButtonsContainer __instance, DynamicInteraction interaction)
{
if (interaction is CustomInteractionImpl impl)
{
2024-03-05 22:48:21 +02:00
__instance.AddCustomButton(impl);
return false;
}
2024-03-05 22:48:21 +02:00
return true;
}
}