StyleCop clean Map
move non-map specific functions to FileFormats
This commit is contained in:
@@ -232,6 +232,16 @@ namespace OpenRA
|
||||
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 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() );
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
var levels = new List<List<MiniYamlNode>>();
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace OpenRA
|
||||
{
|
||||
public class Map
|
||||
{
|
||||
[FieldLoader.Ignore] IFolder Container;
|
||||
[FieldLoader.Ignore] IFolder container;
|
||||
public string Path { get; private set; }
|
||||
|
||||
// Yaml map data
|
||||
@@ -44,14 +44,6 @@ namespace OpenRA
|
||||
|
||||
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;
|
||||
|
||||
// Yaml map data
|
||||
@@ -71,14 +63,12 @@ namespace OpenRA
|
||||
|
||||
[FieldLoader.Ignore] public Lazy<TileReference<ushort, byte>[,]> MapTiles;
|
||||
[FieldLoader.Ignore] public Lazy<TileReference<byte, byte>[,]> MapResources;
|
||||
[FieldLoader.Ignore] public string [,] CustomTerrain;
|
||||
|
||||
public Map() {} /* doesn't really produce a valid map, but enough for loading a mod */
|
||||
[FieldLoader.Ignore] public string[,] CustomTerrain;
|
||||
|
||||
public static Map FromTileset(string tileset)
|
||||
{
|
||||
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()
|
||||
{
|
||||
@@ -98,20 +88,22 @@ namespace OpenRA
|
||||
|
||||
void AssertExists(string filename)
|
||||
{
|
||||
using(var s = Container.GetContent(filename))
|
||||
using (var s = container.GetContent(filename))
|
||||
if (s == null)
|
||||
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)
|
||||
{
|
||||
Path = path;
|
||||
Container = FileSystem.OpenPackage(path, null, int.MaxValue);
|
||||
container = FileSystem.OpenPackage(path, null, int.MaxValue);
|
||||
|
||||
AssertExists("map.yaml");
|
||||
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);
|
||||
Uid = ComputeHash();
|
||||
|
||||
@@ -130,7 +122,7 @@ namespace OpenRA
|
||||
|
||||
Actors = Lazy.New(() =>
|
||||
{
|
||||
var ret = new Dictionary<string, ActorReference>();
|
||||
var ret = new Dictionary<string, ActorReference>();
|
||||
foreach (var kv in yaml.NodesDict["Actors"].NodesDict)
|
||||
ret.Add(kv.Key, new ActorReference(kv.Value.Value, kv.Value.NodesDict));
|
||||
return ret;
|
||||
@@ -150,12 +142,12 @@ namespace OpenRA
|
||||
return ret;
|
||||
});
|
||||
|
||||
Rules = NodesOrEmpty(yaml, "Rules");
|
||||
Sequences = NodesOrEmpty(yaml, "Sequences");
|
||||
VoxelSequences = NodesOrEmpty(yaml, "VoxelSequences");
|
||||
Weapons = NodesOrEmpty(yaml, "Weapons");
|
||||
Voices = NodesOrEmpty(yaml, "Voices");
|
||||
Notifications = NodesOrEmpty(yaml, "Notifications");
|
||||
Rules = MiniYaml.NodesOrEmpty(yaml, "Rules");
|
||||
Sequences = MiniYaml.NodesOrEmpty(yaml, "Sequences");
|
||||
VoxelSequences = MiniYaml.NodesOrEmpty(yaml, "VoxelSequences");
|
||||
Weapons = MiniYaml.NodesOrEmpty(yaml, "Weapons");
|
||||
Voices = MiniYaml.NodesOrEmpty(yaml, "Voices");
|
||||
Notifications = MiniYaml.NodesOrEmpty(yaml, "Notifications");
|
||||
|
||||
CustomTerrain = new string[MapSize.X, MapSize.Y];
|
||||
|
||||
@@ -163,9 +155,12 @@ namespace OpenRA
|
||||
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)
|
||||
@@ -173,7 +168,7 @@ namespace OpenRA
|
||||
MapFormat = 5;
|
||||
|
||||
var root = new List<MiniYamlNode>();
|
||||
var fields = new []
|
||||
var fields = new[]
|
||||
{
|
||||
"Selectable",
|
||||
"MapFormat",
|
||||
@@ -193,20 +188,16 @@ namespace OpenRA
|
||||
{
|
||||
var f = this.GetType().GetField(field);
|
||||
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,
|
||||
Players.Select( p => new MiniYamlNode(
|
||||
"PlayerReference@{0}".F( p.Key ),
|
||||
FieldSaver.SaveDifferences( p.Value, new PlayerReference() ) ) ).ToList() ) );
|
||||
root.Add(new MiniYamlNode("Players", null,
|
||||
Players.Select(p => new MiniYamlNode("PlayerReference@{0}".F(p.Key), FieldSaver.SaveDifferences(p.Value, new PlayerReference()))).ToList()));
|
||||
|
||||
root.Add( new MiniYamlNode( "Actors", null,
|
||||
Actors.Value.Select( x => new MiniYamlNode(
|
||||
x.Key,
|
||||
x.Value.Save() ) ).ToList() ) );
|
||||
root.Add(new MiniYamlNode("Actors", null,
|
||||
Actors.Value.Select(x => new MiniYamlNode(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("Sequences", null, Sequences));
|
||||
root.Add(new MiniYamlNode("VoxelSequences", null, VoxelSequences));
|
||||
@@ -226,17 +217,17 @@ namespace OpenRA
|
||||
|
||||
// Create a new map package
|
||||
// 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
|
||||
Container.Write(entries);
|
||||
container.Write(entries);
|
||||
}
|
||||
|
||||
public TileReference<ushort, byte>[,] LoadMapTiles()
|
||||
{
|
||||
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)
|
||||
throw new InvalidDataException("Unknown binary map format");
|
||||
@@ -260,6 +251,7 @@ namespace OpenRA
|
||||
tiles[i, j] = new TileReference<ushort, byte>(tile, index);
|
||||
}
|
||||
}
|
||||
|
||||
return tiles;
|
||||
}
|
||||
|
||||
@@ -267,7 +259,7 @@ namespace OpenRA
|
||||
{
|
||||
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)
|
||||
throw new InvalidDataException("Unknown binary map format");
|
||||
@@ -280,7 +272,7 @@ namespace OpenRA
|
||||
throw new InvalidDataException("Invalid 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
|
||||
for (var i = 0; i < MapSize.X; i++)
|
||||
@@ -291,6 +283,7 @@ namespace OpenRA
|
||||
resources[i, j] = new TileReference<byte, byte>(type, index);
|
||||
}
|
||||
}
|
||||
|
||||
return resources;
|
||||
}
|
||||
|
||||
@@ -314,8 +307,8 @@ namespace OpenRA
|
||||
for (var j = 0; j < MapSize.Y; j++)
|
||||
{
|
||||
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);
|
||||
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
|
||||
@@ -326,29 +319,20 @@ namespace OpenRA
|
||||
writer.Write(MapResources.Value[i, j].index);
|
||||
}
|
||||
}
|
||||
|
||||
return dataStream.ToArray();
|
||||
}
|
||||
|
||||
public bool IsInMap(CPos xy) { return IsInMap(xy.X, xy.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 bool IsInMap(int x, int y) { return Bounds.Contains(x, y); }
|
||||
|
||||
public void Resize(int width, int height) // editor magic.
|
||||
{
|
||||
var oldMapTiles = MapTiles.Value;
|
||||
var oldMapResources = MapResources.Value;
|
||||
|
||||
MapTiles = Lazy.New(() => ResizeArray(oldMapTiles, oldMapTiles[0, 0], width, height));
|
||||
MapResources = Lazy.New(() => ResizeArray(oldMapResources, oldMapResources[0, 0], width, height));
|
||||
MapTiles = Lazy.New(() => Exts.ResizeArray(oldMapTiles, oldMapTiles[0, 0], width, height));
|
||||
MapResources = Lazy.New(() => Exts.ResizeArray(oldMapResources, oldMapResources[0, 0], 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
|
||||
// Read the relevant data into a buffer
|
||||
var data = Container.GetContent("map.yaml").ReadAllBytes()
|
||||
.Concat(Container.GetContent("map.bin").ReadAllBytes()).ToArray();
|
||||
var data = container.GetContent("map.yaml").ReadAllBytes()
|
||||
.Concat(container.GetContent("map.bin").ReadAllBytes()).ToArray();
|
||||
|
||||
// Take the SHA1
|
||||
using (var csp = SHA1.Create())
|
||||
|
||||
Reference in New Issue
Block a user