bun-memory 1.1.21 → 1.1.23

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/types/Memory.ts CHANGED
@@ -1,884 +1,232 @@
1
1
  /**
2
- * Type definitions for the Memory module providing cross-process memory manipulation.
3
- *
4
- * This module exports type definitions used by the Memory class for type-safe
5
- * interaction with process memory, modules, and various data structures commonly
6
- * found in native applications and games.
7
- *
8
- * These types ensure proper TypeScript support when working with:
9
- * - Process modules and their metadata
10
- * - Memory regions and their properties
11
- * - Mathematical structures (vectors and quaternions)
12
- * - FFI-compatible buffer types for memory operations
13
- *
2
+ * Represents a loaded module in a process.
3
+ * @property base Base address of the module.
4
+ * @property name Module filename.
5
+ * @property size Module size in bytes.
14
6
  * @example
15
- * ```typescript
16
- * import Memory, { Module, Vector3, Quaternion } from './Memory';
17
- *
18
- * const memory = new Memory('game.exe');
19
- *
20
- * // Type-safe module access
21
- * const mainModule: Module = memory.modules['game.exe'];
22
- * console.log(`Base: 0x${mainModule.base.toString(16)}`);
23
- *
24
- * // Type-safe vector operations
25
- * const position: Vector3 = memory.vector3(0x12345678n);
26
- * memory.vector3(0x12345678n, { x: position.x + 10, y: position.y, z: position.z });
27
- *
28
- * memory.close();
29
- * ```
30
- */
31
-
32
- /**
33
- * Represents a loaded module within a target process.
34
- *
35
- * Modules are executable files (EXE, DLL) that have been loaded into a process's
36
- * address space. Each module has a base address where it's loaded, a name
37
- * (typically the filename), and a size indicating how much memory it occupies.
38
- *
39
- * This information is essential for:
40
- * - Calculating absolute addresses from relative offsets
41
- * - Understanding memory layout of the target process
42
- * - Identifying specific libraries or executables
43
- * - Memory scanning within specific module boundaries
44
- *
45
- * @example
46
- * ```typescript
47
- * const memory = new Memory('notepad.exe');
48
- * const modules = memory.modules;
49
- *
50
- * // Access the main executable module
51
- * const mainModule: Module = modules['notepad.exe'];
52
- * console.log(`Main module loaded at: 0x${mainModule.base.toString(16)}`);
53
- * console.log(`Module size: ${mainModule.size} bytes`);
54
- *
55
- * // Access a system library
56
- * const kernel32: Module = modules['kernel32.dll'];
57
- * console.log(`Kernel32 base: 0x${kernel32.base.toString(16)}`);
58
- * ```
59
- *
60
- * @example
61
- * ```typescript
62
- * // Calculate absolute address from relative offset
63
- * const gameModule: Module = memory.modules['game.exe'];
64
- * const relativeOffset = 0x12345678;
65
- * const absoluteAddress = gameModule.base + BigInt(relativeOffset);
66
- *
67
- * // Read data at the calculated address
68
- * const health = memory.f32(absoluteAddress);
69
- * ```
70
- *
71
- * @example
72
- * ```typescript
73
- * // Enumerate all loaded modules
74
- * function listModules(memory: Memory) {
75
- * const modules = memory.modules;
76
- *
77
- * console.log('Loaded modules:');
78
- * for (const [name, module] of Object.entries(modules)) {
79
- * console.log(` ${name}:`);
80
- * console.log(` Base: 0x${module.base.toString(16)}`);
81
- * console.log(` Size: ${module.size} bytes`);
82
- * console.log(` End: 0x${(module.base + BigInt(module.size)).toString(16)}`);
83
- * }
84
- * }
7
+ * ```ts
8
+ * const cs2 = new Memory('cs2.exe');
9
+ * const mainModule = cs2.modules['cs2.exe'];
85
10
  * ```
86
11
  */
87
12
  export type Module = {
88
- /**
89
- * Base memory address where the module is loaded.
90
- *
91
- * This is the virtual memory address where the module's first byte resides
92
- * in the target process. All relative offsets within the module should be
93
- * added to this base address to get the absolute memory address.
94
- *
95
- * @example
96
- * ```typescript
97
- * const module: Module = memory.modules['example.dll'];
98
- * const functionOffset = 0x1000; // Relative offset to a function
99
- * const functionAddress = module.base + BigInt(functionOffset);
100
- * ```
101
- */
13
+ /** Base address of the module. */
102
14
  base: bigint;
103
-
104
- /**
105
- * Name of the module, typically the filename.
106
- *
107
- * This is usually the filename of the executable or library, including
108
- * the file extension (e.g., 'game.exe', 'user32.dll', 'ntdll.dll').
109
- *
110
- * @example
111
- * ```typescript
112
- * const modules = memory.modules;
113
- *
114
- * if ('game.exe' in modules) {
115
- * console.log(`Game executable found: ${modules['game.exe'].name}`);
116
- * }
117
- * ```
118
- */
15
+ /** Module filename. */
119
16
  name: string;
120
-
121
- /**
122
- * Size of the module in bytes.
123
- *
124
- * This represents how much virtual memory the module occupies in the
125
- * target process. The module occupies memory from `base` to `base + size`.
126
- *
127
- * @example
128
- * ```typescript
129
- * const module: Module = memory.modules['large_library.dll'];
130
- * const endAddress = module.base + BigInt(module.size);
131
- *
132
- * console.log(`Module spans from 0x${module.base.toString(16)} to 0x${endAddress.toString(16)}`);
133
- * console.log(`Total size: ${(module.size / 1024 / 1024).toFixed(2)} MB`);
134
- * ```
135
- */
17
+ /** Module size in bytes. */
136
18
  size: number;
137
19
  };
