Skip to content

Commit

Permalink
Added compat for Hemogen Extractor (#300)
Browse files Browse the repository at this point in the history
* 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 <toCy&#$VIH$1cSOu8s&Fv@UVN%L3snN*H09$1^Xghq*hKo9Itnmq#bNf8Ld67#T$#a&cEaY1>
  • Loading branch information
SokyranTheDragon and Sokyran authored Dec 29, 2023
1 parent 4b6b34a commit 575db9e
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Source_Referenced/HemogenExtractor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using HarmonyLib;
using Multiplayer.API;
using RimWorld;
using Verse;

namespace Multiplayer.Compat
{
/// <summary>Hemogen Extractor by Уверен?</summary>
/// <see href="https://steamcommunity.com/sharedfiles/filedetails/?id=2903919607"/>
[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;
}
}
}

0 comments on commit 575db9e

Please sign in to comment.