Load unit data from ra.yaml

This commit is contained in:
Bob
2010-01-09 22:50:56 +13:00
parent 454e3a9ca9
commit 0b6a05fcee
13 changed files with 1707 additions and 6 deletions

58
OpenRa.FileFormats/MiniYaml.cs Executable file
View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace OpenRa.FileFormats
{
public class MiniYaml
{
public string Value;
public Dictionary<string, MiniYaml> Nodes = new Dictionary<string,MiniYaml>();
public MiniYaml( string value ) : this( value, new Dictionary<string, MiniYaml>() ) { }
public MiniYaml( string value, Dictionary<string, MiniYaml> nodes )
{
Value = value;
Nodes = nodes;
}
public static Dictionary<string, MiniYaml> FromFile( string path )
{
var lines = File.ReadAllLines( path );
var levels = new List<Dictionary<string, MiniYaml>>();
levels.Add( new Dictionary<string, MiniYaml>() );
foreach( var line in lines )
{
var t = line.TrimStart( ' ', '\t' );
if( t.Length == 0 || t[ 0 ] == '#' )
continue;
var level = line.Length - t.Length;
if( levels.Count <= level )
throw new InvalidOperationException( "Bad indent in miniyaml" );
while( levels.Count > level + 1 )
levels.RemoveAt( levels.Count - 1 );
var colon = t.IndexOf( ':' );
var d = new Dictionary<string, MiniYaml>();
if( colon == -1 )
levels[ level ].Add( t.Trim(), new MiniYaml( null, d ) );
else
{
var value = t.Substring( colon + 1 ).Trim();
if( value.Length == 0 )
value = null;
levels[ level ].Add( t.Substring( 0, colon ).Trim(), new MiniYaml( value, d ) );
}
levels.Add( d );
}
return levels[ 0 ];
}
}
}

View File

@@ -57,6 +57,7 @@
<Compile Include="IniWriter.cs" />
<Compile Include="IPaletteRemap.cs" />
<Compile Include="Map.cs" />
<Compile Include="MiniYaml.cs" />
<Compile Include="PackageEntry.cs" />
<Compile Include="Package.cs" />
<Compile Include="Palette.cs" />

View File

