Skip to content

Commit

Permalink
Fix CA1854
Browse files Browse the repository at this point in the history
  • Loading branch information
RoosterDragon committed Nov 15, 2023
1 parent 138916e commit e4d3f68
Show file tree
Hide file tree
Showing 19 changed files with 44 additions and 48 deletions.
8 changes: 4 additions & 4 deletions OpenRA.Game/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,18 @@ public void LoadCustomData(ObjectCreator oc)

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 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)
{
if (!yaml.ContainsKey(key))
if (!yaml.TryGetValue(key, out var value))
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
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/Map/MapCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ public string GetUpdatedMap(string uid)

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;
}
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/ObjectCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ public object CreateUsingArgs(ConstructorInfo ctor, Dictionary<string, object> a
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);
Expand Down
8 changes: 4 additions & 4 deletions OpenRA.Game/Sound/Sound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,17 +380,17 @@ public float VideoVolume

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();
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Game/UtilityCommands/ExtractChromeStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ static void FromChromeLayout(MiniYamlNodeBuilder node, Dictionary<string, string
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))
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Cnc/Traits/World/VoxelCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ public IModel GetModelSequence(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(
$"Model `{model}` does not have any sequences defined.");

return models[model].ContainsKey(sequence);
return sequences.ContainsKey(sequence);
}

public IVertexBuffer<ModelVertex> VertexBuffer => loader.VertexBuffer;
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Cnc/UtilityCommands/ImportGen2MapCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ protected virtual void ReadActors(Map map, IniFile file, string type, int2 fullS

var name = entries[1].ToLowerInvariant();

if (DeployableActors.ContainsKey(name))
if (DeployableActors.TryGetValue(name, out var n))
{
name = DeployableActors[name];
name = n;
isDeployed = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ void ReadOverlay(IniFile file)

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))
Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Mods.Common/Lint/CheckTranslationReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ void CheckMissingVariable(string key, string attribute, Action<string> emitError
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}." :
Expand All @@ -302,7 +302,7 @@ void CheckVariableReference(string varName, string key, string attribute, Action

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}.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ public bool IsProducing(string actorType)
{
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)
Expand Down
4 changes: 1 addition & 3 deletions OpenRA.Mods.Common/ServerTraits/LobbyCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,12 @@ static bool Slot(S server, Connection conn, Session.Client client, string s)
{
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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ ActorInfo GetProducibleBuilding(HashSet<string> actors, IEnumerable<ActorInfo> b
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)
Expand Down
4 changes: 1 addition & 3 deletions OpenRA.Mods.Common/Traits/DockClientManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,9 @@ public static class DockExts
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;
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Traits/Infantry/TerrainModifiesDamage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ int IDamageModifier.GetDamageModifier(Actor attacker, Damage damage)
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ static string MakeKey(string[] prerequisites)
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ void ActorAdded(Actor a)
{
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)
{
Expand All @@ -70,7 +70,7 @@ void ActorAdded(Actor a)
}
}

Powers[key].Instances.Add(t);
spi.Instances.Add(t);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNod
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)
{
Expand Down
4 changes: 2 additions & 2 deletions OpenRA.Mods.Common/Widgets/Logic/AssetBrowserLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,10 @@ void PopulateAssetList()
{
foreach (var content in mountedPackage.Contents)
{
if (!files.ContainsKey(content))
if (!files.TryGetValue(content, out var list))
files.Add(content, new List<IReadOnlyPackage> { mountedPackage });
else
files[content].Add(mountedPackage);
list.Add(mountedPackage);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Utility/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ static void Run(string[] args)
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))
{
Expand Down

0 comments on commit e4d3f68

Please sign in to comment.