From f83c9fd4d7b26cf4e5589f8a8899524be0aa07d7 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sun, 30 Oct 2011 10:38:02 +1300 Subject: [PATCH] add IEnum.JoinWith, use it to clean up a bunch of things --- OpenRA.FileFormats/Exts.cs | 5 +++++ OpenRA.FileFormats/FieldLoader.cs | 2 +- OpenRA.FileFormats/MiniYaml.cs | 4 ++-- OpenRA.FileFormats/Primitives/Bits.cs | 2 +- OpenRA.Game/Game.cs | 2 +- OpenRA.Game/GameRules/ActorInfo.cs | 4 ++-- OpenRA.Game/Group.cs | 2 +- OpenRA.Game/Map.cs | 2 +- OpenRA.Game/Orders/UnitOrderGenerator.cs | 2 +- OpenRA.Game/Widgets/WidgetUtils.cs | 2 +- .../Widgets/Logic/ProductionTooltipLogic.cs | 2 +- OpenRA.Mods.RA/ServerTraits/LobbyCommands.cs | 4 ++-- OpenRA.Mods.RA/ServerTraits/MasterServerPinger.cs | 2 +- OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs | 4 ++-- OpenRA.Mods.RA/Widgets/Logic/ServerBrowserLogic.cs | 13 +++++++++---- OpenRA.Renderer.SdlCommon/SdlGraphics.cs | 4 ++-- OpenRA.Utility/Program.cs | 4 ++-- 17 files changed, 35 insertions(+), 25 deletions(-) diff --git a/OpenRA.FileFormats/Exts.cs b/OpenRA.FileFormats/Exts.cs index 117a6c61a2..d48cc49b49 100755 --- a/OpenRA.FileFormats/Exts.cs +++ b/OpenRA.FileFormats/Exts.cs @@ -171,5 +171,10 @@ namespace OpenRA ++v; return v; } + + public static string JoinWith(this IEnumerable ts, string j) + { + return string.Join(j, ts.Select(t => t.ToString()).ToArray()); + } } } diff --git a/OpenRA.FileFormats/FieldLoader.cs b/OpenRA.FileFormats/FieldLoader.cs index ba2f182bf2..6a16dd73ef 100755 --- a/OpenRA.FileFormats/FieldLoader.cs +++ b/OpenRA.FileFormats/FieldLoader.cs @@ -330,7 +330,7 @@ namespace OpenRA.FileFormats if (f.FieldType.IsArray) { var elems = ((Array)v).OfType(); - return string.Join(",", elems.Select(a => a.ToString()).ToArray()); + return elems.JoinWith(","); } return v.ToString(); diff --git a/OpenRA.FileFormats/MiniYaml.cs b/OpenRA.FileFormats/MiniYaml.cs index 6ffa667060..d999533d69 100755 --- a/OpenRA.FileFormats/MiniYaml.cs +++ b/OpenRA.FileFormats/MiniYaml.cs @@ -228,7 +228,7 @@ namespace OpenRA.FileFormats if (throwErrors) if (noInherit.ContainsValue(false)) throw new YamlException("Bogus yaml removals: {0}".F( - string.Join(", ", noInherit.Where(x => !x.Value).Select(x => x.Key).ToArray()))); + noInherit.Where(x => !x.Value).JoinWith(", "))); return ret; } @@ -271,7 +271,7 @@ namespace OpenRA.FileFormats public static string WriteToString(this MiniYamlNodes y) { - return string.Join("\n", y.ToLines(true).Select(x => x.TrimEnd()).ToArray()); + return y.ToLines(true).Select(x => x.TrimEnd()).JoinWith("\n"); } public static IEnumerable ToLines(this MiniYamlNodes y, bool lowest) diff --git a/OpenRA.FileFormats/Primitives/Bits.cs b/OpenRA.FileFormats/Primitives/Bits.cs index 92bdb945ec..2f733b1618 100644 --- a/OpenRA.FileFormats/Primitives/Bits.cs +++ b/OpenRA.FileFormats/Primitives/Bits.cs @@ -57,7 +57,7 @@ namespace OpenRA.FileFormats public override string ToString() { - return string.Join(",", BitAllocator.GetStrings(Value).ToArray()); + return BitAllocator.GetStrings(Value).JoinWith(","); } public override int GetHashCode() { return Value.GetHashCode(); } diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index fc1b924902..a536661221 100755 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -280,7 +280,7 @@ namespace OpenRA // Discard any invalid mods var mm = mods.Where( m => Mod.AllMods.ContainsKey( m ) ).ToArray(); - Console.WriteLine("Loading mods: {0}",string.Join(",",mm)); + Console.WriteLine("Loading mods: {0}", mm.JoinWith(",")); Settings.Game.Mods = mm; Settings.Save(); diff --git a/OpenRA.Game/GameRules/ActorInfo.cs b/OpenRA.Game/GameRules/ActorInfo.cs index 8e39d30bbd..c937af2af5 100644 --- a/OpenRA.Game/GameRules/ActorInfo.cs +++ b/OpenRA.Game/GameRules/ActorInfo.cs @@ -102,8 +102,8 @@ namespace OpenRA else if (++index >= t.Count) throw new InvalidOperationException("Trait prerequisites not satisfied (or prerequisite loop) Actor={0} Unresolved={1} Missing={2}".F( Name, - string.Join(",", t.Select(x => x.GetType().Name).ToArray()), - string.Join(",", unsatisfied.Select(x => x.Name).ToArray()))); + t.Select(x => x.GetType().Name).JoinWith(","), + unsatisfied.Select(x => x.Name).JoinWith(","))); } return ret; diff --git a/OpenRA.Game/Group.cs b/OpenRA.Game/Group.cs index 957cb853ec..854d07924b 100644 --- a/OpenRA.Game/Group.cs +++ b/OpenRA.Game/Group.cs @@ -36,7 +36,7 @@ namespace OpenRA { /* debug crap */ Game.Debug("Group #{0}: {1}".F( - id, string.Join(",", actors.Select(a => "#{0} {1}".F(a.ActorID, a.Info.Name)).ToArray()))); + id, actors.Select(a => "#{0} {1}".F(a.ActorID, a.Info.Name)).JoinWith(","))); } /* todo: add lazy group path crap, groupleader, pruning, etc */ diff --git a/OpenRA.Game/Map.cs b/OpenRA.Game/Map.cs index 9fd4de3b9b..5174b2aa2e 100644 --- a/OpenRA.Game/Map.cs +++ b/OpenRA.Game/Map.cs @@ -343,7 +343,7 @@ namespace OpenRA if (!OpenRA.Rules.TileSets.ContainsKey(Tileset)) throw new InvalidOperationException( "Tileset used by the map ({0}) does not exist in this mod. Valid tilesets are: {1}" - .F(Tileset, string.Join(",", OpenRA.Rules.TileSets.Keys.ToArray()))); + .F(Tileset, OpenRA.Rules.TileSets.Keys.JoinWith(","))); // Tile data for (var i = 0; i < MapSize.X; i++) diff --git a/OpenRA.Game/Orders/UnitOrderGenerator.cs b/OpenRA.Game/Orders/UnitOrderGenerator.cs index 69216a1de3..ae1db35602 100644 --- a/OpenRA.Game/Orders/UnitOrderGenerator.cs +++ b/OpenRA.Game/Orders/UnitOrderGenerator.cs @@ -37,7 +37,7 @@ namespace OpenRA.Orders if (actorsInvolved.Any()) yield return new Order("CreateGroup", actorsInvolved.First().Owner.PlayerActor, false) { - TargetString = string.Join(",", actorsInvolved.Select(a => a.ActorID.ToString()).ToArray()) + TargetString = actorsInvolved.Select(a => a.ActorID).JoinWith(",") }; diff --git a/OpenRA.Game/Widgets/WidgetUtils.cs b/OpenRA.Game/Widgets/WidgetUtils.cs index 0605b174c5..4c359d001d 100644 --- a/OpenRA.Game/Widgets/WidgetUtils.cs +++ b/OpenRA.Game/Widgets/WidgetUtils.cs @@ -203,7 +203,7 @@ namespace OpenRA.Widgets else break; } - return string.Join("\n", newLines.ToArray()); + return newLines.JoinWith("\n"); } return text; } diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs index cc7884efed..3a320ee12b 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTooltipLogic.cs @@ -52,7 +52,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic nameLabel.GetText = () => tooltip.Name; var prereqs = buildable.Prerequisites.Select(a => ActorName(a)); - var requiresString = prereqs.Any() ? "Requires {0}".F(string.Join(", ", prereqs.ToArray())) : ""; + var requiresString = prereqs.Any() ? "Requires {0}".F(prereqs.JoinWith(", ")) : ""; requiresLabel.GetText = () => requiresString; var power = bi != null ? bi.Power : 0; diff --git a/OpenRA.Mods.RA/ServerTraits/LobbyCommands.cs b/OpenRA.Mods.RA/ServerTraits/LobbyCommands.cs index 63a2d67bf1..5914d46e93 100644 --- a/OpenRA.Mods.RA/ServerTraits/LobbyCommands.cs +++ b/OpenRA.Mods.RA/ServerTraits/LobbyCommands.cs @@ -184,7 +184,7 @@ namespace OpenRA.Mods.RA.Server var slot = server.lobbyInfo.Slots[parts[0]]; var bot = server.lobbyInfo.ClientInSlot(parts[0]); - var botType = string.Join(" ", parts.Skip(1).ToArray() ); + var botType = parts.Skip(1).JoinWith(" "); // Invalid slot if (bot != null && bot.Bot == null) @@ -419,7 +419,7 @@ namespace OpenRA.Mods.RA.Server }; var cmdName = cmd.Split(' ').First(); - var cmdValue = string.Join(" ", cmd.Split(' ').Skip(1).ToArray()); + var cmdValue = cmd.Split(' ').Skip(1).JoinWith(" "); Func a; if (!dict.TryGetValue(cmdName, out a)) diff --git a/OpenRA.Mods.RA/ServerTraits/MasterServerPinger.cs b/OpenRA.Mods.RA/ServerTraits/MasterServerPinger.cs index 7dc87b32e2..be165d2aad 100644 --- a/OpenRA.Mods.RA/ServerTraits/MasterServerPinger.cs +++ b/OpenRA.Mods.RA/ServerTraits/MasterServerPinger.cs @@ -64,7 +64,7 @@ namespace OpenRA.Mods.RA.Server server.Settings.ExternalPort, Uri.EscapeUriString(server.Settings.Name), server.GameStarted ? 2 : 1, // todo: post-game states, etc. server.lobbyInfo.Clients.Count, - string.Join(",", Game.CurrentMods.Select(f => "{0}@{1}".F(f.Key, f.Value.Version)).ToArray()), + Game.CurrentMods.Select(f => "{0}@{1}".F(f.Key, f.Value.Version)).JoinWith(","), server.lobbyInfo.GlobalSettings.Map, server.Map.PlayerCount)); diff --git a/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs b/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs index 89c752a729..9511d4f3fd 100755 --- a/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs +++ b/OpenRA.Mods.RA/Widgets/BuildPaletteWidget.cs @@ -480,7 +480,7 @@ namespace OpenRA.Mods.RA.Widgets var bi = info.Traits.GetOrDefault(); if (bi != null) DrawRightAligned("{1}{0}".F(bi.Power, bi.Power > 0 ? "+" : ""), pos + new int2(-5, 20), - ((power.PowerProvided - power.PowerDrained) >= -bi.Power || bi.Power > 0)? Color.White: Color.Red); + ((power.PowerProvided - power.PowerDrained) >= -bi.Power || bi.Power > 0)? Color.White: Color.Red); p += new int2(5, 35); if (!canBuildThis) @@ -488,7 +488,7 @@ namespace OpenRA.Mods.RA.Widgets var prereqs = buildable.Prerequisites .Select( a => Description( a ) ); Game.Renderer.Fonts["Regular"].DrawText( - "Requires {0}".F(string.Join(", ", prereqs.ToArray())), + "Requires {0}".F(prereqs.JoinWith(", ")), p.ToInt2(), Color.White); diff --git a/OpenRA.Mods.RA/Widgets/Logic/ServerBrowserLogic.cs b/OpenRA.Mods.RA/Widgets/Logic/ServerBrowserLogic.cs index 9771098efe..2bba35259b 100644 --- a/OpenRA.Mods.RA/Widgets/Logic/ServerBrowserLogic.cs +++ b/OpenRA.Mods.RA/Widgets/Logic/ServerBrowserLogic.cs @@ -84,12 +84,17 @@ namespace OpenRA.Mods.RA.Widgets.Logic return (currentServer == null) ? null : Game.modData.FindMapByUid(currentServer.Map); } + static string GenerateModLabel(KeyValuePair mod) + { + if (Mod.AllMods.ContainsKey(mod.Key)) + return "{0} ({1})".F(Mod.AllMods[mod.Key].Title, mod.Value); + + return "Unknown Mod: {0}".F(mod.Key); + } + public static string GenerateModsLabel(GameServer s) { - return string.Join("\n", s.UsefulMods - .Select(m => - Mod.AllMods.ContainsKey(m.Key) ? string.Format("{0} ({1})", Mod.AllMods[m.Key].Title, m.Value) - : string.Format("Unknown Mod: {0}",m.Key)).ToArray()); + return s.UsefulMods.Select(m => GenerateModLabel(m)).JoinWith("\n"); } void RefreshServerList(IEnumerable games) diff --git a/OpenRA.Renderer.SdlCommon/SdlGraphics.cs b/OpenRA.Renderer.SdlCommon/SdlGraphics.cs index 204bb4e5d5..fdccf07e2d 100644 --- a/OpenRA.Renderer.SdlCommon/SdlGraphics.cs +++ b/OpenRA.Renderer.SdlCommon/SdlGraphics.cs @@ -74,12 +74,12 @@ namespace OpenRA.Renderer.SdlCommon if (extensions == null) Console.WriteLine("Failed to fetch GL_EXTENSIONS, this is bad."); - var missingExtensions = requiredExtensions.Where( r => !extensions.Contains(r) ).ToArray(); + var missingExtensions = requiredExtensions.Where(r => !extensions.Contains(r)).ToArray(); if (missingExtensions.Any()) { ErrorHandler.WriteGraphicsLog("Unsupported GPU: Missing extensions: {0}" - .F(string.Join(",", missingExtensions))); + .F(missingExtensions.JoinWith(","))); throw new InvalidProgramException("Unsupported GPU. See graphics.log for details."); } diff --git a/OpenRA.Utility/Program.cs b/OpenRA.Utility/Program.cs index 1c391ed9a6..2afdb2c807 100644 --- a/OpenRA.Utility/Program.cs +++ b/OpenRA.Utility/Program.cs @@ -40,8 +40,8 @@ namespace OpenRA.Utility catch( Exception e ) { Log.AddChannel("utility", "utility.log"); - Log.Write("utility", "Received args: {0}", string.Join(" ", args)); - Log.Write("utility", "{0}", e.ToString()); + Log.Write("utility", "Received args: {0}", args.JoinWith(" ")); + Log.Write("utility", "{0}", e); Console.WriteLine("Error: Utility application crashed. See utility.log for details"); throw;