diff --git a/OpenRA.Game/Manifest.cs b/OpenRA.Game/Manifest.cs index a07c5e7138..bf75defec2 100644 --- a/OpenRA.Game/Manifest.cs +++ b/OpenRA.Game/Manifest.cs @@ -200,18 +200,18 @@ namespace OpenRA static string[] YamlList(Dictionary yaml, string key) { - if (!yaml.ContainsKey(key)) + if (!yaml.TryGetValue(key, out var value)) return Array.Empty(); - return yaml[key].Nodes.Select(n => n.Key).ToArray(); + return value.Nodes.Select(n => n.Key).ToArray(); } static IReadOnlyDictionary YamlDictionary(Dictionary yaml, string key) { - if (!yaml.ContainsKey(key)) + if (!yaml.TryGetValue(key, out var value)) return new Dictionary(); - return yaml[key].ToDictionary(my => my.Value); + return value.ToDictionary(my => my.Value); } public bool Contains() where T : IGlobalModData diff --git a/OpenRA.Game/Map/MapCache.cs b/OpenRA.Game/Map/MapCache.cs index ca4990abf0..b6b3025868 100644 --- a/OpenRA.Game/Map/MapCache.cs +++ b/OpenRA.Game/Map/MapCache.cs @@ -348,8 +348,8 @@ namespace OpenRA while (this[uid].Status != MapStatus.Available) { - if (mapUpdates.ContainsKey(uid)) - uid = mapUpdates[uid]; + if (mapUpdates.TryGetValue(uid, out var newUid)) + uid = newUid; else return null; } diff --git a/OpenRA.Game/ObjectCreator.cs b/OpenRA.Game/ObjectCreator.cs index 6c4a62bb1b..5e382908b8 100644 --- a/OpenRA.Game/ObjectCreator.cs +++ b/OpenRA.Game/ObjectCreator.cs @@ -152,8 +152,8 @@ namespace OpenRA for (var i = 0; i < p.Length; i++) { var key = p[i].Name; - if (!args.ContainsKey(key)) throw new InvalidOperationException($"ObjectCreator: key `{key}' not found"); - a[i] = args[key]; + if (!args.TryGetValue(key, out var arg)) throw new InvalidOperationException($"ObjectCreator: key `{key}' not found"); + a[i] = arg; } return ctor.Invoke(a); diff --git a/OpenRA.Game/Sound/Sound.cs b/OpenRA.Game/Sound/Sound.cs index f4ad227bff..bd160ba868 100644 --- a/OpenRA.Game/Sound/Sound.cs +++ b/OpenRA.Game/Sound/Sound.cs @@ -380,17 +380,17 @@ namespace OpenRA if (voicedActor != null) { - if (!rules.VoicePools.Value.ContainsKey(definition)) + if (!rules.VoicePools.Value.TryGetValue(definition, out var p)) throw new InvalidOperationException($"Can't find {definition} in voice pool."); - pool = rules.VoicePools.Value[definition]; + pool = p; } else { - if (!rules.NotificationsPools.Value.ContainsKey(definition)) + if (!rules.NotificationsPools.Value.TryGetValue(definition, out var p)) throw new InvalidOperationException($"Can't find {definition} in notification pool."); - pool = rules.NotificationsPools.Value[definition]; + pool = p; } var clip = pool.GetNext(); diff --git a/OpenRA.Game/UtilityCommands/ExtractChromeStrings.cs b/OpenRA.Game/UtilityCommands/ExtractChromeStrings.cs index cd51bdacca..5f13df43a0 100644 --- a/OpenRA.Game/UtilityCommands/ExtractChromeStrings.cs +++ b/OpenRA.Game/UtilityCommands/ExtractChromeStrings.cs @@ -257,10 +257,10 @@ namespace OpenRA.UtilityCommands var validChildTypes = new List<(MiniYamlNodeBuilder Node, string Type, string Value)>(); foreach (var childNode in node.Value.Nodes) { - if (translatables.ContainsKey(nodeType)) + if (translatables.TryGetValue(nodeType, out var fieldName)) { var childType = childNode.Key.Split('@')[0]; - if (translatables[nodeType].Contains(childType) + if (fieldName.Contains(childType) && !string.IsNullOrEmpty(childNode.Value.Value) && !IsAlreadyTranslated(childNode.Value.Value) && childNode.Value.Value.Any(char.IsLetterOrDigit)) diff --git a/OpenRA.Mods.Cnc/Traits/World/VoxelCache.cs b/OpenRA.Mods.Cnc/Traits/World/VoxelCache.cs index a1c8c76d48..b132678892 100644 --- a/OpenRA.Mods.Cnc/Traits/World/VoxelCache.cs +++ b/OpenRA.Mods.Cnc/Traits/World/VoxelCache.cs @@ -96,11 +96,11 @@ namespace OpenRA.Mods.Cnc.Traits public bool HasModelSequence(string model, string sequence) { - if (!models.ContainsKey(model)) + if (!models.TryGetValue(model, out var sequences)) throw new InvalidOperationException( $"Model `{model}` does not have any sequences defined."); - return models[model].ContainsKey(sequence); + return sequences.ContainsKey(sequence); } public IVertexBuffer VertexBuffer => loader.VertexBuffer; diff --git a/OpenRA.Mods.Cnc/UtilityCommands/ImportGen2MapCommand.cs b/OpenRA.Mods.Cnc/UtilityCommands/ImportGen2MapCommand.cs index 990d7d3e3a..9c53033b55 100644 --- a/OpenRA.Mods.Cnc/UtilityCommands/ImportGen2MapCommand.cs +++ b/OpenRA.Mods.Cnc/UtilityCommands/ImportGen2MapCommand.cs @@ -262,9 +262,9 @@ namespace OpenRA.Mods.Cnc.UtilityCommands var name = entries[1].ToLowerInvariant(); - if (DeployableActors.ContainsKey(name)) + if (DeployableActors.TryGetValue(name, out var n)) { - name = DeployableActors[name]; + name = n; isDeployed = true; } diff --git a/OpenRA.Mods.Cnc/UtilityCommands/ImportTiberianDawnMapCommand.cs b/OpenRA.Mods.Cnc/UtilityCommands/ImportTiberianDawnMapCommand.cs index b775ec3b5d..3deca59a67 100644 --- a/OpenRA.Mods.Cnc/UtilityCommands/ImportTiberianDawnMapCommand.cs +++ b/OpenRA.Mods.Cnc/UtilityCommands/ImportTiberianDawnMapCommand.cs @@ -91,8 +91,8 @@ namespace OpenRA.Mods.Cnc.UtilityCommands var res = (Type: (byte)0, Index: (byte)0); var type = kv.Value.ToLowerInvariant(); - if (OverlayResourceMapping.ContainsKey(type)) - res = OverlayResourceMapping[type]; + if (OverlayResourceMapping.TryGetValue(type, out var r)) + res = r; Map.Resources[cell] = new ResourceTile(res.Type, res.Index); if (OverlayActors.Contains(type)) diff --git a/OpenRA.Mods.Common/Lint/CheckTranslationReference.cs b/OpenRA.Mods.Common/Lint/CheckTranslationReference.cs index 09d1ece657..4967629398 100644 --- a/OpenRA.Mods.Common/Lint/CheckTranslationReference.cs +++ b/OpenRA.Mods.Common/Lint/CheckTranslationReference.cs @@ -285,10 +285,10 @@ namespace OpenRA.Mods.Common.Lint var isAttribute = !string.IsNullOrEmpty(attribute); var keyWithAtrr = isAttribute ? $"{key}.{attribute}" : key; - if (!referencedVariablesPerKey.ContainsKey(keyWithAtrr)) + if (!referencedVariablesPerKey.TryGetValue(keyWithAtrr, out var referencedVariables)) return; - foreach (var referencedVariable in referencedVariablesPerKey[keyWithAtrr]) + foreach (var referencedVariable in referencedVariables) if (!variableReferences.Contains(referencedVariable)) emitError(isAttribute ? $"Missing variable `{referencedVariable}` for attribute `{attribute}` of key `{key}` in {file}." : @@ -302,7 +302,7 @@ namespace OpenRA.Mods.Common.Lint variableReferences.Add(varName); - if (!referencedVariablesPerKey.ContainsKey(keyWithAtrr) || !referencedVariablesPerKey[keyWithAtrr].Contains(varName)) + if (!referencedVariablesPerKey.TryGetValue(keyWithAtrr, out var referencedVariables) || !referencedVariables.Contains(varName)) emitWarning(isAttribute ? $"Unused variable `{varName}` for attribute `{attribute}` of key `{key}` in {file}." : $"Unused variable `{varName}` for key `{key}` in {file}."); diff --git a/OpenRA.Mods.Common/Scripting/Properties/ProductionProperties.cs b/OpenRA.Mods.Common/Scripting/Properties/ProductionProperties.cs index df75f81d33..5e0e631493 100644 --- a/OpenRA.Mods.Common/Scripting/Properties/ProductionProperties.cs +++ b/OpenRA.Mods.Common/Scripting/Properties/ProductionProperties.cs @@ -292,10 +292,10 @@ namespace OpenRA.Mods.Common.Scripting { var queue = GetBuildableInfo(actorType).Queue.First(); - if (!queues.ContainsKey(queue)) + if (!queues.TryGetValue(queue, out var cpq)) return true; - return productionHandlers.ContainsKey(queue) || queues[queue].AllQueued().Any(); + return productionHandlers.ContainsKey(queue) || cpq.AllQueued().Any(); } BuildableInfo GetBuildableInfo(string actorType) diff --git a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs index a07fb331a2..ad2fffd7a5 100644 --- a/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs +++ b/OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs @@ -361,14 +361,12 @@ namespace OpenRA.Mods.Common.Server { lock (server.LobbyInfo) { - if (!server.LobbyInfo.Slots.ContainsKey(s)) + if (!server.LobbyInfo.Slots.TryGetValue(s, out var slot)) { Log.Write("server", $"Invalid slot: {s}"); return false; } - var slot = server.LobbyInfo.Slots[s]; - if (slot.Closed || server.LobbyInfo.ClientInSlot(s) != null) return false; diff --git a/OpenRA.Mods.Common/Traits/BotModules/BotModuleLogic/BaseBuilderQueueManager.cs b/OpenRA.Mods.Common/Traits/BotModules/BotModuleLogic/BaseBuilderQueueManager.cs index 956693d5c0..00e09fa90c 100644 --- a/OpenRA.Mods.Common/Traits/BotModules/BotModuleLogic/BaseBuilderQueueManager.cs +++ b/OpenRA.Mods.Common/Traits/BotModules/BotModuleLogic/BaseBuilderQueueManager.cs @@ -220,10 +220,10 @@ namespace OpenRA.Mods.Common.Traits if (!actors.Contains(actor.Name)) return false; - if (!baseBuilder.Info.BuildingLimits.ContainsKey(actor.Name)) + if (!baseBuilder.Info.BuildingLimits.TryGetValue(actor.Name, out var limit)) return true; - return playerBuildings.Count(a => a.Info.Name == actor.Name) < baseBuilder.Info.BuildingLimits[actor.Name]; + return playerBuildings.Count(a => a.Info.Name == actor.Name) < limit; }); if (orderBy != null) diff --git a/OpenRA.Mods.Common/Traits/DockClientManager.cs b/OpenRA.Mods.Common/Traits/DockClientManager.cs index 5ab8081791..43678d7b8d 100644 --- a/OpenRA.Mods.Common/Traits/DockClientManager.cs +++ b/OpenRA.Mods.Common/Traits/DockClientManager.cs @@ -308,11 +308,9 @@ namespace OpenRA.Mods.Common.Traits clientActor, lookup.Keys, clientActor.Location, BlockedByActor.None, location => { - if (!lookup.ContainsKey(location)) + if (!lookup.TryGetValue(location, out var dock)) return 0; - var dock = lookup[location]; - // Prefer docks with less occupancy (multiplier is to offset distance cost): // TODO: add custom wieghts. E.g. owner vs allied. return dock.Trait.ReservationCount * client.OccupancyCostModifier; diff --git a/OpenRA.Mods.Common/Traits/Infantry/TerrainModifiesDamage.cs b/OpenRA.Mods.Common/Traits/Infantry/TerrainModifiesDamage.cs index 31816fe18d..17d37543ce 100644 --- a/OpenRA.Mods.Common/Traits/Infantry/TerrainModifiesDamage.cs +++ b/OpenRA.Mods.Common/Traits/Infantry/TerrainModifiesDamage.cs @@ -51,10 +51,10 @@ namespace OpenRA.Mods.Common.Traits var pos = map.CellContaining(self.CenterPosition); var terrainType = map.GetTerrainInfo(pos).Type; - if (!Info.TerrainModifier.ContainsKey(terrainType)) + if (!Info.TerrainModifier.TryGetValue(terrainType, out var modifiedDamage)) return FullDamage; - return Info.TerrainModifier[terrainType]; + return modifiedDamage; } } } diff --git a/OpenRA.Mods.Common/Traits/Player/GrantConditionOnPrerequisiteManager.cs b/OpenRA.Mods.Common/Traits/Player/GrantConditionOnPrerequisiteManager.cs index ea25a689b2..12f146871b 100644 --- a/OpenRA.Mods.Common/Traits/Player/GrantConditionOnPrerequisiteManager.cs +++ b/OpenRA.Mods.Common/Traits/Player/GrantConditionOnPrerequisiteManager.cs @@ -42,13 +42,13 @@ namespace OpenRA.Mods.Common.Traits public void Register(Actor actor, GrantConditionOnPrerequisite u, string[] prerequisites) { var key = MakeKey(prerequisites); - if (!upgradables.ContainsKey(key)) + if (!upgradables.TryGetValue(key, out var list)) { - upgradables.Add(key, new List<(Actor, GrantConditionOnPrerequisite)>()); + upgradables.Add(key, list = new List<(Actor, GrantConditionOnPrerequisite)>()); techTree.Add(key, prerequisites, 0, this); } - upgradables[key].Add((actor, u)); + list.Add((actor, u)); // Notify the current state u.PrerequisitesUpdated(actor, techTree.HasPrerequisites(prerequisites)); diff --git a/OpenRA.Mods.Common/Traits/SupportPowers/SupportPowerManager.cs b/OpenRA.Mods.Common/Traits/SupportPowers/SupportPowerManager.cs index 987200a515..dd47c8d738 100644 --- a/OpenRA.Mods.Common/Traits/SupportPowers/SupportPowerManager.cs +++ b/OpenRA.Mods.Common/Traits/SupportPowers/SupportPowerManager.cs @@ -59,9 +59,9 @@ namespace OpenRA.Mods.Common.Traits { var key = MakeKey(t); - if (!Powers.ContainsKey(key)) + if (!Powers.TryGetValue(key, out var spi)) { - Powers.Add(key, t.CreateInstance(key, this)); + Powers.Add(key, spi = t.CreateInstance(key, this)); if (t.Info.Prerequisites.Length > 0) { @@ -70,7 +70,7 @@ namespace OpenRA.Mods.Common.Traits } } - Powers[key].Instances.Add(t); + spi.Instances.Add(t); } } diff --git a/OpenRA.Mods.Common/UpdateRules/Rules/20231010/AbstractDocking.cs b/OpenRA.Mods.Common/UpdateRules/Rules/20231010/AbstractDocking.cs index 017a6154c5..601011f938 100644 --- a/OpenRA.Mods.Common/UpdateRules/Rules/20231010/AbstractDocking.cs +++ b/OpenRA.Mods.Common/UpdateRules/Rules/20231010/AbstractDocking.cs @@ -80,10 +80,10 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules var dockNode = new MiniYamlNodeBuilder("DockHost", ""); var lowActorName = actorNode.Key.ToLowerInvariant(); - if (!refineryNodes.ContainsKey(lowActorName) || !refineryNodes[lowActorName].Any(n => n.Key == "Type")) + if (!refineryNodes.TryGetValue(lowActorName, out var nodes) || !nodes.Any(n => n.Key == "Type")) dockNode.AddNode("Type", "Unload"); else - dockNode.AddNode(refineryNodes[lowActorName].First(n => n.Key == "Type")); + dockNode.AddNode(nodes.First(n => n.Key == "Type")); foreach (var value in moveRefineyValues) { diff --git a/OpenRA.Mods.Common/Widgets/Logic/AssetBrowserLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/AssetBrowserLogic.cs index 25d7e9ed4f..4110686cec 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/AssetBrowserLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/AssetBrowserLogic.cs @@ -622,10 +622,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic { foreach (var content in mountedPackage.Contents) { - if (!files.ContainsKey(content)) + if (!files.TryGetValue(content, out var list)) files.Add(content, new List { mountedPackage }); else - files[content].Add(mountedPackage); + list.Add(mountedPackage); } } } diff --git a/OpenRA.Utility/Program.cs b/OpenRA.Utility/Program.cs index 588ee941af..3de0a5f9bf 100644 --- a/OpenRA.Utility/Program.cs +++ b/OpenRA.Utility/Program.cs @@ -107,11 +107,11 @@ namespace OpenRA try { var command = args[0]; - if (!actions.ContainsKey(command)) + if (!actions.TryGetValue(command, out var kvp)) throw new NoSuchCommandException(command); - var action = actions[command].Key; - var validateActionArgs = actions[command].Value; + var action = kvp.Key; + var validateActionArgs = kvp.Value; if (validateActionArgs.Invoke(args)) {