1
0
Fork 0
forked from aly/qstbak
qstbak/Questionable/Questionable.Controller.Conditions/StopConditionConverter.cs
2026-08-17 20:29:32 +10:00

160 lines
3.6 KiB
C#

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Questionable.Model.Questing;
namespace Questionable.Controller.Conditions;
internal sealed class StopConditionConverter : JsonConverter<StopCondition>
{
public override StopCondition? ReadJson(JsonReader reader, Type objectType, StopCondition? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
try
{
return ReadJsonCore(reader);
}
catch (Exception)
{
return null;
}
}
private static StopCondition? ReadJsonCore(JsonReader reader)
{
JObject jObject = JObject.Load(reader);
StopCondition stopCondition;
switch (jObject["Type"]?.Value<string>())
{
case "QuestComplete":
{
ElementId elementId2 = DeserializeElementId(jObject["QuestId"]);
if ((object)elementId2 != null)
{
stopCondition = new QuestCompleteCondition
{
QuestId = elementId2,
Sequence = jObject["Sequence"]?.Value<int?>()
};
break;
}
goto default;
}
case "QuestAccept":
{
ElementId elementId = DeserializeElementId(jObject["QuestId"]);
if ((object)elementId != null)
{
stopCondition = new QuestAcceptCondition
{
QuestId = elementId
};
break;
}
goto default;
}
case "Level":
stopCondition = new LevelCondition
{
TargetLevel = (jObject["TargetLevel"]?.Value<int>() ?? 50)
};
break;
case "GlobalSequence":
stopCondition = new GlobalSequenceCondition
{
TargetSequence = (jObject["TargetSequence"]?.Value<int>() ?? 1)
};
break;
case "InventoryFull":
stopCondition = new InventoryFullCondition();
break;
case "BeforeDuty":
stopCondition = new BeforeDutyCondition();
break;
case "GilThreshold":
stopCondition = new GilThresholdCondition
{
MinGil = (jObject["MinGil"]?.Value<int>() ?? 1000)
};
break;
default:
stopCondition = null;
break;
}
StopCondition stopCondition2 = stopCondition;
if (stopCondition2 != null)
{
int? num = jObject["Mode"]?.Value<int>();
if (num.HasValue)
{
int valueOrDefault = num.GetValueOrDefault();
stopCondition2.Mode = (EStopConditionMode)valueOrDefault;
}
}
return stopCondition2;
}
public override void WriteJson(JsonWriter writer, StopCondition? value, JsonSerializer serializer)
{
if (value == null || value.IsSession)
{
writer.WriteNull();
return;
}
JObject jObject = new JObject
{
["Type"] = value.Type.ToString(),
["Mode"] = (int)value.Mode
};
if (!(value is QuestCompleteCondition questCompleteCondition))
{
if (!(value is QuestAcceptCondition questAcceptCondition))
{
if (!(value is LevelCondition levelCondition))
{
if (!(value is GlobalSequenceCondition globalSequenceCondition))
{
if (value is GilThresholdCondition gilThresholdCondition)
{
jObject["MinGil"] = gilThresholdCondition.MinGil;
}
}
else
{
jObject["TargetSequence"] = globalSequenceCondition.TargetSequence;
}
}
else
{
jObject["TargetLevel"] = levelCondition.TargetLevel;
}
}
else
{
jObject["QuestId"] = questAcceptCondition.QuestId.ToString();
}
}
else
{
jObject["QuestId"] = questCompleteCondition.QuestId.ToString();
if (questCompleteCondition.Sequence.HasValue)
{
jObject["Sequence"] = questCompleteCondition.Sequence;
}
}
jObject.WriteTo(writer);
}
private static ElementId? DeserializeElementId(JToken? token)
{
string text = token?.Value<string>();
if (text == null)
{
return null;
}
if (!ElementId.TryFromString(text, out ElementId elementId))
{
return null;
}
return elementId;
}
}