Skip to content

Commit

Permalink
Play around with more mechdefs. Somehow custom component settings tri…
Browse files Browse the repository at this point in the history
…gger issues.
  • Loading branch information
CptMoore committed Dec 8, 2024
1 parent 9bad06c commit 65b2686
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@

namespace ModTek.Features.Profiler.Patches;

[HarmonyPatch(typeof(MechDef), nameof(MechDef.RefreshChassis))]
internal static class MechDef_RefreshChassis_Patch
{
internal static void Prefix(ref bool __runOriginal, MechDef __instance)
{
if (__instance.dataManager == null && __instance.Description == null)
{
__runOriginal = false;
}
}
}

[HarmonyPatch]
internal static class JSONSerializationUtility_FromJSON_Patch
{
private static string basePath;
private static bool testNewton = false;
private static bool testNewton = true;

public static bool Prepare()
{
Expand All @@ -29,7 +41,7 @@ public static bool Prepare()
}
Directory.CreateDirectory(basePath);
}
return ModTek.Enabled && ModTek.Config.ProfilerEnabled;
return ModTek.Enabled; // && ModTek.Config.ProfilerEnabled;
}

[HarmonyTargetMethods]
Expand Down Expand Up @@ -106,7 +118,7 @@ public static void Prefix(object target, string json, ref MyState __state)
{
__state = new MyState();

if (testNewton && target.GetType() == typeof(MechDef))
if (testNewton) // && target.GetType() == typeof(MechDef)
{
s_newton.Start();
try
Expand All @@ -119,7 +131,7 @@ public static void Prefix(object target, string json, ref MyState __state)
}
catch (Exception ex)
{
Log.Main.Error?.Log("Error populating " + target.GetType(), ex);
Log.Main.Error?.Log("Error N PopulateObject SerializeObject" + target.GetType(), ex);
}
finally
{
Expand All @@ -135,7 +147,7 @@ public static void Postfix(object target, string json, ref MyState __state)
{
s_stopwatch.AddMeasurement(__state.Tracker.End());

if (testNewton)
if (testNewton) // && target.GetType() == typeof(MechDef)
{
try
{
Expand All @@ -145,7 +157,7 @@ public static void Postfix(object target, string json, ref MyState __state)
}
catch (Exception ex)
{
Log.Main.Error?.Log("Error populating " + target.GetType(), ex);
Log.Main.Error?.Log("Error H SerializeObject " + target.GetType(), ex);
}
}
}
Expand Down
28 changes: 15 additions & 13 deletions ModTek/Util/HBSJsonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using HBS.Collections;
using HBS.Util;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

Expand Down Expand Up @@ -54,17 +55,20 @@ private static string FixHBSJsonCommas(string json)
return s_fixMissingCommasInJson.Replace(json, "$1,\n$2");
}

private static readonly JsonSerializerSettings s_jsonSerializerSettings = new()
{
ContractResolver = new FastJsonContractResolver(),
NullValueHandling = NullValueHandling.Ignore,
Converters = [
new TagSetConverter(),
new StringEnumConverter(),
],
};

// might work, only slightly tested
internal static string SerializeObject(object target)
{
return JsonConvert.SerializeObject(
target,
new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
ContractResolver = new FastJsonContractResolver()
}
);
return JsonConvert.SerializeObject(target, s_jsonSerializerSettings);
}

// does not work 100%, issues e.g. with MechDef and ChassisRefresh
Expand All @@ -76,11 +80,7 @@ internal static void PopulateObject(object target, string json)
var commentsStripped = JSONSerializationUtility.StripHBSCommentsFromJSON(json);
var commasAdded = FixHBSJsonCommas(commentsStripped);
var dictionariesFixed = FixStructures(commasAdded);
JsonConvert.PopulateObject(dictionariesFixed, target, new JsonSerializerSettings
{
ContractResolver = new FastJsonContractResolver(),
Converters = [new TagSetConverter()]
});
JsonConvert.PopulateObject(dictionariesFixed, target, s_jsonSerializerSettings);
}

private static string FixStructures(string json)
Expand Down Expand Up @@ -207,6 +207,8 @@ private static bool ExtractKeyValue(

private class FastJsonContractResolver : DefaultContractResolver
{


private readonly Dictionary<Type, List<MemberInfo>> _serializableMembersCache = new();
protected override List<MemberInfo> GetSerializableMembers(Type objectType)
{
Expand Down

0 comments on commit 65b2686

Please sign in to comment.