Skip to content

Commit f3c6d01

Browse files
committed
Added JsonContext for AOT support & cleanup
1 parent 2b2e4b3 commit f3c6d01

File tree

9 files changed

+28
-32
lines changed

9 files changed

+28
-32
lines changed

Mojang.Api.Skins.Test/Cache/LiteDBCacheTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public async Task Clear_ClearsCacheAsync()
111111
foreach (var directory in directories)
112112
{
113113
Assert.Empty(Directory.GetFiles(directory));
114-
}
114+
}
115115
}
116116

117117
[Fact]

Mojang.Api.Skins.Test/SkinsClientTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414
namespace Mojang.Api.Skins.Test;
1515
public class SkinsClientTests
1616
{
17-
private ProfileProperties _profileProperties = new ProfileProperties
17+
private readonly ProfileProperties _profileProperties = new()
1818
{
1919
Name = "FakePlayer",
2020
Id = Guid.NewGuid(),
21-
Properties = new ProfileProperty[] {
21+
Properties = [
2222
new ProfileProperty() {
2323
Name = "textures",
2424
Value = "ewogICJ0aW1lc3RhbXAiIDogMTcwNTI0MDM4NTE1MSwKICAicHJvZmlsZUlkIiA6ICJlZGM2MzE5YjQ5NjM0ZDhmYmZkNTI1N2QxNzg5N2I0NSIsCiAgInByb2ZpbGVOYW1lIiA6ICJDd2lzdFNpbHYzciIsCiAgInRleHR1cmVzIiA6IHsKICAgICJTS0lOIiA6IHsKICAgICAgInVybCIgOiAiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS83ZTNjNWQ1MTM4MTE1YTRmNjBjYTRmMGMwMTEyZjk3NmFmYmJjZjk3MGNmY2Y5ZWM1NDk0NDMyNTQ1Njg0NWIxIgogICAgfSwKICAgICJDQVBFIiA6IHsKICAgICAgInVybCIgOiAiaHR0cDovL3RleHR1cmVzLm1pbmVjcmFmdC5uZXQvdGV4dHVyZS8yMzQwYzBlMDNkZDI0YTExYjE1YThiMzNjMmE3ZTllMzJhYmIyMDUxYjI0ODFkMGJhN2RlZmQ2MzVjYTdhOTMzIgogICAgfQogIH0KfQ=="
2525
}
26-
}
26+
]
2727
};
28-
private SkinData _defaultSkinData = new SkinData { SkinType = SkinType.Slim, TextureBytes = new byte[] { 1, 2, 3, 4, 5, 6 }, TextureSize = new Size(64, 64) };
29-
private CapeData _defaultCapeData = new CapeData { CapeName = "TestCape", TextureBytes = new byte[] { 1, 2, 3 }, TextureSize = new Size(64, 32) };
28+
private readonly SkinData _defaultSkinData = new() { SkinType = SkinType.Slim, TextureBytes = [1, 2, 3, 4, 5, 6], TextureSize = new Size(64, 64) };
29+
private readonly CapeData _defaultCapeData = new() { CapeName = "TestCape", TextureBytes = [1, 2, 3], TextureSize = new Size(64, 32) };
3030

3131

3232
private readonly Mock<IProfileInformationRepository> _profileInformationRepositoryMock;
@@ -47,7 +47,7 @@ public SkinsClientTests()
4747
_textureCropperMock = new Mock<ITextureCropper>();
4848
_modernSkinConverterMock = new Mock<IModernSkinConverter>();
4949
_skinTypeIdentifierMock = new Mock<ISkinTypeIdentifier>();
50-
}
50+
}
5151

5252
[Fact]
5353
public async Task GetAsync_ByName_ReturnsValidPlayerData()
@@ -140,5 +140,5 @@ public void GetFromFile_WithCape_ReturnsValidPlayerData()
140140
_profileTexturesRepositoryMock.Verify(repo => repo.GetCapeLocal(_defaultCapeData.TextureBytes), Times.Once());
141141
}
142142

143-
private SkinsClient CreateClient() => new SkinsClient(_profileInformationRepositoryMock.Object, _profilePropertiesRepositoryMock.Object, _profileTexturesRepositoryMock.Object, _capeTextureIdentifierMock.Object, _imageUtilitiesMock.Object, _textureCropperMock.Object, _modernSkinConverterMock.Object, _skinTypeIdentifierMock.Object);
143+
private SkinsClient CreateClient() => new(_profileInformationRepositoryMock.Object, _profilePropertiesRepositoryMock.Object, _profileTexturesRepositoryMock.Object, _capeTextureIdentifierMock.Object, _imageUtilitiesMock.Object, _textureCropperMock.Object, _modernSkinConverterMock.Object, _skinTypeIdentifierMock.Object);
144144
}

Mojang.Api.Skins/Cache/LiteDBCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void AddOrSet(string key, object value)
5858
}
5959

6060
return (T)result.Value;
61-
}
61+
}
6262

