mirror of
https://github.com/aelurum/AssetStudio.git
synced 2026-11-15 19:28:48 -05:00
Fixes and improvements for asset parsing via typetree
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AssetStudio
|
||||
{
|
||||
public static partial class JsonConverterHelper
|
||||
{
|
||||
public class ByteArrayConverter : JsonConverter<byte[]>
|
||||
{
|
||||
public override byte[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return reader.TokenType == JsonTokenType.StartArray
|
||||
? JsonSerializer.Deserialize<List<byte>>(ref reader).ToArray() //JsonArray to ByteArray
|
||||
: JsonSerializer.Deserialize<byte[]>(ref reader);
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, byte[] value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteBase64StringValue(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AssetStudio
|
||||
{
|
||||
public static partial class JsonConverterHelper
|
||||
{
|
||||
public class FloatConverter : JsonConverter<float>
|
||||
{
|
||||
public override float Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
return JsonSerializer.Deserialize<float>(ref reader, new JsonSerializerOptions
|
||||
{
|
||||
NumberHandling = options.NumberHandling
|
||||
});
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, float value, JsonSerializerOptions options)
|
||||
{
|
||||
if (float.IsNaN(value) || float.IsInfinity(value))
|
||||
{
|
||||
if (options.NumberHandling == JsonNumberHandling.AllowNamedFloatingPointLiterals)
|
||||
{
|
||||
writer.WriteStringValue($"{value.ToString(CultureInfo.InvariantCulture)}");
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteStringValue(JsonSerializer.Serialize(value));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
writer.WriteNumberValue((decimal)value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AssetStudio
|
||||
{
|
||||
public static partial class JsonConverterHelper
|
||||
{
|
||||
public class PPtrConverter : JsonConverterFactory
|
||||
{
|
||||
public static SerializedFile AssetsFile;
|
||||
|
||||
public override bool CanConvert(Type typeToConvert)
|
||||
{
|
||||
if (!typeToConvert.IsGenericType)
|
||||
return false;
|
||||
|
||||
var generic = typeToConvert.GetGenericTypeDefinition();
|
||||
return generic == typeof(PPtr<>);
|
||||
}
|
||||
|
||||
public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options)
|
||||
{
|
||||
var elementType = type.GetGenericArguments()[0];
|
||||
var converter = (JsonConverter)Activator.CreateInstance(typeof(PPtrConverter<>).MakeGenericType(elementType), AssetsFile);
|
||||
return converter;
|
||||
}
|
||||
}
|
||||
|
||||
private class PPtrConverter<T> : JsonConverter<PPtr<T>> where T : Object
|
||||
{
|
||||
private readonly SerializedFile _assetsFile;
|
||||
|
||||
public PPtrConverter(SerializedFile assetsFile)
|
||||
{
|
||||
_assetsFile = assetsFile;
|
||||
}
|
||||
|
||||
public override PPtr<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var pptrObj = JsonSerializer.Deserialize<PPtr<T>>(ref reader);
|
||||
pptrObj.SetAssetsFile(_assetsFile);
|
||||
return pptrObj;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, PPtr<T> value, JsonSerializerOptions options)
|
||||
{
|
||||
JsonSerializer.Serialize(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace AssetStudio
|
||||
{
|
||||
public static partial class JsonConverterHelper
|
||||
{
|
||||
public class RenderDataMapConverter : JsonConverter<Dictionary<KeyValuePair<Guid, long>, SpriteAtlasData>>
|
||||
{
|
||||
public override Dictionary<KeyValuePair<Guid, long>, SpriteAtlasData> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var dataArray = JsonSerializer.Deserialize<KeyValuePair<JsonObject, SpriteAtlasData>[]>(ref reader, options);
|
||||
var renderDataMap = new Dictionary<KeyValuePair<Guid, long>, SpriteAtlasData>(dataArray.Length);
|
||||
foreach (var kvp in dataArray)
|
||||
{
|
||||
var jsonFirst = kvp.Key["first"];
|
||||
var first = jsonFirst.Deserialize<GUID>(options).Convert();
|
||||
var second = (long) kvp.Key["second"];
|
||||
renderDataMap.Add(new KeyValuePair<Guid, long>(first, second), kvp.Value);
|
||||
}
|
||||
return renderDataMap;
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Dictionary<KeyValuePair<Guid, long>, SpriteAtlasData> value, JsonSerializerOptions options)
|
||||
{
|
||||
var jsonDict = new Dictionary<string, SpriteAtlasData>();
|
||||
foreach (var kv in value)
|
||||
{
|
||||
var strKey = $"{kv.Key.Key}, {kv.Key.Value}";
|
||||
jsonDict.Add(strKey, kv.Value);
|
||||
}
|
||||
var strValue = JsonSerializer.Serialize(jsonDict, options).Replace(" ", " ");
|
||||
writer.WriteRawValue(strValue);
|
||||
}
|
||||
}
|
||||
|
||||
private class GUID
|
||||
{
|
||||
[JsonPropertyName("data[0]")] public uint data0;
|
||||
[JsonPropertyName("data[1]")] public uint data1;
|
||||
[JsonPropertyName("data[2]")] public uint data2;
|
||||
[JsonPropertyName("data[3]")] public uint data3;
|
||||
|
||||
public Guid Convert()
|
||||
{
|
||||
var guidBytes = new byte[16];
|
||||
BitConverter.GetBytes(data0).CopyTo(guidBytes, 0);
|
||||
BitConverter.GetBytes(data1).CopyTo(guidBytes, 4);
|
||||
BitConverter.GetBytes(data2).CopyTo(guidBytes, 8);
|
||||
BitConverter.GetBytes(data3).CopyTo(guidBytes, 12);
|
||||
return new Guid(guidBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user