diff --git a/web/.gitignore b/web/.gitignore index a547bf3..04f08ca 100644 --- a/web/.gitignore +++ b/web/.gitignore @@ -9,6 +9,7 @@ lerna-debug.log* node_modules dist +dist-demo dist-ssr *.local diff --git a/web/package-lock.json b/web/package-lock.json index 95418cb..bfcd2d0 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -1,13 +1,14 @@ { - "name": "web", + "name": "webspeak3-web", "version": "0.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "web", + "name": "webspeak3-web", "version": "0.0.0", "dependencies": { + "fflate": "^0.8.3", "react": "^19.2.7", "react-dom": "^19.2.7" }, @@ -855,6 +856,12 @@ } } }, + "node_modules/fflate": { + "version": "0.8.3", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.3.tgz", + "integrity": "sha512-tbZNuJrLwGUp3zshBtdy4W+ORxZuIh8a5ilyIEQDC5rY1f3U20JMry0Ll3WBzU58EZKsEuJFXhb5gwv8CsPvgA==", + "license": "MIT" + }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", diff --git a/web/package.json b/web/package.json index d3aee06..2d9966f 100644 --- a/web/package.json +++ b/web/package.json @@ -10,6 +10,7 @@ "preview": "vite preview" }, "dependencies": { + "fflate": "^0.8.3", "react": "^19.2.7", "react-dom": "^19.2.7" }, diff --git a/web/src/App.tsx b/web/src/App.tsx index 71615e5..1b393eb 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -40,6 +40,7 @@ import { setSoundsOutputDevice, type SoundEventId, } from "./sounds"; +import { parseSoundpack } from "./soundpack"; // In dev, the gateway runs standalone on its own port. In production it's // served from the same origin/port as the web app (single container behind a @@ -1113,6 +1114,11 @@ function SoundsPanel() { ); const fileInputRef = useRef(null); const uploadTargetRef = useRef(null); + const soundpackInputRef = useRef(null); + const [soundpackResult, setSoundpackResult] = useState<{ + matchedCount: number; + unmatchedFiles: string[]; + } | null>(null); useEffect(() => { let cancelled = false; @@ -1170,6 +1176,27 @@ function SoundsPanel() { }); }; + const handleImportSoundpackClick = () => soundpackInputRef.current?.click(); + + const handleSoundpackSelected = async (e: React.ChangeEvent) => { + const file = e.target.files?.[0]; + e.target.value = ""; + if (!file) return; + setSoundpackResult(null); + const { matched, unmatchedFiles } = await parseSoundpack(file); + for (const [event, sound] of Object.entries(matched) as [SoundEventId, { name: string; blob: Blob }][]) { + await saveCustomSound(event, new File([sound.blob], sound.name, { type: sound.blob.type })); + } + setCustomNames((prev) => { + const next = { ...prev }; + for (const [event, sound] of Object.entries(matched) as [SoundEventId, { name: string; blob: Blob }][]) { + next[event] = sound.name; + } + return next; + }); + setSoundpackResult({ matchedCount: Object.keys(matched).length, unmatchedFiles }); + }; + return ( <>

{t("sounds.title")}

