Skip to content

Commit

Permalink
fix: User user data disk image
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrieseberg committed Jul 28, 2020
1 parent 3b23ad8 commit 9e3e962
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ This is Mac OS 8, running in an [Electron](https://electronjs.org/) app pretendi

| | Windows | macOS | Linux |
|---------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Standalone Download | 📦[Standalone, 32-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-1.0.0-win32-standalone-ia32.zip) <br /> 📦[Standalone, 64-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-1.0.0-win32-standalone-x64.zip) | 📦[Standalone](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-macos-1.0.0.zip) | |
| Installer | 💽[Setup, 64-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-1.0.0-setup-win32-x64.exe) <br /> 💽[Setup, 32-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-1.0.0-setup-win32-ia32.exe) | | 💽[deb, 64-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-linux-1.0.0_amd64.deb) <br /> 💽[rpm, 64-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-linux-1.0.0.x86_64.rpm) |
| Standalone Download | 📦[Standalone, 32-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-win32-x64-1.0.0.zip) <br /> 📦[Standalone, 64-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-win32-ia32-1.0.0.zip) | 📦[Standalone](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-darwin-x64-1.0.0.zip) | |
| Installer | 💽[Setup, 64-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintoshjs-1.0.0-setup-x64.exe) <br /> 💽[Setup, 32-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintoshjs-1.0.0-setup-ia32.exe) | | 💽[deb, 64-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js_1.0.0_amd64.deb) <br /> 💽[rpm, 64-bit](https://github.com/felixrieseberg/macintosh.js/releases/download/v1.0.0/macintosh.js-1.0.0-1.x86_64.rpm) |

## Does it work?
Yes! Quite well, actually - on macOS, Windows, and Linux. Bear in mind that this is written entirely in JavaScript, so please adjust your expectations. The virtual machine is emulating a 1991 Macintosh Quadra 900 with a Motorola CPU, which Apple used before switching to IBM's PowerPC architecture in the late 1990s.
Expand Down
46 changes: 45 additions & 1 deletion src/basilisk/BasiliskII-worker-boot.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
const fs = require("fs");
const path = require("path");
const { error } = require("console");

const homeDir = require("os").homedir();
const macDir = path.join(homeDir, "macintosh.js");
const macintoshCopyPath = path.join(__dirname, "user_files");

// Set by config
let userDataPath;

function getUserDataDiskPath() {
return path.join(userDataPath, 'disk');
}

function cleanupCopyPath() {
try {
if (fs.existsSync(macintoshCopyPath)) {
Expand All @@ -17,6 +25,29 @@ function cleanupCopyPath() {
}
}

function getUserDataDiskImage() {
if (!userDataPath) {
console.error(`getUserDataDiskImage: userDataPath not set`);
return;
}

const diskImageUserPath = getUserDataDiskPath();
const diskImagePath = path.join(__dirname, 'disk');

// If there's a disk image, move it over
if (fs.existsSync(diskImageUserPath)) {
// Delete a possible basilisk disk image
if (fs.existsSync(diskImagePath)) {
console.log(`Disk image ${diskImageUserPath} exists, deleting ${diskImagePath}`);
fs.unlinkSync(diskImagePath);
}

fs.renameSync(diskImageUserPath, diskImagePath);
} else {
console.log(`getUserDataDiskImage: No image in user data dir, not doing anything`);
}
}

function addAutoloader(module) {
const copyFilesAtPath = function (sourcePath) {
try {
Expand Down Expand Up @@ -326,7 +357,8 @@ self.onmessage = async function (msg) {

if (msg && msg.data === "disk_save") {
const diskData = Module.FS.readFile("/disk");
const diskPath = path.join(__dirname, "disk");
const diskPath = getUserDataDiskPath();
const basiliskDiskPath = path.join(__dirname, 'disk');

// I wish we could do this with promises, but OOM crashes kill that idea
try {
Expand All @@ -337,6 +369,14 @@ self.onmessage = async function (msg) {
console.error(`Failed to write disk`, error);
}

try {
if (fs.existsSync(basiliskDiskPath) && !(Module && Module.isDevMode)) {
fs.unlinkSync(basiliskDiskPath);
}
} catch (error) {
console.error(`Failed to delete ${basiliskDiskPath}`);
}

// Now, user files
console.log(`Saving user files`);
await saveFilesInPath("/macintosh.js");
Expand All @@ -349,6 +389,10 @@ self.onmessage = async function (msg) {
};

function startEmulator(parentConfig) {
userDataPath = parentConfig.userDataPath;

getUserDataDiskImage();

let screenBufferView = new Uint8Array(
parentConfig.screenBuffer,
0,
Expand Down
4 changes: 4 additions & 0 deletions src/main/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ function registerIpcHandlers() {
ipcMain.handle("getAppVersion", () => {
return app.getVersion();
});

ipcMain.handle("getUserDataPath", () => {
return app.getPath('userData');
});
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/emulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function asyncLoop() {
}

async function start() {
registerWorker();
await registerWorker();
setupDialogs();
openAudio();
asyncLoop();
Expand Down
4 changes: 4 additions & 0 deletions src/renderer/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ module.exports = {
getAppVersion() {
return ipcRenderer.invoke("getAppVersion");
},

getUserDataPath() {
return ipcRenderer.invoke("getUserDataPath");
}
};
6 changes: 4 additions & 2 deletions src/renderer/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
audioBlockChunkSize,
AUDIO_DATA_BUFFER_SIZE,
} = require("./audio");
const { quit, getIsDevMode } = require("./ipc");
const { quit, getIsDevMode, getUserDataPath } = require("./ipc");

let isWorkerRunning = false;
let isWorkerSaving = false;
Expand Down Expand Up @@ -57,7 +57,7 @@ async function handleWorkerShutdown() {
saveDisk();
}

function registerWorker() {
async function registerWorker() {
const workerConfig = {
inputBuffer: inputBuffer,
inputBufferSize: INPUT_BUFFER_SIZE,
Expand All @@ -71,6 +71,8 @@ function registerWorker() {
audioBlockChunkSize: audioBlockChunkSize,
SCREEN_WIDTH: SCREEN_WIDTH,
SCREEN_HEIGHT: SCREEN_HEIGHT,
userDataPath: await getUserDataPath(),
isDevMode: await getIsDevMode()
};

worker = window.emulatorWorker = new Worker(
Expand Down

0 comments on commit 9e3e962

Please sign in to comment.