diff --git a/.editorconfig b/.editorconfig index cc736e0c9..7d8439dc1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -146,6 +146,7 @@ csharp_space_between_square_brackets = false resharper_wrap_object_and_collection_initializer_style = chop_always resharper_merge_into_pattern_highlighting = none resharper_member_can_be_made_static_global_highlighting = none +resharper_possible_multiple_enumeration_highlighting = none # Ignored warnings dotnet_diagnostic.CS1591.severity = none diff --git a/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs index 8dce0cf0e..19b79edc5 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs @@ -897,7 +897,7 @@ public void ReplaceRootItemId(IEnumerable itemWithChildren, MongoId newId) // Update all parentIds of items attached to base item to use new id foreach (var item in itemWithChildren) { - if (item.ParentId == oldId) + if (item.ParentId != null && item.ParentId == oldId) { item.ParentId = newId; } @@ -905,10 +905,11 @@ public void ReplaceRootItemId(IEnumerable itemWithChildren, MongoId newId) } /// - /// TODO - Write + /// Regenerate all GUIDs with new IDs, except 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. /// - /// - /// + /// Inventory to replace Ids in + /// Insured items that should not have their IDs replaced public void ReplaceProfileInventoryIds(BotBaseInventory inventory, IEnumerable? insuredItems = null) { // Blacklist @@ -916,20 +917,24 @@ public void ReplaceProfileInventoryIds(BotBaseInventory inventory, IEnumerable { - inventory.Equipment.Value, - inventory.QuestRaidItems.Value, - inventory.QuestStashItems.Value, - inventory.SortingTable.Value, - inventory.Stash.Value, - inventory.HideoutCustomizationStashId.Value, + inventory.Equipment!.Value, + inventory.QuestRaidItems!.Value, + inventory.QuestStashItems!.Value, + inventory.SortingTable!.Value, + inventory.Stash!.Value, + inventory.HideoutCustomizationStashId!.Value, } ); - itemIdBlacklist.UnionWith(inventory.HideoutAreaStashes.Values); + + if (inventory.HideoutAreaStashes != null) + { + itemIdBlacklist.UnionWith(inventory.HideoutAreaStashes.Values); + } // Add insured items ids to blacklist if (insuredItems is not null) { - itemIdBlacklist.UnionWith(insuredItems.Select(x => x.ItemId.Value)); + itemIdBlacklist.UnionWith(insuredItems.Select(x => x.ItemId!.Value)); } if (inventory.Items is null) @@ -967,9 +972,12 @@ public void ReplaceProfileInventoryIds(BotBaseInventory inventory, IEnumerable ReplaceIDs(IEnumerable originalItems, PmcData? pm itemIdBlacklist.UnionWith( new List { - pmcData.Inventory.Equipment.Value, - pmcData.Inventory.QuestRaidItems.Value, - pmcData.Inventory.QuestStashItems.Value, - pmcData.Inventory.SortingTable.Value, - pmcData.Inventory.Stash.Value, - pmcData.Inventory.HideoutCustomizationStashId.Value, + pmcData.Inventory!.Equipment!.Value, + pmcData.Inventory.QuestRaidItems!.Value, + pmcData.Inventory.QuestStashItems!.Value, + pmcData.Inventory.SortingTable!.Value, + pmcData.Inventory.Stash!.Value, + pmcData.Inventory.HideoutCustomizationStashId!.Value, } ); - itemIdBlacklist.UnionWith(pmcData.Inventory.HideoutAreaStashes.Values); + if (pmcData.Inventory?.HideoutAreaStashes != null) + { + itemIdBlacklist.UnionWith(pmcData.Inventory.HideoutAreaStashes.Values); + } } // Add insured items ids to blacklist if (insuredItems is not null) { - itemIdBlacklist.UnionWith(insuredItems.Select(x => x.ItemId.Value)); + itemIdBlacklist.UnionWith(insuredItems.Select(x => x.ItemId!.Value)); } foreach (var item in originalItems) @@ -1026,7 +1037,7 @@ public IEnumerable ReplaceIDs(IEnumerable originalItems, PmcData? pm item.Id = newId; // Find all children of item and update their parent ids to match - var childItems = originalItems.Where(x => x.ParentId == originalId); + var childItems = originalItems.Where(x => x.ParentId != null && x.ParentId == originalId); foreach (var childItem in childItems) { childItem.ParentId = newId; @@ -1039,16 +1050,37 @@ public IEnumerable ReplaceIDs(IEnumerable originalItems, PmcData? pm } // Update quick-slot id - // TODO: i dont think the fast panel key is a mongoid, it should be e.g. "Item4" - if (pmcData.Inventory.FastPanel.ContainsKey(originalId)) + var fastPanel = pmcData.Inventory.FastPanel; + if (fastPanel.ContainsValue(originalId) && !TryReplaceFastPanelId(fastPanel, originalId, newId)) { - pmcData.Inventory.FastPanel[originalId] = newId; + logger.Error( + $"Original Id: {originalId.ToString()} is contained in the fast panel, but was unable to replace it with new Id: {newId.ToString()}" + ); } } return originalItems; } + /// + /// Trys to find the original id in FastPanel, if it exists set it to the new value + /// + /// Fast panel dictionary to check + /// Original id of the item + /// New Id of the item + /// True if replaced, otherwise false + public bool TryReplaceFastPanelId(Dictionary fastPanel, MongoId originalId, MongoId newId) + { + var key = fastPanel.FirstOrDefault(kvp => kvp.Value == originalId).Key; + if (key is null) + { + return false; + } + + fastPanel[key] = newId; + return true; + } + /// /// Mark the passed in list of items as found in raid. /// Modifies passed in items @@ -1122,7 +1154,7 @@ public void SetFoundInRaid(Item item, bool excludeCurrency = true) if (parentTemplate.Key && parentTemplate.Value?.Properties?.Slots != null) { isRequiredSlot = - parentTemplate.Value?.Properties?.Slots?.Any(slot => slot?.Name == item?.SlotId && (slot?.Required ?? false)) ?? false; + parentTemplate.Value?.Properties?.Slots?.Any(slot => slot.Name == item.SlotId && (slot.Required ?? false)) ?? false; } return itemTemplate.Key && parentTemplate.Key && !(isNotRaidModdable || isRequiredSlot); @@ -1147,7 +1179,7 @@ public void SetFoundInRaid(Item item, bool excludeCurrency = true) while (currentItem != null && IsAttachmentAttached(currentItem)) { - currentItem = itemsMap.FirstOrDefault(kvp => kvp.Key == currentItem.ParentId).Value; + currentItem = itemsMap.FirstOrDefault(kvp => kvp.Key == currentItem.ParentId!).Value; if (currentItem == null) { return null; @@ -1246,10 +1278,10 @@ public bool IsAttachmentAttached(Item item) // Calculating child ExtraSize if (itemDbTemplate?.Properties?.ExtraSizeForceAdd ?? false) { - forcedUp += itemDbTemplate.Properties.ExtraSizeUp.Value; - forcedDown += itemDbTemplate.Properties.ExtraSizeDown.Value; - forcedLeft += itemDbTemplate.Properties.ExtraSizeLeft.Value; - forcedRight += itemDbTemplate.Properties.ExtraSizeRight.Value; + forcedUp += itemDbTemplate.Properties.ExtraSizeUp!.Value; + forcedDown += itemDbTemplate.Properties.ExtraSizeDown!.Value; + forcedLeft += itemDbTemplate.Properties.ExtraSizeLeft!.Value; + forcedRight += itemDbTemplate.Properties.ExtraSizeRight!.Value; } else { @@ -1275,10 +1307,10 @@ public bool IsAttachmentAttached(Item item) /// Valid caliber for cartridge public MongoId? GetRandomCompatibleCaliberTemplateId(TemplateItem item) { - var cartridges = item?.Properties?.Cartridges?.FirstOrDefault()?.Props?.Filters?.FirstOrDefault()?.Filter; + var cartridges = item.Properties?.Cartridges?.FirstOrDefault()?.Props?.Filters?.FirstOrDefault()?.Filter; if (cartridges is null) { - logger.Warning($"Failed to find cartridge for item: {item?.Id} {item?.Name}"); + logger.Warning($"Failed to find cartridge for item: {item.Id} {item.Name}"); return null; } @@ -1294,7 +1326,7 @@ public void AddCartridgesToAmmoBox(List ammoBox, TemplateItem ammoBoxDetai { var ammoBoxMaxCartridgeCount = ammoBoxDetails.Properties?.StackSlots?.First().MaxCount; var cartridgeTpl = ammoBoxDetails.Properties?.StackSlots?.First().Props?.Filters?.First().Filter?.FirstOrDefault(); - var cartridgeDetails = GetItem(cartridgeTpl.Value); + var cartridgeDetails = GetItem(cartridgeTpl!.Value); var cartridgeMaxStackSize = cartridgeDetails.Value?.Properties?.StackMaxSize; // Exit early if ammo already exists in box @@ -1339,7 +1371,7 @@ public void AddSingleStackCartridgesToAmmoBox(List ammoBox, TemplateItem a { var ammoBoxMaxCartridgeCount = ammoBoxDetails.Properties?.StackSlots?.First().MaxCount ?? 0; var cartridgeTpl = ammoBoxDetails.Properties?.StackSlots?.First().Props?.Filters?.First().Filter?.FirstOrDefault(); - ammoBox.Add(CreateCartridges(ammoBox[0].Id, cartridgeTpl.Value, (int)ammoBoxMaxCartridgeCount, 0)); + ammoBox.Add(CreateCartridges(ammoBox[0].Id, cartridgeTpl!.Value, (int)ammoBoxMaxCartridgeCount, 0)); } /// @@ -1470,7 +1502,7 @@ public void FillMagazineWithCartridge( CreateCartridges(magazineWithChildCartridges[0].Id, cartridgeTpl, cartridgeCountToAdd ?? 0, location) ); - currentStoredCartridgeCount += cartridgeCountToAdd.Value; + currentStoredCartridgeCount += cartridgeCountToAdd!.Value; location++; } @@ -1547,7 +1579,9 @@ public void FillMagazineWithCartridge( continue; } - ammoArray.Add(new ProbabilityObject(ammoDetails.Tpl.Value, (double)ammoDetails.RelativeProbability, null)); + ammoArray.Add( + new ProbabilityObject(ammoDetails.Tpl.Value, (double)ammoDetails.RelativeProbability!.Value, null) + ); } return ammoArray.Draw().FirstOrDefault(); @@ -1757,7 +1791,7 @@ public List ReparentItemAndChildren(Item rootItem, List itemWithChil if (mod.ParentId != null && (!idMappings.ContainsKey(mod.ParentId) || idMappings?[mod.ParentId] is null)) // Make remapping for items parentId { - idMappings[mod.ParentId] = new MongoId(); + idMappings![mod.ParentId] = new MongoId(); } mod.Id = idMappings[mod.Id.ToString()];