Skip to content

Commit

Permalink
Update some of the mods that broke in 1.5 update (#442)
Browse files Browse the repository at this point in the history
Hospitality:
- Some types were moved from `Hospitality` to `Hospitality.Utilities` namespace

Vanilla Factions Expanded - Deserters:
- Changed `nameof(Dialog_Debug.DoWindowContents)` to  `nameof(Dialog_DeserterNetwork.DoWindowContents)`, as it was supposed to be

RimFridge:
- Removed anything related to rename dialog, as renaming was changed in 1.5
  - This can be (somewhat universally) handled by MP itself
- Updated links to point to a fork by "Just Harry", as it's the most up-to-date version
- Changed the current map patch to not long patching failures
  - Some RimFridge forks don't have the current map issue, like the one by "Just Harry" - however, the patch is kept for forks that do need it

PatchingUtilities:
- Modified to allow for current map patch to be applied without logging patching failures
  • Loading branch information
SokyranTheDragon authored Apr 21, 2024
1 parent 5336f89 commit 3700e0d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 41 deletions.
6 changes: 3 additions & 3 deletions Source/Mods/Hospitality.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ private static void LatePatch()
{
// Cache
{
var type = AccessTools.TypeByName("Hospitality.CompUtility");
var type = AccessTools.TypeByName("Hospitality.Utilities.CompUtility");
guestCompsField = AccessTools.StaticFieldRefAccess<IDictionary>(AccessTools.DeclaredField(type, "guestComps"));

type = AccessTools.TypeByName("Hospitality.RelationUtility");
type = AccessTools.TypeByName("Hospitality.Utilities.RelationUtility");
relationCacheField = AccessTools.StaticFieldRefAccess<IDictionary>(AccessTools.DeclaredField(type, "relationCache"));

type = AccessTools.TypeByName("Hospitality.Utilities.GenericUtility");
Expand All @@ -73,7 +73,7 @@ private static void LatePatch()
syncFields = AccessTools.StaticFieldRefAccess<ISyncField[]>(AccessTools.DeclaredField("Hospitality.Multiplayer:guestFields"));

// Basically an extension method to pawn.GetComp<CompGuest>, but with extra caching and null.
compGuestMethod = MethodInvoker.GetHandler(AccessTools.DeclaredMethod("Hospitality.CompUtility:CompGuest"));
compGuestMethod = MethodInvoker.GetHandler(AccessTools.DeclaredMethod("Hospitality.Utilities.CompUtility:CompGuest"));

// Hospitality starts watching in this DoHeader is called before DoCell, so it should be good enough of a place to put it in.
var type = AccessTools.TypeByName("Hospitality.MainTab.PawnColumnWorker_Relationship");
Expand Down
41 changes: 13 additions & 28 deletions Source/Mods/RimFridge.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,34 @@
using System;
using System.Runtime.Serialization;
using HarmonyLib;
using HarmonyLib;
using Multiplayer.API;
using Verse;

namespace Multiplayer.Compat
{
/// <summary>RimFridge by KiameV</summary>
/// <summary>RimFridge by KiameV, maintained by "Just Harry"</summary>
/// <remarks>Fixes for gizmos</remarks>
/// <see href="https://github.com/KiameV/rimworld-rimfridge"/>
/// <see href="https://steamcommunity.com/sharedfiles/filedetails/?id=1180721235"/>
/// <see href="https://github.com/just-harry/rimworld-rimfridge-now-with-shelves"/>
/// <see href="https://steamcommunity.com/sharedfiles/filedetails/?id=2898411376"/>
[MpCompatFor("rimfridge.kv.rw")]
public class RimFridgeCompat
{
private static AccessTools.FieldRef<object, ThingComp> fridgeField;
private static Type dialogType;

public RimFridgeCompat(ModContentPack mod)
{
// Several Gizmos
{
MpCompat.RegisterLambdaDelegate("RimFridge.CompRefrigerator", "CompGetGizmosExtra", 1, 2, 3, 4, 5);
MpCompat.RegisterLambdaMethod("RimFridge.CompToggleGlower", "CompGetGizmosExtra", 0);

dialogType = AccessTools.TypeByName("RimFridge.Dialog_RenameFridge");
fridgeField = AccessTools.FieldRefAccess<ThingComp>(dialogType, "fridge");
var type = AccessTools.TypeByName("RimFridge.CompRefrigerator");
// Offset temperature by x degrees
MP.RegisterSyncMethod(type, "InterfaceChangeTargetTemperature");
// Reset to default
MpCompat.RegisterLambdaDelegate(type, "CompGetGizmosExtra", 2);

MP.RegisterSyncWorker<object>(SyncFridgeName, dialogType);
MP.RegisterSyncMethod(dialogType, "SetName");
// Toggle darklight
MpCompat.RegisterLambdaMethod("RimFridge.CompToggleGlower", "CompGetGizmosExtra", 0);
}

// Current map usage
// Not needed for the fork made by "Not Harry" (at the moment, the only 1.5 fork), but other forks may need this.
{
PatchingUtilities.ReplaceCurrentMapUsage("RimFridge.Patch_ReachabilityUtility_CanReach:Prefix");
}
}

private static void SyncFridgeName(SyncWorker sync, ref object dialog)
{
if (sync.isWriting)
sync.Write(fridgeField(dialog));
else
{
dialog = FormatterServices.GetUninitializedObject(dialogType);
fridgeField(dialog) = sync.Read<ThingComp>();
PatchingUtilities.ReplaceCurrentMapUsage("RimFridge.Patch_ReachabilityUtility_CanReach:Prefix", false);
}
}
}
Expand Down
24 changes: 19 additions & 5 deletions Source/PatchingUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public static void ReplaceCurrentMapUsage(Type type, string methodName)
}
}

public static void ReplaceCurrentMapUsage(string typeColonName)
public static void ReplaceCurrentMapUsage(string typeColonName, bool logIfNothingPatched = true)
{
if (typeColonName.NullOrEmpty())
{
Expand All @@ -379,20 +379,32 @@ public static void ReplaceCurrentMapUsage(string typeColonName)

var method = AccessTools.DeclaredMethod(typeColonName) ?? AccessTools.Method(typeColonName);
if (method != null)
ReplaceCurrentMapUsage(method);
ReplaceCurrentMapUsage(method, logIfNothingPatched);
else
Log.Warning($"Trying to patch current map usage for null method ({typeColonName}). Was the method removed or renamed?");
}

public static void ReplaceCurrentMapUsage(MethodBase method)
public static void ReplaceCurrentMapUsage(MethodBase method, bool logIfNothingPatched = true)
{
if (method != null)
MpCompat.harmony.Patch(method, transpiler: new HarmonyMethod(typeof(PatchingUtilities), nameof(ReplaceCurrentMapUsageTranspiler)));
{
var transpiler = new HarmonyMethod(logIfNothingPatched
? MpMethodUtil.MethodOf(ReplaceCurrentMapUsageTranspiler)
: MpMethodUtil.MethodOf(ReplaceCurrentMapUsageNoLogTranspiler));

MpCompat.harmony.Patch(method, transpiler: transpiler);
}
else
Log.Warning("Trying to patch current map usage for null method. Was the method removed or renamed?");
}

private static IEnumerable<CodeInstruction> ReplaceCurrentMapUsageTranspiler(IEnumerable<CodeInstruction> instr, MethodBase baseMethod)
=> ReplaceCurrentMapUsageTranspilerInternal(instr, baseMethod, true);

private static IEnumerable<CodeInstruction> ReplaceCurrentMapUsageNoLogTranspiler(IEnumerable<CodeInstruction> instr, MethodBase baseMethod)
=> ReplaceCurrentMapUsageTranspilerInternal(instr, baseMethod, false);

private static IEnumerable<CodeInstruction> ReplaceCurrentMapUsageTranspilerInternal(IEnumerable<CodeInstruction> instr, MethodBase baseMethod, bool logNoCurrentMapPatches)
{
var helper = new CurrentMapPatchHelper(baseMethod);

Expand All @@ -409,9 +421,11 @@ private static IEnumerable<CodeInstruction> ReplaceCurrentMapUsageTranspiler(IEn

if (!helper.IsSupported)
Log.Warning($"Unsupported type, can't patch current map usage for {name}");
else if (!helper.IsPatched)
else if (logNoCurrentMapPatches && !helper.IsPatched)
Log.Warning($"Failed patching current map usage for {name}");
#if DEBUG
else if (!helper.IsPatched)
Log.Warning($"Failed patching current map usage for {name}, but logging was disabled");
else
Log.Warning($"Successfully patched the current map usage for {name}");
#endif
Expand Down
8 changes: 3 additions & 5 deletions Source_Referenced/VanillaFactionsDeserters.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if false
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
Expand Down Expand Up @@ -80,7 +79,7 @@ public VanillaFactionsDeserters(ModContentPack mod)
// Re-cache intel amount
// Since we change how it's handled by allowing players to open the dialog locally without forcing pause,
// we need to re-cache the value in case it ends up changing due to decay/other player actions.
MpCompat.harmony.Patch(AccessTools.DeclaredMethod(typeof(Dialog_DeserterNetwork), nameof(Dialog_Debug.DoWindowContents)),
MpCompat.harmony.Patch(AccessTools.DeclaredMethod(typeof(Dialog_DeserterNetwork), nameof(Dialog_DeserterNetwork.DoWindowContents)),
prefix: new HarmonyMethod(typeof(VanillaFactionsDeserters), nameof(PreDeserterNetworkDraw)));

#endregion
Expand Down Expand Up @@ -547,5 +546,4 @@ private static IEnumerable<CodeInstruction> ReplaceServicePurchaseButton(IEnumer

#endregion
}
}
#endif
}

0 comments on commit 3700e0d

Please sign in to comment.