Use stream extensions to read binary map data.

This commit is contained in:
Paul Chote
2013-06-02 17:27:32 +12:00
parent ee058e72a9
commit 5692b95ad2

View File

@@ -229,44 +229,27 @@ namespace OpenRA
Container.Write(entries);
}
static byte ReadByte(Stream s)
{
int ret = s.ReadByte();
if (ret == -1)
throw new NotImplementedException();
return (byte)ret;
}
static ushort ReadWord(Stream s)
{
ushort ret = ReadByte(s);
ret |= (ushort)(ReadByte(s) << 8);
return ret;
}
public TileReference<ushort, byte>[,] LoadMapTiles()
{
var tiles = new TileReference<ushort, byte>[MapSize.X, MapSize.Y];
using (var dataStream = Container.GetContent("map.bin"))
{
if (ReadByte(dataStream) != 1)
if (dataStream.ReadUInt8() != 1)
throw new InvalidDataException("Unknown binary map format");
// Load header info
var width = ReadWord(dataStream);
var height = ReadWord(dataStream);
var width = dataStream.ReadUInt16();
var height = dataStream.ReadUInt16();
if (width != MapSize.X || height != MapSize.Y)
throw new InvalidDataException("Invalid tile data");
// Load tile data
for (int i = 0; i < MapSize.X; i++)
for (int j = 0; j < MapSize.Y; j++)
{
ushort tile = ReadWord(dataStream);
byte index = ReadByte(dataStream);
var tile = dataStream.ReadUInt16();
var index = dataStream.ReadUInt8();
if (index == byte.MaxValue)
index = (byte)(i % 4 + (j % 4) * 4);
@@ -282,26 +265,25 @@ namespace OpenRA
using (var dataStream = Container.GetContent("map.bin"))
{
if (ReadByte(dataStream) != 1)
if (dataStream.ReadUInt8() != 1)
throw new InvalidDataException("Unknown binary map format");
// Load header info
var width = ReadWord(dataStream);
var height = ReadWord(dataStream);
var width = dataStream.ReadUInt16();
var height = dataStream.ReadUInt16();
if (width != MapSize.X || height != MapSize.Y)
throw new InvalidDataException("Invalid tile data");
// Skip past tile data
for (var i = 0; i < 3*MapSize.X*MapSize.Y; i++)
ReadByte(dataStream);
dataStream.Seek(3*MapSize.X*MapSize.Y, SeekOrigin.Current);
// Load resource data
for (var i = 0; i < MapSize.X; i++)
for (var j = 0; j < MapSize.Y; j++)
{
byte type = ReadByte(dataStream);
byte index = ReadByte(dataStream);
var type = dataStream.ReadUInt8();
var index = dataStream.ReadUInt8();
resources[i, j] = new TileReference<byte, byte>(type, index);
}
}