Skip to content

Commit

Permalink
attribute individual fields, not the class
Browse files Browse the repository at this point in the history
  • Loading branch information
ytinasni authored and chrisforbes committed Aug 30, 2010
1 parent 29a77f7 commit 9fedeef
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 71 deletions.
51 changes: 16 additions & 35 deletions OpenRA.FileFormats/FieldLoader.cs
Expand Up @@ -165,49 +165,30 @@ static object ParseYesNo( string p, System.Type fieldType, string field )
{
var ret = new Dictionary<FieldInfo, Func<string, Type, MiniYaml, object>>();

var fieldsToLoad = new List<FieldInfo>();
var attr = (FooAttribute[])type.GetCustomAttributes( typeof( FooAttribute ), false );
if( attr.Length == 0 )
{
var defCtor = type.GetConstructor( new Type[ 0 ] );
if( defCtor == null || ( defCtor.GetMethodImplementationFlags() | MethodImplAttributes.Runtime ) == 0 )
throw new InvalidOperationException( "FieldLoader: refusing to load type {0}; has non-default ctor".F( type ) );
fieldsToLoad.AddRange( type.GetFields() );
}
else if( attr[ 0 ].Fields != null )
fieldsToLoad.AddRange( attr[ 0 ].Fields.Select( x => type.GetField( x ) ) );
else
fieldsToLoad.AddRange( type.GetFields() );

foreach( var field in fieldsToLoad )
ret.Add( field, null );

foreach( var field in type.GetFields() )
{
var use = (LoadUsingAttribute[])field.GetCustomAttributes( typeof( LoadUsingAttribute ), false );
if( use.Length != 0 )
ret[ field ] = ( _1, fieldType, yaml ) => use[ 0 ].LoaderFunc( field )( yaml );
else
{
var attr2 = (FieldFromYamlKeyAttribute[])field.GetCustomAttributes( typeof( FieldFromYamlKeyAttribute ), false );
if( attr2.Length != 0 )
ret[ field ] = ( f, ft, yaml ) => GetValue( f, ft, yaml.Value );
}
var load = (LoadAttribute[])field.GetCustomAttributes( typeof( LoadAttribute ), false );
var loadUsing = (LoadUsingAttribute[])field.GetCustomAttributes( typeof( LoadUsingAttribute ), false );
var fromYamlKey = (FieldFromYamlKeyAttribute[])field.GetCustomAttributes( typeof( FieldFromYamlKeyAttribute ), false );
if( loadUsing.Length != 0 )
ret[ field ] = ( _1, fieldType, yaml ) => loadUsing[ 0 ].LoaderFunc( field )( yaml );
else if( fromYamlKey.Length != 0 )
ret[ field ] = ( f, ft, yaml ) => GetValue( f, ft, yaml.Value );
else if( load.Length != 0 )
ret[ field ] = null;
}

if( ret.Count == 0 )
foreach( var f in type.GetFields() )
ret.Add( f, null );

return ret;
}

public class FooAttribute : Attribute
{
public string[] Fields;

public FooAttribute( params string[] fields )
{
Fields = fields;
}
}
[AttributeUsage( AttributeTargets.Field )]
public class LoadAttribute : Attribute { }

