Skip to content

Commit e4d446d

Browse files
committed
feat: putting text into game chat & notice api for addon
1 parent 903355a commit e4d446d

File tree

5 files changed

+103
-5
lines changed

5 files changed

+103
-5
lines changed

src/Api.vue

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script setup lang="ts">
22
import {useI18n} from "vue-i18n";
3+
import {notice} from "@/infoHud/notice/notice.ts";
34
45
const i18n = useI18n();
56
@@ -9,6 +10,9 @@ window.ayuScriptApi = {
910
addon: {
1011
mergeI18n: (lang: string, langData: LangData) => {
1112
i18n.mergeLocaleMessage(lang, langData);
13+
},
14+
notice: (content: string) => {
15+
notice(content);
1216
}
1317
}
1418
}

src/florr.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type ModuleType = {
1515
HEAPU32: Uint32Array;
1616
HEAPF32: Float32Array;
1717
HEAPF64: Float64Array;
18+
_malloc: (number) => number;
1819
} | {
1920
HEAP8: undefined;
2021
HEAP16: undefined;

src/gameChat/gameChatUtility.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// TODO refactor this file
2+
// TODO find these two value in wasm
3+
const base = 19286288;
4+
const overflow = 3563868;
5+
6+
function writeString(str: string) {
7+
if (!window.Module.HEAPU8) {
8+
throw Error('Game isn\'t ready');
9+
}
10+
const encoder = new TextEncoder();
11+
const utf8 = encoder.encode(str);
12+
const ptr = window.Module._malloc(utf8.length + 1);
13+
window.Module.HEAPU8.set(utf8, ptr);
14+
window.Module.HEAPU8[ptr + utf8.length] = 0;
15+
return ptr;
16+
}
17+
18+
function utf8Length(str: string) {
19+
const encoder = new TextEncoder();
20+
const utf8 = encoder.encode(str);
21+
return utf8.length;
22+
}
23+
24+
function canvasTextWidth(text: string) {
25+
const canvas = document.createElement('canvas');
26+
const ctx = canvas.getContext('2d');
27+
ctx!.font = '14px Game';
28+
return ctx!.measureText(text).width;
29+
}
30+
31+
export function addChatMessage(segments: {text: string, r: number, g: number, b: number}[]) {
32+
if (!window.Module.HEAPU8) {
33+
return;
34+
}
35+
let count = window.Module.HEAP32[(base - 8) / 4];
36+
if (count >= 100) {
37+
for (let i = 0; i < 99; i++) {
38+
for (let j = 0; j < 408; j++) {
39+
window.Module.HEAP8[base + 408 * i + j] = window.Module.HEAP8[base + 408 * (i + 1) + j];
40+
}
41+
}
42+
count = 99;
43+
}
44+
45+
const var5 = base + 408 * count;
46+
for (let i = 0; i < 408; i++) {
47+
window.Module.HEAP8[var5 + i] = 0;
48+
}
49+
50+
window.Module.HEAP32[(var5 + 4) / 4] = 1;
51+
window.Module.HEAP32[(var5 + 12) / 4] = 0;
52+
window.Module.HEAPF64[(var5 + 24) / 8] = Date.now();
53+
window.Module.HEAP32[(var5 + 48) / 4] = overflow;
54+
55+
const segCount = segments.length;
56+
57+
const var1 = window.Module._malloc(32 + segCount * 48);
58+
window.Module.HEAP32[(var5 + 12) / 4] = var1;
59+
for (let i = 0; i < 32 + segCount * 48; i++) {
60+
window.Module.HEAP8[var1 + i] = 0;
61+
}
62+
63+
window.Module.HEAP32[var1 / 4] = segCount;
64+
window.Module.HEAP32[(var1 + 4) / 4] = 55;
65+
66+
const segBase = var1 + 32;
67+
window.Module.HEAP32[(var1 + 8) / 4] = segBase;
68+
69+
for (let s = 0; s < segCount; s++) {
70+
const {text, r = 255, g = 255, b = 255} = segments[s];
71+
const var0 = segBase + s * 48;
72+
73+
window.Module.HEAPF32[var0 / 4] = canvasTextWidth(text);
74+
window.Module.HEAP32[(var0 + 4) / 4] = 1;
75+
window.Module.HEAPF32[(var0 + 8) / 4] = -13;
76+
window.Module.HEAPF32[(var0 + 12) / 4] = 3;
77+
window.Module.HEAP32[(var0 + 16) / 4] = 1;
78+
window.Module.HEAP32[(var0 + 20) / 4] = writeString(text);
79+
window.Module.HEAP32[(var0 + 24) / 4] = utf8Length(text);
80+
window.Module.HEAPU8[var0 + 28] = 64;
81+
window.Module.HEAPU8[var0 + 29] = 0;
82+
window.Module.HEAPU8[var0 + 30] = 0;
83+
window.Module.HEAPU8[var0 + 31] = 128;
84+
window.Module.HEAPU8[var0 + 32] = r;
85+
window.Module.HEAPU8[var0 + 33] = g;
86+
window.Module.HEAPU8[var0 + 34] = b;
87+
window.Module.HEAPU8[var0 + 35] = 0;
88+
window.Module.HEAPU8[var0 + 36] = 0;
89+
window.Module.HEAPU8[var0 + 37] = 0;
90+
window.Module.HEAPU8[var0 + 38] = 0;
91+
window.Module.HEAPU8[var0 + 39] = 0;
92+
window.Module.HEAPF32[(var0 + 40) / 4] = 0.12;
93+
window.Module.HEAPF32[(var0 + 44) / 4] = 14.0;
94+
}
95+
96+
window.Module.HEAP32[(base - 8) / 4] = count + 1;
97+
}

src/gameWebsocket.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,13 @@ const nativeWebSocket = WebSocket;
99
export function patchWebsocket(){
1010
window.WebSocket = function(...args: ConstructorParameters<typeof WebSocket>) {
1111
const url = args[0] as string;
12-
1312
const isGameServer = gameWSSPatterns.some(pattern => pattern.test(url));
1413

15-
1614
if (isGameServer) {
17-
console.log('Allowing connection to game server:', url);
1815
const socket = new nativeWebSocket(...args);
1916
wsURL = socket.url;
2017
return socket;
2118
} else {
22-
23-
console.log('Allowing connection to:', url);
2419
const socket = new nativeWebSocket(...args);
2520
wsURL = socket.url;
2621
return socket;

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {Cp6, Florrio, ModuleType} from "@/florr";
33
export interface AyuScriptAPI {
44
addon: {
55
mergeI18n: (lang: string, langData: Record<string, string>) => undefined;
6+
notice: (content: string) => undefined;
67
}
78
}
89

0 commit comments

Comments
 (0)