-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUIUtils.cs
138 lines (119 loc) · 4.93 KB
/
UIUtils.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
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using ABI_RC.Core.InteractionSystem;
using ABI_RC.Core.Player;
using ABI_RC.Core.Savior;
using ABI_RC.Core.UI;
using cohtml.Net;
using MelonLoader;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using UnityEngine;
namespace BTKUILib
{
/// <summary>
/// Basic utilities used within the UI
/// </summary>
public static class UIUtils
{
private static MD5 _hasher = MD5.Create();
private static FieldInfo _internalCohtmlView = typeof(CohtmlControlledViewWrapper).GetField("_view", BindingFlags.Instance | BindingFlags.NonPublic);
private static PropertyInfo _selfUsername = typeof(MetaPort).Assembly.GetType("ABI_RC.Core.Networking.AuthManager").GetProperty("Username", BindingFlags.Static | BindingFlags.Public);
private static FieldInfo _animatorGetter = typeof(PuppetMaster).GetField("_animator", BindingFlags.Instance | BindingFlags.NonPublic);
private static MethodInfo _kickUserMethod = typeof(MetaPort).Assembly.GetType("ABI_RC.Core.Networking.Guardian.GuardianExtendedControls").GetMethod("KickUser", BindingFlags.Static | BindingFlags.NonPublic);
private static View _internalViewCache;
/// <summary>
/// Check if the CVR_MenuManager view is ready
/// </summary>
/// <returns>True if view is ready, false if it's not</returns>
public static bool IsQMReady()
{
if (CVR_MenuManager.Instance == null)
return false;
return UserInterface.BTKUIReady;
}
/// <summary>
/// Clean non alphanumeric characters from a given string
/// </summary>
/// <param name="input">Input string</param>
/// <returns>Cleaned string</returns>
public static string GetCleanString(string input)
{
return input == null ? null : Regex.Replace(Regex.Replace(input, "<.*?>", string.Empty), @"[^0-9a-zA-Z_]+", string.Empty);
}
/// <summary>
/// Get stream from an EmbeddedResource with a given name
/// </summary>
/// <param name="iconName"></param>
/// <returns></returns>
public static Stream GetIconStream(string iconName)
{
var melon = MelonUtils.GetMelonFromStackTrace();
string assemblyName = melon.MelonAssembly.Assembly.GetName().Name;
return melon.MelonAssembly.Assembly.GetManifestResourceStream($"{assemblyName}.Resources.{iconName}");
}
/// <summary>
/// Gets the private Animator from PuppetMaster
/// </summary>
/// <param name="pm">Target puppet master</param>
/// <returns>Private avatar animator</returns>
public static Animator GetAvatarAnimator(PuppetMaster pm)
{
if (pm == null) return null;
return (Animator)_animatorGetter.GetValue(pm);
}
/// <summary>
/// Gets the username of the local user
/// </summary>
/// <returns>Local users username</returns>
public static string GetSelfUsername()
{
return (string)_selfUsername.GetValue(null);
}
internal static void KickUser(string uuid)
{
_kickUserMethod.Invoke(null, [uuid]);
}
internal static string CreateMD5(string input)
{
// Use input string to calculate MD5 hash
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
return CreateMD5(inputBytes);
}
internal static string CreateMD5(byte[] bytes)
{
byte[] hashBytes = _hasher.ComputeHash(bytes);
// Convert the byte array to hexadecimal string
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
internal static View GetInternalView()
{
if (CVR_MenuManager.Instance == null || CVR_MenuManager.Instance.quickMenu == null) return null;
if (_internalViewCache == null)
_internalViewCache = (View)_internalCohtmlView.GetValue(CVR_MenuManager.Instance.quickMenu.View);
return _internalViewCache;
}
internal static string[] GetPrettyEnumNames<T>() where T : Enum
{
return Enum.GetNames(typeof(T)).Select(PrettyFormatEnumName).ToArray();
}
internal static int GetEnumIndex<T>(T value) where T : Enum
{
return Array.IndexOf(Enum.GetValues(typeof(T)), value);
}
private static string PrettyFormatEnumName(string name)
{
// adds spaces before capital letters (excluding the first letter)
return System.Text.RegularExpressions.Regex.Replace(name, "(\\B[A-Z])", " $1");
}
}
}