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) { if (data.TryGetValue(prop.Name, out var val)) { if (val is string str && prop.PropertyType != typeof(string)) { // if a type has a TypeConverter, and that converter can convert to string, // that will be used in place of object markup by JSON.NET // but that doesn't work with $type metadata, and JSON.NET fails to fall // back on regular object serialization when needed. so try to undo a TypeConverter // operation here var converter = TypeDescriptor.GetConverter(prop.PropertyType); val = converter.ConvertFromString(null, CultureInfo.InvariantCulture, str); } else if (!(val is bool) && prop.PropertyType.IsPrimitive) { // numeric constants are similarly hosed val = Convert.ChangeType(val, prop.PropertyType, CultureInfo.InvariantCulture); } prop.SetValue(tool, val, null); } }
public T CustomLoad <T>(bool focus = true) where T : class, IToolForm { IToolForm newTool = CreateInstance <T>(""); if (newTool == null) { return(null); } if (newTool is Form) { (newTool as Form).Owner = GlobalWin.MainForm; } ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool); string toolType = typeof(T).ToString(); // auto settings if (newTool is IToolFormAutoConfig) { ToolDialogSettings settings; if (!Global.Config.CommonToolSettings.TryGetValue(toolType, out settings)) { settings = new ToolDialogSettings(); Global.Config.CommonToolSettings[toolType] = settings; } AttachSettingHooks(newTool as IToolFormAutoConfig, settings); } return((T)newTool); }
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, BindingFlags.GetProperty, Type.DefaultBinder, null, System.Globalization.CultureInfo.InvariantCulture)); } }
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)); } }
// If the form inherits ToolFormBase, it will set base properties such as Tools, Config, etc private void SetBaseProperties(IToolForm form) { if (form is ToolFormBase tool) { tool.Tools = this; tool.Config = _config; tool.MainForm = _owner; } }
// If the form inherits ToolFormBase, it will set base properties such as Tools, Config, etc private void SetBaseProperties(IToolForm form) { if (form is ToolFormBase tool) { tool.Tools = this; tool.Config = _config; tool.InputManager = _inputManager; tool.MainForm = _owner; tool.MovieSession = _movieSession; tool.Game = _game; } }
/// <summary> /// Loads the tool dialog T (T must implement <see cref="IToolForm"/>) , if it does not exist it will be created, if it is already open, it will be focused /// </summary> /// <typeparam name="T">Type of tool you want to load</typeparam> /// <param name="toolPath">Path to the .dll of the external tool</param> /// <param name="focus">Define if the tool form has to get the focus or not (Default is true)</param> /// <returns>An instantiated <see cref="IToolForm"/></returns> public T Load <T>(string toolPath, bool focus = true) where T : class, IToolForm { bool isExternal = typeof(T) == typeof(IExternalToolForm); if (!IsAvailable <T>() && !isExternal) { return(null); } T existingTool; if (isExternal) { existingTool = (T)_tools.FirstOrDefault(t => t is T && t.GetType().Assembly.Location == toolPath); } else { existingTool = (T)_tools.FirstOrDefault(t => t is T); } if (existingTool != null) { if (existingTool.IsDisposed) { _tools.Remove(existingTool); } else { if (focus) { existingTool.Show(); existingTool.Focus(); } return(existingTool); } } IToolForm newTool = CreateInstance <T>(toolPath); if (newTool == null) { return(null); } if (newTool is Form) { (newTool as Form).Owner = GlobalWin.MainForm; } if (isExternal) { ApiInjector.UpdateApis(GlobalWin.ApiProvider, newTool); } ServiceInjector.UpdateServices(Global.Emulator.ServiceProvider, newTool); string toolType = typeof(T).ToString(); // auto settings if (newTool is IToolFormAutoConfig) { ToolDialogSettings settings; if (!Global.Config.CommonToolSettings.TryGetValue(toolType, out settings)) { settings = new ToolDialogSettings(); Global.Config.CommonToolSettings[toolType] = settings; } AttachSettingHooks(newTool as IToolFormAutoConfig, settings); } // custom settings if (HasCustomConfig(newTool)) { if (!Global.Config.CustomToolSettings.TryGetValue(toolType, out var settings)) { settings = new Dictionary <string, object>(); Global.Config.CustomToolSettings[toolType] = settings; } InstallCustomConfig(newTool, settings); } newTool.Restart(); if (!OSTailoredCode.IsWindows() && newTool is RamSearch) { // the mono winforms implementation is buggy, skip to the return statement and call Show in MainForm instead } else { newTool.Show(); } return((T)newTool); }
private static bool HasCustomConfig(IToolForm tool) { return(tool.GetType().GetPropertiesWithAttrib(typeof(ConfigPersistAttribute)).Any()); }