mirror of
https://github.com/wabbajack-tools/wabbajack.git
synced 2024-08-30 18:42:17 +00:00
47 lines
1.1 KiB
C#
47 lines
1.1 KiB
C#
using System.Text;
|
|
using System.Text.Json;
|
|
using SteamKit2;
|
|
using Wabbajack.DTOs.JsonConverters;
|
|
|
|
namespace Wabbajack.Networking.Steam;
|
|
|
|
public class KeyValueTranslator
|
|
{
|
|
public static void Translate(Utf8JsonWriter wtr, List<KeyValue> kvs)
|
|
{
|
|
foreach (var kv in kvs)
|
|
{
|
|
wtr.WritePropertyName(kv.Name!);
|
|
if (kv.Value == null)
|
|
{
|
|
wtr.WriteStartObject();
|
|
Translate(wtr, kv.Children);
|
|
wtr.WriteEndObject();
|
|
}
|
|
else
|
|
{
|
|
wtr.WriteStringValue(kv.Value);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public static T Translate<T>(KeyValue kv, DTOSerializer dtos)
|
|
{
|
|
var ms = new MemoryStream();
|
|
var wtr = new Utf8JsonWriter(ms, new JsonWriterOptions()
|
|
{
|
|
Indented = true,
|
|
});
|
|
|
|
wtr.WriteStartObject();
|
|
Translate(wtr, kv.Children);
|
|
wtr.WriteEndObject();
|
|
wtr.Flush();
|
|
|
|
var str = Encoding.UTF8.GetString(ms.ToArray());
|
|
|
|
|
|
return JsonSerializer.Deserialize<T>(str, dtos.Options)!;
|
|
}
|
|
} |