bun-memory 1.1.19 → 1.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +73 -116
- package/example/trigger-bot.ts +4 -0
- package/package.json +1 -1
- package/structs/Memory.ts +577 -760
- package/structs/Win32Error.ts +28 -284
- package/types/Memory.ts +143 -795
package/README.md
CHANGED
|
@@ -1,162 +1,119 @@
|
|
|
1
1
|
# bun-memory
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
Win32 APIs.
|
|
3
|
+
Blazing fast, high-performance Windows process memory manipulation for Bun.
|
|
5
4
|
|
|
6
|
-
##
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`bun-memory` provides fast, allocation-conscious tools for reading and writing memory in external Windows processes. Designed for Bun and Windows 10/11, it exposes a single class, `Memory`, with a clear, type-safe API for all common memory operations.
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
- Efficient buffer management for high-speed operations
|
|
10
|
-
- Pattern scanning for offsets \*
|
|
11
|
-
- Read and write memory of Windows processes
|
|
9
|
+
## Features
|
|
12
10
|
|
|
13
|
-
|
|
11
|
+
- Attach to processes by name or PID
|
|
12
|
+
- Read and write all primitive types, arrays, buffers, and common structures
|
|
13
|
+
- Efficient, allocation-free operations using user-provided buffers (scratches)
|
|
14
|
+
- Module enumeration and pointer chain resolution
|
|
15
|
+
- Typed helpers for vectors, matrices, colors, and more
|
|
14
16
|
|
|
15
17
|
## Requirements
|
|
16
18
|
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
+
- Bun runtime
|
|
20
|
+
- Windows 10 or later
|
|
19
21
|
|
|
20
22
|
## Installation
|
|
21
23
|
|
|
22
|
-
```
|
|
24
|
+
```sh
|
|
23
25
|
bun add bun-memory
|
|
24
26
|
```
|
|
25
27
|
|
|
26
|
-
##
|
|
27
|
-
|
|
28
|
-
See [example/trigger-bot.ts](example/trigger-bot.ts) for a real-world example for Counter-Strike 2.
|
|
29
|
-
|
|
30
|
-
### Basic Example
|
|
28
|
+
## Quick Start
|
|
31
29
|
|
|
32
30
|
```ts
|
|
33
31
|
import Memory from 'bun-memory';
|
|
34
32
|
|
|
35
|
-
// Attach to process by name
|
|
36
|
-
const
|
|
37
|
-
// …or PID…
|
|
38
|
-
const memory = new Memory(1_234);
|
|
33
|
+
// Attach to a process by name
|
|
34
|
+
const cs2 = new Memory('cs2.exe');
|
|
39
35
|
|
|
40
|
-
//
|
|
41
|
-
const
|
|
36
|
+
// Read a float
|
|
37
|
+
const myFloat = cs2.f32(0x12345678n);
|
|
38
|
+
|
|
39
|
+
// Write an int
|
|
40
|
+
cs2.i32(0x12345678n, 42);
|
|
41
|
+
|
|
42
|
+
// Access loaded modules
|
|
43
|
+
const client = cs2.modules['client.dll'];
|
|
44
|
+
|
|
45
|
+
// Clean up
|
|
46
|
+
cs2.close();
|
|
47
|
+
```
|
|
42
48
|
|
|
43
|
-
|
|
49
|
+
## API Highlights
|
|
44
50
|
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
- `follow(address, offsets)` — Follow a pointer chain
|
|
52
|
+
- `read(address, scratch)` — Read memory into a scratch (no allocations)
|
|
53
|
+
- `write(address, scratch)` — Write a scratch to memory
|
|
54
|
+
- Module map: `memory.modules['client.dll']`
|
|
55
|
+
- Typed accessors: `bool`, `f32`, `i32`, `matrix4x4`, `u8`, `u64Array`, `vector3`, etc.
|
|
47
56
|
|
|
48
|
-
|
|
49
|
-
const value = memory.i32(client.base + 0x12345678n);
|
|
57
|
+
See the code and type definitions for full details. All methods are documented with concise examples.
|
|
50
58
|
|
|
51
|
-
|
|
52
|
-
memory.f32(client.base + 0x12345678n, 3.14159);
|
|
59
|
+
## Example: Efficient Buffer Reuse
|
|
53
60
|
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
```ts
|
|
62
|
+
const buffer = Buffer.allocUnsafe(256);
|
|
63
|
+
cs2.read(0x12345678n, buffer); // Fills buffer in-place
|
|
64
|
+
// …use buffer…
|
|
56
65
|
```
|
|
57
66
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
A `Memory` instance exposes typed helpers for reading and writing process memory. Pairs indicate
|
|
61
|
-
scalar and array variants; entries without a pair are scalar-only or array-only.
|
|
62
|
-
|
|
63
|
-
- bool
|
|
64
|
-
- buffer
|
|
65
|
-
- cString
|
|
66
|
-
- f32 / f32Array
|
|
67
|
-
- f64 / f64Array
|
|
68
|
-
- i16 / i16Array
|
|
69
|
-
- i32 / i32Array
|
|
70
|
-
- i64 / i64Array
|
|
71
|
-
- i8 / i8Array
|
|
72
|
-
- matrix3x3
|
|
73
|
-
- matrix3x4
|
|
74
|
-
- matrix4x4
|
|
75
|
-
- networkUtlVector
|
|
76
|
-
- qAngle / qAngleArray
|
|
77
|
-
- quaternion / quaternionArray
|
|
78
|
-
- u16 / u16Array
|
|
79
|
-
- u32 / u32Array
|
|
80
|
-
- u64 / u64Array
|
|
81
|
-
- u8 / u8Array
|
|
82
|
-
- vector2 / vector2Array
|
|
83
|
-
- vector3 / vector3Array
|
|
84
|
-
- vector4 / vector4Array
|
|
85
|
-
|
|
86
|
-
### Efficient Reads / Writes Using Scratches
|
|
87
|
-
|
|
88
|
-
There are many ways to use `scratch`es. Scratches are great for avoiding allocation costs by reusing
|
|
89
|
-
a preexisting array, buffer, string, etc.
|
|
67
|
+
## Example: Pointer Chains
|
|
90
68
|
|
|
91
69
|
```ts
|
|
92
|
-
const
|
|
70
|
+
const address = cs2.follow(0x10000000n, [0x10n, 0x20n]);
|
|
71
|
+
```
|
|
93
72
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
} finally {
|
|
103
|
-
continue;
|
|
104
|
-
}
|
|
73
|
+
## Example: Searching Memory
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
const needle = Buffer.from([0x48, 0x8b, 0x05]);
|
|
77
|
+
const address = cs2.indexOf(needle, 0x10000000n, 0x1000);
|
|
78
|
+
if (address !== -1n) {
|
|
79
|
+
console.log(`Found at 0x${address.toString(16)}`);
|
|
105
80
|
}
|
|
106
81
|
```
|
|
107
82
|
|
|
108
83
|
```ts
|
|
109
|
-
const
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
try {
|
|
114
|
-
memory.read(myAddress, buffer); // Updates buffer and pointers, no allocations…
|
|
115
|
-
|
|
116
|
-
// Do something with your pointers…
|
|
117
|
-
for (const pointer of pointers) {
|
|
118
|
-
// Read a 32 length string at pointer…
|
|
119
|
-
const myString = memory.cString(pointer, 32).toString();
|
|
120
|
-
|
|
121
|
-
// …
|
|
122
|
-
}
|
|
123
|
-
} finally {
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
84
|
+
const needle = new Uint32Array([0x01, 0x02, 0x03]);
|
|
85
|
+
const address = cs2.indexOf(needle, 0x10000000n, 0x1000);
|
|
86
|
+
if (address !== -1n) {
|
|
87
|
+
console.log(`Found at 0x${address.toString(16)}`);
|
|
126
88
|
}
|
|
127
89
|
```
|
|
128
90
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
#### `findPattern`
|
|
132
|
-
|
|
133
|
-
Pattern scanning is temporarily disabled but will return shortly.
|
|
134
|
-
|
|
135
|
-
#### `indexOf`
|
|
136
|
-
|
|
137
|
-
Find the first occurrence of a byte sequence within a memory range and return the absolute address (`bigint`) of the first match, or `-1n` when not found. Accepts any Scratch-compatible input (`Buffer`, `TypedArray`, `DataView`, `ArrayBuffer`, or `SharedArrayBuffer`). No region or protection checking is performed; ensure the range is readable before calling.
|
|
91
|
+
## Example: Typed Arrays
|
|
138
92
|
|
|
139
93
|
```ts
|
|
140
|
-
//
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
// Get the loaded client.dll…
|
|
144
|
-
const client = cs2.modules['client.dll'];
|
|
94
|
+
const array = cs2.f32Array(0x12345678n, 4); // Float32Array of length 4
|
|
95
|
+
cs2.i32Array(0x12345678n, new Int32Array([1, 2, 3, 4]));
|
|
96
|
+
```
|
|
145
97
|
|
|
146
|
-
|
|
147
|
-
const needle = Buffer.from([0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00]);
|
|
148
|
-
// const needle = new Uint8Array([0x48, 0x8B, 0x05, 0x00, 0x00, 0x00, 0x00]);
|
|
149
|
-
// const needle = new Uint32Array([0xDEADBEEF, 0x11223344]);
|
|
150
|
-
// const needle = new DataView(Uint8Array.from([0xde, 0xad, 0xbe, 0xef]).buffer);
|
|
98
|
+
## Example: Using Scratches (Recommended)
|
|
151
99
|
|
|
152
|
-
|
|
153
|
-
const
|
|
100
|
+
```ts
|
|
101
|
+
const buffer = Buffer.allocUnsafe(256);
|
|
102
|
+
const array = new Uint64Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 8);
|
|
154
103
|
|
|
155
|
-
|
|
156
|
-
|
|
104
|
+
while (true) {
|
|
105
|
+
cs2.read(0x10000000n, buffer); // Updates array & buffer without allocations
|
|
106
|
+
for (const element of array) {
|
|
107
|
+
// …use element…
|
|
108
|
+
}
|
|
157
109
|
}
|
|
158
110
|
```
|
|
159
111
|
|
|
160
112
|
## Notes
|
|
161
113
|
|
|
162
|
-
-
|
|
114
|
+
- Pattern scanning is temporarily disabled.
|
|
115
|
+
- Windows only. Bun runtime required.
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
For real-world usage, see [example/trigger-bot.ts](example/trigger-bot.ts).
|
package/example/trigger-bot.ts
CHANGED
|
@@ -34,6 +34,10 @@ const { dwEntityList, dwGlobalVars, dwLocalPlayerController, dwLocalPlayerPawn }
|
|
|
34
34
|
[K in keyof (typeof OffsetsJSON)['client.dll']]: bigint;
|
|
35
35
|
};
|
|
36
36
|
|
|
37
|
+
const a: bigint[] = [1n];
|
|
38
|
+
|
|
39
|
+
console.log(typeof a);
|
|
40
|
+
|
|
37
41
|
// Open a handle to cs2.exe…
|
|
38
42
|
const cs2 = new Memory('cs2.exe');
|
|
39
43
|
|