using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Reflection; using Darwinbots3.CommonInterface.Modules; using System.IO; namespace Darwinbots3 { public partial class BootScreen : Form { public BootScreen() { InitializeComponent(); } private void BootScreen_Shown(object sender, EventArgs e) { Application.DoEvents(); BootUp(); } private void BootUp() { Core.Module = new ModuleSelection(); // Nice new clean list of modules to assign // Step 1) Load in configuration XML string configPath = Path.Combine(Directory.GetCurrentDirectory(), "config.xml"); try { Core.Config = Configuration.LoadFromFile(configPath); } catch (IOException) // I guess it did't exist, who knew. { Core.Config = new Configuration(); // Lets just make a new one instead Core.Config.SaveAs(configPath); } // Step 2) Find out if we are the latest version // TODO: // Step 3) Download latest files if applicable // TODO: // Step 4) Load in all the assemblies in the modules folder LoadAssembliesFrom(Core.Config.ModuleFolderPath); // Step 5) Find, validate and index all the modules in these assembiles //FindModules(); // Use reflection to look for modules // Step 6) Create instances of the default/selected modules // TODO: // For now, we will load the first UI module we find. // Step 7) Pass main execution thread to the new UI instance // Step 8) Close this.Close(); } private void LoadAssembliesFrom(string folderPath) { string[] files = FileIOHelper.RecursiveFileSearch(folderPath, "*.dll"); foreach (string file in files) { try { Assembly assembly = Assembly.LoadFile(file); if (ValidAssembly(assembly)) FindModules(assembly); // Look for them now while we have a refrence to hand } catch { // Meh, I am sure we were not ment to be reading that one anyway. } } } private bool ValidAssembly(Assembly assembly) { return true; // yeah what-ever it's fine. TODO: } private void FindModules(Assembly assembly) { Type[] typesInAssembly = assembly.GetTypes(); foreach (Type type in typesInAssembly) { if (null != type.GetInterface(typeof(IModuleUserInterface).FullName)) // seems a bit hacky, TODO: clean-up { IModuleUserInterface ui = Activator.CreateInstance(type) as IModuleUserInterface; // first come first served for now, maybe later look at xml config prefrences or something if (Core.Module.UserInterface == null) Core.Module.UserInterface = ui; } if (null != type.GetInterface(typeof(IModuleGraphics).FullName)) { IModuleGraphics gfx = Activator.CreateInstance(type) as IModuleGraphics; Core.Module.Graphics = gfx; } // else if difrent module then do something difrent :) } } } }