Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: WIP refactor of lowest level CAC layer #168

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Runtime/AvatarCreator/AuthManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ReadyPlayerMe.AvatarCreator
public static class AuthManager
{
private const string TAG = nameof(AuthManager);
private static readonly AuthenticationRequests AuthenticationRequests;
private static readonly AuthApi AuthApi;
private static UserSession userSession;
public static UserSession UserSession => userSession;

Expand All @@ -25,12 +25,12 @@ public static class AuthManager

static AuthManager()
{
AuthenticationRequests = new AuthenticationRequests(CoreSettingsHandler.CoreSettings.Subdomain);
AuthApi = new AuthApi(CoreSettingsHandler.CoreSettings.Subdomain);
}

public static async Task LoginAsAnonymous()
{
userSession = await AuthenticationRequests.LoginAsAnonymous();
userSession = await AuthApi.LoginAsAnonymous();
IsSignedInAnonymously = true;
}

Expand All @@ -43,14 +43,14 @@ public static void SetUser(UserSession session)

public static async void SendEmailCode(string email)
{
await AuthenticationRequests.SendCodeToEmail(email, userSession.Id);
await AuthApi.SendCodeToEmail(email, userSession.Id);
}

public static async Task<bool> LoginWithCode(string otp)
{
try
{
userSession = await AuthenticationRequests.LoginWithCode(otp);
userSession = await AuthApi.LoginWithCode(otp);
IsSignedIn = true;
OnSignedIn?.Invoke(userSession);
return true;
Expand All @@ -64,15 +64,15 @@ public static async Task<bool> LoginWithCode(string otp)

public static async void Signup(string email)
{
await AuthenticationRequests.Signup(email, userSession.Id);
await AuthApi.Signup(email, userSession.Id);
}

public static async Task RefreshToken()
{
(string, string) newTokens;
try
{
newTokens = await AuthenticationRequests.RefreshToken(userSession.Token, userSession.RefreshToken);
newTokens = await AuthApi.RefreshToken(userSession.Token, userSession.RefreshToken);
}
catch (Exception e)
{
Expand Down
20 changes: 10 additions & 10 deletions Runtime/AvatarCreator/AvatarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AvatarManager : IDisposable
{
private const string TAG = nameof(AvatarManager);
private readonly BodyType bodyType;
private readonly AvatarAPIRequests avatarAPIRequests;
private readonly AvatarApi avatarApi;
private readonly string avatarConfigParameters;
private readonly InCreatorAvatarLoader inCreatorAvatarLoader;
private readonly CancellationTokenSource ctxSource;
Expand All @@ -38,7 +38,7 @@ public AvatarManager(BodyType bodyType, AvatarConfig avatarConfig = null, Cancel

ctxSource = CancellationTokenSource.CreateLinkedTokenSource(token);
inCreatorAvatarLoader = new InCreatorAvatarLoader();
avatarAPIRequests = new AvatarAPIRequests(ctxSource.Token);
avatarApi = new AvatarApi(ctxSource.Token);
}

/// <summary>
Expand All @@ -51,7 +51,7 @@ public AvatarManager(BodyType bodyType, AvatarConfig avatarConfig = null, Cancel
GameObject avatar = null;
try
{
avatarProperties = await avatarAPIRequests.CreateNewAvatar(avatarProperties);
avatarProperties = await avatarApi.CreateNewAvatar(avatarProperties);
gender = avatarProperties.Gender;
if (ctxSource.IsCancellationRequested)
{
Expand Down Expand Up @@ -81,7 +81,7 @@ public AvatarManager(BodyType bodyType, AvatarConfig avatarConfig = null, Cancel
var avatarProperties = new AvatarProperties();
try
{
avatarProperties = await avatarAPIRequests.CreateFromTemplateAvatar(
avatarProperties = await avatarApi.CreateFromTemplateAvatar(
id,
CoreSettingsHandler.CoreSettings.Subdomain,
bodyType
Expand Down Expand Up @@ -118,7 +118,7 @@ public async void PrecompileAvatar(string id, PrecompileData precompileData)
}
try
{
await avatarAPIRequests.PrecompileAvatar(id, precompileData, avatarConfigParameters);
await avatarApi.PrecompileAvatar(id, precompileData, avatarConfigParameters);
}
catch (Exception e)
{
Expand All @@ -144,7 +144,7 @@ public async Task<GameObject> GetAvatar(string id, bool isPreview = false)
byte[] data;
try
{
data = await avatarAPIRequests.GetAvatar(avatarId, isPreview, avatarConfigParameters);
data = await avatarApi.GetAvatar(avatarId, isPreview, avatarConfigParameters);
}
catch (Exception e)
{
Expand Down Expand Up @@ -183,7 +183,7 @@ public async Task<GameObject> UpdateAsset(Category category, object assetId)
byte[] data;
try
{
data = await avatarAPIRequests.UpdateAvatar(avatarId, payload, avatarConfigParameters);
data = await avatarApi.UpdateAvatar(avatarId, payload, avatarConfigParameters);
}
catch (Exception e)
{
Expand All @@ -204,7 +204,7 @@ public async Task<ColorPalette[]> LoadAvatarColors()
ColorPalette[] colors = null;
try
{
colors = await avatarAPIRequests.GetAllAvatarColors(avatarId);
colors = await avatarApi.GetAllAvatarColors(avatarId);
}
catch (Exception e)
{
Expand All @@ -221,7 +221,7 @@ public async Task<string> Save()
{
try
{
await avatarAPIRequests.SaveAvatar(avatarId);
await avatarApi.SaveAvatar(avatarId);
}
catch (Exception e)
{
Expand All @@ -239,7 +239,7 @@ public async void Delete(bool isDraft)
{
try
{
await avatarAPIRequests.DeleteAvatar(avatarId, isDraft);
await avatarApi.DeleteAvatar(avatarId, isDraft);
}
catch (Exception e)
{
Expand Down
3 changes: 3 additions & 0 deletions Runtime/AvatarCreator/Data/Avatars.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Runtime/AvatarCreator/Data/Avatars/Avatar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ReadyPlayerMe.AvatarCreator.Avatars
{
public class Avatar
{
public string Id { get; set; }

//TODO: ADD MORE PROPERTIES HERE
}
}
3 changes: 3 additions & 0 deletions Runtime/AvatarCreator/Data/Avatars/Avatar.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Runtime/AvatarCreator/Data/Avatars/AvatarTemplatesResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;

namespace ReadyPlayerMe.AvatarCreator.Avatars
{
public class AvatarTemplateResponse
{
public List<AvatarTemplate> Data { get; set; }
}

public class AvatarTemplate
{
public string Id { get; set; }

public string Gender { get; set; }

public string ImageUrl { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ReadyPlayerMe.AvatarCreator.Avatars
{
public class CreateDraftAvatarResponse
{
public Avatar Data { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Runtime/AvatarCreator/Data/Avatars/UserAvatarResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace ReadyPlayerMe.AvatarCreator.Avatars
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of class output of using point 1.

{
public class UserAvatarResponse
{
public List<UserAvatar> Data { get; set; }
}

public class UserAvatar
{
public string Partner { get; set; }

public string Id { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 1 addition & 11 deletions Runtime/AvatarCreator/Data/Endpoints/AvatarEndpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
{
public abstract class AvatarEndpoints : Endpoints
{
private const string AVATAR_API_V2_ENDPOINT = API_V2_ENDPOINT + "avatars";
private const string AVATAR_API_V2_ENDPOINT = API_V2_BASE_URL + "avatars";
private const string AVATAR_API_V1_ENDPOINT = API_V1_ENDPOINT + "avatars";
private const string MODELS_URL_PREFIX = "https://models.readyplayer.me";

public static string GetColorEndpoint(string avatarId)
{
return $"{AVATAR_API_V2_ENDPOINT}/{avatarId}/colors?type=skin,beard,hair,eyebrow";
}

public static string GetAvatarPublicUrl(string avatarId)
{
return $"{MODELS_URL_PREFIX}/{avatarId}.glb";
Expand Down Expand Up @@ -67,11 +62,6 @@ public static string GetUpdateAvatarEndpoint(string avatarId, string parameters)
return $"{AVATAR_API_V2_ENDPOINT}/{avatarId}?responseType=glb&{parameters}";
}

public static string GetSaveAvatarEndpoint(string avatarId)
{
return $"{AVATAR_API_V2_ENDPOINT}/{avatarId}";
}

public static string GetDeleteAvatarEndpoint(string avatarId, bool isDraft)
{
var draft = isDraft ? "draft" : "";
Expand Down
4 changes: 2 additions & 2 deletions Runtime/AvatarCreator/Data/Endpoints/Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Endpoints
{
protected const string API_SUBDOMAIN_ENDPOINT = "https://{0}.readyplayer.me/api{1}";
protected const string API_V2_ENDPOINT = "https://api.readyplayer.me/v2/";
protected const string API_V1_ENDPOINT = "https://api.readyplayer.me/v1/";
public const string API_V2_BASE_URL = "https://api.readyplayer.me/v2/";
public const string API_V1_BASE_URL = "https://api.readyplayer.me/v1/";
}
}
8 changes: 4 additions & 4 deletions Runtime/AvatarCreator/PartnerAssetsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ public class PartnerAssetsManager : IDisposable
private const string EYE_MASK_SIZE_SIZE = "?w=256";
private const string ASSET_ICON_SIZE = "?w=64";

private readonly PartnerAssetsRequests partnerAssetsRequests;
private readonly AssetsApi assetsApi;

private Dictionary<Category, List<PartnerAsset>> assetsByCategory;
public Action<string> OnError { get; set; }

public PartnerAssetsManager()
{
partnerAssetsRequests = new PartnerAssetsRequests(CoreSettingsHandler.CoreSettings.AppId);
assetsApi = new AssetsApi(CoreSettingsHandler.CoreSettings.AppId);
assetsByCategory = new Dictionary<Category, List<PartnerAsset>>();
}

public async Task<Dictionary<Category,List<PartnerAsset>>> GetAssets(BodyType bodyType, OutfitGender gender, CancellationToken token = default)
{
var startTime = Time.time;

var assets = await partnerAssetsRequests.Get(bodyType, gender, token);
var assets = await assetsApi.Get(bodyType, gender, token);

assetsByCategory = assets.GroupBy(asset => asset.Category).ToDictionary(
group => group.Key,
Expand Down Expand Up @@ -98,7 +98,7 @@ private async Task DownloadIcons(List<PartnerAsset> chunk, Action<string, Textur
{
var url = asset.Category == Category.EyeColor ? asset.Mask + EYE_MASK_SIZE_SIZE : asset.Icon + ASSET_ICON_SIZE;
var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token);
var iconTask = partnerAssetsRequests.GetAssetIcon(url, icon =>
var iconTask = ImageApi.DownloadImageAsync(url, icon =>
{
onDownload?.Invoke(asset.Id, icon);
},
Expand Down
8 changes: 4 additions & 4 deletions Runtime/AvatarCreator/TemplateFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ namespace ReadyPlayerMe.AvatarCreator
public class TemplateFetcher
{
private readonly CancellationToken ctx;
private readonly AvatarAPIRequests avatarAPIRequests;
private readonly AvatarApi avatarApi;
private readonly List<TemplateData> templates;

public TemplateFetcher(CancellationToken ctx = default)
{
this.ctx = ctx;
avatarAPIRequests = new AvatarAPIRequests(ctx);
avatarApi = new AvatarApi(ctx);
templates = new List<TemplateData>();
}

public async Task<List<TemplateData>> GetTemplates()
{
var avatarTemplates = await avatarAPIRequests.GetTemplates();
var avatarTemplates = await avatarApi.GetTemplates();
await GetAllTemplateRenders(avatarTemplates);
return templates;
}
Expand All @@ -37,7 +37,7 @@ private async Task GetAllTemplateRenders(IEnumerable<TemplateData> templateAvata

private async Task GetAvatarRender(TemplateData templateData)
{
templateData.Texture = await avatarAPIRequests.GetTemplateAvatarImage(templateData.ImageUrl);
templateData.Texture = await ImageApi.DownloadImageAsync(templateData.ImageUrl, ctx: ctx);
templates.Add(templateData);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,24 @@
using Newtonsoft.Json.Linq;
using ReadyPlayerMe.Core;
using UnityEngine;
using UnityEngine.Networking;

namespace ReadyPlayerMe.AvatarCreator
{
public class PartnerAssetsRequests
public class AssetsApi
{
private const string TAG = nameof(PartnerAssetsRequests);
private const string TAG = nameof(AssetsApi);
private const int LIMIT = 100;

private readonly AuthorizedRequest authorizedRequest;
private readonly string appId;
private readonly Dictionary<string, Texture> icons;

public PartnerAssetsRequests(string appId)
public AssetsApi(string appId)
{
authorizedRequest = new AuthorizedRequest();
icons = new Dictionary<string, Texture>();
this.appId = appId;
}

public async Task<PartnerAsset[]> Get(BodyType bodyType, OutfitGender gender, CancellationToken ctx = new CancellationToken())
public async Task<PartnerAsset[]> Get(BodyType bodyType, OutfitGender gender, CancellationToken ctx = default)
{
var assets = new HashSet<PartnerAsset>();
AssetData assetData;
Expand Down Expand Up @@ -122,33 +119,5 @@ public PartnerAssetsRequests(string appId)
Pagination = pagination
};
}

public async Task<Texture> GetAssetIcon(string url, Action<Texture> completed, CancellationToken ctx = new CancellationToken())
{
if (icons.ContainsKey(url))
{
completed?.Invoke(icons[url]);
return icons[url];
}

var downloadHandler = new DownloadHandlerTexture();
var response = await authorizedRequest.SendRequest<ResponseTexture>(new RequestData
{
Url = url,
Method = HttpMethod.GET,
DownloadHandler = downloadHandler
}, ctx: ctx);

response.ThrowIfError();

// This check is needed because the same url can be requested multiple times
if (!icons.ContainsKey(url))
{
icons.Add(url, response.Texture);
}

completed?.Invoke(response.Texture);
return response.Texture;
}
}
}
Loading
Loading