bun-memory 1.1.10 → 1.1.11

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.
@@ -10,7 +10,12 @@ import ClientDLLJSON from './offsets/client_dll.json';
10
10
  import OffsetsJSON from './offsets/offsets.json';
11
11
 
12
12
  // !
13
+ const buffer = Buffer.from('Hello world!');
14
+ const bufferPtr = ptr(buffer);
13
15
 
16
+ const cString = new CString(bufferPtr);
17
+
18
+ console.log(cString.byteLength, cString.byteOffset); // undefined undefined
14
19
  // !
15
20
 
16
21
  const Delay = 2.5;
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.10",
25
+ "version": "1.1.11",
26
26
  "main": "./index.ts",
27
27
  "keywords": [
28
28
  "bun",
package/structs/Memory.ts CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { CString, FFIType, dlopen, read } from 'bun:ffi';
6
6
 
7
- import type { Module, Quaternion, Region, Scratch, Vector2, Vector3 } from '../types/Memory';
7
+ import type { Module, NetworkUtlVector, Quaternion, Region, Scratch, Vector2, Vector3 } from '../types/Memory';
8
8
  import Win32Error from './Win32Error';
9
9
 
10
10
  const { f32, f64, i16, i32, i64, i8, u16, u32, u64, u8 } = read;
@@ -917,6 +917,58 @@ class Memory {
917
917
  return this;
918
918
  }
919
919
 
920
+ /**
921
+ * Reads a `NetworkUtlVector` (`Uint32Array`) from memory or writes a `NetworkUtlVector` to memory.
922
+ *
923
+ * The vector is represented in memory as a small header with an out-of-line elements buffer.
924
+ * Layout at `address`:
925
+ * - 0x00: `uint32` size (number of elements)
926
+ * - 0x04: `uint32` capacity/reserved (not modified by this method)
927
+ * - 0x08: `uint64` pointer to a contiguous array of `uint32` elements
928
+ *
929
+ * When reading, this method returns a `Uint32Array` containing `size` elements copied from the
930
+ * elements pointer. When writing, it updates the size field and writes the provided values to the
931
+ * existing elements buffer (no reallocation is performed).
932
+ *
933
+ * @param address - Memory address of the vector header to read from or write to
934
+ * @param values - Optional `NetworkUtlVector` to write. If omitted, performs a read operation
935
+ * @returns `NetworkUtlVector` when reading, or this `Memory` instance when writing
936
+ *
937
+ * @example
938
+ * ```typescript
939
+ * const memory = new Memory('network_app.exe');
940
+ *
941
+ * // Read the current vector
942
+ * const ids = memory.networkUtlVector(0x12345678n);
943
+ * console.log('IDs:', Array.from(ids));
944
+ *
945
+ * // Write new values (must fit the existing buffer capacity)
946
+ * const next = new Uint32Array([10, 20, 30, 40]);
947
+ * memory.networkUtlVector(0x12345678n, next);
948
+ * ```
949
+ */
950
+ public networkUtlVector(address: bigint): NetworkUtlVector;
951
+ public networkUtlVector(address: bigint, values: NetworkUtlVector): this;
952
+ public networkUtlVector(address: bigint, values?: NetworkUtlVector): NetworkUtlVector | this {
953
+ const elementsPtr = this.u64(address + 0x08n);
954
+
955
+ if (values === undefined) {
956
+ const size = this.u32(address);
957
+
958
+ const scratch = new Uint32Array(size);
959
+
960
+ this.read(elementsPtr, scratch);
961
+
962
+ return scratch;
963
+ }
964
+
965
+ this.u32(address, values.length);
966
+
967
+ this.write(elementsPtr, values);
968
+
969
+ return this;
970
+ }
971
+
920
972
  /**
921
973
  * Reads a quaternion (4D rotation) from memory or writes a quaternion to memory.
922
974
  * Quaternions are stored as four 32-bit floats: x, y, z, w.
package/types/Memory.ts CHANGED
@@ -136,6 +136,65 @@ export type Module = {
136
136
  size: number;
137
137
  };
138
138
 
139
+ /**
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
+ *
185
+ * @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
194
+ * ```
195
+ */
196
+ export type NetworkUtlVector = Uint32Array;
197
+
139
198
  /**
140
199
  * Represents a quaternion for 3D rotations.
141
200
  *