( input: ArrayBuffer | Uint8Array | string, )
| 538 | } |
| 539 | |
| 540 | export function tryPcSogs( |
| 541 | input: ArrayBuffer | Uint8Array | string, |
| 542 | ): PcSogsJson | PcSogsV2Json | undefined { |
| 543 | // Try to parse input as SOGS JSON and see if it's valid |
| 544 | try { |
| 545 | let text: string; |
| 546 | if (typeof input === "string") { |
| 547 | text = input; |
| 548 | } else { |
| 549 | const fileBytes = |
| 550 | input instanceof ArrayBuffer ? new Uint8Array(input) : input; |
| 551 | if (fileBytes.length > 65536) { |
| 552 | // Should be only a few KB, definitely not a SOGS JSON file |
| 553 | return undefined; |
| 554 | } |
| 555 | text = new TextDecoder().decode(fileBytes); |
| 556 | } |
| 557 | |
| 558 | const json = JSON.parse(text); |
| 559 | if (!json || typeof json !== "object" || Array.isArray(json)) { |
| 560 | return undefined; |
| 561 | } |
| 562 | const isVersion2 = json.version === 2; |
| 563 | |
| 564 | for (const key of ["means", "scales", "quats", "sh0"]) { |
| 565 | if ( |
| 566 | !json[key] || |
| 567 | typeof json[key] !== "object" || |
| 568 | Array.isArray(json[key]) |
| 569 | ) { |
| 570 | return undefined; |
| 571 | } |
| 572 | if (isVersion2) { |
| 573 | // Expect files |
| 574 | if (!json[key].files) { |
| 575 | return undefined; |
| 576 | } |
| 577 | |
| 578 | // Scales and sh0 should have codebooks |
| 579 | if ((key === "scales" || key === "sh0") && !json[key].codebook) { |
| 580 | return undefined; |
| 581 | } |
| 582 | // Means should have mins and maxs defined |
| 583 | if (key === "means" && (!json[key].mins || !json[key].maxs)) { |
| 584 | return undefined; |
| 585 | } |
| 586 | } else { |
| 587 | // Expect shape and files |
| 588 | if (!json[key].shape || !json[key].files) { |
| 589 | return undefined; |
| 590 | } |
| 591 | // Besides 'quats' all other properties have mins and maxs |
| 592 | if (key !== "quats" && (!json[key].mins || !json[key].maxs)) { |
| 593 | return undefined; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | // This is probably a PC SOGS file |
no test coverage detected