From ff8922bb08d35915a98648b8c2679d7d9db43fe3 Mon Sep 17 00:00:00 2001 From: goyuken Date: Sat, 20 Dec 2014 21:49:53 +0000 Subject: [PATCH] itoolform custom config infrastructure; not complete --- BizHawk.Client.Common/config/Config.cs | 4 +- BizHawk.Client.EmuHawk/tools/GB/GBGPUView.cs | 17 ++--- BizHawk.Client.EmuHawk/tools/IToolForm.cs | 5 ++ BizHawk.Client.EmuHawk/tools/ToolManager.cs | 67 +++++++++++++++++-- .../Extensions/ReflectionExtensions.cs | 8 +++ BizHawk.Emulation.Common/ServiceInjector.cs | 15 ++--- 6 files changed, 86 insertions(+), 30 deletions(-) diff --git a/BizHawk.Client.Common/config/Config.cs b/BizHawk.Client.Common/config/Config.cs index f63ae22945..2c96818ccd 100644 --- a/BizHawk.Client.Common/config/Config.cs +++ b/BizHawk.Client.Common/config/Config.cs @@ -412,6 +412,7 @@ namespace BizHawk.Client.Common #endregion public Dictionary CommonToolSettings = new Dictionary(); + public Dictionary> CustomToolSettings = new Dictionary>(); // SMS VDP Viewer Settings public ToolDialogSettings SmsVdpSettings = new ToolDialogSettings(); @@ -432,9 +433,6 @@ namespace BizHawk.Client.Common public bool AutoLoadNESNameTable = false; public int NESNameTableRefreshRate = 4; - // gb gpu view settings - public Color GBGPUSpriteBack = Color.Lime; - // SNES Graphics Debugger Dialog Settings public bool AutoLoadSNESGraphicsDebugger = false; public bool SNESGraphicsDebuggerSaveWindowPosition = true; diff --git a/BizHawk.Client.EmuHawk/tools/GB/GBGPUView.cs b/BizHawk.Client.EmuHawk/tools/GB/GBGPUView.cs index f5a0291e39..33fa588a42 100644 --- a/BizHawk.Client.EmuHawk/tools/GB/GBGPUView.cs +++ b/BizHawk.Client.EmuHawk/tools/GB/GBGPUView.cs @@ -45,12 +45,13 @@ namespace BizHawk.Client.EmuHawk private Color _spriteback; - Color spriteback + [ConfigPersist] + public Color Spriteback { get { return _spriteback; } set { - _spriteback = value; + _spriteback = Color.FromArgb(255, value); // force fully opaque panelSpriteBackColor.BackColor = _spriteback; labelSpriteBackColor.Text = string.Format("({0},{1},{2})", _spriteback.R, _spriteback.G, _spriteback.B); } @@ -80,8 +81,7 @@ namespace BizHawk.Client.EmuHawk _messagetimer.Interval = 5000; _messagetimer.Tick += messagetimer_Tick; - - spriteback = Color.FromArgb(255, Global.Config.GBGPUSpriteBack); + Spriteback = Color.Lime; // will be overrided from config after construct } public void Restart() @@ -369,7 +369,7 @@ namespace BizHawk.Client.EmuHawk p = (int*)_sppal; for (int i = 0; i < 32; i++) p[i] |= unchecked((int)0xff000000); - int c = spriteback.ToArgb(); + int c = Spriteback.ToArgb(); for (int i = 0; i < 32; i += 4) p[i] = c; } @@ -475,8 +475,6 @@ namespace BizHawk.Client.EmuHawk { Gb.SetScanlineCallback(null, 0); } - - Global.Config.GBGPUSpriteBack = spriteback; } private void GBGPUView_Load(object sender, EventArgs e) @@ -938,13 +936,12 @@ namespace BizHawk.Client.EmuHawk dlg.AllowFullOpen = true; dlg.AnyColor = true; dlg.FullOpen = true; - dlg.Color = spriteback; + dlg.Color = Spriteback; var result = dlg.ShowHawkDialog(); if (result == DialogResult.OK) { - // force full opaque - spriteback = Color.FromArgb(255, dlg.Color); + Spriteback = dlg.Color; } } } diff --git a/BizHawk.Client.EmuHawk/tools/IToolForm.cs b/BizHawk.Client.EmuHawk/tools/IToolForm.cs index 3bb17e78f9..e743ca98df 100644 --- a/BizHawk.Client.EmuHawk/tools/IToolForm.cs +++ b/BizHawk.Client.EmuHawk/tools/IToolForm.cs @@ -53,4 +53,9 @@ namespace BizHawk.Client.EmuHawk public interface IToolFormAutoConfig : IToolForm { } + + [AttributeUsage(AttributeTargets.Property)] + public class ConfigPersistAttribute : Attribute + { + } } diff --git a/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/BizHawk.Client.EmuHawk/tools/ToolManager.cs index 29bf779931..7a7321ca92 100644 --- a/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -7,6 +7,7 @@ using System.Reflection; using BizHawk.Emulation.Common.IEmulatorExtensions; using BizHawk.Client.Common; using BizHawk.Emulation.Common; +using BizHawk.Common.ReflectionExtensions; using System.Windows.Forms; @@ -62,15 +63,29 @@ namespace BizHawk.Client.EmuHawk ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool); - ToolDialogSettings settings; - if (!Global.Config.CommonToolSettings.TryGetValue(toolType.ToString(), out settings)) + // auto settings + if (newTool is IToolFormAutoConfig) { - settings = new ToolDialogSettings(); - Global.Config.CommonToolSettings[toolType.ToString()] = settings; + ToolDialogSettings settings; + if (!Global.Config.CommonToolSettings.TryGetValue(toolType.ToString(), out settings)) + { + settings = new ToolDialogSettings(); + Global.Config.CommonToolSettings[toolType.ToString()] = settings; + } + AttachSettingHooks(newTool as IToolFormAutoConfig, settings); } - if (newTool is IToolFormAutoConfig) - AttachSettingHooks(newTool as IToolFormAutoConfig, settings); + // custom settings + if (HasCustomConfig(newTool)) + { + Dictionary settings; + if (!Global.Config.CustomToolSettings.TryGetValue(toolType.ToString(), out settings)) + { + settings = new Dictionary(); + Global.Config.CustomToolSettings[toolType.ToString()] = settings; + } + InstallCustomConfig(newTool, settings); + } newTool.Restart(); newTool.Show(); @@ -183,9 +198,47 @@ namespace BizHawk.Client.EmuHawk settings.AutoLoad = val; (o as ToolStripMenuItem).Checked = val; }; - } + private static bool HasCustomConfig(IToolForm tool) + { + return tool.GetType().GetPropertiesWithAttrib(typeof(ConfigPersistAttribute)).Any(); + } + + private static void InstallCustomConfig(IToolForm tool, Dictionary data) + { + Type type = tool.GetType(); + var props = type.GetPropertiesWithAttrib(typeof(ConfigPersistAttribute)).ToList(); + if (props.Count == 0) + return; + + foreach (var prop in props) + { + object val; + if (data.TryGetValue(prop.Name, out val)) + { + try + { + prop.SetValue(tool, val, null); + } + catch + { + // FIXME + } + } + } + + (tool as Form).FormClosing += (o, e) => SaveCustomConfig(tool, data, props); + } + + private static void SaveCustomConfig(IToolForm tool, Dictionary data, List props) + { + data.Clear(); + foreach (var prop in props) + { + data.Add(prop.Name, prop.GetValue(tool, null)); + } + } /// diff --git a/BizHawk.Common/Extensions/ReflectionExtensions.cs b/BizHawk.Common/Extensions/ReflectionExtensions.cs index 5b1179cf0c..b94eb0e475 100644 --- a/BizHawk.Common/Extensions/ReflectionExtensions.cs +++ b/BizHawk.Common/Extensions/ReflectionExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Collections.Generic; using System.ComponentModel; using System.IO; @@ -13,6 +14,13 @@ namespace BizHawk.Common.ReflectionExtensions /// public static class ReflectionExtensions { + public static IEnumerable GetPropertiesWithAttrib(this Type type, Type attributeType) + { + return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic) + .Where(p => p.GetCustomAttributes(attributeType, false).Length > 0); + } + + /// /// Gets the description attribute from an object /// diff --git a/BizHawk.Emulation.Common/ServiceInjector.cs b/BizHawk.Emulation.Common/ServiceInjector.cs index 27bff7c8e2..89dede66cd 100644 --- a/BizHawk.Emulation.Common/ServiceInjector.cs +++ b/BizHawk.Emulation.Common/ServiceInjector.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; +using BizHawk.Common.ReflectionExtensions; namespace BizHawk.Emulation.Common { @@ -11,22 +12,16 @@ namespace BizHawk.Emulation.Common /// public static class ServiceInjector { - private static IEnumerable GetPropertiesWithAttr(Type type, Type attributeType) - { - return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic) - .Where(p => p.GetCustomAttributes(attributeType, false).Length > 0); - } - /// /// Feeds the target its required services. /// /// false if update failed public static bool UpdateServices(IEmulatorServiceProvider source, object target) { - Type toolType = target.GetType(); + Type targetType = target.GetType(); object[] tmp = new object[1]; - foreach (var propinfo in GetPropertiesWithAttr(toolType, typeof(RequiredService))) + foreach (var propinfo in targetType.GetPropertiesWithAttrib(typeof(RequiredService))) { tmp[0] = source.GetService(propinfo.PropertyType); if (tmp[0] == null) @@ -34,7 +29,7 @@ namespace BizHawk.Emulation.Common propinfo.GetSetMethod(true).Invoke(target, tmp); } - foreach (var propinfo in GetPropertiesWithAttr(toolType, typeof(OptionalService))) + foreach (var propinfo in targetType.GetPropertiesWithAttrib(typeof(OptionalService))) { tmp[0] = source.GetService(propinfo.PropertyType); propinfo.GetSetMethod(true).Invoke(target, tmp); @@ -48,7 +43,7 @@ namespace BizHawk.Emulation.Common /// public static bool IsAvailable(IEmulatorServiceProvider source, Type targetType) { - return GetPropertiesWithAttr(targetType, typeof(RequiredService)) + return targetType.GetPropertiesWithAttrib(typeof(RequiredService)) .Select(pi => pi.PropertyType) .All(t => source.HasService(t)); }