From ab8c1967b8447c05c689c35c5c81057b7490d26d Mon Sep 17 00:00:00 2001 From: Yui Date: Tue, 22 Oct 2024 19:08:52 -0300 Subject: [PATCH] feat: added a way to get item usage count --- Plugin/Extensions/ItemExtensions.cs | 54 ++++++++++++++++++++++++----- Plugin/Patches/TooltipPatch.cs | 2 +- Plugin/Plugin.csproj | 5 ++- Plugin/Utils/ItemUtils.cs | 17 +++++++++ 4 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 Plugin/Utils/ItemUtils.cs diff --git a/Plugin/Extensions/ItemExtensions.cs b/Plugin/Extensions/ItemExtensions.cs index a3c9807..1df3fda 100644 --- a/Plugin/Extensions/ItemExtensions.cs +++ b/Plugin/Extensions/ItemExtensions.cs @@ -1,20 +1,42 @@ using EFT.InventoryLogic; using SPT.Reflection.Utils; using System.Text; +using KeycardTemplate = GClass2623; +using ArmorTemplate = GClass2550; +using ArmoredRigTemplate = GClass2602; +using SimpleMedClass = GClass2631; namespace LootValueEX.Extensions { internal static class ItemExtensions { internal static bool IsExamined(this Item? item) => item != null && ClientAppUtils.GetMainApp().GetClientBackEndSession().Profile.Examined(item); + internal static float GetItemAttribute(this Item? item, EItemAttributeId attributeId) + { + if (item == null) + return -1f; + try + { + return item.Attributes.Single(att => att.Id.Equals(attributeId)).Base(); + } + catch (InvalidOperationException) + { + return -1f; + } + } internal static string CustomHash(this Item? item) { if (item == null) return string.Empty; - - string itemHashTemplate = string.Format("{0}|{1}|{2}", item.TemplateId, item.StackMaxSize, item.StackObjectsCount); + StringBuilder prehashString = new StringBuilder(); + if (item.IsContainer) + { + return "0"; + } + string itemHashTemplate = string.Format("{0}|{1}|{2}|{3}", item.TemplateId, item.GetDurability(), item.GetUses(), item.StackObjectsCount); return "0"; } +#if DEBUG internal static string AttributesToString(this Item? item) { if (item == null) @@ -26,19 +48,35 @@ namespace LootValueEX.Extensions StringBuilder sb = new StringBuilder(); item.Attributes.ForEach(attr => sb.Append($"\n{attr.Id}\n\tName: {attr.Name}\n\tBase Value: {attr.Base()}\n\tString value: {attr.StringValue()}\n\tDisplay Type: {attr.DisplayType()}")); return sb.ToString(); - } +#endif internal static float GetDurability(this Item? item) { if (item == null) return -1f; - - try - { - return item.Attributes.Single(att => att.Id.Equals(EItemAttributeId.Durability)).Base(); - }catch(InvalidOperationException) + switch (item.Template) { + case ArmoredRigTemplate armoredRig: + case ArmorTemplate armor: + return Utils.ItemUtils.GetArmorDurability(item.GetItemComponentsInChildren(true)); + default: + return item.GetItemAttribute(EItemAttributeId.Durability); + } + } + internal static float GetUses(this Item? item) + { + if (item == null) return -1f; + switch (item.Template) + { + case KeycardTemplate key: + KeyComponent keyComponent = item.GetItemComponent(); + return keyComponent.Template.MaximumNumberOfUsage - keyComponent.NumberOfUsages; + case SimpleMedClass: + case Meds2Class: + return item.GetItemAttribute(EItemAttributeId.HpResource); + default: + return -1f; } } } diff --git a/Plugin/Patches/TooltipPatch.cs b/Plugin/Patches/TooltipPatch.cs index c399514..45f7e2c 100644 --- a/Plugin/Patches/TooltipPatch.cs +++ b/Plugin/Patches/TooltipPatch.cs @@ -23,7 +23,7 @@ namespace LootValueEX.Patches Plugin.Log.LogDebug("Stacktrace of tooltip call: \n" + stackTrace.ToString()); if (GridItemTooltipPatch.PatchTooltip) { - text += $"
TemplateID: {GridItemTooltipPatch.HoveredItem?.TemplateId}
Item hashsum: {GridItemTooltipPatch.HoveredItem?.GetHashSum()}
Item durability: {GridItemTooltipPatch.HoveredItem?.GetDurability()}
GridItemView"; + text += $"
TemplateID: {GridItemTooltipPatch.HoveredItem?.TemplateId}
Template: {GridItemTooltipPatch.HoveredItem?.Template}
Item hashsum: {GridItemTooltipPatch.HoveredItem?.GetHashSum()}
Item durability: {GridItemTooltipPatch.HoveredItem?.GetDurability()}
Item uses: {GridItemTooltipPatch.HoveredItem?.GetUses()}
GridItemView"; Plugin.Log.LogDebug(GridItemTooltipPatch.HoveredItem?.AttributesToString()); } if (InsuranceSlotPatch.PatchTooltip) diff --git a/Plugin/Plugin.csproj b/Plugin/Plugin.csproj index 940730d..400b368 100644 --- a/Plugin/Plugin.csproj +++ b/Plugin/Plugin.csproj @@ -10,6 +10,9 @@ ..\Properties + + ..\References\Managed\ItemComponent.Types.dll + ..\References\Managed\UnityEngine.PhysicsModule.dll @@ -82,7 +85,7 @@ - + diff --git a/Plugin/Utils/ItemUtils.cs b/Plugin/Utils/ItemUtils.cs new file mode 100644 index 0000000..012ff5a --- /dev/null +++ b/Plugin/Utils/ItemUtils.cs @@ -0,0 +1,17 @@ +using EFT.InventoryLogic; + +namespace LootValueEX.Utils +{ + internal class ItemUtils + { + public static float GetArmorDurability(IEnumerable repairableComponents) + { + float totalDurability = 0; + foreach (RepairableComponent component in repairableComponents) + { + totalDurability += component.Durability; + } + return totalDurability; + } + } +}