some fileformats cleanup

This commit is contained in:
Chris Forbes
2010-07-16 17:48:29 +12:00
parent ebec75be0e
commit 9bbb2f78de
6 changed files with 52 additions and 69 deletions

View File

@@ -73,5 +73,10 @@ namespace OpenRA
yield return line;
}
}
public static bool HasAttribute<T>(this MemberInfo mi)
{
return mi.GetCustomAttributes(typeof(T), true).Length != 0;
}
}
}

View File

@@ -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<string, MiniYaml> 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<T>(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);
}
}

View File

@@ -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<ushort, byte>[,] MapTiles;
public TileReference<byte, byte>[,] 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<int2> 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<string> SimpleFields = new List<string>() {
"Selectable", "MapFormat", "Title", "Description", "Author", "PlayerCount", "Tileset", "MapSize", "TopLeft", "BottomRight"
};
public Map()
{
MapSize = new int2(1, 1);
MapResources = new TileReference<byte, byte>[1, 1];
public Map()
{
MapSize = new int2(1, 1);
MapResources = new TileReference<byte, byte>[1, 1];
MapTiles = new TileReference<ushort, byte>[1, 1]
{ { new TileReference<ushort, byte> {
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<string, MiniYaml> root = new Dictionary<string, MiniYaml>();
var root = new Dictionary<string, MiniYaml>();
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<string, MiniYaml> playerYaml = new Dictionary<string, MiniYaml>();
foreach(var p in Players)
playerYaml.Add("PlayerReference@{0}".F(p.Key), FieldSaver.Save(p.Value));
root.Add("Players",new MiniYaml(null, playerYaml));
Dictionary<string, MiniYaml> actorYaml = new Dictionary<string, MiniYaml>();
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<string, int2>(Waypoints));
root.Add("Smudges", MiniYaml.FromList<SmudgeReference>(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"));

View File

@@ -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<string> fields = new List<string>() {"Id", "Image", "Size", "PickAny"};
public TileTemplate(Dictionary<string,MiniYaml> 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()

View File

@@ -139,10 +139,5 @@ namespace OpenRA
return p.Index * 0x567;
return 0;
}
static bool HasAttribute<T>( this MemberInfo mi )
{
return mi.GetCustomAttributes(typeof(T), true).Length != 0;
}
}
}

View File

@@ -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<T>(this MemberInfo mi)
{
return mi.GetCustomAttributes(typeof(T), true).Length != 0;
}
}
}