-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PerformanceMeterController.cs
475 lines (428 loc) · 24.4 KB
/
PerformanceMeterController.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*
* PerformanceMeterController.cs
* PerformanceMeter
*
* This file defines the main functionality of PerformanceMeter.
*
* This code is licensed under the MIT license.
* Copyright (c) 2021-2022 JackMacWindows.
*/
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR;
using TMPro;
namespace PerformanceMeter {
public struct Pair<T, U> {
public T first;
public U second;
public Pair(T a, U b) {first = a; second = b;}
}
public class PerformanceMeterController : MonoBehaviour {
public static PerformanceMeterController instance { get; private set; }
List<Pair<float, float>> energyList = new List<Pair<float, float>>();
List<Pair<float, float>> secondaryEnergyList = new List<Pair<float, float>>();
List<float> misses = new List<float>();
float averageHitValue = 0.0f;
int averageHitValueSize = 0;
float secondaryAverageHitValue = 0.0f;
int secondaryAverageHitValueSize = 0;
ScoreController scoreController;
BeatmapObjectManager objectManager;
IComboController comboController;
GameEnergyCounter energyCounter;
RelativeScoreAndImmediateRankCounter rankCounter;
AudioTimeSyncController audioController;
PauseController pauseController;
GameObject panel;
ILevelEndActions endActions;
bool levelOk = false;
bool levelNotOk = false;
static readonly FieldInfo _beatmapObjectManager = typeof(ScoreController).GetField("_beatmapObjectManager", BindingFlags.NonPublic | BindingFlags.Instance);
static readonly FieldInfo _comboController = typeof(ComboUIController).GetField("_comboController", BindingFlags.NonPublic | BindingFlags.Instance);
static readonly FieldInfo StandardLevelGameplayManager_pauseController = typeof(StandardLevelGameplayManager).GetField("_pauseController", BindingFlags.NonPublic | BindingFlags.Instance);
static readonly FieldInfo MissionLevelGameplayManager_pauseController = typeof(MissionLevelGameplayManager).GetField("_pauseController", BindingFlags.NonPublic | BindingFlags.Instance);
public void ShowResults() {
if (!levelOk || levelNotOk)
return;
levelOk = false;
levelNotOk = false;
Logger.log.Debug("Found " + energyList.Count() + " primary notes, " + secondaryEnergyList.Count() + " secondary notes");
panel = new GameObject("PerformanceMeter");
panel.transform.Rotate(22.5f, 0, 0, Space.World);
Canvas canvas = panel.AddComponent<Canvas>();
canvas.renderMode = RenderMode.WorldSpace;
canvas.scaleFactor = 0.01f;
canvas.GetComponent<RectTransform>().localPosition = new Vector3(0.0f, 0.4f, 2.25f);
canvas.GetComponent<RectTransform>().sizeDelta = new Vector2(1.0f, 0.5f);
canvas.transform.Rotate(22.5f, 0, 0, Space.World);
panel.AddComponent<CanvasRenderer>();
panel.AddComponent<HMUI.CurvedCanvasSettings>();
GameObject imageObj = new GameObject("Background");
imageObj.transform.SetParent(canvas.transform);
imageObj.transform.Rotate(22.5f, 0, 0, Space.World);
Image img = imageObj.AddComponent<Image>();
img.color = new Color(0.1f, 0.1f, 0.1f, 0.25f);
img.GetComponent<RectTransform>().localPosition = new Vector3(0.0f, 0.0f, 0.0f);
img.GetComponent<RectTransform>().sizeDelta = new Vector2(1.0f, 0.6f);
GameObject textObj = new GameObject("Label");
textObj.transform.SetParent(canvas.transform);
textObj.transform.Rotate(22.5f, 0, 0, Space.World);
HMUI.CurvedTextMeshPro text = textObj.AddComponent<HMUI.CurvedTextMeshPro>();
text.font = Instantiate(Resources.FindObjectsOfTypeAll<TMP_FontAsset>().First(t => t.name == "Teko-Medium SDF"));
text.fontSize = 9.0f;
text.alignment = TextAlignmentOptions.Right;
text.text = "Performance";
text.enableAutoSizing = true;
text.transform.localScale = new Vector3(0.005f, 0.005f, 0.005f);
text.GetComponent<RectTransform>().localPosition = new Vector3(-0.3f, -0.25f, 0.1f);
text.GetComponent<RectTransform>().sizeDelta = new Vector2(75.0f, 25.0f);
GameObject textObj2 = new GameObject("Label");
textObj2.transform.SetParent(canvas.transform);
textObj2.transform.Rotate(22.5f, 0, 0, Space.World);
HMUI.CurvedTextMeshPro text2 = textObj2.AddComponent<HMUI.CurvedTextMeshPro>();
text2.font = Instantiate(Resources.FindObjectsOfTypeAll<TMP_FontAsset>().First(t => t.name == "Teko-Medium SDF"));
text2.fontSize = 9.0f;
text2.alignment = TextAlignmentOptions.Right;
text2.text = "By JackMacWindows#9776";
text2.color = new Color(0.3f, 0.3f, 0.3f);
text2.enableAutoSizing = true;
text2.transform.localScale = new Vector3(0.002f, 0.002f, 0.002f);
text2.GetComponent<RectTransform>().localPosition = new Vector3(0.31f, -0.2625f, 0.1f);
text2.GetComponent<RectTransform>().sizeDelta = new Vector2(175.0f, 16.0f);
GameObject graphMask = new GameObject("GraphMask");
graphMask.AddComponent<RectTransform>().localPosition = new Vector3(0.0f, 0.45f, 2.275f);
graphMask.GetComponent<RectTransform>().sizeDelta = new Vector2(1.0f, 0.45f);
graphMask.transform.Rotate(22.5f, 0, 0, Space.World);
graphMask.transform.SetParent(panel.transform);
graphMask.transform.name = "GraphMask";
graphMask.AddComponent<RectMask2D>();
GameObject graphObj = new GameObject("GraphContainer");
graphObj.AddComponent<RectTransform>().localPosition = new Vector3(0.0f, 0.45f, 2.275f);
graphObj.GetComponent<RectTransform>().sizeDelta = new Vector2(1.0f, 0.45f);
graphObj.transform.Rotate(22.5f, 0, 0, Space.World);
graphObj.transform.SetParent(graphMask.transform);
graphObj.transform.name = "GraphContainer";
bool hasPrimary = PluginConfig.Instance.mode != PluginConfig.MeasurementMode.None && energyList.Count > 0;
bool hasSecondary = PluginConfig.Instance.secondaryMode != PluginConfig.MeasurementMode.None && secondaryEnergyList.Count > 0;
if (!hasPrimary && !hasSecondary) {
GameObject textObj3 = new GameObject("Label");
textObj3.transform.SetParent(canvas.transform);
textObj3.transform.Rotate(22.5f, 0, 0, Space.World);
HMUI.CurvedTextMeshPro text3 = textObj3.AddComponent<HMUI.CurvedTextMeshPro>();
text3.font = Instantiate(Resources.FindObjectsOfTypeAll<TMP_FontAsset>().First(t => t.name == "Teko-Medium SDF"));
text3.fontSize = 48.0f;
text3.alignment = TextAlignmentOptions.Center;
text3.text = "No data available";
text3.color = new Color(0.3f, 0.3f, 0.3f);
text3.enableAutoSizing = true;
text3.transform.localScale = new Vector3(0.002f, 0.002f, 0.002f);
text3.GetComponent<RectTransform>().localPosition = new Vector3(0f, 0f, 0f);
text3.GetComponent<RectTransform>().sizeDelta = new Vector2(800.0f, 80.0f);
StartCoroutine(WaitForMenu(graphObj, graphMask));
return;
}
float width = 0.0f;
if (hasPrimary && hasSecondary) {
if (energyList.Last().first > secondaryEnergyList.Last().first)
secondaryEnergyList.Add(new Pair<float, float>(energyList.Last().first, secondaryEnergyList.Last().second));
else if (secondaryEnergyList.Last().first > energyList.Last().first)
energyList.Add(new Pair<float, float>(secondaryEnergyList.Last().first, energyList.Last().second));
}
width = hasPrimary ? energyList.Last().first : secondaryEnergyList.Last().first;
if (hasPrimary)
graphMask.AddComponent<WindowGraph>().ShowGraph(energyList, PluginConfig.Instance.mode, width, PluginConfig.Instance.overrideColor, PluginConfig.Instance.sideColor, true);
if (hasSecondary)
graphMask.AddComponent<WindowGraph>().ShowGraph(secondaryEnergyList, PluginConfig.Instance.secondaryMode, width, PluginConfig.Instance.overrideSecondaryColor, PluginConfig.Instance.secondarySideColor, false);
if (PluginConfig.Instance.showMisses) {
var GraphTransform = graphObj.GetComponent<RectTransform>();
var xSize = GraphTransform.sizeDelta.x / width;
var ySize = GraphTransform.sizeDelta.y;
foreach (float pos in misses) {
var xPosition = pos * xSize;
var dotPositionA = new Vector2(xPosition, 0);
var dotPositionB = new Vector2(xPosition, ySize);
var gameObject = new GameObject("DotConnection", typeof(Image));
gameObject.transform.SetParent(GraphTransform, false);
var image = gameObject.GetComponent<Image>();
image.color = new Color(0.5f, 0.5f, 0.5f, 0.75f);
var rectTransform = gameObject.GetComponent<RectTransform>();
var dir = (dotPositionB - dotPositionA).normalized;
var distance = Vector2.Distance(dotPositionA, dotPositionB);
rectTransform.anchorMin = new Vector2(0, 0);
rectTransform.anchorMax = new Vector2(0, 0);
rectTransform.sizeDelta = new Vector2(distance, 0.0025f);
rectTransform.anchoredPosition = dotPositionA + dir * distance * .5f;
rectTransform.localEulerAngles = new Vector3(0, 0, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg);
}
}
StartCoroutine(WaitForMenu(graphObj, graphMask));
}
IEnumerator WaitForMenu(GameObject graphObj, GameObject graphMask) {
if (endActions is StandardLevelGameplayManager) {
ResultsViewController resultsController = null;
do {
resultsController = Resources.FindObjectsOfTypeAll<ResultsViewController>().LastOrDefault();
yield return new WaitForSeconds(0.1f);
} while (resultsController == null);
resultsController.continueButtonPressedEvent += DismissGraph;
resultsController.restartButtonPressedEvent += DismissGraph;
} else {
MissionResultsViewController resultsController = null;
do {
resultsController = Resources.FindObjectsOfTypeAll<MissionResultsViewController>().LastOrDefault();
yield return new WaitForSeconds(0.1f);
} while (resultsController == null);
resultsController.continueButtonPressedEvent += DismissGraph_Mission;
resultsController.retryButtonPressedEvent += DismissGraph_Mission;
}
Logger.log.Debug("PerformanceMeter menu created successfully");
StartCoroutine(GraphAnimation(graphObj, graphMask));
}
IEnumerator GraphAnimation(GameObject graphObj, GameObject graphMask) {
if (PluginConfig.Instance.animationDuration <= 0f)
yield break;
float fps = XRDevice.refreshRate;
float steps = fps * PluginConfig.Instance.animationDuration;
Vector3 posDelta = new Vector3(0.5f / steps, 0f, 0f);
Vector2 sizeDelta = new Vector2(1f / steps, 0f);
graphMask.GetComponent<RectTransform>().sizeDelta = new Vector2(0.0f, 0.45f);
graphMask.GetComponent<RectTransform>().localPosition -= posDelta * steps;
graphObj.GetComponent<RectTransform>().localPosition += posDelta * steps;
for (int s = 1; s <= steps; s++) {
if (panel == null)
yield break;
graphMask.GetComponent<RectTransform>().sizeDelta += sizeDelta;
graphMask.GetComponent<RectTransform>().localPosition += posDelta;
graphObj.GetComponent<RectTransform>().localPosition -= posDelta;
yield return new WaitForSeconds(1f / fps);
}
}
void DismissGraph(ResultsViewController vc) {
if (panel != null) {
panel.SetActive(false);
Destroy(panel, 1);
panel = null;
scoreController = null;
objectManager = null;
comboController = null;
energyCounter = null;
rankCounter = null;
audioController = null;
endActions = null;
pauseController = null;
}
if (vc != null) {
vc.continueButtonPressedEvent -= DismissGraph;
vc.restartButtonPressedEvent -= DismissGraph;
}
}
void DismissGraph_Mission(MissionResultsViewController vc) {
DismissGraph(null);
if (vc != null) {
vc.continueButtonPressedEvent -= DismissGraph_Mission;
vc.retryButtonPressedEvent -= DismissGraph_Mission;
}
}
public void GetControllers() {
DismissGraph(null);
levelOk = false;
levelNotOk = false;
averageHitValue = 0.0f;
averageHitValueSize = 0;
secondaryAverageHitValue = 0.0f;
secondaryAverageHitValueSize = 0;
energyList.Clear();
secondaryEnergyList.Clear();
misses.Clear();
if (PluginConfig.Instance.mode == PluginConfig.MeasurementMode.Energy)
energyList.Add(new Pair<float, float>(0.0f, 0.5f));
if (PluginConfig.Instance.secondaryMode == PluginConfig.MeasurementMode.Energy)
secondaryEnergyList.Add(new Pair<float, float>(0.0f, 0.5f));
scoreController = Resources.FindObjectsOfTypeAll<ScoreController>().LastOrDefault();
ComboUIController comboUIController = Resources.FindObjectsOfTypeAll<ComboUIController>().LastOrDefault();
energyCounter = Resources.FindObjectsOfTypeAll<GameEnergyCounter>().LastOrDefault();
rankCounter = Resources.FindObjectsOfTypeAll<RelativeScoreAndImmediateRankCounter>().LastOrDefault();
audioController = Resources.FindObjectsOfTypeAll<AudioTimeSyncController>().LastOrDefault();
endActions = Resources.FindObjectsOfTypeAll<StandardLevelGameplayManager>().LastOrDefault();
if (endActions == null)
endActions = Resources.FindObjectsOfTypeAll<MissionLevelGameplayManager>().LastOrDefault();
if (endActions != null && scoreController != null && energyCounter != null && rankCounter != null && audioController != null && comboUIController != null) {
objectManager = (BeatmapObjectManager)_beatmapObjectManager.GetValue(scoreController);
comboController = (ComboController)_comboController.GetValue(comboUIController);
objectManager.noteWasCutEvent += NoteHit;
objectManager.noteWasMissedEvent += NoteMiss;
comboController.comboBreakingEventHappenedEvent += ComboBreak;
endActions.levelFinishedEvent += LevelFinished;
endActions.levelFailedEvent += LevelFinished;
if (endActions is StandardLevelGameplayManager) pauseController = (PauseController)StandardLevelGameplayManager_pauseController.GetValue(endActions);
else pauseController = (PauseController)MissionLevelGameplayManager_pauseController.GetValue(endActions);
pauseController.didReturnToMenuEvent += LevelExit;
Logger.log.Debug("PerformanceMeter reloaded successfully");
} else {
Logger.log.Error("Could not reload PerformanceMeter. This may occur when playing online - if so, disregard this message.");
scoreController = null;
objectManager = null;
comboController = null;
energyCounter = null;
rankCounter = null;
audioController = null;
endActions = null;
pauseController = null;
}
}
private class ScoreFinishEventHandler : ICutScoreBufferDidFinishReceiver {
private PerformanceMeterController controller;
private NoteData data;
private bool secondary;
internal ScoreFinishEventHandler(PerformanceMeterController c, NoteData d, bool s) {controller = c; data = d; secondary = s;}
public void HandleCutScoreBufferDidFinish(CutScoreBuffer cutScoreBuffer) {
if (!secondary)
controller.RecordHitValue(cutScoreBuffer, data, this);
else
controller.RecordHitValueSecondary(cutScoreBuffer, data, this);
}
}
private void RecordHitValue(CutScoreBuffer score, NoteData data, ScoreFinishEventHandler fn) {
float newEnergy;
switch (PluginConfig.Instance.mode) {
case PluginConfig.MeasurementMode.Energy:
newEnergy = energyCounter.energy;
break;
case PluginConfig.MeasurementMode.PercentModified:
newEnergy = (float)scoreController.modifiedScore / scoreController.immediateMaxPossibleModifiedScore;
break;
case PluginConfig.MeasurementMode.PercentRaw:
newEnergy = rankCounter.relativeScore;
break;
case PluginConfig.MeasurementMode.CutValue:
if (score == null)
return;
newEnergy = score.cutScore / 115.0f;
break;
case PluginConfig.MeasurementMode.AvgCutValue:
if (score == null)
return;
averageHitValue = ((averageHitValue * averageHitValueSize) + score.cutScore / 115.0f) / ++averageHitValueSize;
newEnergy = averageHitValue;
break;
default:
Logger.log.Error("An invalid mode was specified! PerformanceMeter will not record scores, resulting in a blank graph. Check the readme for the valid modes.");
return;
}
if (energyList.Count == 0)
energyList.Add(new Pair<float, float>(0, newEnergy));
energyList.Add(new Pair<float, float>(data.time, newEnergy));
if (score != null)
score.UnregisterDidFinishReceiver(fn);
}
private void RecordHitValueSecondary(CutScoreBuffer score, NoteData data, ScoreFinishEventHandler fn) {
float newEnergy;
switch (PluginConfig.Instance.secondaryMode) {
case PluginConfig.MeasurementMode.None:
return;
case PluginConfig.MeasurementMode.Energy:
newEnergy = energyCounter.energy;
break;
case PluginConfig.MeasurementMode.PercentModified:
newEnergy = (float)scoreController.modifiedScore / scoreController.immediateMaxPossibleModifiedScore;
break;
case PluginConfig.MeasurementMode.PercentRaw:
newEnergy = rankCounter.relativeScore;
break;
case PluginConfig.MeasurementMode.CutValue:
if (score == null)
return;
newEnergy = score.cutScore / 115.0f;
break;
case PluginConfig.MeasurementMode.AvgCutValue:
if (score == null)
return;
secondaryAverageHitValue = ((secondaryAverageHitValue * secondaryAverageHitValueSize) + score.cutScore / 115.0f) / ++secondaryAverageHitValueSize;
newEnergy = secondaryAverageHitValue;
break;
default:
Logger.log.Error("An invalid mode was specified! PerformanceMeter will not record scores, resulting in a blank graph. Check the readme for the valid modes.");
return;
}
if (secondaryEnergyList.Count == 0)
secondaryEnergyList.Add(new Pair<float, float>(0, newEnergy));
secondaryEnergyList.Add(new Pair<float, float>(data.time, newEnergy));
if (score != null)
score.UnregisterDidFinishReceiver(fn);
}
private void NoteHit(NoteController controller, in NoteCutInfo info) {
PluginConfig.MeasurementSide side = PluginConfig.Instance.side;
if (side == PluginConfig.MeasurementSide.Both || (side == PluginConfig.MeasurementSide.Left && info.saberType == SaberType.SaberA) || (side == PluginConfig.MeasurementSide.Right && info.saberType == SaberType.SaberB)) {
if (info.noteData == null) {
RecordHitValue(null, controller.noteData, null);
} else {
ScoreFinishEventHandler handler = new ScoreFinishEventHandler(this, controller.noteData, false);
CutScoreBuffer buf = new CutScoreBuffer();
buf.Init(info);
buf.RegisterDidFinishReceiver(handler);
}
}
side = PluginConfig.Instance.secondarySide;
if (side == PluginConfig.MeasurementSide.Both || (side == PluginConfig.MeasurementSide.Left && info.saberType == SaberType.SaberA) || (side == PluginConfig.MeasurementSide.Right && info.saberType == SaberType.SaberB)) {
if (info.noteData == null) {
RecordHitValueSecondary(null, controller.noteData, null);
} else {
ScoreFinishEventHandler handler = new ScoreFinishEventHandler(this, controller.noteData, true);
CutScoreBuffer buf = new CutScoreBuffer();
buf.Init(info);
buf.RegisterDidFinishReceiver(handler);
}
}
}
private void NoteMiss(NoteController controller) {
RecordHitValue(null, controller.noteData, null);
RecordHitValueSecondary(null, controller.noteData, null);
}
private void ComboBreak() {
misses.Add(audioController.songTime);
}
private void LevelFinished() {
if (objectManager != null && energyCounter != null && rankCounter != null && endActions != null) {
levelOk = true;
objectManager.noteWasCutEvent -= NoteHit;
objectManager.noteWasMissedEvent -= NoteMiss;
comboController.comboBreakingEventHappenedEvent -= ComboBreak;
endActions.levelFinishedEvent -= LevelFinished;
endActions.levelFailedEvent -= LevelFinished;
pauseController.didReturnToMenuEvent -= LevelExit;
}
}
private void LevelExit() {
if (!levelOk) levelNotOk = true;
}
#region Monobehaviour Messages
/// <summary>
/// Only ever called once, mainly used to initialize variables.
/// </summary>
private void Awake() {
// For this particular MonoBehaviour, we only want one instance to exist at any time, so store a reference to it in a static property
// and destroy any that are created while one already exists.
if (instance != null) {
Logger.log?.Warn($"Instance of {this.GetType().Name} already exists, destroying.");
DestroyImmediate(this);
return;
}
DontDestroyOnLoad(this); // Don't destroy this object on scene changes
instance = this;
Logger.log?.Debug($"{name}: Awake()");
}
/// <summary>
/// Called when the script is being destroyed.
/// </summary>
private void OnDestroy() {
Logger.log?.Debug($"{name}: OnDestroy()");
instance = null; // This MonoBehaviour is being destroyed, so set the static instance property to null.
}
#endregion
}
}