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.Emulation.Common;
namespace BizHawk.Client.EmuHawk
{
public interface IMainFormForRetroAchievements : IDialogController
public interface IMainFormForRetroAchievements
{
LoadRomArgs CurrentlyOpenRomArgs { get; }
@ -17,8 +15,6 @@ namespace BizHawk.Client.EmuHawk
GameInfo Game { get; }
IntPtr Handle { get; }
IMovieSession MovieSession { get; }
event BeforeQuickLoadEventHandler QuicksaveLoad;

View File

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

View File

@ -100,7 +100,10 @@ namespace BizHawk.Client.EmuHawk
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);
}
@ -110,16 +113,21 @@ namespace BizHawk.Client.EmuHawk
protected override int IdentifyRom(byte[] rom)
=> RA.IdentifyRom(rom, rom.Length);
public RAIntegration(IMainFormForRetroAchievements mainForm, InputManager inputManager, ToolManager tools,
Func<Config> getConfig, ToolStripItemCollection raDropDownItems, Action shutdownRACallback)
: base(mainForm, inputManager, tools, getConfig, raDropDownItems, shutdownRACallback)
public RAIntegration(
MainForm mainForm,
InputManager inputManager,
ToolManager tools,
Func<Config> getConfig,
ToolStripItemCollection raDropDownItems,
Action shutdownRACallback)
: base(mainForm, inputManager, tools, getConfig, raDropDownItems, shutdownRACallback)
{
_memGuard = new(_memLock, _memSema, _memSync);
_memAccess = new(_memLock, _memSema, _memSync);
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();
_unpause = _mainForm.UnpauseEmulator;

View File

@ -202,13 +202,21 @@ namespace BizHawk.Client.EmuHawk
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;
}
public RCheevos(IMainFormForRetroAchievements mainForm, InputManager inputManager, ToolManager tools,
Func<Config> getConfig, ToolStripItemCollection raDropDownItems, Action shutdownRACallback)
: base(mainForm, inputManager, tools, getConfig, raDropDownItems, shutdownRACallback)
public RCheevos(
MainForm mainForm,
InputManager inputManager,
ToolManager tools,
Func<Config> getConfig,
ToolStripItemCollection raDropDownItems,
Action shutdownRACallback)
: base(mainForm, inputManager, tools, getConfig, raDropDownItems, shutdownRACallback)
{
_isActive = true;
_httpThread = new(HttpRequestThreadProc) { IsBackground = true, Priority = ThreadPriority.BelowNormal, Name = "RCheevos HTTP Thread" };
@ -299,7 +307,10 @@ namespace BizHawk.Client.EmuHawk
{
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);
var prefix = HardcoreMode ? "[HARDCORE] " : "";
_mainForm.AddOnScreenMessage($"{prefix}Achievement Unlocked!");
_mainForm.AddOnScreenMessage(cheevo.Description);
_dialogParent.AddOnScreenMessage($"{prefix}Achievement Unlocked!");
_dialogParent.AddOnScreenMessage(cheevo.Description);
if (EnableSoundEffects) _unlockSound.PlayNoExceptions();
if (cheevo.IsOfficial)
@ -486,8 +497,8 @@ namespace BizHawk.Client.EmuHawk
{
cheevo.IsPrimed = true;
var prefix = HardcoreMode ? "[HARDCORE] " : "";
_mainForm.AddOnScreenMessage($"{prefix}Achievement Primed!");
_mainForm.AddOnScreenMessage(cheevo.Description);
_dialogParent.AddOnScreenMessage($"{prefix}Achievement Primed!");
_dialogParent.AddOnScreenMessage(cheevo.Description);
if (EnableSoundEffects) _infoSound.PlayNoExceptions();
}
@ -505,8 +516,8 @@ namespace BizHawk.Client.EmuHawk
if (!lboard.Hidden)
{
CurrentLboard = lboard;
_mainForm.AddOnScreenMessage($"Leaderboard Attempt Started!");
_mainForm.AddOnScreenMessage(lboard.Description);
_dialogParent.AddOnScreenMessage($"Leaderboard Attempt Started!");
_dialogParent.AddOnScreenMessage(lboard.Description);
if (EnableSoundEffects) _lboardStartSound.PlayNoExceptions();
}
}
@ -527,8 +538,8 @@ namespace BizHawk.Client.EmuHawk
CurrentLboard = null;
}
_mainForm.AddOnScreenMessage($"Leaderboard Attempt Failed! ({lboard.Score})");
_mainForm.AddOnScreenMessage(lboard.Description);
_dialogParent.AddOnScreenMessage($"Leaderboard Attempt Failed! ({lboard.Score})");
_dialogParent.AddOnScreenMessage(lboard.Description);
if (EnableSoundEffects) _lboardFailedSound.PlayNoExceptions();
}
@ -565,8 +576,8 @@ namespace BizHawk.Client.EmuHawk
CurrentLboard = null;
}
_mainForm.AddOnScreenMessage($"Leaderboard Attempt Complete! ({lboard.Score})");
_mainForm.AddOnScreenMessage(lboard.Description);
_dialogParent.AddOnScreenMessage($"Leaderboard Attempt Complete! ({lboard.Score})");
_dialogParent.AddOnScreenMessage(lboard.Description);
if (EnableSoundEffects) _unlockSound.PlayNoExceptions();
}
}
@ -592,8 +603,8 @@ namespace BizHawk.Client.EmuHawk
{
cheevo.IsPrimed = false;
var prefix = HardcoreMode ? "[HARDCORE] " : "";
_mainForm.AddOnScreenMessage($"{prefix}Achievement Unprimed!");
_mainForm.AddOnScreenMessage(cheevo.Description);
_dialogParent.AddOnScreenMessage($"{prefix}Achievement Unprimed!");
_dialogParent.AddOnScreenMessage(cheevo.Description);
if (EnableSoundEffects) _infoSound.PlayNoExceptions();
}

