-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHelpers.cs
465 lines (408 loc) · 17.3 KB
/
Helpers.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
using BepInEx.Configuration;
using BepInEx.IL2CPP;
using HarmonyLib;
using Hazel;
using InnerNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnhollowerBaseLib;
using UnityEngine;
namespace TownOfPlus
{
public static class Helpers
{
public static Texture2D LoadTextureFromDisk(string path)
{
try
{
if (File.Exists(path))
{
Texture2D texture = new(2, 2, TextureFormat.ARGB32, true);
byte[] byteTexture = File.ReadAllBytes(path);
LoadImage(texture, byteTexture, false);
return texture;
}
}
catch { }
return null;
}
internal delegate bool d_LoadImage(IntPtr tex, IntPtr data, bool markNonReadable);
internal static d_LoadImage iCall_LoadImage;
private static bool LoadImage(Texture2D tex, byte[] data, bool markNonReadable)
{
if (iCall_LoadImage == null)
iCall_LoadImage = IL2CPP.ResolveICall<d_LoadImage>("UnityEngine.ImageConversion::LoadImage");
var il2cppArray = (Il2CppStructArray<byte>)data;
return iCall_LoadImage.Invoke(tex.Pointer, il2cppArray.Pointer, markNonReadable);
}
public static Sprite CreateSkinSprite(string path)
{
Texture2D texture = LoadTextureFromDisk(path);
if (texture == null)
return null;
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.53f, 0.575f), texture.width * 0.375f);
if (sprite == null)
return null;
texture.hideFlags |= HideFlags.HideAndDontSave | HideFlags.DontUnloadUnusedAsset;
sprite.hideFlags |= HideFlags.HideAndDontSave | HideFlags.DontUnloadUnusedAsset;
return sprite;
}
public static Sprite CreateNamePlateSprite(string path)
{
Texture2D texture = LoadTextureFromDisk(path);
if (texture == null)
return null;
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.505f, 0.4825f), texture.width * 0.375f);
if (sprite == null)
return null;
texture.hideFlags |= HideFlags.HideAndDontSave | HideFlags.DontUnloadUnusedAsset;
sprite.hideFlags |= HideFlags.HideAndDontSave | HideFlags.DontUnloadUnusedAsset;
return sprite;
}
public static void SyncSettings(this GameOptionsData data) =>
PlayerControl.LocalPlayer.RpcSyncSettings(data);
public static bool TryGetPlayer(this byte id, out PlayerControl p)
{
p = PlayerControl.AllPlayerControls.ToArray().FirstOrDefault(f => f?.PlayerId == id);
if (p == null) return false;
return true;
}
public static bool TryGetClient(this byte id, out ClientData client)
{
client = AmongUsClient.Instance.allClients.ToArray().FirstOrDefault(f => f.Character?.PlayerId == id);
if (client == null) return false;
return true;
}
public static bool TryNamePlayer(string name, out string playername, out PlayerControl pc)
{
playername = name;
pc = PlayerControl.AllPlayerControls.ToArray().FirstOrDefault(f => f.Data.PlayerName.RemoveHTML() == name);
if (pc == null) return false;
return true;
}
public static Color GetPlayerRoleColor(RoleTypes RoleType) => RoleType switch
{
RoleTypes.Impostor or RoleTypes.Shapeshifter => Palette.ImpostorRed,
RoleTypes.Engineer or RoleTypes.Scientist or RoleTypes.GuardianAngel => Palette.CrewmateBlue,
_ => Palette.White,
};
public static bool TryGetPlayerColor(int colorid, out Color32 color)
{
color = new Color32();
if (Palette.PlayerColors.Length > colorid)
{
color = Palette.PlayerColors[colorid];
return true;
}
return false;
}
public static bool TryGetPlayerName(int colorid, out string color)
{
color = "";
if (Palette.ColorNames.Length > colorid)
{
color = Palette.ColorNames[colorid].GetTranslation();
return true;
}
return false;
}
public static string GetClientColor(ClientData client) =>
TryGetPlayerColor(client.ColorId, out var color) ? ColorToHex(color) : null;
public static string ColorToHex(Color32 color) =>
color.r.ToString("X2") + color.g.ToString("X2") + color.b.ToString("X2") + color.a.ToString("X2");
public static string GetColorName(ClientData client) =>
TryGetPlayerName(client.ColorId, out var color) ? color : "";
public static void DMChat(ClientData client, string title, string text)
{
StartRpc($"{title}\n{text}", PlayerControl.LocalPlayer.NetId, RpcCalls.SendChat, client.Id);
}
public static void StartRpc(string value, uint targetid, RpcCalls rpccalls, int targetClientid = -1)
{
var msg = AmongUsClient.Instance.StartRpcImmediately(targetid, (byte)rpccalls, SendOption.Reliable, targetClientid);
msg.Write(value);
AmongUsClient.Instance.FinishRpcImmediately(msg);
}
public static Vector3 ScreenToMousePositon =>
Camera.main.ScreenToWorldPoint(Input.mousePosition) - Camera.main.transform.localPosition;
public static void SetPos(this Transform transform, float? x = null, float? y = null, float? z = null)
{
var pos = transform.localPosition;
transform.localPosition = new Vector3(x ?? pos.x, y ?? pos.y, z ?? pos.z);
}
public static void SetPos(this AspectPosition aspectposition, float? x = null, float? y = null, float? z = null)
{
var pos = aspectposition.DistanceFromEdge;
aspectposition.DistanceFromEdge = new Vector3(x ?? pos.x, y ?? pos.y, z ?? pos.z);
}
public static void SetSc(this Transform transform, float? x = null, float? y = null, float? z = null)
{
var pos = transform.localScale;
transform.localScale = new Vector3(x ?? pos.x, y ?? pos.y, z ?? pos.z);
}
public static bool TryGetFileText(string FileUrl, out List<string[]> list)
{
list = new();
if (File.Exists(FileUrl))
{
try
{
using StreamReader sr = new(FileUrl);
while (!sr.EndOfStream)
{
list.Add(sr.ReadLine().Split(','));
}
return true;
}
catch { }
}
return false;
}
public static KeyCode GetKey()
{
foreach (KeyCode code in Enum.GetValues(typeof(KeyCode)))
{
if (Input.GetKey(code))
{
return code;
}
}
return KeyCode.None;
}
public static GameObject Destroy(this GameObject ob, float f = 0)
{
UnityEngine.Object.Destroy(ob, f);
return ob;
}
public static GameObject DontDestroyOnLoad(this GameObject ob)
{
UnityEngine.Object.DontDestroyOnLoad(ob);
return ob;
}
public static bool Contains(this IEnumerable<string> list, string value, StringComparison stringcomparison)
{
return list.Any(a => a.Equals(value, stringcomparison));
}
public static bool ModContains(string mod)
{
foreach (var p in IL2CPPChainloader.Instance.Plugins)
{
if (p.Value.Metadata.Name == mod)
{
return true;
}
}
return false;
}
}
class LateTask
{
public float timer;
public Action action;
public static List<LateTask> Timers = new();
public bool Run(float deltaTime)
{
timer -= deltaTime;
if (timer <= 0)
{
action();
return true;
}
return false;
}
public LateTask(Action action, float timer = 0f)
{
this.action = action;
this.timer = timer;
Timers.Add(this);
}
public static void Update(float deltaTime)
{
List<LateTask> TimersToRemove = new();
Timers.ForEach((Timer) =>
{
if (Timer.Run(deltaTime))
{
TimersToRemove.Add(Timer);
}
});
TimersToRemove.ForEach(Timer => Timers.Remove(Timer));
}
}
[HarmonyPatch(typeof(ModManager), nameof(ModManager.LateUpdate))]
class LateTaskUpdate
{
public static void Postfix()
{
LateTask.Update(Time.deltaTime);
}
}
public static class Flag
{
private static List<string> OneTimeList = new();
private static List<string> FirstRunList = new();
public static void Run(Action action, string type, bool firstrun = false)
{
if ((OneTimeList.Contains(type)) || (firstrun && !FirstRunList.Contains(type)))
{
if (!FirstRunList.Contains(type)) FirstRunList.Add(type);
OneTimeList.Remove(type);
action();
}
}
public static void NewFlag(string type)
{
if (!OneTimeList.Contains(type)) OneTimeList.Add(type);
}
public static void DeleteFlag(string type)
{
if (OneTimeList.Contains(type)) OneTimeList.Remove(type);
}
}
public static class IsChange
{
private static Dictionary<string, object> Dic = new();
private static List<string> SkipList = new();
public static void Run(Action action, object obj, string type, bool firstrun = false)
{
if (!Dic.ContainsKey(type))
{
Dic.Add(type, obj);
if (SkipList.Contains(type)) SkipList.Remove(type);
else if (firstrun) action();
return;
}
if (!Dic[type].Equals(obj))
{
Dic[type] = obj;
if (SkipList.Contains(type)) SkipList.Remove(type);
else action();
}
}
public static void SkipRun(string type)
{
if (!SkipList.Contains(type)) SkipList.Add(type);
}
}
public static class TextPlus
{
public static string AddLine(ref string text, string addtext) => text += "\n" + addtext;
public static string AddLine(this string text) => "\n" + text;
public static string SetColor(ref string text, string color) => text = text.SetColor(color);
public static string SetColor(this string text, string color) => $"<color=#{color.TrimStart('#')}>" + text + "</color>";
public static string SetSize(ref string text, float size) => text = text.SetSize(size);
public static string SetSize(this string text, float size) => $"<size={size}>" + text + "</size>";
public static string RemoveHTML(this string text) => Regex.Replace(text.TrimAll("\n", "\r"), "<[^>]*?>", "");
public static string TrySubstring(this string text, int startindex, int? length = null)
{
if (text.Length > startindex)
{
if (length == null)
return text.Substring(startindex);
if (text.Length >= startindex + length)
return text.Substring(startindex, (int)length);
}
return text;
}
public static string Clipboard(string text = null)
{
if (text != null) GUIUtility.systemCopyBuffer = text;
return GUIUtility.systemCopyBuffer;
}
public static string TrimAll(this string text, params string[] oldValue)
{
oldValue.ToList().ForEach(f => text = text.Replace(f, ""));
return text;
}
public static void AddComChat(this ChatController __instance, string text)
{
ChatCommandUI.IsChatCommand = true;
__instance.AddChat(PlayerControl.LocalPlayer, text);
ChatCommandUI.IsChatCommand = false;
}
public static void RpcSendChat(string text)
{
if (GameState.IsCanSendChat)
{
PlayerControl.LocalPlayer.RpcSendChat(text);
HudManager.Instance.Chat.TimeSinceLastMessage = 0f;
}
}
public static string GetTranslation(this StringNames name) => DestroyableSingleton<TranslationController>.Instance.GetString(name, new Il2CppReferenceArray<Il2CppSystem.Object>(0));
public static char ComWord => main.ChangeComWord?.Getbool() is true ? '/' : '.';
}
public static class Overlay
{
public static SpriteRenderer CreateUnderlay(ConfigEntry<float> x, ConfigEntry<float> y, string name)
{
HudManager hudManager = DestroyableSingleton<HudManager>.Instance;
var underlay = UnityEngine.Object.Instantiate(hudManager.FullScreen, hudManager.transform);
underlay.transform.localPosition = new Vector3(x.Value, y.Value, -900f);
underlay.color = new Color(0.1f, 0.1f, 0.1f, 0.88f);
underlay.name = name + "Underlay";
underlay.gameObject.SetActive(true);
underlay.enabled = true;
return underlay;
}
public static TMPro.TextMeshPro CreateText(ConfigEntry<float> x, ConfigEntry<float> y, string name)
{
HudManager hudManager = DestroyableSingleton<HudManager>.Instance;
var text = UnityEngine.Object.Instantiate(hudManager.TaskText, hudManager.transform);
text.fontSize = text.fontSizeMin = text.fontSizeMax = 1.75f;
text.autoSizeTextContainer = false;
text.enableWordWrapping = false;
text.alignment = TMPro.TextAlignmentOptions.Center;
text.transform.localPosition = new Vector3(x.Value, y.Value, -900f);
text.color = Palette.White;
text.name = name + "text";
text.text = "0";
text.enabled = true;
return text;
}
public static Vector3 SettingPos(ConfigEntry<float> x, ConfigEntry<float> y)
{
if (Input.GetKey(KeyCode.RightArrow))
{
x.Value += 0.05f;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
x.Value -= 0.05f;
}
if (Input.GetKey(KeyCode.DownArrow))
{
y.Value -= 0.05f;
}
if (Input.GetKey(KeyCode.UpArrow))
{
y.Value += 0.05f;
}
if (Input.GetMouseButton(1))
{
x.Value = Helpers.ScreenToMousePositon.x;
y.Value = Helpers.ScreenToMousePositon.y;
}
return new Vector3(x.Value, y.Value, -900f);
}
}
public static class GameState
{
public static bool IsLobby => AmongUsClient.Instance?.GameState is InnerNetClient.GameStates.Joined && !IsFreePlay;
public static bool IsGameStart => AmongUsClient.Instance?.IsGameStarted is true;
public static bool IsLocalGame => AmongUsClient.Instance?.GameMode is GameModes.LocalGame;
public static bool IsHost => AmongUsClient.Instance?.AmHost is true;
public static bool IsFreePlay => AmongUsClient.Instance?.GameMode is GameModes.FreePlay;
public static bool IsMeeting => MeetingHud.Instance != null;
public static bool IsShip => ShipStatus.Instance != null;
public static bool IsChatOpen => HudManager.Instance?.Chat?.IsOpen is true;
public static bool IsChatActive => HudManager.Instance?.Chat?.isActiveAndEnabled is true;
public static bool IsCanSendChat => HudManager.Instance?.Chat?.TimeSinceLastMessage >= 3f;
public static bool IsFocusChatArea => HudManager.Instance?.Chat?.TextArea?.hasFocus is true;
public static bool IsCanKeyCommand => IsFocusChatArea && main.KeyCommand.Getbool();
public static bool IsCanMove => PlayerControl.LocalPlayer?.CanMove is true;
public static bool IsCountDown => GameStartManager.Instance?.startState is GameStartManager.StartingStates.Countdown;
public static bool IsDead => PlayerControl.LocalPlayer?.Data?.IsDead is true;
}
}