2017-11-24 00:22:23 +00:00
|
|
|
// Import the required functions & object types from various packages.
|
2017-11-23 13:06:21 +00:00
|
|
|
import { Client } from 'discord-rpc';
|
|
|
|
import { basename, extname } from 'path';
|
2017-11-23 23:53:16 +00:00
|
|
|
import { setInterval, clearInterval } from 'timers';
|
2017-11-24 02:16:08 +00:00
|
|
|
import {
|
2017-11-27 01:14:12 +00:00
|
|
|
commands,
|
|
|
|
debug,
|
|
|
|
Disposable,
|
2017-11-26 21:16:11 +00:00
|
|
|
env,
|
2017-11-24 02:16:08 +00:00
|
|
|
ExtensionContext,
|
2018-01-05 02:17:18 +00:00
|
|
|
StatusBarItem,
|
|
|
|
StatusBarAlignment,
|
2017-11-27 01:14:12 +00:00
|
|
|
window,
|
|
|
|
workspace,
|
|
|
|
WorkspaceFolder
|
2017-11-24 02:16:08 +00:00
|
|
|
} from 'vscode';
|
2018-01-03 18:11:21 +00:00
|
|
|
const lang = require('./data/languages.json');
|
|
|
|
|
|
|
|
const knownExtentions: { [x: string]: {image: string}} = lang.knownExtentions;
|
|
|
|
const knownLanguages: string[] = lang.knownLanguages;
|
2017-11-23 13:06:21 +00:00
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// Define the RPC variable and its type.
|
2017-11-23 21:14:16 +00:00
|
|
|
let rpc: Client;
|
2017-11-24 02:16:08 +00:00
|
|
|
// Define the eventHandler variable and its type.
|
2017-11-26 15:30:32 +00:00
|
|
|
const eventHandlers: Set<Disposable> = new Set();
|
2017-11-24 02:16:08 +00:00
|
|
|
// Define the config variable and its type.
|
|
|
|
let config;
|
2018-01-06 18:08:30 +00:00
|
|
|
// Define the reconnecting var and its type.
|
|
|
|
let reconnecting: boolean;
|
2017-11-24 02:45:23 +00:00
|
|
|
// Define the reconnect counter and its type.
|
|
|
|
let reconnectCounter = 0;
|
2017-11-24 03:42:51 +00:00
|
|
|
// Define the last known file name and its type.
|
2017-11-24 03:31:18 +00:00
|
|
|
let lastKnownFileName: string;
|
2017-11-26 15:30:32 +00:00
|
|
|
// Define the activity object.
|
|
|
|
let activity: object;
|
|
|
|
// Define the activity timer to not spam the API with requests.
|
|
|
|
let activityTimer: NodeJS.Timer;
|
2018-01-05 02:17:18 +00:00
|
|
|
// Define the status bar icon
|
|
|
|
let statusBarIcon: StatusBarItem;
|
2017-11-23 21:14:16 +00:00
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// `Activate` is fired when the extension is enabled. This SHOULD only fire once.
|
2017-11-23 13:06:21 +00:00
|
|
|
export function activate(context: ExtensionContext) {
|
2017-11-24 00:22:23 +00:00
|
|
|
// Get the workspace's configuration for "discord".
|
2017-11-24 02:16:08 +00:00
|
|
|
config = workspace.getConfiguration('discord');
|
2017-11-23 13:06:21 +00:00
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// Obtain whether or not the extension is activated.
|
2017-12-20 05:25:37 +00:00
|
|
|
if (config.get('enabled')) initRPC(config.get('clientID'));
|
2017-11-24 02:16:08 +00:00
|
|
|
|
2017-11-24 03:42:51 +00:00
|
|
|
// Register the `discord.enable` command, and set the `enabled` config option to true.
|
2017-12-20 05:25:37 +00:00
|
|
|
const enabler = commands.registerCommand('discord.enable', async () => {
|
|
|
|
if (rpc) await destroyRPC();
|
2018-02-04 16:13:19 +00:00
|
|
|
await config.update('enabled', true);
|
|
|
|
config = workspace.getConfiguration('discord');
|
2017-11-24 03:42:51 +00:00
|
|
|
initRPC(config.get('clientID'));
|
|
|
|
window.showInformationMessage('Enabled Discord Rich Presence for this workspace.');
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register the `discord.disable` command, and set the `enabled` config option to false.
|
2017-12-20 05:25:37 +00:00
|
|
|
const disabler = commands.registerCommand('discord.disable', async () => {
|
2018-02-04 16:13:19 +00:00
|
|
|
if (!rpc) return window.showWarningMessage('Discord Rich Presence is already disabled in this workspace.');
|
|
|
|
await config.update('enabled', false);
|
|
|
|
config = workspace.getConfiguration('discord');
|
2017-12-20 05:25:37 +00:00
|
|
|
await destroyRPC();
|
2017-11-24 03:42:51 +00:00
|
|
|
window.showInformationMessage('Disabled Discord Rich Presence for this workspace.');
|
2017-11-23 23:53:16 +00:00
|
|
|
});
|
|
|
|
|
2018-01-05 02:17:18 +00:00
|
|
|
// Register the `discord.reconnect` command
|
|
|
|
const reconnecter = commands.registerCommand('discord.reconnect', async () => {
|
|
|
|
if (rpc) try { await destroyRPC(); } catch {}
|
|
|
|
initRPC(config.get('clientID'), true);
|
2018-01-06 18:08:30 +00:00
|
|
|
|
|
|
|
if (!config.get('silent')) window.showInformationMessage('Reconnecting to Discord RPC');
|
2018-01-05 02:17:18 +00:00
|
|
|
|
|
|
|
if (statusBarIcon) statusBarIcon.text = '$(pulse) Reconnecting';
|
|
|
|
});
|
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// Push the new commands into the subscriptions.
|
2018-01-05 02:17:18 +00:00
|
|
|
context.subscriptions.push(enabler, disabler, reconnecter);
|
2017-11-23 13:06:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// `Deactivate` is fired whenever the extension is deactivated.
|
2017-12-20 05:25:37 +00:00
|
|
|
export async function deactivate() {
|
2017-11-24 00:22:23 +00:00
|
|
|
// If there's an RPC Client initalized, destroy it.
|
2017-12-20 05:25:37 +00:00
|
|
|
await destroyRPC();
|
2017-11-24 02:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initalize the RPC systems.
|
2018-01-05 02:17:18 +00:00
|
|
|
function initRPC(clientID: string, loud?: boolean): void {
|
2017-11-24 02:16:08 +00:00
|
|
|
// Update the RPC variable with a new RPC Client.
|
|
|
|
rpc = new Client({ transport: 'ipc' });
|
|
|
|
|
|
|
|
// Once the RPC Client is ready, set the activity.
|
|
|
|
rpc.once('ready', () => {
|
2018-01-06 18:08:30 +00:00
|
|
|
// Announce the reconnection
|
|
|
|
if (loud && !config.get('silent')) window.showInformationMessage('Successfully reconnected to Discord RPC');
|
2018-01-05 02:17:18 +00:00
|
|
|
|
|
|
|
// Remove icon if connected
|
2018-01-06 18:08:30 +00:00
|
|
|
if (statusBarIcon) {
|
|
|
|
statusBarIcon.dispose();
|
|
|
|
statusBarIcon = null;
|
2017-11-26 15:30:32 +00:00
|
|
|
}
|
2018-01-06 18:08:30 +00:00
|
|
|
|
|
|
|
// Stop from reconnecing.
|
|
|
|
reconnecting = false;
|
2017-11-26 15:30:32 +00:00
|
|
|
// This is purely for safety measures.
|
|
|
|
if (activityTimer) {
|
|
|
|
// Clear the activity interval.
|
|
|
|
clearInterval(activityTimer);
|
|
|
|
// Null activity variable.
|
|
|
|
activityTimer = null;
|
2017-11-24 02:16:08 +00:00
|
|
|
}
|
2017-11-24 03:42:51 +00:00
|
|
|
// Reset the reconnect counter to 0 on a successful reconnect.
|
2017-11-24 02:45:23 +00:00
|
|
|
reconnectCounter = 0;
|
2017-11-24 02:16:08 +00:00
|
|
|
setActivity();
|
2017-11-26 15:30:32 +00:00
|
|
|
// Set the activity once on ready
|
|
|
|
setTimeout(() => rpc.setActivity(activity), 500);
|
2017-11-24 02:16:08 +00:00
|
|
|
// Make sure to listen to the close event and dispose and destroy everything accordingly.
|
2017-12-20 05:25:37 +00:00
|
|
|
rpc.transport.once('close', async () => {
|
2017-11-24 03:17:12 +00:00
|
|
|
if (!config.get('enabled')) return;
|
2017-12-20 05:25:37 +00:00
|
|
|
await destroyRPC();
|
2018-01-06 18:08:30 +00:00
|
|
|
|
|
|
|
// Set the client to begin reconnecting
|
|
|
|
reconnecting = true;
|
|
|
|
initRPC(config.get('clientID'));
|
|
|
|
// Create reconnecting button
|
2018-02-04 16:13:19 +00:00
|
|
|
createButton(true);
|
2017-11-24 02:16:08 +00:00
|
|
|
});
|
2017-11-26 15:30:32 +00:00
|
|
|
|
|
|
|
// Update the user's activity to the `activity` variable.
|
2018-01-03 18:11:21 +00:00
|
|
|
activityTimer = setInterval(() => {
|
2018-02-04 16:13:09 +00:00
|
|
|
// Update the config before updating the activity
|
|
|
|
config = workspace.getConfiguration('discord');
|
|
|
|
setActivity(Boolean(config.get('workspaceElapsedTime')));
|
2018-01-03 18:11:21 +00:00
|
|
|
rpc.setActivity(activity);
|
|
|
|
}, 15000);
|
2017-11-24 02:16:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Log in to the RPC Client, and check whether or not it errors.
|
2017-12-20 05:25:37 +00:00
|
|
|
rpc.login(clientID).catch(async error => {
|
2018-01-06 18:08:30 +00:00
|
|
|
// Check if the client is reconnecting
|
|
|
|
if (reconnecting) {
|
|
|
|
// Destroy and dispose of everything after the set reconnect attempts
|
2018-01-05 02:17:18 +00:00
|
|
|
if (reconnectCounter >= config.get('reconnectThreshold')) {
|
2018-01-06 18:08:30 +00:00
|
|
|
// Create reconnect button
|
2018-02-04 16:13:19 +00:00
|
|
|
createButton();
|
2018-01-05 02:17:18 +00:00
|
|
|
await destroyRPC();
|
|
|
|
} else {
|
2018-01-06 18:08:30 +00:00
|
|
|
// Increment the counter
|
|
|
|
reconnectCounter++;
|
|
|
|
// Create reconnecting button
|
2018-02-04 16:13:19 +00:00
|
|
|
createButton(true);
|
2018-01-06 18:08:30 +00:00
|
|
|
// Retry connection
|
|
|
|
initRPC(config.get('clientID'));
|
2018-01-05 02:17:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-24 02:45:23 +00:00
|
|
|
}
|
2018-01-06 18:08:30 +00:00
|
|
|
// Announce failure
|
2017-12-07 18:49:27 +00:00
|
|
|
if (!config.get('silent')) {
|
2017-12-07 18:43:37 +00:00
|
|
|
if (error.message.includes('ENOENT')) window.showErrorMessage('No Discord Client detected!');
|
2018-01-03 18:11:33 +00:00
|
|
|
else window.showErrorMessage(`Couldn't connect to Discord via RPC: ${error.message}`);
|
2018-02-04 16:13:19 +00:00
|
|
|
createButton();
|
2017-12-07 18:43:37 +00:00
|
|
|
}
|
2017-11-24 02:31:37 +00:00
|
|
|
});
|
2017-11-24 02:16:08 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 02:17:18 +00:00
|
|
|
// Create reconnect button
|
2018-02-04 16:13:19 +00:00
|
|
|
function createButton(isReconnecting?: boolean): void {
|
|
|
|
// Check if the button exists already
|
2018-01-05 02:17:18 +00:00
|
|
|
if (!statusBarIcon) {
|
2018-01-06 18:08:30 +00:00
|
|
|
// Create the icon
|
2018-01-05 02:17:18 +00:00
|
|
|
statusBarIcon = window.createStatusBarItem(StatusBarAlignment.Left);
|
2018-01-06 18:08:30 +00:00
|
|
|
// Check if the client is reconnecting
|
|
|
|
if (isReconnecting) {
|
|
|
|
// Show attempts left
|
|
|
|
const attempts = config.get('reconnectThreshold') - reconnectCounter;
|
|
|
|
statusBarIcon.text = `$(issue-reopened) Reconnecting: ${attempts} attempt${attempts === 1 ? '' : 's'} left`;
|
|
|
|
statusBarIcon.command = '';
|
|
|
|
} else {
|
|
|
|
// Show button to reconnect
|
|
|
|
statusBarIcon.text = '$(plug) Reconnect to Discord';
|
|
|
|
statusBarIcon.command = 'discord.reconnect';
|
|
|
|
}
|
|
|
|
// Show the button
|
2018-01-05 02:17:18 +00:00
|
|
|
statusBarIcon.show();
|
2018-02-04 16:13:19 +00:00
|
|
|
} else {
|
2018-01-06 18:08:30 +00:00
|
|
|
// Check if the client is reconnecting
|
|
|
|
if (isReconnecting) {
|
|
|
|
// Show attempts left
|
|
|
|
const attempts = config.get('reconnectThreshold') - reconnectCounter;
|
|
|
|
statusBarIcon.text = `$(issue-reopened) Reconnecting: ${attempts} attempt${attempts === 1 ? '' : 's'} left`;
|
|
|
|
statusBarIcon.command = '';
|
|
|
|
} else {
|
|
|
|
// Show button to reconnect
|
|
|
|
statusBarIcon.text = '$(plug) Reconnect to Discord';
|
|
|
|
statusBarIcon.command = 'discord.reconnect';
|
|
|
|
}
|
2018-01-05 02:17:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 16:13:19 +00:00
|
|
|
// Cleanly destroy the RPC client (if it isn't already) && add icon to reconnect
|
2017-12-20 05:25:37 +00:00
|
|
|
async function destroyRPC(): Promise<void> {
|
2017-11-24 02:16:08 +00:00
|
|
|
// Do not continue if RPC isn't initalized.
|
|
|
|
if (!rpc) return;
|
2018-01-06 18:08:30 +00:00
|
|
|
// Stop reconnecting.
|
|
|
|
reconnecting = false;
|
2017-11-26 15:30:32 +00:00
|
|
|
// Clear the activity interval.
|
|
|
|
if (activityTimer) clearInterval(activityTimer);
|
|
|
|
// Null the activity timer.
|
|
|
|
activityTimer = null;
|
|
|
|
// Dispose of the event handlers.
|
|
|
|
eventHandlers.forEach(event => event.dispose());
|
2017-11-24 02:16:08 +00:00
|
|
|
// If there's an RPC Client initalized, destroy it.
|
2017-12-20 05:25:37 +00:00
|
|
|
await rpc.destroy();
|
2017-11-24 02:16:08 +00:00
|
|
|
// Null the RPC variable.
|
|
|
|
rpc = null;
|
2017-11-24 03:49:32 +00:00
|
|
|
// Null the last known file name
|
|
|
|
lastKnownFileName = null;
|
2017-11-23 21:14:16 +00:00
|
|
|
}
|
2017-11-23 13:06:21 +00:00
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// This function updates the activity (The Client's Rich Presence status).
|
2017-12-20 05:22:11 +00:00
|
|
|
function setActivity(workspaceElapsedTime: boolean = false): void {
|
2017-11-24 00:22:23 +00:00
|
|
|
// Do not continue if RPC isn't initalized.
|
2017-11-23 13:06:21 +00:00
|
|
|
if (!rpc) return;
|
2018-02-04 16:13:09 +00:00
|
|
|
|
2017-11-24 03:31:18 +00:00
|
|
|
if (window.activeTextEditor && window.activeTextEditor.document.fileName === lastKnownFileName) return;
|
2017-11-24 03:49:32 +00:00
|
|
|
lastKnownFileName = window.activeTextEditor ? window.activeTextEditor.document.fileName : null;
|
2017-11-24 20:00:52 +00:00
|
|
|
|
2017-11-27 01:14:12 +00:00
|
|
|
const fileName: string = window.activeTextEditor ? basename(window.activeTextEditor.document.fileName) : null;
|
2018-01-03 18:11:21 +00:00
|
|
|
const largeImageKey: any = window.activeTextEditor
|
|
|
|
? knownExtentions[Object.keys(knownExtentions).find(key => {
|
2017-11-27 01:14:12 +00:00
|
|
|
if (key.startsWith('.') && fileName.endsWith(key)) return true;
|
2017-11-26 11:32:52 +00:00
|
|
|
const match = key.match(/^\/(.*)\/([mgiy]+)$/);
|
|
|
|
if (!match) return false;
|
|
|
|
const regex = new RegExp(match[1], match[2]);
|
2017-11-27 01:14:12 +00:00
|
|
|
return regex.test(fileName);
|
2018-01-03 18:11:21 +00:00
|
|
|
})] || (knownLanguages.includes(window.activeTextEditor.document.languageId) ? window.activeTextEditor.document.languageId : null)
|
2017-11-26 11:32:52 +00:00
|
|
|
: 'vscode-big';
|
2017-11-24 20:00:52 +00:00
|
|
|
|
2017-12-20 05:22:11 +00:00
|
|
|
// Get the previous activity start timestamp (if available) to preserve workspace elapsed time
|
|
|
|
let previousTimestamp = null;
|
|
|
|
if (activity) previousTimestamp = activity['startTimestamp'];
|
2017-11-24 00:22:23 +00:00
|
|
|
// Create a JSON Object with the user's activity information.
|
2017-11-26 15:30:32 +00:00
|
|
|
activity = {
|
2017-11-27 01:14:12 +00:00
|
|
|
details: generateDetails('detailsDebugging', 'detailsEditing', 'detailsIdle'),
|
|
|
|
state: generateDetails('lowerDetailsDebugging', 'lowerDetailsEditing', 'lowerDetailsIdle'),
|
2017-12-24 13:44:20 +00:00
|
|
|
startTimestamp: previousTimestamp && workspaceElapsedTime ? previousTimestamp : new Date().getTime() / 1000,
|
2017-11-26 22:06:01 +00:00
|
|
|
largeImageKey: largeImageKey
|
|
|
|
? largeImageKey.image
|
2018-01-03 18:11:21 +00:00
|
|
|
|| largeImageKey
|
2017-11-26 22:06:01 +00:00
|
|
|
: 'txt',
|
2017-11-23 21:17:11 +00:00
|
|
|
largeImageText: window.activeTextEditor
|
2018-02-04 16:06:42 +00:00
|
|
|
? config.get('largeImage').replace('{lang}', largeImageKey.image || largeImageKey).replace('{LANG}', largeImageKey.image.toUpperCase() || largeImageKey.toUpperCase())
|
2017-12-10 21:09:58 +00:00
|
|
|
|| window.activeTextEditor.document.languageId.padEnd(2, '\u200b')
|
2017-11-24 12:53:47 +00:00
|
|
|
: config.get('largeImageIdle'),
|
2017-11-26 22:06:01 +00:00
|
|
|
smallImageKey: debug.activeDebugSession
|
|
|
|
? 'debug'
|
|
|
|
: env.appName.includes('Insiders')
|
|
|
|
? 'vscode-insiders'
|
|
|
|
: 'vscode',
|
2017-11-26 21:16:11 +00:00
|
|
|
smallImageText: config.get('smallImage').replace('{appname}', env.appName),
|
2017-11-23 13:06:21 +00:00
|
|
|
instance: false
|
|
|
|
};
|
|
|
|
}
|
2017-11-27 01:14:12 +00:00
|
|
|
|
|
|
|
function generateDetails(debugging, editing, idling): string {
|
|
|
|
const fileName: string = window.activeTextEditor ? basename(window.activeTextEditor.document.fileName) : null;
|
|
|
|
const checkState: boolean = window.activeTextEditor
|
|
|
|
? Boolean(workspace.getWorkspaceFolder(window.activeTextEditor.document.uri))
|
|
|
|
: false;
|
|
|
|
const workspaceFolder: WorkspaceFolder = checkState ? workspace.getWorkspaceFolder(window.activeTextEditor.document.uri) : null;
|
|
|
|
|
|
|
|
return window.activeTextEditor
|
|
|
|
? debug.activeDebugSession
|
|
|
|
? config.get(debugging)
|
|
|
|
.replace('{filename}', fileName)
|
|
|
|
.replace('{workspace}', checkState
|
|
|
|
? workspaceFolder.name
|
|
|
|
: config.get('lowerDetailsNotFound'))
|
|
|
|
: config.get(editing)
|
|
|
|
.replace('{filename}', fileName)
|
|
|
|
.replace('{workspace}', checkState
|
|
|
|
? workspaceFolder.name
|
|
|
|
: config.get('lowerDetailsNotFound'))
|
|
|
|
: config.get(idling);
|
|
|
|
}
|