forked from knah/VRCMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegrityCheckGenerator.cs
222 lines (193 loc) · 13.2 KB
/
IntegrityCheckGenerator.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
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
#nullable enable
namespace IntegrityCheckGenerator
{
[Generator]
class IntegrityCheckGenerator : ISourceGenerator
{
private static readonly DiagnosticDescriptor ourGenerationFailed = new("ICG0000",
"Generation failed", "{0}", "Generators", DiagnosticSeverity.Error, true);
public void Initialize(GeneratorInitializationContext context)
{
}
public void Execute(GeneratorExecutionContext context)
{
string? modTypeName = null;
string? modNamespace = null;
foreach (var tree in context.Compilation.SyntaxTrees)
foreach (var decl in tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>())
{
var baseTypeName = decl.BaseList?.Types.FirstOrDefault()?.Type.ToString();
if (baseTypeName != "MelonMod") continue;
if (decl.Modifiers.All(it => it.ValueText != "partial"))
{
context.ReportDiagnostic(Diagnostic.Create(ourGenerationFailed, decl.GetLocation(), "Mod is not partial"));
continue;
}
if (modTypeName != null)
{
context.ReportDiagnostic(Diagnostic.Create(ourGenerationFailed, decl.GetLocation(), "Too many mods in one project"));
continue;
}
modTypeName = decl.Identifier.ToString();
var symbol = context.Compilation.GetSemanticModel(tree).GetDeclaredSymbol(decl);
modNamespace = symbol!.ContainingNamespace.ToDisplayString();
// hasSceneLoadedDerivative = symbol;
}
if (modTypeName == null)
{
context.ReportDiagnostic(Diagnostic.Create(ourGenerationFailed, null, "Too many mods in one project"));
return;
}
var generatedCode = new StringBuilder();
generatedCode.AppendLine("using HarmonyLib;");
generatedCode.AppendLine("using System;");
generatedCode.AppendLine("using System.Collections;");
generatedCode.AppendLine("using System.IO;");
generatedCode.AppendLine("using System.Linq;");
generatedCode.AppendLine("using System.Reflection;");
generatedCode.AppendLine("using System.Runtime.InteropServices;");
generatedCode.AppendLine("using MelonLoader;");
generatedCode.AppendLine("using UnityEngine;");
generatedCode.AppendLine($"namespace {modNamespace} {{");
generatedCode.AppendLine("[PatchShield]");
generatedCode.AppendLine($"partial class {modTypeName} {{");
generatedCode.AppendLine("internal static bool CheckWasSuccessful;");
generatedCode.AppendLine("internal static bool MustStayFalse = false;");
generatedCode.AppendLine("internal static bool MustStayTrue = true;");
generatedCode.AppendLine("internal static bool RanCheck3 = false;");
generatedCode.AppendLine("private static readonly Func<VRCUiManager> ourGetUiManager;");
generatedCode.AppendLine($"static {modTypeName}() {{");
generatedCode.AppendLine("try { if(typeof(MelonMod).Assembly.GetCustomAttribute<AssemblyProductAttribute>().Product.IndexOf(\"free\", StringComparison.OrdinalIgnoreCase) != -1) {");
PrintCheckFailedCode(generatedCode, 1);
generatedCode.AppendLine("} } catch {");
PrintCheckFailedCode(generatedCode, 1);
generatedCode.AppendLine("}");
generatedCode.AppendLine("CheckA();");
generatedCode.AppendLine("var mm = typeof(MelonUtils).GetMethod(\"ToggleObfuscation\"); if(mm != null) { mm.Invoke(null, null); ");
generatedCode.AppendLine("CheckA(); }");
generatedCode.AppendLine("CheckB();");
generatedCode.AppendLine("ourGetUiManager = (Func<VRCUiManager>) Delegate.CreateDelegate(typeof(Func<VRCUiManager>), typeof(VRCUiManager)");
generatedCode.AppendLine(" .GetProperties(BindingFlags.Static | BindingFlags.Public | BindingFlags.DeclaredOnly)");
generatedCode.AppendLine(" .First(it => it.PropertyType == typeof(VRCUiManager)).GetMethod);");
generatedCode.AppendLine("CheckC();");
generatedCode.AppendLine("CheckWasSuccessful = true;");
generatedCode.AppendLine("}");
generatedCode.AppendLine("partial void OnSceneWasLoaded2(int buildIndex, string sceneName);");
generatedCode.AppendLine("public override void OnSceneWasLoaded(int buildIndex, string sceneName)");
generatedCode.AppendLine("{");
generatedCode.AppendLine(" if (buildIndex != -1 || RanCheck3) return;");
generatedCode.AppendLine(" ");
generatedCode.AppendLine(" try {");
generatedCode.AppendLine(" var harmony = new HarmonyLib.Harmony(Guid.NewGuid().ToString());");
generatedCode.AppendLine($" harmony.Patch(AccessTools.Method(typeof({modTypeName}), nameof(PatchTast)),");
generatedCode.AppendLine($" new HarmonyMethod(typeof({modTypeName}), nameof(ReturnFalse)));");
generatedCode.AppendLine(" PatchTast();");
PrintCheckFailedCode(generatedCode, 2);
generatedCode.AppendLine(" }");
generatedCode.AppendLine(" catch (BadImageFormatException) {}");
generatedCode.AppendLine(" finally { CheckDummyThree(); }");
generatedCode.AppendLine(" RanCheck3 = true;");
generatedCode.AppendLine(" OnSceneWasLoaded2(buildIndex, sceneName);");
generatedCode.AppendLine("}");
generatedCode.AppendLine($"protected {modTypeName}() {{");
generatedCode.AppendLine(" if (CheckWasSuccessful && !MustStayFalse && MustStayTrue) return;");
generatedCode.AppendLine(" ");
PrintCheckFailedCode(generatedCode, 1);
generatedCode.AppendLine("}");
generatedCode.AppendLine("internal static VRCUiManager GetUiManager() => ourGetUiManager();");
generatedCode.AppendLine("private static void DoAfterUiManagerInit(Action code) {");
generatedCode.AppendLine(" MelonCoroutines.Start(OnUiManagerInitCoro(code));");
generatedCode.AppendLine("}");
generatedCode.AppendLine("private static IEnumerator OnUiManagerInitCoro(Action code) {");
generatedCode.AppendLine(" while (GetUiManager() == null)");
generatedCode.AppendLine(" yield return null;");
generatedCode.AppendLine(" code();");
generatedCode.AppendLine("}");
generatedCode.AppendLine("internal static void CheckA() {");
generatedCode.AppendLine(" try {");
generatedCode.AppendLine(" using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(\"_dummy_.dll\");");
generatedCode.AppendLine(" using var memStream = new MemoryStream((int) stream.Length);");
generatedCode.AppendLine(" stream.CopyTo(memStream);");
generatedCode.AppendLine(" Assembly.Load(memStream.ToArray());");
PrintCheckFailedCode(generatedCode, 2);
generatedCode.AppendLine(" }");
generatedCode.AppendLine(" catch (BadImageFormatException) {}");
generatedCode.AppendLine("}");
generatedCode.AppendLine("internal static void CheckB() {");
generatedCode.AppendLine(" try {");
generatedCode.AppendLine(" using var stream1 = Assembly.GetExecutingAssembly().GetManifestResourceStream(\"_dummy2_.dll\");");
generatedCode.AppendLine(" using var memStream1 = new MemoryStream((int) stream1.Length);");
generatedCode.AppendLine(" stream1.CopyTo(memStream1);");
generatedCode.AppendLine(" Assembly.Load(memStream1.ToArray());");
generatedCode.AppendLine(" }");
generatedCode.AppendLine(" catch (BadImageFormatException) {");
PrintCheckFailedCode(generatedCode, 2);
generatedCode.AppendLine(" }");
generatedCode.AppendLine("}");
generatedCode.AppendLine("internal static void CheckC() {");
generatedCode.AppendLine(" try {");
generatedCode.AppendLine(" var harmony = new HarmonyLib.Harmony(Guid.NewGuid().ToString());");
generatedCode.AppendLine($" harmony.Patch(AccessTools.Method(typeof({modTypeName}), nameof(PatchTest)),");
generatedCode.AppendLine($" new HarmonyMethod(typeof({modTypeName}), nameof(ReturnFalse)));");
generatedCode.AppendLine(" PatchTest();");
PrintCheckFailedCode(generatedCode, 2);
generatedCode.AppendLine(" }");
generatedCode.AppendLine(" catch (BadImageFormatException) {}");
generatedCode.AppendLine("}");
generatedCode.AppendLine("private static bool ReturnFalse() => false;");
generatedCode.AppendLine("private static void PatchTest() => throw new BadImageFormatException();");
generatedCode.AppendLine("private static void PatchTast() => throw new BadImageFormatException();");
generatedCode.AppendLine("internal static void CheckDummyThree() {");
generatedCode.AppendLine(" try {");
generatedCode.AppendLine(" using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(\"_dummy3_.dll\");");
generatedCode.AppendLine(" using var memStream = new MemoryStream((int) stream.Length);");
generatedCode.AppendLine(" stream.CopyTo(memStream);");
generatedCode.AppendLine(" Assembly.Load(memStream.ToArray()).GetTypes();");
generatedCode.AppendLine(" while(true);");
generatedCode.AppendLine(" }");
generatedCode.AppendLine(" catch (BadImageFormatException)");
generatedCode.AppendLine(" {");
generatedCode.AppendLine(" }");
generatedCode.AppendLine("}");
generatedCode.AppendLine("private static readonly string[] ourAnnoyingMessages = {");
generatedCode.AppendLine(" \"===================================================================\",");
generatedCode.AppendLine(" \"I'm afraid I can't let you do that, Dave\",");
generatedCode.AppendLine(" \"\",");
generatedCode.AppendLine(" \"You're using MelonLoader with important security features missing.\",");
generatedCode.AppendLine(" \"In addition to such versions being a requirement for malicious mods,\",");
generatedCode.AppendLine(" \"this exposes you to additional risks from certain malicious actors,\",");
generatedCode.AppendLine(" \"including ACCOUNT THEFT, ACCOUNT BANS, and other unwanted consequences\",");
generatedCode.AppendLine(" \"This is not limited to VRChat - other accounts (i.e. Discord) can be affected\",");
generatedCode.AppendLine(" \"This is not what you want, so download the official installer from\",");
generatedCode.AppendLine(" \"https://github.com/LavaGang/MelonLoader/releases\",");
generatedCode.AppendLine(" \"then close this console, and reinstall MelonLoader using it.\",");
generatedCode.AppendLine(" \"\",");
generatedCode.AppendLine(" \"You can read more about why this message is a thing here:\",");
generatedCode.AppendLine(" \"https://github.com/knah/VRCMods/blob/master/Malicious-Mods.md\",");
generatedCode.AppendLine(" \"\",");
generatedCode.AppendLine(" \"Rejecting malicious mods is the only way forward.\",");
generatedCode.AppendLine(" \"Pressing enter will close VRChat.\",");
generatedCode.AppendLine(" \"===================================================================\",");
generatedCode.AppendLine("};");
generatedCode.AppendLine("}");
generatedCode.AppendLine("}");
context.AddSource($"{modTypeName}.Generated.cs", generatedCode.ToString());
}
private static void PrintCheckFailedCode(StringBuilder builder, int indent)
{
var prefix = "".PadLeft(indent * 4, ' ');
builder.AppendLine(prefix + "try {");
builder.AppendLine(prefix + " MustStayFalse = true;");
builder.AppendLine(prefix + " foreach (var message in ourAnnoyingMessages) MelonLogger.Error(message);");
builder.AppendLine(prefix + " Console.In.ReadLine();");
builder.AppendLine(prefix + " Environment.Exit(1);");
builder.AppendLine(prefix + "} finally {");
builder.AppendLine(prefix + " try { Marshal.GetDelegateForFunctionPointer<Action>(Marshal.AllocHGlobal(16))(); } finally { while(true); }");
builder.AppendLine(prefix + "}");
}
}
}