| 212 | // ── CharX (ZIP) parsing ──────────────────────────────────────────── |
| 213 | |
| 214 | async function parseCharx(buffer: ArrayBuffer): Promise<Record<string, unknown>> { |
| 215 | // Find ZIP signature (handle self-extracting archives) |
| 216 | const bytes = new Uint8Array(buffer); |
| 217 | let zipOffset = -1; |
| 218 | for (let i = 0; i < bytes.length - 4; i++) { |
| 219 | if ( |
| 220 | bytes[i] === 0x50 && |
| 221 | bytes[i + 1] === 0x4b && |
| 222 | bytes[i + 2] === 0x03 && |
| 223 | bytes[i + 3] === 0x04 |
| 224 | ) { |
| 225 | zipOffset = i; |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | if (zipOffset < 0) { |
| 230 | throw new Error('No ZIP data found in file'); |
| 231 | } |
| 232 | |
| 233 | const zip = await JSZip.loadAsync(buffer.slice(zipOffset)); |
| 234 | const cardFile = zip.file('card.json'); |
| 235 | if (!cardFile) { |
| 236 | throw new Error('No card.json found in CharX archive'); |
| 237 | } |
| 238 | |
| 239 | const text = await cardFile.async('text'); |
| 240 | return JSON.parse(text); |
| 241 | } |
| 242 | |
| 243 | // ── Input detection ──────────────────────────────────────────────── |
| 244 | |