diff --git a/src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs b/src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs index 745bef098a..a1c4aa63c2 100644 --- a/src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs +++ b/src/BizHawk.Client.Common/Api/Classes/EmuClientApi.cs @@ -40,6 +40,12 @@ namespace BizHawk.Client.Common _logCallback = logCallback; _mainForm = mainForm; VideoProvider = Emulator.AsVideoProviderOrDefault(); + + _mainForm.QuicksaveLoad += CallBeforeQuickLoad; + _mainForm.QuicksaveSave += CallBeforeQuickSave; + _mainForm.RomLoaded += CallRomLoaded; + _mainForm.SavestateLoaded += CallStateLoaded; + _mainForm.SavestateSaved += CallStateSaved; } public int BorderHeight() => _displayManager.TransformPoint(new Point(0, 0)).Y; @@ -50,6 +56,21 @@ namespace BizHawk.Client.Common public int BufferWidth() => VideoProvider.BufferWidth; + private void CallBeforeQuickLoad(object sender, BeforeQuickLoadEventArgs args) + => BeforeQuickLoad?.Invoke(sender, args); + + private void CallBeforeQuickSave(object sender, BeforeQuickSaveEventArgs args) + => BeforeQuickSave?.Invoke(sender, args); + + private void CallRomLoaded(object sender, EventArgs args) + => RomLoaded?.Invoke(sender, args); + + private void CallStateLoaded(object sender, StateLoadedEventArgs args) + => StateLoaded?.Invoke(sender, args); + + private void CallStateSaved(object sender, StateSavedEventArgs args) + => StateSaved?.Invoke(sender, args); + public void ClearAutohold() => _mainForm.ClearHolds(); public void CloseEmulator(int? exitCode = null) => _mainForm.CloseEmulator(exitCode); @@ -58,6 +79,15 @@ namespace BizHawk.Client.Common public void DisplayMessages(bool value) => _config.DisplayMessages = value; + public void Dispose() + { + _mainForm.QuicksaveLoad -= CallBeforeQuickLoad; + _mainForm.QuicksaveSave -= CallBeforeQuickSave; + _mainForm.RomLoaded -= CallRomLoaded; + _mainForm.SavestateLoaded -= CallStateLoaded; + _mainForm.SavestateSaved -= CallStateSaved; + } + public void DoFrameAdvance() { _mainForm.FrameAdvance(); @@ -107,39 +137,6 @@ namespace BizHawk.Client.Common userFriendlyStateName: name, suppressOSD: false); - public void OnBeforeQuickLoad(object sender, string quickSaveSlotName, out bool eventHandled) - { - if (BeforeQuickLoad == null) - { - eventHandled = false; - return; - } - var e = new BeforeQuickLoadEventArgs(quickSaveSlotName); - BeforeQuickLoad(sender, e); - eventHandled = e.Handled; - } - - public void OnBeforeQuickSave(object sender, string quickSaveSlotName, out bool eventHandled) - { - if (BeforeQuickSave == null) - { - eventHandled = false; - return; - } - var e = new BeforeQuickSaveEventArgs(quickSaveSlotName); - BeforeQuickSave(sender, e); - eventHandled = e.Handled; - } - - public void OnRomLoaded() - { - RomLoaded?.Invoke(null, EventArgs.Empty); - } - - public void OnStateLoaded(object sender, string stateName) => StateLoaded?.Invoke(sender, new StateLoadedEventArgs(stateName)); - - public void OnStateSaved(object sender, string stateName) => StateSaved?.Invoke(sender, new StateSavedEventArgs(stateName)); - public bool OpenRom(string path) => _mainForm.LoadRom(path, new LoadRomArgs { OpenAdvanced = OpenAdvancedSerializer.ParseWithLegacy(path) }); diff --git a/src/BizHawk.Client.Common/Api/Interfaces/IEmuClientApi.cs b/src/BizHawk.Client.Common/Api/Interfaces/IEmuClientApi.cs index 32b0c0bc6f..55e95cd781 100644 --- a/src/BizHawk.Client.Common/Api/Interfaces/IEmuClientApi.cs +++ b/src/BizHawk.Client.Common/Api/Interfaces/IEmuClientApi.cs @@ -3,7 +3,7 @@ using System.Drawing; namespace BizHawk.Client.Common { - public interface IEmuClientApi : IExternalApi + public interface IEmuClientApi : IDisposable, IExternalApi { /// /// Occurs before a quickload is done (just after user has pressed the shortcut button @@ -91,41 +91,6 @@ namespace BizHawk.Client.Common /// iff succeeded bool LoadState(string name); - /// - /// Raised before a quickload is done (just after pressing shortcut button) - /// - /// Object who raised the event - /// Slot used for quickload - /// A boolean that can be set if users want to handle save themselves; if so, BizHawk won't do anything - void OnBeforeQuickLoad(object sender, string quickSaveSlotName, out bool eventHandled); - - /// - /// Raised before a quicksave is done (just after pressing shortcut button) - /// - /// Object who raised the event - /// Slot used for quicksave - /// A boolean that can be set if users want to handle save themselves; if so, BizHawk won't do anything - void OnBeforeQuickSave(object sender, string quickSaveSlotName, out bool eventHandled); - - /// - /// Raise when a rom is successfully Loaded - /// - void OnRomLoaded(); - - /// - /// Raise when a state is loaded - /// - /// Object who raised the event - /// User friendly name for saved state - void OnStateLoaded(object sender, string stateName); - - /// - /// Raise when a state is saved - /// - /// Object who raised the event - /// User friendly name for saved state - void OnStateSaved(object sender, string stateName); - bool OpenRom(string path); void Pause(); diff --git a/src/BizHawk.Client.Common/IMainFormForApi.cs b/src/BizHawk.Client.Common/IMainFormForApi.cs index 3ffe4c099f..343aeff4ce 100644 --- a/src/BizHawk.Client.Common/IMainFormForApi.cs +++ b/src/BizHawk.Client.Common/IMainFormForApi.cs @@ -1,3 +1,4 @@ +using System; using System.Drawing; using BizHawk.Emulation.Common; @@ -117,5 +118,15 @@ namespace BizHawk.Client.Common void ToggleSound(); void UnpauseEmulator(); + + event BeforeQuickLoadEventHandler QuicksaveLoad; + + event BeforeQuickSaveEventHandler QuicksaveSave; + + event EventHandler RomLoaded; + + event StateLoadedEventHandler SavestateLoaded; + + event StateSavedEventHandler SavestateSaved; } } diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index 1b9a605def..19dbc6b6bf 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -1014,6 +1014,16 @@ namespace BizHawk.Client.EmuHawk public EmuClientApi EmuClient { get; set; } + public event BeforeQuickLoadEventHandler QuicksaveLoad; + + public event BeforeQuickSaveEventHandler QuicksaveSave; + + public event EventHandler RomLoaded; + + public event StateLoadedEventHandler SavestateLoaded; + + public event StateSavedEventHandler SavestateSaved; + private readonly InputManager InputManager; private IVideoProvider _currentVideoProvider = NullVideo.Instance; @@ -4013,7 +4023,7 @@ namespace BizHawk.Client.EmuHawk ExtToolManager.BuildToolStrip(); - EmuClient.OnRomLoaded(); + RomLoaded?.Invoke(null, EventArgs.Empty); return true; } else if (Emulator.IsNull()) @@ -4027,7 +4037,7 @@ namespace BizHawk.Client.EmuHawk else { // The ROM has been loaded by a recursive invocation of the LoadROM method. - EmuClient.OnRomLoaded(); + RomLoaded?.Invoke(null, EventArgs.Empty); return true; } } @@ -4254,7 +4264,11 @@ namespace BizHawk.Client.EmuHawk } OSD.ClearGuiText(); - EmuClient.OnStateLoaded(this, userFriendlyStateName); + if (SavestateLoaded is not null) + { + StateLoadedEventArgs args = new(userFriendlyStateName); + SavestateLoaded(this, args); + } RA?.OnLoadState(path); if (Tools.Has()) @@ -4287,7 +4301,13 @@ namespace BizHawk.Client.EmuHawk if (!Emulator.HasSavestates()) return false; var quickSlotName = $"QuickSave{slot % 10}"; - EmuClient.OnBeforeQuickLoad(this, quickSlotName, out var handled); + var handled = false; + if (QuicksaveLoad is not null) + { + BeforeQuickLoadEventArgs args = new(quickSlotName); + QuicksaveLoad(this, args); + handled = args.Handled; + } if (handled) return true; // not sure if (IsSavestateSlave) return Master.LoadQuickSave(SlotToInt(quickSlotName)); @@ -4319,7 +4339,11 @@ namespace BizHawk.Client.EmuHawk { new SavestateFile(Emulator, MovieSession, MovieSession.UserBag).Create(path, Config.Savestates); - EmuClient.OnStateSaved(this, userFriendlyStateName); + if (SavestateSaved is not null) + { + StateSavedEventArgs args = new(userFriendlyStateName); + SavestateSaved(this, args); + } RA?.OnSaveState(path); if (!suppressOSD) @@ -4346,7 +4370,13 @@ namespace BizHawk.Client.EmuHawk return; } var quickSlotName = $"QuickSave{slot % 10}"; - EmuClient.OnBeforeQuickSave(this, quickSlotName, out var handled); + var handled = false; + if (QuicksaveSave is not null) + { + BeforeQuickSaveEventArgs args = new(quickSlotName); + QuicksaveSave(this, args); + handled = args.Handled; + } if (handled) { return;