Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure tests, extract query parsing #394

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 79 additions & 129 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import { Config } from "./config.js";
import { initialise as electron } from "./app/electron.js";
import { AudioHandler } from "./web/audio-handler.js";
import { Econet } from "./econet.js";
import { joinQuery, parseQuery, combineQuery } from "./web/query-string.js";

let processor;
let video;
let dbgr;
let frames = 0;
let frameSkip = 0;
let syncLights;
let discSth;
let tapeSth;
Expand All @@ -36,127 +36,92 @@ let model;
const gamepad = new GamePad();
let availableImages;
let discImage;
const extraRoms = [];
if (typeof starCat === "function") {
availableImages = starCat();

if (availableImages && availableImages[0]) {
discImage = availableImages[0].file;
}
}
let queryString = document.location.search.substring(1) + "&" + window.location.hash.substring(1);
let secondDiscImage = null;
let parsedQuery = {};
let needsAutoboot = false;
let autoType = "";
let keyLayout = window.localStorage.keyLayout || "physical";

const BBC = utils.BBC;
const keyCodes = utils.keyCodes;
const emuKeyHandlers = {};
let cpuMultiplier = 1;
let fastAsPossible = false;
let fastTape = false;
let noSeek = false;
let pauseEmu = false;
let stepEmuWhenPaused = false;
let audioFilterFreq = 7000;
let audioFilterQ = 5;
let stationId = 101;
let econet = null;

