bun-memory 1.1.12 → 1.1.13

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 CHANGED
@@ -24,7 +24,7 @@ bun add bun-memory
24
24
 
25
25
  ## Usage
26
26
 
27
- See [example/trigger-bot.ts](example/trigger-bot.ts) for a real-world example.
27
+ See [example/trigger-bot.ts](example/trigger-bot.ts) for a real-world example for Counter-Strike 2.
28
28
 
29
29
  ### Basic Example
30
30
 
@@ -33,27 +33,29 @@ import Memory from 'bun-memory';
33
33
 
34
34
  // Attach to process by name…
35
35
  const memory = new Memory('notepad.exe');
36
- // …or PID
37
- const memory = new Memory(1234);
36
+ // …or PID
37
+ const memory = new Memory(1_234);
38
38
 
39
- // Access loaded modules
39
+ // Access loaded modules
40
40
  const modules = memory.modules;
41
41
  const mainModule = modules['notepad.exe'];
42
42
  console.log(`Base address: 0x${mainModule.modBaseAddr.toString(16)}`);
43
43
  console.log(`Size: ${mainModule.modBaseSize} bytes`);
44
44
 
45
- // Read a 32-bit integer
45
+ // Read a 32-bit integer
46
46
  const value = memory.i32(0x12345678n);
47
47
 
48
- // Write a float
48
+ // Write a float
49
49
  memory.f32(0x12345678n, 3.14159);
50
50
 
51
- // Clean up
51
+ // Clean up
52
52
  memory.close();
53
53
  ```
54
54
 
55
55
  ### Pattern Scanning
56
56
 
57
+ Pattern scanning is temporarily disabled but will return shortly.
58
+
57
59
  ```ts
58
60
  const offset = memory.findPattern('aa??bbccdd??ff', mainModule.modBaseAddr, mainModule.modBaseSize);
59
61
  const value = memory.bool(offset + 0x1234n);
@@ -62,12 +64,44 @@ memory.close();
62
64
 
63
65
  ### Efficient Buffer Reads
64
66
 
67
+ There are many ways to use `scratch`es. Scratches are great for avoiding allocation costs by reusing a
68
+ preexisting array, buffer, string, etc.
69
+
70
+ ```ts
71
+ const handles = new Uint32Array(0x100);
72
+
73
+ while (true) {
74
+ try {
75
+ memory.read(myAddress, handles); // Updated handles, no allocations…
76
+
77
+ // Do something with your handles…
78
+ for (const handle of handles) {
79
+ // …
80
+ }
81
+ } finally {
82
+ continue;
83
+ }
84
+ }
85
+ ```
86
+
65
87
  ```ts
66
- const scratch = Buffer.allocUnsafe(0xf000);
67
- const view = new BigUint64Array(scratch.buffer, scratch.byteOffset, 0xf000 / 8);
88
+ const buffer = Buffer.allocUnsafe(0xf000); // Use buffer as a scratch…
89
+ const pointers = new BigUint64Array(scratchBuffer.buffer, scratchBuffer.byteOffset, 0xf000 / 8);
68
90
 
69
91
  while (true) {
70
- memory.read(myAddress, scratch); // Updates scratch and view, no allocations
92
+ try {
93
+ memory.read(myAddress, buffer); // Updates buffer and pointers, no allocations…
94
+
95
+ // Do something with your pointers…
96
+ for (const pointer of pointers) {
97
+ // Read a 32 length string at pointer…
98
+ const myString = memory.cString(pointer, 32).toString();
99
+
100
+ // …
101
+ }
102
+ } finally {
103
+ continue;
104
+ }
71
105
  }
72
106
  ```
73
107
 
@@ -11,15 +11,6 @@ import OffsetsJSON from './offsets/offsets.json';
11
11
 
12
12
  const { random } = Math;
13
13
 
