Fix CA1854

This commit is contained in:
RoosterDragon
2023-11-15 19:46:36 +00:00
committed by Pavel Penev
parent c2568ebd1f
commit c8efc5fdd7
19 changed files with 44 additions and 48 deletions

View File

@@ -200,18 +200,18 @@ namespace OpenRA
static string[] YamlList(Dictionary<string, MiniYaml> yaml, string key) static string[] YamlList(Dictionary<string, MiniYaml> yaml, string key)
{ {
if (!yaml.ContainsKey(key)) if (!yaml.TryGetValue(key, out var value))
return Array.Empty<string>(); return Array.Empty<string>();
return yaml[key].Nodes.Select(n => n.Key).ToArray(); return value.Nodes.Select(n => n.Key).ToArray();
} }
static IReadOnlyDictionary<string, string> YamlDictionary(Dictionary<string, MiniYaml> yaml, string key) static IReadOnlyDictionary<string, string> YamlDictionary(Dictionary<string, MiniYaml> yaml, string key)
{ {
if (!yaml.ContainsKey(key)) if (!yaml.TryGetValue(key, out var value))
return new Dictionary<string, string>(); return new Dictionary<string, string>();
return yaml[key].ToDictionary(my => my.Value); return value.ToDictionary(my => my.Value);
} }
public bool Contains<T>() where T : IGlobalModData public bool Contains<T>() where T : IGlobalModData

View File

@@ -348,8 +348,8 @@ namespace OpenRA
while (this[uid].Status != MapStatus.Available) while (this[uid].Status != MapStatus.Available)
{ {
if (mapUpdates.ContainsKey(uid)) if (mapUpdates.TryGetValue(uid, out var newUid))
uid = mapUpdates[uid]; uid = newUid;
else else
return null; return null;
} }

View File

@@ -152,8 +152,8 @@ namespace OpenRA
for (var i = 0; i < p.Length; i++) for (var i = 0; i < p.Length; i++)
{ {
var key = p[i].Name; var key = p[i].Name;
if (!args.ContainsKey(key)) throw new InvalidOperationException($"ObjectCreator: key `{key}' not found"); if (!args.TryGetValue(key, out var arg)) throw new InvalidOperationException($"ObjectCreator: key `{key}' not found");
a[i] = args[key]; a[i] = arg;
} }
return ctor.Invoke(a); return ctor.Invoke(a);

View File

@@ -380,17 +380,17 @@ namespace OpenRA
if (voicedActor != null) 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."); throw new InvalidOperationException($"Can't find {definition} in voice pool.");
pool = rules.VoicePools.Value[definition]; pool = p;
} }
else 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."); throw new InvalidOperationException($"Can't find {definition} in notification pool.");
pool = rules.NotificationsPools.Value[definition]; pool = p;
} }
var clip = pool.GetNext(); var clip = pool.GetNext();

View File

