Skip to content

Commit 1e313b7

Browse files
committed
Move stuff around to make more sense for ask-user-about-saving-or-canceling flow even though not all asking and error handling is implemented yet. Also, fix: respect the cancel button.
1 parent 28489c5 commit 1e313b7

File tree

1 file changed

+40
-36
lines changed

1 file changed

+40
-36
lines changed

src/BizHawk.Client.EmuHawk/MainForm.cs

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -863,31 +863,18 @@ private void CheckMayCloseAndCleanup(object/*?*/ closingSender, CancelEventArgs
863863
closingArgs.Cancel = true;
864864
return;
865865
}
866+
// StopAv would be handled in CloseGame, but since we've asked the user about it, best to handle it now.
866867
StopAv();
867868
}
868869

869-
if (!Tools.AskSave())
870+
SaveConfig(); // TODO: Handle failure.
871+
872+
if (!CloseGame())
870873
{
871874
closingArgs.Cancel = true;
872875
return;
873876
}
874-
875877
Tools.Close();
876-
FileWriteResult saveResult = MovieSession.StopMovie();
877-
if (saveResult.IsError)
878-
{
879-
if (!this.ModalMessageBox2(
880-
caption: "Quit anyway?",
881-
icon: EMsgBoxIcon.Question,
882-
text: "The currently playing movie could not be saved. Continue and quit anyway? All unsaved changes will be lost."))
883-
{
884-
closingArgs.Cancel = true;
885-
return;
886-
}
887-
}
888-
889-
CloseGame();
890-
SaveConfig();
891878
}
892879

893880
private readonly bool _suppressSyncSettingsWarning;
@@ -2248,7 +2235,7 @@ private void LoadRomFromRecent(string rom)
22482235

22492236
if (!LoadRom(romPath, new LoadRomArgs(ioa), out var failureIsFromAskSave))
22502237
{
2251-
if (failureIsFromAskSave) AddOnScreenMessage("ROM loading cancelled; a tool had unsaved changes");
2238+
if (failureIsFromAskSave) AddOnScreenMessage("ROM loading cancelled due to unsaved changes");
22522239
else if (ioa is OpenAdvanced_LibretroNoGame || File.Exists(romPath)) AddOnScreenMessage("ROM loading failed");
22532240
else Config.RecentRoms.HandleLoadError(this, romPath, rom);
22542241
}
@@ -3748,6 +3735,12 @@ public bool LoadRom(string path, LoadRomArgs args, out bool failureIsFromAskSave
37483735
private bool LoadRomInternal(string path, LoadRomArgs args, out bool failureIsFromAskSave)
37493736
{
37503737
failureIsFromAskSave = false;
3738+
if (!CloseGame())
3739+
{
3740+
failureIsFromAskSave = true;
3741+
return false;
3742+
}
3743+
37513744
if (path == null)
37523745
throw new ArgumentNullException(nameof(path));
37533746
if (args == null)
@@ -3775,12 +3768,6 @@ private bool LoadRomInternal(string path, LoadRomArgs args, out bool failureIsFr
37753768
// it is then up to the core itself to override its own local DeterministicEmulation setting
37763769
bool deterministic = args.Deterministic ?? MovieSession.NewMovieQueued;
37773770

3778-
if (!Tools.AskSave())
3779-
{
3780-
failureIsFromAskSave = true;
3781-
return false;
3782-
}
3783-
37843771
var loader = new RomLoader(Config, this)
37853772
{
37863773
ChooseArchive = LoadArchiveChooser,
@@ -3795,8 +3782,6 @@ private bool LoadRomInternal(string path, LoadRomArgs args, out bool failureIsFr
37953782
loader.OnLoadSettings += CoreSettings;
37963783
loader.OnLoadSyncSettings += CoreSyncSettings;
37973784

3798-
CloseGame();
3799-
38003785
var nextComm = CreateCoreComm();
38013786

38023787
IOpenAdvanced ioa = args.OpenAdvanced;
@@ -4085,8 +4070,32 @@ private void CommitCoreSettingsToConfig()
40854070
/// This closes the game but does not set things up for using the client with the new null emulator.
40864071
/// This method should only be called (outside of <see cref="CloseRom(bool)"/>) if the caller is about to load a new game with no user interaction between close and load.
40874072
/// </summary>
4088-
private void CloseGame(bool clearSram = false)
4073+
/// <returns>True if the game was closed. False if the user cancelled due to unsaved changes.</returns>
4074+
private bool CloseGame(bool clearSram = false)
40894075
{
4076+
CommitCoreSettingsToConfig(); // Must happen before stopping the movie, since it checks for active movie.
4077+
4078+
if (!Tools.AskSave())
4079+
{
4080+
return false;
4081+
}
4082+
// There is a cheats tool, but cheats can be active while the "cheats tool" is not. And have auto-save option.
4083+
CheatList.SaveOnClose();
4084+
4085+
GameIsClosing = true;
4086+
FileWriteResult saveResult = MovieSession.StopMovie();
4087+
GameIsClosing = false;
4088+
if (saveResult.IsError)
4089+
{
4090+
if (!this.ModalMessageBox2(
4091+
caption: "Quit anyway?",
4092+
icon: EMsgBoxIcon.Question,
4093+
text: "The currently playing movie could not be saved. Continue and quit anyway? All unsaved changes will be lost."))
4094+
{
4095+
return false;
4096+
}
4097+
}
4098+
40904099
if (clearSram)
40914100
{
40924101
var path = Config.PathEntries.SaveRamAbsolutePath(Game, MovieSession.Movie);
@@ -4110,7 +4119,7 @@ private void CloseGame(bool clearSram = false)
41104119
EMsgBoxIcon.Error);
41114120

41124121
if (result is false) break;
4113-
if (result is null) return;
4122+
if (result is null) return false;
41144123
}
41154124
}
41164125

@@ -4124,29 +4133,24 @@ private void CloseGame(bool clearSram = false)
41244133
$"Failed to auto-save state. Do you want to try again?\n\nError details:\n{stateSaveResult.UserFriendlyErrorMessage()}\n{stateSaveResult.Exception.Message}",
41254134
"IOError while writing savestate",
41264135
EMsgBoxIcon.Error);
4127-
if (tryAgain == null) return;
4136+
if (tryAgain == null) return false;
41284137
}
41294138
} while (tryAgain == true);
41304139

41314140
StopAv();
41324141

4133-
CommitCoreSettingsToConfig();
41344142
DisableRewind();
41354143

4136-
if (MovieSession.Movie.IsActive()) // Note: this must be called after CommitCoreSettingsToConfig() because it checks if we have an active movie.
4137-
{
4138-
StopMovie();
4139-
}
4140-
41414144
RA?.Stop();
41424145

4143-
CheatList.SaveOnClose();
41444146
Emulator.Dispose();
41454147
Emulator = new NullEmulator();
41464148
Game = GameInfo.NullInstance;
41474149
InputManager.SyncControls(Emulator, MovieSession, Config);
41484150
RewireSound();
41494151
RebootStatusBarIcon.Visible = false;
4152+
4153+
return true;
41504154
}
41514155

41524156
private FileWriteResult AutoSaveStateIfConfigured()

0 commit comments

Comments
 (0)