mirror of
https://github.com/goatcorp/Dalamud.git
synced 2025-12-13 20:24:16 +01:00
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:
parent
08ca6e958c
commit
19d182546c
1 changed files with 41 additions and 2 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
using Dalamud.Storage;
|
using Dalamud.Storage;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
@ -116,9 +117,18 @@ public sealed class PluginConfigurations
|
||||||
/// <returns>Plugin Configuration.</returns>
|
/// <returns>Plugin Configuration.</returns>
|
||||||
public T LoadForType<T>(string pluginName) where T : IPluginConfiguration
|
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
|
// 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
|
// 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 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue