-
Notifications
You must be signed in to change notification settings - Fork 1
/
VanguardImplementation.cs
550 lines (502 loc) · 16.7 KB
/
VanguardImplementation.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using OpenGL;
using EasyHook;
using RTCV.Common;
using RTCV.CorruptCore;
using RTCV.NetCore;
using RTCV.Vanguard;
using RTCV.UI;
using RTCV.NetCore.Commands;
using System.IO;
namespace XemuVanguardHook
{
public class MemoryDomainCPUMemory : IMemoryDomain
{
public string Name => "System Memory";
public bool BigEndian => false;
public long Size => VanguardImplementation.vanguard_getMemorySize();
public int WordSize => 4;
public ICodeCavesDomain CodeCaves { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override string ToString() => Name;
public MemoryDomainCPUMemory()
{
}
public void PokeByte(long address, byte val)
{
if (address > Size)
{
return;
}
VanguardImplementation.GPA_WRITEB(address, val);
}
public byte PeekByte(long addr)
{
if (addr > Size)
{
return 0;
}
byte buffer = 0;
return (byte)VanguardImplementation.GPA_READB(addr, buffer);
}
public byte[] PeekBytes(long address, int length)
{
var returnArray = new byte[length];
for (var i = 0; i < length; i++)
returnArray[i] = PeekByte(address + i);
return returnArray;
}
public byte[] GetMemory()
{
return PeekBytes(0, (int)Size);
}
}
public class DummyCodeCavesDomain : ICodeCavesDomain //dummy until I implement code caves in this
{
public Dictionary<long, ICodeCave> Caves { get => null; set { } }
public long Size { get => 0; set { } }
public string Name => "DummyCodeCavesDomain";
public int WordSize => 0;
public bool BigEndian => false;
public (long, ulong) AllocateMemory(int size)
{
return (0, 0);
}
public byte PeekByte(long addr)
{
return 0;
}
public byte[] PeekBytes(long address, int length)
{
return new byte[] { 0 };
}
public void PokeByte(long addr, byte val)
{
}
}
public class MemoryDomainFlashROM : IMemoryDomain
{
public string Name => "Flash ROM";
public bool BigEndian => false;
public long Size => 0xFFFFFF + 1;
public int WordSize => 4;
public ICodeCavesDomain CodeCaves { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public override string ToString() => Name;
public MemoryDomainFlashROM()
{
}
public void PokeByte(long address, byte val)
{
if (address > Size)
{
return;
}
VanguardImplementation.GPA_WRITEB(address + 0xFF000000, val);
}
public byte PeekByte(long addr)
{
if (addr > Size)
{
return 0;
}
byte buffer = 0;
return (byte)VanguardImplementation.GPA_READB(addr + 0xFF000000, buffer);
}
public byte[] PeekBytes(long address, int length)
{
var returnArray = new byte[length];
for (var i = 0; i < length; i++)
returnArray[i] = PeekByte(address + i);
return returnArray;
}
public byte[] GetMemory()
{
return PeekBytes(0, (int)Size);
}
}
public class MemoryDomainVirtualMemory : IMemoryDomain, ICodeCavable
{
public string Name => "Virtual Memory";
public bool BigEndian => false;
public long Size => 0x100000000;
public int WordSize => 4;
public ICodeCavesDomain CodeCaves { get; set; }
public override string ToString() => Name;
public MemoryDomainVirtualMemory()
{
}
public void PokeByte(long address, byte val)
{
if (address > Size)
{
return;
}
VanguardImplementation.gva_writeb((ulong)address, val);
}
public byte PeekByte(long addr)
{
if (addr > Size)
{
return 0;
}
return VanguardImplementation.gva_readb((ulong)addr);
}
public byte[] PeekBytes(long address, int length)
{
var returnArray = new byte[length];
for (var i = 0; i < length; i++)
returnArray[i] = PeekByte(address + i);
return returnArray;
}
public byte[] GetMemory()
{
return /*PeekBytes(0, (int)Size)*/ null;
}
}
public class MemoryDomainNV2A : IMemoryDomain
{
public string Name => "NV2A Registers";
public bool BigEndian => false;
public long Size => 0xFFFFFF+1;
public int WordSize => 4;
public override string ToString() => Name;
public MemoryDomainNV2A()
{
}
public void PokeByte(long address, byte val)
{
if (address > Size)
{
return;
}
address += 0xFD000000;
VanguardImplementation.GPA_WRITEB(address, val);
}
public byte PeekByte(long addr)
{
if (addr > Size)
{
return 0;
}
addr += 0xFD000000;
byte buffer = 0;
return (byte)VanguardImplementation.GPA_READB(addr, buffer);
}
public byte[] PeekBytes(long address, int length)
{
var returnArray = new byte[length];
for (var i = 0; i < length; i++)
returnArray[i] = PeekByte(address + i);
return returnArray;
}
}
public class MemoryDomainAPU : IMemoryDomain
{
public string Name => "APU Registers";
public bool BigEndian => false;
public long Size => 0x7FFFF;
public int WordSize => 4;
public override string ToString() => Name;
public MemoryDomainAPU()
{
}
public void PokeByte(long address, byte val)
{
if (address > Size)
{
return;
}
address += 0xFE800000;
VanguardImplementation.GPA_WRITEB(address, val);
}
public byte PeekByte(long addr)
{
if (addr > Size)
{
return 0;
}
addr += 0xFE800000;
byte buffer = 0;
return (byte)VanguardImplementation.GPA_READB(addr, buffer);
}
public byte[] PeekBytes(long address, int length)
{
var returnArray = new byte[length];
for (var i = 0; i < length; i++)
returnArray[i] = PeekByte(address + i);
return returnArray;
}
}
public class VanguardImplementation
{
[DllImport("xemu.exe")]
public static extern byte gpa_readb(ulong addr, byte buf);
[DllImport("xemu.exe")]
public static extern void gpa_writeb(ulong addr, byte buf);
[DllImport("xemu.exe")]
public static extern byte gva_readb(ulong addr);
[DllImport("xemu.exe")]
public static extern void gva_writeb(ulong addr, byte buf);
[DllImport("xemu.exe", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern void vanguard_savevm_state(char* cmd);
[DllImport("xemu.exe", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern void vanguard_loadvm_state(char* cmd);
[DllImport("xemu.exe")]
public static extern int vanguard_getMemorySize();
[DllImport("xemu.exe", CallingConvention = CallingConvention.Cdecl)]
public static extern void vanguard_setMemorySize(int size);
[DllImport("xemu.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern string vanguard_getHDDPath();
[DllImport("xemu.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern void vanguard_setHDDPath([MarshalAs(UnmanagedType.LPStr)] string path);
[DllImport("xemu.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.LPWStr)]
public static extern string vanguard_getDVDPath();
[DllImport("xemu.exe", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern void vanguard_setDVDPath([MarshalAs(UnmanagedType.LPStr)] string path);
[DllImport("xemu.exe", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern void vanguard_setMainThreadCommand(char* command);
[DllImport("xemu.exe", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern void vanguard_setMainThreadCommandCharArg(char* arg);
public static byte GPA_READB(long addr, byte buf)
{
return gpa_readb((ulong)addr, buf);
}
public static void GPA_WRITEB(long addr, byte buf)
{
gpa_writeb((ulong)addr, buf);
}
public static unsafe void SaveVMState(string filename, string realfilepath)
{
//vanguard_savevm_state(filename);
string cmd = "savevm " + filename;
vanguard_savevm_state((char*)Marshal.StringToHGlobalAnsi(cmd).ToPointer());
Thread.Sleep(1000);
}
public static unsafe void LoadVMState(string filename)
{
//vanguard_loadvm_state(filename);
string cmd = "loadvm " + filename;
vanguard_loadvm_state((char*)Marshal.StringToHGlobalAnsi(cmd).ToPointer());
Thread.Sleep(1000);
}
public static string SaveSavestate(string Key, bool threadSave = false)
{
string quickSlotName = Key + ".timejump";
string prefix = VanguardCore.GameName;
//Since I can't figure out how to save vm states outside of the hdd file,
//I'll just save them internally for now, but that makes it impossible to share the states.
//well, since they save the state of the hdd (I think) too they would be copyrighted files anyway
string path = Path.Combine(RtcCore.workingDir, "SESSION", prefix + "." + quickSlotName + ".State");
//File.Create(path); //make dummy savestate file
File.WriteAllText(path, prefix + "." + Key); //make a file that points to the savestate in the qcow2
VanguardImplementation.SaveVMState(prefix + "." + Key, path);
return path;
}
public static bool LoadSavestate(string path, StashKeySavestateLocation stateLocation = StashKeySavestateLocation.DEFAULTVALUE)
{
StepActions.ClearStepBlastUnits();
RtcClock.ResetCount();
string Key = File.ReadAllText(path);
VanguardImplementation.LoadVMState(Key);
return true;
}
public static string SaveDrive()
{
string drivepath = vanguard_getHDDPath();
System.IO.FileInfo fi = new FileInfo(drivepath);
string key = $"HDD_{RtcCore.GetRandomKey()}.{fi.Extension}";
string destdrivepath = Path.Combine(RtcCore.workingDir, "VAULT", key);
System.IO.File.Copy(drivepath, destdrivepath);
return destdrivepath;
}
public void LoadDrive(string packagepath)
{
vanguard_setHDDPath(packagepath);
}
public static string GetGameName()
{
string gamename = "IGNORE";
int i = 0;
MemoryDomainProxy xboxsdram = MemoryDomains.GetProxy("Virtual Memory", 0);
while (i < xboxsdram.Size)
{
if (xboxsdram.PeekByte(i) == 0x58)
{
if (xboxsdram.PeekByte(i + 1) == 0x42)
{
if (xboxsdram.PeekByte(i + 2) == 0x45)
{
if (xboxsdram.PeekByte(i + 3) == 0x48)
{
//MessageBox.Show("Found an XBE!");
int xbestart = i;
int certificateaddress = BitConverter.ToInt32(xboxsdram.PeekBytes(xbestart + 0x0118, xbestart + 0x0118 + 0x4, true), 0) - 0x10000;
gamename = System.Text.Encoding.ASCII.GetString(xboxsdram.PeekBytes(xbestart + certificateaddress + 0xC, xbestart + certificateaddress + 0xC + 0x50, true)).Replace("\0", "");
return gamename;
}
}
}
}
i++;
}
return gamename;
}
public static string GetShortGameName()
{
string gamename = GetGameName();
string shortgamename = gamename.Trim().Replace(" ", "").Substring(0, 8);
return shortgamename;
}
public static void LoadDVD(string dvd)
{
vanguard_setDVDPath(dvd);
}
public static string GetDVD()
{
return vanguard_getDVDPath();
}
//[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
//delegate
public static RTCV.Vanguard.VanguardConnector connector = null;
public static void StartClient()
{
try
{
ConsoleEx.WriteLine("Starting Vanguard Client");
Thread.Sleep(500);
var spec = new NetCoreReceiver();
spec.Attached = VanguardCore.attached;
spec.MessageReceived += OnMessageRecieved;
connector = new RTCV.Vanguard.VanguardConnector(spec);
//while(true)
// {
// }
}
catch (Exception ex)
{
ConsoleEx.WriteLine("ERROR");
throw new RTCV.NetCore.AbortEverythingException();
}
}
public static void RestartClient()
{
connector?.Kill();
connector = null;
StartClient();
}
public static void RefreshDomains()
{
if (VanguardImplementation.connector.netcoreStatus != RTCV.NetCore.Enums.NetworkStatus.CONNECTED)
return;
PartialSpec gameDone = new PartialSpec("VanguardSpec");
gameDone[VSPEC.MEMORYDOMAINS_INTERFACES] = GetInterfaces();
//gameDone[VSPEC.GAMENAME] = GetGameName();
AllSpec.VanguardSpec.Update(gameDone);
LocalNetCoreRouter.Route(RTCV.NetCore.Endpoints.CorruptCore, RTCV.NetCore.Commands.Remote.EventDomainsUpdated, true, true);
}
public static MemoryDomainProxy[] GetInterfaces()
{
Console.WriteLine($"getInterfaces()");
List<MemoryDomainProxy> interfaces = new List<MemoryDomainProxy>();
MemoryDomainCPUMemory cpumem = new MemoryDomainCPUMemory();
interfaces.Add(new MemoryDomainProxy(cpumem));
MemoryDomainVirtualMemory gva = new MemoryDomainVirtualMemory();
interfaces.Add(new MemoryDomainProxy(gva));
MemoryDomainFlashROM flashROM = new MemoryDomainFlashROM();
interfaces.Add(new MemoryDomainProxy(flashROM));
//MemoryDomainNV2A geforce3reg = new MemoryDomainNV2A();
//interfaces.Add(new MemoryDomainProxy(geforce3reg));
//MemoryDomainAPU apureg = new MemoryDomainAPU(); //for some reason, xemu's code for reading and writing to the apu
//REQUIRES that the value be 32-bit. There's an assert function that
//IGNORES the fact I globally disabled all assertions and crashes
//whenever the value is 8-bit.
//So I'll just comment this out for now :(
//interfaces.Add(new MemoryDomainProxy(apureg));
return interfaces.ToArray();
}
private static void OnMessageRecieved(object sender, NetCoreEventArgs e)
{
var message = e.message;
var simpleMessage = message as NetCoreSimpleMessage;
var advancedMessage = message as NetCoreAdvancedMessage;
ConsoleEx.WriteLine(message.Type);
switch (message.Type) //Handle received messages here
{
case RTCV.NetCore.Commands.Remote.AllSpecSent:
{
if (VanguardCore.FirstConnect)
{
SyncObjectSingleton.FormExecute(() => { ; });
VanguardCore.FirstConnect = false;
}
//VanguardCore.LOAD_GAME_DONE();
RefreshDomains();
}
break;
case RTCV.NetCore.Commands.Basic.SaveSavestate:
SyncObjectSingleton.EmuThreadExecute(() => { e.setReturnValue(SaveSavestate(advancedMessage.objectValue as string)); }, true);
break;
case RTCV.NetCore.Commands.Basic.LoadSavestate:
{
var cmd = advancedMessage.objectValue as object[];
var path = cmd[0] as string;
var location = (StashKeySavestateLocation)cmd[1];
SyncObjectSingleton.EmuThreadExecute(() => { e.setReturnValue(LoadSavestate(path, location)); }, true);
break;
}
case RTCV.NetCore.Commands.Remote.LoadROM:
{
var fileName = advancedMessage.objectValue as string;
//VanguardCore.LOAD_GAME_START(fileName);
//LoadDVD(fileName);
}
break;
case RTCV.NetCore.Commands.Remote.CloseGame:
{
//SyncObjectSingleton.FormExecute(() => { Hooks.CLOSE_GAME(true); });
}
break;
case RTCV.NetCore.Commands.Remote.DomainGetDomains:
SyncObjectSingleton.FormExecute(() =>
{
e.setReturnValue(GetInterfaces());
});
break;
case RTCV.NetCore.Commands.Remote.DomainRefreshDomains:
RefreshDomains();
break;
case RTCV.NetCore.Commands.Remote.KeySetSyncSettings:
//SyncObjectSingleton.FormExecute(() => { Hooks.BIZHAWK_GETSET_SYNCSETTINGS = (string)advancedMessage.objectValue; });
break;
case RTCV.NetCore.Commands.Emulator.GetRealtimeAPI:
e.setReturnValue(VanguardCore.RTE_API);
break;
case RTCV.NetCore.Commands.Remote.EventEmuStarted:
//if (RTC_StockpileManager.BackupedState == null)
//S.GET<RTC_Core_Form>().AutoCorrupt = false;
//Todo
//RTC_NetcoreImplementation.SendCommandToBizhawk(new RTC_Command("REMOTE_PUSHVMDS) { objectValue = MemoryDomains.VmdPool.Values.Select(it => (it as VirtualMemoryDomain).Proto).ToArray() }, true, true);
//Thread.Sleep(100);
// if (RTC_StockpileManager.BackupedState != null)
// S.GET<RTC_MemoryDomains_Form>().RefreshDomainsAndKeepSelected(RTC_StockpileManager.BackupedState.SelectedDomains.ToArray());
// if (S.GET<RTC_Core_Form>().cbUseGameProtection.Checked)
// RTC_GameProtection.Start();
break;
case RTCV.NetCore.Commands.Remote.OpenHexEditor:
SyncObjectSingleton.FormExecute(() => LocalNetCoreRouter.Route("HEXEDITOR", Remote.OpenHexEditor, true));
break;
case RTCV.NetCore.Commands.Remote.EventCloseEmulator:
Environment.Exit(-1);
break;
}
}
}
}