if (queryString) {
if (queryString[queryString.length - 1] === "/")
// workaround for shonky python web server
queryString = queryString.substring(0, queryString.length - 1);
queryString.split("&").forEach(function (keyval) {
const keyAndVal = keyval.split("=");
const key = decodeURIComponent(keyAndVal[0]);
let val = null;
if (keyAndVal.length > 1) val = decodeURIComponent(keyAndVal[1]);
parsedQuery[key] = val;

// eg KEY.CAPSLOCK=CTRL
let bbcKey;
if (key.toUpperCase().indexOf("KEY.") === 0) {
bbcKey = val.toUpperCase();

if (BBC[bbcKey]) {
const nativeKey = key.substring(4).toUpperCase(); // remove KEY.
if (keyCodes[nativeKey]) {
console.log("mapping " + nativeKey + " to " + bbcKey);
utils.userKeymap.push({ native: nativeKey, bbc: bbcKey });
} else {
console.log("unknown key: " + nativeKey);
}
const queryString = joinQuery(document.location.search.substring(1), window.location.hash.substring(1));
const queryTypeMap = (() => {
const map = new Map();
map.set("autoboot", "boolIfPresent");
map.set("autochain", "boolIfPresent");
map.set("autorun", "boolIfPresent");
map.set("fastTape", "boolIfPresent");
map.set("noseek", "boolIfPresent");
map.set("audiofilterfreq", "float");
map.set("audiofilterq", "float");
map.set("stationId", "int");
map.set("rom", "array");
map.set("glEnabled", "bool");
map.set("fakeVideo", "boolIfPresent");
map.set("cpuMultiplier", "float");
return map;
})();
let parsedQuery = parseQuery(queryString, queryTypeMap);
for (const key of Object.keys(parsedQuery)) {
const val = parsedQuery[key];
if (key.toUpperCase().startsWith("KEY.")) {
const bbcKey = val.toUpperCase();

if (BBC[bbcKey]) {
const nativeKey = key.substring(4).toUpperCase(); // remove KEY.
if (keyCodes[nativeKey]) {
console.log("mapping " + nativeKey + " to " + bbcKey);
utils.userKeymap.push({ native: nativeKey, bbc: bbcKey });
} else {
console.log("unknown BBC key: " + val);
console.log("unknown key: " + nativeKey);
}
} else if (key.indexOf("GP.") === 0) {
// gamepad mapping
// eg ?GP.FIRE2=RETURN
const gamepadKey = key.substring(3).toUpperCase(); // remove GP. prefix
gamepad.remap(gamepadKey, val.toUpperCase());
} else {
switch (key) {
case "LEFT":
case "RIGHT":
case "UP":
case "DOWN":
case "FIRE":
gamepad.remap(key, val.toUpperCase());
break;
case "autoboot":
needsAutoboot = "boot";
break;
case "autochain":
needsAutoboot = "chain";
break;
case "autorun":
needsAutoboot = "run";
break;
case "autotype":
needsAutoboot = "type";
autoType = val;
break;
case "keyLayout":
keyLayout = (val + "").toLowerCase();
break;
case "disc":
case "disc1":
discImage = val;
break;
case "rom":
extraRoms.push(val);
break;
case "disc2":
secondDiscImage = val;
break;
case "embed":
$(".embed-hide").hide();
$("body").css("background-color", "transparent");
break;
case "fasttape":
fastTape = true;
break;
case "noseek":
noSeek = true;
break;
case "audiofilterfreq":
audioFilterFreq = Number(val);
break;
case "audiofilterq":
audioFilterQ = Number(val);
break;
case "stationId":
stationId = Number(val);
break;
}
console.log("unknown BBC key: " + val);
}
});
} else if (key.startsWith("GP.") === 0) {
// gamepad mapping
// eg ?GP.FIRE2=RETURN
const gamepadKey = key.substring(3).toUpperCase(); // remove GP. prefix
gamepad.remap(gamepadKey, val.toUpperCase());
}
}

if (parsedQuery.frameSkip) frameSkip = parseInt(parsedQuery.frameSkip);
for (const key of ["UP", "DOWN", "LEFT", "RIGHT", "FIRE"]) {
const val = parsedQuery[key];
if (val) gamepad.remap(key, val.toUpperCase());
}

let needsAutoboot = false;
let autoType = parsedQuery.autotype;
if (parsedQuery.autoboot) needsAutoboot = "boot";
else if (parsedQuery.autochain) needsAutoboot = "chain";
else if (parsedQuery.autorun) needsAutoboot = "run";
else if (parsedQuery.autotype) needsAutoboot = "type";
discImage = parsedQuery.disc || parsedQuery.disc1 || discImage;
const extraRoms = parsedQuery.rom;

const secondDiscImage = parsedQuery.disc2;
const keyLayout = parsedQuery.keyLayout || window.localStorage.keyLayout || "physical";

if (parsedQuery.embed) {
$(".embed-hide").hide();
$("body").css("background-color", "transparent");
}
const fastTape = !!parsedQuery.fastTape;
const noSeek = !!parsedQuery.noseek;
const audioFilterFreq = parsedQuery.audiofilterfreq || 7000;
const audioFilterQ = parsedQuery.audiofilterq || 5;
const stationId = parsedQuery.stationId === undefined ? 101 : parsedQuery.stationId;
let frameSkip = parsedQuery.frameSkip || 0;

const emuKeyHandlers = {};
const cpuMultiplier = parsedQuery.cpuMultiplier || 1;
let fastAsPossible = false;
let pauseEmu = false;
let stepEmuWhenPaused = false;
let econet = null;

const printerPort = {
outputStrobe: function (level, output) {
Expand Down Expand Up @@ -210,7 +175,7 @@ const emulationConfig = {
};

const config = new Config(function (changed) {
parsedQuery = _.extend(parsedQuery, changed);
parsedQuery = { ...parsedQuery, ...changed };
if (
changed.model ||
changed.coProcessor !== undefined ||
Expand Down Expand Up @@ -267,17 +232,13 @@ sbBind($(".sidebar.bottom"), parsedQuery.sbBottom, function (div, img) {
div.css({ bottom: -img.height() });
});

if (parsedQuery.cpuMultiplier) {
cpuMultiplier = parseFloat(parsedQuery.cpuMultiplier);
if (cpuMultiplier !== 1) {
console.log("CPU multiplier set to " + cpuMultiplier);
}
const clocksPerSecond = (cpuMultiplier * 2 * 1000 * 1000) | 0;
const MaxCyclesPerFrame = clocksPerSecond / 10;

let tryGl = true;
if (parsedQuery.glEnabled !== undefined) {
tryGl = parsedQuery.glEnabled === "true";
}
const tryGl = parsedQuery.glEnabled === undefined ? true : parsedQuery.glEnabled;
const $screen = $("#screen");
const canvas = tryGl ? canvasLib.bestCanvas($screen[0]) : new canvasLib.Canvas($screen[0]);
video = new Video(model.isMaster, canvas.fb32, function paint(minx, miny, maxx, maxy) {
Expand All @@ -286,7 +247,7 @@ video = new Video(model.isMaster, canvas.fb32, function paint(minx, miny, maxx,
frames = 0;
canvas.paint(minx, miny, maxx, maxy);
});
if (parsedQuery.fakeVideo !== undefined) video = new FakeVideo();
if (parsedQuery.fakeVideo) video = new FakeVideo();

const audioHandler = new AudioHandler($("#audio-warning"), audioFilterFreq, audioFilterQ, noSeek);
// Firefox will report that audio is suspended even when it will
Expand Down Expand Up @@ -706,11 +667,7 @@ discSth = new StairwayToHell(sthStartLoad, makeOnCat(discSthClick), sthOnError,
tapeSth = new StairwayToHell(sthStartLoad, makeOnCat(tapeSthClick), sthOnError, true);

$("#sth .autoboot").click(function () {
if ($("#sth .autoboot").prop("checked")) {
parsedQuery.autoboot = "";
} else {
delete parsedQuery.autoboot;
}
parsedQuery.autoboot = $("#sth .autoboot").prop("checked");
updateUrl();
});

Expand Down Expand Up @@ -836,14 +793,7 @@ function autoRunBasic() {
}

function updateUrl() {
let url = window.location.origin + window.location.pathname;
let sep = "?";
$.each(parsedQuery, function (key, value) {
if (key.length > 0 && value) {
url += sep + encodeURIComponent(key) + "=" + encodeURIComponent(value);
sep = "&";
}
});
const url = `${window.location.origin}${window.location.pathname}?${combineQuery(parsedQuery, queryTypeMap)}`;
window.history.pushState(null, null, url);
}

Expand Down Expand Up @@ -1553,8 +1503,8 @@ function stop(debug) {
const desiredAspectRatio = cubOrigWidth / cubOrigHeight;
const minWidth = cubOrigWidth / 4;
const minHeight = cubOrigHeight / 4;
const borderReservedSize = parsedQuery.embed !== undefined ? 0 : 100;
const bottomReservedSize = parsedQuery.embed !== undefined ? 0 : 68;
const borderReservedSize = parsedQuery.embed ? 0 : 100;
const bottomReservedSize = parsedQuery.embed ? 0 : 68;

function resizeTv() {
let navbarHeight = $("#header-bar").outerHeight();
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
"releaseType": "release"
}
},
"mocha": {
"spec": "tests/unit/**/*.js"
},
"scripts": {
"start": "webpack serve",
"build": "webpack --node-env production",
Expand All @@ -76,8 +79,8 @@
"lint-fix": "eslint . --fix",
"format": "prettier --write .",
"test-long:cpu": "node tests/test-suite.js",
"test:unit": "mocha tests/unit",
"test:integration": "mocha tests/integration",
"test:unit": "mocha --recursive tests/unit",
"test:integration": "mocha --recursive tests/integration",
"test:dormann": "node tests/test-dormann.js",
"test": "npm-run-all -p test:*",
"test-long": "npm-run-all -p test-long:*"
Expand Down
Loading