added ReadAllLines() for stream; rewrote TerrainColorSet to use it

This commit is contained in:
Chris Forbes
2010-05-12 17:53:09 +12:00
parent 8cd38812d9
commit fb592b90d5
2 changed files with 31 additions and 36 deletions

View File

@@ -60,5 +60,18 @@ namespace OpenRA
return data; return data;
} }
} }
public static IEnumerable<string> ReadAllLines(this Stream s)
{
using (var sr = new StreamReader(s))
for (; ; )
{
var line = sr.ReadLine();
if (line == null)
yield break;
else
yield return line;
}
}
} }
} }

View File

@@ -18,52 +18,34 @@
*/ */
#endregion #endregion
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System; using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace OpenRA.FileFormats namespace OpenRA.FileFormats
{ {
public class TerrainColorSet public class TerrainColorSet
{ {
public readonly Dictionary<TerrainType, Color> colors = new Dictionary<TerrainType, Color>(); readonly Dictionary<TerrainType, Color> colors;
string NextLine( StreamReader reader ) public TerrainColorSet(string colorFile)
{ {
string ret; var lines = FileSystem.Open(colorFile).ReadAllLines()
do .Select(l => l.Trim())
{ .Where(l => !l.StartsWith(";") && l.Length > 0);
ret = reader.ReadLine();
if( ret == null ) colors = lines.Select(l => l.Split('=')).ToDictionary(
return null; kv => (TerrainType)Enum.Parse(typeof(TerrainType), kv[0]),
ret = ret.Trim(); kv => ColorFromRgbString(kv[1]));
}
while( ret.Length == 0 || ret[ 0 ] == ';' );
return ret;
} }
public TerrainColorSet( string colorFile ) static Color ColorFromRgbString(string s)
{ {
StreamReader file = new StreamReader( FileSystem.Open(colorFile) ); var parts = s.Split(',');
return Color.FromArgb(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]));
while( true )
{
string line = NextLine( file );
if( line == null )
break;
string[] kv = line.Split('=');
TerrainType key = (TerrainType)Enum.Parse(typeof(TerrainType),kv[0]);
string[] entries = kv[1].Split(',');
Color val = Color.FromArgb(int.Parse(entries[0]),int.Parse(entries[1]),int.Parse(entries[2]));
colors.Add(key,val);
} }
file.Close(); public Color ColorForTerrainType(TerrainType type) { return colors[type]; }
}
public Color ColorForTerrainType(TerrainType type)
{
return colors[type];
}
} }
} }