Make binary info loading lazy
This commit is contained in:
@@ -50,7 +50,7 @@ namespace OpenRA.Graphics
|
||||
{
|
||||
var mapX = x + map.Bounds.Left;
|
||||
var mapY = y + map.Bounds.Top;
|
||||
var type = tileset.GetTerrainType(map.MapTiles[mapX, mapY]);
|
||||
var type = tileset.GetTerrainType(map.MapTiles.Value[mapX, mapY]);
|
||||
if (!tileset.Terrain.ContainsKey(type))
|
||||
throw new InvalidDataException("Tileset {0} lacks terraintype {1}".F(tileset.Id, type));
|
||||
|
||||
@@ -80,11 +80,11 @@ namespace OpenRA.Graphics
|
||||
{
|
||||
var mapX = x + map.Bounds.Left;
|
||||
var mapY = y + map.Bounds.Top;
|
||||
if (map.MapResources[mapX, mapY].type == 0)
|
||||
if (map.MapResources.Value[mapX, mapY].type == 0)
|
||||
continue;
|
||||
|
||||
var res = Rules.Info["world"].Traits.WithInterface<ResourceTypeInfo>()
|
||||
.Where(t => t.ResourceType == map.MapResources[mapX, mapY].type)
|
||||
.Where(t => t.ResourceType == map.MapResources.Value[mapX, mapY].type)
|
||||
.Select(t => t.TerrainType).FirstOrDefault();
|
||||
if (res == null)
|
||||
continue;
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace OpenRA.Graphics
|
||||
Vertex[] vertices = new Vertex[4 * map.Bounds.Height * map.Bounds.Width];
|
||||
ushort[] indices = new ushort[6 * map.Bounds.Height * map.Bounds.Width];
|
||||
|
||||
terrainSheet = tileMapping[map.MapTiles[map.Bounds.Left, map.Bounds.Top]].sheet;
|
||||
terrainSheet = tileMapping[map.MapTiles.Value[map.Bounds.Left, map.Bounds.Top]].sheet;
|
||||
|
||||
int nv = 0;
|
||||
int ni = 0;
|
||||
@@ -45,14 +45,14 @@ namespace OpenRA.Graphics
|
||||
for( int j = map.Bounds.Top; j < map.Bounds.Bottom; j++ )
|
||||
for( int i = map.Bounds.Left; i < map.Bounds.Right; i++ )
|
||||
{
|
||||
Sprite tile = tileMapping[map.MapTiles[i, j]];
|
||||
Sprite tile = tileMapping[map.MapTiles.Value[i, j]];
|
||||
// TODO: The zero below should explicitly refer to the terrain palette, but this code is called
|
||||
// before the palettes are created. Therefore assumes that "terrain" is the first palette to be defined
|
||||
Util.FastCreateQuad(vertices, indices, Game.CellSize * new float2(i, j), tile, Game.modData.Palette.GetPaletteIndex("terrain"), nv, ni, tile.size);
|
||||
nv += 4;
|
||||
ni += 6;
|
||||
|
||||
if (tileMapping[map.MapTiles[i, j]].sheet != terrainSheet)
|
||||
if (tileMapping[map.MapTiles.Value[i, j]].sheet != terrainSheet)
|
||||
throw new InvalidOperationException("Terrain sprites span multiple sheets");
|
||||
}
|
||||
|
||||
|
||||
@@ -42,8 +42,8 @@ namespace OpenRA
|
||||
public byte TileFormat = 1;
|
||||
[FieldLoader.Load] public int2 MapSize;
|
||||
|
||||
public TileReference<ushort, byte>[,] MapTiles;
|
||||
public TileReference<byte, byte>[,] MapResources;
|
||||
public Lazy<TileReference<ushort, byte>[,]> MapTiles;
|
||||
public Lazy<TileReference<byte, byte>[,]> MapResources;
|
||||
public string [,] CustomTerrain;
|
||||
|
||||
public Map()
|
||||
@@ -61,12 +61,12 @@ namespace OpenRA
|
||||
Author = "Your name here",
|
||||
MapSize = new int2(1, 1),
|
||||
Tileset = tileset,
|
||||
MapResources = new TileReference<byte, byte>[1, 1],
|
||||
MapTiles = new TileReference<ushort, byte>[1, 1]
|
||||
MapResources = Lazy.New(() => new TileReference<byte, byte>[1, 1]),
|
||||
MapTiles = Lazy.New(() => new TileReference<ushort, byte>[1, 1]
|
||||
{ { new TileReference<ushort, byte> {
|
||||
type = tile.Key,
|
||||
index = (byte)0 }
|
||||
} },
|
||||
} })
|
||||
};
|
||||
|
||||
return map;
|
||||
@@ -152,8 +152,10 @@ namespace OpenRA
|
||||
// Voices
|
||||
Voices = (yaml.NodesDict.ContainsKey("Voices")) ? yaml.NodesDict["Voices"].Nodes : new List<MiniYamlNode>();
|
||||
|
||||
CustomTerrain = new string[MapSize.X, MapSize.Y];
|
||||
LoadBinaryData();
|
||||
CustomTerrain = new string[MapSize.X, MapSize.Y];
|
||||
|
||||
MapTiles = Lazy.New(() => LoadMapTiles());
|
||||
MapResources = Lazy.New(() => LoadResourceTiles());
|
||||
}
|
||||
|
||||
public void Save(string toPath)
|
||||
@@ -234,9 +236,42 @@ namespace OpenRA
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public void LoadBinaryData()
|
||||
|
||||
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)
|
||||
throw new InvalidDataException("Unknown binary map format");
|
||||
|
||||
// Load header info
|
||||
var width = ReadWord(dataStream);
|
||||
var height = ReadWord(dataStream);
|
||||
|
||||
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);
|
||||
if (index == byte.MaxValue)
|
||||
index = (byte)(i % 4 + (j % 4) * 4);
|
||||
|
||||
tiles[i, j] = new TileReference<ushort, byte>(tile, index);
|
||||
}
|
||||
}
|
||||
return tiles;
|
||||
}
|
||||
|
||||
public TileReference<byte, byte>[,] LoadResourceTiles()
|
||||
{
|
||||
var resources = new TileReference<byte, byte>[MapSize.X, MapSize.Y];
|
||||
|
||||
using (var dataStream = Container.GetContent("map.bin"))
|
||||
{
|
||||
if (ReadByte(dataStream) != 1)
|
||||
@@ -248,21 +283,10 @@ namespace OpenRA
|
||||
|
||||
if (width != MapSize.X || height != MapSize.Y)
|
||||
throw new InvalidDataException("Invalid tile data");
|
||||
|
||||
MapTiles = new TileReference<ushort, byte>[MapSize.X, MapSize.Y];
|
||||
MapResources = new TileReference<byte, byte>[MapSize.X, MapSize.Y];
|
||||
|
||||
// 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);
|
||||
if (index == byte.MaxValue)
|
||||
index = (byte)(i % 4 + (j % 4) * 4);
|
||||
|
||||
MapTiles[i, j] = new TileReference<ushort, byte>(tile, index);
|
||||
}
|
||||
|
||||
// Skip past tile data
|
||||
for (var i = 0; i < 3*MapSize.X*MapSize.Y; i++)
|
||||
ReadByte(dataStream);
|
||||
|
||||
// Load resource data
|
||||
for (int i = 0; i < MapSize.X; i++)
|
||||
@@ -270,9 +294,10 @@ namespace OpenRA
|
||||
{
|
||||
byte type = ReadByte(dataStream);
|
||||
byte index = ReadByte(dataStream);
|
||||
MapResources[i, j] = new TileReference<byte, byte>(type, index);
|
||||
resources[i, j] = new TileReference<byte, byte>(type, index);
|
||||
}
|
||||
}
|
||||
return resources;
|
||||
}
|
||||
|
||||
public byte[] SaveBinaryData()
|
||||
@@ -289,17 +314,17 @@ namespace OpenRA
|
||||
for (int i = 0; i < MapSize.X; i++)
|
||||
for (int j = 0; j < MapSize.Y; j++)
|
||||
{
|
||||
writer.Write(MapTiles[i, j].type);
|
||||
var PickAny = OpenRA.Rules.TileSets[Tileset].Templates[MapTiles[i, j].type].PickAny;
|
||||
writer.Write(PickAny ? (byte)(i % 4 + (j % 4) * 4) : MapTiles[i, j].index);
|
||||
writer.Write(MapTiles.Value[i, j].type);
|
||||
var PickAny = OpenRA.Rules.TileSets[Tileset].Templates[MapTiles.Value[i, j].type].PickAny;
|
||||
writer.Write(PickAny ? (byte)(i % 4 + (j % 4) * 4) : MapTiles.Value[i, j].index);
|
||||
}
|
||||
|
||||
// Resource data
|
||||
for (int i = 0; i < MapSize.X; i++)
|
||||
for (int j = 0; j < MapSize.Y; j++)
|
||||
{
|
||||
writer.Write(MapResources[i, j].type);
|
||||
writer.Write(MapResources[i, j].index);
|
||||
writer.Write(MapResources.Value[i, j].type);
|
||||
writer.Write(MapResources.Value[i, j].index);
|
||||
}
|
||||
}
|
||||
return dataStream.ToArray();
|
||||
@@ -327,8 +352,8 @@ namespace OpenRA
|
||||
|
||||
public void Resize(int width, int height) // editor magic.
|
||||
{
|
||||
MapTiles = ResizeArray(MapTiles, MapTiles[0, 0], width, height);
|
||||
MapResources = ResizeArray(MapResources, MapResources[0, 0], width, height);
|
||||
MapTiles = Lazy.New(() => ResizeArray(MapTiles.Value, MapTiles.Value[0, 0], width, height));
|
||||
MapResources = Lazy.New(() => ResizeArray(MapResources.Value, MapResources.Value[0, 0], width, height));
|
||||
MapSize = new int2(width, height);
|
||||
}
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace OpenRA.Traits
|
||||
for (int y = map.Bounds.Top; y < map.Bounds.Bottom; y++)
|
||||
{
|
||||
var type = resourceTypes.FirstOrDefault(
|
||||
r => r.info.ResourceType == w.Map.MapResources[x, y].type);
|
||||
r => r.info.ResourceType == w.Map.MapResources.Value[x, y].type);
|
||||
|
||||
if (type == null)
|
||||
continue;
|
||||
|
||||
@@ -64,7 +64,7 @@ namespace OpenRA
|
||||
public static string GetTerrainType(this World world, int2 cell)
|
||||
{
|
||||
var custom = world.Map.CustomTerrain[cell.X, cell.Y];
|
||||
return custom != null ? custom : world.TileSet.GetTerrainType(world.Map.MapTiles[cell.X, cell.Y]);
|
||||
return custom != null ? custom : world.TileSet.GetTerrainType(world.Map.MapTiles.Value[cell.X, cell.Y]);
|
||||
}
|
||||
|
||||
public static TerrainTypeInfo GetTerrainInfo(this World world, int2 cell)
|
||||
|
||||
Reference in New Issue
Block a user