Fix parsing via typetree of old Material assets

- Fixed parsing via typetree of Material assets from Unity version <5.6
This commit is contained in:
VaDiM 2024-11-02 01:59:43 +03:00
parent fca937e5e6
commit 316837dfdf
2 changed files with 54 additions and 1 deletions

View File

@ -515,7 +515,7 @@ namespace AssetStudio
var jsonOptions = new JsonSerializerOptions var jsonOptions = new JsonSerializerOptions
{ {
Converters = { new JsonConverterHelper.ByteArrayConverter(), new JsonConverterHelper.PPtrConverter() }, Converters = { new JsonConverterHelper.ByteArrayConverter(), new JsonConverterHelper.PPtrConverter(), new JsonConverterHelper.KVPConverter() },
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals, NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
PropertyNameCaseInsensitive = true, PropertyNameCaseInsensitive = true,
IncludeFields = true, IncludeFields = true,

View File

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace AssetStudio
{
public static partial class JsonConverterHelper
{
public class KVPConverter : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert)
{
if (!typeToConvert.IsGenericType)
return false;
var generic = typeToConvert.GetGenericTypeDefinition();
return generic == typeof(KeyValuePair<,>);
}
public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options)
{
var kvpArgs = type.GetGenericArguments();
return (JsonConverter)Activator.CreateInstance(typeof(KVPConverter<,>).MakeGenericType(kvpArgs));
}
}
private class KVPConverter<TKey, TValue> : JsonConverter<KeyValuePair<TKey, TValue>>
{
public override KeyValuePair<TKey, TValue> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
//startKvpObject
reader.Read(); //propName
reader.Read(); //keyType
var key = reader.TokenType == JsonTokenType.StartObject
? JsonSerializer.Deserialize<Dictionary<string, TKey>>(ref reader).Values.First()
: JsonSerializer.Deserialize<TKey>(ref reader);
reader.Read(); //propName
reader.Read(); //startObject
var value = JsonSerializer.Deserialize<TValue>(ref reader, options);
reader.Read(); //endKvpObject
return new KeyValuePair<TKey, TValue>(key, value);
}
public override void Write(Utf8JsonWriter writer, KeyValuePair<TKey, TValue> value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}
}