bun-memory 1.1.21 → 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 +72 -124
- package/example/trigger-bot.ts +4 -0
- package/package.json +1 -1
- package/structs/Memory.ts +565 -805
- package/structs/Win32Error.ts +28 -284
- package/types/Memory.ts +143 -795
package/README.md
CHANGED
|
@@ -1,171 +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);
|
|
42
38
|
|
|
43
|
-
|
|
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
|
+
```
|
|
44
48
|
|
|
45
|
-
|
|
49
|
+
## API Highlights
|
|
46
50
|
|
|
47
|
-
|
|
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.
|
|
48
56
|
|
|
49
|
-
|
|
50
|
-
const value = memory.i32(address);
|
|
57
|
+
See the code and type definitions for full details. All methods are documented with concise examples.
|
|
51
58
|
|
|
52
|
-
|
|
53
|
-
memory.f32(address, 3.14159);
|
|
59
|
+
## Example: Efficient Buffer Reuse
|
|
54
60
|
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
```ts
|
|
62
|
+
const buffer = Buffer.allocUnsafe(256);
|
|
63
|
+
cs2.read(0x12345678n, buffer); // Fills buffer in-place
|
|
64
|
+
// …use buffer…
|
|
57
65
|
```
|
|
58
66
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
Low-level helpers for pointer resolution and raw, allocation-free memory transfers. Use these when you need maximum control or want to reuse your own scratches.
|
|
62
|
-
|
|
63
|
-
- follow
|
|
64
|
-
- read
|
|
65
|
-
- write
|
|
66
|
-
|
|
67
|
-
### API — Typed Reads / Writes
|
|
68
|
-
|
|
69
|
-
A `Memory` instance exposes typed helpers for reading and writing process memory. Pairs indicate
|
|
70
|
-
scalar and array variants; entries without a pair are scalar-only or array-only.
|
|
71
|
-
|
|
72
|
-
- bool
|
|
73
|
-
- buffer
|
|
74
|
-
- cString
|
|
75
|
-
- f32 / f32Array
|
|
76
|
-
- f64 / f64Array
|
|
77
|
-
- i16 / i16Array
|
|
78
|
-
- i32 / i32Array
|
|
79
|
-
- i64 / i64Array
|
|
80
|
-
- i8 / i8Array
|
|
81
|
-
- matrix3x3
|
|
82
|
-
- matrix3x4
|
|
83
|
-
- matrix4x4
|
|
84
|
-
- networkUtlVector
|
|
85
|
-
- qAngle / qAngleArray
|
|
86
|
-
- quaternion / quaternionArray
|
|
87
|
-
- u16 / u16Array
|
|
88
|
-
- u32 / u32Array
|
|
89
|
-
- u64 / u64Array
|
|
90
|
-
- u8 / u8Array
|
|
91
|
-
- vector2 / vector2Array
|
|
92
|
-
- vector3 / vector3Array
|
|
93
|
-
- vector4 / vector4Array
|
|
94
|
-
|
|
95
|
-
### Efficient Reads / Writes Using Scratches
|
|
96
|
-
|
|
97
|
-
There are many ways to use `scratch`es. Scratches are great for avoiding allocation costs by reusing
|
|
98
|
-
a preexisting array, buffer, string, etc.
|
|
67
|
+
## Example: Pointer Chains
|
|
99
68
|
|
|
100
69
|
```ts
|
|
101
|
-
const
|
|
70
|
+
const address = cs2.follow(0x10000000n, [0x10n, 0x20n]);
|
|
71
|
+
```
|
|
102
72
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
} finally {
|
|
112
|
-
continue;
|
|
113
|
-
}
|
|
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)}`);
|
|
114
80
|
}
|
|
115
81
|
```
|
|
116
82
|
|
|
117
83
|
```ts
|
|
118
|
-
const
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
try {
|
|
123
|
-
memory.read(myAddress, buffer); // Updates buffer and pointers, no allocations…
|
|
124
|
-
|
|
125
|
-
// Do something with your pointers…
|
|
126
|
-
for (const pointer of pointers) {
|
|
127
|
-
// Read a 32 length string at pointer…
|
|
128
|
-
const myString = memory.cString(pointer, 32).toString();
|
|
129
|
-
|
|
130
|
-
// …
|
|
131
|
-
}
|
|
132
|
-
} finally {
|
|
133
|
-
continue;
|
|
134
|
-
}
|
|
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)}`);
|
|
135
88
|
}
|
|
136
89
|
```
|
|
137
90
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
#### `findPattern`
|
|
141
|
-
|
|
142
|
-
Pattern scanning is temporarily disabled but will return shortly.
|
|
143
|
-
|
|
144
|
-
#### `indexOf`
|
|
145
|
-
|
|
146
|
-
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
|
|
147
92
|
|
|
148
93
|
```ts
|
|
149
|
-
//
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
// Get the loaded client.dll…
|
|
153
|
-
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
|
+
```
|
|
154
97
|
|
|
155
|
-
|
|
156
|
-
const needle = Buffer.from([0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00]);
|
|
157
|
-
// const needle = new Uint8Array([0x48, 0x8B, 0x05, 0x00, 0x00, 0x00, 0x00]);
|
|
158
|
-
// const needle = new Uint32Array([0xDEADBEEF, 0x11223344]);
|
|
159
|
-
// const needle = new DataView(Uint8Array.from([0xde, 0xad, 0xbe, 0xef]).buffer);
|
|
98
|
+
## Example: Using Scratches (Recommended)
|
|
160
99
|
|
|
161
|
-
|
|
162
|
-
const
|
|
100
|
+
```ts
|
|
101
|
+
const buffer = Buffer.allocUnsafe(256);
|
|
102
|
+
const array = new Uint64Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / 8);
|
|
163
103
|
|
|
164
|
-
|
|
165
|
-
|
|
104
|
+
while (true) {
|
|
105
|
+
cs2.read(0x10000000n, buffer); // Updates array & buffer without allocations
|
|
106
|
+
for (const element of array) {
|
|
107
|
+
// …use element…
|
|
108
|
+
}
|
|
166
109
|
}
|
|
167
110
|
```
|
|
168
111
|
|
|
169
112
|
## Notes
|
|
170
113
|
|
|
171
|
-
-
|
|
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
|
|