138
20
 
139
21
  /**
140
- * Represents a contiguous vector of unsigned 32-bit integers used by the network
141
- * utility subsystem.
142
- *
143
- * `NetworkUtlVector` is an alias of `Uint32Array`. In the target process, the
144
- * vector’s elements live in an out-of-line buffer referenced by a small header
145
- * at `address` (read/write via {@link Memory.networkUtlVector}):
146
- *
147
- * - 0x00: `uint32` size (number of elements)
148
- * - 0x04: `uint32` capacity (preallocated element count)
149
- * - 0x08: `uint64` pointer to contiguous `uint32` elements
150
- *
151
- * This alias provides a type-safe view for high-throughput operations such as
152
- * batched identifier handling, index sets, and routing tables, while keeping
153
- * zero-copy semantics in userland and avoiding per-element boxing.
154
- *
155
- * Characteristics:
156
- * - Little-endian `uint32` element layout
157
- * - Backed by a single contiguous elements buffer
158
- * - Suitable for FFI and bulk memory transfers
159
- *
160
- * Usage notes:
161
- * - Use {@link Memory.networkUtlVector} to read or write the elements without
162
- * reallocating the in-process buffer.
163
- * - When writing, the implementation updates the size field to `values.length`
164
- * and copies elements into the existing buffer; capacity and pointer are not
165
- * modified.
166
- * - Ensure `values.length` does not exceed the current capacity of the in-process
167
- * buffer to avoid truncation or undefined behavior imposed by the target.
168
- *
169
- * @example
170
- * ```typescript
171
- * const memory = new Memory('network_app.exe');
172
- *
173
- * // Read the current vector
174
- * const ids: NetworkUtlVector = memory.networkUtlVector(0x12345678n);
175
- * console.log(`Count: ${ids.length}`, Array.from(ids));
176
- * ```
177
- *
178
- * @example
179
- * ```typescript
180
- * // Overwrite the vector contents (must fit existing capacity)
181
- * const next: NetworkUtlVector = Uint32Array.from([101, 202, 303, 404]);
182
- * memory.networkUtlVector(0x12345678n, next);
183
- * ```
184
- *
22
+ * Represents a contiguous vector of unsigned 32-bit integers.
185
23
  * @example
186
- * ```typescript
187
- * // Append in place by staging into a new typed array, then writing back
188
- * const baseAddress = 0x12345678n;
189
- * const current = memory.networkUtlVector(baseAddress);
190
- * const extended = new Uint32Array(current.length + 1);
191
- * extended.set(current);
192
- * extended[extended.length - 1] = 0xDEADBEEF;
193
- * memory.networkUtlVector(baseAddress, extended); // capacity must allow the new size
24
+ * ```ts
25
+ * const cs2 = new Memory('cs2.exe');
26
+ * const myVector = cs2.networkUtlVector(0x12345678n);
194
27
  * ```
195
28
  */
196
29
  export type NetworkUtlVector = Uint32Array;
197
30
 
