2024-10-13 00:38:51 +02:00
|
|
|
|
using EFT.InventoryLogic;
|
|
|
|
|
using SPT.Reflection.Utils;
|
|
|
|
|
using System.Text;
|
2024-10-23 00:08:52 +02:00
|
|
|
|
using KeycardTemplate = GClass2623;
|
|
|
|
|
using ArmorTemplate = GClass2550;
|
|
|
|
|
using ArmoredRigTemplate = GClass2602;
|
2024-10-24 03:14:33 +02:00
|
|
|
|
using MagazineTemplate = GClass2586;
|
2024-10-13 00:38:51 +02:00
|
|
|
|
|
|
|
|
|
namespace LootValueEX.Extensions
|
|
|
|
|
{
|
|
|
|
|
internal static class ItemExtensions
|
|
|
|
|
{
|
2024-10-29 23:32:48 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if an item has been examined by the player.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item to check.</param>
|
|
|
|
|
/// <returns>True if the item has been examined, false otherwise.</returns>
|
2024-11-23 01:30:48 +01:00
|
|
|
|
internal static bool IsExamined(this Item? item) => item != null &&
|
|
|
|
|
ClientAppUtils.GetMainApp().GetClientBackEndSession()
|
|
|
|
|
.Profile.Examined(item);
|
|
|
|
|
|
|
|
|
|
internal static bool IsStacked(this Item? item) =>
|
|
|
|
|
item != null && (item.StackObjectsCount > 1 || item.UnlimitedCount);
|
|
|
|
|
|
|
|
|
|
internal static string PrehashTemplate(this Item? item) =>
|
|
|
|
|
string.Format("{0}|{1}|{2}", item?.TemplateId, item.GetDurability(), item.GetUses());
|
2024-11-06 21:45:58 +01:00
|
|
|
|
|
2024-11-08 21:17:10 +01:00
|
|
|
|
internal static Item? UnstackItem(this Item? item)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!item.IsStacked())
|
|
|
|
|
return item;
|
|
|
|
|
|
|
|
|
|
Item? itemClone = item.CloneItem();
|
|
|
|
|
itemClone.StackObjectsCount = 1;
|
|
|
|
|
itemClone.UnlimitedCount = false;
|
|
|
|
|
return itemClone;
|
2024-11-23 01:30:48 +01:00
|
|
|
|
}
|
2024-11-08 21:17:10 +01:00
|
|
|
|
|
2024-10-29 23:32:48 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the value of a specific attribute from an item.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item to retrieve the attribute from.</param>
|
|
|
|
|
/// <param name="attributeId">The ID of the attribute to retrieve.</param>
|
|
|
|
|
/// <returns>The value of the attribute, or -1f if the item is null or the attribute is not found.</returns>
|
2024-10-23 00:08:52 +02:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-29 23:32:48 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generates a custom hash for the given item.
|
2024-11-06 21:45:58 +01:00
|
|
|
|
///
|
2024-10-29 23:32:48 +01:00
|
|
|
|
/// This function takes an optional Item as a parameter and returns its custom hash.
|
|
|
|
|
/// If the item is null, it returns an empty string. The custom hash is determined by the item's template, durability, and uses.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item to retrieve the custom hash for.</param>
|
|
|
|
|
/// <returns>The custom hash of the item, or an empty string if the item is null.</returns>
|
2024-11-06 21:45:58 +01:00
|
|
|
|
internal static async Task<string> GetCustomHashAsync(this Item? item)
|
2024-10-13 00:38:51 +02:00
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
return string.Empty;
|
2024-10-23 00:08:52 +02:00
|
|
|
|
StringBuilder prehashString = new StringBuilder();
|
2024-11-06 21:45:58 +01:00
|
|
|
|
|
|
|
|
|
item.GetAllItems().Where(i => !i.Equals(item)).DoMap(i => prehashString.Append(i.PrehashTemplate()));
|
2024-10-29 23:32:48 +01:00
|
|
|
|
if (item.Template.Equals(typeof(MagazineTemplate)))
|
|
|
|
|
{
|
2024-10-24 03:14:33 +02:00
|
|
|
|
MagazineTemplate magTemplate = (MagazineTemplate)item.Template;
|
2024-11-06 21:45:58 +01:00
|
|
|
|
magTemplate.Cartridges.DoMap(s => s.Items.DoMap(i => prehashString.Append(i.PrehashTemplate())));
|
2024-10-24 03:14:33 +02:00
|
|
|
|
}
|
2024-11-23 01:30:48 +01:00
|
|
|
|
|
2024-11-06 21:45:58 +01:00
|
|
|
|
prehashString.Append(item.PrehashTemplate());
|
|
|
|
|
return await Task.Run(() => Utils.HashingUtils.ConvertToSha256(prehashString.ToString()));
|
2024-10-13 00:38:51 +02:00
|
|
|
|
}
|
2024-11-23 01:30:48 +01:00
|
|
|
|
|
|
|
|
|
internal static string GetCustomHash(this Item? item) =>
|
|
|
|
|
Task.Run(() => item?.GetCustomHashAsync()).Result ?? string.Empty;
|
2024-10-23 00:08:52 +02:00
|
|
|
|
#if DEBUG
|
2024-10-13 00:38:51 +02:00
|
|
|
|
internal static string AttributesToString(this Item? item)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
return String.Empty;
|
|
|
|
|
|
2024-10-29 23:32:48 +01:00
|
|
|
|
if (item.Attributes.IsNullOrEmpty())
|
2024-10-13 00:38:51 +02:00
|
|
|
|
return String.Empty;
|
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2024-11-23 01:30:48 +01:00
|
|
|
|
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()}"));
|
2024-10-13 00:38:51 +02:00
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
2024-10-23 00:08:52 +02:00
|
|
|
|
#endif
|
2024-10-29 23:32:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the durability of a given item.
|
|
|
|
|
///
|
|
|
|
|
/// This function takes an optional Item as a parameter and returns its durability.
|
|
|
|
|
/// If the item is null, it returns -1f. The durability is determined by the item's template.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item to retrieve the durability for.</param>
|
|
|
|
|
/// <returns>The durability of the item, or -1f if the item is null.</returns>
|
2024-10-13 00:38:51 +02:00
|
|
|
|
internal static float GetDurability(this Item? item)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
return -1f;
|
2024-10-29 23:32:48 +01:00
|
|
|
|
|
2024-10-23 00:08:52 +02:00
|
|
|
|
switch (item.Template)
|
2024-10-13 00:38:51 +02:00
|
|
|
|
{
|
2024-10-23 00:08:52 +02:00
|
|
|
|
case ArmoredRigTemplate armoredRig:
|
|
|
|
|
case ArmorTemplate armor:
|
2024-11-23 01:30:48 +01:00
|
|
|
|
return Utils.ItemUtils.GetArmorDurability(
|
|
|
|
|
item.GetItemComponentsInChildren<RepairableComponent>(true));
|
2024-10-23 00:08:52 +02:00
|
|
|
|
default:
|
|
|
|
|
return item.GetItemAttribute(EItemAttributeId.Durability);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-29 23:32:48 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the number of uses remaining for a given item.
|
|
|
|
|
///
|
|
|
|
|
/// This function takes an optional Item as a parameter and returns the number of uses remaining.
|
|
|
|
|
/// If the item is null, it returns -1f. The number of uses is determined by the item's template.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item to retrieve the number of uses for.</param>
|
|
|
|
|
/// <returns>The number of uses remaining for the item, or -1f if the item is null.</returns>
|
2024-10-23 00:08:52 +02:00
|
|
|
|
internal static float GetUses(this Item? item)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
2024-10-13 00:38:51 +02:00
|
|
|
|
return -1f;
|
2024-10-29 23:32:48 +01:00
|
|
|
|
|
2024-10-23 00:08:52 +02:00
|
|
|
|
switch (item.Template)
|
|
|
|
|
{
|
2024-10-24 01:58:19 +02:00
|
|
|
|
case KeycardTemplate:
|
2024-10-23 00:08:52 +02:00
|
|
|
|
KeyComponent keyComponent = item.GetItemComponent<KeyComponent>();
|
|
|
|
|
return keyComponent.Template.MaximumNumberOfUsage - keyComponent.NumberOfUsages;
|
|
|
|
|
case Meds2Class:
|
2024-10-24 01:58:19 +02:00
|
|
|
|
if (item.TryGetItemComponent(out MedKitComponent medKitComponent))
|
|
|
|
|
return medKitComponent.HpResource;
|
|
|
|
|
return -1f;
|
2024-10-29 23:19:16 +01:00
|
|
|
|
case MagazineTemplate:
|
|
|
|
|
MagazineClass? magazineClass = item as MagazineClass;
|
2024-10-29 23:32:48 +01:00
|
|
|
|
return magazineClass?.Count ?? -1f;
|
2024-10-29 23:19:16 +01:00
|
|
|
|
case AmmoBoxTemplate:
|
|
|
|
|
AmmoBox? ammoBox = item as AmmoBox;
|
2024-10-29 23:32:48 +01:00
|
|
|
|
return ammoBox?.Count ?? -1f;
|
2024-10-23 00:08:52 +02:00
|
|
|
|
default:
|
|
|
|
|
return -1f;
|
2024-10-13 00:38:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-23 01:30:48 +01:00
|
|
|
|
}
|