Skip to content

Commit d286074

Browse files
committed
Handle errors when saving config file.
1 parent ac00d94 commit d286074

File tree

4 files changed

+36
-21
lines changed

4 files changed

+36
-21
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
@@ -983,8 +983,15 @@ private void HkOverInputMenuItem_Click(object sender, EventArgs e)
983983

984984
private void SaveConfigMenuItem_Click(object sender, EventArgs e)
985985
{
986-
SaveConfig();
987-
AddOnScreenMessage("Saved settings");
986+
FileWriteResult result = SaveConfig();
987+
if (result.IsError)
988+
{
989+
this.ErrorMessageBox(result);
990+
}
991+
else
992+
{
993+
AddOnScreenMessage("Saved settings");
994+
}
988995
}
989996

990997
private void SaveConfigAsMenuItem_Click(object sender, EventArgs e)
@@ -996,8 +1003,15 @@ private void SaveConfigAsMenuItem_Click(object sender, EventArgs e)
9961003
initFileName: file);
9971004
if (result is not null)
9981005
{
999-
SaveConfig(result);
1000-
AddOnScreenMessage("Copied settings");
1006+
FileWriteResult saveResult = SaveConfig(result);
1007+
if (saveResult.IsError)
1008+
{
1009+
this.ErrorMessageBox(saveResult);
1010+
}
1011+
else
1012+
{
1013+
AddOnScreenMessage("Copied settings");
1014+
}
10011015
}
10021016
}
10031017

src/BizHawk.Client.EmuHawk/MainForm.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,12 @@ private void CheckMayCloseAndCleanup(object/*?*/ closingSender, CancelEventArgs
883883
StopAv();
884884
}
885885

886-
SaveConfig(); // TODO: Handle failure.
886+
TryAgainResult configSaveResult = this.DoWithTryAgainBox(() => SaveConfig(), "Failed to save config file.");
887+
if (configSaveResult == TryAgainResult.Canceled)
888+
{
889+
closingArgs.Cancel = true;
890+
return;
891+
}
887892

888893
if (!CloseGame())
889894
{
@@ -2377,7 +2382,7 @@ public ISettingsAdapter GetSettingsAdapterForLoadedCore<T>()
23772382
public SettingsAdapter GetSettingsAdapterForLoadedCoreUntyped()
23782383
=> new(Emulator, static () => true, HandlePutCoreSettings, MayPutCoreSyncSettings, HandlePutCoreSyncSettings);
23792384

2380-
private void SaveConfig(string path = "")
2385+
private FileWriteResult SaveConfig(string path = "")
23812386
{
23822387
if (Config.SaveWindowPosition)
23832388
{
@@ -2403,7 +2408,7 @@ private void SaveConfig(string path = "")
24032408
}
24042409

24052410
CommitCoreSettingsToConfig();
2406-
ConfigService.Save(path, Config);
2411+
return ConfigService.Save(path, Config);
24072412
}
24082413

24092414
private void ToggleFps()
@@ -3887,10 +3892,7 @@ private bool CloseGame(bool clearSram = false)
38873892
}
38883893
// There is a cheats tool, but cheats can be active while the "cheats tool" is not. And have auto-save option.
38893894
TryAgainResult cheatSaveResult = this.DoWithTryAgainBox(CheatList.SaveOnClose, "Failed to save cheats.");
3890-
if (cheatSaveResult == TryAgainResult.Canceled)
3891-
{
3892-
return false;
3893-
}
3895+
if (cheatSaveResult == TryAgainResult.Canceled) return false;
38943896

38953897
// If TAStudio is open, we already asked about saving the movie.
38963898
if (!Tools.IsLoaded<TAStudio>())

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

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

471471
SaveToDefaults(cd);
472472

473-
ConfigService.Save(Config.ControlDefaultPath, cd);
473+
FileWriteResult saveResult = ConfigService.Save(Config.ControlDefaultPath, cd);
474+
if (saveResult.IsError)
475+
{
476+
this.ErrorMessageBox(saveResult);
477+
}
474478
}
475479
}
476480

0 commit comments

Comments
 (0)