|
1 | 1 | import {CommandDispatcher} from "@jsprismarine/brigadier"; |
| 2 | +import {writeString} from "@/memory/utils.ts"; |
| 3 | +import {canvasTextWidth} from "@/gameChat/gameChatUtility.ts"; |
| 4 | + |
| 5 | +class CommandManager { |
| 6 | + private commands: Map<string, { |
| 7 | + name: string, |
| 8 | + description: string, |
| 9 | + ptr: number |
| 10 | + }>; |
| 11 | + constructor() { |
| 12 | + this.commands = new Map(); |
| 13 | + } |
| 14 | + registerCommand(name: string, description: string) { |
| 15 | + if (window.electron) { |
| 16 | + (async () => { |
| 17 | + // return; |
| 18 | + while (!window.Module?.HEAPU8) { |
| 19 | + await new Promise(r => setTimeout(r, 50)); |
| 20 | + } |
| 21 | + |
| 22 | + const ptr = window.Module._malloc(68); |
| 23 | + const desPtr = window.Module._malloc(name.length + description.length + 2); |
| 24 | + const desPtr2 = window.Module._malloc(name.length + description.length + 5); |
| 25 | + |
| 26 | + window.Module.HEAPU32[ptr / 4] = desPtr; |
| 27 | + window.Module.HEAPU32[(ptr + 4) / 4] = 4; |
| 28 | + window.Module.HEAPU32[(ptr + 8) / 4] = 0; |
| 29 | + window.Module.HEAPF32[(ptr + 12) / 4] = canvasTextWidth(`/${name} - ${description}`); |
| 30 | + window.Module.HEAPU32[(ptr + 16) / 4] = 1; |
| 31 | + window.Module.HEAPU32[(ptr + 20) / 4] = 0; |
| 32 | + window.Module.HEAPU32[(ptr + 24) / 4] = 0; |
| 33 | + window.Module.HEAPU32[(ptr + 28) / 4] = 0; |
| 34 | + window.Module.HEAPU32[(ptr + 32) / 4] = desPtr2; |
| 35 | + window.Module.HEAPU32[(ptr + 36) / 4] = name.length + description.length + 5; |
| 36 | + window.Module.HEAPU32[(ptr + 40) / 4] = 2147483728; |
| 37 | + window.Module.HEAPU32[(ptr + 44) / 4] = 0x00f3c4a3; // color |
| 38 | + window.Module.HEAPU32[(ptr + 48) / 4] = 0; |
| 39 | + window.Module.HEAPU32[(ptr + 52) / 4] = 1039516303; |
| 40 | + |
| 41 | + window.Module.HEAPF32[(ptr + 56) / 4] = 14.00; |
| 42 | + |
| 43 | + window.Module.HEAPU32[(ptr + 60) / 4] = 264; |
| 44 | + window.Module.HEAPU32[(ptr + 64) / 4] = 0; |
| 45 | + writeString(name, desPtr); |
| 46 | + writeString(description, desPtr + name.length + 1); |
| 47 | + writeString(`/${name} - ${description}`, desPtr2); |
| 48 | + |
| 49 | + this.commands.set(name, { name, description, ptr }); |
| 50 | + })() |
| 51 | + } else { |
| 52 | + this.commands.set(name, {ptr: 0, name, description}); |
| 53 | + } |
| 54 | + } |
| 55 | + getAllCommands() { |
| 56 | + return this.commands; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +export const commandManager = new CommandManager(); |
2 | 61 |
|
3 | 62 | export const dispatcher = new CommandDispatcher(); |
4 | 63 |
|
| 64 | + |
| 65 | +if (window.electron) { |
| 66 | + (async () => { |
| 67 | + while (!window.Module?.HEAPU8) { |
| 68 | + await new Promise(r => setTimeout(r, 50)); |
| 69 | + } |
| 70 | + |
| 71 | + let firstPtr = 0; |
| 72 | + window.Module.asm.registerHookPre(440); |
| 73 | + |
| 74 | + window.ayuHooks.pre_79 = (arg0: number, arg1: number) => { |
| 75 | + if (!firstPtr) { |
| 76 | + firstPtr = arg1; |
| 77 | + } |
| 78 | + if (arg1 == firstPtr) { |
| 79 | + for (const [, { ptr }] of commandManager.getAllCommands()) { |
| 80 | + window.Module.asm[79]?.(arg0, ptr); |
| 81 | + } |
| 82 | + } |
| 83 | + }; |
| 84 | + })(); |
| 85 | +} |
| 86 | + |
| 87 | + |
5 | 88 | document.addEventListener("keydown", async (e) => { |
6 | 89 | if (e.key !== "Enter") { |
7 | 90 | return; |
|
0 commit comments