bun-memory 1.1.47 → 1.1.49

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 +197 -101
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bun-memory",
3
- "version": "1.1.47",
3
+ "version": "1.1.49",
4
4
  "author": "Stev Peifer <stev@bell.net>",
5
5
  "repository": {
6
6
  "type": "git",
package/structs/Memory.ts CHANGED
@@ -1533,26 +1533,71 @@ class Memory {
1533
1533
  }
1534
1534
 
1535
1535
  /**
1536
- * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as an Int16Array.
1536
+ * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as a UTF-8 string.
1537
1537
  * @param address Address of the TArray structure.
1538
- * @param values Optional Int16Array to write.
1538
+ * @param value Optional string to write.
1539
1539
  * @param force When writing, if true temporarily changes page protection to allow the write.
1540
- * @returns An Int16Array containing the elements, or this instance if writing.
1540
+ * @returns The string, or this instance if writing.
1541
1541
  * @example
1542
1542
  * ```ts
1543
1543
  * const rl = new Memory('RocketLeague.exe');
1544
- * const myArray = rl.tArrayI16(0x12345678n);
1545
- * rl.tArrayI16(0x12345678n, new Int16Array([1, 2, 3]));
1544
+ * const myString = rl.tArrayChar(0x12345678n);
1545
+ * rl.tArrayChar(0x12345678n, 'hello');
1546
1546
  * ```
1547
1547
  */
1548
- public tArrayI16(address: bigint): Int16Array;
1549
- public tArrayI16(address: bigint, values: Int16Array, force?: boolean): this;
1550
- public tArrayI16(address: bigint, values?: Int16Array, force?: boolean): Int16Array | this {
1548
+ public tArrayChar(address: bigint): string;
1549
+ public tArrayChar(address: bigint, value: string, force?: boolean): this;
1550
+ public tArrayChar(address: bigint, value?: string, force?: boolean): string | this {
1551
1551
  const dataPtr = this.u64(address);
1552
1552
 
1553
- if (values === undefined) {
1553
+ if (value === undefined) {
1554
1554
  const count = this.u32(address + 0x08n);
1555
- const scratch = new Int16Array(count);
1555
+
1556
+ if (count === 0x00) {
1557
+ return '';
1558
+ }
1559
+
1560
+ const scratch = Buffer.allocUnsafe(count - 0x01);
1561
+
1562
+ void this.read(dataPtr, scratch);
1563
+
1564
+ return scratch.toString('utf8');
1565
+ }
1566
+
1567
+ const scratch = Buffer.from(value, 'utf8');
1568
+
1569
+ this.u32(address + 0x08n, scratch.length + 0x01, force);
1570
+
1571
+ void this.write(dataPtr, scratch, force);
1572
+
1573
+ return this;
1574
+ }
1575
+
1576
+ /**
1577
+ * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as a Float32Array.
1578
+ * @param address Address of the TArray structure.
1579
+ * @param values Optional Float32Array to write.
1580
+ * @param force When writing, if true temporarily changes page protection to allow the write.
1581
+ * @returns A Float32Array containing the elements, or this instance if writing.
1582
+ * @example
1583
+ * ```ts
1584
+ * const rl = new Memory('RocketLeague.exe');
1585
+ * const myArray = rl.tArrayF32(0x12345678n);
1586
+ * rl.tArrayF32(0x12345678n, new Float32Array([1.0, 2.0, 3.0]));
1587
+ * ```
1588
+ */
1589
+ public tArrayF32(address: bigint): Float32Array;
1590
+ public tArrayF32(address: bigint, values: Float32Array, force?: boolean): this;
1591
+ public tArrayF32(address: bigint, values?: Float32Array, force?: boolean): Float32Array | this {
1592
+ const dataPtr = this.u64(address);
1593
+
1594
+ if (values === undefined) {
1595
+ const count = this.u32(address + 0x08n),
1596
+ scratch = new Float32Array(count); // prettier-ignore
1597
+
1598
+ if (count === 0) {
1599
+ return scratch;
1600
+ }
1556
1601
 
1557
1602
  void this.read(dataPtr, scratch);
1558
1603
 
@@ -1567,26 +1612,30 @@ class Memory {
1567
1612
  }
1568
1613
 
1569
1614
  /**
1570
- * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as an Int32Array.
1615
+ * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as a Float64Array.
1571
1616
  * @param address Address of the TArray structure.
1572
- * @param values Optional Int32Array to write.
1617
+ * @param values Optional Float64Array to write.
1573
1618
  * @param force When writing, if true temporarily changes page protection to allow the write.
1574
- * @returns An Int32Array containing the elements, or this instance if writing.
1619
+ * @returns A Float64Array containing the elements, or this instance if writing.
1575
1620
  * @example
1576
1621
  * ```ts
1577
1622
  * const rl = new Memory('RocketLeague.exe');
1578
- * const myArray = rl.tArrayI32(0x12345678n);
1579
- * rl.tArrayI32(0x12345678n, new Int32Array([1, 2, 3]));
1623
+ * const myArray = rl.tArrayF64(0x12345678n);
1624
+ * rl.tArrayF64(0x12345678n, new Float64Array([1.0, 2.0, 3.0]));
1580
1625
  * ```
1581
1626
  */
1582
- public tArrayI32(address: bigint): Int32Array;
1583
- public tArrayI32(address: bigint, values: Int32Array, force?: boolean): this;
1584
- public tArrayI32(address: bigint, values?: Int32Array, force?: boolean): Int32Array | this {
1627
+ public tArrayF64(address: bigint): Float64Array;
1628
+ public tArrayF64(address: bigint, values: Float64Array, force?: boolean): this;
1629
+ public tArrayF64(address: bigint, values?: Float64Array, force?: boolean): Float64Array | this {
1585
1630
  const dataPtr = this.u64(address);
1586
1631
 
1587
1632
  if (values === undefined) {
1588
- const count = this.u32(address + 0x08n);
1589
- const scratch = new Int32Array(count);
1633
+ const count = this.u32(address + 0x08n),
1634
+ scratch = new Float64Array(count); // prettier-ignore
1635
+
1636
+ if (count === 0) {
1637
+ return scratch;
1638
+ }
1590
1639
 
1591
1640
  void this.read(dataPtr, scratch);
1592
1641
 
@@ -1601,26 +1650,30 @@ class Memory {
1601
1650
  }
1602
1651
 
1603
1652
  /**
1604
- * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as a BigInt64Array.
1653
+ * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as an Int16Array.
1605
1654
  * @param address Address of the TArray structure.
1606
- * @param values Optional BigInt64Array to write.
1655
+ * @param values Optional Int16Array to write.
1607
1656
  * @param force When writing, if true temporarily changes page protection to allow the write.
1608
- * @returns A BigInt64Array containing the elements, or this instance if writing.
1657
+ * @returns An Int16Array containing the elements, or this instance if writing.
1609
1658
  * @example
1610
1659
  * ```ts
1611
1660
  * const rl = new Memory('RocketLeague.exe');
1612
- * const myArray = rl.tArrayI64(0x12345678n);
1613
- * rl.tArrayI64(0x12345678n, new BigInt64Array([1n, 2n, 3n]));
1661
+ * const myArray = rl.tArrayI16(0x12345678n);
1662
+ * rl.tArrayI16(0x12345678n, new Int16Array([1, 2, 3]));
1614
1663
  * ```
1615
1664
  */
1616
- public tArrayI64(address: bigint): BigInt64Array;
1617
- public tArrayI64(address: bigint, values: BigInt64Array, force?: boolean): this;
1618
- public tArrayI64(address: bigint, values?: BigInt64Array, force?: boolean): BigInt64Array | this {
1665
+ public tArrayI16(address: bigint): Int16Array;
1666
+ public tArrayI16(address: bigint, values: Int16Array, force?: boolean): this;
1667
+ public tArrayI16(address: bigint, values?: Int16Array, force?: boolean): Int16Array | this {
1619
1668
  const dataPtr = this.u64(address);
1620
1669
 
1621
1670
  if (values === undefined) {
1622
- const count = this.u32(address + 0x08n);
1623
- const scratch = new BigInt64Array(count);
1671
+ const count = this.u32(address + 0x08n),
1672
+ scratch = new Int16Array(count); // prettier-ignore
1673
+
1674
+ if (count === 0) {
1675
+ return scratch;
1676
+ }
1624
1677
 
1625
1678
  void this.read(dataPtr, scratch);
1626
1679
 
@@ -1635,26 +1688,30 @@ class Memory {
1635
1688
  }
1636
1689
 
1637
1690
  /**
1638
- * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as a Float32Array.
1691
+ * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as an Int32Array.
1639
1692
  * @param address Address of the TArray structure.
1640
- * @param values Optional Float32Array to write.
1693
+ * @param values Optional Int32Array to write.
1641
1694
  * @param force When writing, if true temporarily changes page protection to allow the write.
1642
- * @returns A Float32Array containing the elements, or this instance if writing.
1695
+ * @returns An Int32Array containing the elements, or this instance if writing.
1643
1696
  * @example
1644
1697
  * ```ts
1645
1698
  * const rl = new Memory('RocketLeague.exe');
1646
- * const myArray = rl.tArrayF32(0x12345678n);
1647
- * rl.tArrayF32(0x12345678n, new Float32Array([1.0, 2.0, 3.0]));
1699
+ * const myArray = rl.tArrayI32(0x12345678n);
1700
+ * rl.tArrayI32(0x12345678n, new Int32Array([1, 2, 3]));
1648
1701
  * ```
1649
1702
  */
1650
- public tArrayF32(address: bigint): Float32Array;
1651
- public tArrayF32(address: bigint, values: Float32Array, force?: boolean): this;
1652
- public tArrayF32(address: bigint, values?: Float32Array, force?: boolean): Float32Array | this {
1703
+ public tArrayI32(address: bigint): Int32Array;
1704
+ public tArrayI32(address: bigint, values: Int32Array, force?: boolean): this;
1705
+ public tArrayI32(address: bigint, values?: Int32Array, force?: boolean): Int32Array | this {
1653
1706
  const dataPtr = this.u64(address);
1654
1707
 
1655
1708
  if (values === undefined) {
1656
- const count = this.u32(address + 0x08n);
1657
- const scratch = new Float32Array(count);
1709
+ const count = this.u32(address + 0x08n),
1710
+ scratch = new Int32Array(count); // prettier-ignore
1711
+
1712
+ if (count === 0) {
1713
+ return scratch;
1714
+ }
1658
1715
 
1659
1716
  void this.read(dataPtr, scratch);
1660
1717
 
@@ -1669,26 +1726,30 @@ class Memory {
1669
1726
  }
1670
1727
 
1671
1728
  /**
1672
- * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as a Float64Array.
1729
+ * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as a BigInt64Array.
1673
1730
  * @param address Address of the TArray structure.
1674
- * @param values Optional Float64Array to write.
1731
+ * @param values Optional BigInt64Array to write.
1675
1732
  * @param force When writing, if true temporarily changes page protection to allow the write.
1676
- * @returns A Float64Array containing the elements, or this instance if writing.
1733
+ * @returns A BigInt64Array containing the elements, or this instance if writing.
1677
1734
  * @example
1678
1735
  * ```ts
1679
1736
  * const rl = new Memory('RocketLeague.exe');
1680
- * const myArray = rl.tArrayF64(0x12345678n);
1681
- * rl.tArrayF64(0x12345678n, new Float64Array([1.0, 2.0, 3.0]));
1737
+ * const myArray = rl.tArrayI64(0x12345678n);
1738
+ * rl.tArrayI64(0x12345678n, new BigInt64Array([1n, 2n, 3n]));
1682
1739
  * ```
1683
1740
  */
1684
- public tArrayF64(address: bigint): Float64Array;
1685
- public tArrayF64(address: bigint, values: Float64Array, force?: boolean): this;
1686
- public tArrayF64(address: bigint, values?: Float64Array, force?: boolean): Float64Array | this {
1741
+ public tArrayI64(address: bigint): BigInt64Array;
1742
+ public tArrayI64(address: bigint, values: BigInt64Array, force?: boolean): this;
1743
+ public tArrayI64(address: bigint, values?: BigInt64Array, force?: boolean): BigInt64Array | this {
1687
1744
  const dataPtr = this.u64(address);
1688
1745
 
1689
1746
  if (values === undefined) {
1690
- const count = this.u32(address + 0x08n);
1691
- const scratch = new Float64Array(count);
1747
+ const count = this.u32(address + 0x08n),
1748
+ scratch = new BigInt64Array(count); // prettier-ignore
1749
+
1750
+ if (count === 0) {
1751
+ return scratch;
1752
+ }
1692
1753
 
1693
1754
  void this.read(dataPtr, scratch);
1694
1755
 
@@ -1721,8 +1782,12 @@ class Memory {
1721
1782
  const dataPtr = this.u64(address);
1722
1783
 
1723
1784
  if (values === undefined) {
1724
- const count = this.u32(address + 0x08n);
1725
- const scratch = new Int8Array(count);
1785
+ const count = this.u32(address + 0x08n),
1786
+ scratch = new Int8Array(count); // prettier-ignore
1787
+
1788
+ if (count === 0) {
1789
+ return scratch;
1790
+ }
1726
1791
 
1727
1792
  void this.read(dataPtr, scratch);
1728
1793
 
@@ -1736,6 +1801,62 @@ class Memory {
1736
1801
  return this;
1737
1802
  }
1738
1803
 
1804
+ /**
1805
+ * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as an array of raw buffers.
1806
+ * Useful for reading/writing arrays of structs or custom-sized elements.
1807
+ * @param address Address of the TArray structure.
1808
+ * @param dataSize Size in bytes of each element.
1809
+ * @param values Optional array of Buffers to write.
1810
+ * @param force When writing, if true temporarily changes page protection to allow the write.
1811
+ * @returns An array of Buffers, one per element, or this instance if writing.
1812
+ * @example
1813
+ * ```ts
1814
+ * const rl = new Memory('RocketLeague.exe');
1815
+ * const structs = rl.tArrayRaw(0x12345678n, 0x18); // Array of 0x18-byte buffers
1816
+ * rl.tArrayRaw(0x12345678n, [buffer1, buffer2]);
1817
+ * ```
1818
+ */
1819
+ public tArrayRaw(address: bigint, dataSize: number): Buffer[];
1820
+ public tArrayRaw(address: bigint, values: Buffer[], force?: boolean): this;
1821
+ public tArrayRaw(address: bigint, dataSizeOrValues: number | Buffer[], force?: boolean): Buffer[] | this {
1822
+ const dataPtr = this.u64(address);
1823
+
1824
+ if (typeof dataSizeOrValues === 'number') {
1825
+ const count = this.u32(address + 0x08n),
1826
+ dataSize = dataSizeOrValues; // prettier-ignore
1827
+
1828
+ if (count === 0) {
1829
+ return [];
1830
+ }
1831
+
1832
+ const scratch = Buffer.allocUnsafe(count * dataSize);
1833
+
1834
+ void this.read(dataPtr, scratch);
1835
+
1836
+ const result: Buffer[] = new Array(count);
1837
+
1838
+ for (let i = 0; i < count; i++) {
1839
+ result[i] = scratch.subarray(i * dataSize, (i + 1) * dataSize);
1840
+ }
1841
+
1842
+ return result;
1843
+ }
1844
+
1845
+ const values = dataSizeOrValues;
1846
+ const dataSize = values[0].length;
1847
+ const scratch = Buffer.allocUnsafe(values.length * dataSize);
1848
+
1849
+ for (let i = 0; i < values.length; i++) {
1850
+ values[i].copy(scratch, i * dataSize);
1851
+ }
1852
+
1853
+ this.u32(address + 0x08n, values.length, force);
1854
+
1855
+ void this.write(dataPtr, scratch, force);
1856
+
1857
+ return this;
1858
+ }
1859
+
1739
1860
  /**
1740
1861
  * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as a Uint16Array.
1741
1862
  * @param address Address of the TArray structure.
@@ -1755,8 +1876,12 @@ class Memory {
1755
1876
  const dataPtr = this.u64(address);
1756
1877
 
1757
1878
  if (values === undefined) {
1758
- const count = this.u32(address + 0x08n);
1759
- const scratch = new Uint16Array(count);
1879
+ const count = this.u32(address + 0x08n),
1880
+ scratch = new Uint16Array(count); // prettier-ignore
1881
+
1882
+ if (count === 0) {
1883
+ return scratch;
1884
+ }
1760
1885
 
1761
1886
  void this.read(dataPtr, scratch);
1762
1887
 
@@ -1789,8 +1914,12 @@ class Memory {
1789
1914
  const dataPtr = this.u64(address);
1790
1915
 
1791
1916
  if (values === undefined) {
1792
- const count = this.u32(address + 0x08n);
1793
- const scratch = new Uint32Array(count);
1917
+ const count = this.u32(address + 0x08n),
1918
+ scratch = new Uint32Array(count); // prettier-ignore
1919
+
1920
+ if (count === 0) {
1921
+ return scratch;
1922
+ }
1794
1923
 
1795
1924
  void this.read(dataPtr, scratch);
1796
1925
 
@@ -1823,8 +1952,12 @@ class Memory {
1823
1952
  const dataPtr = this.u64(address);
1824
1953
 
1825
1954
  if (values === undefined) {
1826
- const count = this.u32(address + 0x08n);
1827
- const scratch = new BigUint64Array(count);
1955
+ const count = this.u32(address + 0x08n),
1956
+ scratch = new BigUint64Array(count); // prettier-ignore
1957
+
1958
+ if (count === 0) {
1959
+ return scratch;
1960
+ }
1828
1961
 
1829
1962
  void this.read(dataPtr, scratch);
1830
1963
 
@@ -1857,8 +1990,12 @@ class Memory {
1857
1990
  const dataPtr = this.u64(address);
1858
1991
 
1859
1992
  if (values === undefined) {
1860
- const count = this.u32(address + 0x08n);
1861
- const scratch = new Uint8Array(count);
1993
+ const count = this.u32(address + 0x08n),
1994
+ scratch = new Uint8Array(count); // prettier-ignore
1995
+
1996
+ if (count === 0) {
1997
+ return scratch;
1998
+ }
1862
1999
 
1863
2000
  void this.read(dataPtr, scratch);
1864
2001
 
@@ -1896,47 +2033,6 @@ class Memory {
1896
2033
  return this.tArrayU64(address, values, force);
1897
2034
  }
1898
2035
 
1899
- /**
1900
- * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as a UTF-8 string.
1901
- * @param address Address of the TArray structure.
1902
- * @param value Optional string to write.
1903
- * @param force When writing, if true temporarily changes page protection to allow the write.
1904
- * @returns The string, or this instance if writing.
1905
- * @example
1906
- * ```ts
1907
- * const rl = new Memory('RocketLeague.exe');
1908
- * const myString = rl.tArrayChar(0x12345678n);
1909
- * rl.tArrayChar(0x12345678n, 'hello');
1910
- * ```
1911
- */
1912
- public tArrayChar(address: bigint): string;
1913
- public tArrayChar(address: bigint, value: string, force?: boolean): this;
1914
- public tArrayChar(address: bigint, value?: string, force?: boolean): string | this {
1915
- const dataPtr = this.u64(address);
1916
-
1917
- if (value === undefined) {
1918
- const count = this.u32(address + 0x08n);
1919
-
1920
- if (count === 0x00) {
1921
- return '';
1922
- }
1923
-
1924
- const scratch = Buffer.allocUnsafe(count - 0x01);
1925
-
1926
- void this.read(dataPtr, scratch);
1927
-
1928
- return scratch.toString('utf8');
1929
- }
1930
-
1931
- const scratch = Buffer.from(value, 'utf8');
1932
-
1933
- this.u32(address + 0x08n, scratch.length + 0x01, force);
1934
-
1935
- void this.write(dataPtr, scratch, force);
1936
-
1937
- return this;
1938
- }
1939
-
1940
2036
  /**
1941
2037
  * Reads or writes a TArray (Data at 0x00, Count at 0x08, Max at 0x0c) as a UTF-16LE string.
1942
2038
  * This is the format used by FName/FString in Unreal Engine.