From b3d0d3e32bf1078b64be93000840a4fbc5d358ba Mon Sep 17 00:00:00 2001 From: Oj Date: Sat, 11 Dec 2021 09:59:39 +0000 Subject: [PATCH] [AsarUpdate] Initial Add, autoupdate after Discord startup --- README.md | 6 ++++-- src/asarUpdate.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++ src/bootstrap.js | 12 +++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 src/asarUpdate.js diff --git a/README.md b/README.md index f1731e5..62a98c2 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Below is a list in order of priority, marked as complete when finished: - [ ] A bunch of specific minor fixes / features - [X] Handle hardware acceleration - [ ] Add Discord-specific Electron flags? +- [ ] Asar auto-updating - [ ] Multi-instance handling - [ ] Auto start - [ ] First run @@ -39,8 +40,9 @@ Custom patches are another main goal of OpenAsar, patching enhancements where ot ## Config You can configure OpenAsar via `settings.json` (found in your Discord app data / user data), under a `openasar` object. Keep in mind most options are defaults for good reason, they may temporarily brick your client until you revert your changes. The avaliable options are: -- `quickstart` (bool) - enables Quickstart (experimental) -- `skipStartupUpdateChecks` (bool) - skips startup update checking (Linux-only) +- `quickstart` (bool, default false) - whether to use Quickstart (experimental) +- `skipStartupUpdateChecks` (bool, default false) - skips startup update checking (Linux-only) +- `autoupdate` (bool, default true) - whether to autoupdate OpenAsar after Discord startup An example of a settings.json with OpenAsar config: ```json diff --git a/src/asarUpdate.js b/src/asarUpdate.js new file mode 100644 index 0000000..8525648 --- /dev/null +++ b/src/asarUpdate.js @@ -0,0 +1,54 @@ +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 = { + nightly: 'https://github.com/GooseMod/OpenAsar/releases/download/nightly/openasar.asar' +}; + +const channel = 'nightly'; // Have prod, etc. once stable / 1.0 + +const getAsarHash = () => crypto.createHash('sha512').update(fs.readFileSync(asarPath)).digest('hex'); + +module.exports = () => { // (Try) update asar + log('AsarUpdate', 'Updating...'); + + const asarUrl = downloadUrls[channel]; + log('AsarUpdate', 'Release Channel:', channel, 'Download URL:', asarUrl); + + const originalHash = getAsarHash(); + log('AsarUpdate', 'Original Hash:', originalHash); + + const file = fs.createWriteStream(asarPath); + log('AsarUpdate', 'Opened write stream to asar'); + + request(asarUrl, (_err, res) => { + log('AsarUpdate', 'Piping download response to stream'); + res.pipe(file); + }); + + file.on('finish', () => { + file.close(); + log('AsarUpdate', 'Completed download'); + + const newHash = getAsarHash(); + const changed = originalHash !== newHash; + + log('AsarUpdate', `Hash Comparison: +Original Hash: ${originalHash} +New Hash: ${newHash} +Changed: ${changed}`); + + if (changed) { + electron.dialog.showMessageBox(null, { + message: 'Updated OpenAsar', + detail: `New version will be used next restart.` + }); + } + }); +}; \ No newline at end of file diff --git a/src/bootstrap.js b/src/bootstrap.js index 9196194..f945470 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -53,6 +53,18 @@ const startUpdate = () => { }, () => { log('Bootstrap', 'Setting main window visible'); desktopCore.setMainWindowVisible(true); + + setTimeout(() => { // Try to update our asar + if (!oaConfig.autoupdate) return; // If autoupdate disabled, don't update + + const asarUpdate = require('./asarUpdate'); + + try { + asarUpdate(); + } catch (e) { + log('AsarUpdate', 'Failed to update', e); + } + }, 1000); }); };