move injector stuff over to top-level injector dir

pull/95/head
dperolio 2 years ago
parent df56ba62f9
commit c518b5411c
No known key found for this signature in database
GPG Key ID: 4191689562D51409

@ -1,7 +1,6 @@
{
"editor.tabSize": 2,
"editor.wordWrap": "on",
"todo-tree.tree.showScanModeButton": false,
"todo-tree.regex.regex" : "((//|#|<!--|;|\\*)\\s*($TAGS)|^\\s*- \\[ \\])",
"todo-tree.general.tags": [
"@bug",
@ -86,6 +85,7 @@
"**/node_modules": true,
"**/package-lock.json": true
},
"eslint.format.enable": true,
"eslint.lintTask.enable": true,
"eslint.alwaysShowStatus": true
}

@ -2,9 +2,9 @@ const { existsSync, mkdirSync } = require('fs');
const { BrowserWindow } = require('electron');
const { join } = require('path');
const PluginsDir = join(__dirname, '..', '..', 'addons', 'plugins');
const ThemesDir = join(__dirname, '..', '..', 'addons', 'themes');
const SettingsDir = join(__dirname, '..', '..', 'settings');
const PluginsDir = join(__dirname, '..', 'addons', 'plugins');
const ThemesDir = join(__dirname, '..', 'addons', 'themes');
const SettingsDir = join(__dirname, '..', 'settings');
let experimentalWebPlatform = false;
let transparentWindow = false;
@ -33,7 +33,7 @@ module.exports = class PatchedBrowserWindow extends BrowserWindow {
} else if (opts.webPreferences && (opts.webPreferences.nodeIntegration || opts.webPreferences.preload?.endsWith('splashScreenPreload.js'))) {
// Splash Screen
originalPreload = opts.webPreferences.preload;
opts.webPreferences.preload = join(__dirname, '..', 'preload', 'splash.js');
// opts.webPreferences.preload = join(__dirname, 'preload', 'splash.js');
} else if (opts.webPreferences && opts.webPreferences.offscreen) {
// Overlay
originalPreload = opts.webPreferences.preload;
@ -41,7 +41,7 @@ module.exports = class PatchedBrowserWindow extends BrowserWindow {
} else if (opts.webPreferences && opts.webPreferences.preload?.endsWith('mainScreenPreload.js')) {
// Discord Client
originalPreload = opts.webPreferences.preload;
opts.webPreferences.preload = join(__dirname, '..', 'preload', 'main.js');
opts.webPreferences.preload = join(__dirname, 'preload.js');
// Transparent window enabled
if (transparentWindow) {
opts.transparent = true;

@ -8,7 +8,7 @@ let smoothScrolling = true;
let settings = {};
try {
settings = require(join(__dirname, '..', '..', 'settings', 'settings.json'));
settings = require(join(__dirname, '..', 'settings', 'settings.json'));
({ reactDeveloperTools, smoothScrolling } = settings);
} catch (err) {
// @todo Consider handling this.
@ -18,7 +18,7 @@ if (smoothScrolling === false) {
electron.app.commandLine.appendSwitch('disable-smooth-scrolling');
}
require('../ipc/main');
require('./ipc');
let fakeAppSettings;
Object.defineProperty(global, 'appSettings', {
@ -35,14 +35,9 @@ Object.defineProperty(global, 'appSettings', {
});
const discordPath = join(dirname(require.main.filename), '..', 'app.asar');
const PatchedBrowserWindow = require('./browserWindow');
const PatchedBrowserWindow = require('./browserwindow');
const electronPath = require.resolve('electron');
/**
* Restore the classic path. The updater relies on it and it makes Discord go corrupt.
*/
require.main.filename = join(discordPath, 'app_bootstrap', 'index.js');
const electronExports = new Proxy(electron, {
get (target, prop) {
switch (prop) {
@ -120,11 +115,11 @@ electron.app.once('ready', () => {
const path = url.replace(`${type}${sep}`, '');
if (type === 'assets') {
return callback({ path: join(__dirname, '..', '..', 'assets', path) });
return callback({ path: join(__dirname, '..', 'assets', path) });
} else if (type === 'plugins' || type === 'themes') {
return callback({ path: join(__dirname, '..', '..', 'addons', type, path) });
return callback({ path: join(__dirname, '..', 'addons', type, path) });
} else if (type === 'builtins') {
return callback({ path: join(__dirname, '..', 'core', 'builtins', path) });
return callback({ path: join(__dirname, '..', 'src', 'core', 'builtins', path) });
}
});
});

@ -4,10 +4,10 @@ const { ipcMain, BrowserWindow } = require('electron');
const { renderSync } = require('sass');
/**
*
* @param {*} evt
* @param {*} opts
* @param {*} externalWindow
*
* @param {*} evt
* @param {*} opts
* @param {*} externalWindow
*/
function openDevTools (evt, opts, externalWindow) {
evt.sender.openDevTools(opts);
@ -24,17 +24,17 @@ function openDevTools (evt, opts, externalWindow) {
}
/**
*
* @param {*} evt
*
* @param {*} evt
*/
function closeDevTools (evt) {
evt.sender.closeDevTools();
}
/**
*
* @param {*} evt
* @returns
*
* @param {*} evt
* @returns
*/
function clearCache (evt) {
return new Promise(resolve => {
@ -43,25 +43,25 @@ function clearCache (evt) {
}
/**
*
* @param {*} evt
* @returns
*
* @param {*} evt
* @returns
*/
function getHistory (evt) {
return evt.sender.history;
}
/**
*
* @param {*} _
* @param {*} file
* @returns
*
* @param {*} _
* @param {*} file
* @returns
*/
function compileSass (_, file) {
return new Promise((res, reject) => {
readFile(file, 'utf8').then(rawScss => {
try {
const relativePath = relative(file, join(__dirname, '..', 'core', 'lib', 'sass'));
const relativePath = relative(file, join(__dirname, '..', 'src', 'core', 'lib', 'sass'));
const absolutePath = resolve(join(file, relativePath));
const fixedScss = rawScss.replace('@vizality', absolutePath.split(sep).join(posix.sep));
const result = renderSync({

@ -49,7 +49,7 @@ exposeGlobal('WebSocket', true);
fixDocument();
require('@vizality/compilers');
require('../ipc/renderer');
require('./renderer');
(() => {
const { Module } = require('module');
@ -57,8 +57,8 @@ require('../ipc/renderer');
for (const ext of extensions) {
const oldRequireExt = Module._extensions[ext];
Module._extensions[ext] = (module, filename) => {
const coreDir = join(__dirname, '..', 'core');
const addonsDir = join(__dirname, '..', '..', 'addons');
const coreDir = join(__dirname, '..', 'src', 'core');
const addonsDir = join(__dirname, '..', 'addons');
if ((filename.indexOf(coreDir) &&
filename.indexOf(addonsDir)) ||
filename.indexOf('node_modules') !== -1
@ -80,7 +80,7 @@ const { Events } = require('@vizality/constants');
/**
* Instantiate Vizality.
*/
const Vizality = require('../core/Core').default;
const Vizality = require('../src/core/Core').default;
window.vizality = new Vizality();
/**

@ -1 +0,0 @@
exports.getAppDir = async () => '/Applications/Discord.app/Contents/Resources/app';

@ -1,51 +0,0 @@
const { existsSync, promises } = require('fs');
const { execSync } = require('child_process');
const { join, posix, sep } = require('path');
const rootPath = join(__dirname, '..', '..');
const nodeModulesPath = join(rootPath, 'node_modules');
function installDeps () {
console.log('\x1b[1m\x1b[34mACTION: \x1b[0mInstalling dependencies...');
execSync('npm install --only=prod --legacy-peer-deps', {
cwd: rootPath,
stdio: [ null, null, null ]
});
console.log('\x1b[1m\x1b[34mACTION: \x1b[0mDependencies successfully installed!');
}
module.exports = () => {
// Don't clone in System32
if (__dirname.toLowerCase().split(sep).join(posix.sep).includes('/windows/system32')) {
console.error('\x1b[1m\x1b[31mERROR: \x1b[0mVizality shouldn\'t be cloned in System32, as this will generate conflicts and bloat your Windows installation. Please remove it and clone it in another place.\n' +
'\x1b[1m\x1b[36mNOTE: \x1b[0mNot opening cmd as administrator will be enough.');
process.exit(1);
}
// Verify if we're on node 10.x
if (!promises) {
console.error('\x1b[1m\x1b[31mERROR: \x1b[0mYou\'re on an outdated Node.js version. Vizality requires you to run at least Node 10. You can download it here: https://nodejs.org');
process.exit(1);
}
// Verify if deps have been installed. If not, install them automatically
if (!existsSync(nodeModulesPath)) {
installDeps();
} else {
const { dependencies } = require('../../package.json');
for (const dependency in dependencies) {
const depPath = join(nodeModulesPath, dependency);
if (!existsSync(depPath)) {
installDeps();
break;
}
const depPackage = require(join(depPath, 'package.json'));
const expectedVerInt = parseInt(dependencies[dependency].replace(/[^\d]/g, ''));
const installedVerInt = parseInt(depPackage.version.replace(/[^\d]/g, ''));
if (installedVerInt < expectedVerInt) {
installDeps();
break;
}
}
}
};

@ -1,37 +0,0 @@
// Perform checks
require('./env_check')();
const { promises: { writeFile } } = require('fs');
const { resolve } = require('path');
const main = require('./main.js');
let platformModule;
try {
platformModule = require(`./${process.platform}.js`);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
console.log(`\x1b[1m\x1b[31mERROR: \x1b[0mUnsupported platform '${process.platform}'`);
process.exit(1);
}
}
(async () => {
if (process.argv[2] === 'inject') {
if (await main.inject(platformModule)) {
// To have it show the dashboard on startup for new users
await writeFile(
resolve(__dirname, '..', '__injected.txt'),
'Vizality successfully injected.'
);
console.log('\x1b[32mSUCCESS: \x1b[0mVizality has been injected!');
}
} else if (process.argv[2] === 'uninject') {
if (await main.uninject(platformModule)) {
console.log('\x1b[32mSUCCESS: \x1b[0mVizality has been uninjected.');
}
} else {
console.log(`\x1b[1m\x1b[31mERROR: \x1b[0mUnsupported argument '${process.argv[2]}'`);
process.exit(1);
}
})().catch(e => console.error('\x1b[1m\x1b[31mERROR: \x1b[0mHmm, something seems to have gone wrong...', e));

@ -1,28 +0,0 @@
const { join } = require('path');
const { existsSync } = require('fs');
const { execSync } = require('child_process');
exports.getAppDir = async () => {
const discordProcess = execSync('ps x')
.toString()
.split('\n')
.map(s => s.split(' ').filter(Boolean))
.find(p => p[4] && (/discord$/i).test(p[4]) && p.includes('--type=renderer'));
if (!discordProcess) {
console.log('\x1b[1m\x1b[33mWARNING: \x1b[0mCannot find Discord process, falling back to legacy path detection.');
const paths = [
'/usr/share/discord',
'/usr/lib64/discord',
'/opt/discord',
'/opt/Discord',
'~/.local/bin/Discord/'
];
const discordPath = paths.find(path => existsSync(path));
return join(discordPath, 'resources', 'app');
}
const discordPath = discordProcess[4].split('/');
discordPath.splice(discordPath.length - 1, 1);
return join('/', ...discordPath, 'resources', 'app');
};

@ -1,50 +0,0 @@
const { promises: { mkdir, writeFile, unlink, rmdir, access } } = require('fs');
const { join, sep } = require('path');
const exists = path =>
access(path)
.then(() => true)
.catch(() => false);
exports.inject = async ({ getAppDir }) => {
const appDir = await getAppDir();
if (await exists(appDir)) {
console.log('\x1b[1m\x1b[31mERROR: \x1b[0mLooks like you already have an injector in place. Try uninjecting (`npm run uninject`) and try again.');
return false;
}
await mkdir(appDir);
await Promise.all([
writeFile(
join(appDir, 'index.js'),
`require(\`${__dirname.replace(RegExp(sep.repeat(2), 'g'), '/')}/../patch\`)`
),
writeFile(
join(appDir, 'package.json'),
JSON.stringify({
main: 'index.js',
name: 'discord'
})
)
]);
return true;
};
exports.uninject = async ({ getAppDir }) => {
const appDir = await getAppDir();
if (!(await exists(appDir))) {
console.log('\x1b[1m\x1b[33mWARNING: \x1b[0mThere is nothing to uninject.');
return false;
}
await Promise.all([
unlink(join(appDir, 'package.json')),
unlink(join(appDir, 'index.js'))
]);
await rmdir(appDir);
return true;
};

@ -1,15 +0,0 @@
const { promises: { readdir } } = require('fs');
const { join } = require('path');
exports.getAppDir = async () => {
const discordPath = join(process.env.LOCALAPPDATA, 'Discord');
const discordDirectory = await readdir(discordPath);
console.log('discordPath', discordPath);
const currentBuild = discordDirectory
.filter(path => path.startsWith('app-'))
.reverse()[0];
console.log('final', join(discordPath, currentBuild, 'resources', 'app'));
return join(discordPath, currentBuild, 'resources', 'app');
};

@ -1,50 +0,0 @@
const { ipcRenderer } = require('electron');
const { join } = require('path');
// Discord's preload
const preload = ipcRenderer.sendSync('VIZALITY_GET_PRELOAD');
if (preload) {
require(preload);
}
window.__SPLASH__ = true;
require('module-alias/register');
require('@vizality/compilers');
require('../ipc/renderer');
(() => {
const { Module } = require('module');
const extensions = [ '.jsx', '.js', '.ts', '.tsx' ];
for (const ext of extensions) {
const oldRequireExt = Module._extensions[ext];
Module._extensions[ext] = (module, filename) => {
const coreDir = join(__dirname, '..', 'core');
const addonsDir = join(__dirname, '..', '..', 'addons');
if ((filename.indexOf(coreDir) &&
filename.indexOf(addonsDir)) ||
filename.indexOf('node_modules') !== -1
) {
return oldRequireExt(module, filename);
}
const compiler = new (require(`@vizality/compilers/${ext.substring(1).toUpperCase()}`))(filename);
const compiled = compiler.compile();
module._compile(compiled, filename);
};
}
})();
/*
* Theme injection
*/
const initialize = () => {
document.documentElement.setAttribute('vizality', '');
const ThemeManager = require('../core/managers/Theme').default;
new ThemeManager();
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initialize);
} else {
initialize();
}
Loading…
Cancel
Save