add IEnum<T>.JoinWith, use it to clean up a bunch of things

This commit is contained in:
Chris Forbes
2011-10-30 10:38:02 +13:00
parent 8111ccbea6
commit f83c9fd4d7
17 changed files with 35 additions and 25 deletions

View File

@@ -171,5 +171,10 @@ namespace OpenRA
++v; ++v;
return v; return v;
} }
public static string JoinWith<T>(this IEnumerable<T> ts, string j)
{
return string.Join(j, ts.Select(t => t.ToString()).ToArray());
}
} }
} }

View File

@@ -330,7 +330,7 @@ namespace OpenRA.FileFormats
if (f.FieldType.IsArray) if (f.FieldType.IsArray)
{ {
var elems = ((Array)v).OfType<object>(); var elems = ((Array)v).OfType<object>();
return string.Join(",", elems.Select(a => a.ToString()).ToArray()); return elems.JoinWith(",");
} }
return v.ToString(); return v.ToString();

View File

@@ -228,7 +228,7 @@ namespace OpenRA.FileFormats
if (throwErrors) if (throwErrors)
if (noInherit.ContainsValue(false)) if (noInherit.ContainsValue(false))
throw new YamlException("Bogus yaml removals: {0}".F( 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; return ret;
} }
@@ -271,7 +271,7 @@ namespace OpenRA.FileFormats
public static string WriteToString(this MiniYamlNodes y) 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<string> ToLines(this MiniYamlNodes y, bool lowest) public static IEnumerable<string> ToLines(this MiniYamlNodes y, bool lowest)

View File

@@ -57,7 +57,7 @@ namespace OpenRA.FileFormats
public override string ToString() public override string ToString()
{ {
return string.Join(",", BitAllocator<T>.GetStrings(Value).ToArray()); return BitAllocator<T>.GetStrings(Value).JoinWith(",");
} }
public override int GetHashCode() { return Value.GetHashCode(); } public override int GetHashCode() { return Value.GetHashCode(); }

View File

@@ -280,7 +280,7 @@ namespace OpenRA
// Discard any invalid mods // Discard any invalid mods
var mm = mods.Where( m => Mod.AllMods.ContainsKey( m ) ).ToArray(); 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.Game.Mods = mm;
Settings.Save(); Settings.Save();

View File

@@ -102,8 +102,8 @@ namespace OpenRA
else if (++index >= t.Count) else if (++index >= t.Count)
throw new InvalidOperationException("Trait prerequisites not satisfied (or prerequisite loop) Actor={0} Unresolved={1} Missing={2}".F( throw new InvalidOperationException("Trait prerequisites not satisfied (or prerequisite loop) Actor={0} Unresolved={1} Missing={2}".F(
Name, Name,
string.Join(",", t.Select(x => x.GetType().Name).ToArray()), t.Select(x => x.GetType().Name).JoinWith(","),
string.Join(",", unsatisfied.Select(x => x.Name).ToArray()))); unsatisfied.Select(x => x.Name).JoinWith(",")));
} }
return ret; return ret;

View File

@@ -36,7 +36,7 @@ namespace OpenRA
{ {
/* debug crap */ /* debug crap */
Game.Debug("Group #{0}: {1}".F( 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 */ /* todo: add lazy group path crap, groupleader, pruning, etc */

View File

@@ -343,7 +343,7 @@ namespace OpenRA
if (!OpenRA.Rules.TileSets.ContainsKey(Tileset)) if (!OpenRA.Rules.TileSets.ContainsKey(Tileset))
throw new InvalidOperationException( throw new InvalidOperationException(
"Tileset used by the map ({0}) does not exist in this mod. Valid tilesets are: {1}" "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 // Tile data
for (var i = 0; i < MapSize.X; i++) for (var i = 0; i < MapSize.X; i++)

View File

@@ -37,7 +37,7 @@ namespace OpenRA.Orders
if (actorsInvolved.Any()) if (actorsInvolved.Any())
yield return new Order("CreateGroup", actorsInvolved.First().Owner.PlayerActor, false) 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(",")
}; };

View File

@@ -203,7 +203,7 @@ namespace OpenRA.Widgets
else else
break; break;
} }
return string.Join("\n", newLines.ToArray()); return newLines.JoinWith("\n");
} }
return text; return text;
} }

View File