@@ -257,10 +257,10 @@ namespace OpenRA.UtilityCommands
var validChildTypes = new List<(MiniYamlNodeBuilder Node, string Type, string Value)>(); var validChildTypes = new List<(MiniYamlNodeBuilder Node, string Type, string Value)>();
foreach (var childNode in node.Value.Nodes) foreach (var childNode in node.Value.Nodes)
{ {
if (translatables.ContainsKey(nodeType)) if (translatables.TryGetValue(nodeType, out var fieldName))
{ {
var childType = childNode.Key.Split('@')[0]; var childType = childNode.Key.Split('@')[0];
if (translatables[nodeType].Contains(childType) if (fieldName.Contains(childType)
&& !string.IsNullOrEmpty(childNode.Value.Value) && !string.IsNullOrEmpty(childNode.Value.Value)
&& !IsAlreadyTranslated(childNode.Value.Value) && !IsAlreadyTranslated(childNode.Value.Value)
&& childNode.Value.Value.Any(char.IsLetterOrDigit)) && childNode.Value.Value.Any(char.IsLetterOrDigit))

View File

@@ -96,11 +96,11 @@ namespace OpenRA.Mods.Cnc.Traits
public bool HasModelSequence(string model, string sequence) public bool HasModelSequence(string model, string sequence)
{ {
if (!models.ContainsKey(model)) if (!models.TryGetValue(model, out var sequences))
throw new InvalidOperationException( throw new InvalidOperationException(
$"Model `{model}` does not have any sequences defined."); $"Model `{model}` does not have any sequences defined.");
return models[model].ContainsKey(sequence); return sequences.ContainsKey(sequence);
} }
public IVertexBuffer<ModelVertex> VertexBuffer => loader.VertexBuffer; public IVertexBuffer<ModelVertex> VertexBuffer => loader.VertexBuffer;

View File

@@ -262,9 +262,9 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
var name = entries[1].ToLowerInvariant(); var name = entries[1].ToLowerInvariant();
if (DeployableActors.ContainsKey(name)) if (DeployableActors.TryGetValue(name, out var n))
{ {
name = DeployableActors[name]; name = n;
isDeployed = true; isDeployed = true;
} }

View File

@@ -91,8 +91,8 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
var res = (Type: (byte)0, Index: (byte)0); var res = (Type: (byte)0, Index: (byte)0);
var type = kv.Value.ToLowerInvariant(); var type = kv.Value.ToLowerInvariant();
if (OverlayResourceMapping.ContainsKey(type)) if (OverlayResourceMapping.TryGetValue(type, out var r))
res = OverlayResourceMapping[type]; res = r;
Map.Resources[cell] = new ResourceTile(res.Type, res.Index); Map.Resources[cell] = new ResourceTile(res.Type, res.Index);
if (OverlayActors.Contains(type)) if (OverlayActors.Contains(type))

View File

@@ -285,10 +285,10 @@ namespace OpenRA.Mods.Common.Lint
var isAttribute = !string.IsNullOrEmpty(attribute); var isAttribute = !string.IsNullOrEmpty(attribute);
var keyWithAtrr = isAttribute ? $"{key}.{attribute}" : key; var keyWithAtrr = isAttribute ? $"{key}.{attribute}" : key;
if (!referencedVariablesPerKey.ContainsKey(keyWithAtrr)) if (!referencedVariablesPerKey.TryGetValue(keyWithAtrr, out var referencedVariables))
return; return;
foreach (var referencedVariable in referencedVariablesPerKey[keyWithAtrr]) foreach (var referencedVariable in referencedVariables)
if (!variableReferences.Contains(referencedVariable)) if (!variableReferences.Contains(referencedVariable))
emitError(isAttribute ? emitError(isAttribute ?
$"Missing variable `{referencedVariable}` for attribute `{attribute}` of key `{key}` in {file}." : $"Missing variable `{referencedVariable}` for attribute `{attribute}` of key `{key}` in {file}." :
@@ -302,7 +302,7 @@ namespace OpenRA.Mods.Common.Lint
variableReferences.Add(varName); variableReferences.Add(varName);
if (!referencedVariablesPerKey.ContainsKey(keyWithAtrr) || !referencedVariablesPerKey[keyWithAtrr].Contains(varName)) if (!referencedVariablesPerKey.TryGetValue(keyWithAtrr, out var referencedVariables) || !referencedVariables.Contains(varName))
emitWarning(isAttribute ? emitWarning(isAttribute ?
$"Unused variable `{varName}` for attribute `{attribute}` of key `{key}` in {file}." : $"Unused variable `{varName}` for attribute `{attribute}` of key `{key}` in {file}." :
$"Unused variable `{varName}` for key `{key}` in {file}."); $"Unused variable `{varName}` for key `{key}` in {file}.");

View File

@@ -292,10 +292,10 @@ namespace OpenRA.Mods.Common.Scripting
{ {
var queue = GetBuildableInfo(actorType).Queue.First(); var queue = GetBuildableInfo(actorType).Queue.First();
if (!queues.ContainsKey(queue)) if (!queues.TryGetValue(queue, out var cpq))
return true; return true;
return productionHandlers.ContainsKey(queue) || queues[queue].AllQueued().Any(); return productionHandlers.ContainsKey(queue) || cpq.AllQueued().Any();
} }
BuildableInfo GetBuildableInfo(string actorType) BuildableInfo GetBuildableInfo(string actorType)

View File

@@ -361,14 +361,12 @@ namespace OpenRA.Mods.Common.Server
{ {
lock (server.LobbyInfo) lock (server.LobbyInfo)
{ {
if (!server.LobbyInfo.Slots.ContainsKey(s)) if (!server.LobbyInfo.Slots.TryGetValue(s, out var slot))
{ {
Log.Write("server", $"Invalid slot: {s}"); Log.Write("server", $"Invalid slot: {s}");
return false; return false;
} }
var slot = server.LobbyInfo.Slots[s];
if (slot.Closed || server.LobbyInfo.ClientInSlot(s) != null) if (slot.Closed || server.LobbyInfo.ClientInSlot(s) != null)
return false; return false;

View File

@@ -220,10 +220,10 @@ namespace OpenRA.Mods.Common.Traits
if (!actors.Contains(actor.Name)) if (!actors.Contains(actor.Name))
return false; return false;
if (!baseBuilder.Info.BuildingLimits.ContainsKey(actor.Name)) if (!baseBuilder.Info.BuildingLimits.TryGetValue(actor.Name, out var limit))
return true; 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) if (orderBy != null)

View File

@@ -308,11 +308,9 @@ namespace OpenRA.Mods.Common.Traits
clientActor, lookup.Keys, clientActor.Location, BlockedByActor.None, clientActor, lookup.Keys, clientActor.Location, BlockedByActor.None,
location => location =>
{ {
if (!lookup.ContainsKey(location)) if (!lookup.TryGetValue(location, out var dock))
return 0; return 0;
var dock = lookup[location];
// Prefer docks with less occupancy (multiplier is to offset distance cost): // Prefer docks with less occupancy (multiplier is to offset distance cost):
// TODO: add custom wieghts. E.g. owner vs allied. // TODO: add custom wieghts. E.g. owner vs allied.
return dock.Trait.ReservationCount * client.OccupancyCostModifier; return dock.Trait.ReservationCount * client.OccupancyCostModifier;

View File

@@ -51,10 +51,10 @@ namespace OpenRA.Mods.Common.Traits
var pos = map.CellContaining(self.CenterPosition); var pos = map.CellContaining(self.CenterPosition);
var terrainType = map.GetTerrainInfo(pos).Type; var terrainType = map.GetTerrainInfo(pos).Type;
if (!Info.TerrainModifier.ContainsKey(terrainType)) if (!Info.TerrainModifier.TryGetValue(terrainType, out var modifiedDamage))
return FullDamage; return FullDamage;
return Info.TerrainModifier[terrainType]; return modifiedDamage;
} }
} }
} }

View File

@@ -42,13 +42,13 @@ namespace OpenRA.Mods.Common.Traits
public void Register(Actor actor, GrantConditionOnPrerequisite u, string[] prerequisites) public void Register(Actor actor, GrantConditionOnPrerequisite u, string[] prerequisites)
{ {
var key = MakeKey(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); techTree.Add(key, prerequisites, 0, this);
} }
upgradables[key].Add((actor, u)); list.Add((actor, u));
// Notify the current state // Notify the current state
u.PrerequisitesUpdated(actor, techTree.HasPrerequisites(prerequisites)); u.PrerequisitesUpdated(actor, techTree.HasPrerequisites(prerequisites));

View File

@@ -59,9 +59,9 @@ namespace OpenRA.Mods.Common.Traits
{ {
var key = MakeKey(t); 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) if (t.Info.Prerequisites.Length > 0)
{ {
@@ -70,7 +70,7 @@ namespace OpenRA.Mods.Common.Traits
} }
} }
Powers[key].Instances.Add(t); spi.Instances.Add(t);
} }
} }

