Skip to content

Commit

Permalink
Merge pull request #725 from healiha/feature/mistlands
Browse files Browse the repository at this point in the history
Fixes for Mistands patch
  • Loading branch information
nxPublic authored Dec 6, 2022
2 parents e57c64e + fda59c1 commit b322be8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
31 changes: 15 additions & 16 deletions ValheimPlus/GameClasses/Character.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,29 +117,28 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio

List<CodeInstruction> 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();
}
Expand Down
23 changes: 12 additions & 11 deletions ValheimPlus/GameClasses/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

/// <summary>
/// Patches out the code that checks if there is enough material to craft a specific object.
Expand Down Expand Up @@ -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));

/// <summary>
/// Patches out the code that checks if there is enough material to craft a specific object.
Expand Down Expand Up @@ -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));

/// <summary>
Expand Down Expand Up @@ -983,26 +983,27 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio

il.Insert(++thisIdx, new CodeInstruction(OpCodes.Ldloc_2));
il.Insert(++thisIdx, new CodeInstruction(OpCodes.Ldloc_3));
il.Insert(++thisIdx, new CodeInstruction(OpCodes.Ldarg_3));
il.Insert(++thisIdx, new CodeInstruction(OpCodes.Call, method_RemoveItemsFromInventoryAndNearbyChests));

return il.AsEnumerable();
}

private static void RemoveItemsFromInventoryAndNearbyChests(Player player, Piece.Requirement item, int amount)
private static void RemoveItemsFromInventoryAndNearbyChests(Player player, Piece.Requirement item, int amount, int itemQuality)
{
GameObject pos = player.GetCurrentCraftingStation()?.gameObject;
if (!pos || !Configuration.Current.CraftFromChest.checkFromWorkbench) pos = player.gameObject;

int inventoryAmount = player.m_inventory.CountItems(item.m_resItem.m_itemData.m_shared.m_name);
player.m_inventory.RemoveItem(item.m_resItem.m_itemData.m_shared.m_name, amount);
player.m_inventory.RemoveItem(item.m_resItem.m_itemData.m_shared.m_name, amount, itemQuality);
amount -= inventoryAmount;
if (amount <= 0) return;

InventoryAssistant.RemoveItemInAmountFromAllNearbyChests(pos, Helper.Clamp(Configuration.Current.CraftFromChest.range, 1, 50), item.m_resItem.m_itemData, amount, !Configuration.Current.CraftFromChest.ignorePrivateAreaCheck);
}
}

*/


public static class EquipPatchState
{
Expand Down

0 comments on commit b322be8

Please sign in to comment.