moved FieldLoader and .F() ext into OpenRa.FileFormats
This commit is contained in:
@@ -3,7 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using IjwFramework.Types;
|
||||
using OpenRa.Game.GameRules;
|
||||
using OpenRa.FileFormats;
|
||||
using OpenRa.Game.Graphics;
|
||||
using OpenRa.Game.Orders;
|
||||
using OpenRa.Game.Support;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using OpenRa.FileFormats;
|
||||
using OpenRa.Game.Graphics;
|
||||
using OpenRa.Game.Traits;
|
||||
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
|
||||
using System.Windows.Forms;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRa.Game.GameRules;
|
||||
using OpenRa.Game.Traits;
|
||||
@@ -9,11 +7,6 @@ namespace OpenRa.Game
|
||||
{
|
||||
static class Exts
|
||||
{
|
||||
public static string F(this string fmt, params object[] args)
|
||||
{
|
||||
return string.Format(fmt, args);
|
||||
}
|
||||
|
||||
public static bool HasModifier(this Modifiers k, Modifiers mod)
|
||||
{
|
||||
return (k & mod) == mod;
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using OpenRa.FileFormats;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace OpenRa.Game.GameRules
|
||||
{
|
||||
static class FieldLoader
|
||||
{
|
||||
public static void Load(object self, IniSection ini)
|
||||
{
|
||||
foreach (var x in ini)
|
||||
{
|
||||
var field = self.GetType().GetField(x.Key.Trim());
|
||||
field.SetValue(self, GetValue(field.FieldType, x.Value.Trim()));
|
||||
}
|
||||
}
|
||||
|
||||
public static void Load(object self, MiniYaml my)
|
||||
{
|
||||
foreach (var x in my.Nodes)
|
||||
{
|
||||
var field = self.GetType().GetField(x.Key.Trim());
|
||||
if (field == null)
|
||||
throw new NotImplementedException("Missing field `{0}` on `{1}`".F(x.Key.Trim(), self.GetType().Name));
|
||||
field.SetValue(self, GetValue(field.FieldType, x.Value.Value.Trim()));
|
||||
}
|
||||
}
|
||||
|
||||
static object GetValue( Type fieldType, string x )
|
||||
{
|
||||
if( fieldType == typeof( int ) )
|
||||
return int.Parse( x );
|
||||
|
||||
else if (fieldType == typeof(float))
|
||||
return float.Parse(x.Replace("%","")) * (x.Contains( '%' ) ? 0.01f : 1f);
|
||||
|
||||
else if (fieldType == typeof(string))
|
||||
return x;
|
||||
|
||||
else if (fieldType.IsEnum)
|
||||
return Enum.Parse(fieldType, x, true);
|
||||
|
||||
else if (fieldType == typeof(bool))
|
||||
return ParseYesNo(x);
|
||||
|
||||
else if (fieldType.IsArray)
|
||||
{
|
||||
var parts = x.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
var ret = Array.CreateInstance(fieldType.GetElementType(), parts.Length);
|
||||
for (int i = 0; i < parts.Length; i++)
|
||||
ret.SetValue(GetValue(fieldType.GetElementType(), parts[i].Trim()), i);
|
||||
return ret;
|
||||
}
|
||||
else if (fieldType == typeof(int2))
|
||||
{
|
||||
var parts = x.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
return new int2(int.Parse(parts[0]), int.Parse(parts[1]));
|
||||
}
|
||||
else
|
||||
throw new InvalidOperationException("FieldLoader: don't know how to load field of type " + fieldType.ToString());
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ using System.Linq;
|
||||
using IjwFramework.Types;
|
||||
using OpenRa.FileFormats;
|
||||
using OpenRa.Game.GameRules;
|
||||
using OpenRa.Game.Traits;
|
||||
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
|
||||
@@ -153,7 +153,6 @@
|
||||
<Compile Include="Controller.cs" />
|
||||
<Compile Include="Cursor.cs" />
|
||||
<Compile Include="Effects\Explosion.cs" />
|
||||
<Compile Include="GameRules\FieldLoader.cs" />
|
||||
<Compile Include="GameRules\Footprint.cs" />
|
||||
<Compile Include="GameRules\InfoLoader.cs" />
|
||||
<Compile Include="GameRules\ProjectileInfo.cs" />
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Drawing;
|
||||
using OpenRa.FileFormats;
|
||||
using OpenRa.Game.GameRules;
|
||||
using OpenRa.Game.Graphics;
|
||||
using OpenRa.Game.Traits;
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
using System.Reflection.Emit;
|
||||
using IjwFramework.Collections;
|
||||
using OpenRa.FileFormats;
|
||||
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user