From aeef1129db568a250d051ff7f87351ab380a2f9c Mon Sep 17 00:00:00 2001 From: Kevin nX_ J Date: Thu, 27 Jan 2022 10:54:52 +0100 Subject: [PATCH] Fixing comfort radius issues - Fixed PR #653 - Properly rewritten the functionality on how we modify the comfort radius calculation. --- ValheimPlus/GameClasses/SE_Rested.cs | 50 ++++++++++++++++++---------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/ValheimPlus/GameClasses/SE_Rested.cs b/ValheimPlus/GameClasses/SE_Rested.cs index 67d9f37e..4ee68af8 100644 --- a/ValheimPlus/GameClasses/SE_Rested.cs +++ b/ValheimPlus/GameClasses/SE_Rested.cs @@ -2,8 +2,11 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using System.Reflection.Emit; using UnityEngine; using ValheimPlus.Configurations; +using HarmonyLib.Tools; +using MonoMod.Utils; namespace ValheimPlus { @@ -23,15 +26,13 @@ public static void Prefix(ref float ___m_TTLPerComfortLevel) } } - [HarmonyPatch(typeof(SE_Rested), nameof(SE_Rested.CalculateComfortLevel))] - public static class SE_Rested_CalculateComfortLevel_Transpiler - { - private static MethodInfo method_SE_Rested_GetNearbyPieces = AccessTools.Method(typeof(SE_Rested), nameof(SE_Rested.GetNearbyComfortPieces)); - private static MethodInfo method_GetNearbyPieces = AccessTools.Method(typeof(SE_Rested_CalculateComfortLevel_Transpiler), nameof(SE_Rested_CalculateComfortLevel_Transpiler.GetNearbyPieces)); - /// - /// Changes the radius in which pieces contribute to the rested bonus. - /// + /// + /// Changes the radius in which pieces contribute to the rested bonus. + /// + [HarmonyPatch(typeof(SE_Rested), nameof(SE_Rested.GetNearbyPieces))] + public static class SE_Rested_GetNearbyPieces_Transpiler + { [HarmonyTranspiler] public static IEnumerable Transpiler(IEnumerable instructions) { @@ -41,21 +42,36 @@ public static IEnumerable Transpiler(IEnumerable GetNearbyPieces(Vector3 point) + } + + /// + /// Changes the radius in which pieces contribute to the rested bonus. + /// + [HarmonyPatch(typeof(SE_Rested), nameof(SE_Rested.GetNearbyComfortPieces))] + public static class SE_Rested_GetNearbyComfortPieces_Transpiler + { + [HarmonyTranspiler] + public static IEnumerable Transpiler(IEnumerable instructions) { - List pieces = new List(); - Piece.GetAllComfortPiecesInRadius(point, Configuration.Current.Building.pieceComfortRadius, pieces); - return pieces; + if (!Configuration.Current.Building.IsEnabled) return instructions; + + List il = instructions.ToList(); + + for (int i = 0; i < il.Count; ++i) + { + if (il[i].opcode == OpCodes.Ldc_R4) + il[i].operand = Configuration.Current.Building.pieceComfortRadius; + } + return il.AsEnumerable(); } + } + }