clean up some messy GC behavior & needlessly longwinded code. slight perf cost on map save.
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -147,8 +147,8 @@ 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
|
// Load header info
|
||||||
byte version = ReadByte(dataStream);
|
byte version = ReadByte(dataStream);
|
||||||
var width = ReadWord(dataStream);
|
var width = ReadWord(dataStream);
|
||||||
@@ -175,11 +175,13 @@ namespace OpenRA.FileFormats
|
|||||||
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);
|
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
|
||||||
@@ -202,39 +204,28 @@ namespace OpenRA.FileFormats
|
|||||||
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)
|
||||||
@@ -263,7 +254,6 @@ namespace OpenRA.FileFormats
|
|||||||
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
{
|
{
|
||||||
@@ -59,9 +58,7 @@ namespace OpenRA.FileFormats
|
|||||||
() => { 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user