Skip to content

Commit e383cc4

Browse files
committed
Handle errors when saving config file.
1 parent eb67b08 commit e383cc4

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
@@ -871,7 +871,12 @@ private void CheckMayCloseAndCleanup(object/*?*/ closingSender, CancelEventArgs
871871
StopAv();
872872
}
873873

874-
SaveConfig(); // TODO: Handle failure.
874+
TryAgainResult configSaveResult = this.DoWithTryAgainBox(() => SaveConfig(), "Failed to save config file.");
875+
if (configSaveResult == TryAgainResult.Canceled)
876+
{
877+
closingArgs.Cancel = true;
878+
return;
879+
}
875880

876881
if (!CloseGame())
877882
{
@@ -2399,7 +2404,7 @@ public ISettingsAdapter GetSettingsAdapterForLoadedCore<T>()
23992404
public SettingsAdapter GetSettingsAdapterForLoadedCoreUntyped()
24002405
=> new(Emulator, static () => true, HandlePutCoreSettings, MayPutCoreSyncSettings, HandlePutCoreSyncSettings);
24012406

2402-
private void SaveConfig(string path = "")
2407+
private FileWriteResult SaveConfig(string path = "")
24032408
{
24042409
if (Config.SaveWindowPosition)
24052410
{
@@ -2425,7 +2430,7 @@ private void SaveConfig(string path = "")
24252430
}
24262431

24272432
CommitCoreSettingsToConfig();
2428-
ConfigService.Save(path, Config);
2433+
return ConfigService.Save(path, Config);
24292434
}
24302435

24312436
private void ToggleFps()
@@ -3925,10 +3930,7 @@ private bool CloseGame(bool clearSram = false)
39253930
}
39263931
// There is a cheats tool, but cheats can be active while the "cheats tool" is not. And have auto-save option.
39273932
TryAgainResult cheatSaveResult = this.DoWithTryAgainBox(CheatList.SaveOnClose, "Failed to save cheats.");
3928-
if (cheatSaveResult == TryAgainResult.Canceled)
3929-
{
3930-
return false;
3931-
}
3933+
if (cheatSaveResult == TryAgainResult.Canceled) return false;
39323934

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