From 418b8d40f997326ed84a24489242c9be1fc078f7 Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Wed, 28 Dec 2011 08:01:59 +1300 Subject: [PATCH] improve TraitDictionary error messages --- OpenRA.Game/ActorInitializer.cs | 8 +++++++ OpenRA.Game/OpenRA.Game.csproj | 1 - OpenRA.Game/TraitDictionary.cs | 32 +++++++++++++-------------- OpenRA.Game/Traits/SubcellInit.cs | 36 ------------------------------- 4 files changed, 23 insertions(+), 54 deletions(-) delete mode 100644 OpenRA.Game/Traits/SubcellInit.cs diff --git a/OpenRA.Game/ActorInitializer.cs b/OpenRA.Game/ActorInitializer.cs index 7dfd385d39..3aa354fbb2 100755 --- a/OpenRA.Game/ActorInitializer.cs +++ b/OpenRA.Game/ActorInitializer.cs @@ -70,6 +70,14 @@ namespace OpenRA public int2 Value( World world ) { return value; } } + public class SubCellInit : IActorInit + { + [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 { [FieldFromYamlKey] public readonly int2 value = int2.Zero; diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index fc1750c113..ac866bda11 100755 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -162,7 +162,6 @@ - diff --git a/OpenRA.Game/TraitDictionary.cs b/OpenRA.Game/TraitDictionary.cs index 1c382db161..ef6cb8eaf5 100755 --- a/OpenRA.Game/TraitDictionary.cs +++ b/OpenRA.Game/TraitDictionary.cs @@ -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( Actor actor ) { - if( actor.Destroyed ) - throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor)); + CheckDestroyed(actor); return ( (TraitContainer)InnerGet( typeof( T ) ) ).GetMultiple( actor.ActorID ).Count() != 0; } public T Get( Actor actor ) { - if( actor.Destroyed ) - throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor)); + CheckDestroyed(actor); return ( (TraitContainer)InnerGet( typeof( T ) ) ).Get( actor.ActorID ); } public T GetOrDefault( Actor actor ) { - if( actor.Destroyed ) - throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor)); + CheckDestroyed(actor); return ( (TraitContainer)InnerGet( typeof( T ) ) ).GetOrDefault( actor.ActorID ); } public IEnumerable WithInterface( Actor actor ) { - if( actor.Destroyed ) - throw new InvalidOperationException("Attempted to get trait from destroyed object ({0})".F(actor)); + CheckDestroyed(actor); return ( (TraitContainer)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 ]; } diff --git a/OpenRA.Game/Traits/SubcellInit.cs b/OpenRA.Game/Traits/SubcellInit.cs deleted file mode 100644 index bd9241a987..0000000000 --- a/OpenRA.Game/Traits/SubcellInit.cs +++ /dev/null @@ -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 - { - [FieldFromYamlKey] - public readonly int value = 0; - - public SubCellInit() { } - - public SubCellInit(int init) - { - value = init; - } - - public SubCell Value(World world) - { - return (SubCell)value; - } - } -}