Experimental generic struct stuff, mostly.
[VOID.git] / VOID_Core.cs
blob:a/VOID_Core.cs -> blob:b/VOID_Core.cs
--- a/VOID_Core.cs
+++ b/VOID_Core.cs
@@ -21,6 +21,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using KSP;
 using UnityEngine;
 
@@ -31,6 +32,15 @@
 		/*
 		 * Static Members
 		 * */
+		protected static bool _initialized = false;
+		public static bool Initialized
+		{
+			get 
+			{
+			return _initialized;
+			}
+		}
+
 		protected static VOID_Core _instance;
 		public static VOID_Core Instance
 		{
@@ -39,6 +49,7 @@
 				if (_instance == null)
 				{
 					_instance = new VOID_Core();
+					_initialized = true;
 				}
 				return _instance;
 			}
@@ -50,25 +61,28 @@
 		protected string VoidName = "VOID";
 		protected string VoidVersion = "0.9.9";
 
-		[AVOID_ConfigValue("configValue")]
-		protected VOID_ConfigValue<int> configVersion = 1;
-
-		protected List<VOID_Module> _modules = new List<VOID_Module>();
-
-		[AVOID_ConfigValue("mainWindowPos")]
-		protected VOID_ConfigValue<Rect> mainWindowPos = new Rect(Screen.width / 2, Screen.height / 2, 10f, 10f);
-
-		[AVOID_ConfigValue("mainGuiMinimized")]
-		protected VOID_ConfigValue<bool> mainGuiMinimized = false;
-
-		[AVOID_ConfigValue("configWindowPos")]
-		protected VOID_ConfigValue<Rect> configWindowPos = new Rect(Screen.width / 2, Screen.height  /2, 10f, 10f);
-
-		[AVOID_ConfigValue("configWindowMinimized")]
-		protected VOID_ConfigValue<bool> configWindowMinimized = true;
-
-		[AVOID_ConfigValue("VOIDIconPos")]
-		protected VOID_ConfigValue<Rect> VOIDIconPos = new Rect(Screen.width / 2 - 200, Screen.height - 30, 30f, 30f);
+		[AVOID_SaveValue("configValue")]
+		protected VOID_SaveValue<int> configVersion = 1;
+
+		protected List<IVOID_Module> _modules = new List<IVOID_Module>();
+		protected bool _modulesLoaded = false;
+
+		protected List<Callback> _configurableCallbacks = new List<Callback>();
+
+		[AVOID_SaveValue("mainWindowPos")]
+		protected VOID_SaveValue<Rect> mainWindowPos = new Rect(Screen.width / 2, Screen.height / 2, 10f, 10f);
+
+		[AVOID_SaveValue("mainGuiMinimized")]
+		protected VOID_SaveValue<bool> mainGuiMinimized = false;
+
+		[AVOID_SaveValue("configWindowPos")]
+		protected VOID_SaveValue<Rect> configWindowPos = new Rect(Screen.width / 2, Screen.height  /2, 10f, 10f);
+
+		[AVOID_SaveValue("configWindowMinimized")]
+		protected VOID_SaveValue<bool> configWindowMinimized = true;
+
+		[AVOID_SaveValue("VOIDIconPos")]
+		protected VOID_SaveValue<Rect> VOIDIconPos = new Rect(Screen.width / 2 - 200, Screen.height - 30, 30f, 30f);
 		protected Texture2D VOIDIconOff = new Texture2D(30, 30, TextureFormat.ARGB32, false);
 		protected Texture2D VOIDIconOn = new Texture2D(30, 30, TextureFormat.ARGB32, false);
 		protected Texture2D VOIDIconTexture;
@@ -77,23 +91,31 @@
 
 		protected int windowBaseID = -96518722;
 
-		public VOID_ConfigValue<bool> togglePower = true;
+		[AVOID_SaveValue("togglePower")]
+		public VOID_SaveValue<bool> togglePower = true;
+
 		public bool powerAvailable = true;
-		protected VOID_ConfigValue<bool> consumeResource = false;
-		protected VOID_ConfigValue<string> resourceName = "ElectricCharge";
-		protected VOID_ConfigValue<float> resourceRate = 0.2f;
+
+		[AVOID_SaveValue("consumeResource")]
+		protected VOID_SaveValue<bool> consumeResource = false;
+
+		[AVOID_SaveValue("resourceName")]
+		protected VOID_SaveValue<string> resourceName = "ElectricCharge";
+
+		[AVOID_SaveValue("resourceRate")]
+		protected VOID_SaveValue<float> resourceRate = 0.2f;
 
 		public float saveTimer = 0;
 
 		protected string defaultSkin = "KSP window 2";
