Simplify collection initialization

This commit is contained in:
Eduardo Cáceres
2022-05-02 13:24:49 +02:00
committed by atlimit8
parent cae43808d9
commit aa998a46d9
9 changed files with 55 additions and 35 deletions

View File

@@ -424,8 +424,10 @@ namespace OpenRA
if (!dupKeys.TryGetValue(key, out var dupKeyMessages))
{
// Log the initial conflicting value already inserted:
dupKeyMessages = new List<string>();
dupKeyMessages.Add(logValue(d[key]));
dupKeyMessages = new List<string>
{
logValue(d[key])
};
dupKeys.Add(key, dupKeyMessages);
}

View File

@@ -154,8 +154,10 @@ namespace OpenRA
if (stringPool == null)
stringPool = new Dictionary<string, string>();
var levels = new List<List<MiniYamlNode>>();
levels.Add(new List<MiniYamlNode>());
var levels = new List<List<MiniYamlNode>>
{
new List<MiniYamlNode>()
};
var lineNo = 0;
foreach (var ll in lines)
@@ -323,8 +325,10 @@ namespace OpenRA
var resolved = new Dictionary<string, MiniYaml>(tree.Count);
foreach (var kv in tree)
{
var inherited = new Dictionary<string, MiniYamlNode.SourceLocation>();
inherited.Add(kv.Key, default(MiniYamlNode.SourceLocation));
var inherited = new Dictionary<string, MiniYamlNode.SourceLocation>
{
{ kv.Key, default(MiniYamlNode.SourceLocation) }
};
var children = ResolveInherits(kv.Value, tree, inherited);
resolved.Add(kv.Key, new MiniYaml(kv.Value.Value, children));

View File

@@ -77,10 +77,12 @@ namespace OpenRA.Network
public string Serialize()
{
var data = new List<MiniYamlNode>();
data.Add(new MiniYamlNode("Handshake", null,
new[] { "Mod", "Version", "Password", "Fingerprint", "AuthSignature", "OrdersProtocol" }.Select(p => FieldSaver.SaveField(this, p)).ToList()));
data.Add(new MiniYamlNode("Client", FieldSaver.Save(Client)));
var data = new List<MiniYamlNode>
{
new MiniYamlNode("Handshake", null,
new[] { "Mod", "Version", "Password", "Fingerprint", "AuthSignature", "OrdersProtocol" }.Select(p => FieldSaver.SaveField(this, p)).ToList()),
new MiniYamlNode("Client", FieldSaver.Save(Client))
};
return data.WriteToString();
}

View File

@@ -503,9 +503,11 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
var dy = rx + ry - fullSize.X - 1;
var cell = new MPos(dx / 2, dy).ToCPos(map);
var ar = new ActorReference((!int.TryParse(kv.Key, out var wpindex) || wpindex > 7) ? "waypoint" : "mpspawn");
ar.Add(new LocationInit(cell));
ar.Add(new OwnerInit("Neutral"));
var ar = new ActorReference((!int.TryParse(kv.Key, out var wpindex) || wpindex > 7) ? "waypoint" : "mpspawn")
{
new LocationInit(cell),
new OwnerInit("Neutral")
};
map.ActorDefinitions.Add(new MiniYamlNode("Actor" + map.ActorDefinitions.Count, ar.Save()));
}
@@ -524,9 +526,11 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
var cell = new MPos(dx / 2, dy).ToCPos(map);
var name = kv.Value.ToLowerInvariant();
var ar = new ActorReference(name);
ar.Add(new LocationInit(cell));
ar.Add(new OwnerInit("Neutral"));
var ar = new ActorReference(name)
{
new LocationInit(cell),
new OwnerInit("Neutral")
};
if (!map.Rules.Actors.ContainsKey(name))
Console.WriteLine($"Ignoring unknown actor type: `{name}`");

View File

@@ -100,9 +100,10 @@ namespace OpenRA.Mods.Cnc.UtilityCommands
data.AppendLine($"\t\tId: {templateIndex}");
var images = new List<string>();
images.Add($"{sectionFilename}{i:D2}.{extension}");
var images = new List<string>
{
$"{sectionFilename}{i:D2}.{extension}"
};
for (var v = 'a'; v <= 'z'; v++)
{
var variant = $"{sectionFilename}{i:D2}{v}.{extension}";

View File

@@ -35,9 +35,10 @@ namespace OpenRA.Mods.Common.Scripting
if (!Context.World.Map.Rules.Actors.TryGetValue(actorType, out var ai))
throw new LuaException($"Unknown actor type '{actorType}'");
var initDict = new TypeDictionary();
initDict.Add(new OwnerInit(owner));
var initDict = new TypeDictionary
{
new OwnerInit(owner)
};
if (entryLocation.HasValue)
{

View File

@@ -161,9 +161,11 @@ namespace OpenRA.Mods.Common.Traits
if (specificOwnerInfo != null && !specificOwnerInfo.ValidOwnerNames.Contains(ownerName))
ownerName = specificOwnerInfo.ValidOwnerNames.First();
var reference = new ActorReference(actor.Name);
reference.Add(new OwnerInit(ownerName));
reference.Add(new FactionInit(owner.Faction));
var reference = new ActorReference(actor.Name)
{
new OwnerInit(ownerName),
new FactionInit(owner.Faction)
};
var worldPx = wr.Viewport.ViewToWorldPx(Viewport.LastMousePos) - wr.ScreenPxOffset(actorCenterOffset);
var cell = wr.Viewport.ViewToWorld(wr.Viewport.WorldToViewPx(worldPx));

View File

@@ -181,9 +181,11 @@ namespace OpenRA.Mods.Common.Widgets.Logic
continue;
var actor = a.Actor;
var td = new TypeDictionary();
td.Add(new OwnerInit(selectedOwner.Name));
td.Add(new FactionInit(selectedOwner.Faction));
var td = new TypeDictionary
{
new OwnerInit(selectedOwner.Name),
new FactionInit(selectedOwner.Faction)
};
foreach (var api in actor.TraitInfos<IActorPreviewInitInfo>())
foreach (var o in api.ActorPreviewInits(actor, ActorPreviewType.MapEditorSidebar))
td.Add(o);

View File

@@ -418,13 +418,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
? new Rectangle(0, 0, Game.Renderer.Resolution.Width, Game.Renderer.Resolution.Height)
: w.Parent.Bounds;
var substitutions = new Dictionary<string, int>();
substitutions.Add("WINDOW_RIGHT", Game.Renderer.Resolution.Width);
substitutions.Add("WINDOW_BOTTOM", Game.Renderer.Resolution.Height);
substitutions.Add("PARENT_RIGHT", parentBounds.Width);
substitutions.Add("PARENT_LEFT", parentBounds.Left);
substitutions.Add("PARENT_TOP", parentBounds.Top);
substitutions.Add("PARENT_BOTTOM", parentBounds.Height);
var substitutions = new Dictionary<string, int>
{
{ "WINDOW_RIGHT", Game.Renderer.Resolution.Width },
{ "WINDOW_BOTTOM", Game.Renderer.Resolution.Height },
{ "PARENT_RIGHT", parentBounds.Width },
{ "PARENT_LEFT", parentBounds.Left },
{ "PARENT_TOP", parentBounds.Top },
{ "PARENT_BOTTOM", parentBounds.Height }
};
var width = Evaluator.Evaluate(w.Width, substitutions);
var height = Evaluator.Evaluate(w.Height, substitutions);