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
|
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');
|
2021-12-11 09:59:39 +00:00
|
|
|
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');
|
2021-12-18 22:48:34 +00:00
|
|
|
|
2022-03-23 22:49:07 +00:00
|
|
|
const asarUrl = `https://github.com/GooseMod/OpenAsar/releases/download/${oaVersion.split('-')[0]}/app.asar`;
|
2021-12-11 09:59:39 +00:00
|
|
|
|
2022-03-28 07:20:03 +00:00
|
|
|
const getAsarHash = () => createHash('sha512').update(fs.readFileSync(asarPath)).digest('hex');
|
2021-12-11 09:59:39 +00:00
|
|
|
|
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...');
|
|
|
|
|
2022-03-23 22:49:07 +00:00
|
|
|
if (!oaVersion.includes('-')) return log('AsarUpdate', 'Non-standard version');
|
2021-12-13 11:02:22 +00:00
|
|
|
|
2021-12-11 09:59:39 +00:00
|
|
|
const originalHash = getAsarHash();
|
|
|
|
|
2022-04-12 12:38:09 +00:00
|
|
|
await new Promise((res) => {
|
2021-12-18 22:48:34 +00:00
|
|
|
const file = fs.createWriteStream(downloadPath);
|
2021-12-11 19:51:14 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
file.on('finish', () => {
|
|
|
|
file.close();
|
2022-04-12 12:38:09 +00:00
|
|
|
res();
|
2021-12-18 20:20:48 +00:00
|
|
|
});
|
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));
|
2021-12-11 09:59:39 +00:00
|
|
|
});
|
|
|
|
|
2022-04-12 12:38:09 +00:00
|
|
|
if (fs.readFileSync(downloadPath, 'utf8').startsWith('<Error>')) return log('AsarUpdate', 'Download error');
|
2021-12-18 20:20:48 +00:00
|
|
|
|
2022-04-14 22:32:02 +00:00
|
|
|
fs.copyFileSync(downloadPath, asarPath); // Overwrite actual app.asar
|
|
|
|
fs.unlinkSync(downloadPath); // Delete downloaded temp file
|
2021-12-11 09:59:39 +00:00
|
|
|
|
|
|
|
|
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, {
|
2021-12-18 20:20:48 +00:00
|
|
|
message: 'Updated OpenAsar',
|
|
|
|
detail: `Restart required to use new version.`,
|
2022-04-14 22:32:02 +00:00
|
|
|
buttons: ['Restart', 'Later'],
|
2021-12-18 20:20:48 +00:00
|
|
|
defaultId: 0
|
|
|
|
});
|
2021-12-11 13:30:27 +00:00
|
|
|
|
2021-12-18 20:20:48 +00:00
|
|
|
if (response === 0) {
|
2022-02-16 09:03:43 +00:00
|
|
|
app.relaunch();
|
|
|
|
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
|
|
|
};
|