Cleanup instantiation of FirmwaresManager and PathConfig

and remove unused MainForm interface prop
This commit is contained in:
YoshiRulz 2020-12-29 16:26:01 +10:00
parent 18ebfecc97
commit 206a3e30c8
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
4 changed files with 68 additions and 44 deletions

View File

@ -8,9 +8,6 @@ namespace BizHawk.Client.EmuHawk
/// <remarks>only referenced from <see cref="GenericCoreConfig"/></remarks>
IEmulator Emulator { get; }
/// <remarks>only referenced from <see cref="FirmwaresConfig"/></remarks>
FirmwareManager FirmwareManager { get; }
IMovieSession MovieSession { get; }
void AddOnScreenMessage(string message);

View File

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

View File

@ -28,10 +28,16 @@ namespace BizHawk.Client.EmuHawk
{
public partial class FirmwaresConfig : Form
{
private readonly IMainFormForConfig _mainForm;
private readonly Config _config;
private readonly IDictionary<string, string> _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<string, string> SystemGroupNames = new Dictionary<string, string>
@ -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<string, string> 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<string> files)
{
bool didSomething = false;
var basePath = _config.PathEntries.FirmwareAbsolutePath();
var basePath = _pathEntries.FirmwareAbsolutePath();
string errors = "";
foreach(var f in files)
{

View File

@ -12,10 +12,16 @@ namespace BizHawk.Client.EmuHawk
{
public partial class PathConfig : Form
{
private readonly Config _config;
private readonly FirmwareManager _firmwareManager;
private readonly IDictionary<string, string> _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<string, string> 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();
}