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

@@ -23,247 +23,237 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Security.Cryptography; using System.Security.Cryptography;
namespace OpenRA.FileFormats namespace OpenRA.FileFormats
{ {
public class Map public class Map
{ {
public IFolder Package; public IFolder Package;
public string Uid; public string Uid;
// Yaml map data // Yaml map data
public int MapFormat = 1; public int MapFormat = 1;
public string Title; public string Title;
public string Description; public string Description;
public string Author; public string Author;
public int PlayerCount; public int PlayerCount;
public string Tileset; public string Tileset;
public Dictionary<string, ActorReference> Actors = new Dictionary<string, ActorReference>(); public Dictionary<string, ActorReference> Actors = new Dictionary<string, ActorReference>();
public List<SmudgeReference> Smudges = new List<SmudgeReference>(); public List<SmudgeReference> Smudges = new List<SmudgeReference>();
public Dictionary<string, int2> Waypoints = new Dictionary<string, int2>(); public Dictionary<string, int2> Waypoints = new Dictionary<string, int2>();
public Dictionary<string, MiniYaml> Rules = new Dictionary<string, MiniYaml>(); public Dictionary<string, MiniYaml> Rules = new Dictionary<string, MiniYaml>();
// Binary map data // Binary map data
public byte TileFormat = 1; public byte TileFormat = 1;
public int2 MapSize; public int2 MapSize;
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)
{ {
Package = package; Package = package;
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
foreach (var kv in yaml["Actors"].Nodes) foreach (var kv in yaml["Actors"].Nodes)
{ {
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
foreach (var kv in yaml["Smudges"].Nodes) foreach (var kv in yaml["Smudges"].Nodes)
{ {
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
Rules = yaml["Rules"].Nodes; Rules = yaml["Rules"].Nodes;
LoadUid(); LoadUid();
LoadBinaryData(); LoadBinaryData();
} }
public void Save(string filepath) public void Save(string filepath)
{ {
Dictionary<string, MiniYaml> root = new Dictionary<string, MiniYaml>(); Dictionary<string, MiniYaml> root = new Dictionary<string, MiniYaml>();
var d = new Dictionary<string, MiniYaml>(); var d = new Dictionary<string, MiniYaml>();
foreach (var field in SimpleFields) foreach (var field in SimpleFields)
{ {
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;
} }
static ushort ReadWord(Stream s) static ushort ReadWord(Stream s)
{ {
ushort ret = ReadByte(s); ushort ret = ReadByte(s);
ret |= (ushort)(ReadByte(s) << 8); ret |= (ushort)(ReadByte(s) << 8);
return ret; return ret;
} }
public void LoadBinaryData() public void LoadBinaryData()
{ {
Stream dataStream = Package.GetContent("map.bin"); using (var dataStream = Package.GetContent("map.bin"))
{
// Load header info // Load header info
byte version = ReadByte(dataStream); byte version = ReadByte(dataStream);
var width = ReadWord(dataStream); var width = ReadWord(dataStream);
var height = ReadWord(dataStream); var height = ReadWord(dataStream);
if (width != MapSize.X || height != MapSize.Y) if (width != MapSize.X || height != MapSize.Y)
throw new InvalidDataException("Invalid tile data"); throw new InvalidDataException("Invalid tile data");
MapTiles = new TileReference<ushort, byte>[ MapSize.X, MapSize.Y ]; MapTiles = new TileReference<ushort, byte>[MapSize.X, MapSize.Y];
MapResources = new TileReference<byte, byte>[ MapSize.X, MapSize.Y ]; MapResources = new TileReference<byte, byte>[MapSize.X, MapSize.Y];
// Load tile data // Load 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++)
{ {
ushort tile = ReadWord(dataStream); ushort tile = ReadWord(dataStream);
byte index = ReadByte(dataStream); byte index = ReadByte(dataStream);
byte image = (index == byte.MaxValue) ? (byte)( i % 4 + ( j % 4 ) * 4 ) : index; byte image = (index == byte.MaxValue) ? (byte)(i % 4 + (j % 4) * 4) : index;
MapTiles[i,j] = new TileReference<ushort,byte>(tile,index, image); MapTiles[i, j] = new TileReference<ushort, byte>(tile, index, image);
} }
// Load resource 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)); 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); {
BinaryWriter writer = new BinaryWriter( dataStream ); using (var dataStream = File.Create(filepath + ".tmp"))
writer.BaseStream.Seek(0, SeekOrigin.Begin); using (var writer = new BinaryWriter(dataStream))
{
// File header consists of a version byte, followed by 2 ushorts for width and height writer.BaseStream.Seek(0, SeekOrigin.Begin);
writer.Write(TileFormat);
writer.Write((ushort)MapSize.X); // File header consists of a version byte, followed by 2 ushorts for width and height
writer.Write((ushort)MapSize.Y); writer.Write(TileFormat);
writer.Write((ushort)MapSize.X);
// Tile data writer.Write((ushort)MapSize.Y);
for( int i = 0 ; i < MapSize.X ; i++ )
for( int j = 0 ; j < MapSize.Y ; j++ ) // Tile data
{ for (int i = 0; i < MapSize.X; i++)
writer.Write( MapTiles[i,j].type ); for (int j = 0; j < MapSize.Y; j++)
writer.Write( MapTiles[i,j].index ); {
} writer.Write(MapTiles[i, j].type);
writer.Write(MapTiles[i, j].index);
// Resource data }
for( int i = 0 ; i < MapSize.X ; i++ )
for( int j = 0 ; j < MapSize.Y ; j++ ) // Resource data
{ for (int i = 0; i < MapSize.X; i++)
writer.Write( MapResources[i,j].type ); for (int j = 0; j < MapSize.Y; j++)
writer.Write( MapResources[i,j].index ); {
} writer.Write(MapResources[i, j].type);
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
{ // Read the relevant data into a buffer
// UID is calculated by taking an SHA1 of the yaml and binary data var data = Exts.ReadAllBytes(Package.GetContent("map.yaml"))
// Read the relevant data into a buffer .Concat(Exts.ReadAllBytes(Package.GetContent("map.bin"))).ToArray();
var yamlStream = Package.GetContent("map.yaml");
var binaryStream = Package.GetContent("map.bin"); // Take the SHA1
var data = new byte[yamlStream.Length+binaryStream.Length]; using (var csp = SHA1.Create())
Uid = new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray());
yamlStream.Read(data,0,(int)yamlStream.Length);
binaryStream.Read(data,(int)yamlStream.Length,(int)binaryStream.Length); File.WriteAllText(filename, Uid);
}
// Take the SHA1
using (var csp = SHA1.Create()) public bool IsInMap(int2 xy)
Uid = new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray()); {
return IsInMap(xy.X, xy.Y);
// Save to file }
StreamWriter file = new System.IO.StreamWriter(filename);
file.WriteLine(Uid); public bool IsInMap(int x, int y)
file.Close(); {
} return (x >= TopLeft.X && y >= TopLeft.Y && x < BottomRight.X && y < BottomRight.Y);
}
public bool IsInMap(int2 xy)
{ public void DebugContents()
return IsInMap(xy.X,xy.Y); {
} foreach (var field in SimpleFields)
Console.WriteLine("Loaded {0}: {1}", field, this.GetType().GetField(field).GetValue(this));
public bool IsInMap(int x, int y)
{ Console.WriteLine("Loaded Waypoints:");
return (x >= TopLeft.X && y >= TopLeft.Y && x < BottomRight.X && y < BottomRight.Y); foreach (var wp in Waypoints)
} Console.WriteLine("\t{0} => {1}", wp.Key, wp.Value);
public void DebugContents() Console.WriteLine("Loaded Actors:");
{ foreach (var wp in Actors)
foreach (var field in SimpleFields) Console.WriteLine("\t{0} => {1} {2} {3}", wp.Key, wp.Value.Name, wp.Value.Owner, wp.Value.Location);
Console.WriteLine("Loaded {0}: {1}", field, this.GetType().GetField(field).GetValue(this));
Console.WriteLine("Loaded Smudges:");
Console.WriteLine("Loaded Waypoints:"); foreach (var s in Smudges)
foreach (var wp in Waypoints) Console.WriteLine("\t{0} {1} {2}", s.Type, s.Location, s.Depth);
Console.WriteLine("\t{0} => {1}",wp.Key,wp.Value); }
}
Console.WriteLine("Loaded 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("Loaded Smudges:");
foreach (var s in Smudges)
Console.WriteLine("\t{0} {1} {2}",s.Type,s.Location,s.Depth);
}
}
} }

View File

@@ -16,63 +16,60 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with OpenRA. If not, see <http://www.gnu.org/licenses/>. * along with OpenRA. If not, see <http://www.gnu.org/licenses/>.
*/ */
#endregion #endregion
using System.Collections.Generic; using System;
using System.Drawing; using System.Collections.Generic;
using System.IO; using System.Drawing;
using System;
namespace OpenRA.FileFormats
namespace OpenRA.FileFormats {
{ public class MapStub
public class MapStub {
{ public IFolder Package;
public IFolder Package;
// Yaml map data
// Yaml map data public string Uid;
public string Uid; public string Title;
public string Title; public string Description;
public string Description; public string Author;
public string Author; public int PlayerCount;
public int PlayerCount; public string Tileset;
public string Tileset;
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 dw = (int)(scale * (size - Width)) / 2;
var size = Math.Max(Width, Height); var dh = (int)(scale * (size - Height)) / 2;
var dw = (int)(scale*(size - Width)) / 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)); }
}
}
} }