Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Make sure you can't switch into deleted or private avatars in both av…
Browse files Browse the repository at this point in the history
…atar history and avatar favorites.
  • Loading branch information
RequiDev committed Aug 23, 2021
1 parent 9bb5a7a commit c31019f
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 2 deletions.
42 changes: 42 additions & 0 deletions ReModCE/Components/AvatarFavoritesComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
using System.IO;
using System.Linq;
using ReModCE.Core;
using ReModCE.Loader;
using ReModCE.Managers;
using ReModCE.UI;
using ReModCE.VRChat;
using UnityEngine;
using UnityEngine.UI;
using VRC.Core;
using VRC.SDKBase.Validation.Performance.Stats;
using AvatarList = Il2CppSystem.Collections.Generic.List<VRC.Core.ApiAvatar>;
Expand All @@ -20,6 +22,8 @@ internal class AvatarFavoritesComponent : ModComponent, IAvatarListOwner

private readonly List<ReAvatar> _savedAvatars;

private Button.ButtonClickedEvent _changeButtonEvent;

public AvatarFavoritesComponent()
{
if (File.Exists("UserData/ReModCE/avatars.bin"))
Expand All @@ -40,6 +44,44 @@ public override void OnUiManagerInit(UiManager uiManager)
_favoriteButton = new ReUiButton("Favorite", new Vector2(-600f, 375f), new Vector2(0.5f, 1f), () => FavoriteAvatar(_avatarList.AvatarPedestal.field_Internal_ApiAvatar_0),
GameObject.Find("UserInterface/MenuContent/Screens/Avatar/Favorite Button").transform.parent);

var changeButton = GameObject.Find("UserInterface/MenuContent/Screens/Avatar/Change Button");
if (changeButton != null)
{
var button = changeButton.GetComponent<Button>();
_changeButtonEvent = button.onClick;

button.onClick = new Button.ButtonClickedEvent();
button.onClick.AddListener(new Action(() =>
{
var currentAvatar = _avatarList.AvatarPedestal.field_Internal_ApiAvatar_0;
if (!HasAvatarFavorited(currentAvatar.id)) // this isn't in our list. we don't care about it
{
_changeButtonEvent.Invoke();
return;
}

new ApiAvatar { id = currentAvatar.id }.Fetch(new Action<ApiContainer>(ac =>
{
var updatedAvatar = ac.Model.Cast<ApiAvatar>();
switch (updatedAvatar.releaseStatus)
{
case "private" when updatedAvatar.authorId != APIUser.CurrentUser.id:
VRCUiPopupManager.prop_VRCUiPopupManager_0.ShowAlert("ReMod CE", "This avatar is private and you don't own it. You can't switch into it.");
break;
case "unavailable":
VRCUiPopupManager.prop_VRCUiPopupManager_0.ShowAlert("ReMod CE", "This avatar has been deleted. You can't switch into it.");
break;
default:
_changeButtonEvent.Invoke();
break;
}
}), new Action<ApiContainer>(ac =>
{
VRCUiPopupManager.prop_VRCUiPopupManager_0.ShowAlert("ReMod CE", "This avatar has been deleted. You can't switch into it.");
}));
}));
}

if (uiManager.IsRemodLoaded)
{
_favoriteButton.Position += new Vector3(UiManager.ButtonSize, 0f);
Expand Down
52 changes: 50 additions & 2 deletions ReModCE/Components/AvatarHistoryComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class AvatarHistoryComponent : ModComponent, IAvatarListOwner
private ReAvatarList _avatarList;
private readonly List<ReAvatar> _recentAvatars;

private Button.ButtonClickedEvent _changeButtonEvent;
public AvatarHistoryComponent()
{
if (File.Exists("UserData/ReModCE/recent_avatars.bin"))
Expand All @@ -36,7 +37,46 @@ public override void OnUiManagerInit(UiManager uiManager)
_avatarList = new ReAvatarList("Recently Used", this, false);

var changeButton = GameObject.Find("UserInterface/MenuContent/Screens/Avatar/Change Button");
changeButton.GetComponent<Button>().onClick.AddListener(new Action(OnChangeAvatar));

if (changeButton != null)
{
var button = changeButton.GetComponent<Button>();
_changeButtonEvent = button.onClick;

button.onClick = new Button.ButtonClickedEvent();
button.onClick.AddListener(new Action(() =>
{
var currentAvatar = _avatarList.AvatarPedestal.field_Internal_ApiAvatar_0;
if (!IsAvatarInHistory(currentAvatar.id)) // this isn't in our list. we don't care about it
{
_changeButtonEvent.Invoke();
return;
}

new ApiAvatar { id = currentAvatar.id }.Fetch(new Action<ApiContainer>(ac =>
{
var updatedAvatar = ac.Model.Cast<ApiAvatar>();
switch (updatedAvatar.releaseStatus)
{
case "private" when updatedAvatar.authorId != APIUser.CurrentUser.id:
VRCUiPopupManager.prop_VRCUiPopupManager_0.ShowAlert("ReMod CE", "This avatar is private and you don't own it. You can't switch into it.");
break;
case "unavailable":
VRCUiPopupManager.prop_VRCUiPopupManager_0.ShowAlert("ReMod CE", "This avatar has been deleted. You can't switch into it.");
_recentAvatars.RemoveAll(a => a.Id == currentAvatar.id);
_avatarList.Refresh(GetAvatars());
break;
default:
_changeButtonEvent.Invoke();
break;
}
}), new Action<ApiContainer>(ac =>
{
VRCUiPopupManager.prop_VRCUiPopupManager_0.ShowAlert("ReMod CE", "This avatar has been deleted. You can't switch into it.");
_recentAvatars.RemoveAll(a => a.Id == currentAvatar.id);
}));
}));
}
}

public override void OnAvatarIsReady(VRCPlayer vrcPlayer)
Expand All @@ -47,9 +87,17 @@ public override void OnAvatarIsReady(VRCPlayer vrcPlayer)
}
}

private bool IsAvatarInHistory(string id)
{
return _recentAvatars.FirstOrDefault(a => a.Id == id) != null;
}

private void AddAvatarToHistory(ApiAvatar avatar)
{
if (_recentAvatars.FirstOrDefault(a => a.Id == avatar.id) != null)
if (avatar.IsLocal)
return;

if (IsAvatarInHistory(avatar.id))
{
_recentAvatars.RemoveAll(a => a.Id == avatar.id);
}
Expand Down

0 comments on commit c31019f

Please sign in to comment.