fix bug 609 -- removing traits that arent on an actor is an error.

This commit is contained in:
Chris Forbes
2011-04-05 21:26:32 +12:00
parent 576e9b90f6
commit 55c609ee16
2 changed files with 61 additions and 22 deletions

View File

@@ -22,6 +22,7 @@ namespace OpenRA.FileFormats
public struct SourceLocation public struct SourceLocation
{ {
public string Filename; public int Line; public string Filename; public int Line;
public override string ToString() { return "{0}:{1}".F(Filename, Line); }
} }
public SourceLocation Location; public SourceLocation Location;
@@ -53,6 +54,11 @@ namespace OpenRA.FileFormats
: this( k, new MiniYaml( v, n ), loc ) : this( k, new MiniYaml( v, n ), loc )
{ {
} }
public override string ToString()
{
return "{{YamlNode: {0} @ {1}}}".F(Key, Location);
}
} }
public class MiniYaml public class MiniYaml
@@ -95,7 +101,7 @@ namespace OpenRA.FileFormats
var level = line.Length - t.Length; var level = line.Length - t.Length;
if (levels.Count <= level) if (levels.Count <= level)
throw new InvalidOperationException("Bad indent in miniyaml"); throw new YamlException("Bad indent in miniyaml");
while (levels.Count > level + 1) while (levels.Count > level + 1)
levels.RemoveAt(levels.Count - 1); levels.RemoveAt(levels.Count - 1);
@@ -171,7 +177,8 @@ namespace OpenRA.FileFormats
var bDict = b.ToDictionary( x => x.Key ); var bDict = b.ToDictionary( x => x.Key );
var keys = aDict.Keys.Union( bDict.Keys ).ToList(); var keys = aDict.Keys.Union( bDict.Keys ).ToList();
var noInherit = keys.Where( x => x.Length > 0 && x[ 0 ] == '-' ).Select( x => x.Substring( 1 ) ).ToList(); var noInherit = keys.Where(x => x.Length > 0 && x[0] == '-')
.ToDictionary(x => x.Substring(1), x => false);
foreach( var key in keys ) foreach( var key in keys )
{ {
@@ -179,10 +186,11 @@ namespace OpenRA.FileFormats
aDict.TryGetValue( key, out aa ); aDict.TryGetValue( key, out aa );
bDict.TryGetValue( key, out bb ); bDict.TryGetValue( key, out bb );
if( noInherit.Contains( key ) ) if( noInherit.ContainsKey( key ) )
{ {
if( aa != null ) // if( aa != null )
ret.Add( aa ); // ret.Add( aa );
noInherit[key] = true;
} }
else else
{ {
@@ -192,6 +200,10 @@ namespace OpenRA.FileFormats
} }
} }
if (noInherit.ContainsValue(false))
throw new YamlException("Bogus yaml removals: {0}".F(
string.Join(", ", noInherit.Where(x => !x.Value).Select(x => x.Key).ToArray())));
return ret; return ret;
} }
@@ -237,4 +249,9 @@ namespace OpenRA.FileFormats
} }
} }
} }
public class YamlException : Exception
{
public YamlException(string s) : base(s) { }
}
} }

View File

@@ -23,14 +23,30 @@ namespace OpenRA
public ActorInfo( string name, MiniYaml node, Dictionary<string, MiniYaml> allUnits ) public ActorInfo( string name, MiniYaml node, Dictionary<string, MiniYaml> allUnits )
{ {
var mergedNode = MergeWithParent( node, allUnits ).NodesDict; try
{
var mergedNode = MergeWithParent(node, allUnits).NodesDict;
Name = name; Name = name;
foreach( var t in mergedNode ) foreach (var t in mergedNode)
if( t.Key != "Inherits" && !t.Key.StartsWith("-") ) if (t.Key != "Inherits" && !t.Key.StartsWith("-"))
Traits.Add( LoadTraitInfo( t.Key.Split('@')[0], t.Value ) ); Traits.Add(LoadTraitInfo(t.Key.Split('@')[0], t.Value));
}
catch (YamlException e)
{
throw new YamlException("Actor type {0}: {1}".F(name, e.Message));
}
} }
static IEnumerable<MiniYaml> GetInheritanceChain(MiniYaml node, Dictionary<string, MiniYaml> allUnits)
{
while (node != null)
{
yield return node;
node = GetParent(node, allUnits);
}
}
static MiniYaml GetParent( MiniYaml node, Dictionary<string, MiniYaml> allUnits ) static MiniYaml GetParent( MiniYaml node, Dictionary<string, MiniYaml> allUnits )
{ {
MiniYaml inherits; MiniYaml inherits;
@@ -50,8 +66,14 @@ namespace OpenRA
static MiniYaml MergeWithParent( MiniYaml node, Dictionary<string, MiniYaml> allUnits ) static MiniYaml MergeWithParent( MiniYaml node, Dictionary<string, MiniYaml> allUnits )
{ {
var parent = GetParent( node, allUnits ); var parent = GetParent( node, allUnits );
if( parent != null ) if (parent != null)
return MiniYaml.Merge( node, MergeWithParent( parent, allUnits ) ); {
var result = MiniYaml.Merge(node, MergeWithParent(parent, allUnits));
// strip the '-'
result.Nodes.RemoveAll(a => a.Key.StartsWith("-"));
return result;
}
return node; return node;
} }