Skip to content

Commit

Permalink
Add json converters for dyes
Browse files Browse the repository at this point in the history
  • Loading branch information
sliekens committed Jan 25, 2025
1 parent 518e892 commit d747145
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
10 changes: 9 additions & 1 deletion GW2SDK.Tests/Features/Hero/Equipment/Dyes/Colors.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using GuildWars2.Tests.TestInfrastructure;
using System.Text.Json;
using GuildWars2.Hero.Crafting.Recipes;
using GuildWars2.Hero.Equipment.Dyes;
using GuildWars2.Tests.TestInfrastructure;

namespace GuildWars2.Tests.Features.Hero.Equipment.Dyes;

Expand Down Expand Up @@ -39,6 +42,11 @@ public async Task Can_be_listed()
var link = color.GetChatLink();
Assert.Equal(color.ItemId, link?.ItemId);
}

var json = JsonSerializer.Serialize(color);
var roundTrip = JsonSerializer.Deserialize<DyeColor>(json);
Assert.IsType(color.GetType(), roundTrip);
Assert.Equal(color, roundTrip);
}
);
}
Expand Down
2 changes: 2 additions & 0 deletions GW2SDK/Features/Hero/Equipment/Dyes/ColorInfo.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Drawing;
using System.Text.Json.Serialization;

namespace GuildWars2.Hero.Equipment.Dyes;

/// <summary>Information about how a dye color looks when applied to a material.</summary>
[PublicAPI]
[DataTransferObject]
[JsonConverter(typeof(ColorInfoJsonConverter))]
public sealed record ColorInfo
{
/// <summary>The brightness of the color.</summary>
Expand Down
52 changes: 52 additions & 0 deletions GW2SDK/Features/Hero/Equipment/Dyes/ColorInfoJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Drawing;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace GuildWars2.Hero.Equipment.Dyes;

internal sealed class ColorInfoJsonConverter : JsonConverter<ColorInfo>
{
public override ColorInfo Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
using var document = JsonDocument.ParseValue(ref reader);
return Read(document.RootElement);
}

public static ColorInfo Read(JsonElement json)
{
return new ColorInfo
{
Brightness = json.GetProperty("brightness").GetInt32(),
Contrast = json.GetProperty("contrast").GetDouble(),
Hue = json.GetProperty("hue").GetInt32(),
Saturation = json.GetProperty("saturation").GetDouble(),
Lightness = json.GetProperty("lightness").GetDouble(),
Rgb = Color.FromArgb(json.GetProperty("rgb").GetInt32())
};
}

public override void Write(
Utf8JsonWriter writer,
ColorInfo value,
JsonSerializerOptions options
)
{
Write(writer, value);
}

public static void Write(Utf8JsonWriter writer, ColorInfo value)
{
writer.WriteStartObject();
writer.WriteNumber("brightness", value.Brightness);
writer.WriteNumber("contrast", value.Contrast);
writer.WriteNumber("hue", value.Hue);
writer.WriteNumber("saturation", value.Saturation);
writer.WriteNumber("lightness", value.Lightness);
writer.WriteNumber("rgb", value.Rgb.ToArgb());
writer.WriteEndObject();
}
}
64 changes: 64 additions & 0 deletions GW2SDK/Features/Hero/Equipment/Dyes/DyeColor.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Drawing;
using System.Text.Json;
using System.Text.Json.Serialization;
using GuildWars2.Chat;
using GuildWars2.Json;

namespace GuildWars2.Hero.Equipment.Dyes;

/// <summary>Information about a dye color.</summary>
[PublicAPI]
[DataTransferObject]
[JsonConverter(typeof(DyeColorJsonConverter))]
public sealed record DyeColor
{
/// <summary>The color ID of Dye Remover.</summary>
Expand Down Expand Up @@ -51,3 +55,63 @@ public sealed record DyeColor
return ItemId.HasValue ? new ItemLink { ItemId = ItemId.Value } : null;
}
}

internal sealed class DyeColorJsonConverter : JsonConverter<DyeColor>
{
public override DyeColor? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
using var document = JsonDocument.ParseValue(ref reader);
var json = document.RootElement;
return new DyeColor
{
Id = json.GetProperty("id").GetInt32(),
Name = json.GetProperty("name").GetStringRequired(),
BaseRgb = Color.FromArgb(json.GetProperty("base_rgb").GetInt32()),
Cloth = ColorInfoJsonConverter.Read(json.GetProperty("cloth")),
Leather = ColorInfoJsonConverter.Read(json.GetProperty("leather")),
Metal = ColorInfoJsonConverter.Read(json.GetProperty("metal")),
Fur = json.GetProperty("fur").GetNullable(ColorInfoJsonConverter.Read),
ItemId = json.GetProperty("item_id").GetNullableInt32(),
Hue = json.GetProperty("hue").GetEnum<Hue>(),
Material = json.GetProperty("material").GetEnum<Material>(),
Set = json.GetProperty("set").GetEnum<ColorSet>()
};
}

public override void Write(Utf8JsonWriter writer, DyeColor value, JsonSerializerOptions options)
{
writer.WriteStartObject();
writer.WriteNumber("id", value.Id);
writer.WriteString("name", value.Name);
writer.WriteNumber("base_rgb", value.BaseRgb.ToArgb());
writer.WritePropertyName("cloth");
ColorInfoJsonConverter.Write(writer, value.Cloth);
writer.WritePropertyName("leather");
ColorInfoJsonConverter.Write(writer, value.Leather);
writer.WritePropertyName("metal");
ColorInfoJsonConverter.Write(writer, value.Metal);
writer.WritePropertyName("fur");
if (value.Fur is not null)
{
ColorInfoJsonConverter.Write(writer, value.Fur);
}
else
{
writer.WriteNullValue();
}

if (value.ItemId.HasValue)
{
writer.WriteNumber("item_id", value.ItemId.Value);
}
else
{
writer.WriteNull("item_id");
}

writer.WriteString("hue", value.Hue.ToString());
writer.WriteString("material", value.Material.ToString());
writer.WriteString("set", value.Set.ToString());
writer.WriteEndObject();
}
}

0 comments on commit d747145

Please sign in to comment.