Skip to content

Commit

Permalink
Merge pull request #294 from Aragas/dev
Browse files Browse the repository at this point in the history
v5.0.0 #3
  • Loading branch information
Aragas authored Oct 27, 2022
2 parents 861278c + 046b839 commit 4e6b22c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
29 changes: 27 additions & 2 deletions src/MCM.Abstractions/Formats/BaseJsonSettingsFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
using MCM.Abstractions.Base;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;

namespace MCM.Implementation
{
Expand Down Expand Up @@ -51,9 +54,17 @@ protected bool TryLoadFromJson(ref BaseSettings settings, string content)
{
try
{
JsonConvert.PopulateObject(content, settings, JsonSerializerSettings);
if (!TryParse(content, out var jo))
return false;

using var reader = jo.CreateReader();
var serializer = JsonSerializer.CreateDefault(JsonSerializerSettings);
var settingsType = settings.GetType();
var settingsConverter = JsonSerializerSettings.Converters.OfType<BaseSettingsJsonConverter>().FirstOrDefault();
if (settingsConverter is not null && settingsConverter.CanConvert(settingsType))
settingsConverter.ReadJson(reader, settingsType, settings, serializer);
}
catch (JsonSerializationException e)
catch (JsonSerializationException)
{
return false;
}
Expand Down Expand Up @@ -122,5 +133,19 @@ protected void ClearSerializationProperties()
{
_existingObjects.Clear();
}

private static bool TryParse(string content, [NotNullWhen(true)] out JObject? jObject)
{
try
{
jObject = JObject.Parse(content);
return true;
}
catch (JsonReaderException)
{
jObject = null;
return false;
}
}
}
}
4 changes: 2 additions & 2 deletions tests/MCM.Tests/BaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ private static bool MockedGetModulesNames(ref string[] __result)
[OneTimeSetUp]
public void OneTimeSetUp()
{
_harmony.Patch(AccessTools2.Method(typeof(ButterLibSubModule).Assembly.GetType("Bannerlord.BUTR.Shared.Helpers.FSIOHelper"), "GetConfigPath"),
prefix: new HarmonyMethod(SymbolExtensions2.GetMethodInfo((string x) => MockedGetConfigsPath(ref x))));
//_harmony.Patch(AccessTools2.Method(typeof(ButterLibSubModule).Assembly.GetType("Bannerlord.BUTR.Shared.Helpers.FSIOHelper"), "GetConfigPath"),
// prefix: new HarmonyMethod(SymbolExtensions2.GetMethodInfo((string x) => MockedGetConfigsPath(ref x))));
//_harmony.Patch(SymbolExtensions2.GetMethodInfo(() => FSIOHelper.GetConfigPath()),
// prefix: new HarmonyMethod(SymbolExtensions2.GetMethodInfo((string x) => MockedGetConfigsPath(ref x))));
//_harmony.Patch(SymbolExtensions2.GetMethodInfo(() => ModuleInfoHelper.GetLoadedModules()),
Expand Down
2 changes: 1 addition & 1 deletion tests/MCM.Tests/MCM.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ItemGroup>
<PackageReference Include="nunit" Version="3.13.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.0-alpha-net7.4" />

<PackageReference Include="Bannerlord.ButterLib" Version="$(ButterLibVersion)" />
<PackageReference Include="Bannerlord.UIExtenderEx" Version="$(UIExtenderExVersion)" />
Expand Down

0 comments on commit 4e6b22c

Please sign in to comment.