Skip to content

Commit 629df12

Browse files
committed
Handle errors when saving config file.
1 parent 1e57b74 commit 629df12

File tree

4 files changed

+37
-30
lines changed

4 files changed

+37
-30
lines changed

src/BizHawk.Client.Common/config/ConfigService.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,14 @@ public static bool IsFromSameVersion(string filepath, out string msg)
104104
return config ?? new T();
105105
}
106106

107-
public static void Save(string filepath, object config)
107+
public static FileWriteResult Save(string filepath, object config)
108108
{
109-
var file = new FileInfo(filepath);
110-
try
109+
return FileWriter.Write(filepath, (fs) =>
111110
{
112-
using var writer = file.CreateText();
111+
using var writer = new StreamWriter(fs);
113112
var w = new JsonTextWriter(writer) { Formatting = Formatting.Indented };
114113
Serializer.Serialize(w, config);
115-
}
116-
catch
117-
{
118-
/* Eat it */
119-
}
114+
});
120115
}
121116

122117
// movie 1.0 header stuff

src/BizHawk.Client.EmuHawk/MainForm.Events.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,15 @@ private void HkOverInputMenuItem_Click(object sender, EventArgs e)
976976

977977
private void SaveConfigMenuItem_Click(object sender, EventArgs e)
978978
{
979-
SaveConfig();
980-
AddOnScreenMessage("Saved settings");
979+
FileWriteResult result = SaveConfig();
980+
if (result.IsError)
981+
{
982+
this.ErrorMessageBox(result);
983+
}
984+
else
985+
{
986+
AddOnScreenMessage("Saved settings");
987+
}
981988
}
982989

983990
private void SaveConfigAsMenuItem_Click(object sender, EventArgs e)
@@ -989,8 +996,15 @@ private void SaveConfigAsMenuItem_Click(object sender, EventArgs e)
989996
initFileName: file);
990997
if (result is not null)
991998
{
992-
SaveConfig(result);
993-
AddOnScreenMessage("Copied settings");
999+
FileWriteResult saveResult = SaveConfig(result);
1000+
if (saveResult.IsError)
1001+
{
1002+
this.ErrorMessageBox(saveResult);
1003+
}
1004+
else
1005+
{
1006+
AddOnScreenMessage("Copied settings");
1007+
}
9941008
}
9951009
}
9961010

src/BizHawk.Client.EmuHawk/MainForm.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,12 @@ private void CheckMayCloseAndCleanup(object/*?*/ closingSender, CancelEventArgs
867867
StopAv();
868868
}
869869

870-
SaveConfig(); // TODO: Handle failure.
870+
TryAgainResult configSaveResult = this.DoWithTryAgainBox(() => SaveConfig(), "Failed to save config file.");
871+
if (configSaveResult == TryAgainResult.Canceled)
872+
{
873+
closingArgs.Cancel = true;
874+
return;
875+
}
871876

872877
if (!CloseGame())
873878
{
@@ -2525,7 +2530,7 @@ public ISettingsAdapter GetSettingsAdapterForLoadedCore<T>()
25252530
public SettingsAdapter GetSettingsAdapterForLoadedCoreUntyped()
25262531
=> new(Emulator, static () => true, HandlePutCoreSettings, MayPutCoreSyncSettings, HandlePutCoreSyncSettings);
25272532

2528-
private void SaveConfig(string path = "")
2533+
private FileWriteResult SaveConfig(string path = "")
25292534
{
25302535
if (Config.SaveWindowPosition)
25312536
{
@@ -2551,7 +2556,7 @@ private void SaveConfig(string path = "")
25512556
}
25522557

25532558
CommitCoreSettingsToConfig();
2554-
ConfigService.Save(path, Config);
2559+
return ConfigService.Save(path, Config);
25552560
}
25562561

25572562
private void ToggleFps()
@@ -4050,29 +4055,18 @@ private bool CloseGame(bool clearSram = false)
40504055
{
40514056
CommitCoreSettingsToConfig(); // Must happen before stopping the movie, since it checks for active movie.
40524057

4053-
GameIsClosing = true;
40544058
if (!Tools.AskSave())
40554059
{
4056-
GameIsClosing = false;
40574060
return false;
40584061
}
40594062
// There is a cheats tool, but cheats can be active while the "cheats tool" is not. And have auto-save option.
40604063
TryAgainResult cheatSaveResult = this.DoWithTryAgainBox(CheatList.SaveOnClose, "Failed to save cheats.");
4061-
if (cheatSaveResult == TryAgainResult.Canceled)
4062-
{
4063-
GameIsClosing = false;
4064-
return false;
4065-
}
4064+
if (cheatSaveResult == TryAgainResult.Canceled) return false;
40664065

40674066
// If TAStudio is open, we already asked about saving the movie.
4068-
if (Tools.IsLoaded<TAStudio>())
4069-
{
4070-
GameIsClosing = false;
4071-
}
4072-
else
4067+
if (!Tools.IsLoaded<TAStudio>())
40734068
{
40744069
TryAgainResult saveMovieResult = this.DoWithTryAgainBox(() => MovieSession.StopMovie(), "Failed to save movie.");
4075-
GameIsClosing = false;
40764070
if (saveMovieResult == TryAgainResult.Canceled) return false;
40774071
}
40784072

src/BizHawk.Client.EmuHawk/config/ControllerConfig.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,11 @@ private void ButtonSaveDefaults_Click(object sender, EventArgs e)
475475

476476
SaveToDefaults(cd);
477477

478-
ConfigService.Save(Config.ControlDefaultPath, cd);
478+
FileWriteResult saveResult = ConfigService.Save(Config.ControlDefaultPath, cd);
479+
if (saveResult.IsError)
480+
{
481+
this.ErrorMessageBox(saveResult);
482+
}
479483
}
480484
}
481485

0 commit comments

Comments
 (0)