198
31
  /**
199
- * Represents a quaternion for 3D rotations.
200
- *
201
- * Quaternions are a mathematical representation of rotations in 3D space that
202
- * avoid gimbal lock and provide smooth interpolation. They consist of four
203
- * components: x, y, z (vector part) and w (scalar part).
204
- *
205
- * Quaternions are commonly used in:
206
- * - 3D games for character and camera rotations
207
- * - 3D modeling and animation software
208
- * - Robotics and aerospace applications
209
- * - Physics simulations
210
- *
211
- * The quaternion components represent:
212
- * - x, y, z: The axis of rotation (vector part)
213
- * - w: The amount of rotation around that axis (scalar part)
214
- *
32
+ * Represents a 2D point.
33
+ * @property x X coordinate.
34
+ * @property y Y coordinate.
215
35
  * @example
216
- * ```typescript
217
- * const memory = new Memory('3d_game.exe');
218
- *
219
- * // Read player rotation
220
- * const playerRotation: Quaternion = memory.quaternion(0x12345678n);
221
- * console.log(`Player rotation: x=${playerRotation.x}, y=${playerRotation.y}, z=${playerRotation.z}, w=${playerRotation.w}`);
222
- *
223
- * // Set identity rotation (no rotation)
224
- * const identity: Quaternion = { x: 0, y: 0, z: 0, w: 1 };
225
- * memory.quaternion(0x12345678n, identity);
36
+ * ```ts
37
+ * const cs2 = new Memory('cs2.exe');
38
+ * const myPoint = cs2.point(0x12345678n);
226
39
  * ```
227
- *
228
- * @example
229
- * ```typescript
230
- * // Read array of bone rotations for skeletal animation
231
- * const boneCount = 50;
232
- * const boneRotations: Quaternion[] = memory.quaternionArray(0x12345678n, boneCount);
233
- *
234
- * // Modify specific bone rotation
235
- * boneRotations[10] = { x: 0.707, y: 0, z: 0, w: 0.707 }; // 90-degree rotation around X-axis
236
- *
237
- * // Write back the modified rotations
238
- * memory.quaternionArray(0x12345678n, boneRotations);
239
- * ```
240
- *
40
+ */
41
+ export type Point = {
42
+ /** X coordinate. */
43
+ x: number;
44
+ /** Y coordinate. */
45
+ y: number;
46
+ };
47
+
48
+ /**
49
+ * Represents a quaternion for 3D rotations.
50
+ * @property w W component.
51
+ * @property x X component.
52
+ * @property y Y component.
53
+ * @property z Z component.
241
54
  * @example
242
- * ```typescript
243
- * // Interpolate between two rotations (basic lerp example)
244
- * function lerpQuaternion(q1: Quaternion, q2: Quaternion, t: number): Quaternion {
245
- * return {
246
- * x: q1.x + (q2.x - q1.x) * t,
247
- * y: q1.y + (q2.y - q1.y) * t,
248
- * z: q1.z + (q2.z - q1.z) * t,
249
- * w: q1.w + (q2.w - q1.w) * t
250
- * };
251
- * }
252
- *
253
- * const startRotation: Quaternion = memory.quaternion(0x12345678n);
254
- * const endRotation: Quaternion = { x: 0, y: 0.707, z: 0, w: 0.707 };
255
- * const interpolated = lerpQuaternion(startRotation, endRotation, 0.5);
256
- * memory.quaternion(0x12345678n, interpolated);
55
+ * ```ts
56
+ * const cs2 = new Memory('cs2.exe');
57
+ * const myQuaternion = cs2.quaternion(0x12345678n);
257
58
  * ```
258
59
  */
259
60
  export type Quaternion = {
260
- /**
261
- * W component (scalar part) of the quaternion.
262
- *
263
- * The w component represents the "amount" of rotation. For a normalized quaternion:
264
- * - w = 1 represents no rotation (identity)
265
- * - w = 0 represents a 180-degree rotation
266
- * - w = cos(θ/2) where θ is the rotation angle
267
- *
268
- * @example
269
- * ```typescript
270
- * // Identity quaternion (no rotation)
271
- * const identity: Quaternion = { x: 0, y: 0, z: 0, w: 1 };
272
- *
273
- * // 180-degree rotation around Y-axis
274
- * const flip: Quaternion = { x: 0, y: 1, z: 0, w: 0 };
275
- * ```
276
- */
61
+ /** W component. */
277
62
  w: number;
278
-
279
- /**
280
- * X component of the quaternion's vector part.
281
- *
282
- * The x component contributes to the axis of rotation. For rotations around
283
- * the X-axis, this value will be non-zero while y and z approach zero.
284
- *
285
- * @example
286
- * ```typescript
287
- * // 90-degree rotation around X-axis
288
- * const xRotation: Quaternion = { x: 0.707, y: 0, z: 0, w: 0.707 };
289
- * ```
290
- */
63
+ /** X component. */
291
64
  x: number;
292
-
293
- /**
294
- * Y component of the quaternion's vector part.
295
- *
296
- * The y component contributes to the axis of rotation. For rotations around
297
- * the Y-axis (yaw), this value will be non-zero while x and z approach zero.
298
- *
299
- * @example
300
- * ```typescript
301
- * // 45-degree yaw rotation (around Y-axis)
302
- * const yawRotation: Quaternion = { x: 0, y: 0.383, z: 0, w: 0.924 };
303
- * ```
304
- */
65
+ /** Y component. */
305
66
  y: number;
306
-
307
- /**
308
- * Z component of the quaternion's vector part.
309
- *
310
- * The z component contributes to the axis of rotation. For rotations around
311
- * the Z-axis (roll), this value will be non-zero while x and y approach zero.
312
- *
313
- * @example
314
- * ```typescript
315
- * // 30-degree roll rotation (around Z-axis)
316
- * const rollRotation: Quaternion = { x: 0, y: 0, z: 0.259, w: 0.966 };
317
- * ```
318
- */
67
+ /** Z component. */
319
68
  z: number;
320
69
  };
321
70
 
322
71
  /**
323
72
  * Represents an orientation using Euler angles.
324
- *
325
- * `QAngle` stores three 32-bit floating-point angles that describe rotation
326
- * in degrees around the principal axes: `pitch` (X), `yaw` (Y), and `roll` (Z).
327
- * This format is common in Source-engine-style telemetry and many gameplay
328
- * camera systems. For quaternions, see {@link Quaternion}.
329
- *
330
- * Typical uses include:
331
- * - Camera/view rotations
332
- * - Bone/attachment orientations
333
- * - Aim and recoil calculations
334
- *
73
+ * @property pitch Pitch (X axis).
74
+ * @property roll Roll (Z axis).
75
+ * @property yaw Yaw (Y axis).
335
76
  * @example
336
- * ```typescript
337
- * // Read and tweak view angles
338
- * const view: QAngle = memory.qAngle(0x12345678n);
339
- * const leveled: QAngle = { pitch: 0, yaw: view.yaw, roll: 0 };
340
- * memory.qAngle(0x12345678n, leveled);
77
+ * ```ts
78
+ * const cs2 = new Memory('cs2.exe');
79
+ * const myQAngle = cs2.qAngle(0x12345678n);
341
80
  * ```
342
81
  */
