Skip to content

Commit

Permalink
Release 1.0.3
Browse files Browse the repository at this point in the history
Reinforcing Null Checks - Defensive Adjustments
  • Loading branch information
Vapok committed Feb 26, 2023
1 parent 6ba2b94 commit c25b35d
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 25 deletions.
5 changes: 5 additions & 0 deletions PATCHNOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Shield Me Bruh! Patchnotes

# 1.0.3 - Defensive Equip Check
* When equipping shields, make sure that the item isn't null to prevent downstream issues.
* Reinforced possible null exceptions with null checks
* Ensure shield is removed when item is moved with Fast Item Transfer

# 1.0.2 - Bug Fix
* Shield Icon resets on Death event.

Expand Down
21 changes: 15 additions & 6 deletions ShieldMeBruh/Patches/DeathEvent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using HarmonyLib;
using ShieldMeBruh.Features;

namespace ShieldMeBruh.Patches;

Expand All @@ -20,18 +19,28 @@ private static class TombstoneTakeAllEventPatch
{
private static void Postfix(TombStone __instance, bool __runOriginal)
{
if (Game.instance == null || __instance == null)
return;

if (__instance.IsOwner() && Player.m_localPlayer is { } player && __runOriginal)
{
var name = Game.instance.GetPlayerProfile().GetName();
var name = Game.instance.GetPlayerProfile()?.GetName();

if (name == null || __instance.m_container == null)
return;

if (__instance.m_container.m_name.Equals(name))
{
var savedElementVector = ShieldMeBruh.AutoShield.GetShieldSaveData().SavedElement;
var savedElementVector = ShieldMeBruh.AutoShield.GetShieldSaveData()?.SavedElement;

if (savedElementVector.x >= 0 && savedElementVector.y >= 0)
if (savedElementVector == null)
return;

if (savedElementVector.Value.x >= 0 && savedElementVector.Value.y >= 0)
{
var savedItem = player.GetInventory().GetItemAt(savedElementVector.x, savedElementVector.y);
var savedElement = ShieldMeBruh.AutoShield.GetActiveInstance().GetElement(savedElementVector.x, savedElementVector.y, player.GetInventory().m_width);
var savedItem = player.GetInventory().GetItemAt(savedElementVector.Value.x, savedElementVector.Value.y);
var savedElement = ShieldMeBruh.AutoShield.GetActiveInstance().GetElement(savedElementVector.Value.x, savedElementVector.Value.y, player.GetInventory().m_width);

if (savedElement != null && savedItem != null)
{
if (savedItem.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Shield)
Expand Down
10 changes: 8 additions & 2 deletions ShieldMeBruh/Patches/Humanoid_Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ private static void Postfix(Humanoid __instance, ItemDrop.ItemData item, ref boo
if (ShieldMeBruh.AutoShield.SelectedShield != null)
{
var equipItem = player.m_inventory.GetItemAt(ShieldMeBruh.AutoShield.CurrentElement.m_pos.x, ShieldMeBruh.AutoShield.CurrentElement.m_pos.y);
player.EquipItem(equipItem);
if (equipItem != null && equipItem.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Shield)
{
player.EquipItem(equipItem);
}
}
else
{
Expand All @@ -27,7 +30,10 @@ private static void Postfix(Humanoid __instance, ItemDrop.ItemData item, ref boo
if (savedData.SavedElement.x >= 0 && savedData.SavedElement.y >= 0)
{
var equipItem = player.m_inventory.GetItemAt(savedData.SavedElement.x, savedData.SavedElement.y);
player.EquipItem(equipItem);
if (equipItem != null && equipItem.m_shared.m_itemType == ItemDrop.ItemData.ItemType.Shield)
{
player.EquipItem(equipItem);
}
}
}
}
Expand Down
73 changes: 60 additions & 13 deletions ShieldMeBruh/Patches/MoveProtection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,28 @@ public static class MoveProtection
private static InventoryGrid.Element _futureElement;
private static InventoryGrid.Element _oldElement;

[HarmonyPatch(typeof(Inventory), nameof(Inventory.MoveItemToThis), typeof(Inventory), typeof(ItemDrop.ItemData),
typeof(int), typeof(int), typeof(int))]
[HarmonyPatch(typeof(Inventory), nameof(Inventory.MoveItemToThis), typeof(Inventory), typeof(ItemDrop.ItemData))]
private static class MoveItemPatch
{
private static void Prefix(Inventory __instance, Inventory fromInventory, ItemDrop.ItemData item)
{
if (_movingWithDropItem)
return;

if (ShieldMeBruh.AutoShield.CurrentElement == null && ShieldMeBruh.AutoShield.SelectedShield == null)
return;

if (__instance == null || fromInventory == null || item == null)
return;

if (!__instance.m_name.Equals("Inventory"))
{
ShieldMeBruh.AutoShield.ResetCurrentSheildElement();
}
}
}

[HarmonyPatch(typeof(Inventory), nameof(Inventory.MoveItemToThis), typeof(Inventory), typeof(ItemDrop.ItemData), typeof(int), typeof(int), typeof(int))]
private static class MoveItemToThisPatch
{
private static void Prefix(Inventory __instance, Inventory fromInventory, ItemDrop.ItemData item, int x, int y)
Expand All @@ -23,6 +43,9 @@ private static void Prefix(Inventory __instance, Inventory fromInventory, ItemDr
if (ShieldMeBruh.AutoShield.CurrentElement == null && ShieldMeBruh.AutoShield.SelectedShield == null)
return;

if (__instance == null || fromInventory == null || item == null)
return;

/* Two Scenarios:
* 1) SelectedShield is moving to another item. In this case, "item" is selected sheild, and pos is position of other item moving to.
* 2) another item, or shield, is moving to a position where SelectedItem is shield, which means it will move.
Expand All @@ -42,6 +65,9 @@ private static void Prefix(Inventory __instance, Inventory fromInventory, ItemDr
.GetElement(item.m_gridPos.x, item.m_gridPos.y, __instance.m_width);
var itemAt = __instance.GetItemAt(x, y);

if (targetElement == null || sourceElement == null || itemAt == null)
return;

if (itemAt != ShieldMeBruh.AutoShield.SelectedShield)
return;

Expand All @@ -56,6 +82,9 @@ private static void Prefix(Inventory __instance, Inventory fromInventory, ItemDr
var sourceElement = ShieldMeBruh.AutoShield.GetActiveInstance()
.GetElement(item.m_gridPos.x, item.m_gridPos.y, __instance.m_width);

if (targetElement == null || sourceElement == null)
return;

_futureElement = targetElement;
_oldElement = sourceElement;
}
Expand All @@ -78,9 +107,11 @@ private static void Postfix(Inventory __instance, Inventory fromInventory, ItemD
{
var newItem = __instance.GetItemAt(_futureElement.m_pos.x, _futureElement.m_pos.y);

ShieldMeBruh.AutoShield.ResetCurrentSheildElement(_oldElement);
ShieldMeBruh.AutoShield.ApplyShieldToElement(_futureElement, newItem);

if (newItem != null && _oldElement != null && _futureElement != null)
{
ShieldMeBruh.AutoShield.ResetCurrentSheildElement(_oldElement);
ShieldMeBruh.AutoShield.ApplyShieldToElement(_futureElement, newItem);
}
_reEnableShield = false;
}

Expand Down Expand Up @@ -118,7 +149,10 @@ private static class DropItemPatch
private static void Prefix(InventoryGrid __instance, Inventory fromInventory, ItemDrop.ItemData item,
int amount, Vector2i pos)
{
if (ShieldMeBruh.AutoShield.CurrentElement == null && ShieldMeBruh.AutoShield.SelectedShield == null)
if (item == null || __instance == null)
return;

if (ShieldMeBruh.AutoShield.SelectedShield == null || ShieldMeBruh.AutoShield.GetActiveInstance() == null)
return;

/* Two Scenarios:
Expand All @@ -134,12 +168,13 @@ private static void Prefix(InventoryGrid __instance, Inventory fromInventory, It
if (item != ShieldMeBruh.AutoShield.SelectedShield)
{
//Peer into the next item
var targetElement = ShieldMeBruh.AutoShield.GetActiveInstance()
.GetElement(pos.x, pos.y, __instance.m_width);
var sourceElement = ShieldMeBruh.AutoShield.GetActiveInstance()
.GetElement(item.m_gridPos.x, item.m_gridPos.y, __instance.m_width);
var targetElement = ShieldMeBruh.AutoShield.GetActiveInstance().GetElement(pos.x, pos.y, __instance.m_width);
var sourceElement = ShieldMeBruh.AutoShield.GetActiveInstance().GetElement(item.m_gridPos.x, item.m_gridPos.y, __instance.m_width);
var itemAt = __instance.m_inventory.GetItemAt(pos.x, pos.y);

if (targetElement == null || sourceElement == null || itemAt == null)
return;

if (itemAt != ShieldMeBruh.AutoShield.SelectedShield)
return;

Expand All @@ -154,6 +189,9 @@ private static void Prefix(InventoryGrid __instance, Inventory fromInventory, It
var sourceElement = ShieldMeBruh.AutoShield.GetActiveInstance()
.GetElement(item.m_gridPos.x, item.m_gridPos.y, __instance.m_width);

if (targetElement == null || sourceElement == null)
return;

_futureElement = targetElement;
_oldElement = sourceElement;
}
Expand All @@ -167,15 +205,24 @@ private static void Postfix(InventoryGrid __instance, Inventory fromInventory, I
int amount, Vector2i pos, ref bool __result, bool __runOriginal)
{
if (!__result || !__runOriginal)
{
_reEnableShieldOnDropItem = false;
_oldElement = null;
_futureElement = null;
_movingWithDropItem = false;
return;
}


if (_reEnableShieldOnDropItem)
{
var newItem = __instance.m_inventory.GetItemAt(_futureElement.m_pos.x, _futureElement.m_pos.y);

ShieldMeBruh.AutoShield.ResetCurrentSheildElement(_oldElement);
ShieldMeBruh.AutoShield.ApplyShieldToElement(_futureElement, newItem);

if (newItem != null && _oldElement != null && _futureElement != null)
{
ShieldMeBruh.AutoShield.ResetCurrentSheildElement(_oldElement);
ShieldMeBruh.AutoShield.ApplyShieldToElement(_futureElement, newItem);
}
_reEnableShieldOnDropItem = false;
}

Expand Down
4 changes: 2 additions & 2 deletions ShieldMeBruh/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.2.0")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyVersion("1.0.3.0")]
[assembly: AssemblyFileVersion("1.0.3.0")]
2 changes: 1 addition & 1 deletion ShieldMeBruh/ShieldMeBruh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ShieldMeBruh : BaseUnityPlugin, IPluginInfo
//Module Constants
private const string _pluginId = "vapok.mods.shieldmebruh";
private const string _displayName = "Shield Me Bruh!";
private const string _version = "1.0.2";
private const string _version = "1.0.3";
public static bool ValheimAwake;
public static Waiting Waiter;

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ShieldMeBruh",
"version_number": "1.0.2",
"version_number": "1.0.3",
"website_url": "https://github.com/Vapok/ShieldMeBruh",
"description": "A Valheim Quality of Life Mod that will auto equip a selected shield when a one-handed weapon is equipped. I need a shield! Shield me Bruh!",
"dependencies": [
Expand Down

0 comments on commit c25b35d

Please sign in to comment.