diff --git a/OpenRA.FileFormats/Exts.cs b/OpenRA.FileFormats/Exts.cs index da12c917c3..b317326bf3 100644 --- a/OpenRA.FileFormats/Exts.cs +++ b/OpenRA.FileFormats/Exts.cs @@ -73,5 +73,10 @@ namespace OpenRA yield return line; } } + + public static bool HasAttribute(this MemberInfo mi) + { + return mi.GetCustomAttributes(typeof(T), true).Length != 0; + } } } diff --git a/OpenRA.FileFormats/FieldLoader.cs b/OpenRA.FileFormats/FieldLoader.cs index a727ebb3c6..6663c99047 100644 --- a/OpenRA.FileFormats/FieldLoader.cs +++ b/OpenRA.FileFormats/FieldLoader.cs @@ -36,18 +36,14 @@ namespace OpenRA.FileFormats { throw new NotImplementedException( "FieldLoader: Missing field `{0}` on `{1}`".F( s, f.Name ) ); }; - - public static void Load(object self, IniSection ini) - { - foreach (var x in ini) - LoadField(self, x.Key, x.Value); - } - public static void Load(object self, MiniYaml my) + public static void Load(object self, MiniYaml my) { Load(self, my.Nodes); } + + public static void Load(object self, Dictionary my) { - foreach (var x in my.Nodes) + foreach (var x in my) if (!x.Key.StartsWith("-")) - LoadField( self, x.Key, x.Value.Value ); + LoadField(self, x.Key, x.Value.Value); } public static T Load(MiniYaml y) where T : new() @@ -62,7 +58,7 @@ namespace OpenRA.FileFormats foreach (var field in fields) { if (!my.ContainsKey(field)) continue; - FieldLoader.LoadField(self,field,my[field].Value); + LoadField(self,field,my[field].Value); } } diff --git a/OpenRA.FileFormats/Map/Map.cs b/OpenRA.FileFormats/Map/Map.cs index 3808d3d4c7..ceae8f7be0 100644 --- a/OpenRA.FileFormats/Map/Map.cs +++ b/OpenRA.FileFormats/Map/Map.cs @@ -61,8 +61,6 @@ namespace OpenRA.FileFormats public int2 TopLeft; public int2 BottomRight; - public Rectangle Bounds { get { return Rectangle.FromLTRB(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y); } } - public TileReference[,] MapTiles; public TileReference[,] MapResources; @@ -74,31 +72,32 @@ namespace OpenRA.FileFormats public int Height { get { return BottomRight.Y - TopLeft.Y; } } public string Theater { get { return Tileset; } } public IEnumerable SpawnPoints { get { return Waypoints.Select(kv => kv.Value); } } + public Rectangle Bounds { get { return Rectangle.FromLTRB(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y); } } static List SimpleFields = new List() { "Selectable", "MapFormat", "Title", "Description", "Author", "PlayerCount", "Tileset", "MapSize", "TopLeft", "BottomRight" }; - public Map() - { - MapSize = new int2(1, 1); - MapResources = new TileReference[1, 1]; + public Map() + { + MapSize = new int2(1, 1); + MapResources = new TileReference[1, 1]; MapTiles = new TileReference[1, 1] { { new TileReference { type = (ushort)0xffffu, image = (byte)0xffu, - index = (byte)0xffu } } }; - - PlayerCount = 0; - TopLeft = new int2(0,0); - BottomRight = new int2(0,0); - - Tileset = "TEMPERAT"; - Players.Add("Neutral", new PlayerReference("Neutral", "neutral", "allies", true, true)); - - Title = "Name your map here"; - Description = "Describe your map here"; - Author = "Your name here"; + index = (byte)0xffu } } }; + + PlayerCount = 0; + TopLeft = new int2(0, 0); + BottomRight = new int2(0, 0); + + Tileset = "TEMPERAT"; + Players.Add("Neutral", new PlayerReference("Neutral", "neutral", "allies", true, true)); + + Title = "Name your map here"; + Description = "Describe your map here"; + Author = "Your name here"; } public Map(IFolder package) @@ -170,27 +169,28 @@ namespace OpenRA.FileFormats { MapFormat = 2; - Dictionary root = new Dictionary(); + var root = new Dictionary(); foreach (var field in SimpleFields) { FieldInfo f = this.GetType().GetField(field); if (f.GetValue(this) == null) continue; root.Add(field, new MiniYaml(FieldSaver.FormatValue(this, f), null)); - } - - Dictionary playerYaml = new Dictionary(); - foreach(var p in Players) - playerYaml.Add("PlayerReference@{0}".F(p.Key), FieldSaver.Save(p.Value)); - root.Add("Players",new MiniYaml(null, playerYaml)); - - Dictionary actorYaml = new Dictionary(); - foreach(var p in Actors) - actorYaml.Add("ActorReference@{0}".F(p.Key), FieldSaver.Save(p.Value)); - root.Add("Actors",new MiniYaml(null, actorYaml)); + } + + root.Add("Players", + new MiniYaml(null, Players.ToDictionary( + p => "PlayerReference@{0}".F(p.Key), + p => FieldSaver.Save(p.Value)))); + + root.Add("Actors", + new MiniYaml(null, Actors.ToDictionary( + a => "ActorReference@{0}".F(a.Key), + a => FieldSaver.Save(a.Value)))); root.Add("Waypoints", MiniYaml.FromDictionary(Waypoints)); root.Add("Smudges", MiniYaml.FromList(Smudges)); root.Add("Rules", new MiniYaml(null, Rules)); + SaveBinaryData(Path.Combine(filepath, "map.bin")); root.WriteToFile(Path.Combine(filepath, "map.yaml")); SaveUid(Path.Combine(filepath, "map.uid")); diff --git a/OpenRA.FileFormats/Map/TileSet.cs b/OpenRA.FileFormats/Map/TileSet.cs index 7dd6c6d158..5631a9dfc6 100644 --- a/OpenRA.FileFormats/Map/TileSet.cs +++ b/OpenRA.FileFormats/Map/TileSet.cs @@ -22,6 +22,7 @@ using System.Collections.Generic; using System.Globalization; using System.IO; using System.Drawing; +using System.Linq; namespace OpenRA.FileFormats { @@ -32,11 +33,8 @@ namespace OpenRA.FileFormats public bool AcceptSmudge = true; public bool IsWater = false; public Color Color; - - public TerrainTypeInfo(MiniYaml my) - { - FieldLoader.Load(this, my); - } + + public TerrainTypeInfo(MiniYaml my) { FieldLoader.Load(this, my); } } public class TileTemplate @@ -49,10 +47,10 @@ namespace OpenRA.FileFormats static List fields = new List() {"Id", "Image", "Size", "PickAny"}; - public TileTemplate(Dictionary my) + public TileTemplate(MiniYaml my) { - FieldLoader.LoadFields(this, my, fields); - foreach (var tt in my["Tiles"].Nodes) + FieldLoader.LoadFields(this, my.Nodes, fields); + foreach (var tt in my.Nodes["Tiles"].Nodes) Tiles.Add(byte.Parse(tt.Key), tt.Value.Value); } } @@ -75,18 +73,12 @@ namespace OpenRA.FileFormats FieldLoader.Load(this, yaml["General"]); // TerrainTypes - foreach (var tt in yaml["Terrain"].Nodes) - { - var t = new TerrainTypeInfo(tt.Value); - Terrain.Add(t.Type, t); - } - + Terrain = yaml["Terrain"].Nodes.Values + .Select(y => new TerrainTypeInfo(y)).ToDictionary(t => t.Type); + // Templates - foreach (var tt in yaml["Templates"].Nodes) - { - var t = new TileTemplate(tt.Value.Nodes); - Templates.Add(t.Id, t); - } + Templates = yaml["Templates"].Nodes.Values + .Select(y => new TileTemplate(y)).ToDictionary(t => t.Id); } public void LoadTiles() diff --git a/OpenRA.Game/Sync.cs b/OpenRA.Game/Sync.cs index 2ca21d2c13..f30894454a 100755 --- a/OpenRA.Game/Sync.cs +++ b/OpenRA.Game/Sync.cs @@ -139,10 +139,5 @@ namespace OpenRA return p.Index * 0x567; return 0; } - - static bool HasAttribute( this MemberInfo mi ) - { - return mi.GetCustomAttributes(typeof(T), true).Length != 0; - } } } diff --git a/RALint/RALint.cs b/RALint/RALint.cs index 5b4edc0518..ae2e8edcca 100644 --- a/RALint/RALint.cs +++ b/RALint/RALint.cs @@ -95,10 +95,5 @@ namespace RALint EmitError("{0}.{1}.{2}: Missing {3} `{4}`." .F(actorInfo.Name, traitInfo.GetType().Name, fieldInfo.Name, type, v)); } - - static bool HasAttribute(this MemberInfo mi) - { - return mi.GetCustomAttributes(typeof(T), true).Length != 0; - } } }