clean up some messy GC behavior & needlessly longwinded code. slight perf cost on map save.

This commit is contained in:
Chris Forbes
2010-04-08 21:07:50 +12:00
committed by Paul Chote
parent ba24ffc442
commit ed08314ec0
3 changed files with 303 additions and 298 deletions

View File

@@ -22,6 +22,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using System.Linq; using System.Linq;
using System.IO;
namespace OpenRA namespace OpenRA
{ {
@@ -42,5 +43,22 @@ namespace OpenRA
{ {
return a.GetTypes().Select(t => t.Namespace).Distinct().Where(n => n != null); return a.GetTypes().Select(t => t.Namespace).Distinct().Where(n => n != null);
} }
public static string ReadAllText(this Stream s)
{
using (s)
using (var sr = new StreamReader(s))
return sr.ReadToEnd();
}
public static byte[] ReadAllBytes(this Stream s)
{
using (s)
{
var data = new byte[s.Length - s.Position];
s.Read(data, 0, data.Length);
return data;
}
}
} }
} }

View File

@@ -52,23 +52,23 @@ namespace OpenRA.FileFormats
public int2 TopLeft; public int2 TopLeft;
public int2 BottomRight; public int2 BottomRight;
public TileReference<ushort,byte>[ , ] MapTiles; public TileReference<ushort, byte>[,] MapTiles;
public TileReference<byte, byte>[ , ] MapResources; public TileReference<byte, byte>[,] MapResources;
// Temporary compat hacks // Temporary compat hacks
public int XOffset {get {return TopLeft.X;}} public int XOffset { get { return TopLeft.X; } }
public int YOffset {get {return TopLeft.Y;}} public int YOffset { get { return TopLeft.Y; } }
public int Width {get {return BottomRight.X-TopLeft.X;}} public int Width { get { return BottomRight.X - TopLeft.X; } }
public int Height {get {return BottomRight.Y-TopLeft.Y;}} public int Height { get { return BottomRight.Y - TopLeft.Y; } }
public string Theater {get {return Tileset;}} public string Theater { get { return Tileset; } }
public IEnumerable<int2> SpawnPoints {get {return Waypoints.Select(kv => kv.Value);}} public IEnumerable<int2> SpawnPoints { get { return Waypoints.Select(kv => kv.Value); } }
static List<string> SimpleFields = new List<string>() { static List<string> SimpleFields = new List<string>() {
"MapFormat", "Title", "Description", "Author", "PlayerCount", "Tileset", "MapSize", "TopLeft", "BottomRight" "MapFormat", "Title", "Description", "Author", "PlayerCount", "Tileset", "MapSize", "TopLeft", "BottomRight"
}; };
public Map() {} public Map() { }
public Map(IFolder package) public Map(IFolder package)
{ {
@@ -76,13 +76,13 @@ namespace OpenRA.FileFormats
var yaml = MiniYaml.FromStream(Package.GetContent("map.yaml")); var yaml = MiniYaml.FromStream(Package.GetContent("map.yaml"));
// 'Simple' metadata // 'Simple' metadata
FieldLoader.LoadFields(this,yaml,SimpleFields); FieldLoader.LoadFields(this, yaml, SimpleFields);
// Waypoints // Waypoints
foreach (var wp in yaml["Waypoints"].Nodes) foreach (var wp in yaml["Waypoints"].Nodes)
{ {
string[] loc = wp.Value.Value.Split(','); string[] loc = wp.Value.Value.Split(',');
Waypoints.Add(wp.Key, new int2(int.Parse(loc[0]),int.Parse(loc[1]))); Waypoints.Add(wp.Key, new int2(int.Parse(loc[0]), int.Parse(loc[1])));
} }
// Actors // Actors
@@ -90,8 +90,8 @@ namespace OpenRA.FileFormats
{ {
string[] vals = kv.Value.Value.Split(' '); string[] vals = kv.Value.Value.Split(' ');
string[] loc = vals[2].Split(','); string[] loc = vals[2].Split(',');
var a = new ActorReference(vals[0], new int2(int.Parse(loc[0]),int.Parse(loc[1])) ,vals[2]); var a = new ActorReference(vals[0], new int2(int.Parse(loc[0]), int.Parse(loc[1])), vals[2]);
Actors.Add(kv.Key,a); Actors.Add(kv.Key, a);
} }
// Smudges // Smudges
@@ -99,7 +99,7 @@ namespace OpenRA.FileFormats
{ {
string[] vals = kv.Key.Split(' '); string[] vals = kv.Key.Split(' ');
string[] loc = vals[1].Split(','); string[] loc = vals[1].Split(',');
Smudges.Add(new SmudgeReference(vals[0], new int2(int.Parse(loc[0]),int.Parse(loc[1])) ,int.Parse(vals[2]))); Smudges.Add(new SmudgeReference(vals[0], new int2(int.Parse(loc[0]), int.Parse(loc[1])), int.Parse(vals[2])));
} }
// Rules // Rules
@@ -117,22 +117,22 @@ namespace OpenRA.FileFormats
{ {
FieldInfo f = this.GetType().GetField(field); FieldInfo f = this.GetType().GetField(field);
if (f.GetValue(this) == null) continue; if (f.GetValue(this) == null) continue;
root.Add(field,new MiniYaml(FieldSaver.FormatValue(this,f),null)); root.Add(field, new MiniYaml(FieldSaver.FormatValue(this, f), null));
} }
root.Add("Actors",MiniYaml.FromDictionary<string,ActorReference>(Actors)); root.Add("Actors", MiniYaml.FromDictionary<string, ActorReference>(Actors));
root.Add("Waypoints",MiniYaml.FromDictionary<string,int2>(Waypoints)); root.Add("Waypoints", MiniYaml.FromDictionary<string, int2>(Waypoints));
root.Add("Smudges",MiniYaml.FromList<SmudgeReference>(Smudges)); root.Add("Smudges", MiniYaml.FromList<SmudgeReference>(Smudges));
root.Add("Rules",new MiniYaml(null,Rules)); root.Add("Rules", new MiniYaml(null, Rules));
SaveBinaryData(Path.Combine(filepath,"map.bin")); SaveBinaryData(Path.Combine(filepath, "map.bin"));
root.WriteToFile(Path.Combine(filepath,"map.yaml")); root.WriteToFile(Path.Combine(filepath, "map.yaml"));
SaveUid(Path.Combine(filepath,"map.uid")); SaveUid(Path.Combine(filepath, "map.uid"));
} }
static byte ReadByte( Stream s ) static byte ReadByte(Stream s)
{ {
int ret = s.ReadByte(); int ret = s.ReadByte();
if( ret == -1 ) if (ret == -1)
throw new NotImplementedException(); throw new NotImplementedException();
return (byte)ret; return (byte)ret;
} }
@@ -147,99 +147,90 @@ namespace OpenRA.FileFormats
public void LoadBinaryData() public void LoadBinaryData()
{ {
Stream dataStream = Package.GetContent("map.bin"); using (var dataStream = Package.GetContent("map.bin"))
{
// Load header info
byte version = ReadByte(dataStream);
var width = ReadWord(dataStream);
var height = ReadWord(dataStream);
// Load header info if (width != MapSize.X || height != MapSize.Y)
byte version = ReadByte(dataStream); throw new InvalidDataException("Invalid tile data");
var width = ReadWord(dataStream);
var height = ReadWord(dataStream);
if (width != MapSize.X || height != MapSize.Y) MapTiles = new TileReference<ushort, byte>[MapSize.X, MapSize.Y];
throw new InvalidDataException("Invalid tile data"); MapResources = new TileReference<byte, byte>[MapSize.X, MapSize.Y];
MapTiles = new TileReference<ushort, byte>[ MapSize.X, MapSize.Y ]; // Load tile data
MapResources = new TileReference<byte, byte>[ MapSize.X, MapSize.Y ]; for (int i = 0; i < MapSize.X; i++)
for (int j = 0; j < MapSize.Y; j++)
{
ushort tile = ReadWord(dataStream);
byte index = ReadByte(dataStream);
byte image = (index == byte.MaxValue) ? (byte)(i % 4 + (j % 4) * 4) : index;
MapTiles[i, j] = new TileReference<ushort, byte>(tile, index, image);
}
// Load tile data // Load resource data
for( int i = 0 ; i < MapSize.X ; i++ ) for (int i = 0; i < MapSize.X; i++)
for( int j = 0 ; j < MapSize.Y ; j++ ) for (int j = 0; j < MapSize.Y; j++)
{ MapResources[i, j] = new TileReference<byte, byte>(ReadByte(dataStream), ReadByte(dataStream));
ushort tile = ReadWord(dataStream); }
byte index = ReadByte(dataStream);
byte image = (index == byte.MaxValue) ? (byte)( i % 4 + ( j % 4 ) * 4 ) : index;
MapTiles[i,j] = new TileReference<ushort,byte>(tile,index, image);
}
// Load resource data
for( int i = 0 ; i < MapSize.X ; i++ )
for( int j = 0 ; j < MapSize.Y ; j++ )
MapResources[i,j] = new TileReference<byte,byte>(ReadByte(dataStream),ReadByte(dataStream));
} }
public void SaveBinaryData(string filepath) public void SaveBinaryData(string filepath)
{ {
FileStream dataStream = new FileStream(filepath+".tmp", FileMode.Create, FileAccess.Write); using (var dataStream = File.Create(filepath + ".tmp"))
BinaryWriter writer = new BinaryWriter( dataStream ); using (var writer = new BinaryWriter(dataStream))
writer.BaseStream.Seek(0, SeekOrigin.Begin); {
writer.BaseStream.Seek(0, SeekOrigin.Begin);
// File header consists of a version byte, followed by 2 ushorts for width and height // File header consists of a version byte, followed by 2 ushorts for width and height
writer.Write(TileFormat); writer.Write(TileFormat);
writer.Write((ushort)MapSize.X); writer.Write((ushort)MapSize.X);
writer.Write((ushort)MapSize.Y); writer.Write((ushort)MapSize.Y);
// Tile data // Tile data
for( int i = 0 ; i < MapSize.X ; i++ ) for (int i = 0; i < MapSize.X; i++)
for( int j = 0 ; j < MapSize.Y ; j++ ) for (int j = 0; j < MapSize.Y; j++)
{ {
writer.Write( MapTiles[i,j].type ); writer.Write(MapTiles[i, j].type);
writer.Write( MapTiles[i,j].index ); writer.Write(MapTiles[i, j].index);
} }
// Resource data // Resource data
for( int i = 0 ; i < MapSize.X ; i++ ) for (int i = 0; i < MapSize.X; i++)
for( int j = 0 ; j < MapSize.Y ; j++ ) for (int j = 0; j < MapSize.Y; j++)
{ {
writer.Write( MapResources[i,j].type ); writer.Write(MapResources[i, j].type);
writer.Write( MapResources[i,j].index ); writer.Write(MapResources[i, j].index);
} }
}
writer.Flush();
writer.Close();
File.Delete(filepath); File.Delete(filepath);
File.Move(filepath+".tmp",filepath); File.Move(filepath + ".tmp", filepath);
} }
public void LoadUid() public void LoadUid()
{ {
StreamReader uidStream = new StreamReader(Package.GetContent("map.uid")); Uid = Package.GetContent("map.uid").ReadAllText();
Uid = uidStream.ReadLine();
uidStream.Close();
} }
public void SaveUid(string filename) public void SaveUid(string filename)
{ {
// UID is calculated by taking an SHA1 of the yaml and binary data // UID is calculated by taking an SHA1 of the yaml and binary data
// Read the relevant data into a buffer // Read the relevant data into a buffer
var yamlStream = Package.GetContent("map.yaml"); var data = Exts.ReadAllBytes(Package.GetContent("map.yaml"))
var binaryStream = Package.GetContent("map.bin"); .Concat(Exts.ReadAllBytes(Package.GetContent("map.bin"))).ToArray();
var data = new byte[yamlStream.Length+binaryStream.Length];
yamlStream.Read(data,0,(int)yamlStream.Length);
binaryStream.Read(data,(int)yamlStream.Length,(int)binaryStream.Length);
// Take the SHA1 // Take the SHA1
using (var csp = SHA1.Create()) using (var csp = SHA1.Create())
Uid = new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray()); Uid = new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray());
// Save to file File.WriteAllText(filename, Uid);
StreamWriter file = new System.IO.StreamWriter(filename);
file.WriteLine(Uid);
file.Close();
} }
public bool IsInMap(int2 xy) public bool IsInMap(int2 xy)
{ {
return IsInMap(xy.X,xy.Y); return IsInMap(xy.X, xy.Y);
} }
public bool IsInMap(int x, int y) public bool IsInMap(int x, int y)
@@ -254,16 +245,15 @@ namespace OpenRA.FileFormats
Console.WriteLine("Loaded Waypoints:"); Console.WriteLine("Loaded Waypoints:");
foreach (var wp in Waypoints) foreach (var wp in Waypoints)
Console.WriteLine("\t{0} => {1}",wp.Key,wp.Value); Console.WriteLine("\t{0} => {1}", wp.Key, wp.Value);
Console.WriteLine("Loaded Actors:"); Console.WriteLine("Loaded Actors:");
foreach (var wp in Actors) foreach (var wp in Actors)
Console.WriteLine("\t{0} => {1} {2} {3}",wp.Key,wp.Value.Name, wp.Value.Owner,wp.Value.Location); Console.WriteLine("\t{0} => {1} {2} {3}", wp.Key, wp.Value.Name, wp.Value.Owner, wp.Value.Location);
Console.WriteLine("Loaded Smudges:"); Console.WriteLine("Loaded Smudges:");
foreach (var s in Smudges) foreach (var s in Smudges)
Console.WriteLine("\t{0} {1} {2}",s.Type,s.Location,s.Depth); Console.WriteLine("\t{0} {1} {2}", s.Type, s.Location, s.Depth);
} }
} }
} }

