bun-memory 1.1.2 → 1.1.3

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/structs/Memory.ts +48 -1
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.2",
25
+ "version": "1.1.3",
26
26
  "main": "./index.ts",
27
27
  "keywords": [
28
28
  "bun",
package/structs/Memory.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  // TODO: Reintroduce indexOf(…)…
3
3
  // TODO: String methods…
4
4
 
5
- import { FFIType, dlopen, read } from 'bun:ffi';
5
+ import { CString, FFIType, dlopen, read } from 'bun:ffi';
6
6
 
7
7
  import type { Module, Quaternion, Region, Scratch, Vector2, Vector3 } from '../types/Memory';
8
8
  import Win32Error from './Win32Error';
@@ -261,6 +261,8 @@ class Memory {
261
261
  * @returns This Memory instance for method chaining
262
262
  * @throws {Win32Error} When the read operation fails
263
263
  *
264
+ * @todo Research what it will take to add CString to the Scratch type.
265
+ *
264
266
  * @example
265
267
  * ```typescript
266
268
  * const memory = new Memory('notepad.exe');
@@ -425,6 +427,51 @@ class Memory {
425
427
  return this;
426
428
  }
427
429
 
430
+ /**
431
+ * Reads a NUL-terminated C string from memory or writes a NUL-terminated C string to memory.
432
+ *
433
+ * When reading, up to `length` bytes are copied into a temporary buffer and a `CString`
434
+ * is constructed from that buffer. Ensure the requested `length` is large enough to
435
+ * include the terminator to avoid truncation. When writing, pass a `CString` that is
436
+ * already NUL-terminated.
437
+ *
438
+ * @param address - Memory address to read from or write to
439
+ * @param lengthOrValue - Number of bytes to read (when reading), or `CString` to write (when writing)
440
+ * @returns `CString` when reading, or this `Memory` instance when writing
441
+ *
442
+ * @todo Research and consider alternatives that do not require so many new allocations.
443
+ *
444
+ * @example
445
+ * ```typescript
446
+ * const memory = new Memory('game.exe');
447
+ *
448
+ * // Read up to 64 bytes and interpret as a C string
449
+ * const playerName = memory.cString(0x12345678n, 64);
450
+ * console.log('Player name:', playerName.toString());
451
+ *
452
+ * // Write a C string (NUL-terminated)
453
+ * const value = new CString('PlayerOne');
454
+ * memory.cString(0x12345678n, value);
455
+ * ```
456
+ */
457
+ public cString(address: bigint, length: number): CString;
458
+ public cString(address: bigint, value: CString): this;
459
+ public cString(address: bigint, lengthOrValue: number | CString): CString | this {
460
+ if (typeof lengthOrValue === 'number') {
461
+ const scratch = Buffer.allocUnsafe(lengthOrValue);
462
+
463
+ this.read(address, scratch);
464
+
465
+ return new CString(scratch.ptr);
466
+ }
467
+
468
+ const scratch = Buffer.from(lengthOrValue);
469
+
470
+ this.write(address, scratch);
471
+
472
+ return this;
473
+ }
474
+
428
475
  /**
429
476
  * Reads a 32-bit floating-point value from memory or writes a 32-bit floating-point value to memory.
430
477
  *