Updated inject script (works on macOS and Linux)

This commit is contained in:
Samuel Elliott 2018-11-24 23:11:10 +00:00
parent 468422084a
commit d98ed6649e
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
1 changed files with 23 additions and 19 deletions

View File

@ -2,27 +2,30 @@ const args = process.argv;
const fs = require('fs');
const path = require('path');
const releaseInput = args[2] ? args[2].toLowerCase() : '';
const release = releaseInput == 'canary' ? 'DiscordCanary' : releaseInput == 'ptb' ? 'DiscordPTB' : 'Discord';
const useBdRelease = args[2] && args[2].toLowerCase() === 'release';
const releaseInput = useBdRelease ? args[3] && args[3].toLowerCase() : args[2] && args[2].toLowerCase();
const release = releaseInput === 'canary' ? 'DiscordCanary' : releaseInput === 'ptb' ? 'DiscordPTB' : 'Discord';
console.log(`Injecting into version ${release}`);
const discordPath = (function() {
if (process.platform === 'win32') {
const basedir = path.join(process.env.LOCALAPPDATA, release);
if (!fs.existsSync(basedir)) throw new Error(`Cannot find directory for ${release}`);
const version = fs.readdirSync(basedir).filter(f => fs.lstatSync(path.join(basedir, f)).isDirectory() && f.split('.').length > 1).sort().reverse()[0];
const dir = path.join(basedir, version, 'resources');
if (!version || !fs.existsSync(dir)) throw new Error(`Cannot find directory for ${release}`);
return dir;
return path.join(basedir, version, 'resources');
} else if (process.platform === 'darwin') {
const appPath = releaseInput === 'canary' ? path.join('/Applications', 'Discord Canary.app')
: releaseInput === 'ptb' ? path.join('/Applications', 'Discord PTB.app')
: useBdRelease && args[3] ? args[3] ? args[2] : args[2]
: path.join('/Applications', 'Discord.app');
return path.join(appPath, 'Contents', 'Resources');
} else if (process.platform === 'linux') {
return path.join('/usr', 'share', release.toLowerCase(), 'resources');
}
else if (process.platform == 'darwin') {
const dir = path.join('Applications', `${release}.app`, 'Contents', 'Resources');
if (!fs.existsSync(dir)) throw new Error(`Cannot find directory for ${release}`);
return dir;
}
const dir = path.join('usr', 'share', release.toLowerCase(), 'resources');
if (!fs.existsSync(dir)) throw new Error(`Cannot find directory for ${release}`);
return dir;
})();
if (!fs.existsSync(discordPath)) throw new Error(`Cannot find directory for ${release}`);
console.log(`Found ${release} in ${discordPath}`);
const appPath = path.join(discordPath, 'app');
@ -33,13 +36,14 @@ if (!fs.existsSync(appPath)) fs.mkdirSync(appPath);
if (fs.existsSync(packageJson)) fs.unlinkSync(packageJson);
if (fs.existsSync(indexJs)) fs.unlinkSync(indexJs);
const bdPath = useBdRelease ? path.resolve(__dirname, '..', 'release') : path.resolve(__dirname, '..');
console.log(`Writing package.json`);
fs.writeFileSync(packageJson, JSON.stringify({
name: 'betterdiscord',
description: 'BetterDiscord',
main: 'index.js',
private: true,
dependencies: {}
private: true
}, null, 4));
console.log(`Writing index.js`);
@ -48,9 +52,9 @@ const fs = require('fs');
const Module = require('module');
const electron = require('electron');
const basePath = path.join(__dirname, '..', 'app.asar');
const pkg = require(path.join(basePath, 'package.json'));
electron.app.getAppPath = () => basePath;
Module._load(path.join(basePath, pkg.main), null, true);
electron.app.on('ready', () => {new (require('${path.join(__dirname, '..', 'core').replace(/\\/g, '/')}').BetterDiscord)();});`);
Module._load(basePath, null, true);
electron.app.on('ready', () => new (require('${bdPath.replace(/\\/g, '\\\\').replace(/'/g, '\\\'')}').BetterDiscord)());
`);
console.log(`Injection successful, please restart ${release}.`);
console.log(`Injection successful, please restart ${release}.`);