343
82
  export interface QAngle {
344
- /**
345
- * Rotation around the X axis, in degrees.
346
- *
347
- * Positive values pitch the nose downward in many right-handed game
348
- * coordinate systems; verify the target engine’s convention before writing.
349
- *
350
- * @example
351
- * ```typescript
352
- * const aimDown: QAngle = { pitch: 10, yaw: 0, roll: 0 };
353
- * ```
354
- */
83
+ /** Pitch (X axis). */
355
84
  pitch: number;
356
-
357
- /**
358
- * Rotation around the Z axis, in degrees.
359
- *
360
- * Often used for banking/tilt effects. Some engines clamp or ignore roll for
361
- * first-person cameras.
362
- *
363
- * @example
364
- * ```typescript
365
- * const slightBank: QAngle = { pitch: 0, yaw: 0, roll: 5 };
366
- * ```
367
- */
85
+ /** Roll (Z axis). */
368
86
  roll: number;
369
-
370
- /**
371
- * Rotation around the Y axis, in degrees.
372
- *
373
- * Positive values typically turn to the right (clockwise when viewed from
374
- * above) in right-handed systems.
375
- *
376
- * @example
377
- * ```typescript
378
- * const turnRight: QAngle = { pitch: 0, yaw: 90, roll: 0 };
379
- * ```
380
- */
87
+ /** Yaw (Y axis). */
381
88
  yaw: number;
382
89
  }
383
90
 
384
91
  /**
385
- * Represents a memory region with its properties and protection flags.
386
- *
387
- * Memory regions are contiguous blocks of virtual memory with uniform properties
388
- * such as protection flags (read/write/execute permissions), state (committed,
389
- * reserved, free), and type (private, mapped, image).
390
- *
391
- * This information is crucial for:
392
- * - Safe memory scanning and modification
393
- * - Understanding memory layout and permissions
394
- * - Avoiding access violations when reading/writing memory
395
- * - Identifying different types of memory (code, data, heap, etc.)
396
- *
92
+ * Represents an RGB color.
93
+ * @property r Red.
94
+ * @property g Green.
95
+ * @property b Blue.
397
96
  * @example
398
- * ```typescript
399
- * // This type is typically used internally by the Memory class
400
- * // Users generally don't need to create Region objects directly
401
- *
402
- * const memory = new Memory('target_process.exe');
403
- * // The regions() method (if exposed) would return Region objects
97
+ * ```ts
98
+ * const cs2 = new Memory('cs2.exe');
99
+ * const myRGB = cs2.rgb(0x12345678n);
404
100
  * ```
405
- *
101
+ */
102
+ export type RGB = {
103
+ /** Red. */
104
+ r: number;
105
+ /** Green. */
106
+ g: number;
107
+ /** Blue. */
108
+ b: number;
109
+ };
110
+
111
+ /**
112
+ * Represents an RGBA color.
113
+ * @property r Red.
114
+ * @property g Green.
115
+ * @property b Blue.
116
+ * @property a Alpha.
117
+ * @example
118
+ * ```ts
119
+ * const cs2 = new Memory('cs2.exe');
120
+ * const myRGBA = cs2.rgba(0x12345678n);
121
+ * ```
122
+ */
123
+ export type RGBA = {
124
+ /** Red. */
125
+ r: number;
126
+ /** Green. */
127
+ g: number;
128
+ /** Blue. */
129
+ b: number;
130
+ /** Alpha. */
131
+ a: number;
132
+ };
133
+
134
+ /**
135
+ * Represents a memory region.
136
+ * @property base Base address.
137
+ * @property protect Protection flags.
138
+ * @property size Size in bytes.
139
+ * @property state State.
140
+ * @property type Type.
406
141
  * @example
407
- * ```typescript
408
- * // Understanding memory protection flags
409
- * // These constants match Windows PAGE_* flags:
410
- * // 0x04 = PAGE_READWRITE
411
- * // 0x20 = PAGE_EXECUTE_READ
412
- * // 0x40 = PAGE_EXECUTE_READWRITE
413
- * // 0x02 = PAGE_READONLY
142
+ * ```ts
143
+ * // Used internally by Memory
414
144
  * ```
415
145
  */
