From 2f18ad5be108fc33689f9999a72e43644bb4363a Mon Sep 17 00:00:00 2001 From: nattthebear Date: Fri, 3 Jul 2020 05:54:01 -0400 Subject: [PATCH] Defer deserialization of core config That was simple. No config file change either. Doesn't seem to have a noticeable effect on startup speed, but it's not any slower, and we're more resilient to core changes now. --- src/BizHawk.Client.Common/config/Config.cs | 5 +- .../config/ConfigExtensions.cs | 120 ++++++++++++++---- .../config/ConfigService.cs | 2 +- 3 files changed, 100 insertions(+), 27 deletions(-) diff --git a/src/BizHawk.Client.Common/config/Config.cs b/src/BizHawk.Client.Common/config/Config.cs index 1375300b08..f7b8465e8b 100644 --- a/src/BizHawk.Client.Common/config/Config.cs +++ b/src/BizHawk.Client.Common/config/Config.cs @@ -4,6 +4,7 @@ using System.IO; using BizHawk.Common; using BizHawk.Common.PathExtensions; using BizHawk.Emulation.Cores; +using Newtonsoft.Json.Linq; namespace BizHawk.Client.Common { @@ -252,8 +253,8 @@ namespace BizHawk.Client.Common public bool VideoWriterAudioSync { get; set; } = true; // Emulation core settings - public Dictionary CoreSettings { get; set; } = new Dictionary(); - public Dictionary CoreSyncSettings { get; set; } = new Dictionary(); + internal Dictionary CoreSettings { get; set; } = new Dictionary(); + internal Dictionary CoreSyncSettings { get; set; } = new Dictionary(); public Dictionary CommonToolSettings { get; set; } = new Dictionary(); public Dictionary> CustomToolSettings { get; set; } = new Dictionary>(); diff --git a/src/BizHawk.Client.Common/config/ConfigExtensions.cs b/src/BizHawk.Client.Common/config/ConfigExtensions.cs index f5532a2f49..264e97ea15 100644 --- a/src/BizHawk.Client.Common/config/ConfigExtensions.cs +++ b/src/BizHawk.Client.Common/config/ConfigExtensions.cs @@ -1,68 +1,140 @@ using System; using BizHawk.Emulation.Common; +using Newtonsoft.Json.Linq; namespace BizHawk.Client.Common { public static class ConfigExtensions { - public static object GetCoreSettings(this Config config, Type t) + private class TypeNameEncapsulator { - config.CoreSettings.TryGetValue(t.ToString(), out var ret); - return ret; + public object o; + } + private static JToken Serialize(object o) + { + var tne = new TypeNameEncapsulator { o = o }; + return JToken.FromObject(ConfigService.Serializer)["o"]; + } + private static object Deserialize(JToken j) + { + var jne = new JObject(new JProperty("o", j)); + try + { + return jne.ToObject(ConfigService.Serializer).o; + } + catch + { + // presumably some sort of config mismatch. Anywhere we can expose this usefully? + return null; + } } - public static object GetCoreSettings(this Config config) - where T : IEmulator + /// + /// Returns the core settings for a core + /// + /// + /// + /// null if no settings were saved, or there was an error deserializing + public static object GetCoreSettings(this Config config, Type coreType) { - return config.GetCoreSettings(typeof(T)); + config.CoreSettings.TryGetValue(coreType.ToString(), out var j); + return Deserialize(j); } - public static void PutCoreSettings(this Config config, object o, Type t) + /// + /// Returns the core settings for a core + /// + /// + /// + /// null if no settings were saved, or there was an error deserializing + public static object GetCoreSettings(this Config config) + where TCore : IEmulator + { + return config.GetCoreSettings(typeof(TCore)); + } + + /// + /// saves the core settings for a core + /// + /// + /// null to remove settings for that core instead + /// + public static void PutCoreSettings(this Config config, object o, Type coreType) { if (o != null) { - config.CoreSettings[t.ToString()] = o; + config.CoreSettings[coreType.ToString()] = Serialize(o); } else { - config.CoreSettings.Remove(t.ToString()); + config.CoreSettings.Remove(coreType.ToString()); } } - public static void PutCoreSettings(this Config config, object o) - where T : IEmulator + /// + /// saves the core settings for a core + /// + /// + /// null to remove settings for that core instead + /// + public static void PutCoreSettings(this Config config, object o) + where TCore : IEmulator { - config.PutCoreSettings(o, typeof(T)); + config.PutCoreSettings(o, typeof(TCore)); } - public static object GetCoreSyncSettings(this Config config) - where T : IEmulator + /// + /// Returns the core syncsettings for a core + /// + /// + /// + /// null if no settings were saved, or there was an error deserializing + public static object GetCoreSyncSettings(this Config config, Type coreType) { - return config.GetCoreSyncSettings(typeof(T)); + config.CoreSyncSettings.TryGetValue(coreType.ToString(), out var j); + return Deserialize(j); } - public static object GetCoreSyncSettings(this Config config, Type t) + /// + /// Returns the core syncsettings for a core + /// + /// + /// + /// null if no settings were saved, or there was an error deserializing + public static object GetCoreSyncSettings(this Config config) + where TCore : IEmulator { - config.CoreSyncSettings.TryGetValue(t.ToString(), out var ret); - return ret; + return config.GetCoreSyncSettings(typeof(TCore)); } - public static void PutCoreSyncSettings(this Config config, object o, Type t) + /// + /// saves the core syncsettings for a core + /// + /// + /// null to remove settings for that core instead + /// + public static void PutCoreSyncSettings(this Config config, object o, Type coreType) { if (o != null) { - config.CoreSyncSettings[t.ToString()] = o; + config.CoreSyncSettings[coreType.ToString()] = Serialize(o); } else { - config.CoreSyncSettings.Remove(t.ToString()); + config.CoreSyncSettings.Remove(coreType.ToString()); } } - public static void PutCoreSyncSettings(this Config config, object o) - where T : IEmulator + /// + /// saves the core syncsettings for a core + /// + /// + /// null to remove settings for that core instead + /// + public static void PutCoreSyncSettings(this Config config, object o) + where TCore : IEmulator { - config.PutCoreSyncSettings(o, typeof(T)); + config.PutCoreSyncSettings(o, typeof(TCore)); } } } diff --git a/src/BizHawk.Client.Common/config/ConfigService.cs b/src/BizHawk.Client.Common/config/ConfigService.cs index ec27761971..392ace453f 100644 --- a/src/BizHawk.Client.Common/config/ConfigService.cs +++ b/src/BizHawk.Client.Common/config/ConfigService.cs @@ -10,7 +10,7 @@ namespace BizHawk.Client.Common { public static class ConfigService { - private static readonly JsonSerializer Serializer; + internal static readonly JsonSerializer Serializer; static ConfigService() {