View File

@@ -80,10 +80,10 @@ namespace OpenRA.Mods.Common.UpdateRules.Rules
var dockNode = new MiniYamlNodeBuilder("DockHost", ""); var dockNode = new MiniYamlNodeBuilder("DockHost", "");
var lowActorName = actorNode.Key.ToLowerInvariant(); 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"); dockNode.AddNode("Type", "Unload");
else else
dockNode.AddNode(refineryNodes[lowActorName].First(n => n.Key == "Type")); dockNode.AddNode(nodes.First(n => n.Key == "Type"));
foreach (var value in moveRefineyValues) foreach (var value in moveRefineyValues)
{ {

View File

@@ -622,10 +622,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{ {
foreach (var content in mountedPackage.Contents) foreach (var content in mountedPackage.Contents)
{ {
if (!files.ContainsKey(content)) if (!files.TryGetValue(content, out var list))
files.Add(content, new List<IReadOnlyPackage> { mountedPackage }); files.Add(content, new List<IReadOnlyPackage> { mountedPackage });
else else
files[content].Add(mountedPackage); list.Add(mountedPackage);
} }
} }
} }

View File

@@ -107,11 +107,11 @@ namespace OpenRA
try try
{ {
var command = args[0]; var command = args[0];
if (!actions.ContainsKey(command)) if (!actions.TryGetValue(command, out var kvp))
throw new NoSuchCommandException(command); throw new NoSuchCommandException(command);
var action = actions[command].Key; var action = kvp.Key;
var validateActionArgs = actions[command].Value; var validateActionArgs = kvp.Value;
if (validateActionArgs.Invoke(args)) if (validateActionArgs.Invoke(args))
{ {