416
146
  export type Region = {
417
- /**
418
- * Base address of the memory region.
419
- *
420
- * This is the starting virtual memory address of the region. All addresses
421
- * within the region fall between base and (base + size).
422
- *
423
- * @example
424
- * ```typescript
425
- * const region: Region = {
426
- * base: 0x10000000n,
427
- * size: 0x1000n,
428
- * protect: 0x04, // PAGE_READWRITE
429
- * state: 0x1000, // MEM_COMMIT
430
- * type: 0x20000 // MEM_PRIVATE
431
- * };
432
- *
433
- * // Region spans from 0x10000000 to 0x10001000
434
- * ```
435
- */
147
+ /** Base address. */
436
148
  base: bigint;
437
-
438
- /**
439
- * Memory protection flags for this region.
440
- *
441
- * These flags determine what operations are allowed on the memory:
442
- * - Read permissions: Can the memory be read?
443
- * - Write permissions: Can the memory be modified?
444
- * - Execute permissions: Can code in this memory be executed?
445
- *
446
- * Common Windows protection flags:
447
- * - 0x01: PAGE_NOACCESS - No access allowed
448
- * - 0x02: PAGE_READONLY - Read-only access
449
- * - 0x04: PAGE_READWRITE - Read and write access
450
- * - 0x08: PAGE_WRITECOPY - Copy-on-write access
451
- * - 0x10: PAGE_EXECUTE - Execute-only access
452
- * - 0x20: PAGE_EXECUTE_READ - Execute and read access
453
- * - 0x40: PAGE_EXECUTE_READWRITE - Execute, read, and write access
454
- * - 0x100: PAGE_GUARD - Guard page (triggers exception on access)
455
- *
456
- * @example
457
- * ```typescript
458
- * function isWritableRegion(region: Region): boolean {
459
- * const writableFlags = 0x04 | 0x08 | 0x40; // READWRITE | WRITECOPY | EXECUTE_READWRITE
460
- * return (region.protect & writableFlags) !== 0;
461
- * }
462
- *
463
- * function isExecutableRegion(region: Region): boolean {
464
- * const executableFlags = 0x10 | 0x20 | 0x40 | 0x80; // Various execute flags
465
- * return (region.protect & executableFlags) !== 0;
466
- * }
467
- * ```
468
- */
149
+ /** Protection flags. */
469
150
  protect: number;
470
-
471
- /**
472
- * Size of the memory region in bytes.
473
- *
474
- * This indicates how many bytes the region spans. The region occupies
475
- * virtual addresses from base to (base + size - 1).
476
- *
477
- * @example
478
- * ```typescript
479
- * function analyzeRegion(region: Region) {
480
- * const endAddress = region.base + region.size;
481
- * const sizeMB = Number(region.size) / (1024 * 1024);
482
- *
483
- * console.log(`Region: 0x${region.base.toString(16)} - 0x${endAddress.toString(16)}`);
484
- * console.log(`Size: ${sizeMB.toFixed(2)} MB`);
485
- * }
486
- * ```
487
- */
151
+ /** Size in bytes. */
488
152
  size: bigint;
489
-
490
- /**
491
- * State of the memory region.
492
- *
493
- * The state indicates how the virtual memory is currently being used:
494
- * - MEM_COMMIT (0x1000): Memory is allocated and backed by physical storage
495
- * - MEM_RESERVE (0x2000): Memory is reserved but not yet committed
496
- * - MEM_FREE (0x10000): Memory is available for allocation
497
- *
498
- * Only committed memory (MEM_COMMIT) can be safely read from or written to.
499
- *
500
- * @example
501
- * ```typescript
502
- * function isCommittedMemory(region: Region): boolean {
503
- * const MEM_COMMIT = 0x1000;
504
- * return region.state === MEM_COMMIT;
505
- * }
506
- *
507
- * function getStateDescription(state: number): string {
508
- * switch (state) {
509
- * case 0x1000: return 'Committed';
510
- * case 0x2000: return 'Reserved';
511
- * case 0x10000: return 'Free';
512
- * default: return `Unknown (0x${state.toString(16)})`;
513
- * }
514
- * }
515
- * ```
516
- */
153
+ /** State. */
517
154
  state: number;
518
-
519
- /**
520
- * Type of the memory region.
521
- *
522
- * The type indicates how the memory was allocated and what it contains:
523
- * - MEM_PRIVATE (0x20000): Private memory allocated by the process
524
- * - MEM_MAPPED (0x40000): Memory-mapped file
525
- * - MEM_IMAGE (0x1000000): Memory containing executable image (EXE/DLL)
526
- *
527
- * Different types have different characteristics and usage patterns.
528
- *
529
- * @example
530
- * ```typescript
531
- * function getMemoryTypeDescription(type: number): string {
532
- * switch (type) {
533
- * case 0x20000: return 'Private (heap/stack)';
534
- * case 0x40000: return 'Mapped (file-backed)';
535
- * case 0x1000000: return 'Image (executable)';
536
- * default: return `Unknown (0x${type.toString(16)})`;
537
- * }
538
- * }
539
- *
540
- * function isExecutableImage(region: Region): boolean {
541
- * const MEM_IMAGE = 0x1000000;
542
- * return region.type === MEM_IMAGE;
543
- * }
544
- * ```
545
- */
155
+ /** Type. */
546
156
  type: number;
547
157
  };
548
158
 
