scratch-sb1-converter/src/sb1-file-packets.js
Michael "Z" Goddard 9231957791
feat: export AssertionError and ValidationError
Add ValidationError to help distinguish between unrecoverable errors
while decoding an `.sb` file and encoding to Scratch 2 data.

Export AssertionError and ValidationError so users of the converter can
know when an error is from validating the Scratch 1 file (as in it
likely is not an Scratch 1 file) or from making sure the Scratch 1 file
data matches expected ranges (like not making memory buffers that are
too big).
2018-12-19 19:50:15 -05:00

62 lines
1.4 KiB
JavaScript

import {assert} from './util/assert';
import {Packet} from './coders/byte-packets';
import {FixedAsciiString, Uint8, Uint32BE} from './coders/byte-primitives';
/**
* @augments Packet
*/
class SB1Signature extends Packet.extend({
/**
* 10 byte ascii string equaling `'ScratchV01'` or `'ScratchV02'`.
* @type {string}
* @memberof SB1Signature#
*/
version: new FixedAsciiString(10),
/**
* Number of bytes in the info block.
* @type {number}
* @memberof SB1Signature#
*/
infoByteLength: Uint32BE
}) {
/**
* Is this a valid SB1Signature?
* @method
* @throws {AssertionError} Throws if it is not valid.
*/
validate () {
assert.validate(
this.equals({version: 'ScratchV01'}) ||
this.equals({version: 'ScratchV02'}),
'Invalid Scratch file signature.'
);
}
}
Packet.initConstructor(SB1Signature);
class SB1Header extends Packet.extend({
ObjS: new FixedAsciiString(4),
ObjSValue: Uint8,
Stch: new FixedAsciiString(4),
StchValue: Uint8,
numObjects: Uint32BE
}) {
validate () {
assert.validate(
this.equals({
ObjS: 'ObjS',
ObjSValue: 1,
Stch: 'Stch',
StchValue: 1
}),
'Invalid Scratch file info packet header.'
);
}
}
Packet.initConstructor(SB1Header);
export {SB1Signature, SB1Header};