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.Reflection;
using System.Linq;
using System.IO;
namespace OpenRA
{
@@ -42,5 +43,22 @@ namespace OpenRA
{
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

@@ -147,8 +147,8 @@ namespace OpenRA.FileFormats
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);
@@ -175,11 +175,13 @@ namespace OpenRA.FileFormats
for (int j = 0; j < MapSize.Y; j++)
MapResources[i, j] = new TileReference<byte, byte>(ReadByte(dataStream), ReadByte(dataStream));
}
}
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"))
using (var writer = new BinaryWriter(dataStream))
{
writer.BaseStream.Seek(0, SeekOrigin.Begin);
// File header consists of a version byte, followed by 2 ushorts for width and height
@@ -202,39 +204,28 @@ namespace OpenRA.FileFormats
writer.Write(MapResources[i, j].type);
writer.Write(MapResources[i, j].index);
}
writer.Flush();
writer.Close();
}
File.Delete(filepath);
File.Move(filepath + ".tmp", filepath);
}
public void LoadUid()
{
StreamReader uidStream = new StreamReader(Package.GetContent("map.uid"));
Uid = uidStream.ReadLine();
uidStream.Close();
Uid = Package.GetContent("map.uid").ReadAllText();
}
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
var yamlStream = Package.GetContent("map.yaml");
var binaryStream = Package.GetContent("map.bin");
var data = new byte[yamlStream.Length+binaryStream.Length];
yamlStream.Read(data,0,(int)yamlStream.Length);
binaryStream.Read(data,(int)yamlStream.Length,(int)binaryStream.Length);
var data = Exts.ReadAllBytes(Package.GetContent("map.yaml"))
.Concat(Exts.ReadAllBytes(Package.GetContent("map.bin"))).ToArray();
// Take the SHA1
using (var csp = SHA1.Create())
Uid = new string(csp.ComputeHash(data).SelectMany(a => a.ToString("x2")).ToArray());
// Save to file
StreamWriter file = new System.IO.StreamWriter(filename);
file.WriteLine(Uid);
file.Close();
File.WriteAllText(filename, Uid);
}
public bool IsInMap(int2 xy)
@@ -263,7 +254,6 @@ namespace OpenRA.FileFormats
Console.WriteLine("Loaded Smudges:");
foreach (var s in Smudges)
Console.WriteLine("\t{0} {1} {2}", s.Type, s.Location, s.Depth);
}
}
}

View File

@@ -18,10 +18,9 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System;
namespace OpenRA.FileFormats
{
@@ -59,9 +58,7 @@ namespace OpenRA.FileFormats
() => { return new Bitmap(Package.GetContent("preview.png")); }
);
StreamReader uidStream = new StreamReader(Package.GetContent("map.uid"));
Uid = uidStream.ReadLine();
uidStream.Close();
Uid = Package.GetContent("map.uid").ReadAllText();
}
public Rectangle PreviewBounds(Rectangle container)