549
159
  /**
550
- * Union type of all buffer types that can be used as scratch space for memory operations.
551
- *
552
- * This type represents all the binary data views that can be used with the Memory class's
553
- * low-level read() and write() methods. These types all provide direct access to raw
554
- * memory buffers and can be passed to FFI functions.
555
- *
556
- * The Scratch type ensures type safety when working with different buffer formats while
557
- * maintaining compatibility with Bun's FFI system through the .ptr property extensions.
558
- *
559
- * Supported buffer types:
560
- * - **BigInt64Array / BigUint64Array**: 64-bit integer arrays
561
- * - **Buffer**: Node.js-style buffer (Bun-compatible)
562
- * - **Float32Array / Float64Array**: Floating-point number arrays
563
- * - **DataView**: Generic binary data view
564
- * - **Int8Array / Int16Array / Int32Array**: Signed integer arrays
565
- * - **Uint8Array / Uint16Array / Uint32Array / Uint8ClampedArray**: Unsigned integer arrays
566
- *
567
- * @example
568
- * ```typescript
569
- * const memory = new Memory('target.exe');
570
- *
571
- * // All of these are valid Scratch types:
572
- * const buffer1: Scratch = new Uint8Array(1024);
573
- * const buffer2: Scratch = Buffer.allocUnsafe(512);
574
- * const buffer3: Scratch = new Float32Array(256);
575
- * const buffer4: Scratch = new DataView(new ArrayBuffer(128));
576
- *
577
- * // Can be used with Memory methods:
578
- * memory.read(0x12345678n, buffer1);
579
- * memory.read(0x12345679n, buffer2);
580
- * memory.read(0x1234567An, buffer3);
581
- * ```
582
- *
583
- * @example
584
- * ```typescript
585
- * // Type-safe function that accepts any valid scratch buffer
586
- * function readMemoryBlock(memory: Memory, address: bigint, buffer: Scratch): void {
587
- * memory.read(address, buffer);
588
- * console.log(`Read ${buffer.byteLength} bytes from 0x${address.toString(16)}`);
589
- * }
590
- *
591
- * // Usage with different buffer types
592
- * const floatData = new Float32Array(10);
593
- * const byteData = new Uint8Array(40);
594
- *
595
- * readMemoryBlock(memory, 0x1000000n, floatData);
596
- * readMemoryBlock(memory, 0x1000100n, byteData);
597
- * ```
598
- *
160
+ * Represents a buffer usable for memory operations.
599
161
  * @example
600
- * ```typescript
601
- * // Custom memory reader with automatic buffer allocation
602
- * function createScratchBuffer(size: number, type: 'bytes' | 'floats' | 'ints'): Scratch {
603
- * switch (type) {
604
- * case 'bytes': return new Uint8Array(size);
605
- * case 'floats': return new Float32Array(size);
606
- * case 'ints': return new Int32Array(size);
607
- * default: throw new Error('Unknown buffer type');
608
- * }
609
- * }
610
- *
611
- * const scratchBuffer = createScratchBuffer(1024, 'bytes');
612
- * memory.read(someAddress, scratchBuffer);
162
+ * ```ts
163
+ * const cs2 = new Memory('cs2.exe');
164
+ * const myBuffer = new Uint8Array(4);
165
+ * cs2.read(0x12345678n, myBuffer);
613
166
  * ```
614
167
  */
615
168
  export type Scratch = BigInt64Array | BigUint64Array | Buffer | Float32Array | Float64Array | DataView | Int16Array | Int32Array | Int8Array | Uint16Array | Uint8Array | Uint8ClampedArray | Uint32Array;
616
169
 
617
170
  /**
618
- * Represents a 2D vector with x and y components.
619
- *
620
- * Vector2 is commonly used for:
621
- * - 2D positions and coordinates
622
- * - Screen/UI coordinates
623
- * - Texture coordinates (UV mapping)
624
- * - 2D velocities and directions
625
- * - Size and dimension data
626
- * - Mouse cursor positions
627
- *
171
+ * Represents a 2D vector.
172
+ * @property x X coordinate.
173
+ * @property y Y coordinate.
628
174
  * @example
629
- * ```typescript
630
- * const memory = new Memory('2d_game.exe');
631
- *
632
- * // Read player position in 2D space
633
- * const playerPos: Vector2 = memory.vector2(0x12345678n);
634
- * console.log(`Player at (${playerPos.x}, ${playerPos.y})`);
635
- *
636
- * // Move player to a new location
637
- * const newPosition: Vector2 = { x: 100.5, y: 200.7 };
638
- * memory.vector2(0x12345678n, newPosition);
639
- * ```
640
- *
641
- * @example
642
- * ```typescript
643
- * // Read array of waypoints for 2D pathfinding
644
- * const waypointCount = 10;
645
- * const waypoints: Vector2[] = memory.vector2Array(0x12345678n, waypointCount);
646
- *
647
- * // Process each waypoint
648
- * waypoints.forEach((point, index) => {
649
- * console.log(`Waypoint ${index}: (${point.x}, ${point.y})`);
650
- * });
651
- *
652
- * // Add a new waypoint at the end
653
- * waypoints.push({ x: 500, y: 300 });
654
- * memory.vector2Array(0x12345678n, waypoints);
655
- * ```
656
- *
657
- * @example
658
- * ```typescript
659
- * // Vector math operations
660
- * function distance2D(a: Vector2, b: Vector2): number {
661
- * const dx = b.x - a.x;
662
- * const dy = b.y - a.y;
663
- * return Math.sqrt(dx * dx + dy * dy);
664
- * }
665
- *
666
- * function add2D(a: Vector2, b: Vector2): Vector2 {
667
- * return { x: a.x + b.x, y: a.y + b.y };
668
- * }
669
- *
670
- * const playerPos = memory.vector2(0x12345678n);
671
- * const targetPos = memory.vector2(0x12345688n);
672
- * const distanceToTarget = distance2D(playerPos, targetPos);
673
- *
674
- * console.log(`Distance to target: ${distanceToTarget.toFixed(2)}`);
175
+ * ```ts
176
+ * const cs2 = new Memory('cs2.exe');
177
+ * const myVector2 = cs2.vector2(0x12345678n);
675
178
  * ```
676
179
  */
