diff --git a/Libraries/Core/Helpers/ItemHelper.cs b/Libraries/Core/Helpers/ItemHelper.cs index ffefaed7..92c93dc2 100644 --- a/Libraries/Core/Helpers/ItemHelper.cs +++ b/Libraries/Core/Helpers/ItemHelper.cs @@ -1,16 +1,13 @@ -using System.Text.Json; using System.Text.Json.Serialization; using SptCommon.Annotations; using Core.Models.Eft.Common; using Core.Models.Eft.Common.Tables; -using Core.Models.Eft.ItemEvent; using Core.Models.Enums; using Core.Models.Utils; using Core.Services; using Core.Utils; using Core.Utils.Cloners; using Core.Utils.Collections; -using SptCommon.Extensions; namespace Core.Helpers; @@ -933,8 +930,67 @@ public class ItemHelper( } } + public void ReplaceProfileInventoryIds(BotBaseInventory inventory, List? insuredItems = null) + { + // Blacklist + var itemIdBlacklist = new HashSet(); + itemIdBlacklist.UnionWith( + new List{ + inventory.Equipment, + inventory.QuestRaidItems, + inventory.QuestStashItems, + inventory.SortingTable, + inventory.Stash, + inventory.HideoutCustomizationStashId + }); + itemIdBlacklist.UnionWith(inventory.HideoutAreaStashes.Keys); + + // Add insured items ids to blacklist + if (insuredItems is not null) + { + itemIdBlacklist.UnionWith(insuredItems.Select(x => x.ItemId)); + } + + + foreach (var item in inventory.Items) + { + if (itemIdBlacklist.Contains(item.Id)) + { + continue; + } + + // Generate new id + var newId = _hashUtil.Generate(); + + // Keep copy of original id + var originalId = item.Id; + + // Update items id to new one we generated + item.Id = newId; + + // Find all children of item and update their parent ids to match + var childItems = inventory.Items.Where(x => x.ParentId == originalId); + foreach (var childItem in childItems) + { + childItem.ParentId = newId; + } + + // Also replace in quick slot if the old ID exists. + if (inventory.FastPanel is null) + { + continue; + } + + // Update quickslot id + if (inventory.FastPanel.ContainsKey(originalId)) + { + inventory.FastPanel[originalId] = newId; + } + } + } + /// - /// Regenerate all GUIDs with new IDs, for the exception of special item types (e.g. quest, sorting table, etc.) This + /// Regenerate all GUIDs with new IDs, with the exception of special item types (e.g. quest, sorting table, etc.) This /// function will not mutate the original items list, but will return a new list with new GUIDs. /// /// Items to adjust the IDs of @@ -942,11 +998,14 @@ public class ItemHelper( /// Insured items that should not have their IDs replaced /// Quick slot panel /// List - public List ReplaceIDs(List originalItems, PmcData pmcData = null, List insuredItems = null, - Dictionary fastPanel = null) + public List ReplaceIDs( + List originalItems, + PmcData pmcData = null, + List? insuredItems = null, + Dictionary? fastPanel = null) { var serialisedInventory = _jsonUtil.Serialize(originalItems); - var hideoutAreaStashes = pmcData?.Inventory?.HideoutAreaStashes ?? new(); + var hideoutAreaStashes = pmcData?.Inventory?.HideoutAreaStashes ?? []; foreach (var item in originalItems) { diff --git a/Libraries/Core/Services/CreateProfileService.cs b/Libraries/Core/Services/CreateProfileService.cs index 3ed90034..09c4b116 100644 --- a/Libraries/Core/Services/CreateProfileService.cs +++ b/Libraries/Core/Services/CreateProfileService.cs @@ -74,12 +74,7 @@ public class CreateProfileService( AddMissingInternalContainersToProfile(pmcData); // Change item IDs to be unique - pmcData.Inventory.Items = _itemHelper.ReplaceIDs( - pmcData.Inventory.Items, - pmcData, - null, - pmcData.Inventory.FastPanel - ); + _itemHelper.ReplaceProfileInventoryIds(pmcData.Inventory); // Create profile var profileDetails = new SptProfile