6363
public async Task CacheImageAsync(string key, byte[] data)
6464
{

Mojang.Api.Skins/ImageService/Identifier/Cape/CapeTextureIdentifier.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Mojang.Api.Skins.ImageService.General;
2-
using System.Runtime.InteropServices;
32

43
namespace Mojang.Api.Skins.ImageService.Identifier.Cape;
54
public sealed class CapeTextureIdentifier : ICapeTextureIdentifier

Mojang.Api.Skins/JsonContext.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Mojang.Api.Skins.Data.MojangApi;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Mojang.Api.Skins;
5+
[JsonSourceGenerationOptions(WriteIndented = true)]
6+
[JsonSerializable(typeof(ApiErrorResponse))]
7+
[JsonSerializable(typeof(ProfileInformation))]
8+
[JsonSerializable(typeof(ProfileProperties))]
9+
[JsonSerializable(typeof(ProfileTextureInformation))]
10+
internal partial class JsonContext : JsonSerializerContext
11+
{
12+
}

Mojang.Api.Skins/Repository/MinecraftProfileInformation/ProfileInformationRepository.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Mojang.Api.Skins.Data;
22
using Mojang.Api.Skins.Data.MojangApi;
33
using System.Net.Http.Json;
4-
using System.Text.Json;
54

65
namespace Mojang.Api.Skins.Repository.MinecraftProfileInformation;
76
public sealed class ProfileInformationRepository : IProfileInformationRepository
@@ -47,12 +46,12 @@ public async Task<ProfileInformation> Get(string playerName)
4746

4847
if (!response.IsSuccessStatusCode)
4948
{
50-
var errorResponse = await response.Content.ReadFromJsonAsync<ApiErrorResponse>().ConfigureAwait(false); ;
49+
var errorResponse = await response.Content.ReadFromJsonAsync(JsonContext.Default.ApiErrorResponse).ConfigureAwait(false);
5150
var errorMessage = errorResponse != null ? errorResponse.ErrorMessage : "Unknown error occurred.";
5251
throw new HttpRequestException(errorMessage);
5352
}
5453

55-
var profileInformation = await response.Content.ReadFromJsonAsync<ProfileInformation>().ConfigureAwait(false);
54+
var profileInformation = await response.Content.ReadFromJsonAsync(JsonContext.Default.ProfileInformation).ConfigureAwait(false);
5655

5756
lock (_lock)
5857
Options.Cache?.AddOrSet(cacheKey, profileInformation!.Id);

Mojang.Api.Skins/Repository/MinecraftProfileProperties/ProfilePropertiesRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public async Task<ProfileProperties> Get(Guid playerUUID)
4444
var response = await _httpClient.GetAsync(string.Format(MinecraftProfilePropertiesURL, playerUUID.ToString("N"))).ConfigureAwait(false);
4545
if (!response.IsSuccessStatusCode)
4646
{
47-
var errorResponse = await response.Content.ReadFromJsonAsync<ApiErrorResponse>();
47+
var errorResponse = await response.Content.ReadFromJsonAsync(JsonContext.Default.ApiErrorResponse).ConfigureAwait(false);
4848
var errorMessage = errorResponse != null ? errorResponse.ErrorMessage : "Unknown error occurred.";
4949
throw new HttpRequestException(errorMessage);
5050
}
5151

52-
var profileProperties = await response.Content.ReadFromJsonAsync<ProfileProperties>();
52+
var profileProperties = await response.Content.ReadFromJsonAsync(JsonContext.Default.ProfileProperties).ConfigureAwait(false);
5353

5454
lock (_lock)
5555
Options.Cache?.AddOrSet(cacheKey, profileProperties!);

Mojang.Api.Skins/Repository/MinecraftProfileTextures/ProfileTexturesRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private ProfileTextureInformation DecodeProfileTextureInformation(ProfilePropert
221221
var base64Bytes = Convert.FromBase64String(texturesProperty.Value);
222222
var decodedString = System.Text.Encoding.UTF8.GetString(base64Bytes);
223223

224-
var profileTextureInformation = JsonSerializer.Deserialize<ProfileTextureInformation>(decodedString);
224+
var profileTextureInformation = JsonSerializer.Deserialize(decodedString, JsonContext.Default.ProfileTextureInformation);
225225

226226
return profileTextureInformation ?? throw new InvalidOperationException("Failed to deserialize the profile texture information.");
227227
}

Mojang.Api.Skins/Utilities/HttpClientExtension.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
using Mojang.Api.Skins.Data.MojangApi;
2-
using Polly;
1+
using Polly;
32
using Polly.Extensions.Http;
4-
using System.Diagnostics;
53
using System.Net.Http.Headers;
6-
using System.Text.Json;
74

85
namespace Mojang.Api.Skins.Utilities;
96
internal static class HttpClientExtension
@@ -26,18 +23,7 @@ internal static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
2623
if (message.IsSuccessStatusCode)
2724
return false;
2825

29-
return true;
30-
//var contentAsString = message.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
31-
//Debug.WriteLine(contentAsString);
32-
//try
33-
//{
34-
// var apiErrorResponse = JsonSerializer.Deserialize<ApiErrorResponse>(contentAsString);
35-
// return apiErrorResponse == null;
36-
//}
37-
//catch
38-
//{
39-
// return true;
40-
//}
26+
return true;
4127
})
4228
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
4329
}

0 commit comments

Comments
 (0)