Mercurial > repos > IBDev-IBBoard.WarFoundry.API
view api/Factories/WarFoundryFactoryFactory.cs @ 8:613bc5eaac59
Re #9 - Make WarFoundry loading granular
* Remove specific staged loading classes
* Rework category loading for GameSystem and Race to make it use AddCategory(Category) method
* Promote staged loading from Native Factory to all Factories level
* Refactor XML Factory to use smaller methods
Also removed some commented code that isn't used any more
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 04 Jan 2009 19:24:13 +0000 |
parents | 520818033bb6 |
children | 306558904c2a |
line wrap: on
line source
// AbstractNativeWarFoundryFactory.cs // // Copyright (C) 2007 IBBoard // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License version 2.1 of the License as published by the Free // Software Foundation. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // using System; using System.Collections.Generic; using System.Reflection; using IBBoard; using IBBoard.Logging; namespace IBBoard.WarFoundry.API.Factories { public class WarFoundryFactoryFactory { private Dictionary<Type, IWarFoundryFactory> factories = new Dictionary<Type, IWarFoundryFactory>(); private Type defaultType; private static WarFoundryFactoryFactory factoryFactory; private WarFoundryFactoryFactory() { } public static WarFoundryFactoryFactory GetFactoryFactory() { if (factoryFactory == null) { factoryFactory = new WarFoundryFactoryFactory(); } return factoryFactory; } public IWarFoundryFactory GetFactory() { return GetFactory(DefaultType); } public IWarFoundryFactory GetFactory(Type cls) { cls = CheckType(cls); IWarFoundryFactory factory = null; factories.TryGetValue(cls, out factory); if (factory == null) { factory = null; MethodInfo method = cls.GetMethod("CreateFactory"); if (method!=null) { LogNotifier.Debug(GetType(), "Found CreateFactory method on " + cls.Name); object temp = method.Invoke(null, new object[]{}); if (temp is IWarFoundryFactory) { factory = (IWarFoundryFactory)temp; factories.Add(cls, factory); } } if (factory == null) { throw new ArgumentException("Could not create factory for class "+cls.FullName); } } return factory; } public Type DefaultType { get { return defaultType; } set { value = CheckType(value); defaultType = value; } } private Type CheckType(Type cls) { if (cls == null) { if (DefaultType!=null) { return DefaultType; } else { throw new InvalidOperationException("Class cannot be null when no default class is set"); } } else if (!typeof(IWarFoundryFactory).IsAssignableFrom(cls)) { throw new ArgumentException("Class "+cls.FullName+" was not a subtype of "+typeof(IWarFoundryFactory).FullName); } return cls; } } }