677
180
  export type Vector2 = {
678
- /**
679
- * X coordinate component.
680
- *
681
- * In most coordinate systems:
682
- * - Represents horizontal position
683
- * - Positive values typically go right
684
- * - For screen coordinates, usually increases left-to-right
685
- *
686
- * @example
687
- * ```typescript
688
- * const screenPos: Vector2 = { x: 640, y: 480 }; // Center of 1280x960 screen
689
- * const worldPos: Vector2 = { x: -15.5, y: 23.7 }; // World coordinates
690
- * ```
691
- */
181
+ /** X coordinate. */
692
182
  x: number;
693
-
694
- /**
695
- * Y coordinate component.
696
- *
697
- * In most coordinate systems:
698
- * - Represents vertical position
699
- * - Direction (up/down) depends on the coordinate system
700
- * - Screen coordinates often have Y increasing downward
701
- * - World coordinates often have Y increasing upward
702
- *
703
- * @example
704
- * ```typescript
705
- * // Screen coordinates (Y increases downward)
706
- * const uiElement: Vector2 = { x: 100, y: 50 }; // 100 pixels right, 50 pixels down
707
- *
708
- * // 3D world coordinates (Y often increases upward)
709
- * const worldPoint: Vector2 = { x: 10, y: 25 }; // 10 units east, 25 units up
710
- * ```
711
- */
183
+ /** Y coordinate. */
712
184
  y: number;
713
185
  };
714
186
 
187
+ export type UPtr = bigint;
188
+
189
+ export type UPtrArray = BigUint64Array;
190
+
715
191
  /**
716
- * Represents a 3D vector with x, y, and z components.
717
- *
718
- * Vector3 is commonly used for:
719
- * - 3D positions and coordinates
720
- * - 3D velocities and directions
721
- * - Surface normals in 3D graphics
722
- * - RGB color values (though usually normalized to 0-1)
723
- * - 3D rotations (Euler angles)
724
- * - Scale factors for 3D objects
725
- * - Physics forces and accelerations
726
- *
192
+ * Represents a 3D vector.
193
+ * @property x X coordinate.
194
+ * @property y Y coordinate.
195
+ * @property z Z coordinate.
727
196
  * @example
728
- * ```typescript
729
- * const memory = new Memory('3d_game.exe');
730
- *
731
- * // Read player position in 3D world
732
- * const playerPos: Vector3 = memory.vector3(0x12345678n);
733
- * console.log(`Player at (${playerPos.x}, ${playerPos.y}, ${playerPos.z})`);
734
- *
735
- * // Teleport player to spawn point
736
- * const spawnPoint: Vector3 = { x: 0, y: 10, z: 0 };
737
- * memory.vector3(0x12345678n, spawnPoint);
738
- * ```
739
- *
740
- * @example
741
- * ```typescript
742
- * // Read array of 3D model vertices
743
- * const vertexCount = 1000;
744
- * const vertices: Vector3[] = memory.vector3Array(0x12345678n, vertexCount);
745
- *
746
- * // Find the bounding box of the model
747
- * let minX = Infinity, minY = Infinity, minZ = Infinity;
748
- * let maxX = -Infinity, maxY = -Infinity, maxZ = -Infinity;
749
- *
750
- * vertices.forEach(vertex => {
751
- * minX = Math.min(minX, vertex.x);
752
- * maxX = Math.max(maxX, vertex.x);
753
- * minY = Math.min(minY, vertex.y);
754
- * maxY = Math.max(maxY, vertex.y);
755
- * minZ = Math.min(minZ, vertex.z);
756
- * maxZ = Math.max(maxZ, vertex.z);
757
- * });
758
- *
759
- * console.log(`Bounding box: (${minX}, ${minY}, ${minZ}) to (${maxX}, ${maxY}, ${maxZ})`);
760
- * ```
761
- *
762
- * @example
763
- * ```typescript
764
- * // Vector math operations for 3D
765
- * function distance3D(a: Vector3, b: Vector3): number {
766
- * const dx = b.x - a.x;
767
- * const dy = b.y - a.y;
768
- * const dz = b.z - a.z;
769
- * return Math.sqrt(dx * dx + dy * dy + dz * dz);
770
- * }
771
- *
772
- * function normalize3D(v: Vector3): Vector3 {
773
- * const length = Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
774
- * if (length === 0) return { x: 0, y: 0, z: 0 };
775
- * return { x: v.x / length, y: v.y / length, z: v.z / length };
776
- * }
777
- *
778
- * function crossProduct(a: Vector3, b: Vector3): Vector3 {
779
- * return {
780
- * x: a.y * b.z - a.z * b.y,
781
- * y: a.z * b.x - a.x * b.z,
782
- * z: a.x * b.y - a.y * b.x
783
- * };
784
- * }
785
- *
786
- * const forward = memory.vector3(0x12345678n);
787
- * const right = memory.vector3(0x12345688n);
788
- * const up = crossProduct(forward, right); // Calculate up vector
789
- * ```
790
- *
791
- * @example
792
- * ```typescript
793
- * // Color manipulation using Vector3
794
- * const playerColor: Vector3 = memory.vector3(0x12345678n);
795
- *
796
- * // Assuming RGB values are in 0-255 range
797
- * console.log(`Player color: R=${playerColor.x}, G=${playerColor.y}, B=${playerColor.z}`);
798
- *
799
- * // Set player color to bright red
800
- * const brightRed: Vector3 = { x: 255, y: 0, z: 0 };
801
- * memory.vector3(0x12345678n, brightRed);
197
+ * ```ts
198
+ * const cs2 = new Memory('cs2.exe');
199
+ * const myVector3 = cs2.vector3(0x12345678n);
802
200
  * ```
803
201
  */