@@ -1190,6 +1217,17 @@ function SoundsPanel() { /> {Math.round(volume * 100)}% +
+ +
+ {soundpackResult && ( +

+ {t("sounds.importResult", { + matched: String(soundpackResult.matchedCount), + unmatched: String(soundpackResult.unmatchedFiles.length), + })} +

+ )} {SOUND_EVENTS.map((eventId) => ( @@ -1226,6 +1264,13 @@ function SoundsPanel() { style={{ display: "none" }} onChange={(e) => void handleFileSelected(e)} /> + void handleSoundpackSelected(e)} + /> ); } diff --git a/web/src/i18n.tsx b/web/src/i18n.tsx index 9d602a0..afb1514 100644 --- a/web/src/i18n.tsx +++ b/web/src/i18n.tsx @@ -234,6 +234,8 @@ const translations: Record> = { "sounds.test": "▶ Testen", "sounds.upload": "Hochladen", "sounds.reset": "Zurücksetzen", + "sounds.importSoundpack": "Soundpack importieren (.ts3soundpack)", + "sounds.importResult": "{matched} Sound(s) übernommen, {unmatched} Datei(en) nicht zugeordnet", "nachrichten.title": "Nachrichten", "nachrichten.subtitle": "Nachrichten anpassen", @@ -471,6 +473,8 @@ const translations: Record> = { "sounds.test": "▶ Test", "sounds.upload": "Upload", "sounds.reset": "Reset", + "sounds.importSoundpack": "Import soundpack (.ts3soundpack)", + "sounds.importResult": "{matched} sound(s) imported, {unmatched} file(s) not matched", "nachrichten.title": "Messages", "nachrichten.subtitle": "Customize messages", diff --git a/web/src/soundpack.ts b/web/src/soundpack.ts new file mode 100644 index 0000000..522718e --- /dev/null +++ b/web/src/soundpack.ts @@ -0,0 +1,71 @@ +import { unzipSync } from "fflate"; +import type { SoundEventId } from "./sounds"; + +const AUDIO_EXTENSIONS = [".wav", ".mp3", ".ogg", ".m4a"]; + +const MIME_BY_EXTENSION: Record = { + ".wav": "audio/wav", + ".mp3": "audio/mpeg", + ".ogg": "audio/ogg", + ".m4a": "audio/mp4", +}; + +/** Real .ts3soundpack files use TeamSpeak's own internal event names, which + * vary between client versions and aren't documented anywhere stable - so + * this matches by keyword against the filename (soundpacks are zips of + * individual audio files, not proprietary-encoded) rather than depending on + * an exact manifest format. Falls back to a "not matched" list the caller + * can surface, instead of silently dropping unrecognized files. */ +const EVENT_KEYWORDS: Record = { + connect: ["connectionaccepted", "connectionestablished", "youhaveconnected", "connect"], + disconnect: ["connectionlost", "connectiondisconnected", "youhavebeendisconnected", "disconnect"], + clientJoin: ["userenteredchannel", "userenteredvisibility", "cliententerview", "userjoined", "enter"], + clientLeave: ["userleftchannel", "userleftvisibility", "clientleftview", "userleave", "userleft", "leave"], + message: ["textmessagereceived", "chatmessage", "newmessage", "message"], + poke: ["gotpoked", "poke"], +}; + +function normalize(filename: string): string { + const base = filename.slice(filename.lastIndexOf("/") + 1); + const dot = base.lastIndexOf("."); + return (dot === -1 ? base : base.slice(0, dot)).toLowerCase().replace(/[^a-z0-9]/g, ""); +} + +function extensionOf(filename: string): string | null { + const lower = filename.toLowerCase(); + return AUDIO_EXTENSIONS.find((ext) => lower.endsWith(ext)) ?? null; +} + +export interface SoundpackImportResult { + matched: Partial>; + unmatchedFiles: string[]; +} + +export async function parseSoundpack(file: File): Promise { + const buffer = new Uint8Array(await file.arrayBuffer()); + const entries = unzipSync(buffer, { + filter: (entry) => extensionOf(entry.name) !== null, + }); + + const matched: SoundpackImportResult["matched"] = {}; + const unmatchedFiles: string[] = []; + + for (const [name, data] of Object.entries(entries)) { + const ext = extensionOf(name); + if (!ext) continue; + const key = normalize(name); + const eventId = (Object.keys(EVENT_KEYWORDS) as SoundEventId[]).find( + (id) => !matched[id] && EVENT_KEYWORDS[id].some((keyword) => key.includes(keyword)) + ); + if (eventId) { + matched[eventId] = { + name: name.slice(name.lastIndexOf("/") + 1), + blob: new Blob([data.slice()], { type: MIME_BY_EXTENSION[ext] }), + }; + } else { + unmatchedFiles.push(name); + } + } + + return { matched, unmatchedFiles }; +}