Stricter disk format typing

This commit is contained in:
Will Scullin 2021-07-03 13:13:53 -07:00
parent 431809e5f2
commit 9e1384dd8c
No known key found for this signature in database
GPG Key ID: 26DCD1042C6638CD
5 changed files with 64 additions and 39 deletions

View File

@ -22,7 +22,7 @@ import type {
import {
FormatWorkerMessage,
FormatWorkerResponse,
DiskFormat,
NibbleFormat,
DISK_PROCESSED,
DRIVE_NUMBERS,
DriveNumber,
@ -174,7 +174,7 @@ export interface Callbacks {
interface BaseDrive {
/** Current disk format. */
format: DiskFormat,
format: NibbleFormat,
/** Current disk volume number. */
volume: byte,
/** Displayed disk name */
@ -220,7 +220,7 @@ function isWozDrive(drive: Drive): drive is WozDrive {
}
interface DriveState {
format: DiskFormat,
format: NibbleFormat,
encoding: typeof ENCODING_BITSTREAM | typeof ENCODING_NIBBLE
volume: byte,
name: string,
@ -826,7 +826,7 @@ export default class DiskII implements Card {
return true;
}
setBinary(drive: DriveNumber, name: string, fmt: DiskFormat, rawData: ArrayBuffer) {
setBinary(drive: DriveNumber, name: string, fmt: NibbleFormat, rawData: ArrayBuffer) {
const readOnly = false;
const volume = 254;
const options = {

View File

@ -1,6 +1,6 @@
import type { memory } from '../types';
import { includes, memory } from '../types';
import { base64_decode } from '../base64';
import type { Disk, DiskFormat, DiskOptions, JSONDisk } from './types';
import { Disk, NibbleFormat, DiskOptions, JSONDisk, NIBBLE_FORMATS } from './types';
import _2MG from './2mg';
import D13 from './d13';
import DOS from './do';
@ -8,7 +8,7 @@ import ProDOS from './po';
import Woz from './woz';
import Nibble from './nib';
export function createDisk(fmt: DiskFormat, options: DiskOptions) {
export function createDisk(fmt: NibbleFormat, options: DiskOptions) {
let disk: Disk | null = null;
switch (fmt) {
@ -41,32 +41,36 @@ export function createDiskFromJsonDisk(disk: JSONDisk): Disk | null {
const readOnly = disk.readOnly;
const name = disk.name;
let trackData: memory[][];
if (disk.encoding == 'base64') {
trackData = [];
for (let t = 0; t < disk.data.length; t++) {
trackData[t] = [];
if (fmt == 'nib') {
trackData[t][0] = base64_decode(disk.data[t] as string);
} else {
for (let s = 0; s < disk.data[t].length; s++) {
trackData[t][s] = base64_decode(disk.data[t][s] as string);
if (includes(NIBBLE_FORMATS, fmt)) {
let trackData: memory[][];
if (disk.encoding == 'base64') {
trackData = [];
for (let t = 0; t < disk.data.length; t++) {
trackData[t] = [];
if (fmt == 'nib') {
trackData[t][0] = base64_decode(disk.data[t] as string);
} else {
for (let s = 0; s < disk.data[t].length; s++) {
trackData[t][s] = base64_decode(disk.data[t][s] as string);
}
}
}
} else {
trackData = disk.data;
}
const volume = disk.volume || 0xfe;
const options = {
volume,
readOnly,
name,
data: trackData
} as DiskOptions;
return createDisk(fmt, options);
} else {
trackData = disk.data;
return null;
}
const volume = disk.volume || 0xfe;
const options = {
volume,
readOnly,
name,
data: trackData
} as DiskOptions;
return createDisk(fmt, options);
}

View File

@ -66,17 +66,26 @@ export interface BlockDisk extends Disk {
* block devices.
*/
export const DISK_FORMATS = [
export const NIBBLE_FORMATS = [
'2mg',
'd13',
'do',
'dsk',
'hdv',
'po',
'nib',
'woz'
] as const;
export const BLOCK_FORMATS = [
'2mg',
'hdv',
'po',
] as const;
export const DISK_FORMATS = [...NIBBLE_FORMATS, ...BLOCK_FORMATS ] as const;
export type NibbleFormat = MemberOf<typeof NIBBLE_FORMATS>;
export type BlockFormat = MemberOf<typeof BLOCK_FORMATS>;
export type DiskFormat = MemberOf<typeof DISK_FORMATS>;
/**
@ -131,7 +140,7 @@ export interface ProcessBinaryMessage {
type: typeof PROCESS_BINARY
payload: {
drive: DriveNumber
fmt: DiskFormat
fmt: NibbleFormat
options: DiskOptions
}
}
@ -180,5 +189,5 @@ export type FormatWorkerResponse =
* Block device common interface
*/
export interface MassStorage {
setBinary(drive: number, name: string, ext: DiskFormat, data: ArrayBuffer): boolean
setBinary(drive: number, name: string, ext: BlockFormat, data: ArrayBuffer): boolean
}

View File

@ -4,7 +4,7 @@ import { base64_json_parse, base64_json_stringify } from '../base64';
import { Audio, SOUND_ENABLED_OPTION } from './audio';
import DriveLights from './drive_lights';
import { byte, includes, word } from '../types';
import { MassStorage } from '../formats/types';
import { BLOCK_FORMATS, MassStorage, NIBBLE_FORMATS } from '../formats/types';
import {
DISK_FORMATS,
DriveNumber,
@ -388,13 +388,19 @@ function doLoadLocalDisk(drive: DriveNumber, file: File) {
if (includes(DISK_FORMATS, ext)) {
if (result.byteLength >= 800 * 1024) {
if (_massStorage.setBinary(drive, name, ext, result)) {
if (
includes(BLOCK_FORMATS, ext) &&
_massStorage.setBinary(drive, name, ext, result)
) {
initGamepad();
} else {
openAlert(`Unable to load ${name}`);
}
} else {
if (_disk2.setBinary(drive, name, ext, result)) {
if (
includes(NIBBLE_FORMATS, ext) &&
_disk2.setBinary(drive, name, ext, result)
) {
initGamepad();
} else {
openAlert(`Unable to load ${name}`);
@ -453,11 +459,17 @@ export function doLoadHTTP(drive: DriveNumber, url?: string) {
const name = decodeURIComponent(fileParts.join('.'));
if (includes(DISK_FORMATS, ext)) {
if (data.byteLength >= 800 * 1024) {
if (_massStorage.setBinary(drive, name, ext, data)) {
if (
includes(BLOCK_FORMATS, ext) &&
_massStorage.setBinary(drive, name, ext, data)
) {
initGamepad();
}
} else {
if (_disk2.setBinary(drive, name, ext, data)) {
if (
includes(NIBBLE_FORMATS, ext) &&
_disk2.setBinary(drive, name, ext, data)
) {
initGamepad();
}
}

View File

@ -7,7 +7,7 @@
"dev": "webpack serve --mode=development",
"index": "bin/index > json/disks/index.js",
"lint": "eslint '**/*.js' '**/*.ts'",
"start": "webpack serve --mode=development",
"start": "webpack serve --mode=development --progress",
"test": "jest"
},
"engines": {