forked from lllsondowlll/botw-trainer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGecko.cs
524 lines (420 loc) · 14.2 KB
/
Gecko.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace BotwTrainer
{
public enum Command
{
COMMAND_WRITE_8 = 0x01,
COMMAND_WRITE_16 = 0x02,
COMMAND_WRITE_32 = 0x03,
COMMAND_READ_MEMORY = 0x04,
COMMAND_READ_MEMORY_KERNEL = 0x05,
COMMAND_VALIDATE_ADDRESS_RANGE = 0x06,
COMMAND_MEMORY_DISASSEMBLE = 0x08,
COMMAND_READ_MEMORY_COMPRESSED = 0x09,
COMMAND_KERNEL_WRITE = 0x0B,
COMMAND_KERNEL_READ = 0x0C,
COMMAND_TAKE_SCREEN_SHOT = 0x0D,
COMMAND_UPLOAD_MEMORY = 0x41,
COMMAND_SERVER_STATUS = 0x50,
COMMAND_GET_DATA_BUFFER_SIZE = 0x51,
COMMAND_READ_FILE = 0x52,
COMMAND_READ_DIRECTORY = 0x53,
COMMAND_REPLACE_FILE = 0x54,
COMMAND_GET_CODE_HANDLER_ADDRESS = 0x55,
COMMAND_READ_THREADS = 0x56,
COMMAND_ACCOUNT_IDENTIFIER = 0x57,
COMMAND_WRITE_SCREEN = 0x58,
COMMAND_FOLLOW_POINTER = 0x60,
COMMAND_RPC = 0x70,
COMMAND_GET_SYMBOL = 0x71,
COMMAND_MEMORY_SEARCH = 0x73,
COMMAND_SERVER_VERSION = 0x99,
COMMAND_OS_VERSION = 0x9A,
COMMAND_RUN_KERNEL_COPY_SERVICE = 0xCD
}
public class Gecko
{
private readonly uint _maximumMemoryChunkSize;
private readonly TcpConn _tcpConn;
private readonly MainWindow _mainWindow;
public Gecko(TcpConn tcpConn, MainWindow mainWindow)
{
_tcpConn = tcpConn;
_maximumMemoryChunkSize = 0x400;
_mainWindow = mainWindow;
_maximumMemoryChunkSize = ReadDataBufferSize();
}
public string ByteToHexBitFiddle(byte[] bytes)
{
int count;
try
{
count = bytes.Length;
}
catch (OverflowException overflowException)
{
_mainWindow.LogError(overflowException);
return string.Empty;
}
var c = new char[count * 2];
for (var i = 0; i < count; i++)
{
var b = bytes[i] >> 4;
c[i * 2] = (char)(55 + b + (((b - 10) >> 31) & -7));
b = bytes[i] & 0xF;
c[i * 2 + 1] = (char)(55 + b + (((b - 10) >> 31) & -7));
}
return new string(c);
}
public int GetOsVersion()
{
SendCommand(Command.COMMAND_OS_VERSION);
uint bytesRead = 0;
var response = new byte[4];
_tcpConn.Read(response, 4, ref bytesRead);
uint os;
try
{
os = ByteSwap.Swap(BitConverter.ToUInt32(response, 0));
}
catch (ArgumentOutOfRangeException argumentOutOfRangeException)
{
_mainWindow.LogError(argumentOutOfRangeException);
return -1;
}
catch (ArgumentNullException argumentNullException)
{
_mainWindow.LogError(argumentNullException);
return -1;
}
catch (ArgumentException argumentException)
{
_mainWindow.LogError(argumentException);
return -1;
}
return (int)os;
}
public string GetServerVersion()
{
SendCommand(Command.COMMAND_SERVER_VERSION);
uint bytesRead = 0;
var response = new byte[4];
_tcpConn.Read(response, 4, ref bytesRead);
uint length;
try
{
length = ByteSwap.Swap(BitConverter.ToUInt32(response, 0));
}
catch (ArgumentException argumentException)
{
_mainWindow.LogError(argumentException);
return string.Empty;
}
response = new byte[length];
_tcpConn.Read(response, length, ref bytesRead);
string server;
try
{
server = Encoding.Default.GetString(response);
}
catch (ArgumentException argumentException)
{
_mainWindow.LogError(argumentException);
return string.Empty;
}
return server;
}
public int GetServerStatus()
{
SendCommand(Command.COMMAND_SERVER_STATUS);
uint bytesRead = 0;
var response = new byte[1];
_tcpConn.Read(response, 1, ref bytesRead);
var status = response[0];
return status;
}
public int GetInt(uint address)
{
var bytes = ReadBytes(address, 0x4);
int value;
try
{
Array.Reverse(bytes);
value = !bytes.Any() ? 0 : BitConverter.ToInt32(bytes, 0);
}
catch (ArgumentNullException argumentNullException)
{
_mainWindow.LogError(argumentNullException);
return -1;
}
catch (ArgumentException argumentException)
{
_mainWindow.LogError(argumentException);
return -1;
}
catch (RankException rankException)
{
_mainWindow.LogError(rankException);
return -1;
}
return value;
}
public short GetShort(uint address)
{
var bytes = ReadBytes(address, 0x2);
short value;
try
{
Array.Reverse(bytes);
value = (short) (!bytes.Any() ? 0 : BitConverter.ToInt16(bytes, 0));
}
catch (ArgumentNullException argumentNullException)
{
_mainWindow.LogError(argumentNullException);
return -1;
}
catch (ArgumentException argumentException)
{
_mainWindow.LogError(argumentException);
return -1;
}
catch (RankException rankException)
{
_mainWindow.LogError(rankException);
return -1;
}
return value;
}
public uint GetUInt(uint address)
{
var bytes = ReadBytes(address, 0x4);
uint value;
try
{
Array.Reverse(bytes);
value = !bytes.Any() ? 0 : BitConverter.ToUInt32(bytes, 0);
}
catch (ArgumentNullException argumentNullException)
{
_mainWindow.LogError(argumentNullException);
return 1;
}
catch (ArgumentException argumentException)
{
_mainWindow.LogError(argumentException);
return 1;
}
catch (RankException rankException)
{
_mainWindow.LogError(rankException);
return 1;
}
return value;
}
public string GetString(uint address)
{
var bytes = ReadBytes(address, 0x4);
string value;
try
{
value = !bytes.Any() ? string.Empty : BitConverter.ToString(bytes).Replace("-", string.Empty);
}
catch (ArgumentException argumentException)
{
_mainWindow.LogError(argumentException);
return string.Empty;
}
return value;
}
public byte[] ReadBytes(uint address, uint length)
{
try
{
RequestBytes(address, length);
uint bytesRead = 0;
var response = new byte[1];
_tcpConn.Read(response, 1, ref bytesRead);
var ms = new MemoryStream();
// all zeros
if (response[0] == 0xB0)
{
return ms.ToArray();
}
uint remainingBytesCount = length;
while (remainingBytesCount > 0)
{
uint chunkSize = remainingBytesCount;
// Don't read more bytes than the remote buffer can hold
if (chunkSize > _maximumMemoryChunkSize)
{
chunkSize = _maximumMemoryChunkSize;
}
var buffer = new byte[chunkSize];
bytesRead = 0;
_tcpConn.Read(buffer, chunkSize, ref bytesRead);
ms.Write(buffer, 0, (int)chunkSize);
remainingBytesCount -= chunkSize;
}
return ms.ToArray();
}
catch (Exception ex)
{
_mainWindow.LogError(ex);
}
return null;
}
public void WriteUInt(uint address, uint value)
{
byte[] bytes = BitConverter.GetBytes(value);
try
{
Array.Reverse(bytes);
}
catch (ArgumentNullException argumentNullException)
{
_mainWindow.LogError(argumentNullException);
}
catch (RankException rankException)
{
_mainWindow.LogError(rankException);
}
WriteBytes(address, bytes);
}
public void WriteInt(uint address, int value)
{
byte[] bytes = BitConverter.GetBytes(value);
try
{
Array.Reverse(bytes);
}
catch (ArgumentNullException argumentNullException)
{
_mainWindow.LogError(argumentNullException);
}
catch (RankException rankException)
{
_mainWindow.LogError(rankException);
}
WriteBytes(address, bytes);
}
public void WriteShort(uint address, short value)
{
byte[] bytes = BitConverter.GetBytes(value);
try
{
Array.Reverse(bytes);
}
catch (ArgumentNullException argumentNullException)
{
_mainWindow.LogError(argumentNullException);
}
catch (RankException rankException)
{
_mainWindow.LogError(rankException);
}
WriteBytes(address, bytes);
}
public void WriteFloat(uint address, float value)
{
byte[] bytes = BitConverter.GetBytes(value);
try
{
Array.Reverse(bytes);
}
catch (ArgumentNullException argumentNullException)
{
_mainWindow.LogError(argumentNullException);
}
catch (RankException rankException)
{
_mainWindow.LogError(rankException);
}
WriteBytes(address, bytes);
}
public void WriteBytes(uint address, byte[] bytes)
{
var partitionedBytes = Partition(bytes, _maximumMemoryChunkSize);
WritePartitionedBytes(address, partitionedBytes);
}
private static IEnumerable<byte[]> Partition(byte[] bytes, uint chunkSize)
{
var byteArrayChunks = new List<byte[]>();
uint startingIndex = 0;
while (startingIndex < bytes.Length)
{
var end = Math.Min(bytes.Length, startingIndex + chunkSize);
byteArrayChunks.Add(CopyOfRange(bytes, startingIndex, end));
startingIndex += chunkSize;
}
return byteArrayChunks;
}
private static byte[] CopyOfRange(byte[] src, long start, long end)
{
var len = end - start;
var dest = new byte[len];
Array.Copy(src, start, dest, 0, len);
return dest;
}
private void SendCommand(Command command)
{
uint bytesWritten = 0;
_tcpConn.Write(new[] { (byte)command }, 1, ref bytesWritten);
}
private void RequestBytes(uint address, uint length)
{
try
{
SendCommand(Command.COMMAND_READ_MEMORY);
uint bytesRead = 0;
var bytes = BitConverter.GetBytes(ByteSwap.Swap(address));
var bytes2 = BitConverter.GetBytes(ByteSwap.Swap(address + length));
_tcpConn.Write(bytes, 4, ref bytesRead);
_tcpConn.Write(bytes2, 4, ref bytesRead);
}
catch (Exception ex)
{
_mainWindow.LogError(ex);
}
}
private void WritePartitionedBytes(uint address, IEnumerable<byte[]> byteChunks)
{
IEnumerable<byte[]> enumerable = byteChunks as IList<byte[]> ?? byteChunks.ToList();
var length = (uint)enumerable.Sum(chunk => chunk.Length);
try
{
SendCommand(Command.COMMAND_UPLOAD_MEMORY);
uint bytesRead = 0;
var start = BitConverter.GetBytes(ByteSwap.Swap(address));
var end = BitConverter.GetBytes(ByteSwap.Swap(address + length));
_tcpConn.Write(start, 4, ref bytesRead);
_tcpConn.Write(end, 4, ref bytesRead);
address = enumerable.Aggregate(address, UploadBytes);
}
catch (Exception ex)
{
_mainWindow.LogError(ex);
}
}
private uint UploadBytes(uint address, byte[] bytes)
{
var length = bytes.Length;
uint endAddress = address + (uint)bytes.Length;
uint bytesRead = 0;
_tcpConn.Write(bytes, length, ref bytesRead);
return endAddress;
}
private uint ReadDataBufferSize()
{
SendCommand(Command.COMMAND_GET_DATA_BUFFER_SIZE);
uint bytesRead = 0;
var response = new byte[4];
_tcpConn.Read(response, 4, ref bytesRead);
var buffer = ByteSwap.Swap(BitConverter.ToUInt32(response, 0));
return buffer;
}
}
}