break out cheat related config items to their own class, pass this config into CheatList

This commit is contained in:
adelikat 2020-06-06 12:26:31 -05:00
parent 6b22630ab1
commit 420253b6dd
8 changed files with 71 additions and 49 deletions

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BizHawk.Client.Common
{
public interface ICheatConfig
{
bool DisableOnLoad { get; }
bool AutoSaveOnClose { get; }
RecentFiles Recent { get; }
}
public class CheatConfig : ICheatConfig
{
public bool DisableOnLoad { get; set; }
public bool LoadFileByGame { get; set; } = true;
public bool AutoSaveOnClose { get; set; } = true;
public RecentFiles Recent { get; set; } = new RecentFiles(8);
}
}

View File

@ -259,11 +259,7 @@ namespace BizHawk.Client.Common
public Dictionary<string, ToolDialogSettings> CommonToolSettings { get; set; } = new Dictionary<string, ToolDialogSettings>();
public Dictionary<string, Dictionary<string, object>> CustomToolSettings { get; set; } = new Dictionary<string, Dictionary<string, object>>();
// Cheats
public bool DisableCheatsOnLoad { get; set; }
public bool LoadCheatFileByGame { get; set; } = true;
public bool CheatsAutoSaveOnClose { get; set; } = true;
public RecentFiles RecentCheats { get; set; } = new RecentFiles(8);
public CheatConfig Cheats { get; set; } = new CheatConfig();
// Macro Tool
public RecentFiles RecentMacros { get; set; } = new RecentFiles(8);

View File

@ -24,10 +24,16 @@ namespace BizHawk.Client.Common
private const string TypeColumn = "DisplayTypeColumn";
private const string ComparisonType = "ComparisonTypeColumn";
private readonly ICheatConfig _config;
private List<Cheat> _cheatList = new List<Cheat>();
private string _defaultFileName = "";
private bool _changes;
public CheatCollection(ICheatConfig config)
{
_config = config;
}
public delegate void CheatListEventHandler(object sender, CheatListEventArgs e);
public event CheatListEventHandler Changed;
@ -72,10 +78,10 @@ namespace BizHawk.Client.Common
/// <summary>
/// Looks for a .cht file that matches the ROM loaded based on the default filename for a given ROM
/// </summary>
public bool AttemptToLoadCheatFile(IMemoryDomains domains, bool disable)
public bool AttemptToLoadCheatFile(IMemoryDomains domains)
{
var file = new FileInfo(_defaultFileName);
return file.Exists && Load(domains, file.FullName, disable, false);
return file.Exists && Load(domains, file.FullName, false);
}
public void NewList(string defaultFileName, bool autosave = false)
@ -226,19 +232,22 @@ namespace BizHawk.Client.Common
public void SaveOnClose()
{
if (Changes && _cheatList.Any())
if (_config.AutoSaveOnClose)
{
if (string.IsNullOrWhiteSpace(CurrentFileName))
if (Changes && _cheatList.Any())
{
CurrentFileName = _defaultFileName;
}
if (string.IsNullOrWhiteSpace(CurrentFileName))
{
CurrentFileName = _defaultFileName;
}
SaveFile(CurrentFileName);
}
else if (!_cheatList.Any() && !string.IsNullOrWhiteSpace(CurrentFileName))
{
new FileInfo(CurrentFileName).Delete();
Global.Config.RecentCheats.Remove(CurrentFileName);
SaveFile(CurrentFileName);
}
else if (!_cheatList.Any() && !string.IsNullOrWhiteSpace(CurrentFileName))
{
new FileInfo(CurrentFileName).Delete();
_config.Recent.Remove(CurrentFileName);
}
}
}
@ -297,7 +306,7 @@ namespace BizHawk.Client.Common
File.WriteAllText(path, sb.ToString());
CurrentFileName = path;
Global.Config.RecentCheats.Add(CurrentFileName);
_config.Recent.Add(CurrentFileName);
Changes = false;
return true;
}
@ -307,7 +316,7 @@ namespace BizHawk.Client.Common
}
}
public bool Load(IMemoryDomains domains, string path, bool disable, bool append)
public bool Load(IMemoryDomains domains, string path, bool append)
{
var file = new FileInfo(path);
if (file.Exists == false)
@ -391,15 +400,16 @@ namespace BizHawk.Client.Common
bigEndian,
name);
Add(new Cheat(watch, value, compare, !disable && enabled, comparisonType));
Add(new Cheat(watch, value, compare, !_config.DisableOnLoad && enabled, comparisonType));
}
}
catch
{
continue;
// Ignore and continue
}
}
_config.Recent.Add(CurrentFileName);
Changes = false;
return true;
}

View File

@ -13,7 +13,7 @@ namespace BizHawk.Client.EmuHawk
public static IEmulator Emulator { get; set; }
public static CheatCollection CheatList { get; set; } = new CheatCollection();
public static CheatCollection CheatList { get; set; }
/// <summary>
/// the IGL to be used for rendering

View File

@ -2787,7 +2787,7 @@ namespace BizHawk.Client.EmuHawk
Tools.LoadRamWatch(!Config.DisplayRamWatch);
}
if (Config.RecentCheats.AutoLoad)
if (Config.Cheats.Recent.AutoLoad)
{
Tools.Load<Cheats>();
}

View File

@ -52,7 +52,7 @@ namespace BizHawk.Client.EmuHawk
private void LoadCheats(string filename, string archive = null)
{
CheatList.Load(Emulator.AsMemoryDomains(), filename, Config.DisableCheatsOnLoad, false);
CheatList.Load(Emulator.AsMemoryDomains(), filename, false);
Tools.Load<Cheats>();
}

View File

@ -296,7 +296,6 @@ namespace BizHawk.Client.EmuHawk
Emulator = new NullEmulator();
GlobalWin.Tools = new ToolManager(this, Config, Emulator, MovieSession);
CheatList.Changed += Tools.UpdateCheatRelatedTools;
UpdateStatusSlots();
UpdateKeyPriorityIcon();
@ -414,6 +413,8 @@ namespace BizHawk.Client.EmuHawk
Sound.StartSound();
InputManager.SyncControls(Emulator, Config);
GlobalWin.CheatList = new CheatCollection(Config.Cheats);
CheatList.Changed += Tools.UpdateCheatRelatedTools;
RewireSound();
// Workaround for windows, location is -32000 when minimized, if they close it during this time, that's what gets saved
@ -3799,10 +3800,10 @@ namespace BizHawk.Client.EmuHawk
Tools.Restart(Emulator);
if (Config.LoadCheatFileByGame && Emulator.HasMemoryDomains())
if (Config.Cheats.LoadFileByGame && Emulator.HasMemoryDomains())
{
CheatList.SetDefaultFileName(Tools.GenerateDefaultCheatFilename());
if (CheatList.AttemptToLoadCheatFile(Emulator.AsMemoryDomains(), Config.DisableCheatsOnLoad))
if (CheatList.AttemptToLoadCheatFile(Emulator.AsMemoryDomains()))
{
AddOnScreenMessage("Cheats file loaded");
}
@ -3942,11 +3943,7 @@ namespace BizHawk.Client.EmuHawk
Tools.Get<TraceLogger>().Restart();
}
if (Config.CheatsAutoSaveOnClose)
{
CheatList.SaveOnClose();
}
CheatList.SaveOnClose();
Emulator.Dispose();
Emulator = new NullEmulator();
ClientApi.UpdateEmulatorAndVP(Emulator);

View File

