-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeaderboardMod.cs
306 lines (277 loc) · 14.9 KB
/
LeaderboardMod.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
using BepInEx;
using BepInEx.Harmony;
using HarmonyLib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Team17.Online;
using UnityEngine;
using UnityEngine.UI;
namespace OCLeaderboardMod
{
[BepInPlugin("dev.hpmv.overcooked.experimental.interceptors.v1", "Hpmv Overcooked Experimental Interceptors V1", "1.0.0.0")]
[BepInProcess("Overcooked2.exe")]
public class LeaderboardMod : BaseUnityPlugin
{
// Awake is called once when both the game and the plug-in are loaded
void Awake()
{
InstallHooks();
playerCountSwitchButton1 = new LogicalKeycodeButton(KeyCode.Alpha1, ControlPadInput.Button.Invalid);
playerCountSwitchButton2 = new LogicalKeycodeButton(KeyCode.Alpha2, ControlPadInput.Button.Invalid);
playerCountSwitchButton3 = new LogicalKeycodeButton(KeyCode.Alpha3, ControlPadInput.Button.Invalid);
playerCountSwitchButton4 = new LogicalKeycodeButton(KeyCode.Alpha4, ControlPadInput.Button.Invalid);
playerCountSwitchButtonCancel = new LogicalKeycodeButton(KeyCode.Alpha0, ControlPadInput.Button.Invalid);
UnityEngine.Debug.Log("Overcooked 2 Leaderboard Mod V1 Loaded! Brought to you by hpmv & greeny.");
}
void Update()
{
int? original = numPlayersOverride;
if (playerCountSwitchButton1.IsDown())
{
numPlayersOverride = 1;
}
if (playerCountSwitchButton2.IsDown())
{
numPlayersOverride = 2;
}
if (playerCountSwitchButton3.IsDown())
{
numPlayersOverride = 3;
}
if (playerCountSwitchButton4.IsDown())
{
numPlayersOverride = 4;
}
if (playerCountSwitchButtonCancel.IsDown())
{
numPlayersOverride = null;
}
if (original != numPlayersOverride)
{
foreach (var instance in FindObjectsOfType<WorldMapKitchenLevelIconUI>())
{
UpdateLevelPreviewIfApplicable(instance);
}
}
}
private static ILogicalButton playerCountSwitchButton1;
private static ILogicalButton playerCountSwitchButton2;
private static ILogicalButton playerCountSwitchButton3;
private static ILogicalButton playerCountSwitchButton4;
private static ILogicalButton playerCountSwitchButtonCancel;
private static int? numPlayersOverride = null;
public static void InstallHooks()
{
HarmonyWrapper.PatchAll(typeof(LeaderboardMod));
}
private static Type tUserScoreUI = typeof(WorldMapKitchenLevelIconUI).GetNestedType("UserScoreUI", System.Reflection.BindingFlags.NonPublic);
private static FieldInfo field_m_userScoreUis = typeof(WorldMapKitchenLevelIconUI).GetField("m_userScoreUis", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
private static FieldInfo field_m_sceneData = typeof(WorldMapKitchenLevelIconUI).GetField("m_sceneData", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
private static Type tSceneDirectoryEntry = field_m_sceneData.FieldType;
private static FieldInfo field_LoadScreenOverride = tSceneDirectoryEntry.GetField("LoadScreenOverride", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
private static FieldInfo field_container = tUserScoreUI.GetField("m_container", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
private static FieldInfo field_name = tUserScoreUI.GetField("m_name", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
private static FieldInfo field_score = tUserScoreUI.GetField("m_score", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
private static void UpdateLevelPreviewIfApplicable(WorldMapKitchenLevelIconUI instance)
{
var m_userScoreUis = field_m_userScoreUis.GetValue(instance) as Array;
if (m_userScoreUis.Length <= 4)
{
return;
}
var loadScreenOverride = field_LoadScreenOverride.GetValue(field_m_sceneData.GetValue(instance)) as Sprite;
var levelName = loadScreenOverride?.name ?? "unknown";
UpdateScoreUIs(m_userScoreUis, levelName);
}
private static bool UpdateScoreUIs(Array userScoreUIs, string levelName)
{
var levelScores = LeaderboardScoresRepository.Instance.GetOrStartRequestForLevel(levelName);
var playerCount = numPlayersOverride ?? ClientUserSystem.m_Users.Count;
var levelScoresForPlayerCount = levelScores?.SizeScores?.GetValueSafe(playerCount);
for (int i = 4; i < 9; i++)
{
var ui = userScoreUIs.GetValue(i);
(field_container.GetValue(ui) as GameObject).SetActive(true);
if (i == 4)
{
(field_name.GetValue(ui) as T17Text).text = "";
(field_score.GetValue(ui) as T17Text).text = "";
(field_container.GetValue(ui) as GameObject).SetActive(levelScores == null || levelScores.LevelIsSupported);
}
if (i == 5)
{
(field_container.GetValue(ui) as GameObject).SetActive(levelScores == null || levelScores.LevelIsSupported);
if (levelScores == null)
{
(field_name.GetValue(ui) as T17Text).text = "Loading Online Scores...";
}
else
{
(field_name.GetValue(ui) as T17Text).text = "Online Scores for " + playerCount + (playerCount > 1 ? " Players" : " Player");
}
(field_score.GetValue(ui) as T17Text).text = "";
}
if (i >= 6)
{
if (levelScores != null && levelScores.LevelIsSupported && levelScoresForPlayerCount != null &&
levelScoresForPlayerCount.Scores.Count > i - 6)
{
(field_container.GetValue(ui) as GameObject).SetActive(true);
(field_name.GetValue(ui) as T17Text).text = levelScoresForPlayerCount.Scores[i - 6].Place + ". " + levelScoresForPlayerCount.Scores[i - 6].Name;
(field_score.GetValue(ui) as T17Text).text = levelScoresForPlayerCount.Scores[i - 6].Score;
}
else
{
(field_container.GetValue(ui) as GameObject).SetActive(false);
}
}
}
return levelScores != null;
}
private static IEnumerator UpdateScoreUIsCoroutine(Array userScoreUIs, string levelName)
{
// Refresh for at most 2 seconds.
DateTime start = DateTime.Now;
DateTime end = start + new TimeSpan(0, 0, 2);
while (DateTime.Now < end)
{
if (UpdateScoreUIs(userScoreUIs, levelName))
{
yield break;
}
yield return null;
}
}
// Returns the container for user scores.
private static GameObject RearrangeLevelPreviewLayout(GameObject layoutRoot)
{
try
{
var rootPanel = layoutRoot.transform.GetChild(0).GetChild(0).gameObject;
var levelInfo = rootPanel.transform.GetChild(0).gameObject;
if (rootPanel.transform.GetChild(1).gameObject.name == "RightHandSidePanel")
{
// Already rearranged.
return rootPanel.transform.GetChild(1).gameObject;
}
var playerScores = new GameObject[]{
rootPanel.transform.GetChild(1).gameObject,
rootPanel.transform.GetChild(2).gameObject,
rootPanel.transform.GetChild(3).gameObject,
rootPanel.transform.GetChild(4).gameObject,
};
var goButton = rootPanel.transform.GetChild(5).gameObject;
var goButtonIcon = goButton.transform.GetChild(0).gameObject;
var rightHandPanel = new GameObject("RightHandSidePanel", typeof(RectTransform), typeof(CanvasRenderer), typeof(VerticalLayoutGroup));
{
var rightHandPanel_RectTransform = rightHandPanel.GetComponent<RectTransform>();
rightHandPanel_RectTransform.sizeDelta = new Vector2(400, 0);
}
{
var rightHandPanel_VerticalLayoutGroup = rightHandPanel.GetComponent<VerticalLayoutGroup>();
rightHandPanel_VerticalLayoutGroup.spacing = 4;
rightHandPanel_VerticalLayoutGroup.childAlignment = TextAnchor.MiddleCenter;
rightHandPanel_VerticalLayoutGroup.childControlWidth = true;
rightHandPanel_VerticalLayoutGroup.childControlHeight = true;
rightHandPanel_VerticalLayoutGroup.childForceExpandWidth = true;
rightHandPanel_VerticalLayoutGroup.childForceExpandHeight = false;
}
foreach (var playerScore in playerScores)
{
playerScore.transform.SetParent(rightHandPanel.transform, false);
}
rightHandPanel.transform.SetParent(rootPanel.transform, false);
rightHandPanel.transform.SetSiblingIndex(1);
DestroyImmediate(rootPanel.GetComponent<VerticalLayoutGroup>());
{
var rootPanel_HorizontalLayoutGroup = rootPanel.AddComponent<HorizontalLayoutGroup>();
rootPanel_HorizontalLayoutGroup.padding = new RectOffset(50, 50, 20, 40);
rootPanel_HorizontalLayoutGroup.spacing = 10;
rootPanel_HorizontalLayoutGroup.childAlignment = TextAnchor.UpperLeft;
rootPanel_HorizontalLayoutGroup.childControlWidth = false;
rootPanel_HorizontalLayoutGroup.childControlHeight = true;
rootPanel_HorizontalLayoutGroup.childForceExpandWidth = false;
rootPanel_HorizontalLayoutGroup.childForceExpandHeight = false;
}
{
var rootPanel_ContentSizeFitter = rootPanel.GetComponent<ContentSizeFitter>();
rootPanel_ContentSizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
rootPanel_ContentSizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
}
{
var levelInfo_RectTransform = levelInfo.GetComponent<RectTransform>();
levelInfo_RectTransform.sizeDelta = new Vector2(400, 0);
}
{
var goButton_RectTransform = goButton.GetComponent<RectTransform>();
goButton_RectTransform.sizeDelta = new Vector2(0, 0);
}
{
var goButtonIcon_RectTransform = goButtonIcon.GetComponent<RectTransform>();
goButtonIcon_RectTransform.anchoredPosition = new Vector2(10, -290);
}
return rightHandPanel;
} catch (Exception e)
{
Debug.Log("Unable to rearrange level preview " + layoutRoot.name + ": " + e);
return null;
}
}
[HarmonyPostfix]
[HarmonyPatch(typeof(WorldMapKitchenLevelIconUI), "SetUserScores")]
public static void WorldMapSetUserScoresPatch(
WorldMapKitchenLevelIconUI __instance
)
{
var m_userScoreUis = field_m_userScoreUis.GetValue(__instance) as Array;
var existingUserScoreContainer = RearrangeLevelPreviewLayout(__instance.gameObject);
if (existingUserScoreContainer == null)
{
return;
}
var existingUserScoreUi = existingUserScoreContainer.transform.GetChild(0).gameObject;
if (m_userScoreUis.Length == 4)
{
var newUserScoreUis = Array.CreateInstance(tUserScoreUI, 9);
m_userScoreUis.CopyTo(newUserScoreUis, 0);
for (int i = 0; i < 5; i++)
{
var newGo = UnityEngine.Object.Instantiate(existingUserScoreUi);
newGo.transform.SetParent(existingUserScoreContainer.transform, false);
newGo.transform.SetSiblingIndex(4 + i);
var texts = newGo.GetComponentsInChildren<T17Text>();
var newUserScoreUI = Activator.CreateInstance(tUserScoreUI);
field_name.SetValue(newUserScoreUI, texts[0]);
field_score.SetValue(newUserScoreUI, texts[1]);
field_container.SetValue(newUserScoreUI, newGo);
newUserScoreUis.SetValue(newUserScoreUI, 4 + i);
if (i == 0)
{
newGo.GetComponent<T17Image>().color = new Color(0, 0, 0, 0);
newGo.GetComponent<LayoutElement>().preferredHeight = 10;
}
if (i == 1)
{
newGo.GetComponent<T17Image>().color = new Color(0, 0, 0, 0);
newGo.GetComponentsInChildren<T17Text>()[0].color = new Color32(8, 86, 25, 255);
}
if (i >= 2)
{
newGo.GetComponent<T17Image>().color = new Color32(77, 193, 188, 255);
newGo.GetComponentsInChildren<T17Text>()[0].color = new Color32(9, 107, 6, 255);
newGo.GetComponentsInChildren<T17Text>()[1].color = new Color32(245, 255, 73, 255);
}
}
field_m_userScoreUis.SetValue(__instance, newUserScoreUis);
m_userScoreUis = newUserScoreUis;
}
var loadScreenOverride = field_LoadScreenOverride.GetValue(field_m_sceneData.GetValue(__instance)) as Sprite;
var levelName = loadScreenOverride?.name ?? "unknown";
__instance.StartCoroutine(UpdateScoreUIsCoroutine(m_userScoreUis, levelName));
}
}
}