Skip to content

Commit

Permalink
Removed Newtonsoft.Json dependency in Preloader.
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Dec 25, 2024
1 parent 8415c32 commit 943ad1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
44 changes: 25 additions & 19 deletions ModTek.Preloader/Loader/Config.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
using System;
using System.IO;
using System.Text.RegularExpressions;
using ModTek.Common.Globals;
using ModTek.Common.Utils;
using Newtonsoft.Json;

namespace ModTek.Preloader.Loader;

internal class Config
{
#pragma warning disable CS0649
[JsonProperty]
internal readonly string _Description = $"When changing any of the listed settings, copy the relevant parts into `{FileUtils.GetRelativePath(Paths.PreloaderConfigFile)}`.";

[JsonProperty]
internal readonly string _Description = $"When changing any of the listed settings, copy the relevant parts into `{FileUtils.GetRelativePath(Paths.PreloaderConfigFile)}`. This is not a normal JSON format, each key-value pairs have to be on separate lines.";
internal readonly string Harmony12XLogChannelFilter_Description
= $"The channels to log into `{FileUtils.GetRelativePath(Paths.HarmonyLogFile)}`: None=0 Info=2 IL=4 Warn=8 Error=16 Debug=32 All=62";
[JsonProperty]
internal int Harmony12XLogChannelFilter = 26;

[JsonProperty]
internal readonly string Harmony12XFakeAssemblyLocationEnabled_Description =
"Make Assembly.Location return the path of the original non-shimmed assembly and not the path to the shimmed assembly. Workaround to some mods expecting their assembly to be in their respective mod directory.";
[JsonProperty]
internal bool Harmony12XFakeAssemblyLocationEnabled = true;
#pragma warning restore CS0649

internal static Config Instance = new();

Expand All @@ -32,23 +23,38 @@ private Config()
FileUtils.CreateDirectoryForFile(Paths.PreloaderConfigDefaultsFile);
File.WriteAllText(
Paths.PreloaderConfigDefaultsFile,
JsonConvert.SerializeObject(this, Formatting.Indented)
$$"""
{
"_Description": "{{_Description}}",
"Harmony12XLogChannelFilter_Description": "{{Harmony12XLogChannelFilter_Description}}",
"Harmony12XLogChannelFilter": {{Harmony12XLogChannelFilter}},
"Harmony12XFakeAssemblyLocationEnabled_Description": "{{Harmony12XFakeAssemblyLocationEnabled_Description}}",
"Harmony12XFakeAssemblyLocationEnabled": {{Harmony12XFakeAssemblyLocationEnabled}}
}
"""
);

if (File.Exists(Paths.PreloaderConfigFile))
{
try
{
var text = File.ReadAllText(Paths.PreloaderConfigFile);
JsonConvert.PopulateObject(
text,
this,
new JsonSerializerSettings
// avoid preloading a JSON library, so let's do some Regex instead
var regex = new Regex("""^\s*"([^"]+?)"\s*:\s*(.+?)\s*,?\s*$""");
foreach (Match match in regex.Matches(text))
{
var key = match.Groups[0].Value;
var value = match.Groups[1].Value;
switch (key)
{
ObjectCreationHandling = ObjectCreationHandling.Replace,
NullValueHandling = NullValueHandling.Ignore,
case "Harmony12XLogChannelFilter":
Harmony12XLogChannelFilter = int.Parse(value);
break;
case "Harmony12XFakeAssemblyLocationEnabled":
Harmony12XFakeAssemblyLocationEnabled = bool.Parse(value);
break;
}
);
}
}
catch (Exception e)
{
Expand Down
3 changes: 0 additions & 3 deletions ModTek.Preloader/ModTek.Preloader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@

<ItemGroup>
<PackageReference Include="HarmonyX" />
<Reference Include="Newtonsoft.Json">
<Private>False</Private>
</Reference>
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 943ad1d

Please sign in to comment.