Sync with stable@0.0.307, I guess ?

This commit is contained in:
Jean Ouina 2020-08-06 23:34:25 +02:00
parent 85e4a7936a
commit dd860e82ba
4 changed files with 361 additions and 446 deletions

View File

@ -1,56 +1,56 @@
import * as fs from "fs" import * as fs from "fs"
import * as path from "path" import * as path from "path"
import rimraf from "rimraf" import rimraf from "rimraf"
import { app } from "electron" import { app } from "electron"
// Determines environment-specific paths based on info provided // Determines environment-specific paths based on info provided
import originalFs from 'fs'; import originalFs from 'fs';
let userDataPath = null; let userDataPath = null;
let resourcesPath = null; let resourcesPath = null;
function determineAppUserDataRoot() { function determineAppUserDataRoot() {
return app.getPath('appData') return app.getPath('appData')
} }
function determineUserData(userDataRoot, buildInfo) { function determineUserData(userDataRoot, buildInfo) {
return path.join(userDataRoot, 'Lightcord'); return path.join(userDataRoot, 'Lightcord');
} }
// cleans old version data in the background // cleans old version data in the background
export function cleanOldVersions(buildInfo) { export function cleanOldVersions(buildInfo) {
const entries = fs.readdirSync(userDataPath) || []; const entries = fs.readdirSync(userDataPath) || [];
entries.forEach(entry => { entries.forEach(entry => {
const fullPath = path.join(userDataPath, entry); const fullPath = path.join(userDataPath, entry);
if (fs.statSync(fullPath).isDirectory() && entry.indexOf(buildInfo.version) === -1) { if (fs.lstatSync(fullPath).isDirectory() && entry.indexOf(buildInfo.version) === -1) {
if (entry.match('^[0-9]+.[0-9]+.[0-9]+') != null) { if (entry.match('^[0-9]+.[0-9]+.[0-9]+') != null) {
console.log('Removing old directory ', entry); console.log('Removing old directory ', entry);
rimraf(fullPath, originalFs, error => { rimraf(fullPath, originalFs, error => {
if (error) { if (error) {
console.warn('...failed with error: ', error); console.warn('...failed with error: ', error);
} }
}); });
} }
} }
}); });
} }
export function init(buildInfo) { export function init(buildInfo) {
resourcesPath = path.join(require.main.filename, '..', '..', '..'); resourcesPath = path.join(require.main.filename, '..', '..', '..');
const userDataRoot = determineAppUserDataRoot(); const userDataRoot = determineAppUserDataRoot();
userDataPath = determineUserData(userDataRoot, buildInfo); userDataPath = determineUserData(userDataRoot, buildInfo);
const { app } = require('electron'); const { app } = require('electron');
app.setPath('userData', userDataPath); app.setPath('userData', userDataPath);
console.log(userDataPath, buildInfo.version) console.log(userDataPath, buildInfo.version)
} }
export function getUserData() { export function getUserData() {
return userDataPath; return userDataPath;
} }
export function getResources() { export function getResources() {
return resourcesPath; return resourcesPath;
} }

View File

