OpenAsar/src/asarUpdate.js

51 lines
1.6 KiB
JavaScript
Raw Normal View History

const request = require('request');
const fs = require('original-fs'); // Use original-fs, not Electron's modified fs
2022-03-28 07:20:03 +00:00
const { createHash } = require('crypto');
2022-02-16 09:03:43 +00:00
const { dialog, app } = require('electron');
const { join } = require('path');
const asarPath = join(require.main.filename, '..');
2022-02-16 09:03:43 +00:00
const downloadPath = join(asarPath, '..', 'app.asar.download');
const asarUrl = `https://github.com/GooseMod/OpenAsar/releases/download/${oaVersion.split('-')[0]}/app.asar`;
2022-03-28 07:20:03 +00:00
const getAsarHash = () => createHash('sha512').update(fs.readFileSync(asarPath)).digest('hex');
module.exports = async () => { // (Try) update asar
log('AsarUpdate', 'Updating...');
if (!oaVersion.includes('-')) return log('AsarUpdate', 'Non-standard version');
const originalHash = getAsarHash();
2022-04-12 12:38:09 +00:00
await new Promise((res) => {
const file = fs.createWriteStream(downloadPath);
file.on('finish', () => {
file.close();
2022-04-12 12:38:09 +00:00
res();
});
2022-04-12 12:38:09 +00:00
2022-04-14 22:32:02 +00:00
request.get(asarUrl).on('response', r => r.pipe(file));
});
2022-04-12 12:38:09 +00:00
if (fs.readFileSync(downloadPath, 'utf8').startsWith('<Error>')) return log('AsarUpdate', 'Download error');
2022-04-14 22:32:02 +00:00
fs.copyFileSync(downloadPath, asarPath); // Overwrite actual app.asar
fs.unlinkSync(downloadPath); // Delete downloaded temp file
2022-04-14 22:32:02 +00:00
if (oaConfig.updatePrompt === true && originalHash !== getAsarHash()) {
2022-02-16 09:03:43 +00:00
const { response } = await dialog.showMessageBox(null, {
message: 'Updated OpenAsar',
detail: `Restart required to use new version.`,
2022-04-14 22:32:02 +00:00
buttons: ['Restart', 'Later'],
defaultId: 0
});
if (response === 0) {
2022-02-16 09:03:43 +00:00
app.relaunch();
app.exit();
}
}
};