Example #1
0
        public void LoadCritters()
        {
            StopWorld();
            // Get list of all dll files in specified folder. Iterate through them to find classes that implement ICritterFactory.
            string configDLLPath = Utility.GetConfiguration().BrainDLLPath.Trim();
            string dllPath       = (configDLLPath.Length > 0) ? configDLLPath : Path.GetDirectoryName(Application.ExecutablePath);

            Logger.OutputToLog("Loading critter brains from " + dllPath, Logger.LogLevel.Message);
            string[] dllFiles = System.IO.Directory.GetFiles(dllPath, "*.dll");
            foreach (string file in dllFiles)
            {
                try
                {
                    Assembly assembly = Assembly.LoadFrom(file);
                    foreach (Type type in assembly.GetTypes())
                    {
                        if (type.IsClass && type.GetInterface("ICritterFactory") != null)
                        {
                            // ICritterFactory is implemented. Get the brains and add them to the list.
                            try
                            {
                                ICritterFactory critterFactory = (ICritterFactory)Activator.CreateInstance(type);
                                foreach (CritterBrains.CritterBrain brain in critterFactory.GetCritterBrains())
                                {
                                    if (brain == null)
                                    {
                                        Logger.OutputToLog("Error in brain loading from " + file + ". Failed to load.  Brain is null.", Logger.LogLevel.Error);
                                        CritterWorldForm.AddMarqueeMessage(file + " crashed.");
                                    }
                                    else
                                    {
                                        waitingCritters.Add(new CritterWrapper(file, brain));
                                    }
                                }
                            }
                            catch (Exception e)
                            {
                                Logger.OutputToLog("Error in brain loading from " + file + ". Exception is " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error);
                                CritterWorldForm.AddMarqueeMessage(file + " couldn't load.");
                            }
                        }
                        else
                        {
                            Logger.OutputToLog("Skipped type " + type.Name + " in " + file + "; it is not a class or doesn't implement ICritterFactory.", Logger.LogLevel.Warning);
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.OutputToLog("Error loading file " + file + ". Exception is " + e.Message + "\n" + e.StackTrace, Logger.LogLevel.Error);
                    CritterWorldForm.AddMarqueeMessage(file + " couldn't load.");
                }
            }
            updateTimer.Start();
        }
 // default dependancies
 public AbstractFactory()
 {
     this.npcs = new NpcFactory();
     this.critters = new CritterFactory();
     this.characters = new AdventurerFactory();
 }
 public AbstractFactory(INpcFactory npcFactory, ICritterFactory critterFactory, ICharacterFactory characterFactory)
 {
     this.npcs = npcFactory;
     this.critters = critterFactory;
     this.characters = characterFactory;
 }
 // default dependancies
 public AbstractFactory()
 {
     this.npcs       = new NpcFactory();
     this.critters   = new CritterFactory();
     this.characters = new AdventurerFactory();
 }
 public AbstractFactory(INpcFactory npcFactory, ICritterFactory critterFactory, ICharacterFactory characterFactory)
 {
     this.npcs       = npcFactory;
     this.critters   = critterFactory;
     this.characters = characterFactory;
 }