804
202
  export type Vector3 = {
805
- /**
806
- * X coordinate component.
807
- *
808
- * In 3D coordinate systems:
809
- * - Often represents the horizontal axis (left-right)
810
- * - In right-handed systems, positive X typically points right
811
- * - In left-handed systems, positive X typically points right
812
- * - May represent red component in RGB color contexts
813
- *
814
- * @example
815
- * ```typescript
816
- * const position: Vector3 = { x: 15.5, y: 0, z: -10 }; // 15.5 units right
817
- * const color: Vector3 = { x: 255, y: 128, z: 64 }; // Red component = 255
818
- * ```
819
- */
203
+ /** X coordinate. */
820
204
  x: number;
821
-
822
- /**
823
- * Y coordinate component.
824
- *
825
- * In 3D coordinate systems:
826
- * - Often represents the vertical axis (up-down)
827
- * - In right-handed systems, positive Y typically points up
828
- * - In some graphics systems, positive Y may point down
829
- * - May represent green component in RGB color contexts
830
- *
831
- * @example
832
- * ```typescript
833
- * const position: Vector3 = { x: 0, y: 10.5, z: 0 }; // 10.5 units up
834
- * const color: Vector3 = { x: 255, y: 128, z: 64 }; // Green component = 128
835
- * ```
836
- */
205
+ /** Y coordinate. */
837
206
  y: number;
838
-
839
- /**
840
- * Z coordinate component.
841
- *
842
- * In 3D coordinate systems:
843
- * - Often represents the depth axis (forward-backward)
844
- * - In right-handed systems, positive Z typically points toward viewer
845
- * - In left-handed systems, positive Z typically points away from viewer
846
- * - May represent blue component in RGB color contexts
847
- *
848
- * @example
849
- * ```typescript
850
- * const position: Vector3 = { x: 0, y: 0, z: -5.2 }; // 5.2 units away (right-handed)
851
- * const color: Vector3 = { x: 255, y: 128, z: 64 }; // Blue component = 64
852
- * ```
853
- */
207
+ /** Z coordinate. */
854
208
  z: number;
855
209
  };
856
210
 
857
211
  /**
858
- * Represents a 4D vector with w, x, y, z components.
859
- *
860
- * Common use cases:
861
- * - Homogeneous coordinates (w ≠ 0)
862
- * - Colors with alpha (x = R, y = G, z = B, w = A)
863
- * - Packed attributes and shader parameters
864
- * - As a quaternion carrier when interoperating with APIs expecting {x,y,z,w}
865
- *
866
- * Memory layout used by {@link Memory.vector4} is four 32-bit floats stored in
867
- * the order **x, y, z, w**, while this type exposes `{ w, x, y, z }` for
868
- * consistency with {@link Quaternion}.
869
- *
212
+ * Represents a 4D vector.
213
+ * @property w W component.
214
+ * @property x X component.
215
+ * @property y Y component.
216
+ * @property z Z component.
870
217
  * @example
871
- * ```typescript
872
- * // RGBA color
873
- * const red: Vector4 = { x: 255, y: 0, z: 0, w: 255 };
874
- *
875
- * // Homogeneous point (x, y, z, w)
876
- * const p: Vector4 = { x: 10, y: 20, z: 30, w: 1 };
218
+ * ```ts
219
+ * const cs2 = new Memory('cs2.exe');
220
+ * const myVector4 = cs2.vector4(0x12345678n);
877
221
  * ```
878
222
  */
879
223
  export type Vector4 = {
224
+ /** W component. */
880
225
  w: number;
226
+ /** X component. */
881
227
  x: number;
228
+ /** Y component. */
882
229
  y: number;
230
+ /** Z component. */
883
231
  z: number;
884
232
  };