From 35df976d9d521482715a39380decf504d3151746 Mon Sep 17 00:00:00 2001 From: SokyranTheDragon <36712560+SokyranTheDragon@users.noreply.github.com> Date: Mon, 9 Dec 2024 07:50:17 +0100 Subject: [PATCH] Compat for Vanilla Christmas Expanded (#501) Syncs a (graphical only) gizmo and fixes the overlay overlapping MP buttons. Nothing major or necessary to use the mod in MP, but nice to have. --- Source/Mods/VanillaChristmasExpanded.cs | 78 +++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Source/Mods/VanillaChristmasExpanded.cs diff --git a/Source/Mods/VanillaChristmasExpanded.cs b/Source/Mods/VanillaChristmasExpanded.cs new file mode 100644 index 0000000..966cc4f --- /dev/null +++ b/Source/Mods/VanillaChristmasExpanded.cs @@ -0,0 +1,78 @@ +using System.Collections.Generic; +using System.Reflection; +using System.Reflection.Emit; +using HarmonyLib; +using Multiplayer.API; +using Verse; + +namespace Multiplayer.Compat; + +/// Vanilla Christmas Expanded by Oskar Potocki, Sarg Bjornson, Taranchuk +/// +/// +[MpCompatFor("VE.VanillaChristmasExpanded")] +public class VanillaChristmasExpanded +{ + public VanillaChristmasExpanded(ModContentPack mod) + { + LongEventHandler.ExecuteWhenFinished(LatePatch); + + // Change graphic + MpCompat.RegisterLambdaMethod("VanillaChristmasExpanded.GrandFestiveTree", nameof(Building.GetGizmos), 0); + } + + private static void LatePatch() + { + MpCompatPatchLoader.LoadPatch(); + } + + private static int ModifyScreenWidthValue(int screenWidth) + { + // Constant values taken directly from MP's code. + const int mpBtnMargin = 8; + const int mpBtnWidth = 80; + // If we don't offset it further the text will be covered by the + // indicator rectangle (its width isn't stored as a constant). + // Slightly winging it here with a value that doesn't fully match + // MP size but results in the overlay looking alright (as in, it + // doesn't overlap and there isn't too much of a gap between it + // and the MP UI). + const int extraOffset = 20; + + if (MP.IsInMultiplayer) + return screenWidth - (mpBtnWidth + 2 * mpBtnMargin + extraOffset); + return screenWidth; + } + + [MpCompatTranspiler("VanillaChristmasExpanded.FestiveFavorManager", "DrawUI")] + private static IEnumerable MoveFestiveOverlayPosition(IEnumerable instr, MethodBase baseMethod) + { + // Same issue as Vanilla Factions Insectoid 2. The overlay overlaps MP buttons. Since + // both overlays depend on a specific storyteller, they won't overlap with themselves. + // Since this mod requires a bit more offset (as it's overlapping with the rectangle + // "ticks behind" indicator) the same method cannot be reused as for VFEI2. + + var target = AccessTools.DeclaredField(typeof(UI), nameof(UI.screenWidth)); + var replacement = MpMethodUtil.MethodOf(ModifyScreenWidthValue); + var replacedCount = 0; + + foreach (var ci in instr) + { + yield return ci; + + if (ci.LoadsField(target)) + { + yield return new CodeInstruction(OpCodes.Call, replacement); + + replacedCount++; + } + } + + const int expected = 1; + if (replacedCount != expected) + { + var name = (baseMethod.DeclaringType?.Namespace).NullOrEmpty() ? baseMethod.Name : $"{baseMethod.DeclaringType!.Name}:{baseMethod.Name}"; + Log.Warning($"Patched incorrect number of Find.CameraDriver.MapPosition calls (patched {replacedCount}, expected {expected}) for method {name}"); + } + } +} \ No newline at end of file