From df8ffa2b1637b544e2b396c339649dbf7f20f003 Mon Sep 17 00:00:00 2001 From: Chomp Date: Sun, 9 Mar 2025 17:12:07 +0000 Subject: [PATCH] Replaced various instances of dictionaries and hashsets with their frozen counterparts --- .../Generators/BotEquipmentModGenerator.cs | 13 +++++++------ .../Generators/BotInventoryGenerator.cs | 3 ++- .../Helpers/BotGeneratorHelper.cs | 3 ++- .../SPTarkov.Server.Core/Helpers/BotHelper.cs | 3 ++- .../Helpers/BotWeaponGeneratorHelper.cs | 3 ++- .../SptCommands/GiveCommand/GiveSptCommand.cs | 3 ++- .../SPTFriend/Commands/HelloMessageHandler.cs | 3 ++- .../Helpers/InventoryHelper.cs | 3 ++- .../SPTarkov.Server.Core/Helpers/ItemHelper.cs | 15 ++++++++------- .../SPTarkov.Server.Core/Helpers/ProfileHelper.cs | 3 ++- .../Helpers/RagfairOfferHelper.cs | 3 ++- .../Models/Spt/Server/Locations.cs | 7 ++++--- 12 files changed, 37 insertions(+), 25 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Generators/BotEquipmentModGenerator.cs b/Libraries/SPTarkov.Server.Core/Generators/BotEquipmentModGenerator.cs index 040d9057..84deeb9c 100644 --- a/Libraries/SPTarkov.Server.Core/Generators/BotEquipmentModGenerator.cs +++ b/Libraries/SPTarkov.Server.Core/Generators/BotEquipmentModGenerator.cs @@ -1,3 +1,4 @@ +using System.Collections.Frozen; using SPTarkov.Server.Core.Helpers; using SPTarkov.Server.Core.Models.Common; using SPTarkov.Server.Core.Models.Eft.Common; @@ -40,10 +41,10 @@ public class BotEquipmentModGenerator( ) { protected BotConfig _botConfig = _configServer.GetConfig(); - protected static readonly HashSet _modSightIds = ["mod_sight_front", "mod_sight_rear"]; + protected static readonly FrozenSet _modSightIds = ["mod_sight_front", "mod_sight_rear"]; // Slots that hold scopes - protected static readonly HashSet _scopeIds = + protected static readonly FrozenSet _scopeIds = [ "mod_scope", "mod_mount", @@ -55,13 +56,13 @@ public class BotEquipmentModGenerator( ]; // Slots that hold muzzles - protected static readonly HashSet _muzzleIds = ["mod_muzzle", "mod_muzzle_000", "mod_muzzle_001"]; + protected static readonly FrozenSet _muzzleIds = ["mod_muzzle", "mod_muzzle_000", "mod_muzzle_001"]; // Slots a weapon can store its stock in - protected static readonly HashSet _stockSlots = ["mod_stock", "mod_stock_000", "mod_stock_001", "mod_stock_akms"]; + protected static readonly FrozenSet _stockSlots = ["mod_stock", "mod_stock_000", "mod_stock_001", "mod_stock_akms"]; // Slots that hold cartridges - protected static readonly HashSet _cartridgeHolderSlots = + protected static readonly FrozenSet _cartridgeHolderSlots = [ "mod_magazine", "patron_in_weapon", @@ -1487,7 +1488,7 @@ public class BotEquipmentModGenerator( /// e.g. mod_magazine / patron_in_weapon_000 /// /// string array - public HashSet GetAmmoContainers() + public FrozenSet GetAmmoContainers() { return _cartridgeHolderSlots; } diff --git a/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs b/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs index 1df78c38..a1f1d1c4 100644 --- a/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs +++ b/Libraries/SPTarkov.Server.Core/Generators/BotInventoryGenerator.cs @@ -1,3 +1,4 @@ +using System.Collections.Frozen; using SPTarkov.Server.Core.Context; using SPTarkov.Server.Core.Helpers; using SPTarkov.Server.Core.Models.Eft.Common.Tables; @@ -39,7 +40,7 @@ public class BotInventoryGenerator( private readonly BotConfig _botConfig = _configServer.GetConfig(); // Slots handled individually inside `GenerateAndAddEquipmentToBot` - private static readonly HashSet _excludedEquipmentSlots = + private static readonly FrozenSet _excludedEquipmentSlots = [ EquipmentSlots.Pockets, EquipmentSlots.FirstPrimaryWeapon, diff --git a/Libraries/SPTarkov.Server.Core/Helpers/BotGeneratorHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/BotGeneratorHelper.cs index e48d28f6..65eda049 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/BotGeneratorHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/BotGeneratorHelper.cs @@ -10,6 +10,7 @@ using SPTarkov.Server.Core.Services; using SPTarkov.Server.Core.Utils; using SPTarkov.Common.Annotations; using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; +using System.Collections.Frozen; namespace SPTarkov.Server.Core.Helpers; @@ -30,7 +31,7 @@ public class BotGeneratorHelper( protected PmcConfig _pmcConfig = _configServer.GetConfig(); // Equipment slot ids that do not conflict with other slots - protected static readonly HashSet _slotsWithNoCompatIssues = ["Scabbard", "Backpack", "SecureContainer", "Holster", "ArmBand"]; + protected static readonly FrozenSet _slotsWithNoCompatIssues = ["Scabbard", "Backpack", "SecureContainer", "Holster", "ArmBand"]; /// /// Adds properties to an item diff --git a/Libraries/SPTarkov.Server.Core/Helpers/BotHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/BotHelper.cs index d4daeb61..e9e1036e 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/BotHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/BotHelper.cs @@ -5,6 +5,7 @@ using SPTarkov.Server.Core.Servers; using SPTarkov.Server.Core.Services; using SPTarkov.Server.Core.Utils; using SPTarkov.Common.Annotations; +using System.Collections.Frozen; namespace SPTarkov.Server.Core.Helpers; @@ -18,7 +19,7 @@ public class BotHelper( { protected BotConfig _botConfig = _configServer.GetConfig(); protected PmcConfig _pmcConfig = _configServer.GetConfig(); - protected static readonly HashSet _pmcTypeIds = ["usec", "bear", "pmc", "pmcbear", "pmcusec"]; + protected static readonly FrozenSet _pmcTypeIds = ["usec", "bear", "pmc", "pmcbear", "pmcusec"]; protected Dictionary> _pmcNameCache = new(); /// diff --git a/Libraries/SPTarkov.Server.Core/Helpers/BotWeaponGeneratorHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/BotWeaponGeneratorHelper.cs index 732ff30c..832c4f88 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/BotWeaponGeneratorHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/BotWeaponGeneratorHelper.cs @@ -5,6 +5,7 @@ using SPTarkov.Server.Core.Servers; using SPTarkov.Server.Core.Services; using SPTarkov.Server.Core.Utils; using SPTarkov.Common.Annotations; +using System.Collections.Frozen; namespace SPTarkov.Server.Core.Helpers; @@ -20,7 +21,7 @@ public class BotWeaponGeneratorHelper( LocalisationService _localisationService ) { - private static readonly HashSet _magCheck = ["CylinderMagazine", "SpringDrivenCylinder"]; + private static readonly FrozenSet _magCheck = ["CylinderMagazine", "SpringDrivenCylinder"]; /// /// Get a randomized number of bullets for a specific magazine diff --git a/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/Commando/SptCommands/GiveCommand/GiveSptCommand.cs b/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/Commando/SptCommands/GiveCommand/GiveSptCommand.cs index 1deeba6b..2810c938 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/Commando/SptCommands/GiveCommand/GiveSptCommand.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/Commando/SptCommands/GiveCommand/GiveSptCommand.cs @@ -9,6 +9,7 @@ using SPTarkov.Server.Core.Services; using SPTarkov.Server.Core.Utils; using SPTarkov.Server.Core.Utils.Cloners; using SPTarkov.Common.Annotations; +using System.Collections.Frozen; namespace SPTarkov.Server.Core.Helpers.Dialogue.Commando.SptCommands.GiveCommand; @@ -29,7 +30,7 @@ public class GiveSptCommand( private static readonly Regex _commandRegex = new(@"^spt give (((([a-z]{2,5}) )?""(.+)""|\w+) )?([0-9]+)$"); // Exception for flares - protected static readonly HashSet _excludedPresetItems = + protected static readonly FrozenSet _excludedPresetItems = [ ItemTpl.FLARE_RSP30_REACTIVE_SIGNAL_CARTRIDGE_RED, ItemTpl.FLARE_RSP30_REACTIVE_SIGNAL_CARTRIDGE_GREEN, diff --git a/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/SPTFriend/Commands/HelloMessageHandler.cs b/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/SPTFriend/Commands/HelloMessageHandler.cs index 1bfe8c47..dffb5338 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/SPTFriend/Commands/HelloMessageHandler.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/Dialogue/SPTFriend/Commands/HelloMessageHandler.cs @@ -4,6 +4,7 @@ using SPTarkov.Server.Core.Models.Eft.Profile; using SPTarkov.Server.Core.Services; using SPTarkov.Server.Core.Utils; using SPTarkov.Common.Annotations; +using System.Collections.Frozen; namespace SPTarkov.Server.Core.Helpers.Dialogue.SPTFriend.Commands; @@ -12,7 +13,7 @@ public class HelloMessageHandler( MailSendService _mailSendService, RandomUtil _randomUtil) : IChatMessageHandler { - protected static readonly HashSet _listOfGreetings = ["hello", "hi", "sup", "yo", "hey", "bonjour"]; + protected static readonly FrozenSet _listOfGreetings = ["hello", "hi", "sup", "yo", "hey", "bonjour"]; public int GetPriority() diff --git a/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs index ed3141a2..84cf3c25 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/InventoryHelper.cs @@ -16,6 +16,7 @@ using SPTarkov.Server.Core.Utils.Cloners; using SPTarkov.Common.Annotations; using SPTarkov.Common.Extensions; using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; +using System.Collections.Frozen; namespace SPTarkov.Server.Core.Helpers; @@ -36,7 +37,7 @@ public class InventoryHelper( ) { protected InventoryConfig _inventoryConfig = _configServer.GetConfig(); - private static readonly HashSet _variableSizeItemTypes = [BaseClasses.WEAPON, BaseClasses.FUNCTIONAL_MOD]; + private static readonly FrozenSet _variableSizeItemTypes = [BaseClasses.WEAPON, BaseClasses.FUNCTIONAL_MOD]; /// /// Add multiple items to player stash (assuming they all fit) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs index 2169e025..13cbd94d 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs @@ -9,6 +9,7 @@ using SPTarkov.Server.Core.Utils.Cloners; using SPTarkov.Server.Core.Utils.Collections; using SPTarkov.Common.Annotations; using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; +using System.Collections.Frozen; namespace SPTarkov.Server.Core.Helpers; @@ -27,7 +28,7 @@ public class ItemHelper( ICloner _cloner ) { - protected static readonly HashSet _defaultInvalidBaseTypes = + protected static readonly FrozenSet _defaultInvalidBaseTypes = [ BaseClasses.LOOT_CONTAINER, BaseClasses.MOB_CONTAINER, @@ -38,7 +39,7 @@ public class ItemHelper( BaseClasses.POCKETS ]; - protected static readonly HashSet _slotsAsStrings = + protected static readonly FrozenSet _slotsAsStrings = [ EquipmentSlots.Headwear.ToString(), EquipmentSlots.Earpiece.ToString(), @@ -56,7 +57,7 @@ public class ItemHelper( EquipmentSlots.Scabbard.ToString() ]; - protected static readonly HashSet _dogTagTpls = + protected static readonly FrozenSet _dogTagTpls = [ ItemTpl.BARTER_DOGTAG_BEAR, ItemTpl.BARTER_DOGTAG_BEAR_EOD, @@ -70,7 +71,7 @@ public class ItemHelper( ItemTpl.BARTER_DOGTAG_USEC_PRESTIGE_2 ]; - protected static readonly HashSet _softInsertIds = + protected static readonly FrozenSet _softInsertIds = [ "groin", "groin_back", @@ -88,7 +89,7 @@ public class ItemHelper( "helmet_ears" ]; - protected static readonly HashSet _removablePlateSlotIds = + protected static readonly FrozenSet _removablePlateSlotIds = [ "front_plate", "back_plate", @@ -451,7 +452,7 @@ public class ItemHelper( // Get all soft insert slot ids // @returns A List of soft insert ids (e.g. soft_armor_back, helmet_top) - public HashSet GetSoftInsertSlotIds() + public FrozenSet GetSoftInsertSlotIds() { return _softInsertIds; } @@ -1980,7 +1981,7 @@ public class ItemHelper( // Get a list of slot names that hold removable plates // Returns Array of slot ids (e.g. front_plate) - public HashSet GetRemovablePlateSlotIds() + public FrozenSet GetRemovablePlateSlotIds() { return _removablePlateSlotIds; } diff --git a/Libraries/SPTarkov.Server.Core/Helpers/ProfileHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/ProfileHelper.cs index b6f36bd2..8d71d0b2 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/ProfileHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/ProfileHelper.cs @@ -10,6 +10,7 @@ using SPTarkov.Server.Core.Utils; using SPTarkov.Server.Core.Utils.Cloners; using SPTarkov.Common.Annotations; using LogLevel = SPTarkov.Server.Core.Models.Spt.Logging.LogLevel; +using System.Collections.Frozen; namespace SPTarkov.Server.Core.Helpers; @@ -27,7 +28,7 @@ public class ProfileHelper( ConfigServer _configServer ) { - protected static readonly HashSet gameEditionsWithFreeRefresh = ["edge_of_darkness", "unheard_edition"]; + protected static readonly FrozenSet gameEditionsWithFreeRefresh = ["edge_of_darkness", "unheard_edition"]; protected InventoryConfig _inventoryConfig = _configServer.GetConfig(); /// diff --git a/Libraries/SPTarkov.Server.Core/Helpers/RagfairOfferHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/RagfairOfferHelper.cs index ba999e96..c89bcf61 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/RagfairOfferHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/RagfairOfferHelper.cs @@ -12,6 +12,7 @@ using SPTarkov.Server.Core.Services; using SPTarkov.Server.Core.Utils; using SPTarkov.Common.Annotations; using SPTarkov.Common.Extensions; +using System.Collections.Frozen; namespace SPTarkov.Server.Core.Helpers; @@ -43,7 +44,7 @@ public class RagfairOfferHelper( protected BotConfig _botConfig = _configServer.GetConfig(); protected RagfairConfig _ragfairConfig = _configServer.GetConfig(); - protected static readonly HashSet _currencies = ["all", "RUB", "USD", "EUR"]; + protected static readonly FrozenSet _currencies = ["all", "RUB", "USD", "EUR"]; /// /// Passthrough to ragfairOfferService.getOffers(), get flea offers a player should see diff --git a/Libraries/SPTarkov.Server.Core/Models/Spt/Server/Locations.cs b/Libraries/SPTarkov.Server.Core/Models/Spt/Server/Locations.cs index 6054cb0a..a01dfade 100644 --- a/Libraries/SPTarkov.Server.Core/Models/Spt/Server/Locations.cs +++ b/Libraries/SPTarkov.Server.Core/Models/Spt/Server/Locations.cs @@ -1,3 +1,4 @@ +using System.Collections.Frozen; using System.Text.Json.Serialization; using SPTarkov.Server.Core.Models.Eft.Common.Tables; @@ -7,7 +8,7 @@ public record Locations { // sometimes we get the key or value given so save changing logic in each place // have it key both - private static readonly Dictionary _locationMappings = new() + private readonly FrozenDictionary _locationMappings = new Dictionary { // EFT { "factory4_day", "Factory4Day" }, @@ -48,7 +49,7 @@ public record Locations { "Woods", "Woods" }, { "Sandbox", "Sandbox" }, { "SandboxHigh", "SandboxHigh" } - }; + }.ToFrozenDictionary(); private Dictionary? _locationDictionaryCache; @@ -209,7 +210,7 @@ public record Locations /// public string GetMappedKey(string key) { - return _locationMappings.TryGetValue(key, out var value) ? value : key; + return _locationMappings.GetValueOrDefault(key, key); } private void HydrateDictionary()