From f817e0860996198ff06659a05c1bd55c95b71f34 Mon Sep 17 00:00:00 2001 From: Raigo Kovask Date: Mon, 17 Jun 2024 15:19:29 +0300 Subject: [PATCH] feat: fetch bodytype specific templates and refactor code a bit --- .../Scripts/WebRequests/AvatarAPIRequests.cs | 20 ++----------------- .../Core/Scripts/Extensions/EnumExtensions.cs | 11 ++++++++++ .../JsonConverters/BodyTypeConverter.cs | 13 +----------- 3 files changed, 14 insertions(+), 30 deletions(-) diff --git a/Runtime/AvatarCreator/Scripts/WebRequests/AvatarAPIRequests.cs b/Runtime/AvatarCreator/Scripts/WebRequests/AvatarAPIRequests.cs index e43e6915..3cb47d69 100644 --- a/Runtime/AvatarCreator/Scripts/WebRequests/AvatarAPIRequests.cs +++ b/Runtime/AvatarCreator/Scripts/WebRequests/AvatarAPIRequests.cs @@ -20,9 +20,6 @@ public class AvatarAPIRequests private const string RPM_AVATAR_V2_BASE_URL = Env.RPM_API_V2_BASE_URL + "avatars"; private const string COLOR_PARAMETERS = "colors?type=skin,beard,hair,eyebrow"; - private const string FULL_BODY = "fullbody"; - private const string FULL_BODY_XR = "fullbody-xr"; - private const string HALF_BODY = "halfbody"; private const string PARTNER = "partner"; private const string DATA = "data"; private const string BODY_TYPE = "bodyType"; @@ -66,7 +63,7 @@ public async Task> GetAvatarTemplates() var response = await authorizedRequest.SendRequest( new RequestData { - Url = $"{RPM_AVATAR_V2_BASE_URL}/templates", + Url = $"{RPM_AVATAR_V2_BASE_URL}/templates?{BODY_TYPE}={CoreSettingsHandler.CoreSettings.BodyType.GetDescription()}", Method = HttpMethod.GET }, ctx @@ -83,7 +80,7 @@ public async Task CreateFromTemplateAvatar(string templateId, var payloadData = new Dictionary { { nameof(partner), partner }, - { nameof(bodyType), GetBodyTypeValue(bodyType) } + { nameof(bodyType), bodyType.GetDescription() } }; var payload = AuthDataConverter.CreatePayload(payloadData); @@ -105,19 +102,6 @@ public async Task CreateFromTemplateAvatar(string templateId, return JsonConvert.DeserializeObject(data); } - private static string GetBodyTypeValue(BodyType bodyType) - { - - var body = bodyType switch - { - BodyType.FullBody => FULL_BODY, - BodyType.FullBodyXR => FULL_BODY_XR, - BodyType.HalfBody => HALF_BODY, - _ => throw new ArgumentOutOfRangeException(nameof(bodyType), bodyType, null) - }; - return body; - } - public async Task GetAvatarColors(string avatarId, AssetType assetType = AssetType.None) { ValidateAvatarId(avatarId); diff --git a/Runtime/Core/Scripts/Extensions/EnumExtensions.cs b/Runtime/Core/Scripts/Extensions/EnumExtensions.cs index c81e504a..80a95060 100644 --- a/Runtime/Core/Scripts/Extensions/EnumExtensions.cs +++ b/Runtime/Core/Scripts/Extensions/EnumExtensions.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel; +using System.Reflection; namespace ReadyPlayerMe.Core { @@ -24,5 +25,15 @@ public static T GetValueFromDescription(string description) where T : Enum return default; } + + public static string GetDescription(this Enum value) + { + var field = value.GetType().GetField(value.ToString()); + + var attribute = field.GetCustomAttribute(); + + return attribute == null ? value.ToString() : attribute.Description; + } + } } diff --git a/Runtime/Core/Scripts/JsonConverters/BodyTypeConverter.cs b/Runtime/Core/Scripts/JsonConverters/BodyTypeConverter.cs index 002d0fd5..e10404fa 100644 --- a/Runtime/Core/Scripts/JsonConverters/BodyTypeConverter.cs +++ b/Runtime/Core/Scripts/JsonConverters/BodyTypeConverter.cs @@ -8,10 +8,6 @@ namespace ReadyPlayerMe.Core [Preserve] public class BodyTypeConverter : JsonConverter { - private const string FULL_BODY = "fullbody"; - private const string FULL_BODY_XR = "fullbody-xr"; - private const string HALF_BODY = "halfbody"; - public override void WriteJson(JsonWriter writer, BodyType value, JsonSerializer serializer) { serializer.Serialize(writer, value.ToString().ToLower()); @@ -23,14 +19,7 @@ public override BodyType ReadJson(JsonReader reader, Type objectType, BodyType e var token = JToken.Load(reader); if (token.Type == JTokenType.String) { - - return token.ToString() switch - { - FULL_BODY => BodyType.FullBody, - HALF_BODY => BodyType.HalfBody, - FULL_BODY_XR => BodyType.FullBodyXR, - _ => BodyType.None - }; + return EnumExtensions.GetValueFromDescription(token.ToString()); } throw new JsonSerializationException("Expected string value, instead found: " + token.Type);