1- import { matches , readLeb128 } from "@/memory/utils.ts" ;
2-
3- function parseInventory ( bytes : Uint8Array ) : number {
4- const patternPrefix = [ 0x41 , 0x01 , 0x3a , 0x00 , 0x00 , 0x41 ] ;
5- const patternSuffix = [ 0x41 , 0x05 , 0x36 , 0x02 , 0x00 ] ;
6-
1+ import { matches , readLeb128 } from "@/memory/utils.ts" ;
2+
3+ function parsePattern (
4+ bytes : Uint8Array ,
5+ prefix : number [ ] ,
6+ suffix : number [ ] ,
7+ transform : ( value : number ) => number = v => v
8+ ) : number {
79 for ( let i = 0 ; i < bytes . length ; i ++ ) {
8- if ( ! matches ( bytes , i , patternPrefix ) ) continue ;
10+ if ( ! matches ( bytes , i , prefix ) ) continue ;
911
10- const addrStart = i + patternPrefix . length ;
11- const { value : addr , nextIndex } = readLeb128 ( bytes , addrStart ) ;
12+ const addrStart = i + prefix . length ;
13+ const { value, nextIndex } = readLeb128 ( bytes , addrStart ) ;
1214
13- if ( ! matches ( bytes , nextIndex , patternSuffix ) ) continue ;
15+ if ( ! matches ( bytes , nextIndex , suffix ) ) continue ;
1416
15- return addr >> 2 ;
17+ return transform ( value ) ;
1618 }
1719
18- throw new Error ( ' Could not find the magic value for "inventory"' ) ;
20+ throw new Error ( ` Could not find pattern.` ) ;
1921}
2022
21- async function streamToUint8Array ( stream : ReadableStream < Uint8Array > ) : Promise < Uint8Array > {
22- const reader = stream . getReader ( ) ;
23- const chunks : Uint8Array [ ] = [ ] ;
24-
25- while ( true ) {
26- const { value, done } = await reader . read ( ) ;
27- if ( done ) break ;
28- if ( value ) chunks . push ( value ) ;
29- }
30-
31- // Calculate total length
32- const totalLength = chunks . reduce ( ( sum , chunk ) => sum + chunk . length , 0 ) ;
33-
34- // Concatenate all chunks
35- const result = new Uint8Array ( totalLength ) ;
36- let offset = 0 ;
37- for ( const chunk of chunks ) {
38- result . set ( chunk , offset ) ;
39- offset += chunk . length ;
40- }
41-
42- return result ;
23+ async function fetchWasmBytes ( ) : Promise < Uint8Array > {
24+ const response = await fetch ( `https://static.florr.io/${ window . versionHash } /client.wasm` ) ;
25+ const buffer = await response . arrayBuffer ( ) ;
26+ return new Uint8Array ( buffer ) ;
4327}
4428
45-
4629export async function injectAPI ( ) : Promise < {
47- inventory : number
48- } > {
49- return new Promise ( ( resolve => {
50- fetch ( `https://static.florr.io/${ window . versionHash } /client.wasm` )
51- . then ( response => response . body )
52- . then ( async stream => {
53- const bytes = await streamToUint8Array ( stream ! ) ;
54- resolve ( {
55- inventory : parseInventory ( bytes )
56- } ) ;
57- } ) ;
58- } ) ) ;
59- }
30+ inventory : number ;
31+ chatBase : number ;
32+ chatOverflow : number ;
33+ } > {
34+ const bytes = await fetchWasmBytes ( ) ;
35+
36+ return {
37+ inventory : parsePattern (
38+ bytes ,
39+ [ 0x41 , 0x01 , 0x3a , 0x00 , 0x00 , 0x41 ] ,
40+ [ 0x41 , 0x05 , 0x36 , 0x02 , 0x00 ] ,
41+ value => value >> 2
42+ ) ,
43+ chatBase : parsePattern (
44+ bytes ,
45+ [ 0x41 , 0x98 , 0x03 , 0x6C , 0x22 , 0x00 , 0x41 ] ,
46+ [ 0x6A , 0x41 , 0x00 , 0x41 , 0x98 , 0x03 ]
47+ ) ,
48+ chatOverflow : parsePattern (
49+ bytes ,
50+ [ 0x22 , 0x05 , 0x41 ] ,
51+ [ 0x36 , 0x02 , 0x00 , 0x20 , 0x02 , 0x41 , 0x08 , 0x6A ]
52+ )
53+ } ;
54+ }
0 commit comments