diff --git a/Anamnesis/Files/CharacterFile.cs b/Anamnesis/Files/CharacterFile.cs index 129765e0..251c51ca 100644 --- a/Anamnesis/Files/CharacterFile.cs +++ b/Anamnesis/Files/CharacterFile.cs @@ -589,6 +589,11 @@ public GlassesSave(GlassesMemory from) this.GlassesId = from.GlassesId; } + public GlassesSave(ushort glassesId) + { + this.GlassesId = glassesId; + } + public ushort GlassesId { get; set; } public void Write(GlassesMemory? glasses) diff --git a/Anamnesis/Serialization/Converters/GlassesSaveConverter.cs b/Anamnesis/Serialization/Converters/GlassesSaveConverter.cs new file mode 100644 index 00000000..32108b8a --- /dev/null +++ b/Anamnesis/Serialization/Converters/GlassesSaveConverter.cs @@ -0,0 +1,48 @@ +// © Anamnesis. +// Licensed under the MIT license. + +namespace Anamnesis.Serialization.Converters; + +using Anamnesis.Services; +using System.Text.Json; +using System; +using System.Text.Json.Serialization; +using Serilog; +using static Anamnesis.Files.CharacterFile; + +internal class GlassesSaveConverter : JsonConverter +{ + public override GlassesSave Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + ushort glassesId = 0; + + try + { + // Legacy chara files will have Glasses serialized as "Glasses" : #. + // Current chara files have it as an object with a GlassesId property. + if (reader.TokenType == JsonTokenType.Number) + { + glassesId = reader.GetUInt16(); + } + else if (reader.TokenType == JsonTokenType.StartObject) + { + JsonDocument jsonDoc = JsonDocument.ParseValue(ref reader); + if (jsonDoc.RootElement.TryGetProperty("GlassesId", out JsonElement glassesIdEl)) + { + glassesId = glassesIdEl.GetUInt16(); + } + } + } + catch(Exception ex) + { + Log.Error(ex, "Error reading glasses from file."); + } + + return new GlassesSave(GameDataService.Glasses.Get((byte)glassesId).GlassesId); + } + + public override void Write(Utf8JsonWriter writer, GlassesSave value, JsonSerializerOptions options) + { + JsonSerializer.Serialize(writer, value); + } +} diff --git a/Anamnesis/Serialization/Converters/IGlassesConverter.cs b/Anamnesis/Serialization/Converters/IGlassesConverter.cs deleted file mode 100644 index b78165a3..00000000 --- a/Anamnesis/Serialization/Converters/IGlassesConverter.cs +++ /dev/null @@ -1,29 +0,0 @@ -// © Anamnesis. -// Licensed under the MIT license. - -namespace Anamnesis.Serialization.Converters; - -using Anamnesis.Services; -using System.Text.Json; -using System; -using System.Text.Json.Serialization; -using Anamnesis.GameData.Excel; - -internal class IGlassesConverter : JsonConverter -{ - public override Glasses Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - string? str = reader.GetString(); - - if (str == null) - throw new Exception($"Invalid serialized glasses value"); - - uint glassesID = uint.Parse(str); - return GameDataService.Glasses.Get(glassesID); - } - - public override void Write(Utf8JsonWriter writer, Glasses value, JsonSerializerOptions options) - { - writer.WriteStringValue($"{value.GlassesId}"); - } -} diff --git a/Anamnesis/Serialization/SerializerService.cs b/Anamnesis/Serialization/SerializerService.cs index e3e7467e..d0e497e9 100644 --- a/Anamnesis/Serialization/SerializerService.cs +++ b/Anamnesis/Serialization/SerializerService.cs @@ -37,7 +37,7 @@ static SerializerService() Options.Converters.Add(new MediaColorConverter()); Options.Converters.Add(new PointConverter()); Options.Converters.Add(new VersionConverter()); - Options.Converters.Add(new IGlassesConverter()); + Options.Converters.Add(new GlassesSaveConverter()); } public static string Serialize(object obj)