using System; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Questionable.Model.Questing; namespace Questionable.Controller.Conditions; internal sealed class StopConditionConverter : JsonConverter { 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()) { case "QuestComplete": { ElementId elementId2 = DeserializeElementId(jObject["QuestId"]); if ((object)elementId2 != null) { stopCondition = new QuestCompleteCondition { QuestId = elementId2, Sequence = jObject["Sequence"]?.Value() }; 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() ?? 50) }; break; case "GlobalSequence": stopCondition = new GlobalSequenceCondition { TargetSequence = (jObject["TargetSequence"]?.Value() ?? 1) }; break; case "InventoryFull": stopCondition = new InventoryFullCondition(); break; case "BeforeDuty": stopCondition = new BeforeDutyCondition(); break; case "GilThreshold": stopCondition = new GilThresholdCondition { MinGil = (jObject["MinGil"]?.Value() ?? 1000) }; break; default: stopCondition = null; break; } StopCondition stopCondition2 = stopCondition; if (stopCondition2 != null) { int? num = jObject["Mode"]?.Value(); 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(); if (text == null) { return null; } if (!ElementId.TryFromString(text, out ElementId elementId)) { return null; } return elementId; } }