-		protected VOID_ConfigValue<string> _skin;
+		protected VOID_SaveValue<string> _skin;
 
 		public bool configDirty;
 
 		/*
 		 * Properties
 		 * */
-		public List<VOID_Module> Modules
+		public List<IVOID_Module> Modules
 		{
 			get
 			{
@@ -130,11 +152,62 @@
 
 			this.VOIDIconOn = GameDatabase.Instance.GetTexture (this.VOIDIconOnPath, false);
 			this.VOIDIconOff = GameDatabase.Instance.GetTexture (this.VOIDIconOffPath, false);
-		}
-
-		public void LoadModule(Type T)
-		{
-			this._modules.Add (Activator.CreateInstance (T) as VOID_Module);
+
+			this.LoadConfig ();
+		}
+
+		protected void LoadModules()
+		{
+			var types = AssemblyLoader.loadedAssemblies
+				.Select (a => a.assembly.GetExportedTypes ())
+					.SelectMany (t => t)
+					.Where (v => typeof(IVOID_Module).IsAssignableFrom (v)
+					        && !(v.IsInterface || v.IsAbstract) &&
+					        !typeof(VOID_Core).IsAssignableFrom (v)
+					        );
+
+			Tools.PostDebugMessage (string.Format (
+				"{0}: Found {1} modules to check.",
+				this.GetType ().Name,
+				types.Count ()
+				));
+			foreach (var voidType in types)
+			{
+				Tools.PostDebugMessage (string.Format (
+					"{0}: found Type {1}",
+					this.GetType ().Name,
+					voidType.Name
+					));
+
+				this.LoadModule(voidType);
+			}
+
+			this._modulesLoaded = true;
+
+			Tools.PostDebugMessage (string.Format ("VOID_Core: Loaded {0} modules.", this.Modules.Count));
+		}
+
+		protected void LoadModule(Type T)
+		{
+			var existingModules = this._modules.Where (mod => mod.GetType ().Name == T.Name);
+			if (existingModules.Any())
+			{
+				Tools.PostDebugMessage(string.Format(
+					"{0}: refusing to load {1}: already loaded",
+					this.GetType().Name,
+					T.Name
+				));
+				return;
+			}
+			IVOID_Module module = Activator.CreateInstance (T) as IVOID_Module;
+			module.LoadConfig();
+			this._modules.Add (module);
+
+			Tools.PostDebugMessage(string.Format(
+				"{0}: loaded module {1}.",
+				this.GetType().Name,
+				T.Name
+			));
 		}
 
 		public void Update()
@@ -146,13 +219,18 @@
 				this.StartGUI ();
 			}
 
