2024-07-20 17:09:51 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
|
2024-08-02 16:30:21 +00:00
|
|
|
|
namespace Questionable.Model.Questing.Converter;
|
2024-07-20 17:09:51 +00:00
|
|
|
|
|
|
|
|
|
public sealed class QuestWorkConfigConverter : JsonConverter<QuestWorkValue>
|
|
|
|
|
{
|
|
|
|
|
public override QuestWorkValue Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (reader.TokenType == JsonTokenType.Number)
|
|
|
|
|
return new QuestWorkValue(reader.GetByte());
|
|
|
|
|
|
|
|
|
|
if (reader.TokenType != JsonTokenType.StartObject)
|
|
|
|
|
throw new JsonException();
|
|
|
|
|
|
|
|
|
|
byte? high = null, low = null;
|
2024-08-01 01:22:01 +00:00
|
|
|
|
EQuestWorkMode mode = EQuestWorkMode.Bitwise;
|
2024-07-20 17:09:51 +00:00
|
|
|
|
while (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
switch (reader.TokenType)
|
|
|
|
|
{
|
|
|
|
|
case JsonTokenType.PropertyName:
|
|
|
|
|
string? propertyName = reader.GetString();
|
|
|
|
|
if (propertyName == null || !reader.Read())
|
|
|
|
|
throw new JsonException();
|
|
|
|
|
|
|
|
|
|
switch (propertyName)
|
|
|
|
|
{
|
|
|
|
|
case nameof(QuestWorkValue.High):
|
|
|
|
|
high = reader.GetByte();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case nameof(QuestWorkValue.Low):
|
|
|
|
|
low = reader.GetByte();
|
|
|
|
|
break;
|
|
|
|
|
|
2024-08-01 01:22:01 +00:00
|
|
|
|
case nameof(QuestWorkValue.Mode):
|
|
|
|
|
mode = new QuestWorkModeConverter().Read(ref reader, typeof(EQuestWorkMode), options);
|
|
|
|
|
break;
|
|
|
|
|
|
2024-07-20 17:09:51 +00:00
|
|
|
|
default:
|
|
|
|
|
throw new JsonException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case JsonTokenType.EndObject:
|
2024-08-01 01:22:01 +00:00
|
|
|
|
return new QuestWorkValue(high, low, mode);
|
2024-07-20 17:09:51 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new JsonException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new JsonException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Write(Utf8JsonWriter writer, QuestWorkValue value, JsonSerializerOptions options)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
}
|