14
- // !
15
- const buffer = Buffer.from('Hello world!');
16
- const bufferPtr = ptr(buffer);
17
-
18
- const cString = new CString(bufferPtr);
19
-
20
- console.log(cString.byteLength, cString.byteOffset); // undefined undefined
21
- // !
22
-
23
14
  const Delay = 2.5;
24
15
 
25
16
  // Load user32.dll…
@@ -54,14 +45,24 @@ if (ClientPtr === undefined) {
54
45
  }
55
46
  // !
56
47
 
57
- console.time('Read test');
48
+ const dec = new TextDecoder('utf-8');
58
49
 
59
- for (let i = 0; i < 10_000_000; i++) {
60
- // new Uint8Array((random() * 256 + 1) >> 0);
61
- Buffer.allocUnsafe((random() * 256 + 1) >> 0);
50
+ const buffer = Buffer.allocUnsafe(32);
51
+ const pointers = [4749947025128n, 4749937696488n, 4749757372648n, 4745786211048n, 4745800086248n, 4748263960808n, 4748274490088n, 4748287459048n, 4747096900328n, 4747107757288n];
52
+
53
+ console.time('test');
54
+
55
+ while (true) {
56
+ for (const pointer of pointers) {
57
+ const cString = cs2.cString(pointer, 32).toString();
58
+
59
+ if (cString.length > 32) {
60
+ console.log(cString);
61
+ }
62
+ }
62
63
  }
63
64
 
64
- console.timeEnd('Read test');
65
+ console.timeEnd('test');
65
66
 
66
67
  // !
67
68
 
package/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "url": "git://github.com/obscuritysrl/bun-memory.git"
23
23
  },
24
24
  "type": "module",
25
- "version": "1.1.12",
25
+ "version": "1.1.13",
26
26
  "main": "./index.ts",
