forked from loukylor/VRC-Mods
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEntryManager.cs
333 lines (299 loc) · 15.1 KB
/
EntryManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using System;
using System.Collections;
using System.Collections.Generic;
using MelonLoader;
using PlayerList.Config;
using PlayerList.Entries;
using PlayerList.Utilities;
using UnityEngine;
using VRC;
using VRC.Core;
using VRC.SDKBase.Validation.Performance;
using VRChatUtilityKit.Utilities;
using Object = UnityEngine.Object;
namespace PlayerList
{
public class EntryManager
{
internal static LocalPlayerEntry localPlayerEntry = null;
public static List<PlayerEntry> playerEntries = new List<PlayerEntry>(); // This will not be sorted
public static List<PlayerLeftPairEntry> playerLeftPairsEntries = new List<PlayerLeftPairEntry>();
public static Dictionary<string, PlayerLeftPairEntry> idToEntryTable = new Dictionary<string, PlayerLeftPairEntry>();
public static List<EntryBase> generalInfoEntries = new List<EntryBase>();
public static List<EntryBase> entries = new List<EntryBase>();
public class deferredAvInstantiate
{
public VRCAvatarManager player;
public ApiAvatar avatar;
public GameObject gameObject;
public int numAttempts;
public deferredAvInstantiate(VRCAvatarManager a, ApiAvatar b, GameObject c)
{
this.player = a;
this.avatar = b;
this.gameObject = c;
this.numAttempts = 0;
}
}
public static Dictionary<string, deferredAvInstantiate> AvInstBacklog = new Dictionary<string, deferredAvInstantiate>();
public static void Init()
{
PlayerListConfig.fontSize.OnValueChanged += (oldValue, newValue) => SetFontSize(newValue);
PlayerListConfig.OnConfigChanged += OnConfigChanged;
NetworkEvents.OnPlayerJoined += OnPlayerJoined;
NetworkEvents.OnPlayerLeft += OnPlayerLeft;
NetworkEvents.OnInstanceChanged += OnInstanceChanged;
NetworkEvents.OnAvatarInstantiated += OnAvatarInstantiated;
NetworkEvents.OnAvatarDownloadProgressed += OnAvatarDownloadProgressed;
MelonCoroutines.Start(EntryRefreshEnumerator());
}
private static IEnumerator EntryRefreshEnumerator()
{
while (playerEntries.Count == 0)
yield return null;
int i = -1;
while (true)
{
i += 1;
if (i >= playerEntries.Count)
{
i = 0;
if (playerEntries.Count == 0)
{
yield return null;
continue;
}
}
try
{
if (playerEntries[i].player == null)
{
playerEntries[i].playerLeftPairEntry.Remove();
continue;
}
if (playerEntries[i].timeSinceLastUpdate.ElapsedMilliseconds > 100)
PlayerEntry.UpdateEntry(playerEntries[i].player.prop_PlayerNet_0, playerEntries[i]);
}
catch (Exception ex)
{
MelonLogger.Error(ex.ToString());
}
yield return null;
}
}
public static void OnUpdate()
{
RefreshAllEntries();
}
public static void OnSceneWasLoaded()
{
for (int i = playerEntries.Count - 1; i >= 0; i--)
playerEntries[i].playerLeftPairEntry.Remove();
localPlayerEntry?.OnSceneWasLoaded();
}
public static void OnInstanceChanged(ApiWorld world, ApiWorldInstance instance)
{
foreach (EntryBase entry in entries)
entry.OnInstanceChange(world, instance);
RefreshLeftPlayerEntries(0, 0, true);
}
public static void OnConfigChanged()
{
foreach (EntryBase entry in entries)
entry.OnConfigChanged();
}
public static void OnAvatarInstantiated(VRCAvatarManager player, ApiAvatar avatar, GameObject gameObject)
{
//MelonLogger.Msg("EM: OnAvInst");
/*foreach (EntryBase entry in playerEntries)
entry.OnAvatarInstantiated(player, avatar, gameObject);
localPlayerEntry?.OnAvatarInstantiated(player, avatar, gameObject);*/
//There's a race condition, sometimes an avatar instantiated event will happen before a player join event.
//If the player hasn't been added to the idToEntryTable yet, we'll add the information to a backlog to call when the player has been added.
string playerid = player.field_Private_VRCPlayer_0.prop_Player_0.prop_APIUser_0?.id;
if (!idToEntryTable.TryGetValue(player.field_Private_VRCPlayer_0.prop_Player_0.prop_APIUser_0?.id, out PlayerLeftPairEntry entry))
{
//MelonLogger.Msg("EM: Key not found in dict: " + player.field_Private_VRCPlayer_0.prop_Player_0.prop_APIUser_0?.displayName);
if (!AvInstBacklog.ContainsKey(playerid))
AvInstBacklog.Add(playerid, new deferredAvInstantiate(player, avatar, gameObject));
return;
}
try
{
entry.playerEntry.OnAvatarInstantiated(player, avatar, gameObject);
}
catch
{
if (!AvInstBacklog.ContainsKey(playerid))
AvInstBacklog.Add(playerid, new deferredAvInstantiate(player, avatar, gameObject));
return;
}
ProcessAvatarInstantiateBacklog();
}
public static void OnAvatarDownloadProgressed(AvatarLoadingBar loadingBar, float downloadPercent, long fileSize)
{
foreach (EntryBase entry in playerEntries)
entry.OnAvatarDownloadProgressed(loadingBar, downloadPercent, fileSize);
localPlayerEntry?.OnAvatarDownloadProgressed(loadingBar, downloadPercent, fileSize);
}
public static void OnPlayerJoined(Player player)
{
if (player.name.Contains("Local") && player.prop_APIUser_0 == null)
player.prop_APIUser_0 = APIUser.CurrentUser;
if (idToEntryTable.ContainsKey(player.prop_APIUser_0.id))
return; // If already in list
if (player.name.Contains("Local"))
{
if (localPlayerEntry != null)
return;
GameObject template = Object.Instantiate(Constants.playerListLayout.transform.Find("Template").gameObject, Constants.playerListLayout.transform);
template.SetActive(true);
LeftSidePlayerEntry leftSidePlayerEntry = EntryBase.CreateInstance<LeftSidePlayerEntry>(template.transform.Find("LeftPart").gameObject);
EntryBase.CreateInstance<LocalPlayerEntry>(template.transform.Find("RightPart").gameObject);
AddPlayerLeftPairEntry(EntryBase.CreateInstance<PlayerLeftPairEntry>(template, new object[] { leftSidePlayerEntry, localPlayerEntry }));
}
else
{
GameObject template = Object.Instantiate(Constants.playerListLayout.transform.Find("Template").gameObject, Constants.playerListLayout.transform);
template.SetActive(true);
LeftSidePlayerEntry leftSidePlayerEntry = EntryBase.CreateInstance<LeftSidePlayerEntry>(template.transform.Find("LeftPart").gameObject);
PlayerEntry playerEntry = EntryBase.CreateInstance<PlayerEntry>(template.transform.Find("RightPart").gameObject, new object[] { player });
AddPlayerLeftPairEntry(EntryBase.CreateInstance<PlayerLeftPairEntry>(template, new object[] { leftSidePlayerEntry, playerEntry }));
}
//ProcessAvatarInstantiateBacklog();
}
public static void ProcessAvatarInstantiateBacklog()
{
if (AvInstBacklog.Count != 0)
{
MelonLogger.Msg("Addressing Backlog. Size: " + AvInstBacklog.Count.ToString());
var keys = new string[AvInstBacklog.Count];
AvInstBacklog.Keys.CopyTo(keys, 0);
foreach (var key in keys)
{
if (idToEntryTable.TryGetValue(key, out PlayerLeftPairEntry e))
{
try
{
e.playerEntry.OnAvatarInstantiated(AvInstBacklog[key].player, AvInstBacklog[key].avatar, AvInstBacklog[key].gameObject);
AvInstBacklog.Remove(key);
}
catch
{
//MelonLogger.Msg("OAI Failed!");
AvInstBacklog[key].numAttempts++;
}
}
else
{
AvInstBacklog[key].numAttempts++;
if (AvInstBacklog[key].numAttempts > 2)
{
MelonLogger.Msg("Max attempts exceeded for backlog entry");
AvInstBacklog.Remove(key);
}
}
}
//AvInstBacklog.Clear();
}
}
public static void CleanUpHungAOI()
{
foreach (PlayerEntry entry in playerEntries)
{
if ((entry.perf == PerformanceRating.None) && ((entry.perfString == "100% ") || (entry.perfString == "?¿?¿?")))
{
AvInstBacklog.Add(entry.userId, new deferredAvInstantiate(null, null, null));
}
}
ProcessAvatarInstantiateBacklog();
}
public static void OnPlayerLeft(Player player)
{
if (player.prop_APIUser_0 == null)
{
MelonLogger.Error("Null Player Left!");
return;
}
if (player.prop_APIUser_0.IsSelf)
return;
//MelonLogger.Msg("OPL: Removing " + player.field_Private_APIUser_0.displayName);
if (!idToEntryTable.TryGetValue(player.prop_APIUser_0.id, out PlayerLeftPairEntry entry))
return;
entry.Remove();
RefreshLeftPlayerEntries(0, 0, true);
}
public static void AddGeneralInfoEntries()
{
MelonLogger.Msg("Adding List Entries...");
AddGeneralInfoEntry(EntryBase.CreateInstance<PlayerListHeaderEntry>(Constants.playerListLayout.transform.Find("Header").gameObject, includeConfig: true));
AddGeneralInfoEntry(EntryBase.CreateInstance<RoomTimeEntry>(Constants.generalInfoLayout.transform.Find("RoomTime").gameObject, includeConfig: true));
AddGeneralInfoEntry(EntryBase.CreateInstance<SystemTime12HrEntry>(Constants.generalInfoLayout.transform.Find("SystemTime12Hr").gameObject, includeConfig: true));
AddGeneralInfoEntry(EntryBase.CreateInstance<SystemTime24HrEntry>(Constants.generalInfoLayout.transform.Find("SystemTime24Hr").gameObject, includeConfig: true));
AddGeneralInfoEntry(EntryBase.CreateInstance<GameVersionEntry>(Constants.generalInfoLayout.transform.Find("GameVersion").gameObject, includeConfig: true));
AddGeneralInfoEntry(EntryBase.CreateInstance<CoordinatePositionEntry>(Constants.generalInfoLayout.transform.Find("CoordinatePosition").gameObject, includeConfig: true));
AddGeneralInfoEntry(EntryBase.CreateInstance<WorldNameEntry>(Constants.generalInfoLayout.transform.Find("WorldName").gameObject, includeConfig: true));
AddGeneralInfoEntry(EntryBase.CreateInstance<WorldAuthorEntry>(Constants.generalInfoLayout.transform.Find("WorldAuthor").gameObject, includeConfig: true));
AddGeneralInfoEntry(EntryBase.CreateInstance<InstanceMasterEntry>(Constants.generalInfoLayout.transform.Find("InstanceMaster").gameObject, includeConfig: true));
AddGeneralInfoEntry(EntryBase.CreateInstance<InstanceCreatorEntry>(Constants.generalInfoLayout.transform.Find("InstanceCreator").gameObject, includeConfig: true));
AddGeneralInfoEntry(EntryBase.CreateInstance<RiskyFuncAllowedEntry>(Constants.generalInfoLayout.transform.Find("RiskyFuncAllowed").gameObject, includeConfig: true));
}
public static void AddEntry(EntryBase entry)
{
if (entry.textComponent != null)
entry.textComponent.fontSize = PlayerListConfig.fontSize.Value;
entries.Add(entry);
}
public static void AddPlayerLeftPairEntry(PlayerLeftPairEntry entry)
{
playerLeftPairsEntries.Add(entry);
//idToEntryTable.Add(entry.playerEntry.userId, entry);
if (!entry.playerEntry.isSelf)
playerEntries.Add(entry.playerEntry);
AddEntry(entry);
AddEntry(entry.leftSidePlayerEntry);
AddEntry(entry.playerEntry);
entry.playerEntry.gameObject.SetActive(true);
EntrySortManager.SortPlayer(entry);
RefreshLeftPlayerEntries(0, 0, true);
}
public static void AddGeneralInfoEntry(EntryBase entry)
{
AddEntry(entry);
generalInfoEntries.Add(entry);
}
public static void RefreshLeftPlayerEntries(int oldCount, int newCount, bool bypassCount = false)
{
// If new digit reached (like 9 - 10)
if (oldCount.ToString().Length != newCount.ToString().Length || bypassCount)
foreach (PlayerLeftPairEntry playerLeftPairEntry in playerLeftPairsEntries)
playerLeftPairEntry.leftSidePlayerEntry.CalculateLeftPart();
}
public static void RefreshPlayerEntries(bool bypassActive = false)
{
if (RoomManager.field_Internal_Static_ApiWorld_0 == null || Player.prop_Player_0 == null || Player.prop_Player_0.gameObject == null || Player.prop_Player_0.prop_VRCPlayerApi_0 == null || (!MenuManager.playerList.active && !bypassActive)) return;
foreach (PlayerEntry entry in playerEntries)
PlayerEntry.UpdateEntry(entry.player.prop_PlayerNet_0, entry, bypassActive);
localPlayerEntry.Refresh();
}
public static void RefreshGeneralInfoEntries()
{
foreach (EntryBase entry in generalInfoEntries)
entry.Refresh();
}
public static void RefreshAllEntries()
{
// Dont refresh if the local player gameobject has been deleted or if the playerlist is hidden
if (RoomManager.field_Internal_Static_ApiWorld_0 == null || Player.prop_Player_0 == null || !MenuManager.playerList.active) return;
localPlayerEntry?.Refresh();
RefreshGeneralInfoEntries();
}
public static void SetFontSize(int fontSize)
{
MenuManager.fontSizeLabel.TextComponent.text = $"{fontSize}";
foreach (EntryBase entry in entries)
if (entry.textComponent != null)
entry.textComponent.fontSize = fontSize;
}
}
}