@ -73,14 +73,13 @@ namespace BizHawk.Client.EmuHawk
var askResult = !MainForm.CheatList.Changes || AskSaveChanges();
if (askResult)
{
var loadResult = MainForm.CheatList.Load(Core, path, Config.DisableCheatsOnLoad, append: false);
var loadResult = MainForm.CheatList.Load(Core, path, append: false);
if (!loadResult)
{
Config.RecentCheats.HandleLoadError(path);
Config.Cheats.Recent.HandleLoadError(path);
}
else
{
Config.RecentCheats.Add(path);
GeneralUpdate();
UpdateMessageLabel();
}
@ -108,10 +107,9 @@ namespace BizHawk.Client.EmuHawk
if (result)
{
MainForm.CheatList.Load(Core, file.FullName, Config.DisableCheatsOnLoad, append);
MainForm.CheatList.Load(Core, file.FullName, append);
GeneralUpdate();
UpdateMessageLabel();
Config.RecentCheats.Add(MainForm.CheatList.CurrentFileName);
}
}
}
@ -340,7 +338,7 @@ namespace BizHawk.Client.EmuHawk
private void RecentSubMenu_DropDownOpened(object sender, EventArgs e)
{
RecentSubMenu.DropDownItems.Clear();
RecentSubMenu.DropDownItems.AddRange(Config.RecentCheats.RecentMenu(LoadFileFromRecent, "Cheats", noAutoload: true));
RecentSubMenu.DropDownItems.AddRange(Config.Cheats.Recent.RecentMenu(LoadFileFromRecent, "Cheats", noAutoload: true));
}
private void NewMenuItem_Click(object sender, EventArgs e)
@ -514,10 +512,10 @@ namespace BizHawk.Client.EmuHawk
private void OptionsSubMenu_DropDownOpened(object sender, EventArgs e)
{
AlwaysLoadCheatsMenuItem.Checked = Config.LoadCheatFileByGame;
AutoSaveCheatsMenuItem.Checked = Config.CheatsAutoSaveOnClose;
DisableCheatsOnLoadMenuItem.Checked = Config.DisableCheatsOnLoad;
AutoloadMenuItem.Checked = Config.RecentCheats.AutoLoad;
AlwaysLoadCheatsMenuItem.Checked = Config.Cheats.LoadFileByGame;
AutoSaveCheatsMenuItem.Checked = Config.Cheats.AutoSaveOnClose;
DisableCheatsOnLoadMenuItem.Checked = Config.Cheats.DisableOnLoad;
AutoloadMenuItem.Checked = Config.Cheats.Recent.AutoLoad;
SaveWindowPositionMenuItem.Checked = Settings.SaveWindowPosition;
AlwaysOnTopMenuItem.Checked = Settings.TopMost;
FloatingWindowMenuItem.Checked = Settings.FloatingWindow;
@ -525,22 +523,22 @@ namespace BizHawk.Client.EmuHawk
private void AlwaysLoadCheatsMenuItem_Click(object sender, EventArgs e)
{
Config.LoadCheatFileByGame ^= true;
Config.Cheats.LoadFileByGame ^= true;
}
private void AutoSaveCheatsMenuItem_Click(object sender, EventArgs e)
{
Config.CheatsAutoSaveOnClose ^= true;
Config.Cheats.AutoSaveOnClose ^= true;
}
private void CheatsOnOffLoadMenuItem_Click(object sender, EventArgs e)
{
Config.DisableCheatsOnLoad ^= true;
Config.Cheats.DisableOnLoad ^= true;
}
private void AutoloadMenuItem_Click(object sender, EventArgs e)
{
Config.RecentCheats.AutoLoad ^= true;
Config.Cheats.Recent.AutoLoad ^= true;
}
private void SaveWindowPositionMenuItem_Click(object sender, EventArgs e)
@ -571,9 +569,9 @@ namespace BizHawk.Client.EmuHawk
CheatsMenu.Items.Add(CheatListView.ToColumnsMenu(ColumnToggleCallback));
Config.DisableCheatsOnLoad = false;
Config.LoadCheatFileByGame = true;
Config.CheatsAutoSaveOnClose = true;
Config.Cheats.DisableOnLoad = false;
Config.Cheats.LoadFileByGame = true;
Config.Cheats.AutoSaveOnClose = true;
RefreshFloatingWindowControl(Settings.FloatingWindow);
CheatListView.AllColumns.Clear();