itoolform custom config infrastructure; not complete

This commit is contained in:
goyuken 2014-12-20 21:49:53 +00:00
parent 1d0b623d88
commit ff8922bb08
6 changed files with 86 additions and 30 deletions

View File

@ -412,6 +412,7 @@ namespace BizHawk.Client.Common
#endregion
public Dictionary<string, ToolDialogSettings> CommonToolSettings = new Dictionary<string, ToolDialogSettings>();
public Dictionary<string, Dictionary<string, object>> CustomToolSettings = new Dictionary<string, Dictionary<string, object>>();
// 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;

View File

@ -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;
}
}
}

View File

@ -53,4 +53,9 @@ namespace BizHawk.Client.EmuHawk
public interface IToolFormAutoConfig : IToolForm
{
}
[AttributeUsage(AttributeTargets.Property)]
public class ConfigPersistAttribute : Attribute
{
}
}

View File

@ -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<string, object> settings;
if (!Global.Config.CustomToolSettings.TryGetValue(toolType.ToString(), out settings))
{
settings = new Dictionary<string, object>();
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<string, object> 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<string, object> data, List<PropertyInfo> props)
{
data.Clear();
foreach (var prop in props)
{
data.Add(prop.Name, prop.GetValue(tool, null));
}
}
/// <summary>

View File

@ -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
/// </summary>
public static class ReflectionExtensions
{
public static IEnumerable<PropertyInfo> GetPropertiesWithAttrib(this Type type, Type attributeType)
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
.Where(p => p.GetCustomAttributes(attributeType, false).Length > 0);
}
/// <summary>
/// Gets the description attribute from an object
/// </summary>

View File

@ -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
/// </summary>
public static class ServiceInjector
{
private static IEnumerable<PropertyInfo> GetPropertiesWithAttr(Type type, Type attributeType)
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic)
.Where(p => p.GetCustomAttributes(attributeType, false).Length > 0);
}
/// <summary>
/// Feeds the target its required services.
/// </summary>
/// <returns>false if update failed</returns>
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
/// </summary>
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));
}