From e86b0cdaa094c73fc5af8bdca7301ccf030aa018 Mon Sep 17 00:00:00 2001 From: Bob Date: Tue, 13 Oct 2009 19:17:43 +1300 Subject: [PATCH] extracted GameRules.FieldLoader from UnitInfo.cs --- OpenRa.Game/GameRules/FieldLoader.cs | 46 ++++++++++++++++++++++++++++ OpenRa.Game/GameRules/UnitInfo.cs | 32 +------------------ OpenRa.Game/OpenRa.Game.csproj | 1 + 3 files changed, 48 insertions(+), 31 deletions(-) create mode 100755 OpenRa.Game/GameRules/FieldLoader.cs diff --git a/OpenRa.Game/GameRules/FieldLoader.cs b/OpenRa.Game/GameRules/FieldLoader.cs new file mode 100755 index 0000000000..d76747b1f6 --- /dev/null +++ b/OpenRa.Game/GameRules/FieldLoader.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using OpenRa.FileFormats; + +namespace OpenRa.Game.GameRules +{ + static class FieldLoader + { + public static void Load( UnitInfo.BaseInfo self, IniSection ini ) + { + foreach( var x in ini ) + { + var field = self.GetType().GetField( x.Key ); + if( field.FieldType == typeof( int ) ) + field.SetValue( self, int.Parse( x.Value ) ); + + else if( field.FieldType == typeof( float ) ) + field.SetValue( self, float.Parse( x.Value ) ); + + else if( field.FieldType == typeof( string ) ) + field.SetValue( self, x.Value.ToLowerInvariant() ); + + else if( field.FieldType.IsEnum ) + field.SetValue( self, Enum.Parse( field.FieldType, x.Value ) ); + + else if( field.FieldType == typeof( bool ) ) + field.SetValue( self, ParseYesNo( x.Value ) ); + + else + do { } while( false ); + } + } + + static bool ParseYesNo( string p ) + { + p = p.ToLowerInvariant(); + if( p == "yes" ) return true; + if( p == "true" ) return true; + if( p == "no" ) return false; + if( p == "false" ) return false; + throw new InvalidOperationException(); + } + } +} diff --git a/OpenRa.Game/GameRules/UnitInfo.cs b/OpenRa.Game/GameRules/UnitInfo.cs index f4a2dfe8c2..461227d53e 100755 --- a/OpenRa.Game/GameRules/UnitInfo.cs +++ b/OpenRa.Game/GameRules/UnitInfo.cs @@ -10,16 +10,6 @@ namespace OpenRa.Game.GameRules { public class UnitInfo { - static bool ParseYesNo( string p ) - { - p = p.ToLowerInvariant(); - if( p == "yes" ) return true; - if( p == "true" ) return true; - if( p == "no" ) return false; - if( p == "false" ) return false; - throw new InvalidOperationException(); - } - static Func BindInfoCtor() where T : BaseInfo { @@ -92,27 +82,7 @@ namespace OpenRa.Game.GameRules { Name = name.ToLowerInvariant(); - foreach( var x in ini ) - { - var field = this.GetType().GetField( x.Key ); - if( field.FieldType == typeof( int ) ) - field.SetValue( this, int.Parse( x.Value ) ); - - else if( field.FieldType == typeof( float ) ) - field.SetValue( this, float.Parse( x.Value ) ); - - else if( field.FieldType == typeof( string ) ) - field.SetValue( this, x.Value.ToLowerInvariant() ); - - else if( field.FieldType == typeof( ArmorType ) ) - field.SetValue( this, Enum.Parse(x.Value) ); - - else if( field.FieldType == typeof( bool ) ) - field.SetValue( this, ParseYesNo( x.Value ) ); - - else - do { } while( false ); - } + FieldLoader.Load( this, ini ); } } diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index aee4d9b3b5..fb865d3bfa 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -72,6 +72,7 @@ +