View File

@@ -18,10 +18,9 @@
*/ */
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.IO;
using System;
namespace OpenRA.FileFormats namespace OpenRA.FileFormats
{ {
@@ -39,40 +38,38 @@ namespace OpenRA.FileFormats
public int2 TopLeft; public int2 TopLeft;
public int2 BottomRight; public int2 BottomRight;
public int Width {get {return BottomRight.X - TopLeft.X;}} public int Width { get { return BottomRight.X - TopLeft.X; } }
public int Height {get {return BottomRight.Y - TopLeft.Y;}} public int Height { get { return BottomRight.Y - TopLeft.Y; } }
public Lazy<Bitmap> Preview; public Lazy<Bitmap> Preview;
static List<string> Fields = new List<string>() { static List<string> Fields = new List<string>() {
"Title", "Description", "Author", "PlayerCount", "Tileset", "TopLeft", "BottomRight" "Title", "Description", "Author", "PlayerCount", "Tileset", "TopLeft", "BottomRight"
}; };
public MapStub() {} public MapStub() { }
public MapStub(IFolder package) public MapStub(IFolder package)
{ {
Package = package; Package = package;
var yaml = MiniYaml.FromStream(Package.GetContent("map.yaml")); var yaml = MiniYaml.FromStream(Package.GetContent("map.yaml"));
FieldLoader.LoadFields(this,yaml,Fields); FieldLoader.LoadFields(this, yaml, Fields);
Preview = Lazy.New( Preview = Lazy.New(
() => {return new Bitmap(Package.GetContent("preview.png"));} () => { return new Bitmap(Package.GetContent("preview.png")); }
); );
StreamReader uidStream = new StreamReader(Package.GetContent("map.uid")); Uid = Package.GetContent("map.uid").ReadAllText();
Uid = uidStream.ReadLine();
uidStream.Close();
} }
public Rectangle PreviewBounds(Rectangle container) public Rectangle PreviewBounds(Rectangle container)
{ {
float scale = Math.Min(container.Width*1.0f/Width,container.Height*1.0f/Height); float scale = Math.Min(container.Width * 1.0f / Width, container.Height * 1.0f / Height);
var size = Math.Max(Width, Height); var size = Math.Max(Width, Height);
var dw = (int)(scale*(size - Width)) / 2; var dw = (int)(scale * (size - Width)) / 2;
var dh = (int)(scale*(size - Height)) / 2; var dh = (int)(scale * (size - Height)) / 2;
return new Rectangle(container.X + dw, container.Y + dh, (int)(Width*scale), (int)(Height*scale)); return new Rectangle(container.X + dw, container.Y + dh, (int)(Width * scale), (int)(Height * scale));
} }
} }
} }