Skip to content

Commit

Permalink
Merge pull request #237 from BUTR/dev
Browse files Browse the repository at this point in the history
v2.3.5
  • Loading branch information
Aragas authored Nov 2, 2022
2 parents 32c1329 + 395fc7c commit 2621eb8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build/common.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!--Development Variables-->
<PropertyGroup>
<!--Module Version-->
<Version>2.3.4</Version>
<Version>2.3.5</Version>
<!--Harmony Version-->
<HarmonyVersion>2.2.2</HarmonyVersion>
<HarmonyExtensionsVersion>3.1.0.67</HarmonyExtensionsVersion>
Expand Down
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
---------------------------------------------------------------------------------------------------
Version: 2.3.5
Game Versions: v1.0.0
* Fixed CTD when failing to serialize ButterLib's settings
---------------------------------------------------------------------------------------------------
Version: 2.3.4
Game Versions: v1.0.0
* Another potential CTD fix
Expand Down
42 changes: 35 additions & 7 deletions src/Bannerlord.ButterLib/Options/JsonButterLibOptionsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,15 @@ public JsonButterLibOptionsModel()
if (file.Directory is null)
throw new NullReferenceException($"Directory for path {Path} is null!");

if (!file.Directory.Exists)
file.Directory.Create();
try
{
if (!file.Directory.Exists)
file.Directory.Create();
}
catch
{
return;
}

if (file.Exists)
{
Expand All @@ -41,17 +48,38 @@ public JsonButterLibOptionsModel()
}
catch (Exception e) when (e is JsonSerializationException)
{
using var fs = file.OpenWrite();
using var sw = new StreamWriter(fs);
sw.WriteLine(JsonConvert.SerializeObject(this));
TryOverwrite(file, this);
}
catch
{
return;
}
}
else
{
using var fs = file.Create();
TryCreate(file, this);
}
}

private static void TryCreate(FileInfo file, JsonButterLibOptionsModel model)
{
try
{
using var sw = file.CreateText();
sw.WriteLine(JsonConvert.SerializeObject(model));
}
catch { }
}

private static void TryOverwrite(FileInfo file, JsonButterLibOptionsModel model)
{
try
{
using var fs = file.OpenWrite();
using var sw = new StreamWriter(fs);
sw.WriteLine(JsonConvert.SerializeObject(this));
sw.WriteLine(JsonConvert.SerializeObject(model));
}
catch { }
}
}
}

0 comments on commit 2621eb8

Please sign in to comment.