This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
125 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using ReModCE.Core; | ||
using ReModCE.Managers; | ||
using ReModCE.UI; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
using VRC.Core; | ||
using VRC.SDKBase.Validation.Performance.Stats; | ||
using AvatarList = Il2CppSystem.Collections.Generic.List<VRC.Core.ApiAvatar>; | ||
|
||
namespace ReModCE.Components | ||
{ | ||
internal class RecentAvatarsComponent : ModComponent | ||
internal class RecentAvatarsComponent : ModComponent, IAvatarListOwner | ||
{ | ||
private ReAvatarList _avatarList; | ||
private readonly List<ReAvatar> _recentAvatars; | ||
|
||
public RecentAvatarsComponent() | ||
{ | ||
if (File.Exists("UserData/ReModCE/recent_avatars.json")) | ||
{ | ||
_recentAvatars = JsonConvert.DeserializeObject<List<ReAvatar>>(File.ReadAllText("UserData/ReModCE/recent_avatars.json")); | ||
} | ||
else | ||
{ | ||
_recentAvatars = new List<ReAvatar>(); | ||
} | ||
} | ||
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)); | ||
} | ||
|
||
private void OnChangeAvatar() | ||
{ | ||
var apiAvatar = _avatarList.AvatarPedestal.field_Internal_ApiAvatar_0; | ||
if (_recentAvatars.FirstOrDefault(a => a.Id == apiAvatar.id) != null) | ||
{ | ||
_recentAvatars.RemoveAll(a => a.Id == apiAvatar.id); | ||
} | ||
|
||
_recentAvatars.Insert(0, new ReAvatar(apiAvatar)); | ||
|
||
if (_recentAvatars.Count > 100) | ||
{ | ||
_recentAvatars.Remove(_recentAvatars.Last()); | ||
} | ||
|
||
SaveAvatarsToDisk(); | ||
|
||
_avatarList.Refresh(GetAvatars()); | ||
} | ||
|
||
private void SaveAvatarsToDisk() | ||
{ | ||
Directory.CreateDirectory("UserData/ReModCE"); | ||
File.WriteAllText("UserData/ReModCE/recent_avatars.json", JsonConvert.SerializeObject(_recentAvatars)); | ||
} | ||
|
||
public AvatarList GetAvatars() | ||
{ | ||
var list = new AvatarList(); | ||
foreach (var avi in _recentAvatars.Distinct().Select(x => x.AsApiAvatar()).ToList()) | ||
{ | ||
list.Add(avi); | ||
} | ||
return list; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters