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