Always specify TypeNameHandling for types under Dalamud assembly (#1910)

LoadForType{T} intentionally does not specify TypeNameHandling which
seemingly is required for handling inheritance for nested properties.
Added a custom JsonConverter that will force TypeNameHandling=Objects
for types under Dalamud assembly, as a quick workaround for forcing font
related types to be correctly handled during deserialization.
This commit is contained in:
srkizer 2024-07-10 05:03:45 +09:00 committed by GitHub
parent 08ca6e958c
commit 19d182546c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,4 +1,5 @@
using System.IO;
using System.Reflection;
using Dalamud.Storage;
using Newtonsoft.Json;
@ -116,9 +117,18 @@ public sealed class PluginConfigurations
/// <returns>Plugin Configuration.</returns>
public T LoadForType<T>(string pluginName) where T : IPluginConfiguration
{
var path = this.GetConfigFile(pluginName);
if (this.GetConfigFile(pluginName) is not { Exists: true } path)
return default;
return !path.Exists ? default : JsonConvert.DeserializeObject<T>(File.ReadAllText(path.FullName));
return JsonConvert.DeserializeObject<T>(
File.ReadAllText(path.FullName),
new JsonSerializerSettings
{
Converters =
[
DalamudAssemblyTypeNameForcingJsonConverter.Instance,
],
});
// intentionally no type handling - it will break when updating a plugin at runtime
// and turns out to be unnecessary when we fully qualify the object type
@ -162,4 +172,33 @@ public sealed class PluginConfigurations
}
private DirectoryInfo GetDirectoryPath(string pluginName) => new(Path.Combine(this.configDirectory.FullName, pluginName));
private class DalamudAssemblyTypeNameForcingJsonConverter : JsonConverter
{
public static readonly DalamudAssemblyTypeNameForcingJsonConverter Instance = new();
private static readonly Assembly DalamudAssembly = typeof(DalamudAssemblyTypeNameForcingJsonConverter).Assembly;
private static readonly JsonSerializer TypeNameForcingJsonConverterDefaultSerializer = new()
{
TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple,
TypeNameHandling = TypeNameHandling.Objects,
};
private DalamudAssemblyTypeNameForcingJsonConverter()
{
}
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer) =>
TypeNameForcingJsonConverterDefaultSerializer.Serialize(writer, value);
public override object? ReadJson(
JsonReader reader,
Type objectType,
object? existingValue,
JsonSerializer serializer) =>
TypeNameForcingJsonConverterDefaultSerializer.Deserialize(reader, objectType);
public override bool CanConvert(Type objectType) => objectType.Assembly == DalamudAssembly;
}
}