StyleCop clean Map
move non-map specific functions to FileFormats
This commit is contained in:
@@ -232,6 +232,16 @@ namespace OpenRA
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static T[,] ResizeArray<T>(T[,] ts, T t, int width, int height)
|
||||||
|
{
|
||||||
|
var result = new T[width, height];
|
||||||
|
for (var i = 0; i < width; i++)
|
||||||
|
for (var j = 0; j < height; j++)
|
||||||
|
result[i, j] = i <= ts.GetUpperBound(0) && j <= ts.GetUpperBound(1)
|
||||||
|
? ts[i, j] : t;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public static Rectangle Bounds(this Bitmap b) { return new Rectangle(0, 0, b.Width, b.Height); }
|
public static Rectangle Bounds(this Bitmap b) { return new Rectangle(0, 0, b.Width, b.Height); }
|
||||||
|
|
||||||
public static int ToBits(this IEnumerable<bool> bits)
|
public static int ToBits(this IEnumerable<bool> bits)
|
||||||
|
|||||||
@@ -99,6 +99,11 @@ namespace OpenRA.FileFormats
|
|||||||
return new MiniYaml( null, list.Select( x => new MiniYamlNode( x.ToString(), new MiniYaml( null ) ) ).ToList() );
|
return new MiniYaml( null, list.Select( x => new MiniYamlNode( x.ToString(), new MiniYaml( null ) ) ).ToList() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<MiniYamlNode> NodesOrEmpty(MiniYaml y, string s)
|
||||||
|
{
|
||||||
|
return y.NodesDict.ContainsKey(s) ? y.NodesDict[s].Nodes : new List<MiniYamlNode>();
|
||||||
|
}
|
||||||
|
|
||||||
static List<MiniYamlNode> FromLines(string[] lines, string filename)
|
static List<MiniYamlNode> FromLines(string[] lines, string filename)
|
||||||
{
|
{
|
||||||
var levels = new List<List<MiniYamlNode>>();
|
var levels = new List<List<MiniYamlNode>>();
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
public class Map
|
public class Map
|
||||||
{
|
{
|
||||||
[FieldLoader.Ignore] IFolder Container;
|
[FieldLoader.Ignore] IFolder container;
|
||||||
public string Path { get; private set; }
|
public string Path { get; private set; }
|
||||||
|
|
||||||
// Yaml map data
|
// Yaml map data
|
||||||
@@ -44,14 +44,6 @@ namespace OpenRA
|
|||||||
|
|
||||||
public int PlayerCount { get { return Players.Count(p => p.Value.Playable); } }
|
public int PlayerCount { get { return Players.Count(p => p.Value.Playable); } }
|
||||||
|
|
||||||
public int2[] GetSpawnPoints()
|
|
||||||
{
|
|
||||||
return Actors.Value.Values
|
|
||||||
.Where(a => a.Type == "mpspawn")
|
|
||||||
.Select(a => a.InitDict.Get<LocationInit>().value)
|
|
||||||
.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Rectangle Bounds;
|
public Rectangle Bounds;
|
||||||
|
|
||||||
// Yaml map data
|
// Yaml map data
|
||||||
@@ -71,14 +63,12 @@ namespace OpenRA
|
|||||||
|
|
||||||
[FieldLoader.Ignore] public Lazy<TileReference<ushort, byte>[,]> MapTiles;
|
[FieldLoader.Ignore] public Lazy<TileReference<ushort, byte>[,]> MapTiles;
|
||||||
[FieldLoader.Ignore] public Lazy<TileReference<byte, byte>[,]> MapResources;
|
[FieldLoader.Ignore] public Lazy<TileReference<byte, byte>[,]> MapResources;
|
||||||
[FieldLoader.Ignore] public string [,] CustomTerrain;
|
[FieldLoader.Ignore] public string[,] CustomTerrain;
|
||||||
|
|
||||||
public Map() {} /* doesn't really produce a valid map, but enough for loading a mod */
|
|
||||||
|
|
||||||
public static Map FromTileset(string tileset)
|
public static Map FromTileset(string tileset)
|
||||||
{
|
{
|
||||||
var tile = OpenRA.Rules.TileSets[tileset].Templates.First();
|
var tile = OpenRA.Rules.TileSets[tileset].Templates.First();
|
||||||
var tileRef = new TileReference<ushort,byte> { type = tile.Key, index = (byte)0 };
|
var tileRef = new TileReference<ushort, byte> { type = tile.Key, index = (byte)0 };
|
||||||
|
|
||||||
Map map = new Map()
|
Map map = new Map()
|
||||||
{
|
{
|
||||||
@@ -98,20 +88,22 @@ namespace OpenRA
|
|||||||
|
|
||||||
void AssertExists(string filename)
|
void AssertExists(string filename)
|
||||||
{
|
{
|
||||||
using(var s = Container.GetContent(filename))
|
using (var s = container.GetContent(filename))
|
||||||
if (s == null)
|
if (s == null)
|
||||||
throw new InvalidOperationException("Required file {0} not present in this map".F(filename));
|
throw new InvalidOperationException("Required file {0} not present in this map".F(filename));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Map() { } /* doesn't really produce a valid map, but enough for loading a mod */
|
||||||
|
|
||||||
public Map(string path)
|
public Map(string path)
|
||||||
{
|
{
|
||||||
Path = path;
|
Path = path;
|
||||||
Container = FileSystem.OpenPackage(path, null, int.MaxValue);
|
container = FileSystem.OpenPackage(path, null, int.MaxValue);
|
||||||
|
|
||||||
AssertExists("map.yaml");
|
AssertExists("map.yaml");
|
||||||
AssertExists("map.bin");
|
AssertExists("map.bin");
|
||||||
|
|
||||||
var yaml = new MiniYaml( null, MiniYaml.FromStream(Container.GetContent("map.yaml")) );
|
var yaml = new MiniYaml(null, MiniYaml.FromStream(container.GetContent("map.yaml")));
|
||||||
FieldLoader.Load(this, yaml);
|
FieldLoader.Load(this, yaml);
|
||||||
Uid = ComputeHash();
|
Uid = ComputeHash();
|
||||||
|
|
||||||
@@ -130,7 +122,7 @@ namespace OpenRA
|
|||||||
|
|
||||||
Actors = Lazy.New(() =>
|
Actors = Lazy.New(() =>
|
||||||
{
|
{
|
||||||
var ret = new Dictionary<string, ActorReference>();
|
var ret = new Dictionary<string, ActorReference>();
|
||||||
foreach (var kv in yaml.NodesDict["Actors"].NodesDict)
|
foreach (var kv in yaml.NodesDict["Actors"].NodesDict)
|
||||||
ret.Add(kv.Key, new ActorReference(kv.Value.Value, kv.Value.NodesDict));
|
ret.Add(kv.Key, new ActorReference(kv.Value.Value, kv.Value.NodesDict));
|
||||||
return ret;
|
return ret;
|
||||||
@@ -150,12 +142,12 @@ namespace OpenRA
|
|||||||
return ret;
|
return ret;
|
||||||
});
|
});
|
||||||
|
|
||||||
Rules = NodesOrEmpty(yaml, "Rules");
|
Rules = MiniYaml.NodesOrEmpty(yaml, "Rules");
|
||||||
Sequences = NodesOrEmpty(yaml, "Sequences");
|
Sequences = MiniYaml.NodesOrEmpty(yaml, "Sequences");
|
||||||
VoxelSequences = NodesOrEmpty(yaml, "VoxelSequences");
|
VoxelSequences = MiniYaml.NodesOrEmpty(yaml, "VoxelSequences");
|
||||||
Weapons = NodesOrEmpty(yaml, "Weapons");
|
Weapons = MiniYaml.NodesOrEmpty(yaml, "Weapons");
|
||||||
Voices = NodesOrEmpty(yaml, "Voices");
|
Voices = MiniYaml.NodesOrEmpty(yaml, "Voices");
|
||||||
Notifications = NodesOrEmpty(yaml, "Notifications");
|
Notifications = MiniYaml.NodesOrEmpty(yaml, "Notifications");
|
||||||
|
|
||||||
CustomTerrain = new string[MapSize.X, MapSize.Y];
|
CustomTerrain = new string[MapSize.X, MapSize.Y];
|
||||||
|
|
||||||
@@ -163,9 +155,12 @@ namespace OpenRA
|
|||||||
MapResources = Lazy.New(() => LoadResourceTiles());
|
MapResources = Lazy.New(() => LoadResourceTiles());
|
||||||
}
|
}
|
||||||
|
|
||||||
static List<MiniYamlNode> NodesOrEmpty(MiniYaml y, string s)
|
public int2[] GetSpawnPoints()
|
||||||
{
|
{
|
||||||
return y.NodesDict.ContainsKey(s) ? y.NodesDict[s].Nodes : new List<MiniYamlNode>();
|
return Actors.Value.Values
|
||||||
|
.Where(a => a.Type == "mpspawn")
|
||||||
|
.Select(a => a.InitDict.Get<LocationInit>().value)
|
||||||
|
.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Save(string toPath)
|
public void Save(string toPath)
|
||||||
@@ -173,7 +168,7 @@ namespace OpenRA
|
|||||||
MapFormat = 5;
|
MapFormat = 5;
|
||||||
|
|
||||||
var root = new List<MiniYamlNode>();
|
var root = new List<MiniYamlNode>();
|
||||||
var fields = new []
|
var fields = new[]
|
||||||
{
|
{
|
||||||
"Selectable",
|
"Selectable",
|
||||||
"MapFormat",
|
"MapFormat",
|
||||||
@@ -193,20 +188,16 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
var f = this.GetType().GetField(field);
|
var f = this.GetType().GetField(field);
|
||||||
if (f.GetValue(this) == null) continue;
|
if (f.GetValue(this) == null) continue;
|
||||||
root.Add( new MiniYamlNode( field, FieldSaver.FormatValue( this, f ) ) );
|
root.Add(new MiniYamlNode(field, FieldSaver.FormatValue(this, f)));
|
||||||
}
|
}
|
||||||
|
|
||||||
root.Add( new MiniYamlNode( "Players", null,
|
root.Add(new MiniYamlNode("Players", null,
|
||||||
Players.Select( p => new MiniYamlNode(
|
Players.Select(p => new MiniYamlNode("PlayerReference@{0}".F(p.Key), FieldSaver.SaveDifferences(p.Value, new PlayerReference()))).ToList()));
|
||||||
"PlayerReference@{0}".F( p.Key ),
|
|
||||||
FieldSaver.SaveDifferences( p.Value, new PlayerReference() ) ) ).ToList() ) );
|
|
||||||
|
|
||||||
root.Add( new MiniYamlNode( "Actors", null,
|
root.Add(new MiniYamlNode("Actors", null,
|
||||||
Actors.Value.Select( x => new MiniYamlNode(
|
Actors.Value.Select(x => new MiniYamlNode(x.Key, x.Value.Save())).ToList()));
|
||||||
x.Key,
|
|
||||||
x.Value.Save() ) ).ToList() ) );
|
|
||||||
|
|
||||||
root.Add(new MiniYamlNode("Smudges", MiniYaml.FromList<SmudgeReference>( Smudges.Value )));
|
root.Add(new MiniYamlNode("Smudges", MiniYaml.FromList<SmudgeReference>(Smudges.Value)));
|
||||||
root.Add(new MiniYamlNode("Rules", null, Rules));
|
root.Add(new MiniYamlNode("Rules", null, Rules));
|
||||||
root.Add(new MiniYamlNode("Sequences", null, Sequences));
|
root.Add(new MiniYamlNode("Sequences", null, Sequences));
|
||||||
root.Add(new MiniYamlNode("VoxelSequences", null, VoxelSequences));
|
root.Add(new MiniYamlNode("VoxelSequences", null, VoxelSequences));
|
||||||
@@ -226,17 +217,17 @@ namespace OpenRA
|
|||||||
|
|
||||||
// Create a new map package
|
// Create a new map package
|
||||||
// TODO: Add other files (custom assets) to the entries list
|
// TODO: Add other files (custom assets) to the entries list
|
||||||
Container = FileSystem.CreatePackage(Path, int.MaxValue, entries);
|
container = FileSystem.CreatePackage(Path, int.MaxValue, entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update existing package
|
// Update existing package
|
||||||
Container.Write(entries);
|
container.Write(entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 (dataStream.ReadUInt8() != 1)
|
if (dataStream.ReadUInt8() != 1)
|
||||||
throw new InvalidDataException("Unknown binary map format");
|
throw new InvalidDataException("Unknown binary map format");
|
||||||
@@ -260,6 +251,7 @@ namespace OpenRA
|
|||||||
tiles[i, j] = new TileReference<ushort, byte>(tile, index);
|
tiles[i, j] = new TileReference<ushort, byte>(tile, index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return tiles;
|
return tiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,7 +259,7 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
var resources = new TileReference<byte, byte>[MapSize.X, MapSize.Y];
|
var resources = new TileReference<byte, byte>[MapSize.X, MapSize.Y];
|
||||||
|
|
||||||
using (var dataStream = Container.GetContent("map.bin"))
|
using (var dataStream = container.GetContent("map.bin"))
|
||||||
{
|
{
|
||||||
if (dataStream.ReadUInt8() != 1)
|
if (dataStream.ReadUInt8() != 1)
|
||||||
throw new InvalidDataException("Unknown binary map format");
|
throw new InvalidDataException("Unknown binary map format");
|
||||||
@@ -280,7 +272,7 @@ namespace OpenRA
|
|||||||
throw new InvalidDataException("Invalid tile data");
|
throw new InvalidDataException("Invalid tile data");
|
||||||
|
|
||||||
// Skip past tile data
|
// Skip past tile data
|
||||||
dataStream.Seek(3*MapSize.X*MapSize.Y, SeekOrigin.Current);
|
dataStream.Seek(3 * MapSize.X * MapSize.Y, SeekOrigin.Current);
|
||||||
|
|
||||||
// Load resource data
|
// Load resource data
|
||||||
for (var i = 0; i < MapSize.X; i++)
|
for (var i = 0; i < MapSize.X; i++)
|
||||||
@@ -291,6 +283,7 @@ namespace OpenRA
|
|||||||
resources[i, j] = new TileReference<byte, byte>(type, index);
|
resources[i, j] = new TileReference<byte, byte>(type, index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return resources;
|
return resources;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,8 +307,8 @@ namespace OpenRA
|
|||||||
for (var j = 0; j < MapSize.Y; j++)
|
for (var j = 0; j < MapSize.Y; j++)
|
||||||
{
|
{
|
||||||
writer.Write(MapTiles.Value[i, j].type);
|
writer.Write(MapTiles.Value[i, j].type);
|
||||||
var PickAny = OpenRA.Rules.TileSets[Tileset].Templates[MapTiles.Value[i, j].type].PickAny;
|
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);
|
writer.Write(pickAny ? (byte)(i % 4 + (j % 4) * 4) : MapTiles.Value[i, j].index);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resource data
|
// Resource data
|
||||||
@@ -326,29 +319,20 @@ namespace OpenRA
|
|||||||
writer.Write(MapResources.Value[i, j].index);
|
writer.Write(MapResources.Value[i, j].index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return dataStream.ToArray();
|
return dataStream.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsInMap(CPos xy) { return IsInMap(xy.X, xy.Y); }
|
public bool IsInMap(CPos xy) { return IsInMap(xy.X, xy.Y); }
|
||||||
public bool IsInMap(int x, int y) { return Bounds.Contains(x,y); }
|
public bool IsInMap(int x, int y) { return Bounds.Contains(x, y); }
|
||||||
|
|
||||||
static T[,] ResizeArray<T>(T[,] ts, T t, int width, int height)
|
|
||||||
{
|
|
||||||
var result = new T[width, height];
|
|
||||||
for (var i = 0; i < width; i++)
|
|
||||||
for (var j = 0; j < height; j++)
|
|
||||||
result[i, j] = i <= ts.GetUpperBound(0) && j <= ts.GetUpperBound(1)
|
|
||||||
? ts[i, j] : t;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Resize(int width, int height) // editor magic.
|
public void Resize(int width, int height) // editor magic.
|
||||||
{
|
{
|
||||||
var oldMapTiles = MapTiles.Value;
|
var oldMapTiles = MapTiles.Value;
|
||||||
var oldMapResources = MapResources.Value;
|
var oldMapResources = MapResources.Value;
|
||||||
|
|
||||||
MapTiles = Lazy.New(() => ResizeArray(oldMapTiles, oldMapTiles[0, 0], width, height));
|
MapTiles = Lazy.New(() => Exts.ResizeArray(oldMapTiles, oldMapTiles[0, 0], width, height));
|
||||||
MapResources = Lazy.New(() => ResizeArray(oldMapResources, oldMapResources[0, 0], width, height));
|
MapResources = Lazy.New(() => Exts.ResizeArray(oldMapResources, oldMapResources[0, 0], width, height));
|
||||||
MapSize = new int2(width, height);
|
MapSize = new int2(width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -361,8 +345,8 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
// 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 data = Container.GetContent("map.yaml").ReadAllBytes()
|
var data = container.GetContent("map.yaml").ReadAllBytes()
|
||||||
.Concat(Container.GetContent("map.bin").ReadAllBytes()).ToArray();
|
.Concat(container.GetContent("map.bin").ReadAllBytes()).ToArray();
|
||||||
|
|
||||||
// Take the SHA1
|
// Take the SHA1
|
||||||
using (var csp = SHA1.Create())
|
using (var csp = SHA1.Create())
|
||||||
|
|||||||
Reference in New Issue
Block a user