-
Notifications
You must be signed in to change notification settings - Fork 4
/
Patches.cs
206 lines (179 loc) · 6.27 KB
/
Patches.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
using System;
using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.Networking.API;
using ABI_RC.Core.Networking.API.Responses;
using ABI_RC.Core.Networking.IO.Social;
using ABI_RC.Systems.GameEventSystem;
using HarmonyLib;
using MelonLoader;
using System.Threading.Tasks;
namespace BTKUILib
{
internal class Patches
{
internal static UserDetailsResponse LocalUserDetails;
private static HarmonyLib.Harmony _modInstance;
private static bool _firstOnLoadComplete;
internal static void Initialize(HarmonyLib.Harmony modInstance)
{
_modInstance = modInstance;
ApplyPatches(typeof(CVRMenuManagerPatch));
ApplyPatches(typeof(ViewManagerPatches));
ApplyPatches(typeof(UsersPatch));
CVRGameEventSystem.Instance.OnConnectionLost.AddListener((message) =>
{
try
{
QuickMenuAPI.OnWorldLeave?.Invoke();
}
catch (Exception e)
{
BTKUILib.Log.Error(e);
}
});
CVRGameEventSystem.Instance.OnConnected.AddListener(msg =>
{
PlayerList.Instance.OnWorldJoin();
});
CVRGameEventSystem.Instance.OnConnectionRecovered.AddListener(s =>
{
if (PlayerList.Instance == null) return;
try
{
PlayerList.Instance.ResetListAfterConnRecovery();
}
catch (Exception e)
{
BTKUILib.Log.Error(e);
}
});
CVRGameEventSystem.Instance.OnDisconnected.AddListener(s =>
{
try
{
QuickMenuAPI.OnWorldLeave?.Invoke();
}
catch (Exception e)
{
BTKUILib.Log.Error(e);
}
});
CVRGameEventSystem.Authentication.OnLogin.AddListener(resp =>
{
Task.Run(async () =>
{
var profileRequest = (await ApiConnection.MakeRequest<UserDetailsResponse>(ApiConnection.ApiOperation.UserDetails, new
{
userID = resp.UserId
}, null, false)).Data;
LocalUserDetails = profileRequest;
}).ContinueWith(t =>
{
if (!t.IsFaulted) return;
BTKUILib.Log.Error("Unable to retrieve local user details! User entry in PlayerList will have no image!");
});
});
CVRGameEventSystem.World.OnLoad.AddListener((message) =>
{
if (_firstOnLoadComplete) return;
_firstOnLoadComplete = true;
CVRGameEventSystem.Player.OnJoinEntity.AddListener(entity =>
{
try
{
QuickMenuAPI.UserJoin?.Invoke(entity);
}
catch (Exception e)
{
BTKUILib.Log.Error(e);
}
});
CVRGameEventSystem.Player.OnLeaveEntity.AddListener(entity =>
{
try
{
QuickMenuAPI.UserLeave?.Invoke(entity);
}
catch (Exception e)
{
BTKUILib.Log.Error(e);
}
});
});
BTKUILib.Log.Msg("Applied patches!");
}
private static void ApplyPatches(Type type)
{
MelonDebug.Msg($"Applying {type.Name} patches!");
try
{
_modInstance.PatchAll(type);
}
catch (Exception e)
{
BTKUILib.Log.Error($"Failed while patching {type.Name}!");
BTKUILib.Log.Error(e);
}
}
}
[HarmonyPatch(typeof(CVR_MenuManager))]
class CVRMenuManagerPatch
{
[HarmonyPatch("RegisterEvents")]
[HarmonyPostfix]
static void MarkMenuAsReady(CVR_MenuManager __instance)
{
try
{
UserInterface.Instance?.OnMenuRegenerate();
}
catch (Exception e)
{
BTKUILib.Log.Error(e);
}
}
//We'll use this point to detect a menu reload/setup and ensure BTKUIReady is false
[HarmonyPatch("UpdateModList")]
[HarmonyPrefix]
static bool UpdateModListPatch()
{
UserInterface.BTKUIReady = false;
return true;
}
}
[HarmonyPatch(typeof(Users))]
class UsersPatch
{
[HarmonyPatch(nameof(Users.ShowDetails))]
[HarmonyPrefix]
static bool ShowDetailsPrefix(string userId)
{
if (!CVR_MenuManager.Instance.IsQuickMenuOpen || !BTKUILib.Instance.QMPlayerSelectorRedirect.Value) return true;
//QM is open, redirect selection to playerlist
QuickMenuAPI.OpenPlayerListByUserID(userId);
return false;
}
}
[HarmonyPatch(typeof(ViewManager))]
class ViewManagerPatches
{
[HarmonyPatch("SendToWorldUi")]
[HarmonyPostfix]
static void SendToWorldUi(string value)
{
var elapsedTime = DateTime.Now.Subtract(QuickMenuAPI.TimeSinceKeyboardOpen);
//Ensure that we check if the keyboard action was used within 3 minutes, this will avoid the next keyboard usage triggering the action
if (elapsedTime.TotalMinutes <= 3 && (!QuickMenuAPI.KeyboardCloseFired || elapsedTime.TotalSeconds <= 10))
QuickMenuAPI.OnKeyboardSubmitted?.Invoke(value);
QuickMenuAPI.OnKeyboardSubmitted = null;
}
[HarmonyPatch(nameof(ViewManager.KeyboardClosed))]
[HarmonyPostfix]
static void KeyboardClosedPatch()
{
//Update cause the keyboard has been closed
QuickMenuAPI.TimeSinceKeyboardOpen = DateTime.Now;
QuickMenuAPI.KeyboardCloseFired = true;
}
}
}