@ -1,119 +1,141 @@
/** settings */ /** settings */
import appSettings from "./appSettings" import appSettings from "./appSettings";
let settings = appSettings.getSettings() let settings = appSettings.getSettings();
/** Glasstron */ /** Glasstron */
if(settings.get("GLASSTRON", true)){ if (settings.get("GLASSTRON", true)) {
const glasstron = require("glasstron") const glasstron = require("glasstron");
glasstron.init() glasstron.init();
} }
/** Modules */ /** Modules */
import * as electron from "electron" import * as electron from "electron";
import requireNativeDiscordModule from "./requireNative"; import requireNativeDiscordModule from "./requireNative";
import autoStart from "./autoStart" import autoStart from "./autoStart";
import * as buildInfo from "./buildInfo" import * as buildInfo from "./buildInfo";
import * as Constants from "./Constants" import * as Constants from "./Constants";
import * as GPUSettings from "./GPUSettings" import * as GPUSettings from "./GPUSettings";
import * as moduleUpdater from "./common/moduleUpdater" import * as moduleUpdater from "./common/moduleUpdater";
import * as paths from "./common/paths" import * as paths from "./common/paths";
import { create } from "./singleInstance"; import * as splashScreen from "./splashScreen";
import * as splashScreen from "./splashScreen" import { join } from "path";
import { join } from "path" import { homedir } from "os";
import { homedir } from "os"
if (process.platform === 'linux') { if (process.platform === "linux") {
// Some people are reporting audio problems on Linux that are fixed by setting // Some people are reporting audio problems on Linux that are fixed by setting
// an environment variable PULSE_LATENCY_MSEC=30 -- the "real" fix is to see // an environment variable PULSE_LATENCY_MSEC=30 -- the "real" fix is to see
// what conditions require this and set this then (also to set it directly in // what conditions require this and set this then (also to set it directly in
// our webrtc setup code rather than here) but this should fix the bug for now. // our webrtc setup code rather than here) but this should fix the bug for now.
if (process.env.PULSE_LATENCY_MSEC === undefined) { if (process.env.PULSE_LATENCY_MSEC === undefined) {
process.env.PULSE_LATENCY_MSEC = "30"; process.env.PULSE_LATENCY_MSEC = "30";
} }
} }
paths.init(buildInfo) paths.init(buildInfo);
electron.app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required'); electron.app.commandLine.appendSwitch(
"autoplay-policy",
"no-user-gesture-required"
);
electron.app.commandLine.appendSwitch("no-force-async-hooks-checks"); electron.app.commandLine.appendSwitch("no-force-async-hooks-checks");
function setupHardwareAcceleration() { function setupHardwareAcceleration() {
const settings = appSettings.getSettings(); const settings = appSettings.getSettings();
//@ts-ignore // TODO: this is a copy of gpuSettings.getEnableHardwareAcceleration
const electronMajor = parseInt(process.versions.electron.split('.')[0]); if (!settings.get("enableHardwareAcceleration", true)) {
const allowed = process.env.DISCORD_ENABLE_HARDWARE_ACCELERATION || buildInfo.releaseChannel === 'development' || !(electronMajor === 7 && process.platform === 'darwin'); electron.app.disableHardwareAcceleration();
// TODO: this is a copy of gpuSettings.getEnableHardwareAcceleration }
if (!allowed || !settings.get('enableHardwareAcceleration', true)) {
electron.app.disableHardwareAcceleration();
}
} }
global["releaseChannel"] = "stable" electron.app.allowRendererProcessReuse = false;
global["releaseChannel"] = "stable";
setupHardwareAcceleration(); setupHardwareAcceleration();
function hasArgvFlag(flag) { function hasArgvFlag(flag) {
return (process.argv || []).slice(1).includes(flag); return (process.argv || []).slice(1).includes(flag);
} }
//Transform main thread into async //Transform main thread into async
(async function Main(){ (async function Main() {
await electron.app.whenReady() await electron.app.whenReady();
if(process.argv.includes("--should-create-shortcut")){
console.log(`Creating shortcuts.`)
if(process.platform === "win32"){
electron.shell.writeShortcutLink(join(homedir(), "Desktop", "Lightcord.lnk"), "create", {
"appUserModelId": Constants.APP_ID,
description: Constants.packageJSON.description,
target: process.execPath
})
electron.shell.writeShortcutLink(join(electron.app.getPath("appData"), "Microsoft", "Windows", "Start Menu", "Programs", "Lightcord.lnk"), "create", {
"appUserModelId": Constants.APP_ID,
description: Constants.packageJSON.description,
target: process.execPath
})
}
}
console.log(`Initializing Lightcord.`) if (process.argv.includes("--should-create-shortcut")) {
console.log(`Version: ${buildInfo.version} console.log(`Creating shortcuts.`);
if (process.platform === "win32") {
electron.shell.writeShortcutLink(
join(homedir(), "Desktop", "Lightcord.lnk"),
"create",
{
appUserModelId: Constants.APP_ID,
description: Constants.packageJSON.description,
target: process.execPath,
}
);
electron.shell.writeShortcutLink(
join(
electron.app.getPath("appData"),
"Microsoft",
"Windows",
"Start Menu",
"Programs",
"Lightcord.lnk"
),
"create",
{
appUserModelId: Constants.APP_ID,
description: Constants.packageJSON.description,
target: process.execPath,
}
);
}
}
console.log(`Initializing Lightcord.`);
console.log(`Version: ${buildInfo.version}
releaseChannel: ${buildInfo.releaseChannel} releaseChannel: ${buildInfo.releaseChannel}
commit: ${buildInfo.commit}`) commit: ${buildInfo.commit}`);
if(!electron.app.commandLine.hasSwitch('enable-transparent-visuals'))electron.app.commandLine.appendSwitch('enable-transparent-visuals'); if (!electron.app.commandLine.hasSwitch("enable-transparent-visuals"))electron.app.commandLine.appendSwitch("enable-transparent-visuals");
electron.app.setAppUserModelId(Constants.APP_ID); electron.app.setAppUserModelId(Constants.APP_ID);
let coreModule
create(() => {
const startMinimized = hasArgvFlag('--start-minimized');
coreModule = requireNativeDiscordModule('discord_desktop_core');
coreModule.startup({
paths,
splashScreen,
moduleUpdater,
autoStart,
buildInfo,
appSettings,
Constants,
GPUSettings
});
coreModule.setMainWindowVisible(!startMinimized)
}, (args) => {
if(args && args.length > 0){
if(args.length > 0 && args[0] === '--squirrel-uninstall') {
electron.app.quit();
return;
}
if(args && args.length === 1 && args[0] === "--overlay-host"){ // this is a patch for Lightcord that focus itself
//console.warn("OVERLAY HOST DÉTECTÉ. EVENNEMENT IGNORÉ MAIS POURRAIT CAUSER UN PROBLÈME.")
return
}
}
if (coreModule) {
coreModule.handleSingleInstance(args); let coreModule;
}
}) const isFirstInstance = electron.app.requestSingleInstanceLock();
})() const allowMultipleInstances = hasArgvFlag("--multi-instance");
if (!isFirstInstance && !allowMultipleInstances) {
electron.app.quit();
return
}
if (!allowMultipleInstances) {
electron.app.on("second-instance", (_event, args, _workingDirectory) => {
if(args && args[0] === "--overlay-host"){
// this is a patch for Lightcord that focus itself when the user is playing a game.
//console.warn("OVERLAY HOST DÉTECTÉ. EVENNEMENT IGNORÉ MAIS POURRAIT CAUSER UN PROBLÈME.")
return;
}
if (coreModule) {
coreModule.handleSingleInstance(args);
}
});
}
const startMinimized = hasArgvFlag("--start-minimized");
coreModule = requireNativeDiscordModule("discord_desktop_core");
coreModule.startup({
paths,
splashScreen,
moduleUpdater,
autoStart,
buildInfo,
appSettings,
Constants,
GPUSettings,
});
coreModule.setMainWindowVisible(!startMinimized);
})();

View File

@ -1,106 +0,0 @@
import * as electron from "electron"
import * as net from "net"
import * as path from "path"
import * as fs from "fs"
import * as os from "os"
import * as buildinfo from "./buildInfo"
function deleteSocketFile(socketPath) {
if (process.platform === 'win32') {
return;
}
if (fs.existsSync(socketPath)) {
try {
fs.unlinkSync(socketPath);
} catch (error) {
// Ignore ENOENT errors in case the file was deleted between the exists
// check and the call to unlink sync. This occurred occasionally on CI
// which is why this check is here.
if (error.code !== 'ENOENT') {
throw error;
}
}
}
}
/**
* Creates server to listen for additional atom application launches.
*
* You can run the command multiple times, but after the first launch
* the other launches will just pass their information to this server and then
* close immediately.
*/
function listenForArgumentsFromNewProcess(socketPath, callback) {
deleteSocketFile(socketPath);
const server = net.createServer(connection => {
connection.on('data', data => {
const args = JSON.parse(data.toString());
callback(args);
});
});
server.listen(socketPath);
server.on('error', error => console.error('Application server failed', error));
return server;
}
function tryStart(socketPath, callback, otherAppFound) {
// FIXME: Sometimes when socketPath doesn't exist, net.connect would strangely
// take a few seconds to trigger 'error' event, it could be a bug of node
// or atom-shell, before it's fixed we check the existence of socketPath to
// speedup startup.
if (process.platform !== 'win32' && !fs.existsSync(socketPath)) {
callback();
return;
}
const client = net.connect({ path: socketPath }, () => {
client.write(JSON.stringify(process.argv.slice(1)), () => {
client.end();
otherAppFound();
});
});
client.on('error', callback);
}
function makeSocketPath() {
let name = electron.app.name ? electron.app.name : electron.app.getName();
if (buildinfo.releaseChannel !== 'stable') {
name += buildinfo.releaseChannel;
}
if (process.platform === 'win32') {
return '\\\\.\\pipe\\' + name + '-sock';
} else {
return path.join(os.tmpdir(), name + '.sock');
}
}
export function create(startCallback, newProcessCallback) {
const socketPath = makeSocketPath();
tryStart(socketPath, () => {
const server = listenForArgumentsFromNewProcess(socketPath, newProcessCallback);
electron.app.on('will-quit', () => {
server.close();
deleteSocketFile(socketPath);
});
//@ts-ignore
electron.app.on('will-exit', () => {
server.close();
deleteSocketFile(socketPath);
});
startCallback();
}, () => {
console.log('Another instance exists. Quitting.');
electron.app.exit(0);
});
}
export function pipeCommandLineArgs(noOtherAppFoundCallback, otherAppFound) {
tryStart(makeSocketPath(), noOtherAppFoundCallback, otherAppFound);
}

View File

@ -1,192 +1,191 @@
import * as electron from "electron" import * as electron from "electron"
import {EventEmitter} from "events" import {EventEmitter} from "events"
import * as fs from "fs" import * as fs from "fs"
import * as path from "path" import * as path from "path"
import * as url from "url" import * as moduleUpdater from "./common/moduleUpdater"
import * as moduleUpdater from "./common/moduleUpdater" import * as paths from "./common/paths"
import * as paths from "./common/paths" import * as ipcMain from "./ipcMain"
import * as ipcMain from "./ipcMain"
// citron note: atom seems to add about 50px height to the frame on mac but not windows
// citron note: atom seems to add about 50px height to the frame on mac but not windows // TODO: see if we can eliminate fudge by using useContentSize BrowserWindow option
// TODO: see if we can eliminate fudge by using useContentSize BrowserWindow option const LOADING_WINDOW_WIDTH = 300;
const LOADING_WINDOW_WIDTH = 300; const LOADING_WINDOW_HEIGHT = process.platform === 'darwin' ? 300 : 350;
const LOADING_WINDOW_HEIGHT = process.platform == 'darwin' ? 300 : 350;
// TODO: addModulesListener events should use Module's constants
// TODO: addModulesListener events should use Module's constants const UPDATE_CHECK_FINISHED = 'update-check-finished';
const UPDATE_CHECK_FINISHED = 'update-check-finished'; const LAUNCHING = 'launching';
const LAUNCHING = 'launching';
export const APP_SHOULD_LAUNCH = 'APP_SHOULD_LAUNCH';
export const APP_SHOULD_LAUNCH = 'APP_SHOULD_LAUNCH'; export const APP_SHOULD_SHOW = 'APP_SHOULD_SHOW';
export const APP_SHOULD_SHOW = 'APP_SHOULD_SHOW';
export const events = new EventEmitter();
export const events = new EventEmitter();
function webContentsSend(win, event, ...args) {
function webContentsSend(win, event, ...args) { if(event === "SPLASH_UPDATE_STATE")lastStatus = args[0].status
if(event === "SPLASH_UPDATE_STATE")lastStatus = args[0].status if (win != null && win.webContents != null) {
if (win != null && win.webContents != null) { win.webContents.send(`DISCORD_${event}`, ...args);
win.webContents.send(`DISCORD_${event}`, ...args); }
} }
}
let splashWindow:electron.BrowserWindow;
let splashWindow:electron.BrowserWindow; let modulesListeners;
let modulesListeners; let splashState;
let splashState; let launchedMainWindow;
let launchedMainWindow; let quoteCachePath;
let quoteCachePath; let lastStatus
let lastStatus
export function setSplashState(state:any){
export function setSplashState(state:any){ splashState = state
splashState = state if (splashWindow != null && !splashWindow.isDestroyed() && !splashWindow.webContents.isDestroyed()) {
if (splashWindow != null && !splashWindow.isDestroyed() && !splashWindow.webContents.isDestroyed()) { webContentsSend(splashWindow, 'SPLASH_UPDATE_STATE', Object.assign({ status: lastStatus }, splashState));
webContentsSend(splashWindow, 'SPLASH_UPDATE_STATE', Object.assign({ status: lastStatus }, splashState)); }
} }
}
export function launchMainWindow(){
export function launchMainWindow(){ launchMainWindowInternal();
launchMainWindowInternal(); updateSplashState(LAUNCHING);
updateSplashState(LAUNCHING); }
}
export function initSplash(startMinimized = false) {
export function initSplash(startMinimized = false) { modulesListeners = {};
modulesListeners = {}; splashState = {};
splashState = {}; launchedMainWindow = false;
launchedMainWindow = false; /*
/* addModulesListener(UPDATE_CHECK_FINISHED, ({ succeeded, updateCount, manualRequired }) => {
addModulesListener(UPDATE_CHECK_FINISHED, ({ succeeded, updateCount, manualRequired }) => { launchMainWindow();
launchMainWindow(); updateSplashState(LAUNCHING);
updateSplashState(LAUNCHING); });*/
});*/
launchSplashWindow(startMinimized);
launchSplashWindow(startMinimized);
quoteCachePath = path.join(paths.getUserData(), 'quotes.json');
quoteCachePath = path.join(paths.getUserData(), 'quotes.json'); ipcMain.default.on('UPDATED_QUOTES', (_event, quotes) => cacheLatestQuotes(quotes));
ipcMain.default.on('UPDATED_QUOTES', (_event, quotes) => cacheLatestQuotes(quotes)); }
}
function destroySplash() {
function destroySplash() { if (splashWindow) {
if (splashWindow) { splashWindow.setSkipTaskbar(true);
splashWindow.setSkipTaskbar(true); // defer the window hiding for a short moment so it gets covered by the main window
// defer the window hiding for a short moment so it gets covered by the main window const _nukeWindow = () => {
const _nukeWindow = () => { splashWindow.hide();
splashWindow.hide(); splashWindow.close();
splashWindow.close(); splashWindow = null;
splashWindow = null; };
}; setTimeout(_nukeWindow, 100);
setTimeout(_nukeWindow, 100); }
} }
}
function addModulesListener(event, listener) {
function addModulesListener(event, listener) { modulesListeners[event] = listener;
modulesListeners[event] = listener; moduleUpdater.events.addListener(event, listener);
moduleUpdater.events.addListener(event, listener); }
}
function removeModulesListeners() {
function removeModulesListeners() { for (const event of Object.keys(modulesListeners)) {
for (const event of Object.keys(modulesListeners)) { moduleUpdater.events.removeListener(event, modulesListeners[event]);
moduleUpdater.events.removeListener(event, modulesListeners[event]); }
} }
} export function updateSplashState(event) {
export function updateSplashState(event) { if (splashWindow != null && !splashWindow.isDestroyed() && !splashWindow.webContents.isDestroyed()) {
if (splashWindow != null && !splashWindow.isDestroyed() && !splashWindow.webContents.isDestroyed()) { webContentsSend(splashWindow, 'SPLASH_UPDATE_STATE', Object.assign({ status: event }, splashState));
webContentsSend(splashWindow, 'SPLASH_UPDATE_STATE', Object.assign({ status: event }, splashState)); }
} }
}
function launchSplashWindow(startMinimized = false) {
function launchSplashWindow(startMinimized = false) { const windowConfig = {
const windowConfig = { width: LOADING_WINDOW_WIDTH,
width: LOADING_WINDOW_WIDTH, height: LOADING_WINDOW_HEIGHT,
height: LOADING_WINDOW_HEIGHT, transparent: false,
transparent: false, frame: false,
frame: false, resizable: false,
resizable: false, center: true,
center: true, show: false,
show: false, webPreferences: {
webPreferences: { nodeIntegration: true,
nodeIntegration: true, enableRemoteModule: true
},
}, icon: path.join(__dirname, "..", "discord.png")
icon: path.join(__dirname, "..", "discord.png") };
};
splashWindow = new electron.BrowserWindow(windowConfig);
splashWindow = new electron.BrowserWindow(windowConfig);
splashWindow.webContents.session.protocol.interceptFileProtocol('http', (request, callback) => {
splashWindow.webContents.session.protocol.interceptFileProtocol('http', (request, callback) => { callback(path.join(__dirname, '..', "splash", request.url.replace('http://localhost/', '')));
callback(path.join(__dirname, '..', "splash", request.url.replace('http://localhost/', ''))); });
});
// prevent users from dropping links to navigate in splash window
// prevent users from dropping links to navigate in splash window splashWindow.webContents.on('will-navigate', e => e.preventDefault());
splashWindow.webContents.on('will-navigate', e => e.preventDefault());
splashWindow.webContents.on('new-window', (e, windowURL) => {
splashWindow.webContents.on('new-window', (e, windowURL) => { e.preventDefault();
e.preventDefault(); electron.shell.openExternal(windowURL);
electron.shell.openExternal(windowURL); // exit, but delay half a second because openExternal is about to fire
// exit, but delay half a second because openExternal is about to fire // some events to things that are freed by app.quit.
// some events to things that are freed by app.quit. setTimeout(electron.app.quit, 500);
setTimeout(electron.app.quit, 500); });
});
if (process.platform !== 'darwin') {
if (process.platform !== 'darwin') { // citron note: this causes a crash on quit while the window is open on osx
// citron note: this causes a crash on quit while the window is open on osx splashWindow.on('closed', () => {
splashWindow.on('closed', () => { splashWindow = null;
splashWindow = null; if (!launchedMainWindow) {
if (!launchedMainWindow) { // user has closed this window before we launched the app, so let's quit
// user has closed this window before we launched the app, so let's quit electron.app.quit();
electron.app.quit(); }
} });
}); }
}
ipcMain.default.on('SPLASH_SCREEN_READY', () => {
ipcMain.default.on('SPLASH_SCREEN_READY', () => { const cachedQuote = chooseCachedQuote();
const cachedQuote = chooseCachedQuote(); if (cachedQuote) {
if (cachedQuote) { webContentsSend(splashWindow, 'SPLASH_SCREEN_QUOTE', cachedQuote);
webContentsSend(splashWindow, 'SPLASH_SCREEN_QUOTE', cachedQuote); }
}
if (splashWindow && !startMinimized) {
if (splashWindow && !startMinimized) { splashWindow.show();
splashWindow.show(); }
}
moduleUpdater.installPendingUpdates();
moduleUpdater.installPendingUpdates();
events.emit("SPLASH_SCREEN_READY")
events.emit("SPLASH_SCREEN_READY") });
}); ipcMain.default.on('LAUNCH_ANYWAY', () => {
ipcMain.default.on('LAUNCH_ANYWAY', () => { launchMainWindowInternal()
launchMainWindowInternal() });
});
splashWindow.loadURL("http://localhost/index.html");
splashWindow.loadURL("http://localhost/index.html"); }
}
function launchMainWindowInternal() {
function launchMainWindowInternal() { removeModulesListeners();
removeModulesListeners(); if (!launchedMainWindow && splashWindow != null) {
if (!launchedMainWindow && splashWindow != null) { launchedMainWindow = true;
launchedMainWindow = true; events.emit(APP_SHOULD_LAUNCH);
events.emit(APP_SHOULD_LAUNCH); }
} }
}
function scheduleUpdateCheck() {}
function scheduleUpdateCheck() {}
export function focusWindow() {
export function focusWindow() { if (splashWindow != null) {
if (splashWindow != null) { splashWindow.focus();
splashWindow.focus(); }
} }
}
export function pageReady() {
export function pageReady() { destroySplash();
destroySplash(); process.nextTick(() => events.emit(APP_SHOULD_SHOW));
process.nextTick(() => events.emit(APP_SHOULD_SHOW)); }
}
function cacheLatestQuotes(quotes) {
function cacheLatestQuotes(quotes) { fs.writeFile(quoteCachePath, JSON.stringify(quotes), e => {
fs.writeFile(quoteCachePath, JSON.stringify(quotes), e => { if (e) {
if (e) { console.warn('Failed updating quote cache with error: ', e);
console.warn('Failed updating quote cache with error: ', e); }
} });
}); }
}
function chooseCachedQuote() {
function chooseCachedQuote() { let cachedQuote = "Launching...";
let cachedQuote = "Launching..."; return cachedQuote;
return cachedQuote;
} }