@@ -52,7 +52,7 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
nameLabel.GetText = () => tooltip.Name; nameLabel.GetText = () => tooltip.Name;
var prereqs = buildable.Prerequisites.Select(a => ActorName(a)); 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; requiresLabel.GetText = () => requiresString;
var power = bi != null ? bi.Power : 0; var power = bi != null ? bi.Power : 0;

View File

@@ -184,7 +184,7 @@ namespace OpenRA.Mods.RA.Server
var slot = server.lobbyInfo.Slots[parts[0]]; var slot = server.lobbyInfo.Slots[parts[0]];
var bot = server.lobbyInfo.ClientInSlot(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 // Invalid slot
if (bot != null && bot.Bot == null) if (bot != null && bot.Bot == null)
@@ -419,7 +419,7 @@ namespace OpenRA.Mods.RA.Server
}; };
var cmdName = cmd.Split(' ').First(); var cmdName = cmd.Split(' ').First();
var cmdValue = string.Join(" ", cmd.Split(' ').Skip(1).ToArray()); var cmdValue = cmd.Split(' ').Skip(1).JoinWith(" ");
Func<string,bool> a; Func<string,bool> a;
if (!dict.TryGetValue(cmdName, out a)) if (!dict.TryGetValue(cmdName, out a))

View File

@@ -64,7 +64,7 @@ namespace OpenRA.Mods.RA.Server
server.Settings.ExternalPort, Uri.EscapeUriString(server.Settings.Name), server.Settings.ExternalPort, Uri.EscapeUriString(server.Settings.Name),
server.GameStarted ? 2 : 1, // todo: post-game states, etc. server.GameStarted ? 2 : 1, // todo: post-game states, etc.
server.lobbyInfo.Clients.Count, 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.lobbyInfo.GlobalSettings.Map,
server.Map.PlayerCount)); server.Map.PlayerCount));

View File

@@ -488,7 +488,7 @@ namespace OpenRA.Mods.RA.Widgets
var prereqs = buildable.Prerequisites var prereqs = buildable.Prerequisites
.Select( a => Description( a ) ); .Select( a => Description( a ) );
Game.Renderer.Fonts["Regular"].DrawText( Game.Renderer.Fonts["Regular"].DrawText(
"Requires {0}".F(string.Join(", ", prereqs.ToArray())), "Requires {0}".F(prereqs.JoinWith(", ")),
p.ToInt2(), p.ToInt2(),
Color.White); Color.White);

View File

@@ -84,12 +84,17 @@ namespace OpenRA.Mods.RA.Widgets.Logic
return (currentServer == null) ? null : Game.modData.FindMapByUid(currentServer.Map); return (currentServer == null) ? null : Game.modData.FindMapByUid(currentServer.Map);
} }
static string GenerateModLabel(KeyValuePair<string,string> 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) public static string GenerateModsLabel(GameServer s)
{ {
return string.Join("\n", s.UsefulMods return s.UsefulMods.Select(m => GenerateModLabel(m)).JoinWith("\n");
.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());
} }
void RefreshServerList(IEnumerable<GameServer> games) void RefreshServerList(IEnumerable<GameServer> games)

View File

@@ -74,12 +74,12 @@ namespace OpenRA.Renderer.SdlCommon
if (extensions == null) if (extensions == null)
Console.WriteLine("Failed to fetch GL_EXTENSIONS, this is bad."); 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()) if (missingExtensions.Any())
{ {
ErrorHandler.WriteGraphicsLog("Unsupported GPU: Missing extensions: {0}" ErrorHandler.WriteGraphicsLog("Unsupported GPU: Missing extensions: {0}"
.F(string.Join(",", missingExtensions))); .F(missingExtensions.JoinWith(",")));
throw new InvalidProgramException("Unsupported GPU. See graphics.log for details."); throw new InvalidProgramException("Unsupported GPU. See graphics.log for details.");
} }

View File

@@ -40,8 +40,8 @@ namespace OpenRA.Utility
catch( Exception e ) catch( Exception e )
{ {
Log.AddChannel("utility", "utility.log"); Log.AddChannel("utility", "utility.log");
Log.Write("utility", "Received args: {0}", string.Join(" ", args)); Log.Write("utility", "Received args: {0}", args.JoinWith(" "));
Log.Write("utility", "{0}", e.ToString()); Log.Write("utility", "{0}", e);
Console.WriteLine("Error: Utility application crashed. See utility.log for details"); Console.WriteLine("Error: Utility application crashed. See utility.log for details");
throw; throw;