Skip to content

Commit 0da893e

Browse files
committed
Handle errors when saving config file.
1 parent e5d8ad4 commit 0da893e

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
@@ -979,8 +979,15 @@ private void HkOverInputMenuItem_Click(object sender, EventArgs e)
979979

980980
private void SaveConfigMenuItem_Click(object sender, EventArgs e)
981981
{
982-
SaveConfig();
983-
AddOnScreenMessage("Saved settings");
982+
FileWriteResult result = SaveConfig();
983+
if (result.IsError)
984+
{
985+
this.ErrorMessageBox(result);
986+
}
987+
else
988+
{
989+
AddOnScreenMessage("Saved settings");
990+
}
984991
}
985992

986993
private void SaveConfigAsMenuItem_Click(object sender, EventArgs e)
@@ -992,8 +999,15 @@ private void SaveConfigAsMenuItem_Click(object sender, EventArgs e)
992999
initFileName: file);
9931000
if (result is not null)
9941001
{
995-
SaveConfig(result);
996-
AddOnScreenMessage("Copied settings");
1002+
FileWriteResult saveResult = SaveConfig(result);
1003+
if (saveResult.IsError)
1004+
{
1005+
this.ErrorMessageBox(saveResult);
1006+
}
1007+
else
1008+
{
1009+
AddOnScreenMessage("Copied settings");
1010+
}
9971011
}
9981012
}
9991013

src/BizHawk.Client.EmuHawk/MainForm.cs

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

878-
SaveConfig(); // TODO: Handle failure.
878+
TryAgainResult configSaveResult = this.DoWithTryAgainBox(() => SaveConfig(), "Failed to save config file.");
879+
if (configSaveResult == TryAgainResult.Canceled)
880+
{
881+
closingArgs.Cancel = true;
882+
return;
883+
}
879884

880885
if (!CloseGame())
881886
{
@@ -2417,7 +2422,7 @@ public ISettingsAdapter GetSettingsAdapterForLoadedCore<T>()
24172422
public SettingsAdapter GetSettingsAdapterForLoadedCoreUntyped()
24182423
=> new(Emulator, static () => true, HandlePutCoreSettings, MayPutCoreSyncSettings, HandlePutCoreSyncSettings);
24192424

2420-
private void SaveConfig(string path = "")
2425+
private FileWriteResult SaveConfig(string path = "")
24212426
{
24222427
if (Config.SaveWindowPosition)
24232428
{
@@ -2443,7 +2448,7 @@ private void SaveConfig(string path = "")
24432448
}
24442449

24452450
CommitCoreSettingsToConfig();
2446-
ConfigService.Save(path, Config);
2451+
return ConfigService.Save(path, Config);
24472452
}
24482453

24492454
private void ToggleFps()
@@ -3943,10 +3948,7 @@ private bool CloseGame(bool clearSram = false)
39433948
}
39443949
// There is a cheats tool, but cheats can be active while the "cheats tool" is not. And have auto-save option.
39453950
TryAgainResult cheatSaveResult = this.DoWithTryAgainBox(CheatList.SaveOnClose, "Failed to save cheats.");
3946-
if (cheatSaveResult == TryAgainResult.Canceled)
3947-
{
3948-
return false;
3949-
}
3951+
if (cheatSaveResult == TryAgainResult.Canceled) return false;
39503952

39513953
// If TAStudio is open, we already asked about saving the movie.
39523954
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)