Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix food degradation for Eitr #769

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions ValheimPlus/GameClasses/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,14 @@ private static bool IsInsideNoBuildLocation(Vector3 point)
[HarmonyPatch(typeof(Player), nameof(Player.GetTotalFoodValue))]
public static class Player_GetTotalFoodValue_Transpiler
{
private static FieldInfo field_Food_m_health = AccessTools.Field(typeof(Player.Food), nameof(Player.Food.m_health));
private static FieldInfo field_Food_m_stamina = AccessTools.Field(typeof(Player.Food), nameof(Player.Food.m_stamina));
private static FieldInfo field_Food_m_item = AccessTools.Field(typeof(Player.Food), nameof(Player.Food.m_item));
private static FieldInfo field_ItemData_m_shared = AccessTools.Field(typeof(ItemDrop.ItemData), nameof(ItemDrop.ItemData.m_shared));
private static FieldInfo field_SharedData_m_food = AccessTools.Field(typeof(ItemDrop.ItemData.SharedData), nameof(ItemDrop.ItemData.SharedData.m_food));
private static FieldInfo field_SharedData_m_foodStamina = AccessTools.Field(typeof(ItemDrop.ItemData.SharedData), nameof(ItemDrop.ItemData.SharedData.m_foodStamina));
private static readonly FieldInfo field_Food_m_health = AccessTools.Field(typeof(Player.Food), nameof(Player.Food.m_health));
private static readonly FieldInfo field_Food_m_stamina = AccessTools.Field(typeof(Player.Food), nameof(Player.Food.m_stamina));
private static readonly FieldInfo field_Food_m_eitr = AccessTools.Field(typeof(Player.Food), nameof(Player.Food.m_eitr));
private static readonly FieldInfo field_Food_m_item = AccessTools.Field(typeof(Player.Food), nameof(Player.Food.m_item));
private static readonly FieldInfo field_ItemData_m_shared = AccessTools.Field(typeof(ItemDrop.ItemData), nameof(ItemDrop.ItemData.m_shared));
private static readonly FieldInfo field_SharedData_m_food = AccessTools.Field(typeof(ItemDrop.ItemData.SharedData), nameof(ItemDrop.ItemData.SharedData.m_food));
private static readonly FieldInfo field_SharedData_m_foodStamina = AccessTools.Field(typeof(ItemDrop.ItemData.SharedData), nameof(ItemDrop.ItemData.SharedData.m_foodStamina));
private static readonly FieldInfo field_SharedData_m_foodEitr = AccessTools.Field(typeof(ItemDrop.ItemData.SharedData), nameof(ItemDrop.ItemData.SharedData.m_foodEitr));

/// <summary>
/// Replaces loads to the current health/stamina for food with loads to the original health/stamina for food
Expand All @@ -347,12 +349,23 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
{
bool loads_health = il[i].LoadsField(field_Food_m_health);
bool loads_stamina = il[i].LoadsField(field_Food_m_stamina);
bool loads_eitr = il[i].LoadsField(field_Food_m_eitr);

if (loads_health || loads_stamina)
if (loads_health || loads_stamina || loads_eitr)
{
il[i].operand = field_Food_m_item;
il.Insert(++i, new CodeInstruction(OpCodes.Ldfld, field_ItemData_m_shared));
il.Insert(++i, new CodeInstruction(OpCodes.Ldfld, loads_health ? field_SharedData_m_food : field_SharedData_m_foodStamina));
if (loads_health)
{
il.Insert(++i, new CodeInstruction(OpCodes.Ldfld, field_SharedData_m_food));
} else if (loads_stamina)
{
il.Insert(++i, new CodeInstruction(OpCodes.Ldfld, field_SharedData_m_foodStamina));
}
else

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably put this in an else if (loads_eitr) and have some sort of error handling for everything else (which should be nothing for now, but if at some point something gets added or changed we would see this)

(I stumbled upon this by chance and have not been contributing to Valheim+ yet, so please see my comment with a grain of salt because you know and understand this code much better!)

{
il.Insert(++i, new CodeInstruction(OpCodes.Ldfld, field_SharedData_m_foodEitr));
}
}
}
}
Expand Down