@@ -48,7 +48,10 @@ namespace OpenRa.Game
if( Info.Traits == null )
throw new InvalidOperationException( "No Actor traits for {0}; add Traits= to units.ini for appropriate unit".F(Info.Name) );
foreach (var traitName in Info.Traits)
//foreach (var traitName in Info.Traits)
// traits.Add(ConstructTrait(traitName));
foreach( var traitName in Rules.NewUnitInfo[Info.Name.ToLower()].Traits.Keys )
traits.Add( ConstructTrait( traitName ) );
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Linq;
using OpenRa.FileFormats;
using System.Collections.Generic;
namespace OpenRa.Game.GameRules
{
@@ -15,6 +16,39 @@ namespace OpenRa.Game.GameRules
}
}
public static void CheckYaml( object self, Dictionary<string, MiniYaml> d )
{
//foreach( var x in d )
//{
// if( x.Key == "Tab" ) continue;
// if( x.Key == "Description" ) continue;
// if( x.Key == "LongDesc" ) continue;
// var key = x.Key;
// if( key == "Prerequisites" ) key = "Prerequisite";
// if( key == "HP" ) key = "Strength";
// if( key == "Priority" ) key = "SelectionPriority";
// if( key == "Bounds" ) key = "SelectionSize";
// var field = self.GetType().GetField( key );
// var old = field.GetValue( self );
// var neww = GetValue( field.FieldType, x.Value.Value.Trim() );
// if( old.ToString() != neww.ToString() )
// throw new NotImplementedException();
//}
foreach( var x in d )
{
var key = x.Key;
if( key == "Tab" )
continue;
if( key == "Prerequisites" ) key = "Prerequisite";
if( key == "HP" ) key = "Strength";
if( key == "Priority" ) key = "SelectionPriority";
if( key == "Bounds" ) key = "SelectionSize";
var field = self.GetType().GetField( key.Trim() );
field.SetValue( self, GetValue( field.FieldType, x.Value.Value.Trim() ) );
}
}
static object GetValue( Type fieldType, string x )
{
if( fieldType == typeof( int ) )

View File

@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRa.FileFormats;
namespace OpenRa.Game.GameRules
{
class NewUnitInfo
{
public readonly string Parent;
public readonly Dictionary<string, MiniYaml> Traits = new Dictionary<string, MiniYaml>();
public NewUnitInfo( MiniYaml node )
{
MiniYaml inherit;
if( node.Nodes.TryGetValue( "Inherits", out inherit ) )
{
Parent = inherit.Value;
node.Nodes.Remove( "Inherits" );
}
Traits = node.Nodes;
}
}
}

View File

@@ -24,6 +24,8 @@ namespace OpenRa.Game
public static Map Map;
public static TileSet TileSet;
public static Dictionary<string, NewUnitInfo> NewUnitInfo;
public static void LoadRules(string mapFileName, bool useAftermath)
{
if( useAftermath )
@@ -91,6 +93,14 @@ namespace OpenRa.Game
Map = new Map( AllRules );
FileSystem.MountTemporary( new Package( Rules.Map.Theater + ".mix" ) );
TileSet = new TileSet( Map.TileSuffix );
NewUnitInfo = new Dictionary<string, NewUnitInfo>();
foreach( var kv in MiniYaml.FromFile( "ra.yaml" ) )
NewUnitInfo.Add( kv.Key.ToLowerInvariant(), new NewUnitInfo( kv.Value ) );
foreach( var unit in NewUnitInfo )
foreach( var trait in unit.Value.Traits.Values )
FieldLoader.CheckYaml( UnitInfo[ unit.Key.ToLowerInvariant() ], trait.Nodes );
}
static void LoadCategories(params string[] types)

View File

@@ -95,6 +95,7 @@
<Compile Include="GameRules\ActorInfo.cs" />
<Compile Include="GameRules\AftermathInfo.cs" />
<Compile Include="GameRules\GeneralInfo.cs" />
<Compile Include="GameRules\NewUnitInfo.cs" />
<Compile Include="GameRules\SupportPowerInfo.cs" />
<Compile Include="GameRules\TechTree.cs" />
<Compile Include="GameRules\UserSettings.cs" />
@@ -215,6 +216,7 @@
<Compile Include="Traits\AutoHeal.cs" />
<Compile Include="Traits\AutoTarget.cs" />
<Compile Include="Traits\BelowUnits.cs" />
<Compile Include="Traits\Buildable.cs" />
<Compile Include="Traits\Building.cs" />
<Compile Include="Traits\C4Demolition.cs" />
<Compile Include="Traits\Cargo.cs" />
@@ -240,6 +242,7 @@
<Compile Include="Traits\ProvidesRadar.cs" />
<Compile Include="Traits\Repairable.cs" />
<Compile Include="Traits\Reservable.cs" />
<Compile Include="Traits\Selectable.cs" />
<Compile Include="Traits\SquishByTank.cs" />
<Compile Include="Traits\Plane.cs" />
<Compile Include="Traits\ProductionQueue.cs" />

View File

@@ -9,7 +9,7 @@ namespace OpenRa.Game.Support
{
static class PerfHistory
{
static readonly Color[] colors = { Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Orange, Color.Fuchsia, Color.Lime, Color.LightBlue };
static readonly Color[] colors = { Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Orange, Color.Fuchsia, Color.Lime, Color.LightBlue, Color.White, Color.Black };
static int nextColor;
public static Cache<string, PerfItem> items = new Cache<string, PerfItem>(

12
OpenRa.Game/Traits/Buildable.cs Executable file
View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits
{
class Buildable
{
public Buildable( Actor self ) { }
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OpenRa.Game.Traits
{
class Selectable
{
public Selectable( Actor self ) { }
}
}

View File

@@ -45,7 +45,7 @@ namespace RulesConverter
{ "Selectable", new PL {
{ "Priority", "SelectionPriority" },
{ "Voice", "Voice" },
{ "@Bounds", "SelectionSize" } }
{ "Bounds", "SelectionSize" } }
},
{ "Mobile", new PL {
@@ -112,7 +112,7 @@ namespace RulesConverter
{ "Buildable", new PL {
{ "TechLevel", "TechLevel" },
{ "Tab", "$Tab" },
{ "@Prerequisites", "Prerequisite" },
{ "Prerequisites", "Prerequisite" },
{ "Owner", "Owner" },
{ "Cost", "Cost" },
{ "Icon", "Icon" },
@@ -121,7 +121,7 @@ namespace RulesConverter
},
{ "Cargo", new PL {
{ "@PassengerTypes", "PassengerTypes" } }
{ "PassengerTypes", "PassengerTypes" } }
},
{ "LimitedAmmo", new PL {

View File

@@ -21,6 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<UseVSHostingProcess>false</UseVSHostingProcess>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>

1543
ra.yaml Normal file

File diff suppressed because it is too large Load Diff