feat: async inventory processing

TODO: Do !!NOT!! process containers twice!
This commit is contained in:
Yui
2024-11-06 17:45:58 -03:00
parent fa0821686b
commit a86213d3a6
2 changed files with 45 additions and 12 deletions

View File

@@ -16,7 +16,8 @@ namespace LootValueEX.Extensions
/// <param name="item">The item to check.</param>
/// <returns>True if the item has been examined, false otherwise.</returns>
internal static bool IsExamined(this Item? item) => item != null && ClientAppUtils.GetMainApp().GetClientBackEndSession().Profile.Examined(item);
internal static string PrehashTemplate(this Item? item) => string.Format("{0}|{1}|{2}", item?.TemplateId, item.GetDurability(), item.GetUses());
/// <summary>
/// Retrieves the value of a specific attribute from an item.
/// </summary>
@@ -39,27 +40,28 @@ namespace LootValueEX.Extensions
/// <summary>
/// Generates a custom hash for the given item.
///
///
/// 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>
internal static string GetCustomHash(this Item? item)
internal static async Task<string> GetCustomHashAsync(this Item? item)
{
if (item == null)
return string.Empty;
StringBuilder prehashString = new StringBuilder();
item.GetAllItems().Where(prop => !prop.Equals(item)).ExecuteForEach(prop => prehashString.Append(prop.GetCustomHash()));
item.GetAllItems().Where(i => !i.Equals(item)).DoMap(i => prehashString.Append(i.PrehashTemplate()));
if (item.Template.Equals(typeof(MagazineTemplate)))
{
MagazineTemplate magTemplate = (MagazineTemplate)item.Template;
magTemplate.Cartridges.ExecuteForEach(prop => prop.Items.ExecuteForEach(ammo => prehashString.Append(ammo.GetCustomHash())));
magTemplate.Cartridges.DoMap(s => s.Items.DoMap(i => prehashString.Append(i.PrehashTemplate())));
}
string itemHashTemplate = string.Format("{0}|{1}|{2}|{3}", prehashString.ToString(), item.TemplateId, item.GetDurability(), item.GetUses());
return Utils.HashingUtils.ConvertToSha256(itemHashTemplate);
prehashString.Append(item.PrehashTemplate());
return await Task.Run(() => Utils.HashingUtils.ConvertToSha256(prehashString.ToString()));
}
internal static string GetCustomHash(this Item? item) => Task.Run(() => item?.GetCustomHashAsync()).Result ?? string.Empty;
#if DEBUG
internal static string AttributesToString(this Item? item)
{