forked from EmberVulpix/DifferentSLIAuto
/
frmPatcher.cs
429 lines (397 loc) · 20.6 KB
/
frmPatcher.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
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using XMplayerBasedOnKurapicaMapper;
namespace DifferentSLIAuto
{
public partial class frmPatcher : Form
{
/// <summary>
/// m_vFileToPatch
///
/// The contents of the file to be patched.
/// </summary>
private byte[] m_vFileToPatch;
private string m_DriverFile;
private ListBoxLog listBoxLog;
internal enum uFMOD_Flags
{
XM_RESOURCE = 0,
XM_MEMORY = 1,
XM_FILE = 2,
XM_NOLOOP = 8,
XM_SUSPENDED = 16
}
internal delegate IntPtr PlaySong(IntPtr lpXM, int param, uFMOD_Flags fdwSong);
internal delegate IntPtr SetVolume(int vol);
public frmPatcher()
{
InitializeComponent();
listBoxLog = new ListBoxLog(_listBox);
}
private void frmPatcher_Load(object sender, EventArgs e)
{
listBoxLog.Log(ListBoxLog.Level.Info, string.Format("Welcome to {0}", this.Text));
listBoxLog.Log(ListBoxLog.Level.Info, string.Format("Press \"{0}\" to patch your driver.", btnPatch.Text));
byte[] dat = Properties.Resources.ufmod;
DynamicDllLoader loader = new DynamicDllLoader();
// Load DLL
bool loaded = loader.LoadLibrary(dat);
if (!loaded)
{
listBoxLog.Log(ListBoxLog.Level.Warning, "Could not load uFMOD to play chiptune.");
return;
}
// Get method Address
uint addrSetVolume = loader.GetProcAddress("uFMOD_SetVolume");
uint addrPlaySong = loader.GetProcAddress("uFMOD_PlaySong");
using (UnmanagedMemoryStream xm = (UnmanagedMemoryStream)Assembly.GetEntryAssembly().GetManifestResourceStream("DifferentSLIAuto.moonflight.xm"))
{
if (xm == null)
{
listBoxLog.Log(ListBoxLog.Level.Warning, "Could not load XM resource to play chiptune.");
return;
}
SetVolume invokeSetVolume = (SetVolume)Marshal.GetDelegateForFunctionPointer((IntPtr)addrSetVolume, typeof(SetVolume));
PlaySong invokePlaySong = (PlaySong)Marshal.GetDelegateForFunctionPointer((IntPtr)addrPlaySong, typeof(PlaySong));
unsafe
{
invokeSetVolume(19);
invokePlaySong((IntPtr)xm.PositionPointer, (int)xm.Length, uFMOD_Flags.XM_MEMORY);
}
}
}
private void btnPatch_Click(object sender, EventArgs e)
{
btnPatch.Enabled = false;
Thread thread = new Thread(PatcherThread);
thread.IsBackground = true;
thread.Start();
}
private readonly byte[][][] patch5To10Bytes =
{
new [] { new byte[] { 0x0F, 0xBA, 0x00, 0x11 }, new byte[] { 0x0F, 0xBA, 0x00, 0x18 }, new byte[] { 0x0F, 0xBA, 0x00, 0x19 }, new byte[] { 0x0F, 0xBA, 0x00, 0x0B }, new byte[] { 0x0F, 0xBA, 0x00, 0x0C }, new byte[] { 0x0F, 0xBA, 0x00, 0x10 } },
new [] { new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x11 }, new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x18 }, new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x19 }, new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x0B }, new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x0C }, new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x10 } },
new [] { new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11 }, new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18 }, new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x19 }, new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0B }, new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C }, new byte[] { 0x0F, 0xBA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10 } }
};
private void PatcherThread()
{
int[] patches = new int[10];
int[] patchPerms = new int[10];
int patchTemp = 0;
if (!File.Exists("nvlddmkm.sys"))
{
if (!File.Exists("nvlddmkm2.sys"))
{
listBoxLog.Log(ListBoxLog.Level.Error, "Could not find \"nvlddmkm.sys\" or \"nvlddmkm2.sys,\" please place it in the patcher directory.");
btnPatch.Enabled = true;
return;
}
else
{
m_DriverFile = "nvlddmkm2.sys";
}
}
else
{
m_DriverFile = "nvlddmkm.sys";
}
if ((patches[0] = FindPattern(new byte[] { 0x74, 0x00, 0xFE, 0x81, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xBE, 0x81 }, "x?xx????xxx", 0x600)) == -1)
listBoxLog.Log(ListBoxLog.Level.Error, "Could not find patch #1. Please inform Ember @ techPowerUp! forums.");
else if ((patches[1] = FindPattern(new byte[] { 0x74, 0x00, 0xFE, 0x81, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xBE, 0x81 }, "x?xx????xxx", patches[0] + 0xA)) == -1)
listBoxLog.Log(ListBoxLog.Level.Error, "Could not find patch #2. Please inform Ember @ techPowerUp! forums.");
if ((patches[2] = FindPattern(new byte[] { 0x85, 0xC0, 0x74, 0x00, 0x41, 0x0F, 0xBA, 0x2C, 0x24, 0x0F }, "xxx?xxxxxx", 0x600)) == -1)
listBoxLog.Log(ListBoxLog.Level.Error, "Could not find patch #3. Please inform Ember @ techPowerUp! forums.");
if ((patches[3] = FindPattern(new byte[] { 0x75, 0x00, 0x0F, 0xBA, 0xE8, 0x00, 0x89, 0x45, 0x00, 0x85, 0xC0, 0x0F, 0x85, 0x00, 0x00, 0x00, 0x00, 0x85, 0xDB, 0x0F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x33 }, "x?xxx?xx?xxxx????xxxx????x", 0x600)) == -1)
{
listBoxLog.Log(ListBoxLog.Level.Info, "Could not find patch #4, falling back to permutation #1");
if ((patches[3] = FindPattern(new byte[] { 0x75, 0x00, 0x0F, 0xBA, 0x6D, 0x00, 0x0E, 0x85, 0xDB, 0x0F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x33 }, "x?xxx?xxxxx????x", 0x600)) == -1)
{
listBoxLog.Log(ListBoxLog.Level.Info, "Could not find patch #4, falling back to permutation #2");
if ((patches[3] = FindPattern(new byte[] { 0x75, 0x00, 0x0F, 0xBA, 0xAC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x85, 0xF6, 0x0F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x33 }, "x?xxx??????xxxx????x", 0x600)) == -1)
{
listBoxLog.Log(ListBoxLog.Level.Info, "Could not find patch #4, falling back to permutation #3");
if ((patches[3] = FindPattern(new byte[] { 0x75, 0x00, 0x0F, 0xBA, 0xE8, 0x0E, 0x89, 0x45, 0x40, 0x85, 0xC0, 0x0F, 0x85, 0x00, 0x00, 0x00, 0x00, 0x85, 0xFF, 0x0F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x44 }, "x?xxxxxxxxxxx????xxxx????x", 0x600)) == -1) //337.50
{
listBoxLog.Log(ListBoxLog.Level.Info, "Could not find patch #4, falling back to permutation #4");
if ((patches[3] = FindPattern(new byte[] { 0x75, 0x05, 0x0F, 0xBA, 0x6D, 0x00, 0x00, 0x85, 0xF6, 0x0F, 0x84 }, "xxxxx??xxxx", 0x600)) == -1) //307.32
{
listBoxLog.Log(ListBoxLog.Level.Error, "Could not find patch #4 at all. Please inform Ember @ techPowerUp! forums.");
}
else patchPerms[3] = 4;
}
else patchPerms[3] = 3;
}
else patchPerms[3] = 2;
}
else patchPerms[3] = 1;
}
else patchPerms[3] = 0;
if (patches[3] != -1)
{
for (int x = 4; x <= 9; x++)
{
if ((patchTemp = FindPattern(patch5To10Bytes[0][x - 4], "xx?x", patches[x - 1])) != -1 && (patchTemp - patches[x - 1] < 0x200))
{
patches[x] = patchTemp;
patchPerms[x] = 0;
}
else if ((patchTemp = FindPattern(patch5To10Bytes[1][x - 4], "xx??x", patches[x - 1])) != -1 && (patchTemp - patches[x - 1] < 0x200))
{
patches[x] = patchTemp;
patchPerms[x] = 1;
}
else if ((patchTemp = FindPattern(patch5To10Bytes[2][x - 4], "xx??????x", patches[x - 1])) != -1 && (patchTemp - patches[x - 1] < 0x200))
{
patches[x] = patchTemp;
patchPerms[x] = 2;
}
else
{
listBoxLog.Log(ListBoxLog.Level.Error, string.Format("Could not find patch #{0}. Please inform Ember @ techPowerUp! forums.", x + 1));
patches[x] = -1;
}
}
}
if (!patches.Any(t => t == -1))
{
m_vFileToPatch[patches[0] + 2] = 0x90;
m_vFileToPatch[patches[0] + 3] = 0x90;
m_vFileToPatch[patches[0] + 4] = 0x90;
m_vFileToPatch[patches[0] + 5] = 0x90;
m_vFileToPatch[patches[0] + 6] = 0x90;
m_vFileToPatch[patches[0] + 7] = 0x90;
m_vFileToPatch[patches[1] + 2] = 0x90;
m_vFileToPatch[patches[1] + 3] = 0x90;
m_vFileToPatch[patches[1] + 4] = 0x90;
m_vFileToPatch[patches[1] + 5] = 0x90;
m_vFileToPatch[patches[1] + 6] = 0x90;
m_vFileToPatch[patches[1] + 7] = 0x90;
m_vFileToPatch[patches[2]] = 0x31;
if (patchPerms[3] == 0)
{
m_vFileToPatch[patches[3]] = 0x90;
m_vFileToPatch[patches[3] + 1] = 0x90;
m_vFileToPatch[patches[3] + 2] = 0x90;
m_vFileToPatch[patches[3] + 3] = 0x90;
m_vFileToPatch[patches[3] + 4] = 0x90;
m_vFileToPatch[patches[3] + 5] = 0x90;
m_vFileToPatch[patches[3] + 6] = 0xC7;
m_vFileToPatch[patches[3] + 9] = 0x00;
m_vFileToPatch[patches[3] + 10] = 0x00;
m_vFileToPatch[patches[3] + 11] = 0x00;
m_vFileToPatch[patches[3] + 12] = 0x00;
m_vFileToPatch[patches[3] + 13] = 0x90;
m_vFileToPatch[patches[3] + 14] = 0x90;
m_vFileToPatch[patches[3] + 15] = 0x90;
m_vFileToPatch[patches[3] + 16] = 0x90;
m_vFileToPatch[patches[3] + 17] = 0x90;
m_vFileToPatch[patches[3] + 18] = 0x90;
m_vFileToPatch[patches[3] + 19] = 0x90;
m_vFileToPatch[patches[3] + 20] = 0x90;
m_vFileToPatch[patches[3] + 21] = 0x90;
m_vFileToPatch[patches[3] + 22] = 0x90;
m_vFileToPatch[patches[3] + 23] = 0x90;
m_vFileToPatch[patches[3] + 24] = 0x90;
}
else if (patchPerms[3] == 1)
{
m_vFileToPatch[patches[3]] = 0x90;
m_vFileToPatch[patches[3] + 1] = 0x90;
m_vFileToPatch[patches[3] + 2] = 0x90;
m_vFileToPatch[patches[3] + 3] = 0xC7;
m_vFileToPatch[patches[3] + 4] = 0x45;
m_vFileToPatch[patches[3] + 6] = 0x00;
m_vFileToPatch[patches[3] + 7] = 0x00;
m_vFileToPatch[patches[3] + 8] = 0x00;
m_vFileToPatch[patches[3] + 9] = 0x00;
m_vFileToPatch[patches[3] + 10] = 0x90;
m_vFileToPatch[patches[3] + 11] = 0x90;
m_vFileToPatch[patches[3] + 12] = 0x90;
m_vFileToPatch[patches[3] + 13] = 0x90;
m_vFileToPatch[patches[3] + 14] = 0x90;
}
else if (patchPerms[3] == 2)
{
m_vFileToPatch[patches[3]] = 0x90;
m_vFileToPatch[patches[3] + 1] = 0x90;
m_vFileToPatch[patches[3] + 2] = 0x90;
m_vFileToPatch[patches[3] + 3] = 0xC7;
m_vFileToPatch[patches[3] + 4] = 0x84;
m_vFileToPatch[patches[3] + 7] = 0x00;
m_vFileToPatch[patches[3] + 8] = 0x00;
m_vFileToPatch[patches[3] + 9] = 0x00;
m_vFileToPatch[patches[3] + 10] = 0x00;
m_vFileToPatch[patches[3] + 11] = 0x00;
m_vFileToPatch[patches[3] + 12] = 0x00;
m_vFileToPatch[patches[3] + 13] = 0x00;
m_vFileToPatch[patches[3] + 14] = 0x90;
m_vFileToPatch[patches[3] + 15] = 0x90;
m_vFileToPatch[patches[3] + 16] = 0x90;
m_vFileToPatch[patches[3] + 17] = 0x90;
m_vFileToPatch[patches[3] + 18] = 0x90;
}
else if (patchPerms[3] == 3) //337.50 beta
{
m_vFileToPatch[patches[3]] = 0x90;
m_vFileToPatch[patches[3] + 1] = 0x90;
m_vFileToPatch[patches[3] + 2] = 0x90;
m_vFileToPatch[patches[3] + 3] = 0x90;
m_vFileToPatch[patches[3] + 4] = 0x90;
m_vFileToPatch[patches[3] + 5] = 0x90;
m_vFileToPatch[patches[3] + 6] = 0xC7;
m_vFileToPatch[patches[3] + 7] = 0x45;
m_vFileToPatch[patches[3] + 9] = 0x00;
m_vFileToPatch[patches[3] + 10] = 0x00;
m_vFileToPatch[patches[3] + 11] = 0x00;
m_vFileToPatch[patches[3] + 12] = 0x00;
m_vFileToPatch[patches[3] + 13] = 0x90;
m_vFileToPatch[patches[3] + 14] = 0x90;
m_vFileToPatch[patches[3] + 15] = 0x90;
m_vFileToPatch[patches[3] + 16] = 0x90;
m_vFileToPatch[patches[3] + 17] = 0x90;
m_vFileToPatch[patches[3] + 18] = 0x90;
m_vFileToPatch[patches[3] + 19] = 0x90;
m_vFileToPatch[patches[3] + 20] = 0x90;
m_vFileToPatch[patches[3] + 21] = 0x90;
m_vFileToPatch[patches[3] + 22] = 0x90;
m_vFileToPatch[patches[3] + 23] = 0x90;
m_vFileToPatch[patches[3] + 24] = 0x90;
}
else if (patchPerms[3] == 4) //307.32
{
m_vFileToPatch[patches[3]] = 0x90;
m_vFileToPatch[patches[3] + 1] = 0x90;
m_vFileToPatch[patches[3] + 2] = 0x90;
m_vFileToPatch[patches[3] + 3] = 0xC7;
m_vFileToPatch[patches[3] + 4] = 0x45;
m_vFileToPatch[patches[3] + 6] = 0x00;
m_vFileToPatch[patches[3] + 7] = 0x00;
m_vFileToPatch[patches[3] + 8] = 0x00;
m_vFileToPatch[patches[3] + 9] = 0x00;
m_vFileToPatch[patches[3] + 10] = 0x90;
m_vFileToPatch[patches[3] + 11] = 0x90;
m_vFileToPatch[patches[3] + 12] = 0x90;
m_vFileToPatch[patches[3] + 13] = 0x90;
m_vFileToPatch[patches[3] + 14] = 0x90;
}
for (int x = 4; x <= 9; x++)
{
if (patchPerms[x] == 0)
{
m_vFileToPatch[patches[x]] = 0x90;
m_vFileToPatch[patches[x] + 1] = 0x90;
m_vFileToPatch[patches[x] + 2] = 0x90;
m_vFileToPatch[patches[x] + 3] = 0x90;
}
else if (patchPerms[x] == 1)
{
m_vFileToPatch[patches[x]] = 0x90;
m_vFileToPatch[patches[x] + 1] = 0x90;
m_vFileToPatch[patches[x] + 2] = 0x90;
m_vFileToPatch[patches[x] + 3] = 0x90;
m_vFileToPatch[patches[x] + 4] = 0x90;
}
else if (patchPerms[x] == 0)
{
m_vFileToPatch[patches[x]] = 0x90;
m_vFileToPatch[patches[x] + 1] = 0x90;
m_vFileToPatch[patches[x] + 2] = 0x90;
m_vFileToPatch[patches[x] + 3] = 0x90;
m_vFileToPatch[patches[x] + 4] = 0x90;
m_vFileToPatch[patches[x] + 5] = 0x90;
m_vFileToPatch[patches[x] + 6] = 0x90;
m_vFileToPatch[patches[x] + 7] = 0x90;
m_vFileToPatch[patches[x] + 8] = 0x90;
}
}
File.Move(m_DriverFile, string.Concat(m_DriverFile, ".bak"));
using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(m_DriverFile)))
{
bw.Write(m_vFileToPatch, 0, m_vFileToPatch.Length);
bw.Flush();
bw.Close();
}
listBoxLog.Log(ListBoxLog.Level.Success, string.Format("Patching was successful!"));
}
else
{
listBoxLog.Log(ListBoxLog.Level.Critical, string.Format("Patching was aborted because all of the patch locations could not be found."));
}
btnPatch.Enabled = true;
}
#region "sigScan Class Private Methods"
/// <summary>
/// MaskCheck
///
/// Compares the current pattern byte to the current memory dump
/// byte to check for a match. Uses wildcards to skip bytes that
/// are deemed unneeded in the compares.
/// </summary>
/// <param name="nOffset">Offset in the dump to start at.</param>
/// <param name="btPattern">Pattern to scan for.</param>
/// <param name="strMask">Mask to compare against.</param>
/// <returns>Boolean depending on if the pattern was found.</returns>
private bool MaskCheck(int nOffset, byte[] btPattern, string strMask)
{
// Loop the pattern and compare to the mask and dump.
for (int x = 0; x < btPattern.Length; x++)
{
// If the mask char is a wildcard, just continue.
if (strMask[x] == '?')
continue;
// If the mask char is not a wildcard, ensure a match is made in the pattern.
if ((strMask[x] == 'x') && (btPattern[x] != this.m_vFileToPatch[nOffset + x]))
return false;
}
// The loop was successful so we found the pattern.
return true;
}
/// <summary>
/// FindPattern
///
/// Attempts to locate the given pattern inside the dumped memory region
/// compared against the given mask. If the pattern is found, the offset
/// is added to the located address and returned to the user.
/// </summary>
/// <param name="btPattern">Byte pattern to look for in the dumped region.</param>
/// <param name="strMask">The mask string to compare against.</param>
/// <param name="startAddress">The offset to start searching.</param>
/// <returns>-1 if not found, address if found, and -2 on retard.</returns>
private int FindPattern(byte[] btPattern, string strMask, int startAddress)
{
try
{
// Read the file to patch into memory if we have not read it yet.
if (this.m_vFileToPatch == null || this.m_vFileToPatch.Length == 0)
{
this.m_vFileToPatch = File.ReadAllBytes(m_DriverFile);
}
if (startAddress < 0) return -1;
// Ensure the mask and pattern lengths match.
if (strMask.Length != btPattern.Length)
return -2;
// Loop the region and look for the pattern.
for (int x = startAddress; x < this.m_vFileToPatch.Length; x++)
{
if (this.MaskCheck(x, btPattern, strMask))
{
// The pattern was found, return it.
return x;
}
}
// Pattern was not found.
return -1;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}