From e532d51afd185c62ed6f03b768850c62ea07dd85 Mon Sep 17 00:00:00 2001 From: nepx <23300488+nepx@users.noreply.github.com> Date: Fri, 22 May 2020 12:56:09 -0700 Subject: [PATCH] Add XHR backend and wrap up disk initialization --- libhalfix.js | 93 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 86 insertions(+), 7 deletions(-) diff --git a/libhalfix.js b/libhalfix.js index 60aaea6..8f74e61 100644 --- a/libhalfix.js +++ b/libhalfix.js @@ -367,15 +367,29 @@ gc(); }; + global["drives"] = []; - window["drive_init"] = function (info_ptr, path, id) { - var p = readstr(path); - if(p.indexOf("!") !== -1){ + global["drive_init"] = function (info_ptr, path, id) { + var p = readstr(path), image; + if (p.indexOf("!") !== -1) { var chunks = p.split("!"); - var image = new image_backends[chunks[0]](_cache[parseInt(chunks[1]) | 0]); - }else{ - var image = 0; - } + image = new image_backends[chunks[0]](_cache[parseInt(chunks[1]) | 0]); + } else + image = new XHRImage(); + requests_in_progress = requests_in_progress + 1 | 0; + image.init(p, function(err, data){ + if(err) throw err; + + var dataptr = alloc(data.length), strptr = alloc(p.length + 1); + memcpy(dataptr, data); + strcpy(strptr, p); + wrap("drive_emscripten_init")(info_ptr, strptr, dataptr, id); + gc(); + + global["drives"][id] = image; + requests_in_progress = requests_in_progress - 1 | 0; + if (requests_in_progress === 0) run_wrapper2(); + }); }; // ======================================================================== @@ -559,6 +573,15 @@ throw new Error("implement me"); }; + /** + * Initialize hard drive image + * @param {string} arg + * @param {function(Uint8Array)} cb + */ + HardDriveImage.prototype.init = function (arg, cb) { + throw new Error("implement me"); + }; + /** * Convert a URL (i.e. os2/blk0000005a.bin) into a number (i.e. 0x5a) * @param {string} str @@ -569,6 +592,19 @@ return parseInt(parts[1], 16) | 0; } + /** + * Create an "info.dat" file + * @param {number} size + * @param {number} blksize + * @returns {Uint8Array} The data that would have been contained in info.dat + */ + function _construct_info(size, blksize) { + var i32 = new Int32Array(2); + i32[0] = size; + i32[1] = blksize; + return new Uint8Array(i32.buffer); + } + /** * ArrayBuffer-backed image * @param {ArrayBuffer} ab @@ -594,6 +630,12 @@ cb(null, data); }, 0); }; + ArrayBufferImage.prototype.init = function (arg, cb) { + var data = _construct_info(this.data.byteLength, 256 << 10); + setTimeout(function () { + cb(null, data); + }, 0); + }; /** * File API-backed image @@ -632,6 +674,30 @@ }; fr.readAsArrayBuffer(this.file); }; + FileImage.prototype.init = function (arg, cb) { + var data = _construct_info(this.file.size, 256 << 10); + setTimeout(function () { + cb(null, data); + }, 0); + }; + + /** + * XHR-backed image + * @constructor + * @extends HardDriveImage + */ + function XHRImage() { + } + XHRImage.prototype = new HardDriveImage(); + XHRImage.prototype.load = function (reqs, cb) { + loadFiles(reqs, cb); + }; + XHRImage.prototype.init = function (arg, cb) { + loadFiles([join_path(arg, "info.dat")], function (err, data) { + if (err) throw err; + cb(null, data[0]); + }); + }; var image_backends = { "file": FileImage, @@ -713,5 +779,18 @@ return Module["_" + nm]; } + /** + * Join two fragments of a path together + * @param {string} a The first part of the path + * @param {string} b The second part of the path + */ + function join_path(a, b) { + if (b.charAt(0) !== "/") + b = "/" + b; + if (a.charAt(a.length - 1 | 0) === "/") + a = a.substring(0, a.length - 1 | 0); + return a + b; //normalize_path(a + b); + } + global["Halfix"] = Halfix; })(typeof window !== "undefined" ? window : this); \ No newline at end of file