From a5264294880f47c27b8b25d16b27fa1fee5cdaa0 Mon Sep 17 00:00:00 2001 From: Lilian Cahuzac Date: Tue, 6 Dec 2022 17:03:57 +0100 Subject: [PATCH 1/2] Fix craft from chest --- ValheimPlus/GameClasses/Player.cs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/ValheimPlus/GameClasses/Player.cs b/ValheimPlus/GameClasses/Player.cs index 3f8f30fd..1aa7281b 100644 --- a/ValheimPlus/GameClasses/Player.cs +++ b/ValheimPlus/GameClasses/Player.cs @@ -833,12 +833,12 @@ private static bool Prefix(ref Player __instance, ref bool __result) } } - /* - [HarmonyPatch(typeof(Player), nameof(Player.HaveRequirements), new System.Type[] { typeof(Piece.Requirement[]), typeof(bool), typeof(int) })] - public static class Player_HaveRequirements_Transpiler + + [HarmonyPatch(typeof(Player), nameof(Player.HaveRequirementItems), new System.Type[] { typeof(Recipe), typeof(bool), typeof(int) })] + public static class Player_HaveRequirementItems_Transpiler { private static MethodInfo method_Inventory_CountItems = AccessTools.Method(typeof(Inventory), nameof(Inventory.CountItems)); - private static MethodInfo method_ComputeItemQuantity = AccessTools.Method(typeof(Player_HaveRequirements_Transpiler), nameof(Player_HaveRequirements_Transpiler.ComputeItemQuantity)); + private static MethodInfo method_ComputeItemQuantity = AccessTools.Method(typeof(Player_HaveRequirementItems_Transpiler), nameof(Player_HaveRequirementItems_Transpiler.ComputeItemQuantity)); /// /// Patches out the code that checks if there is enough material to craft a specific object. @@ -888,10 +888,10 @@ private static int ComputeItemQuantity(int fromInventory, Piece.Requirement item } [HarmonyPatch(typeof(Player), nameof(Player.HaveRequirements), new System.Type[] { typeof(Piece), typeof(Player.RequirementMode) })] - public static class Player_HaveRequirements_2_Transpiler + public static class Player_HaveRequirements_Transpiler { private static MethodInfo method_Inventory_CountItems = AccessTools.Method(typeof(Inventory), nameof(Inventory.CountItems)); - private static MethodInfo method_ComputeItemQuantity = AccessTools.Method(typeof(Player_HaveRequirements_2_Transpiler), nameof(Player_HaveRequirements_2_Transpiler.ComputeItemQuantity)); + private static MethodInfo method_ComputeItemQuantity = AccessTools.Method(typeof(Player_HaveRequirements_Transpiler), nameof(Player_HaveRequirements_Transpiler.ComputeItemQuantity)); /// /// Patches out the code that checks if there is enough material to craft a specific object. @@ -941,10 +941,10 @@ private static int ComputeItemQuantity(int fromInventory, Piece.Requirement item } } - [HarmonyPatch(typeof(Player), nameof(Player.ConsumeResources))] + [HarmonyPatch(typeof(Player), nameof(Player.ConsumeResources), new Type[] { typeof(Piece.Requirement[]), typeof(int), typeof(int)})] public static class Player_ConsumeResources_Transpiler { - private static MethodInfo method_Inventory_RemoveItem = AccessTools.Method(typeof(Inventory), nameof(Inventory.RemoveItem), new System.Type[] { typeof(string), typeof(int) }); + private static MethodInfo method_Inventory_RemoveItem = AccessTools.Method(typeof(Inventory), nameof(Inventory.RemoveItem), new Type[] { typeof(string), typeof(int), typeof(int) }); private static MethodInfo method_RemoveItemsFromInventoryAndNearbyChests = AccessTools.Method(typeof(Player_ConsumeResources_Transpiler), nameof(Player_ConsumeResources_Transpiler.RemoveItemsFromInventoryAndNearbyChests)); /// @@ -983,18 +983,19 @@ public static IEnumerable Transpiler(IEnumerable Date: Tue, 6 Dec 2022 17:04:16 +0100 Subject: [PATCH 2/2] Fix fall damages transpiler --- ValheimPlus/GameClasses/Character.cs | 31 ++++++++++++++-------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/ValheimPlus/GameClasses/Character.cs b/ValheimPlus/GameClasses/Character.cs index 1a26b43c..116b72b1 100644 --- a/ValheimPlus/GameClasses/Character.cs +++ b/ValheimPlus/GameClasses/Character.cs @@ -117,29 +117,28 @@ public static IEnumerable Transpiler(IEnumerable il = instructions.ToList(); + bool found = false; + // The original code checks if (this.IsPlayer() && num > 4f); if so, it calculates the fall damage as Mathf.Clamp01((num - 4f) / 16f) * 100f // ... where num is the fall distance. // We want to remove this calculation and replace it with our own, so we replace the calculation with a call to our own method, defined below this one. for (int i = 0; i < il.Count; i++) { - if (il[i].opcode != OpCodes.Newobj) continue; - if (i + 11 >= il.Count || il[i + 1].opcode != OpCodes.Stloc_2 || il[i + 11].opcode != OpCodes.Mul) + if (!found && il[i].opcode != OpCodes.Newobj) { - // Looks like the wrong location, so we failed and this transpiler needs updating - ZLog.Log("Unable to transpile Character::UpdateGroundContact to patch fall damage calculation"); - return instructions; + continue; } - - // OK, looks like we're good! We want to patch a bit further ahead. - // il[i] now points to: newobj instance void HitData::.ctor() - // We want to keep that and the three instructions after: stloc.2, ldloc.2, ldflda (...) - // We want to remove the next 8 instructions (from ldloc.0 to mul, inclusive). - il.RemoveRange(i + 4, 8); - - // We now have i+3 = ldflda m_damage, i+4 = stfld m_damage. - // We want to insert the call to our damage calculation in between. - il.Insert(i + 4, new CodeInstruction(OpCodes.Ldloc_0)); // Load the fall distance - il.Insert(i + 5, new CodeInstruction(OpCodes.Call, method_calculateFallDamage)); + else if (il[i].opcode == OpCodes.Newobj) + { + found = true; + continue; + } + if (il[i].opcode == OpCodes.Ldloc_2) + { + il[i].opcode = OpCodes.Ldloc_0; + il.Insert(i + 1, new CodeInstruction(OpCodes.Call, method_calculateFallDamage)); + } + else continue; return il.AsEnumerable(); }