-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreditsHandler.cs
More file actions
244 lines (204 loc) · 8.07 KB
/
CreditsHandler.cs
File metadata and controls
244 lines (204 loc) · 8.07 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
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
using System.Collections;
using System.Collections.Generic;
using HarmonyLib;
using MelonLoader;
using TMPro;
using UnityEngine;
namespace MelatoninAccess
{
/// <summary>
/// Narrates credits sections and names while the credits list is scrolling.
/// </summary>
public static class CreditsHandler
{
[HarmonyPatch(typeof(Credits), "Show")]
public static class Credits_Show_Patch
{
public static void Postfix(Credits __instance)
{
CreditsNarrationHelper.Reset();
if (!ModConfig.AnnounceCreditsRoll) return;
ScreenReader.Say(Loc.Get("credits_title"), true);
}
}
[HarmonyPatch(typeof(Credits), "TransitionLogoCompanyToCreator")]
public static class Credits_TransitionLogoCompanyToCreator_Patch
{
public static void Postfix(Credits __instance)
{
if (!ModConfig.AnnounceCreditsRoll) return;
MelonCoroutines.Start(CreditsNarrationHelper.AnnounceCreatorDelayed(__instance));
}
}
[HarmonyPatch(typeof(Credits), "ScrollList")]
public static class Credits_ScrollList_Patch
{
public static void Postfix(Credits __instance)
{
if (!ModConfig.AnnounceCreditsRoll) return;
MelonCoroutines.Start(CreditsNarrationHelper.NarrateScrollingEntries(__instance));
}
}
[HarmonyPatch(typeof(Creditor), "ExitToTitle")]
public static class Creditor_ExitToTitle_Patch
{
public static void Postfix()
{
CreditsNarrationHelper.MarkExitingToTitle();
}
}
private static class CreditsNarrationHelper
{
private static bool _isNarrating;
private static bool _isExitingToTitle;
private static readonly List<string> _entries = new List<string>();
private static int _nextEntryIndex;
private static float _entryInterval = 1f;
public static void Reset()
{
_isNarrating = false;
_entries.Clear();
_nextEntryIndex = 0;
_entryInterval = 1f;
_isExitingToTitle = false;
}
public static void MarkExitingToTitle()
{
_isExitingToTitle = true;
_isNarrating = false;
ClearProgress();
}
public static IEnumerator AnnounceCreatorDelayed(Credits credits)
{
yield return new WaitForSecondsRealtime(0.72f);
if (!PrepareEntries(credits)) yield break;
if (!ShouldNarrationSessionAlive(credits)) yield break;
if (IsPausedBySubmenu()) yield break;
if (_nextEntryIndex >= _entries.Count) yield break;
string entry = _entries[_nextEntryIndex];
if (!string.IsNullOrWhiteSpace(entry))
{
ScreenReader.Say(entry, false);
}
_nextEntryIndex++;
}
public static IEnumerator NarrateScrollingEntries(Credits credits)
{
if (_isNarrating) yield break;
if (!PrepareEntries(credits)) yield break;
_isNarrating = true;
yield return WaitWithPauseSupport(0.9f, credits);
if (!ShouldNarrationSessionAlive(credits))
{
ClearProgress();
_isNarrating = false;
yield break;
}
while (_nextEntryIndex < _entries.Count)
{
if (!ShouldNarrationSessionAlive(credits))
{
ClearProgress();
break;
}
if (IsPausedBySubmenu())
{
yield return null;
continue;
}
string entry = _entries[_nextEntryIndex];
_nextEntryIndex++;
if (!string.IsNullOrWhiteSpace(entry))
{
ScreenReader.Say(entry, false);
}
if (_nextEntryIndex >= _entries.Count) break;
yield return WaitWithPauseSupport(_entryInterval, credits);
}
if (_nextEntryIndex >= _entries.Count)
{
ClearProgress();
}
_isNarrating = false;
}
private static IEnumerator WaitWithPauseSupport(float seconds, Credits credits)
{
float elapsed = 0f;
while (elapsed < seconds)
{
if (!ShouldNarrationSessionAlive(credits)) yield break;
if (!IsPausedBySubmenu())
{
elapsed += Time.unscaledDeltaTime;
}
yield return null;
}
}
private static bool PrepareEntries(Credits credits)
{
if (_entries.Count > 0) return true;
_entries.Clear();
_nextEntryIndex = 0;
_entryInterval = 1f;
List<string> collectedEntries = CollectEntries(credits);
if (collectedEntries.Count == 0) return false;
_entries.AddRange(collectedEntries);
float scrollDuration = Mathf.Max(credits.GetScrollDuration(), 1f);
_entryInterval = Mathf.Clamp(scrollDuration / Mathf.Max(_entries.Count, 1), 0.7f, 2f);
return true;
}
private static void ClearProgress()
{
_entries.Clear();
_nextEntryIndex = 0;
_entryInterval = 1f;
}
private static bool ShouldNarrationSessionAlive(Credits credits)
{
if (!ModConfig.AnnounceCreditsRoll) return false;
if (credits == null) return false;
if (!credits.gameObject.activeInHierarchy) return false;
if (_isExitingToTitle) return false;
return true;
}
private static bool IsPausedBySubmenu()
{
return Interface.env != null &&
Interface.env.Submenu != null &&
Interface.env.Submenu.CheckIsActivated();
}
private static List<string> CollectEntries(Credits credits)
{
var entries = new List<string>();
if (credits == null || credits.names == null) return entries;
for (int i = 0; i < credits.names.Length; i++)
{
string entry = BuildEntry(credits, i);
if (!string.IsNullOrWhiteSpace(entry))
{
entries.Add(entry);
}
}
return entries;
}
private static string BuildEntry(Credits credits, int index)
{
if (credits == null) return "";
string label = GetText(credits.labels, index);
string name = GetText(credits.names, index);
if (string.IsNullOrWhiteSpace(label)) return name;
if (string.IsNullOrWhiteSpace(name)) return label;
return $"{label}. {name}";
}
private static string GetText(textboxFragment[] fragments, int index)
{
if (fragments == null || index < 0 || index >= fragments.Length) return "";
textboxFragment fragment = fragments[index];
if (fragment == null) return "";
var tmp = fragment.GetComponent<TextMeshPro>();
if (tmp == null || string.IsNullOrWhiteSpace(tmp.text)) return "";
return tmp.text.Trim();
}
}
}
}