[AttributeUsage( AttributeTargets.Field )]
public class LoadUsingAttribute : Attribute
{
Func<MiniYaml, object> loaderFuncCache;
Expand Down
17 changes: 8 additions & 9 deletions OpenRA.FileFormats/Map/MapStub.cs
Expand Up @@ -15,27 +15,26 @@

namespace OpenRA.FileFormats
{
[FieldLoader.Foo( "Selectable", "Title", "Description", "Author", "PlayerCount", "Tileset", "TopLeft", "BottomRight" )]
public class MapStub
{
public readonly IFolder Package;

// Yaml map data
public readonly string Uid;
public bool Selectable;
[FieldLoader.Load] public bool Selectable;

public string Title;
public string Description;
public string Author;
public int PlayerCount;
public string Tileset;
[FieldLoader.Load] public string Title;
[FieldLoader.Load] public string Description;
[FieldLoader.Load] public string Author;
[FieldLoader.Load] public int PlayerCount;
[FieldLoader.Load] public string Tileset;

[FieldLoader.LoadUsing( "LoadWaypoints" )]
public Dictionary<string, int2> Waypoints = new Dictionary<string, int2>();
public IEnumerable<int2> SpawnPoints { get { return Waypoints.Select(kv => kv.Value); } }

public int2 TopLeft;
public int2 BottomRight;
[FieldLoader.Load] public int2 TopLeft;
[FieldLoader.Load] public int2 BottomRight;
public int Width { get { return BottomRight.X - TopLeft.X; } }
public int Height { get { return BottomRight.Y - TopLeft.Y; } }

Expand Down
11 changes: 5 additions & 6 deletions OpenRA.FileFormats/Map/TileSet.cs
Expand Up @@ -29,13 +29,12 @@ public class TerrainTypeInfo
public MiniYaml Save() { return FieldSaver.Save(this); }
}

[FieldLoader.Foo("Id", "Image", "Size", "PickAny")]
public class TileTemplate
{
public ushort Id;
public string Image;
public int2 Size;
public bool PickAny;
[FieldLoader.Load] public ushort Id;
[FieldLoader.Load] public string Image;
[FieldLoader.Load] public int2 Size;
[FieldLoader.Load] public bool PickAny;

[FieldLoader.LoadUsing( "LoadTiles" )]
public Dictionary<byte, string> Tiles = new Dictionary<byte, string>();
Expand Down Expand Up @@ -84,7 +83,7 @@ public class TileSet
public TileSet() {}
public TileSet( string filepath )
{
var yaml = MiniYaml.FromFile(filepath).ToDictionary( x => x.Key, x => x.Value );
var yaml = MiniYaml.DictFromFile( filepath );

// General info
FieldLoader.Load(this, yaml["General"]);
Expand Down
7 changes: 3 additions & 4 deletions OpenRA.Game/GameRules/MusicInfo.cs
Expand Up @@ -12,12 +12,11 @@

namespace OpenRA.GameRules
{
[FieldLoader.Foo()]
public class MusicInfo
{
public readonly string Filename = null;
public readonly string Title = null;
public readonly int Length = 0; // seconds
[FieldLoader.Load] public readonly string Filename = null;
[FieldLoader.Load] public readonly string Title = null;
[FieldLoader.Load] public readonly int Length = 0; // seconds

public MusicInfo( string key, MiniYaml value )
{
Expand Down
16 changes: 10 additions & 6 deletions OpenRA.Game/GameRules/VoiceInfo.cs
Expand Up @@ -15,17 +15,21 @@

namespace OpenRA.GameRules
{
[FieldLoader.Foo( "DisableVariants" )]
public class VoiceInfo
{
public readonly Dictionary<string,string[]> Variants;
public readonly Dictionary<string,string[]> Voices;
public readonly string DefaultVariant = ".aud" ;
public readonly string[] DisableVariants = { };

Func<MiniYaml, string, Dictionary<string, string[]>> Load = (y,name) => (y.NodesDict.ContainsKey(name))? y.NodesDict[name].NodesDict.ToDictionary(a => a.Key,
a => (string[])FieldLoader.GetValue( "(value)", typeof(string[]), a.Value.Value ))
: new Dictionary<string, string[]>();
[FieldLoader.Load] public readonly string[] DisableVariants = { };

static Dictionary<string, string[]> Load( MiniYaml y, string name )
{
return y.NodesDict.ContainsKey( name )
? y.NodesDict[ name ].NodesDict.ToDictionary(
a => a.Key,
a => (string[])FieldLoader.GetValue( "(value)", typeof( string[] ), a.Value.Value ) )
: new Dictionary<string, string[]>();
}

public readonly Lazy<Dictionary<string, VoicePool>> Pools;

Expand Down
21 changes: 10 additions & 11 deletions OpenRA.Game/Map.cs
Expand Up @@ -19,20 +19,19 @@

namespace OpenRA
{
[FieldLoader.Foo( "Selectable", "MapFormat", "Title", "Description", "Author", "PlayerCount", "Tileset", "MapSize", "TopLeft", "BottomRight" )]
public class Map
{
public IFolder Package;
public string Uid;

// Yaml map data
public bool Selectable = true;
public int MapFormat;
public string Title;
public string Description;
public string Author;
public int PlayerCount;
public string Tileset;
[FieldLoader.Load] public bool Selectable = true;
[FieldLoader.Load] public int MapFormat;
[FieldLoader.Load] public string Title;
[FieldLoader.Load] public string Description;
[FieldLoader.Load] public string Author;
[FieldLoader.Load] public int PlayerCount;
[FieldLoader.Load] public string Tileset;

public Dictionary<string, PlayerReference> Players = new Dictionary<string, PlayerReference>();
public Dictionary<string, ActorReference> Actors = new Dictionary<string, ActorReference>();
Expand All @@ -44,10 +43,10 @@ public class Map

// Binary map data
public byte TileFormat = 1;
public int2 MapSize;
[FieldLoader.Load] public int2 MapSize;

public int2 TopLeft;
public int2 BottomRight;
[FieldLoader.Load] public int2 TopLeft;
[FieldLoader.Load] public int2 BottomRight;

public TileReference<ushort, byte>[,] MapTiles;
public TileReference<byte, byte>[,] MapResources;
Expand Down

0 comments on commit 9fedeef

Please sign in to comment.