Clean up handling of modal dialogs and parenting in RA

This commit is contained in:
James Groom 2023-11-23 07:56:45 +00:00 committed by GitHub
parent 36df9c387b
commit 2105871122
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 43 deletions

View File

@ -1,11 +1,9 @@
using System;
using BizHawk.Client.Common; using BizHawk.Client.Common;
using BizHawk.Emulation.Common; using BizHawk.Emulation.Common;
namespace BizHawk.Client.EmuHawk namespace BizHawk.Client.EmuHawk
{ {
public interface IMainFormForRetroAchievements : IDialogController public interface IMainFormForRetroAchievements
{ {
LoadRomArgs CurrentlyOpenRomArgs { get; } LoadRomArgs CurrentlyOpenRomArgs { get; }
@ -17,8 +15,6 @@ namespace BizHawk.Client.EmuHawk
GameInfo Game { get; } GameInfo Game { get; }
IntPtr Handle { get; }
IMovieSession MovieSession { get; } IMovieSession MovieSession { get; }
event BeforeQuickLoadEventHandler QuicksaveLoad; event BeforeQuickLoadEventHandler QuicksaveLoad;

View File

@ -58,7 +58,7 @@ namespace BizHawk.Client.EmuHawk
return downloadForm.DownloadSucceeded(); return downloadForm.DownloadSucceeded();
} }
public static bool CheckUpdateRA(IMainFormForRetroAchievements mainForm) public static bool CheckUpdateRA(IDialogParent dialogParent)
{ {
try try
{ {
@ -71,8 +71,7 @@ namespace BizHawk.Client.EmuHawk
if (_version < minVer) if (_version < minVer)
{ {
if (!mainForm.ShowMessageBox2( if (!dialogParent.ModalMessageBox2(
owner: null,
text: text:
"An update is required to use RetroAchievements. Do you want to download the update now?", "An update is required to use RetroAchievements. Do you want to download the update now?",
caption: "Update", caption: "Update",
@ -86,8 +85,7 @@ namespace BizHawk.Client.EmuHawk
if (_version >= lastestVer) return true; if (_version >= lastestVer) return true;
if (!mainForm.ShowMessageBox2( if (!dialogParent.ModalMessageBox2(
owner: null,
text: text:
"An optional update is available for RetroAchievements. Do you want to download the update now?", "An optional update is available for RetroAchievements. Do you want to download the update now?",
caption: "Update", caption: "Update",
@ -100,8 +98,7 @@ namespace BizHawk.Client.EmuHawk
return true; // even if this fails, should be OK to use the old dll return true; // even if this fails, should be OK to use the old dll
} }
mainForm.ShowMessageBox( dialogParent.ModalMessageBox(
owner: null,
text: "Failed to fetch update information, cannot start RetroAchievements.", text: "Failed to fetch update information, cannot start RetroAchievements.",
caption: "Error", caption: "Error",
icon: EMsgBoxIcon.Error); icon: EMsgBoxIcon.Error);
@ -111,8 +108,7 @@ namespace BizHawk.Client.EmuHawk
catch (Exception ex) catch (Exception ex)
{ {
// is this needed? // is this needed?
mainForm.ShowMessageBox( dialogParent.ModalMessageBox(
owner: null,
text: $"Exception {ex.Message} occurred when fetching update information, cannot start RetroAchievements.", text: $"Exception {ex.Message} occurred when fetching update information, cannot start RetroAchievements.",
caption: "Error", caption: "Error",
icon: EMsgBoxIcon.Error); icon: EMsgBoxIcon.Error);

View File

@ -100,7 +100,10 @@ namespace BizHawk.Client.EmuHawk
protected override void HandleHardcoreModeDisable(string reason) protected override void HandleHardcoreModeDisable(string reason)
{ {
_mainForm.ShowMessageBox(null, $"{reason} Disabling hardcore mode.", "Warning", EMsgBoxIcon.Warning); _dialogParent.ModalMessageBox(
caption: "Warning",
icon: EMsgBoxIcon.Warning,
text: $"{reason} Disabling hardcore mode.");
RA.WarnDisableHardcore(null); RA.WarnDisableHardcore(null);
} }
@ -110,16 +113,21 @@ namespace BizHawk.Client.EmuHawk
protected override int IdentifyRom(byte[] rom) protected override int IdentifyRom(byte[] rom)
=> RA.IdentifyRom(rom, rom.Length); => RA.IdentifyRom(rom, rom.Length);
public RAIntegration(IMainFormForRetroAchievements mainForm, InputManager inputManager, ToolManager tools, public RAIntegration(
Func<Config> getConfig, ToolStripItemCollection raDropDownItems, Action shutdownRACallback) MainForm mainForm,
: base(mainForm, inputManager, tools, getConfig, raDropDownItems, shutdownRACallback) InputManager inputManager,
ToolManager tools,
Func<Config> getConfig,
ToolStripItemCollection raDropDownItems,
Action shutdownRACallback)
: base(mainForm, inputManager, tools, getConfig, raDropDownItems, shutdownRACallback)
{ {
_memGuard = new(_memLock, _memSema, _memSync); _memGuard = new(_memLock, _memSema, _memSync);
_memAccess = new(_memLock, _memSema, _memSync); _memAccess = new(_memLock, _memSema, _memSync);
RA = BizInvoker.GetInvoker<RAInterface>(_resolver, _memAccess, CallingConventionAdapters.Native); RA = BizInvoker.GetInvoker<RAInterface>(_resolver, _memAccess, CallingConventionAdapters.Native);
RA.InitClient(_mainForm.Handle, "BizHawk", VersionInfo.GetEmuVersion()); RA.InitClient(mainForm.AsWinFormsHandle().Handle, "BizHawk", VersionInfo.GetEmuVersion());
_isActive = () => !Emu.IsNull(); _isActive = () => !Emu.IsNull();
_unpause = _mainForm.UnpauseEmulator; _unpause = _mainForm.UnpauseEmulator;

View File

@ -202,13 +202,21 @@ namespace BizHawk.Client.EmuHawk
protected override void HandleHardcoreModeDisable(string reason) protected override void HandleHardcoreModeDisable(string reason)
{ {
_mainForm.ShowMessageBox(null, $"{reason} Disabling hardcore mode.", "Warning", EMsgBoxIcon.Warning); _dialogParent.ModalMessageBox(
caption: "Warning",
icon: EMsgBoxIcon.Warning,
text: $"{reason} Disabling hardcore mode.");
HardcoreMode = false; HardcoreMode = false;
} }
public RCheevos(IMainFormForRetroAchievements mainForm, InputManager inputManager, ToolManager tools, public RCheevos(
Func<Config> getConfig, ToolStripItemCollection raDropDownItems, Action shutdownRACallback) MainForm mainForm,
: base(mainForm, inputManager, tools, getConfig, raDropDownItems, shutdownRACallback) InputManager inputManager,
ToolManager tools,
Func<Config> getConfig,
ToolStripItemCollection raDropDownItems,
Action shutdownRACallback)
: base(mainForm, inputManager, tools, getConfig, raDropDownItems, shutdownRACallback)
{ {
_isActive = true; _isActive = true;
_httpThread = new(HttpRequestThreadProc) { IsBackground = true, Priority = ThreadPriority.BelowNormal, Name = "RCheevos HTTP Thread" }; _httpThread = new(HttpRequestThreadProc) { IsBackground = true, Priority = ThreadPriority.BelowNormal, Name = "RCheevos HTTP Thread" };
@ -299,7 +307,10 @@ namespace BizHawk.Client.EmuHawk
{ {
if (HardcoreMode) if (HardcoreMode)
{ {
e.Handled = _mainForm.ShowMessageBox2(null, "Loading a quicksave is not allowed in hardcode mode. Abort loading state?", "Warning", EMsgBoxIcon.Warning); e.Handled = _dialogParent.ModalMessageBox2(
caption: "Warning",
icon: EMsgBoxIcon.Warning,
text: "Loading a quicksave is not allowed in hardcode mode. Abort loading state?");
} }
} }
@ -465,8 +476,8 @@ namespace BizHawk.Client.EmuHawk
cheevo.SetUnlocked(HardcoreMode, true); cheevo.SetUnlocked(HardcoreMode, true);
var prefix = HardcoreMode ? "[HARDCORE] " : ""; var prefix = HardcoreMode ? "[HARDCORE] " : "";
_mainForm.AddOnScreenMessage($"{prefix}Achievement Unlocked!"); _dialogParent.AddOnScreenMessage($"{prefix}Achievement Unlocked!");
_mainForm.AddOnScreenMessage(cheevo.Description); _dialogParent.AddOnScreenMessage(cheevo.Description);
if (EnableSoundEffects) _unlockSound.PlayNoExceptions(); if (EnableSoundEffects) _unlockSound.PlayNoExceptions();
if (cheevo.IsOfficial) if (cheevo.IsOfficial)
@ -486,8 +497,8 @@ namespace BizHawk.Client.EmuHawk
{ {
cheevo.IsPrimed = true; cheevo.IsPrimed = true;
var prefix = HardcoreMode ? "[HARDCORE] " : ""; var prefix = HardcoreMode ? "[HARDCORE] " : "";
_mainForm.AddOnScreenMessage($"{prefix}Achievement Primed!"); _dialogParent.AddOnScreenMessage($"{prefix}Achievement Primed!");
_mainForm.AddOnScreenMessage(cheevo.Description); _dialogParent.AddOnScreenMessage(cheevo.Description);
if (EnableSoundEffects) _infoSound.PlayNoExceptions(); if (EnableSoundEffects) _infoSound.PlayNoExceptions();
} }
@ -505,8 +516,8 @@ namespace BizHawk.Client.EmuHawk
if (!lboard.Hidden) if (!lboard.Hidden)
{ {
CurrentLboard = lboard; CurrentLboard = lboard;
_mainForm.AddOnScreenMessage($"Leaderboard Attempt Started!"); _dialogParent.AddOnScreenMessage($"Leaderboard Attempt Started!");
_mainForm.AddOnScreenMessage(lboard.Description); _dialogParent.AddOnScreenMessage(lboard.Description);
if (EnableSoundEffects) _lboardStartSound.PlayNoExceptions(); if (EnableSoundEffects) _lboardStartSound.PlayNoExceptions();
} }
} }
@ -527,8 +538,8 @@ namespace BizHawk.Client.EmuHawk
CurrentLboard = null; CurrentLboard = null;
} }
_mainForm.AddOnScreenMessage($"Leaderboard Attempt Failed! ({lboard.Score})"); _dialogParent.AddOnScreenMessage($"Leaderboard Attempt Failed! ({lboard.Score})");
_mainForm.AddOnScreenMessage(lboard.Description); _dialogParent.AddOnScreenMessage(lboard.Description);
if (EnableSoundEffects) _lboardFailedSound.PlayNoExceptions(); if (EnableSoundEffects) _lboardFailedSound.PlayNoExceptions();
} }
@ -565,8 +576,8 @@ namespace BizHawk.Client.EmuHawk
CurrentLboard = null; CurrentLboard = null;
} }
_mainForm.AddOnScreenMessage($"Leaderboard Attempt Complete! ({lboard.Score})"); _dialogParent.AddOnScreenMessage($"Leaderboard Attempt Complete! ({lboard.Score})");
_mainForm.AddOnScreenMessage(lboard.Description); _dialogParent.AddOnScreenMessage(lboard.Description);
if (EnableSoundEffects) _unlockSound.PlayNoExceptions(); if (EnableSoundEffects) _unlockSound.PlayNoExceptions();
} }
} }
@ -592,8 +603,8 @@ namespace BizHawk.Client.EmuHawk
{ {
cheevo.IsPrimed = false; cheevo.IsPrimed = false;
var prefix = HardcoreMode ? "[HARDCORE] " : ""; var prefix = HardcoreMode ? "[HARDCORE] " : "";
_mainForm.AddOnScreenMessage($"{prefix}Achievement Unprimed!"); _dialogParent.AddOnScreenMessage($"{prefix}Achievement Unprimed!");
_mainForm.AddOnScreenMessage(cheevo.Description); _dialogParent.AddOnScreenMessage(cheevo.Description);
if (EnableSoundEffects) _infoSound.PlayNoExceptions(); if (EnableSoundEffects) _infoSound.PlayNoExceptions();
} }

View File

@ -9,6 +9,8 @@ namespace BizHawk.Client.EmuHawk
{ {
public abstract partial class RetroAchievements : IRetroAchievements public abstract partial class RetroAchievements : IRetroAchievements
{ {
protected readonly IDialogParent _dialogParent;
protected readonly IMainFormForRetroAchievements _mainForm; protected readonly IMainFormForRetroAchievements _mainForm;
protected readonly InputManager _inputManager; protected readonly InputManager _inputManager;
protected readonly ToolManager _tools; protected readonly ToolManager _tools;
@ -23,9 +25,15 @@ namespace BizHawk.Client.EmuHawk
protected IReadOnlyList<MemFunctions> _memFunctions; protected IReadOnlyList<MemFunctions> _memFunctions;
protected RetroAchievements(IMainFormForRetroAchievements mainForm, InputManager inputManager, ToolManager tools, protected RetroAchievements(
Func<Config> getConfig, ToolStripItemCollection raDropDownItems, Action shutdownRACallback) MainForm mainForm,
InputManager inputManager,
ToolManager tools,
Func<Config> getConfig,
ToolStripItemCollection raDropDownItems,
Action shutdownRACallback)
{ {
_dialogParent = mainForm;
_mainForm = mainForm; _mainForm = mainForm;
_inputManager = inputManager; _inputManager = inputManager;
_tools = tools; _tools = tools;
@ -34,11 +42,16 @@ namespace BizHawk.Client.EmuHawk
_shutdownRACallback = shutdownRACallback; _shutdownRACallback = shutdownRACallback;
} }
public static IRetroAchievements CreateImpl(IMainFormForRetroAchievements mainForm, InputManager inputManager, ToolManager tools, public static IRetroAchievements CreateImpl(
Func<Config> getConfig, ToolStripItemCollection raDropDownItems, Action shutdownRACallback) MainForm mainForm,
InputManager inputManager,
ToolManager tools,
Func<Config> getConfig,
ToolStripItemCollection raDropDownItems,
Action shutdownRACallback)
{ {
if (getConfig().SkipRATelemetryWarning || mainForm.ShowMessageBox2( var config = getConfig();
owner: null, if (config.SkipRATelemetryWarning || mainForm.ModalMessageBox2(
text: "In order to use RetroAchievements, some information needs to be sent to retroachievements.org:\n" + text: "In order to use RetroAchievements, some information needs to be sent to retroachievements.org:\n" +
"\n\u2022 Your RetroAchievements username and password (first login) or token (subsequent logins)." + "\n\u2022 Your RetroAchievements username and password (first login) or token (subsequent logins)." +
"\n\u2022 The hash of the game(s) you have loaded into BizHawk. (for game identification + achievement unlock + leaderboard submission)" + "\n\u2022 The hash of the game(s) you have loaded into BizHawk. (for game identification + achievement unlock + leaderboard submission)" +
@ -51,7 +64,7 @@ namespace BizHawk.Client.EmuHawk
icon: EMsgBoxIcon.Question, icon: EMsgBoxIcon.Question,
useOKCancel: false)) useOKCancel: false))
{ {
getConfig().SkipRATelemetryWarning = true; config.SkipRATelemetryWarning = true;
if (RAIntegration.IsAvailable && RAIntegration.CheckUpdateRA(mainForm)) if (RAIntegration.IsAvailable && RAIntegration.CheckUpdateRA(mainForm))
{ {