apple2js/js/formats/po.ts
Ian Flanigan 9d4a265daf Refactor storage types
Today, lots of information about how a file or JSON becomes a disk
image is embedded in the metadata for the image and/or disk. This
makes it hard to write back to the source when the in-memory disk
image changes.

This refactoring is an attempt to break out all of the bits of logic
into composable pieces.  While this is mestly concerned with reading
right now, the idea is that it will eventually allow configuring
writing as well.  The main goal is to allow round-tripping to the same
file on disk, but, in theory, it could also save to a different file
or the local database, too.

Note that this is a work in progress.
2022-06-05 23:30:34 +02:00

42 lines
1.4 KiB
TypeScript

import { NibbleDisk, DiskOptions, ENCODING_NIBBLE, TrackSectorSource } from './types';
import { ByteArrayArrayTrackSectorSource, ByteArrayByteSource, ByteTrackSectorSource, ProdosOrderedTrackSectorSource, TrackSector6x2NibbleTrackSource } from './sources';
/**
* Returns a `Disk` object from ProDOS-ordered image data.
* @param options the disk image and options
* @returns A nibblized disk
*/
export default function createDiskFromProDOS(options: DiskOptions) {
const { data, name, side, rawData, volume, readOnly } = options;
const disk: NibbleDisk = {
format: 'nib',
encoding: ENCODING_NIBBLE,
name,
side,
volume: volume || 254,
tracks: [],
readOnly: readOnly || false,
};
let trackSectorSource: TrackSectorSource;
if (rawData) {
trackSectorSource =
new ByteTrackSectorSource(
new ByteArrayByteSource(new Uint8Array(rawData)));
} else if (data) {
trackSectorSource = new ByteArrayArrayTrackSectorSource(data);
} else {
throw new Error('Requires data or rawData');
}
const nibbleTrackSource =
new TrackSector6x2NibbleTrackSource(
new ProdosOrderedTrackSectorSource(trackSectorSource), volume);
for (let physical_track = 0; physical_track < nibbleTrackSource.numTracks(); physical_track++) {
disk.tracks[physical_track] = nibbleTrackSource.read(physical_track);
}
return disk;
}