mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +08:00
Support importing .ts3soundpack files
Real TS3 soundpacks have no documented, stable manifest format across client versions, so instead of parsing a manifest this unzips the pack (via fflate) and matches each audio file to one of our sound events by keyword against its filename. Unmatched files are reported rather than silently dropped, since match confidence varies by pack.
This commit is contained in:
1 parent
3644ae7c41
commit
249687d98a
6 files changed
+131
-2
No files matched your search
@@ -9,6 +9,7 @@ lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-demo
|
||||
dist-ssr
|
||||
*.local
|
||||
|
||||
|
||||
Generated
+9
-2
@@ -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",
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"fflate": "^0.8.3",
|
||||
"react": "^19.2.7",
|
||||
"react-dom": "^19.2.7"
|
||||
},
|
||||
|
||||
@@ -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<HTMLInputElement | null>(null);
|
||||
const uploadTargetRef = useRef<SoundEventId | null>(null);
|
||||
const soundpackInputRef = useRef<HTMLInputElement | null>(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<HTMLInputElement>) => {
|
||||
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 (
|
||||
<>
|
||||
<h3>{t("sounds.title")}</h3>
|
||||
@@ -1190,6 +1217,17 @@ function SoundsPanel() {
|
||||
/>
|
||||
<span>{Math.round(volume * 100)}%</span>
|
||||
</div>
|
||||
<div className="ts-options-field-row">
|
||||
<button onClick={handleImportSoundpackClick}>{t("sounds.importSoundpack")}</button>
|
||||
</div>
|
||||
{soundpackResult && (
|
||||
<p className="ts-options-subtitle">
|
||||
{t("sounds.importResult", {
|
||||
matched: String(soundpackResult.matchedCount),
|
||||
unmatched: String(soundpackResult.unmatchedFiles.length),
|
||||
})}
|
||||
</p>
|
||||
)}
|
||||
<table className="ts-options-sounds-table">
|
||||
<tbody>
|
||||
{SOUND_EVENTS.map((eventId) => (
|
||||
@@ -1226,6 +1264,13 @@ function SoundsPanel() {
|
||||
style={{ display: "none" }}
|
||||
onChange={(e) => void handleFileSelected(e)}
|
||||
/>
|
||||
<input
|
||||
ref={soundpackInputRef}
|
||||
type="file"
|
||||
accept=".ts3soundpack,.zip"
|
||||
style={{ display: "none" }}
|
||||
onChange={(e) => void handleSoundpackSelected(e)}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -234,6 +234,8 @@ const translations: Record<Lang, Record<string, string>> = {
|
||||
"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<Lang, Record<string, string>> = {
|
||||
"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",
|
||||
|
||||
@@ -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<string, string> = {
|
||||
".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<SoundEventId, string[]> = {
|
||||
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<Record<SoundEventId, { name: string; blob: Blob }>>;
|
||||
unmatchedFiles: string[];
|
||||
}
|
||||
|
||||
export async function parseSoundpack(file: File): Promise<SoundpackImportResult> {
|
||||
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 };
|
||||
}
|
||||
Reference in new issue
Block a user