Skip to content

Commit

Permalink
Merge pull request #1390 from CaptainSticky/feature/brio-legacy-chara…
Browse files Browse the repository at this point in the history
…-glasses-converter

Added support for legacy facewear serialization in chara files.
  • Loading branch information
StoiaCode authored Sep 25, 2024
2 parents 0e91744 + 28e9ded commit aa2c1d1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
5 changes: 5 additions & 0 deletions Anamnesis/Files/CharacterFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions Anamnesis/Serialization/Converters/GlassesSaveConverter.cs
Original file line number Diff line number Diff line change
@@ -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<GlassesSave>
{
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);
}
}
29 changes: 0 additions & 29 deletions Anamnesis/Serialization/Converters/IGlassesConverter.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Anamnesis/Serialization/SerializerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit aa2c1d1

Please sign in to comment.