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,
|
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;
|
|
|
|
// Define the reconnect timer and its type.
|
2017-11-26 15:30:32 +00:00
|
|
|
let reconnectTimer: NodeJS.Timer;
|
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;
|
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();
|
2017-11-24 03:42:51 +00:00
|
|
|
config.update('enabled', true);
|
|
|
|
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 () => {
|
2017-11-24 03:42:51 +00:00
|
|
|
if (!rpc) return;
|
|
|
|
config.update('enabled', false);
|
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
|
|
|
});
|
|
|
|
|
2017-11-24 00:22:23 +00:00
|
|
|
// Push the new commands into the subscriptions.
|
2017-11-24 03:42:51 +00:00
|
|
|
context.subscriptions.push(enabler, disabler);
|
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.
|
|
|
|
function initRPC(clientID: string): void {
|
|
|
|
// 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', () => {
|
2017-11-26 15:30:32 +00:00
|
|
|
// This is purely for safety measures.
|
|
|
|
if (reconnectTimer) {
|
2017-11-24 02:16:08 +00:00
|
|
|
// Clear the reconnect interval.
|
2017-11-26 15:30:32 +00:00
|
|
|
clearInterval(reconnectTimer);
|
2017-11-24 02:16:08 +00:00
|
|
|
// Null reconnect variable.
|
2017-11-26 15:30:32 +00:00
|
|
|
reconnectTimer = null;
|
|
|
|
}
|
|
|
|
// 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-12-20 05:22:11 +00:00
|
|
|
const workspaceElapsedTime = Boolean(config.get('workspaceElapsedTime'));
|
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();
|
2017-11-24 02:16:08 +00:00
|
|
|
// Set an interval for reconnecting.
|
2017-11-26 15:30:32 +00:00
|
|
|
reconnectTimer = setInterval(() => {
|
2017-11-24 02:45:23 +00:00
|
|
|
reconnectCounter++;
|
|
|
|
initRPC(config.get('clientID'));
|
|
|
|
}, 5000);
|
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(() => {
|
|
|
|
setActivity(workspaceElapsedTime);
|
|
|
|
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 => {
|
2017-11-26 15:30:32 +00:00
|
|
|
if (reconnectTimer) {
|
2017-11-24 12:53:47 +00:00
|
|
|
// Destroy and dispose of everything after a default of 20 reconnect attempts
|
2017-12-20 05:25:37 +00:00
|
|
|
if (reconnectCounter >= config.get('reconnectThreshold')) await destroyRPC();
|
2017-11-24 02:51:50 +00:00
|
|
|
else return;
|
2017-11-24 02:45:23 +00:00
|
|
|
}
|
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!');
|
|
|
|
else window.showErrorMessage(`Couldn't connect to discord via rpc: ${error.message}`);
|
|
|
|
}
|
2017-11-24 02:31:37 +00:00
|
|
|
});
|
2017-11-24 02:16:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanly destroy the RPC client (if it isn't already).
|
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;
|
2017-11-24 02:51:50 +00:00
|
|
|
// Clear the reconnect interval.
|
2017-11-26 15:30:32 +00:00
|
|
|
if (reconnectTimer) clearInterval(reconnectTimer);
|
2017-11-24 02:51:50 +00:00
|
|
|
// Null reconnect variable.
|
2017-11-26 15:30:32 +00:00
|
|
|
reconnectTimer = null;
|
|
|
|
// 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;
|
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
|
2017-11-26 22:06:01 +00:00
|
|
|
? config.get('largeImage')
|
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);
|
|
|
|
}
|