Skip to content

Commit

Permalink
Small CAC refactoring in prep for CAC 2.0 and wizardless flow (#151)
Browse files Browse the repository at this point in the history
<!-- Copy the TICKETID for this task from Jira and add it to the PR name
in brackets -->
<!-- PR name should look like: [TICKETID] My Pull Request -->

<!-- Add link for the ticket here editing the TICKETID-->

## [TICKETID](https://ready-player-me.atlassian.net/browse/TICKETID)

## Description

- This PR adds some minor changes which should add some extra
flexibility and help us in CAC 2.0. Please share your feedback though
and point out any potential floors in the updates. I will list some main
changes and the reasoning below.

**1. Added an extra return property to the CreateAvatar(AvatarProperties
avatarProperties) method (made it return a tuple same as
CreateAvatarFromTemplate.**
**Reasoning:** To create an avatar from a photo with gender prediction
enabled we need to also have the avatar properties, in particular we can
get the gender of the predicted avatar which is required to load/update
the avatar.

**2. Removed gender from AvatarManager constructor**
**Reasoning**: this makes AvatarManager easier to work with as you don't
need to create a new one each time you change genders. Also
CreateAvatar() function is passed avartarProperties (which can
optionally have gender predefined) and CreateAvatarFromTemplate gets the
gender from Template data. The only thing that may be useful in future
is to add a separate "SetGender" function but at the moment I don't
really see a use for it.

**3. Removed partner parameter from CreateAvatarFromTemplate()
function.**
**Reasoning:** Why would anybody ever want to pass a "partner" value
that is not the same as in their CoreSettings ? Doing so would probably
break API's anyways (due to non matching subdomain and API key
   
   

<!-- Fill the section below with Added, Updated and Removed information.
-->
<!-- If there is no item under one of the lists remove it's title. -->

<!-- Testability -->

## How to Test

- Mainly just test that current CAC sample still works (and it should)

<!-- Update your progress with the task here -->

## Checklist

-   [ ] Tests written or updated for the changes.
-   [ ] Documentation is updated.
-   [ ] Changelog is updated.

<!--- Remember to copy the Changes Section into the commit message when
you close the PR -->
  • Loading branch information
HarrisonHough authored Nov 14, 2023
1 parent 4b09422 commit f7bee7b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
22 changes: 10 additions & 12 deletions Runtime/AvatarCreator/AvatarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,22 @@ public class AvatarManager : IDisposable
{
private const string TAG = nameof(AvatarManager);
private readonly BodyType bodyType;
private readonly OutfitGender gender;
private readonly AvatarAPIRequests avatarAPIRequests;
private readonly string avatarConfigParameters;
private readonly InCreatorAvatarLoader inCreatorAvatarLoader;
private readonly CancellationTokenSource ctxSource;

private OutfitGender gender;
public Action<string> OnError { get; set; }

public string AvatarId => avatarId;
private string avatarId;

/// <param name="bodyType">Body type of avatar</param>
/// <param name="gender">Gender of avatar</param>
/// <param name="avatarConfig">Config for downloading preview avatar</param>
/// <param name="token">Cancellation token</param>
public AvatarManager(BodyType bodyType, OutfitGender gender, AvatarConfig avatarConfig = null, CancellationToken token = default)
public AvatarManager(BodyType bodyType, AvatarConfig avatarConfig = null, CancellationToken token = default)
{
this.bodyType = bodyType;
this.gender = gender;

if (avatarConfig != null)
{
Expand All @@ -49,15 +46,16 @@ public AvatarManager(BodyType bodyType, OutfitGender gender, AvatarConfig avatar
/// </summary>
/// <param name="avatarProperties">Properties which describes avatar</param>
/// <returns>Avatar gameObject</returns>
public async Task<GameObject> CreateAvatar(AvatarProperties avatarProperties)
public async Task<(GameObject, AvatarProperties)> CreateAvatar(AvatarProperties avatarProperties)
{
GameObject avatar = null;
try
{
avatarProperties = await avatarAPIRequests.CreateNewAvatar(avatarProperties);
gender = avatarProperties.Gender;
if (ctxSource.IsCancellationRequested)
{
return null;
return (null, avatarProperties);
}

avatarId = avatarProperties.Id;
Expand All @@ -66,29 +64,29 @@ public async Task<GameObject> CreateAvatar(AvatarProperties avatarProperties)
catch (Exception e)
{
OnError?.Invoke(e.Message);
return avatar;
return (avatar, avatarProperties);
}

return avatar;
return (avatar, avatarProperties);
}

/// <summary>
/// Create a new avatar from a provided template.
/// </summary>
/// <param name="id">Template id</param>
/// <param name="partner">Partner name</param>
/// <returns>Avatar gameObject</returns>
public async Task<(GameObject, AvatarProperties)> CreateAvatarFromTemplate(string id, string partner)
public async Task<(GameObject, AvatarProperties)> CreateAvatarFromTemplate(string id)
{
GameObject avatar = null;
var avatarProperties = new AvatarProperties();
try
{
avatarProperties = await avatarAPIRequests.CreateFromTemplateAvatar(
id,
partner,
CoreSettingsHandler.CoreSettings.Subdomain,
bodyType
);
gender = avatarProperties.Gender;
if (ctxSource.IsCancellationRequested)
{
return (null, avatarProperties);
Expand Down
4 changes: 2 additions & 2 deletions Runtime/AvatarCreator/WebRequests/AvatarAPIRequests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public async Task<AvatarProperties> CreateNewAvatar(AvatarProperties avatarPrope
{
Url = AvatarEndpoints.GetCreateEndpoint(),
Method = HttpMethod.POST,
Payload = avatarProperties.ToJson()
Payload = avatarProperties.ToJson(true)
},
ctx: ctx
);
Expand All @@ -154,7 +154,7 @@ public async Task<byte[]> GetAvatar(string avatarId, bool isPreview = false, str
var response = await authorizedRequest.SendRequest<Response>(
new RequestData
{
Url =AvatarEndpoints.GetAvatarModelEndpoint(avatarId, isPreview, parameters),
Url = AvatarEndpoints.GetAvatarModelEndpoint(avatarId, isPreview, parameters),
Method = HttpMethod.GET
},
ctx: ctx);
Expand Down
15 changes: 7 additions & 8 deletions Samples~/AvatarCreatorSamples/Scripts/ProfileManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using ReadyPlayerMe.AvatarCreator;
Expand Down Expand Up @@ -26,14 +27,11 @@ private void Awake()

private void OnEnable()
{
profileUI.SignedOut += AuthManager.Logout;
AuthManager.OnSignedOut += DeleteSession;
}


private void OnDisable()
{
profileUI.SignedOut -= AuthManager.Logout;
AuthManager.OnSignedOut -= DeleteSession;
}

Expand All @@ -48,9 +46,10 @@ public bool LoadSession()
var json = Encoding.UTF8.GetString(bytes);
var userSession = JsonConvert.DeserializeObject<UserSession>(json);
AuthManager.SetUser(userSession);

SetProfileData(userSession);

SDKLogger.Log(TAG, $"Loaded session from {filePath}");
SDKLogger.Log(TAG, $"Loaded session from {filePath}");
return true;
}

Expand All @@ -60,7 +59,7 @@ public void SaveSession(UserSession userSession)
DirectoryUtility.ValidateDirectory(directoryPath);
File.WriteAllBytes(filePath, Encoding.UTF8.GetBytes(json));
SetProfileData(userSession);

SDKLogger.Log(TAG, $"Saved session to {filePath}");
}

Expand All @@ -71,15 +70,15 @@ private void SetProfileData(UserSession userSession)
char.ToUpperInvariant(userSession.Name[0]).ToString()
);
}

private void DeleteSession()
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
profileUI.ClearProfile();

SDKLogger.Log(TAG, $"Deleted session at {filePath}");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ private async void Setup()

avatarManager = new AvatarManager(
AvatarCreatorData.AvatarProperties.BodyType,
AvatarCreatorData.AvatarProperties.Gender,
inCreatorConfig,
ctxSource.Token);
avatarManager.OnError += OnErrorCallback;
Expand Down Expand Up @@ -158,15 +157,14 @@ private async Task<GameObject> LoadAvatar()
if (string.IsNullOrEmpty(AvatarCreatorData.AvatarProperties.Id))
{
AvatarCreatorData.AvatarProperties.Assets ??= GetDefaultAssets();
avatar = await avatarManager.CreateAvatar(AvatarCreatorData.AvatarProperties);
(avatar, _) = await avatarManager.CreateAvatar(AvatarCreatorData.AvatarProperties);
}
else
{
if (!AvatarCreatorData.IsExistingAvatar)
{
(avatar, AvatarCreatorData.AvatarProperties) = await avatarManager.CreateAvatarFromTemplate(
AvatarCreatorData.AvatarProperties.Id,
AvatarCreatorData.AvatarProperties.Partner
AvatarCreatorData.AvatarProperties.Id
);
}
else
Expand Down

0 comments on commit f7bee7b

Please sign in to comment.