From 206a3e30c86f26cd276eb7c60d0b505e80a9a1c5 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Tue, 29 Dec 2020 16:26:01 +1000 Subject: [PATCH] Cleanup instantiation of FirmwaresManager and PathConfig and remove unused MainForm interface prop --- .../IMainFormForConfig.cs | 3 - src/BizHawk.Client.EmuHawk/MainForm.Events.cs | 6 +- .../config/FirmwaresConfig.cs | 58 ++++++++++++------- .../config/PathConfig.cs | 45 +++++++++----- 4 files changed, 68 insertions(+), 44 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/IMainFormForConfig.cs b/src/BizHawk.Client.EmuHawk/IMainFormForConfig.cs index b4a4bd2d13..71adebd41a 100644 --- a/src/BizHawk.Client.EmuHawk/IMainFormForConfig.cs +++ b/src/BizHawk.Client.EmuHawk/IMainFormForConfig.cs @@ -8,9 +8,6 @@ namespace BizHawk.Client.EmuHawk /// only referenced from IEmulator Emulator { get; } - /// only referenced from - FirmwareManager FirmwareManager { get; } - IMovieSession MovieSession { get; } void AddOnScreenMessage(string message); diff --git a/src/BizHawk.Client.EmuHawk/MainForm.Events.cs b/src/BizHawk.Client.EmuHawk/MainForm.Events.cs index cdb8cb40a7..2b9a1fca30 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.Events.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.Events.cs @@ -866,13 +866,13 @@ namespace BizHawk.Client.EmuHawk { if (e is RomLoader.RomErrorArgs args) { - using var configForm = new FirmwaresConfig(this, Config, Game, true, args.RomPath); + using var configForm = new FirmwaresConfig(FirmwareManager, Config.FirmwareUserSpecifications, Game, this, Config.PathEntries, retryLoadRom: true, reloadRomPath: args.RomPath); var result = configForm.ShowDialog(); args.Retry = result == DialogResult.Retry; } else { - using var configForm = new FirmwaresConfig(this, Config, Game); + using var configForm = new FirmwaresConfig(FirmwareManager, Config.FirmwareUserSpecifications, Game, this, Config.PathEntries); configForm.ShowDialog(); } } @@ -888,7 +888,7 @@ namespace BizHawk.Client.EmuHawk private void PathsMenuItem_Click(object sender, EventArgs e) { - using var form = new PathConfig(this, Config, Game); + using var form = new PathConfig(FirmwareManager, Config.FirmwareUserSpecifications, Game, this, Config.PathEntries); form.ShowDialog(); } diff --git a/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs b/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs index 5b447cc223..3b98123ec9 100644 --- a/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/FirmwaresConfig.cs @@ -28,10 +28,16 @@ namespace BizHawk.Client.EmuHawk { public partial class FirmwaresConfig : Form { - private readonly IMainFormForConfig _mainForm; - private readonly Config _config; + private readonly IDictionary _firmwareUserSpecifications; + private readonly IGameInfo _game; + private readonly IMainFormForConfig _mainForm; + + private readonly PathEntryCollection _pathEntries; + + private readonly FirmwareManager Manager; + // friendlier names than the system Ids // Redundant with SystemLookup? Not so fast. That data drives things. This is one step abstracted. Don't be such a smart guy. Keep this redundant list up to date. private static readonly Dictionary SystemGroupNames = new Dictionary @@ -95,11 +101,21 @@ namespace BizHawk.Client.EmuHawk private string _currSelectorDir; private readonly ListViewSorter _listViewSorter; - public FirmwaresConfig(IMainFormForConfig mainForm, Config config, IGameInfo game, bool retryLoadRom = false, string reloadRomPath = null) + public FirmwaresConfig( + FirmwareManager firmwareManager, + IDictionary firmwareUserSpecifications, + IGameInfo game, + IMainFormForConfig mainForm, + PathEntryCollection pathEntries, + bool retryLoadRom = false, + string reloadRomPath = null) { - _mainForm = mainForm; - _config = config; + _firmwareUserSpecifications = firmwareUserSpecifications; _game = game; + _mainForm = mainForm; + _pathEntries = pathEntries; + Manager = firmwareManager; + InitializeComponent(); tbbGroup.Image @@ -253,22 +269,20 @@ namespace BizHawk.Client.EmuHawk DoScan(); } - private FirmwareManager Manager => _mainForm.FirmwareManager; - private void DoScan() { lvFirmwares.BeginUpdate(); Manager.DoScanAndResolve( - _config.PathEntries, - _config.FirmwareUserSpecifications); + _pathEntries, + _firmwareUserSpecifications); // for each type of firmware, try resolving and record the result foreach (ListViewItem lvi in lvFirmwares.Items) { var fr = lvi.Tag as FirmwareDatabase.FirmwareRecord; var ri = Manager.Resolve( - _config.PathEntries, - _config.FirmwareUserSpecifications, + _pathEntries, + _firmwareUserSpecifications, fr, true); @@ -285,7 +299,7 @@ namespace BizHawk.Client.EmuHawk else { // lazy substring extraction. really should do a better job - var basePath = _config.PathEntries.FirmwareAbsolutePath() + Path.DirectorySeparatorChar; + var basePath = _pathEntries.FirmwareAbsolutePath() + Path.DirectorySeparatorChar; var path = ri.FilePath.Replace(basePath, ""); @@ -353,16 +367,16 @@ namespace BizHawk.Client.EmuHawk return; } - Manager.DoScanAndResolve(_config.PathEntries, _config.FirmwareUserSpecifications); + Manager.DoScanAndResolve(_pathEntries, _firmwareUserSpecifications); foreach (var fr in FirmwareDatabase.FirmwareRecords) { - var ri = Manager.Resolve(_config.PathEntries, _config.FirmwareUserSpecifications, fr); + var ri = Manager.Resolve(_pathEntries, _firmwareUserSpecifications, fr); if (ri?.KnownFirmwareFile == null) continue; if (ri.UserSpecified) continue; var fpTarget = Path.Combine( - _config.PathEntries.FirmwareAbsolutePath(), + _pathEntries.FirmwareAbsolutePath(), ri.KnownFirmwareFile.RecommendedName); string fpSource = ri.FilePath; @@ -382,7 +396,7 @@ namespace BizHawk.Client.EmuHawk private void TbbOpenFolder_Click(object sender, EventArgs e) { - var frmWares = _config.PathEntries.FirmwareAbsolutePath(); + var frmWares = _pathEntries.FirmwareAbsolutePath(); if (!Directory.Exists(frmWares)) { Directory.CreateDirectory(frmWares); @@ -418,7 +432,7 @@ namespace BizHawk.Client.EmuHawk InitialDirectory = _currSelectorDir, RestoreDirectory = true }; - string firmwarePath = _config.PathEntries.FirmwareAbsolutePath(); + string firmwarePath = _pathEntries.FirmwareAbsolutePath(); if (ofd.ShowDialog() == DialogResult.OK) { @@ -481,7 +495,7 @@ namespace BizHawk.Client.EmuHawk } } - _config.FirmwareUserSpecifications[fr.ConfigKey] = filePath; + _firmwareUserSpecifications[fr.ConfigKey] = filePath; } } catch (Exception ex) @@ -500,7 +514,7 @@ namespace BizHawk.Client.EmuHawk foreach (ListViewItem lvi in lvFirmwares.SelectedItems) { var fr = lvi.Tag as FirmwareDatabase.FirmwareRecord; - _config.FirmwareUserSpecifications.Remove(fr.ConfigKey); + _firmwareUserSpecifications.Remove(fr.ConfigKey); } DoScan(); @@ -594,7 +608,7 @@ namespace BizHawk.Client.EmuHawk return; } - using var pathConfig = new PathConfig(_mainForm, _config, _game); + using var pathConfig = new PathConfig(Manager, _firmwareUserSpecifications, _game, _mainForm, _pathEntries); pathConfig.ShowDialog(this); RefreshBasePath(); } @@ -602,7 +616,7 @@ namespace BizHawk.Client.EmuHawk private void RefreshBasePath() { string oldBasePath = _currSelectorDir; - linkBasePath.Text = _currSelectorDir = _config.PathEntries.FirmwareAbsolutePath(); + linkBasePath.Text = _currSelectorDir = _pathEntries.FirmwareAbsolutePath(); if (oldBasePath != _currSelectorDir) { DoScan(); @@ -666,7 +680,7 @@ namespace BizHawk.Client.EmuHawk private void RunImportJob(IEnumerable files) { bool didSomething = false; - var basePath = _config.PathEntries.FirmwareAbsolutePath(); + var basePath = _pathEntries.FirmwareAbsolutePath(); string errors = ""; foreach(var f in files) { diff --git a/src/BizHawk.Client.EmuHawk/config/PathConfig.cs b/src/BizHawk.Client.EmuHawk/config/PathConfig.cs index 34b8fc1c55..e08229a2c4 100644 --- a/src/BizHawk.Client.EmuHawk/config/PathConfig.cs +++ b/src/BizHawk.Client.EmuHawk/config/PathConfig.cs @@ -12,10 +12,16 @@ namespace BizHawk.Client.EmuHawk { public partial class PathConfig : Form { - private readonly Config _config; + private readonly FirmwareManager _firmwareManager; + + private readonly IDictionary _firmwareUserSpecifications; + private readonly IGameInfo _game; + private readonly IMainFormForConfig _mainForm; + private readonly PathEntryCollection _pathEntries; + // All path text boxes should do some kind of error checking // Config path under base, config will default to %exe% private void LockDownCores() @@ -43,20 +49,27 @@ namespace BizHawk.Client.EmuHawk "..\\" }; - public PathConfig(IMainFormForConfig mainForm, Config config, IGameInfo game) + public PathConfig( + FirmwareManager firmwareManager, + IDictionary firmwareUserSpecifications, + IGameInfo game, + IMainFormForConfig mainForm, + PathEntryCollection pathEntries) { - _mainForm = mainForm; - _config = config; + _firmwareManager = firmwareManager; + _firmwareUserSpecifications = firmwareUserSpecifications; _game = game; + _mainForm = mainForm; + _pathEntries = pathEntries; InitializeComponent(); SpecialCommandsBtn.Image = Properties.Resources.Help; } private void LoadSettings() { - RecentForROMs.Checked = _config.PathEntries.UseRecentForRoms; + RecentForROMs.Checked = _pathEntries.UseRecentForRoms; - DoTabs(_config.PathEntries.ToList()); + DoTabs(_pathEntries.ToList()); SetDefaultFocusedTab(); DoRomToggle(); } @@ -88,7 +101,7 @@ namespace BizHawk.Client.EmuHawk PathTabControl.TabPages.Clear(); // Separate by system - var systems = _config.PathEntries + var systems = _pathEntries .Select(s => s.SystemDisplayName) .Distinct() .ToList(); @@ -112,7 +125,7 @@ namespace BizHawk.Client.EmuHawk foreach (var systemDisplayName in systems) { - var systemId = _config.PathEntries.First(p => p.SystemDisplayName == systemDisplayName).System; + var systemId = _pathEntries.First(p => p.SystemDisplayName == systemDisplayName).System; var t = new TabPage { Text = systemDisplayName, @@ -179,7 +192,7 @@ namespace BizHawk.Client.EmuHawk return; } - using var f = new FirmwaresConfig(_mainForm, _config, _game) { TargetSystem = "Global" }; + using var f = new FirmwaresConfig(_firmwareManager, _firmwareUserSpecifications, _game, _mainForm, _pathEntries) { TargetSystem = "Global" }; f.ShowDialog(this); }; @@ -225,7 +238,7 @@ namespace BizHawk.Client.EmuHawk using var f = new FolderBrowserDialog { Description = $"Set the directory for {name}", - SelectedPath = _config.PathEntries.AbsolutePathFor(box.Text, system) + SelectedPath = _pathEntries.AbsolutePathFor(box.Text, system) }; result = f.ShowDialog(); selectedPath = f.SelectedPath; @@ -235,28 +248,28 @@ namespace BizHawk.Client.EmuHawk using var f = new FolderBrowserEx { Description = $"Set the directory for {name}", - SelectedPath = _config.PathEntries.AbsolutePathFor(box.Text, system) + SelectedPath = _pathEntries.AbsolutePathFor(box.Text, system) }; result = f.ShowDialog(); selectedPath = f.SelectedPath; } if (result.IsOk()) { - box.Text = _config.PathEntries.TryMakeRelative(selectedPath, system); + box.Text = _pathEntries.TryMakeRelative(selectedPath, system); } } private void SaveSettings() { - _config.PathEntries.UseRecentForRoms = RecentForROMs.Checked; + _pathEntries.UseRecentForRoms = RecentForROMs.Checked; foreach (var t in AllPathBoxes) { - var pathEntry = _config.PathEntries.First(p => p.System == t.Parent.Name && p.Type == t.Name); + var pathEntry = _pathEntries.First(p => p.System == t.Parent.Name && p.Type == t.Name); pathEntry.Path = t.Text; } - _mainForm.MovieSession.BackupDirectory = _config.PathEntries.MovieBackupsAbsolutePath(); + _mainForm.MovieSession.BackupDirectory = _pathEntries.MovieBackupsAbsolutePath(); } private void DoRomToggle() @@ -326,7 +339,7 @@ namespace BizHawk.Client.EmuHawk { SaveSettings(); - _config.PathEntries.RefreshTempPath(); + _pathEntries.RefreshTempPath(); _mainForm.AddOnScreenMessage("Path settings saved"); Close(); }