improve TraitDictionary error messages
This commit is contained in:
@@ -70,6 +70,14 @@ namespace OpenRA
|
|||||||
public int2 Value( World world ) { return value; }
|
public int2 Value( World world ) { return value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class SubCellInit : IActorInit<SubCell>
|
||||||
|
{
|
||||||
|
[FieldFromYamlKey] public readonly int value = 0;
|
||||||
|
public SubCellInit() { }
|
||||||
|
public SubCellInit(int init) { value = init; }
|
||||||
|
public SubCell Value(World world) { return (SubCell)value; }
|
||||||
|
}
|
||||||
|
|
||||||
public class CenterLocationInit : IActorInit<int2>
|
public class CenterLocationInit : IActorInit<int2>
|
||||||
{
|
{
|
||||||
[FieldFromYamlKey] public readonly int2 value = int2.Zero;
|
[FieldFromYamlKey] public readonly int2 value = int2.Zero;
|
||||||
|
|||||||
@@ -162,7 +162,6 @@
|
|||||||
<Compile Include="Traits\RevealsShroud.cs" />
|
<Compile Include="Traits\RevealsShroud.cs" />
|
||||||
<Compile Include="Traits\Selectable.cs" />
|
<Compile Include="Traits\Selectable.cs" />
|
||||||
<Compile Include="Traits\SelectionDecorations.cs" />
|
<Compile Include="Traits\SelectionDecorations.cs" />
|
||||||
<Compile Include="Traits\SubcellInit.cs" />
|
|
||||||
<Compile Include="Traits\Target.cs" />
|
<Compile Include="Traits\Target.cs" />
|
||||||
<Compile Include="Traits\TraitsInterfaces.cs" />
|
<Compile Include="Traits\TraitsInterfaces.cs" />
|
||||||
<Compile Include="Traits\Util.cs" />
|
<Compile Include="Traits\Util.cs" />
|
||||||
|
|||||||
@@ -54,31 +54,33 @@ namespace OpenRA
|
|||||||
InnerGet( t ).Add( actor, val );
|
InnerGet( t ).Add( actor, val );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheckDestroyed(Actor actor)
|
||||||
|
{
|
||||||
|
if (actor.Destroyed)
|
||||||
|
throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor));
|
||||||
|
}
|
||||||
|
|
||||||
public bool Contains<T>( Actor actor )
|
public bool Contains<T>( Actor actor )
|
||||||
{
|
{
|
||||||
if( actor.Destroyed )
|
CheckDestroyed(actor);
|
||||||
throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor));
|
|
||||||
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).GetMultiple( actor.ActorID ).Count() != 0;
|
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).GetMultiple( actor.ActorID ).Count() != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Get<T>( Actor actor )
|
public T Get<T>( Actor actor )
|
||||||
{
|
{
|
||||||
if( actor.Destroyed )
|
CheckDestroyed(actor);
|
||||||
throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor));
|
|
||||||
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).Get( actor.ActorID );
|
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).Get( actor.ActorID );
|
||||||
}
|
}
|
||||||
|
|
||||||
public T GetOrDefault<T>( Actor actor )
|
public T GetOrDefault<T>( Actor actor )
|
||||||
{
|
{
|
||||||
if( actor.Destroyed )
|
CheckDestroyed(actor);
|
||||||
throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor));
|
|
||||||
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).GetOrDefault( actor.ActorID );
|
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).GetOrDefault( actor.ActorID );
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<T> WithInterface<T>( Actor actor )
|
public IEnumerable<T> WithInterface<T>( Actor actor )
|
||||||
{
|
{
|
||||||
if( actor.Destroyed )
|
CheckDestroyed(actor);
|
||||||
throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor));
|
|
||||||
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).GetMultiple( actor.ActorID );
|
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).GetMultiple( actor.ActorID );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,14 +120,10 @@ namespace OpenRA
|
|||||||
|
|
||||||
public T Get( uint actor )
|
public T Get( uint actor )
|
||||||
{
|
{
|
||||||
++queries;
|
var result = GetOrDefault(actor);
|
||||||
var index = actors.BinarySearchMany( actor );
|
if ((object)result == null)
|
||||||
if( index >= actors.Count || actors[ index ].ActorID != actor )
|
throw new InvalidOperationException("Actor does not have trait of type `{0}`".F(typeof(T)));
|
||||||
throw new InvalidOperationException("TraitDictionary does not contain instance of type `{0}`".F(typeof(T)));
|
return result;
|
||||||
else if( index + 1 < actors.Count && actors[ index + 1 ].ActorID == actor )
|
|
||||||
throw new InvalidOperationException("TraitDictionary contains multiple instance of type `{0}`".F(typeof(T)));
|
|
||||||
else
|
|
||||||
return traits[ index ];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T GetOrDefault( uint actor )
|
public T GetOrDefault( uint actor )
|
||||||
@@ -135,7 +133,7 @@ namespace OpenRA
|
|||||||
if( index >= actors.Count || actors[ index ].ActorID != actor )
|
if( index >= actors.Count || actors[ index ].ActorID != actor )
|
||||||
return default( T );
|
return default( T );
|
||||||
else if( index + 1 < actors.Count && actors[ index + 1 ].ActorID == actor )
|
else if( index + 1 < actors.Count && actors[ index + 1 ].ActorID == actor )
|
||||||
throw new InvalidOperationException("TraitDictionary contains multiple instance of type `{0}`".F(typeof(T)));
|
throw new InvalidOperationException("Actor has multiple traits of type `{0}`".F(typeof(T)));
|
||||||
else return traits[ index ];
|
else return traits[ index ];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
#region Copyright & License Information
|
|
||||||
/*
|
|
||||||
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
|
|
||||||
* This file is part of OpenRA, which is free software. It is made
|
|
||||||
* available to you under the terms of the GNU General Public License
|
|
||||||
* as published by the Free Software Foundation. For more information,
|
|
||||||
* see COPYING.
|
|
||||||
*/
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using OpenRA.FileFormats;
|
|
||||||
|
|
||||||
namespace OpenRA.Traits
|
|
||||||
{
|
|
||||||
public class SubCellInit : IActorInit<SubCell>
|
|
||||||
{
|
|
||||||
[FieldFromYamlKey]
|
|
||||||
public readonly int value = 0;
|
|
||||||
|
|
||||||
public SubCellInit() { }
|
|
||||||
|
|
||||||
public SubCellInit(int init)
|
|
||||||
{
|
|
||||||
value = init;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SubCell Value(World world)
|
|
||||||
{
|
|
||||||
return (SubCell)value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user