-
Notifications
You must be signed in to change notification settings - Fork 5
/
CustomRpc.cs
321 lines (274 loc) · 17.3 KB
/
CustomRpc.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
using System;
using System.Collections.Generic;
using HarmonyLib;
using Hazel;
using static DillyzRoleApi_Rewritten.Il2CppItemAttribute;
namespace DillyzRoleApi_Rewritten
{
internal enum CustomRpc
{
SetRole = 100, // Takes two arguments: PlayerId (byte) & RoleName (string). Sets the role of the player.
ResetRoles = 101, // Takes no arguments. Resets all player roles.
CustomRoleWin = 102, // The default jester role's winnning RPC. All jester stuff will move to a standalone mod in the future.
Assassinate = 103, // Custom-made assassination.
SetSettings = 104, // Setting the settings sets the setting settings.
RoleCheck = 105, // A role check for me to do. If you're missing any roles or have too many, you're kicked out.
ModCheck = 106, // A mod check for me to do. Technically, this is called before role check. 🤓
RevivePlayer = 107, // Revives a player. What do you expect?
CustomRPCCall = 110 // Available spaces for custom RPC. Register your own with DillyzUtil.regRpcCallback("RpcName", delegate(MessageReader reader) {});
}
class CustomRpcCallback {
public string rpcName;
private Action<MessageReader> _callback;
public CustomRpcCallback(string rpcName, Action<MessageReader> callback)
{
this.rpcName = rpcName;
this._callback = callback;
}
public void InvokeCallback(MessageReader reader) {
if (this._callback != null)
{
this._callback(reader);
return;
}
DillyzRoleApiMain.Instance.Log.LogError("Callback for RPC \"" + rpcName + "\" not found! (Did you pass a null callback?)" +
"\n To add a callback, do \"delegate(MessageReader message) {}\" instead of \"null\".");
}
}
[HarmonyPatch(typeof(PlayerControl), nameof(PlayerControl.HandleRpc))]
class CustomRpcHandler {
public static List<CustomRpcCallback> customRpcCallbacks = new List<CustomRpcCallback>();
static void Postfix(byte callId, MessageReader reader) {
switch (callId) {
case (byte)CustomRpc.SetRole:
if (AmongUsClient.Instance.AmHost)
return;
byte playerId = reader.ReadByte();
string roleName = reader.ReadString();
CustomRole.setRoleName(playerId, roleName);
break;
case (byte)CustomRpc.ResetRoles:
if (AmongUsClient.Instance.AmHost)
return;
CustomRole.roleNameMap.Clear();
break;
case (byte)CustomRpc.CustomRoleWin:
GameOverPatch.SetAllToWin(reader.ReadString(), DillyzUtil.findPlayerControl(reader.ReadByte()), false);
break;
case (byte)CustomRpc.Assassinate:
DillyzUtil.commitAssassination(DillyzUtil.findPlayerControl(reader.ReadByte()), DillyzUtil.findPlayerControl(reader.ReadByte()), reader.ReadBoolean());
break;
case (byte)CustomRpc.SetSettings:
if (AmongUsClient.Instance.AmHost)
return;
byte settingsCount = reader.ReadByte();
for (int i = 0; i < settingsCount; i++)
{
string settingToSet = reader.ReadString();
if (settingToSet.StartsWith("LOBBY_ROLE_SETTING-"))
{
int epictrtoll = settingToSet.IndexOf("-") + 1;
string roleToGet_Wthing = settingToSet.Substring(epictrtoll, settingToSet.Length - epictrtoll);
string roleToACTAULLYGet = roleToGet_Wthing.Substring(0, roleToGet_Wthing.LastIndexOf("-"));
int thevaluelol = reader.ReadInt32();
foreach (LobbyRoleSetting newsetting in LobbyConfigManager.lobbyRoleSettings)
{
if (newsetting.roleName == roleToACTAULLYGet)
{
if (roleToGet_Wthing.EndsWith("-Count"))
newsetting.roleCount = thevaluelol;
else if (roleToGet_Wthing.EndsWith("-Chance"))
newsetting.roleChance = thevaluelol;
}
}
continue;
}
// LOBBY_ARS-Sheriff-Punished
if (settingToSet.StartsWith("LOBBY_ARS-"))
{
int epictrtoll = settingToSet.IndexOf("-") + 1;
string roleToGet_Wthing = settingToSet.Substring(epictrtoll, settingToSet.Length - epictrtoll);
string roleToACTAULLYGet = roleToGet_Wthing.Substring(0, roleToGet_Wthing.LastIndexOf("-"));
int welcomeold = roleToGet_Wthing.LastIndexOf("-") + 1;
string typeToActaullyGet = roleToGet_Wthing.Substring(welcomeold, roleToGet_Wthing.Length - welcomeold);
CustomRole role = CustomRole.getByName(roleToACTAULLYGet);
if (role == null)
continue;
foreach (CustomSetting rolesetting in role.advancedSettings)
{
if (rolesetting.title == typeToActaullyGet)
{
switch (rolesetting.settingType)
{
case CustomSettingType.Boolean:
CustomBooleanSetting boolsetting = rolesetting as CustomBooleanSetting;
boolsetting.settingValue = reader.ReadBoolean();
break;
case CustomSettingType.Float:
CustomFloatSetting intsetting = rolesetting as CustomFloatSetting;
intsetting.settingValue = reader.ReadInt32();
break;
case CustomSettingType.String:
CustomStringSetting strsetting = rolesetting as CustomStringSetting;
strsetting.settingValue = reader.ReadString();
break;
}
}
}
continue;
}
DillyzRoleApiMain.Instance.Log.LogError($"Bad setting RPC \"{settingToSet}\" found! (Terminating setting RPC)");
return;
}
LobbyConfigManager.Save();
break;
case (byte)CustomRpc.RoleCheck:
if (AmongUsClient.Instance.AmHost)
return;
int rolesToConstruct = reader.ReadInt32();
List<string> hostRoles = new List<string>();
for (int i = 0; i < rolesToConstruct; i++)
hostRoles.Add(reader.ReadString());
List<string> clientRoles = CustomRole.allRoleNames;
List<string> missingRoles = new List<string>();
List<string> extraRoles = new List<string>();
foreach (string r in hostRoles)
if (!clientRoles.Contains(r))
missingRoles.Add(r);
foreach (string r in clientRoles)
if (!hostRoles.Contains(r))
extraRoles.Add(r);
bool missedRoles = missingRoles.Count != 0, moreRoles = extraRoles.Count != 0;
if (missedRoles || moreRoles) {
AmongUsClient.Instance.LastDisconnectReason = DisconnectReasons.Custom;
AmongUsClient.Instance.LastCustomDisconnect = "Your roles <#FF6A00><b>do not</b></color> match the host's.\n";
if (missedRoles)
{
switch (missingRoles.Count)
{
case 1:
AmongUsClient.Instance.LastCustomDisconnect += "\n<b><#FF0000>Missing</color> role:</b>\n";
AmongUsClient.Instance.LastCustomDisconnect += $"{missingRoles[0]}.";
break;
case 2:
AmongUsClient.Instance.LastCustomDisconnect += "\n<b><#FF0000>Missing</color> roles:</b>\n";
AmongUsClient.Instance.LastCustomDisconnect += $"{missingRoles[0]} & {missingRoles[1]}.";
break;
default:
AmongUsClient.Instance.LastCustomDisconnect += "\n<b><#FF0000>Missing</color> roles:</b>\n";
for (int i = 0; i < missingRoles.Count; i++)
AmongUsClient.Instance.LastCustomDisconnect += (i == missingRoles.Count - 1) ? $"& {missingRoles[i]}." : $"{missingRoles[i]}, ";
break;
}
}
if (moreRoles)
{
switch (extraRoles.Count) {
case 1:
AmongUsClient.Instance.LastCustomDisconnect += "\n<b><#6400FF>Extra</color> role:</b>\n";
AmongUsClient.Instance.LastCustomDisconnect += $"{extraRoles[0]}.";
break;
case 2:
AmongUsClient.Instance.LastCustomDisconnect += "\n<b><#6400FF>Extra</color> roles:</b>\n";
AmongUsClient.Instance.LastCustomDisconnect += $"{extraRoles[0]} & {extraRoles[1]}.";
break;
default:
AmongUsClient.Instance.LastCustomDisconnect += "\n<b><#6400FF>Extra</color> roles:</b>\n";
for (int i = 0; i < extraRoles.Count; i++)
AmongUsClient.Instance.LastCustomDisconnect += (i == extraRoles.Count - 1) ? $"& {extraRoles[i]}." : $"{extraRoles[i]}, ";
break;
}
}
AmongUsClient.Instance.HandleDisconnect(AmongUsClient.Instance.LastDisconnectReason, AmongUsClient.Instance.LastCustomDisconnect);
return;
}
break;
case (byte)CustomRpc.ModCheck:
if (AmongUsClient.Instance.AmHost)
return;
List<PluginBuildInfo> hostPlugins = new List<PluginBuildInfo>();
int pluginsToFind = reader.ReadInt32();
for (int i = 0; i < pluginsToFind; i++)
hostPlugins.Add(new PluginBuildInfo(reader.ReadString(), reader.ReadString(), reader.ReadString()));
List<PluginBuildInfo> missingPlugins = new List<PluginBuildInfo>();
List<PluginBuildInfo> extraPlugins = new List<PluginBuildInfo>();
foreach (PluginBuildInfo plugin in hostPlugins)
{
bool found = false;
foreach (PluginBuildInfo plugin2 in DillyzRoleApiMain.pluginData)
if (plugin2.Name.ToLower() == plugin.Name.ToLower() && plugin2.Version.ToLower() == plugin.Version.ToLower())// && plugin2.Id == plugin.Id)
found = true;
if (!found && plugin.Name.ToLower() != "unityexplorer")
missingPlugins.Add(plugin);
}
foreach (PluginBuildInfo plugin in DillyzRoleApiMain.pluginData)
{
bool found = false;
foreach (PluginBuildInfo plugin2 in hostPlugins)
if (plugin2.Name.ToLower() == plugin.Name.ToLower() && plugin2.Version.ToLower() == plugin.Version.ToLower())// && plugin2.Id == plugin.Id)
found = true;
if (!found && plugin.Name.ToLower() != "unityexplorer")
extraPlugins.Add(plugin);
}
bool missedMods = missingPlugins.Count != 0, moreMods = extraPlugins.Count != 0;
if (!missedMods && !moreMods)
return;
AmongUsClient.Instance.LastDisconnectReason = DisconnectReasons.Custom;
if (missedMods)
{
foreach (PluginBuildInfo plugin in missingPlugins)
switch (missingPlugins.Count)
{
case 1:
AmongUsClient.Instance.LastCustomDisconnect = "<b><#FF0000>Missing</color> mod:</b>\n";
AmongUsClient.Instance.LastCustomDisconnect += $"{missingPlugins[0].Name} (v{missingPlugins[0].Version}).";
break;
case 2:
AmongUsClient.Instance.LastCustomDisconnect = "<b><#FF0000>Missing</color> mods:</b>\n";
AmongUsClient.Instance.LastCustomDisconnect += $"{missingPlugins[0].Name} (v{missingPlugins[0].Version}) & {missingPlugins[1].Name} (v{missingPlugins[1].Version}).";
break;
default:
AmongUsClient.Instance.LastCustomDisconnect = "<b><#FF0000>Missing</color> mods:</b>\n";
for (int i = 0; i < missingPlugins.Count; i++)
AmongUsClient.Instance.LastCustomDisconnect += (i == missingPlugins.Count - 1) ? $"& {missingPlugins[i].Name} (v{missingPlugins[i].Version})." : $"{missingPlugins[i].Name} (v{missingPlugins[i].Version}), ";
break;
}
AmongUsClient.Instance.HandleDisconnect(AmongUsClient.Instance.LastDisconnectReason, AmongUsClient.Instance.LastCustomDisconnect);
return;
}
foreach (PluginBuildInfo plugin in extraPlugins)
switch (extraPlugins.Count)
{
case 1:
AmongUsClient.Instance.LastCustomDisconnect = "<b><#6400FF>Extra</color> mod:</b>\n";
AmongUsClient.Instance.LastCustomDisconnect += $"{extraPlugins[0].Name} (v{extraPlugins[0].Version}).";
break;
case 2:
AmongUsClient.Instance.LastCustomDisconnect = "<b><#6400FF>Extra</color> mods:</b>\n";
AmongUsClient.Instance.LastCustomDisconnect += $"{extraPlugins[0].Name} (v{extraPlugins[0].Version}) & {extraPlugins[1].Name} (v{extraPlugins[1].Version}).";
break;
default:
AmongUsClient.Instance.LastCustomDisconnect = "<b><#6400FF>Extra</color> mods:</b>\n";
for (int i = 0; i < extraPlugins.Count; i++)
AmongUsClient.Instance.LastCustomDisconnect += (i == extraPlugins.Count - 1) ? $"& {extraPlugins[i].Name} (v{extraPlugins[i].Version})." : $"{extraPlugins[i].Name} (v{extraPlugins[i].Version}), ";
break;
}
AmongUsClient.Instance.HandleDisconnect(AmongUsClient.Instance.LastDisconnectReason, AmongUsClient.Instance.LastCustomDisconnect);
break;
case (byte)CustomRpc.RevivePlayer:
DillyzUtil.findPlayerControl(reader.ReadByte()).Revive();
break;
case (byte)CustomRpc.CustomRPCCall:
string rpcToGet = reader.ReadString();
foreach (CustomRpcCallback callback in customRpcCallbacks)
if (callback.rpcName == rpcToGet)
{
callback.InvokeCallback(reader);
return;
}
DillyzRoleApiMain.Instance.Log.LogError("Warning! No rpc called " + rpcToGet + " exists! Why did you even call it?!");
break;
}
}
}
}