-			foreach (VOID_Module module in this.Modules)
+			if (!HighLogic.LoadedSceneIsFlight && this.guiRunning)
+			{
+				this.StopGUI ();
+			}
+
+			foreach (IVOID_Module module in this.Modules)
 			{
 				if (!module.guiRunning && module.toggleActive)
 				{
 					module.StartGUI ();
 				}
-				if (module.guiRunning && !module.toggleActive || !this.togglePower)
+				if (module.guiRunning && !module.toggleActive || !this.togglePower || !HighLogic.LoadedSceneIsFlight)
 				{
 					module.StopGUI();
 				}
@@ -196,7 +274,7 @@
 				if (GUILayout.Button("Power " + str)) togglePower = !togglePower;
 			    if (togglePower)
 			    {
-					foreach (VOID_Module module in this.Modules)
+					foreach (IVOID_Module module in this.Modules)
 					{
 						module.toggleActive = GUILayout.Toggle (module.toggleActive, module.Name);
 					}
@@ -220,19 +298,34 @@
 		{
 			GUILayout.BeginVertical ();
 
-			this.consumeResource = GUILayout.Toggle (this.consumeResource, "Consume Resources");
+			this.DrawConfigurables ();
 
 			GUILayout.EndVertical ();
 			GUI.DragWindow ();
 		}
 
+		public override void DrawConfigurables()
+		{
+			this.consumeResource = GUILayout.Toggle (this.consumeResource, "Consume Resources");
+
+			foreach (IVOID_Module mod in this.Modules)
+			{
+				mod.DrawConfigurables ();
+			}
+		}
+
 		public override void DrawGUI()
 		{
+			if (!this._modulesLoaded)
+			{
+				this.LoadModules ();
+			}
+
 			GUI.skin = this.Skin;
 
 			int windowID = this.windowBaseID;
 
-            this.VOIDIconTexture = this.VOIDIconOff;  //icon off default
+			this.VOIDIconTexture = this.VOIDIconOff;  //icon off default
 			if (this.togglePower) this.VOIDIconTexture = this.VOIDIconOn;     //or on if power_toggle==true
 			if (GUI.Button(new Rect(VOIDIconPos), VOIDIconTexture, new GUIStyle()))
 			{
@@ -241,81 +334,48 @@
 
 			if (!this.mainGuiMinimized)
 			{
-				this.mainWindowPos = GUILayout.Window (
+				Rect _mainWindowPos = this.mainWindowPos;
+
+				_mainWindowPos = GUILayout.Window (
 					++windowID,
-					this.mainWindowPos,
+					_mainWindowPos,
 					this.VOIDMainWindow,
-					string.Join (" ", this.VoidName, this.VoidVersion),
+					string.Join (" ", new string[] {this.VoidName, this.VoidVersion}),
 					GUILayout.Width (250),
 					GUILayout.Height (50)
 				);
+
+				if (_mainWindowPos != this.mainWindowPos)
+				{
+					this.mainWindowPos = _mainWindowPos;
+				}
 			}
 
 			if (!this.configWindowMinimized)
 			{
-				this.configWindowPos = GUILayout.Window (
+				Rect _configWindowPos = this.configWindowPos;
+
+				_configWindowPos = GUILayout.Window (
 					++windowID,
-					this.configWindowPos,
+					_configWindowPos,
 					this.VOIDConfigWindow,
-					string.Join (" ", this.VoidName, "Configuration"),
+					string.Join (" ", new string[] {this.VoidName, "Configuration"}),
 					GUILayout.Width (250),
 					GUILayout.Height (50)
 				);
-			}
-		}
-
-		public new void StartGUI()
-		{
-			base.StartGUI ();
-			foreach (var module in this._modules)
-			{
-				if (module.toggleActive)
-				{
-					module.StartGUI ();
-				}
-			}
-
-			this._Running = true;
-		}
-
-		public new void StopGUI()
-		{
-			base.StopGUI ();
-			foreach (var module in this._modules)
-			{
-				if (module.guiRunning)
-				{
-					module.StopGUI ();
-				}
-			}
-
-			this._Running = false;
+
+				if (_configWindowPos != this.configWindowPos)
+				{
+					this.configWindowPos = _configWindowPos;
+				}
+			}
 		}
 
 		public override void LoadConfig()
 		{
-			var config = KSP.IO.PluginConfiguration.CreateForType<VOID_Core>();
-			config.load();
-
-			if (this.configVersion > config.GetValue("configVersion", 0))
-			{
-				// TODO: Config update stuff.
-			}
-
-			foreach (var field in this.GetType().GetFields().Where(f => f.IsDefined(typeof(AVOID_ConfigValue), false)))
-			{
-				var attr = field.GetCustomAttributes(typeof(AVOID_ConfigValue), false)[0];
-				string fieldName = (attr as AVOID_ConfigValue).Name;
-
-				var fieldValue = field.GetValue(this);
-
-				field.SetValue(
-					this,
-					config.GetValue(fieldName, fieldValue)
-				);
-			}
-
-			foreach (VOID_Module module in this.Modules)
+			base.LoadConfig ();
+
+			foreach (IVOID_Module module in this.Modules)
 			{
 				module.LoadConfig ();
 			}
@@ -323,23 +383,14 @@
 
 		public override void SaveConfig()
 		{
-			var config = KSP.IO.PluginConfiguration.CreateForType<VOID_Core> ();
-			config.load ();
-
-			foreach (var field in this.GetType().GetFields().Where(f => f.IsDefined(typeof(AVOID_ConfigValue), false)))
-			{
-				var attr = field.GetCustomAttributes(typeof(AVOID_ConfigValue), false)[0];
-				string fieldName = (attr as AVOID_ConfigValue).Name;
-
-				object fieldValue = field.GetValue(this);
-				Type T = (fieldValue as IVOID_ConfigValue).type;
-
-				config.SetValue(fieldName, fieldValue);
-			}
-
-			config.save ();
-
-			foreach (VOID_Module module in this.Modules)
+			if (!this.configDirty)
+			{
+				return;
+			}
+
+			base.SaveConfig ();
+
+			foreach (IVOID_Module module in this.Modules)
 			{
 				module.SaveConfig ();
 			}