diff --git a/OpenRa.DataStructures/TypeDictionary.cs b/OpenRa.DataStructures/TypeDictionary.cs index b30afa5465..957e9d0284 100755 --- a/OpenRa.DataStructures/TypeDictionary.cs +++ b/OpenRa.DataStructures/TypeDictionary.cs @@ -13,9 +13,9 @@ namespace OpenRa inner.Add( t, val ); } - public void Add( T val ) + public void Add( object val ) { - Add( typeof( T ), val ); + Add( val.GetType(), val ); } public void Remove() diff --git a/OpenRa.Game/Actor.cs b/OpenRa.Game/Actor.cs index 4bfd8a76c6..4a731827fa 100755 --- a/OpenRa.Game/Actor.cs +++ b/OpenRa.Game/Actor.cs @@ -19,6 +19,16 @@ namespace OpenRa.Game public int Health; IActivity currentActivity; + object ConstructTrait(string traitName) + { + /* todo: allow mods to introduce traits */ + var type = typeof(Mobile).Assembly.GetType(typeof(Mobile).Namespace + "." + traitName, true, false); + var ctor = type.GetConstructor(new[] { typeof(Actor) }); + if (ctor == null) + throw new InvalidOperationException("Trait {0} does not have the correct constructor: {0}(Actor self)".F(type.Name)); + return ctor.Invoke(new object[] { this }); + } + public Actor( ActorInfo info, int2 location, Player owner ) { ActorID = Game.world.NextAID(); @@ -29,20 +39,13 @@ namespace OpenRa.Game if (Info == null) return; - Health = Info.Strength; /* todo: handle cases where this is not true! */ + Health = Info.Strength; /* todo: fix walls, etc so this is always true! */ 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) - { /* todo: a better solution than `the assembly Mobile lives in`, for mod support & sanity. */ - var type = typeof(Mobile).Assembly.GetType(typeof(Mobile).Namespace + "." + traitName, true, false); - var ctor = type.GetConstructor(new[] { typeof(Actor) }); - if (ctor == null) - throw new InvalidOperationException("Trait {0} does not have the correct constructor: {0}(Actor self)".F(type.Name)); - - traits.Add(type, ctor.Invoke(new object[] { this })); - } + traits.Add(ConstructTrait(traitName)); } public void Tick()