improve TraitDictionary error messages

This commit is contained in:
Chris Forbes
2011-12-28 08:01:59 +13:00
parent 9581595a3d
commit 418b8d40f9
4 changed files with 23 additions and 54 deletions

View File

@@ -70,6 +70,14 @@ namespace OpenRA
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>
{
[FieldFromYamlKey] public readonly int2 value = int2.Zero;

View File

@@ -162,7 +162,6 @@
<Compile Include="Traits\RevealsShroud.cs" />
<Compile Include="Traits\Selectable.cs" />
<Compile Include="Traits\SelectionDecorations.cs" />
<Compile Include="Traits\SubcellInit.cs" />
<Compile Include="Traits\Target.cs" />
<Compile Include="Traits\TraitsInterfaces.cs" />
<Compile Include="Traits\Util.cs" />

View File

@@ -54,31 +54,33 @@ namespace OpenRA
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 )
{
if( actor.Destroyed )
throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor));
CheckDestroyed(actor);
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).GetMultiple( actor.ActorID ).Count() != 0;
}
public T Get<T>( Actor actor )
{
if( actor.Destroyed )
throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor));
CheckDestroyed(actor);
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).Get( actor.ActorID );
}
public T GetOrDefault<T>( Actor actor )
{
if( actor.Destroyed )
throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor));
CheckDestroyed(actor);
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).GetOrDefault( actor.ActorID );
}
public IEnumerable<T> WithInterface<T>( Actor actor )
{
if( actor.Destroyed )
throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor));
CheckDestroyed(actor);
return ( (TraitContainer<T>)InnerGet( typeof( T ) ) ).GetMultiple( actor.ActorID );
}
@@ -118,14 +120,10 @@ namespace OpenRA
public T Get( uint actor )
{
++queries;
var index = actors.BinarySearchMany( actor );
if( index >= actors.Count || actors[ index ].ActorID != actor )
throw new InvalidOperationException("TraitDictionary does not contain instance of type `{0}`".F(typeof(T)));
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 ];
var result = GetOrDefault(actor);
if ((object)result == null)
throw new InvalidOperationException("Actor does not have trait of type `{0}`".F(typeof(T)));
return result;
}
public T GetOrDefault( uint actor )
@@ -135,7 +133,7 @@ namespace OpenRA
if( index >= actors.Count || actors[ index ].ActorID != actor )
return default( T );
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 ];
}

View File

@@ -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;
}
}
}