-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResultsHandler.cs
More file actions
192 lines (160 loc) · 6.92 KB
/
ResultsHandler.cs
File metadata and controls
192 lines (160 loc) · 6.92 KB
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
using HarmonyLib;
using MelonLoader;
using UnityEngine;
using TMPro;
using System.Collections;
using System.Text;
namespace MelatoninAccess
{
public static class ResultsHandler
{
[HarmonyPatch(typeof(Results), "Activate")]
public static class Results_Activate_Patch
{
public static void Postfix(Results __instance)
{
MelonCoroutines.Start(ReadResultsDelayed(__instance));
}
}
private static IEnumerator ReadResultsDelayed(Results results)
{
yield return new WaitForSecondsRealtime(0.5f); // Wait for animations
StringBuilder sb = new StringBuilder();
// Title / Score Message
if (results.ScoreMessage != null)
{
if (results.ScoreMessage.title != null)
{
var tmp = results.ScoreMessage.title.GetComponent<TextMeshPro>();
if (tmp != null) sb.Append(tmp.text + ". ");
}
if (results.ScoreMessage.subtitle != null && results.ScoreMessage.subtitle.CheckIsMeshRendered())
{
var tmp = results.ScoreMessage.subtitle.GetComponent<TextMeshPro>();
if (tmp != null) sb.Append(tmp.text + ". ");
}
}
// Stats
// Counter 0: Perfect, 1: Late, 2: Early, 3: Miss
if (Dream.dir != null)
{
int perfect = Dream.dir.GetCounter(0);
int late = Dream.dir.GetCounter(1);
int early = Dream.dir.GetCounter(2);
int miss = Dream.dir.GetCounter(3);
sb.Append(Loc.Get("results_stats", perfect, late, early, miss));
}
string comment = GetWavesComment(results);
if (!string.IsNullOrWhiteSpace(comment))
{
if (sb.Length > 0) sb.Append(" ");
sb.Append(comment);
if (!comment.EndsWith(".") && !comment.EndsWith("!") && !comment.EndsWith("?"))
{
sb.Append(".");
}
}
if (results.StageEndMenu != null &&
StageEndMenuHelper.TryBuildSelectionAnnouncement(results.StageEndMenu, out string firstOptionAnnouncement))
{
if (sb.Length > 0) sb.Append(" ");
sb.Append(firstOptionAnnouncement);
}
ScreenReader.Say(sb.ToString(), true);
}
private static string GetWavesComment(Results results)
{
if (results == null || results.WavesBox == null || results.WavesBox.message == null) return "";
var tmp = results.WavesBox.message.GetComponent<TextMeshPro>();
if (tmp == null || string.IsNullOrWhiteSpace(tmp.text)) return "";
return tmp.text.Trim();
}
// --- Stage End Menu (Replay/Next) ---
[HarmonyPatch(typeof(StageEndMenu), "Show")]
public static class StageEndMenu_Show_Patch
{
public static void Postfix(StageEndMenu __instance)
{
// Initial option is announced together with the results summary.
}
}
[HarmonyPatch(typeof(StageEndMenu), "Next")]
public static class StageEndMenu_Next_Patch
{
public static void Postfix(StageEndMenu __instance)
{
StageEndMenuHelper.AnnounceSelection(__instance);
}
}
[HarmonyPatch(typeof(StageEndMenu), "Prev")]
public static class StageEndMenu_Prev_Patch
{
public static void Postfix(StageEndMenu __instance)
{
StageEndMenuHelper.AnnounceSelection(__instance);
}
}
public static class StageEndMenuHelper
{
private const float StageEndRepeatBlockSeconds = 0.35f;
private static string _lastStageEndAnnouncement = "";
private static float _lastStageEndAnnouncementTime = -10f;
public static void AnnounceSelection(StageEndMenu menu)
{
if (!TryBuildSelectionAnnouncement(menu, out string text)) return;
float now = Time.unscaledTime;
if (text == _lastStageEndAnnouncement && now - _lastStageEndAnnouncementTime < StageEndRepeatBlockSeconds) return;
_lastStageEndAnnouncement = text;
_lastStageEndAnnouncementTime = now;
ScreenReader.Say(text, true);
}
public static bool TryBuildSelectionAnnouncement(StageEndMenu menu, out string announcement)
{
announcement = "";
if (menu == null) return false;
int highlightPosition = Traverse.Create(menu).Field("highlightPosition").GetValue<int>();
int activeOptionsCount = GetVisibleOptionsCount(menu);
if (menu.labels == null || highlightPosition < 0 || highlightPosition >= menu.labels.Length) return false;
var label = menu.labels[highlightPosition];
var tmp = label != null ? label.GetComponent<TextMeshPro>() : null;
if (tmp == null || string.IsNullOrWhiteSpace(tmp.text)) return false;
string text = tmp.text.Trim();
if (ModConfig.AnnounceMenuPositions && activeOptionsCount > 0)
{
text = Loc.Get("stage_end_position", text, highlightPosition + 1, activeOptionsCount);
}
string lockReason = GetLockReason(menu, highlightPosition);
if (!string.IsNullOrWhiteSpace(lockReason))
{
text = $"{text}. {lockReason}";
}
announcement = text;
return true;
}
private static int GetVisibleOptionsCount(StageEndMenu menu)
{
if (menu.labels == null) return 0;
int count = 0;
foreach (var label in menu.labels)
{
if (label != null && label.CheckIsMeshRendered())
{
count++;
}
}
return count;
}
private static string GetLockReason(StageEndMenu menu, int highlightPosition)
{
if (highlightPosition != 0) return "";
int gameMode = Traverse.Create(menu).Field("gameMode").GetValue<int>();
if (gameMode != 1 && gameMode != 3) return "";
string sceneName = SceneMonitor.mgr != null ? SceneMonitor.mgr.GetActiveSceneName() : "";
if (string.IsNullOrEmpty(sceneName) || SaveManager.mgr == null) return "";
return SaveManager.mgr.GetScore(sceneName) >= 2
? ""
: Loc.Get("locked_requires_two_stars");
}
}
}
}