2021-12-11 09:59:39 +00:00
|
|
|
const request = require('request');
|
|
|
|
const fs = require('original-fs'); // Use original-fs, not Electron's modified fs
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const electron = require('electron');
|
|
|
|
const { join } = require('path');
|
|
|
|
|
|
|
|
const asarPath = join(require.main.filename, '..');
|
|
|
|
log('AsarUpdate', 'Asar Path:', asarPath);
|
|
|
|
|
|
|
|
const downloadUrls = {
|
2021-12-11 11:33:15 +00:00
|
|
|
nightly: 'https://github.com/GooseMod/OpenAsar/releases/download/nightly/app.asar'
|
2021-12-11 09:59:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const channel = 'nightly'; // Have prod, etc. once stable / 1.0
|
|
|
|
|
|
|
|
const getAsarHash = () => crypto.createHash('sha512').update(fs.readFileSync(asarPath)).digest('hex');
|
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
module.exports = async () => { // (Try) update asar
|
2021-12-11 09:59:39 +00:00
|
|
|
log('AsarUpdate', 'Updating...');
|
|
|
|
|
2021-12-13 11:02:22 +00:00
|
|
|
if (!oaVersion.startsWith('nightly-')) {
|
|
|
|
return log('AsarUpdate', 'Found non-standard version, not updating');
|
|
|
|
}
|
|
|
|
|
2021-12-11 09:59:39 +00:00
|
|
|
const asarUrl = downloadUrls[channel];
|
|
|
|
log('AsarUpdate', 'Release Channel:', channel, 'Download URL:', asarUrl);
|
|
|
|
|
|
|
|
const originalHash = getAsarHash();
|
|
|
|
log('AsarUpdate', 'Original Hash:', originalHash);
|
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
const updateSuccess = await new Promise((res) => {
|
|
|
|
const file = fs.createWriteStream(asarPath);
|
2021-12-11 19:51:14 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
let writeError = false;
|
|
|
|
file.on('error', err => {
|
|
|
|
log('AsarUpdate', 'Failed to write', err);
|
|
|
|
file.close();
|
2021-12-11 19:51:14 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
writeError = true;
|
|
|
|
res(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
log('AsarUpdate', 'Opened write stream to asar');
|
|
|
|
|
|
|
|
request(asarUrl, (_err, res) => {
|
|
|
|
if (writeError) return;
|
2021-12-11 19:51:14 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
log('AsarUpdate', 'Piping download response to stream');
|
|
|
|
res.pipe(file);
|
|
|
|
});
|
2021-12-11 09:59:39 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
file.on('finish', () => {
|
|
|
|
file.close();
|
|
|
|
res(true);
|
|
|
|
});
|
2021-12-11 09:59:39 +00:00
|
|
|
});
|
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
if (!updateSuccess) {
|
|
|
|
log('AsarUpdate', 'Aborting rest of update due to update error');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log('AsarUpdate', 'Completed download');
|
2021-12-11 09:59:39 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
const newHash = getAsarHash();
|
|
|
|
const changed = originalHash !== newHash;
|
2021-12-11 09:59:39 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
log('AsarUpdate', `Hash Comparison:
|
2021-12-11 09:59:39 +00:00
|
|
|
Original Hash: ${originalHash}
|
|
|
|
New Hash: ${newHash}
|
|
|
|
Changed: ${changed}`);
|
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
if (changed && oaConfig.updatePrompt === true) {
|
|
|
|
const { response } = await electron.dialog.showMessageBox(null, {
|
|
|
|
message: 'Updated OpenAsar',
|
|
|
|
detail: `Restart required to use new version.`,
|
|
|
|
buttons: ['Restart Now', 'Later'],
|
|
|
|
defaultId: 0
|
|
|
|
});
|
2021-12-11 13:30:27 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
log('AsarUpdate', 'Modal response', response);
|
2021-12-11 13:30:27 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
if (response === 0) {
|
|
|
|
log('AsarUpdate', 'Restarting');
|
2021-12-11 13:30:27 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
electron.app.relaunch();
|
|
|
|
electron.app.exit();
|
2021-12-11 09:59:39 +00:00
|
|
|
}
|
2021-12-18 20:20:48 +00:00
|
|
|
}
|
2021-12-11 09:59:39 +00:00
|
|
|
};
|