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