-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathbuffers.js
78 lines (65 loc) · 2.34 KB
/
buffers.js
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
/**
* This example uses the following structs defined in C++:
struct vector {
float x, y, z;
};
struct player {
vector position;
double health;
std::string name;
float distance;
bool alive;
};
*/
// https://github.com/LordVonAdel/structron
const Struct = require('structron');
const memoryjs = require('../index');
const processObject = memoryjs.openProcess('Testing.exe');
const structAddress = 0x000000DEADBEEF;
// -- Step 1: define the structures
// Custom double consumer/producer (since it's not yet implemented in `structron`)
const double = {
read(buffer, offset) {
return buffer.readDoubleLE(offset);
},
write(value, context, offset) {
context.buffer.writeDoubleLE(value, offset);
},
SIZE: 8,
};
// Use string consumer/producer provided by the library (custom implementation for `std::string`),
// pass process handle and base address of structure so the library can read/write the string,
// also requires passing the platform architecture to determine the structure of `std::string`
const string = memoryjs.STRUCTRON_TYPE_STRING(processObject.handle, structAddress, '64');
// Define vector structure
const Vector = new Struct()
.addMember(Struct.TYPES.FLOAT, 'x') // 4 bytes
.addMember(Struct.TYPES.FLOAT, 'y') // 4 bytes
.addMember(Struct.TYPES.FLOAT, 'z'); // 4 bytes
// Define player structure
const Player = new Struct()
.addMember(Vector, 'position') // 12 bytes
.addMember(Struct.TYPES.SKIP(4), 'unused') // compiler padding to put member on 8 byte boundary
.addMember(double, 'health') // 8 bytes
.addMember(string, 'name') // 32 bytes (in 64bit process, 24 bytes in 32bit process)
.addMember(Struct.TYPES.FLOAT, 'distance') // 4 bytes
.addMember(Struct.TYPES.BYTE, 'alive'); // 1 byte
// -- Step 2: create object to write to memory
const object = {
position: {
x: 1.23, y: 4.56, z: 7.89,
},
health: 80.12,
name: 'Example Name 1234567890',
distance: 4.20,
alive: false,
};
// -- Step 3: create buffer from object and write to memory
let context = Player.write(object);
memoryjs.writeBuffer(processObject.handle, structAddress, context.buffer);
// -- Step 4: read buffer from memory and parse
const buffer = memoryjs.readBuffer(processObject.handle, structAddress, context.buffer.length);
context = Player.readContext(buffer);
if (!context.hasErrors()) {
console.log(context.data);
}