27
27
  "keywords": [
28
28
  "bun",
package/structs/Memory.ts CHANGED
@@ -168,6 +168,9 @@ class Memory {
168
168
  private readonly ScratchMemoryBasicInformation = Buffer.allocUnsafe(0x30 /* sizeof(MEMORY_BASIC_INFORMATION) */);
169
169
  private readonly ScratchModuleEntry32W = Buffer.allocUnsafe(0x438 /* sizeof(MODULEENTRY32W) */);
170
170
 
171
+ private static readonly TextDecoderUTF16 = new TextDecoder('utf-16');
172
+ private static readonly TextDecoderUTF8 = new TextDecoder('utf-8');
173
+
171
174
  /**
172
175
  * Handle to the target process.
173
176
  */
@@ -185,7 +188,6 @@ class Memory {
185
188
  *
186
189
  * @example
187
190
  * ```typescript
188
- * const memory = new Memory('notepad.exe');
189
191
  * const modules = memory.modules;
190
192
  *
191
193
  * // Access a specific module
@@ -211,7 +213,6 @@ class Memory {
211
213
  *
212
214
  * @example
213
215
  * ```typescript
214
- * const memory = new Memory('notepad.exe');
215
216
  * const regions = memory.regions(0x10000000n, 0x1000n);
216
217
  *
217
218
  * regions.forEach(region => {
@@ -265,7 +266,6 @@ class Memory {
265
266
  *
266
267
  * @example
267
268
  * ```typescript
268
- * const memory = new Memory('notepad.exe');
269
269
  * const buffer = new Uint8Array(4);
270
270
  * memory.read(0x12345678n, buffer);
271
271
  * ```
@@ -295,7 +295,6 @@ class Memory {
295
295
  *
296
296
  * @example
297
297
  * ```typescript
298
- * const memory = new Memory('notepad.exe');
299
298
  * const buffer = new Uint8Array([0x41, 0x42, 0x43, 0x44]);
300
299
  * memory.write(0x12345678n, buffer);
301
300
  * ```
@@ -342,8 +341,6 @@ class Memory {
342
341
  *
343
342
  * @example
344
343
  * ```typescript
345
- * const memory = new Memory('notepad.exe');
346
- *
347
344
  * // Initial modules
348
345
  * console.log('Initial modules:', Object.keys(memory.modules));
349
346
  *
@@ -401,8 +398,6 @@ class Memory {
401
398
  *
402
399
  * @example
403
400
  * ```typescript
404
- * const memory = new Memory('game.exe');
405
- *
406
401
  * // Read a boolean value
407
402
  * const isAlive = memory.bool(0x12345678n);
408
403
  * console.log('Player is alive:', isAlive);
@@ -444,14 +439,14 @@ class Memory {
444
439
  *
445
440
  * @example
446
441
  * ```typescript
447
- * const memory = new Memory('game.exe');
448
- *
449
442
  * // Read up to 64 bytes and interpret as a C string
450
443
  * const playerName = memory.cString(0x12345678n, 64);
451
444
  * console.log('Player name:', playerName.toString());
452
445
  *
453
446
  * // Write a C string (NUL-terminated)
454
- * const value = new CString('PlayerOne');
447
+ * const valueBuffer = Buffer.from('PlayerOne\0');
448
+ * const valuePtr = ptr(valueBuffer);
449
+ * const value = new CString(valuePtr);
455
450
  * memory.cString(0x12345678n, value);
456
451
  * ```
457
452
  */
@@ -482,8 +477,6 @@ class Memory {
482
477
  *
483
478
  * @example
484
479
  * ```typescript
485
- * const memory = new Memory('game.exe');
486
- *
487
480
  * // Read a float value
488
481
  * const playerHealth = memory.f32(0x12345678n);
489
482
  * console.log('Player health:', playerHealth);
@@ -517,8 +510,6 @@ class Memory {
517
510
  *
518
511
  * @example
519
512
  * ```typescript
520
- * const memory = new Memory('game.exe');
521
- *
522
513
  * // Read an array of 10 float values
523
514
  * const coordinates = memory.f32Array(0x12345678n, 10);
524
515
  * console.log('Coordinates:', coordinates);
@@ -556,8 +547,6 @@ class Memory {
556
547
  *
557
548
  * @example
558
549
  * ```typescript
559
- * const memory = new Memory('scientific_app.exe');
560
- *
561
550
  * // Read a double precision value
562
551
  * const preciseValue = memory.f64(0x12345678n);
563
552
  * console.log('Precise value:', preciseValue);
@@ -591,8 +580,6 @@ class Memory {
591
580
  *
592
581
  * @example
593
582
  * ```typescript
594
- * const memory = new Memory('scientific_app.exe');
595
- *
596
583
  * // Read an array of 5 double precision values
597
584
  * const preciseData = memory.f64Array(0x12345678n, 5);
598
585
  * console.log('Precise data:', preciseData);
@@ -630,8 +617,6 @@ class Memory {
630
617
  *
631
618
  * @example
632
619
  * ```typescript
633
- * const memory = new Memory('game.exe');
634
- *
635
620
  * // Read a signed 16-bit integer
636
621
  * const temperature = memory.i16(0x12345678n);
637
622
  * console.log('Temperature:', temperature);
@@ -665,8 +650,6 @@ class Memory {
665
650
  *
666
651
  * @example
667
652
  * ```typescript
668
- * const memory = new Memory('audio_app.exe');
669
- *
670
653
  * // Read an array of audio samples
671
654
  * const samples = memory.i16Array(0x12345678n, 1024);
672
655
  * console.log('Audio samples:', samples);
@@ -704,8 +687,6 @@ class Memory {
704
687
  *
705
688
  * @example
706
689
  * ```typescript
707
- * const memory = new Memory('game.exe');
708
- *
709
690
  * // Read a player's score
710
691
  * const score = memory.i32(0x12345678n);
711
692
  * console.log('Player score:', score);
@@ -739,8 +720,6 @@ class Memory {
739
720
  *
740
721
  * @example
741
722
  * ```typescript
742
- * const memory = new Memory('game.exe');
743
- *
744
723
  * // Read inventory item counts
745
724
  * const inventory = memory.i32Array(0x12345678n, 20);
746
725
  * console.log('Inventory:', inventory);
@@ -778,8 +757,6 @@ class Memory {
778
757
  *
779
758
  * @example
780
759
  * ```typescript
781
- * const memory = new Memory('database.exe');
782
- *
783
760
  * // Read a large number (timestamp, file size, etc.)
784
761
  * const timestamp = memory.i64(0x12345678n);
785
762
  * console.log('Timestamp:', timestamp);
@@ -813,8 +790,6 @@ class Memory {
813
790
  *
814
791
  * @example
815
792
  * ```typescript
816
- * const memory = new Memory('database.exe');
817
- *
818
793
  * // Read an array of large numbers
819
794
  * const largeNumbers = memory.i64Array(0x12345678n, 10);
820
795
  * console.log('Large numbers:', largeNumbers);
@@ -852,8 +827,6 @@ class Memory {
852
827
  *
853
828
  * @example
854
829
  * ```typescript
855
- * const memory = new Memory('game.exe');
856
- *
857
830
  * // Read a small signed value (e.g., direction, state)
858
831
  * const direction = memory.i8(0x12345678n);
859
832
  * console.log('Direction:', direction);
@@ -887,8 +860,6 @@ class Memory {
887
860
  *
888
861
  * @example
889
862
  * ```typescript
890
- * const memory = new Memory('game.exe');
891
- *
892
863
  * // Read an array of small signed values
893
864
  * const directions = memory.i8Array(0x12345678n, 8);
894
865
  * console.log('Movement directions:', directions);
@@ -917,6 +888,97 @@ class Memory {
917
888
  return this;
918
889
  }
919
890
 
891
+ /**
892
+ * Reads a 3×3 matrix from memory or writes a 3×3 matrix to memory.
893
+ *
894
+ * The matrix is represented as 9 contiguous 32-bit floating-point values (`Float32Array`).
895
+ * No transposition or stride is applied—values are copied exactly as stored in memory.
896
+ *
897
+ * @param address - Memory address to read from or write to
898
+ * @param values - Optional `Float32Array` of length 9 to write. If omitted, performs a read operation
899
+ * @returns `Float32Array` of length 9 when reading, or this `Memory` instance when writing
900
+ * @throws {RangeError} When `values.length` is not exactly 9
901
+ *
902
+ * @example
903
+ * ```typescript
904
+ * // Read a 3×3 matrix
905
+ * const matrix = memory.matrix3x3(0x12345678n); // Float32Array(9)
906
+ *
907
+ * // Write a 3×3 matrix (length must be 9)
908
+ * const next = new Float32Array([
909
+ * 1, 0, 0,
910
+ * 0, 1, 0,
911
+ * 0, 0, 1
912
+ * ]);
913
+ * memory.matrix3x3(0x12345678n, next);
914
+ * ```
915
+ */
916
+ public matrix3x3(address: bigint): Float32Array;
917
+ public matrix3x3(address: bigint, values: Float32Array): this;
918
+ public matrix3x3(address: bigint, values?: Float32Array): Float32Array | this {
919
+ if (values === undefined) {
920
+ const scratch = new Float32Array(0x09);
921
+
922
+ this.read(address, scratch);
923
+
924
+ return scratch;
925
+ }
926
+
927
+ if (values.length !== 0x09) {
928
+ throw new RangeError('values.length must be 9.');
929
+ }
930
+
931
+ this.write(address, values);
932
+
933
+ return this;
934
+ }
935
+
936
+ /**
937
+ * Reads a 4×4 matrix from memory or writes a 4×4 matrix to memory.
938
+ *
939
+ * The matrix is represented as 16 contiguous 32-bit floating-point values (`Float32Array`).
940
+ * No transposition or stride is applied—values are copied exactly as stored in memory.
941
+ *
942
+ * @param address - Memory address to read from or write to
943
+ * @param values - Optional `Float32Array` of length 16 to write. If omitted, performs a read operation
944
+ * @returns `Float32Array` of length 16 when reading, or this `Memory` instance when writing
945
+ * @throws {RangeError} When `values.length` is not exactly 16
946
+ *
947
+ * @example
948
+ * ```typescript
949
+ * // Read a 4×4 matrix
950
+ * const matrix = memory.matrix4x4(0x12345678n); // Float32Array(16)
951
+ *
952
+ * // Write a 4×4 matrix (length must be 16)
953
+ * const next = new Float32Array([
954
+ * 1, 0, 0, 0,
955
+ * 0, 1, 0, 0,
956
+ * 0, 0, 1, 0,
957
+ * 0, 0, 0, 1
958
+ * ]);
959
+ * memory.matrix4x4(0x12345678n, next);
960
+ * ```
961
+ */
962
+ public matrix4x4(address: bigint): Float32Array;
963
+ public matrix4x4(address: bigint, values: Float32Array): this;
964
+ public matrix4x4(address: bigint, values?: Float32Array): Float32Array | this {
965
+ if (values === undefined) {
966
+ const scratch = new Float32Array(0x10);
967
+
968
+ this.read(address, scratch);
969
+
970
+ return scratch;
971
+ }
972
+
973
+ if (values.length !== 0x10) {
974
+ throw new RangeError('values.length must be 16.');
975
+ }
976
+
977
+ this.write(address, values);
978
+
979
+ return this;
980
+ }
981
+
920
982
  /**
921
983
  * Reads a `NetworkUtlVector` (`Uint32Array`) from memory or writes a `NetworkUtlVector` to memory.
922
984
  *
@@ -936,8 +998,6 @@ class Memory {
936
998
  *
937
999
  * @example
938
1000
  * ```typescript
939
- * const memory = new Memory('network_app.exe');
940
- *
941
1001
  * // Read the current vector
942
1002
  * const ids = memory.networkUtlVector(0x12345678n);
943
1003
  * console.log('IDs:', Array.from(ids));
@@ -979,8 +1039,6 @@ class Memory {
979
1039
  *
980
1040
  * @example
981
1041
  * ```typescript
982
- * const memory = new Memory('game.exe');
983
- *
984
1042
  * // Read player rotation
985
1043
  * const rotation = memory.quaternion(0x12345678n);
986
1044
  * console.log('Player rotation:', rotation);
@@ -1022,8 +1080,6 @@ class Memory {
1022
1080
  *
1023
1081
  * @example
1024
1082
  * ```typescript
1025
- * const memory = new Memory('game.exe');
1026
- *
1027
1083
  * // Read bone rotations for skeletal animation
1028
1084
  * const boneRotations = memory.quaternionArray(0x12345678n, 50);
1029
1085
  * console.log('Bone rotations:', boneRotations);
@@ -1082,8 +1138,6 @@ class Memory {
1082
1138
  *
1083
1139
  * @example
1084
1140
  * ```typescript
1085
- * const memory = new Memory('game.exe');
1086
- *
1087
1141
  * // Read a port number or small positive value
1088
1142
  * const port = memory.u16(0x12345678n);
1089
1143
  * console.log('Network port:', port);
@@ -1117,8 +1171,6 @@ class Memory {
1117
1171
  *
1118
1172
  * @example
1119
1173
  * ```typescript
1120
- * const memory = new Memory('network_app.exe');
1121
- *
1122
1174
  * // Read an array of port numbers
1123
1175
  * const ports = memory.u16Array(0x12345678n, 10);
1124
1176
  * console.log('Active ports:', ports);
@@ -1156,8 +1208,6 @@ class Memory {
1156
1208
  *
1157
1209
  * @example
1158
1210
  * ```typescript
1159
- * const memory = new Memory('game.exe');
1160
- *
1161
1211
  * // Read player's money (always positive)
1162
1212
  * const money = memory.u32(0x12345678n);
1163
1213
  * console.log('Player money:', money);
@@ -1191,8 +1241,6 @@ class Memory {
1191
1241
  *
1192
1242
  * @example
1193
1243
  * ```typescript
1194
- * const memory = new Memory('game.exe');
1195
- *
1196
1244
  * // Read resource amounts
1197
1245
  * const resources = memory.u32Array(0x12345678n, 6);
1198
1246
  * console.log('Resources:', resources);
@@ -1230,8 +1278,6 @@ class Memory {
1230
1278
  *
1231
1279
  * @example
1232
1280
  * ```typescript
1233
- * const memory = new Memory('database.exe');
1234
- *
1235
1281
  * // Read a very large positive number
1236
1282
  * const recordId = memory.u64(0x12345678n);
1237
1283
  * console.log('Record ID:', recordId);
@@ -1265,8 +1311,6 @@ class Memory {
1265
1311
  *
1266
1312
  * @example
1267
1313
  * ```typescript
1268
- * const memory = new Memory('database.exe');
1269
- *
1270
1314
  * // Read an array of record IDs
1271
1315
  * const recordIds = memory.u64Array(0x12345678n, 100);
1272
1316
  * console.log('Record IDs:', recordIds);
@@ -1304,8 +1348,6 @@ class Memory {
1304
1348
  *
1305
1349
  * @example
1306
1350
  * ```typescript
1307
- * const memory = new Memory('game.exe');
1308
- *
1309
1351
  * // Read a byte value (0-255)
1310
1352
  * const opacity = memory.u8(0x12345678n);
1311
1353
  * console.log('UI opacity:', opacity);
@@ -1339,8 +1381,6 @@ class Memory {
1339
1381
  *
1340
1382
  * @example
1341
1383
  * ```typescript
1342
- * const memory = new Memory('image_editor.exe');
1343
- *
1344
1384
  * // Read pixel data
1345
1385
  * const pixels = memory.u8Array(0x12345678n, 1024);
1346
1386
  * console.log('Pixel data:', pixels);
@@ -1379,8 +1419,6 @@ class Memory {
1379
1419
  *
1380
1420
  * @example
1381
1421
  * ```typescript
1382
- * const memory = new Memory('game.exe');
1383
- *
1384
1422
  * // Read player position
1385
1423
  * const position = memory.vector2(0x12345678n);
1386
1424
  * console.log('Player position:', position);
@@ -1418,8 +1456,6 @@ class Memory {
1418
1456
  *
1419
1457
  * @example
1420
1458
  * ```typescript
1421
- * const memory = new Memory('game.exe');
1422
- *
1423
1459
  * // Read waypoints for AI pathfinding
1424
1460
  * const waypoints = memory.vector2Array(0x12345678n, 20);
1425
1461
  * console.log('AI waypoints:', waypoints);
@@ -1479,8 +1515,6 @@ class Memory {
1479
1515
  *
1480
1516
  * @example
1481
1517
  * ```typescript
1482
- * const memory = new Memory('game.exe');
1483
- *
1484
1518
  * // Read player 3D position
1485
1519
  * const position = memory.vector3(0x12345678n);
1486
1520
  * console.log('Player 3D position:', position);
@@ -1520,8 +1554,6 @@ class Memory {
1520
1554
  *
1521
1555
  * @example
1522
1556
  * ```typescript
1523
- * const memory = new Memory('game.exe');
1524
- *
1525
1557
  * // Read vertex positions for 3D model
1526
1558
  * const vertices = memory.vector3Array(0x12345678n, 500);
1527
1559
  * console.log('3D vertices:', vertices);