View File

@ -9,6 +9,8 @@ namespace BizHawk.Client.EmuHawk
{
public abstract partial class RetroAchievements : IRetroAchievements
{
protected readonly IDialogParent _dialogParent;
protected readonly IMainFormForRetroAchievements _mainForm;
protected readonly InputManager _inputManager;
protected readonly ToolManager _tools;
@ -23,9 +25,15 @@ namespace BizHawk.Client.EmuHawk
protected IReadOnlyList<MemFunctions> _memFunctions;
protected RetroAchievements(IMainFormForRetroAchievements mainForm, InputManager inputManager, ToolManager tools,
Func<Config> getConfig, ToolStripItemCollection raDropDownItems, Action shutdownRACallback)
protected RetroAchievements(
MainForm mainForm,
InputManager inputManager,
ToolManager tools,
Func<Config> getConfig,
ToolStripItemCollection raDropDownItems,
Action shutdownRACallback)
{
_dialogParent = mainForm;
_mainForm = mainForm;
_inputManager = inputManager;
_tools = tools;
@ -34,11 +42,16 @@ namespace BizHawk.Client.EmuHawk
_shutdownRACallback = shutdownRACallback;
}
public static IRetroAchievements CreateImpl(IMainFormForRetroAchievements mainForm, InputManager inputManager, ToolManager tools,
Func<Config> getConfig, ToolStripItemCollection raDropDownItems, Action shutdownRACallback)
public static IRetroAchievements CreateImpl(
MainForm mainForm,
InputManager inputManager,
ToolManager tools,
Func<Config> getConfig,
ToolStripItemCollection raDropDownItems,
Action shutdownRACallback)
{
if (getConfig().SkipRATelemetryWarning || mainForm.ShowMessageBox2(
owner: null,
var config = getConfig();
if (config.SkipRATelemetryWarning || mainForm.ModalMessageBox2(
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 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,
useOKCancel: false))
{
getConfig().SkipRATelemetryWarning = true;
config.SkipRATelemetryWarning = true;
if (RAIntegration.IsAvailable && RAIntegration.CheckUpdateRA(mainForm))
{