From 575db9ea5a16d4cab2b89afc60cdf4b3dfabbdfa Mon Sep 17 00:00:00 2001 From: SokyranTheDragon <36712560+SokyranTheDragon@users.noreply.github.com> Date: Fri, 29 Dec 2023 07:17:21 +0100 Subject: [PATCH] Added compat for Hemogen Extractor (#300) * Added compat for Hemogen Extractor * Updated Hemogen Extractor compat Now uses the changed API introduced by Zetrith, instead of the (now outdated) ones made by me. --------- Co-authored-by: Sokyran --- Source_Referenced/HemogenExtractor.cs | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Source_Referenced/HemogenExtractor.cs diff --git a/Source_Referenced/HemogenExtractor.cs b/Source_Referenced/HemogenExtractor.cs new file mode 100644 index 00000000..2bdb50f7 --- /dev/null +++ b/Source_Referenced/HemogenExtractor.cs @@ -0,0 +1,52 @@ +using System; +using HarmonyLib; +using Multiplayer.API; +using RimWorld; +using Verse; + +namespace Multiplayer.Compat +{ + /// Hemogen Extractor by Уверен? + /// + [MpCompatFor("Uveren.HemogenExtractor")] + public class HemogenExtractor + { + private static FastInvokeHandler getStoreSettingsMethod; + private static FastInvokeHandler getParentStoreSettingsMethod; + + private static Type nutritionRefuelableType; + + public HemogenExtractor(ModContentPack mod) + { + MpCompat.RegisterLambdaMethod("HemogenExtractor.CompSpawnerHemogen", "CompGetGizmosExtra", 0, 1).SetDebugOnly(); + + var type = nutritionRefuelableType = AccessTools.TypeByName("HemogenExtractor.CompNutritionRefuelable"); + getStoreSettingsMethod = MethodInvoker.GetHandler(AccessTools.DeclaredMethod(type, "GetStoreSettings")); + getParentStoreSettingsMethod = MethodInvoker.GetHandler(AccessTools.DeclaredMethod(type, "GetParentStoreSettings")); + + MpCompat.harmony.Patch(AccessTools.DeclaredMethod("HemogenExtractor.ITab_CustomNutrition:FillTab"), + prefix: new HarmonyMethod(typeof(HemogenExtractor), nameof(PreFillTab)), + finalizer: new HarmonyMethod(typeof(HemogenExtractor), nameof(PostFillTab))); + } + + private static void PreFillTab() + { + if (!MP.IsInMultiplayer || Find.Selector.SingleSelectedThing is not ThingWithComps thing) + return; + + if (thing.comps.FirstOrDefault(c => c.GetType() == nutritionRefuelableType) is CompRefuelable comp) + MP.SetThingFilterContext(new HemogenExtractorContext(comp)); + } + + private static void PostFillTab() => MP.SetThingFilterContext(null); + + [MpCompatRequireMod("Uveren.HemogenExtractor")] + public record HemogenExtractorContext(CompRefuelable Obj) : ThingFilterContext + { + public CompRefuelable Obj { get; } = Obj; + + public override ThingFilter Filter => ((StorageSettings)getStoreSettingsMethod(Obj)).filter; + public override ThingFilter ParentFilter => ((StorageSettings)getParentStoreSettingsMethod(Obj))?.filter; + } + } +} \ No newline at end of file