Fixes to machine save support

This commit is contained in:
Jeff Parsons 2016-02-17 11:05:27 -08:00
commit 585a851ce5
14 changed files with 2476 additions and 2315 deletions

View file

@ -32,7 +32,7 @@
"use strict";
/* global document: true, window: true, XSLTProcessor: false, APPNAME: false, APPVERSION: false, DEBUG: true */
/* global document: true, window: true, XSLTProcessor: false, APPNAME: false, APPVERSION: false, XMLVERSION: true, DEBUG: true */
if (NODE) {
var Component = require("./component");
@ -500,110 +500,12 @@ function embedPC(idMachine, sXMLFile, sXSLFile, sParms)
return embedMachine("PCjs", APPVERSION, idMachine, sXMLFile, sXSLFile, sParms);
}
/**
* savePC(idMachine, sPCJSFile)
*
* @param {string} idMachine
* @param {string} [sPCJSFile]
* @return {boolean} true if successful, false if error
*/
function savePC(idMachine, sPCJSFile)
{
var cmp = Component.getComponentByType("Computer", idMachine);
var dbg = Component.getComponentByType("Debugger", idMachine);
if (cmp) {
if (!sPCJSFile) sPCJSFile = "/versions/pcjs/" + (XMLVERSION || APPVERSION) + "/pc" + (dbg? "-dbg" : "") + ".js";
web.loadResource(sPCJSFile, true, null, null, downloadPC, [idMachine, cmp, dbg]);
return true;
}
web.alertUser("Unable to identify machine '" + idMachine + "'");
return false;
}
/**
* downloadPC(sURL, sPCJS, nErrorCode, aMachineInfo)
*
* @param {string} sURL
* @param {string} sPCJS
* @param {number} nErrorCode
* @param {string} aMachineInfo ([0] = idMachine, [1] = Computer component, [2] = Debugger component, if any)
*/
function downloadPC(sURL, sPCJS, nErrorCode, aMachineInfo)
{
/*
* sPCJS is supposed to contain the entire PCjs script, which has been wrapped with:
*
* (function(){...
*
* at the top and:
*
* ...})();
*
* at the bottom, thanks to the following Closure Compiler option:
*
* --output_wrapper "(function(){%output%})();"
*
* Immediately inside that wrapping, we want to embed all the specified machine's resources, using:
*
* var resources = {"xml": "...", "xsl": "...", ...};
*
* Note that the "resources" variable has been added to our externs.js, to prevent it from being renamed
* by the Closure Compiler.
*/
if (sPCJS) {
var idMachine = aMachineInfo[0];
var matchScript = sPCJS.match(/^(\s*\(function\(\)\{)([\s\S]*)(}\)\(\);\s*)$/);
if (matchScript) {
var resources = Component.getMachineResources(idMachine), resourcesNew = {};
for (var name in resources) {
var data = resources[name];
if (name == "xml") {
/*
* Look through this resource for <disk> entries whose paths do not appear as one of the other
* machine resources, and remove those entries.
*/
var matchDisk, reDisk = /[ \t]*<disk [^>]*path=(['"])(.*?)\1.*?<\/disk>\n?/g;
while (matchDisk = reDisk.exec(resources[name])) {
var path = matchDisk[2];
if (path) {
if (resources[path]) {
console.log("saving disk: '" + path);
} else {
data = data.replace(matchDisk[0], "");
}
}
}
}
console.log("saving resource: '" + name + "' (" + data.length + " bytes)");
resourcesNew[name] = data;
}
var sResources = JSON.stringify(resourcesNew);
sPCJS = matchScript[1] + "var resources=" + sResources + ";" + matchScript[2] + matchScript[3];
console.log("saving machine: '" + idMachine + "' (" + sPCJS.length + " bytes)");
var uri = "data:application/octet-stream;base64," + btoa(sPCJS);
var link = document.createElement('a');
if (typeof link.download == 'string') {
link.href = uri;
link.download = str.getBaseName(sURL, true) + ".json";
document.body.appendChild(link); // Firefox requires the link to be in the body (?)
link.click();
document.body.removeChild(link);
} else {
window.open(uri);
}
}
}
}
/**
* Prevent the Closure Compiler from renaming functions we want to export, by adding them
* as (named) properties of a global object.
*/
if (APPNAME == "PCjs") {
window['embedPC'] = embedPC;
window['savePC'] = savePC;
}
if (APPNAME == "C1Pjs") {