Compatibility with old animation formats (#18804)

This commit is contained in:
昌敦伟
2025-06-23 10:45:43 +08:00
committed by GitHub
parent cebc8ce7d0
commit c19f85b59e
2 changed files with 33 additions and 11 deletions
+14 -6
View File
@@ -69,18 +69,26 @@ const downloadArrayBuffer = (url: string, options: Record<string, any>, onComple
};
const downloadCCONB = (url: string, options: Record<string, any>, onComplete: ((err: Error | null, data?: CCON | null) => void)): void => {
url = url.replace('.cconb', '.bin');
downloader._downloadArrayBuffer(url, options, (err, arrayBuffer: ArrayBuffer): void => {
if (err) {
onComplete(err);
return;
}
const handleArrayBuffer = (arrayBuffer: ArrayBuffer, onComplete: ((err: Error | null, data?: CCON | null) => void)): void => {
try {
const ccon = decodeCCONBinary(new Uint8Array(arrayBuffer));
onComplete(null, ccon);
} catch (err) {
onComplete(err as Error);
}
};
downloader._downloadArrayBuffer(url.replace('.cconb', '.bin'), options, (err, arrayBuffer: ArrayBuffer): void => {
if (err) {
downloader._downloadArrayBuffer(url, options, (err, arrayBuffer: ArrayBuffer): void => {
if (err) {
onComplete(err);
return;
}
handleArrayBuffer(arrayBuffer, onComplete);
});
return;
}
handleArrayBuffer(arrayBuffer, onComplete);
});
};
+19 -5
View File
@@ -26,7 +26,7 @@ import { getError, cclegacy } from '../core';
import { notepackDecode } from '../../external/deserialize/notepack_decode';
import { notepackEncode } from '../../external/deserialize/notepack_encode';
const VERSION = 1;
const VERSION = 2;
const MAGIC = 0x4E4F4343;
@@ -100,9 +100,6 @@ export function decodeCCONBinary (bytes: Uint8Array): CCON {
}
const version = dataView.getUint32(4, true);
if (version !== VERSION) {
throw new InvalidCCONError(getError(13101, version));
}
const dataByteLength = dataView.getUint32(8, true);
if (dataByteLength !== dataView.byteLength) {
@@ -117,7 +114,12 @@ export function decodeCCONBinary (bytes: Uint8Array): CCON {
chunksStart += jsonDataLength;
let json: unknown;
try {
json = notepackDecode(jsonData);
if (version === 1) {
const jsonString = decodeJson(jsonData);
json = JSON.parse(jsonString);
} else if (version === 2) {
json = notepackDecode(jsonData);
}
} catch (err) {
throw new InvalidCCONError(err as string);
}
@@ -157,6 +159,18 @@ interface BufferConstructor {
from(buffer: ArrayBuffer, byteOffset?: number, byteLength?: number): Buffer;
}
function decodeJson (data: Uint8Array): string {
if (typeof TextDecoder !== 'undefined') {
return new TextDecoder().decode(data);
} else if ('Buffer' in globalThis) {
const { Buffer } = (globalThis as unknown as { Buffer: BufferConstructor });
// eslint-disable-next-line no-buffer-constructor
return Buffer.from(data.buffer, data.byteOffset, data.byteLength).toString();
} else {
throw new Error(getError(13104));
}
}
export class InvalidCCONError extends Error { }
export class BufferBuilder {