From a4012458bef28a937e2c422262682c1a037dfac1 Mon Sep 17 00:00:00 2001 From: Daniel Scalzi Date: Sun, 3 Dec 2017 02:13:42 -0500 Subject: [PATCH] Further work on discord rich presence. Current implementation is untested. --- app/assets/js/actionbinder.js | 9 +++++++++ app/assets/js/configmanager.js | 24 +----------------------- app/assets/js/discordwrapper.js | 29 +++++++++++++++++++++-------- app/assets/westeroscraft.json | 10 ++++++++++ docs/distro.md | 12 ++++++++++++ 5 files changed, 53 insertions(+), 31 deletions(-) diff --git a/app/assets/js/actionbinder.js b/app/assets/js/actionbinder.js index db115ad..cc44b05 100644 --- a/app/assets/js/actionbinder.js +++ b/app/assets/js/actionbinder.js @@ -3,6 +3,7 @@ const path = require('path') const {AssetGuard} = require(path.join(__dirname, 'assets', 'js', 'assetguard.js')) const ProcessBuilder = require(path.join(__dirname, 'assets', 'js', 'processbuilder.js')) const ConfigManager = require(path.join(__dirname, 'assets', 'js', 'configmanager.js')) +const DiscordWrapper = require(path.join(__dirname, 'assets', 'js', 'discordwrapper.js')) document.addEventListener('readystatechange', function(){ if (document.readyState === 'interactive'){ @@ -95,6 +96,14 @@ testdownloads = async function(){ } } proc.stdout.on('data', tempListener) + // Init Discord Hook (Untested) + const distro = AssetGuard.retrieveDistributionDataSync(ConfigManager.getGameDirectory) + if(distro.discord != null && serv.discord != null){ + DiscordWrapper.initRPC(distro.discord, serv.discord) + proc.on('close', (code, signal) => { + DiscordWrapper.shutdownRPC() + }) + } } catch(err) { //det_text.innerHTML = 'Error: ' + err.message; det_text.innerHTML = 'Error: See log for details..'; diff --git a/app/assets/js/configmanager.js b/app/assets/js/configmanager.js index 9967c42..8013943 100644 --- a/app/assets/js/configmanager.js +++ b/app/assets/js/configmanager.js @@ -42,10 +42,7 @@ const DEFAULT_CONFIG = { clientToken: uuidV4(), selectedServer: null, // Resolved selectedAccount: null, - authenticationDatabase: [], - discord: { - clientID: 385581240906022916 - } + authenticationDatabase: [] } let config = null; @@ -123,25 +120,6 @@ exports.setSelectedServer = function(serverID){ //TODO Write Authentication Database/Selected Account accessors here -/** - * Retrieve the launcher's Discord Client ID. - * - * @param {Boolean} def - optional. If true, the default value will be returned. - * @returns {String} - the launcher's Discord Client ID. - */ -exports.getDiscordClientID = function(def = false){ - return !def ? config.discord.clientID : DEFAULT_CONFIG.discord.clientID -} - -/** - * Set the launcher's Discord Client ID. - * - * @param {String} clientID - the launcher's new Discord Client ID. - */ -exports.setDiscordClientID = function(clientID){ - config.discord.clientID = clientID -} - // User Configurable Settings // Java Settings diff --git a/app/assets/js/discordwrapper.js b/app/assets/js/discordwrapper.js index 658b19a..f085fa8 100644 --- a/app/assets/js/discordwrapper.js +++ b/app/assets/js/discordwrapper.js @@ -4,24 +4,37 @@ const ConfigManager = require('./configmanager.js') let rpc -function initRPC(){ +exports.initRPC = function(genSettings, servSettings){ rpc = new Client({ transport: 'ipc' }); - rpc.login(ConfigManager.getDiscordClientID()).catch(error => { + rpc.on('ready', () => { + const activity = { + // state = top text + // details = bottom text + state: 'Server: ' + settings.shortId, + details: '', + largeImageKey: servSettings.largeImageKey, + largeImageText: serSettings.largeImageText, + smallImageKey: genSettings.smallImageKey, + smallImageText: genSettings.smallImageText, + startTimestamp: new Date().getTime() / 1000, + instance: false + } + + rpc.setActivity(activity) + }) + + rpc.login(genSettings.clientID()).catch(error => { if(error.message.includes('ENOENT')) { console.log('Unable to initialize Discord Rich Presence, no client detected.') } else { console.log('Unable to initialize Discord Rich Presence: ' + error.message) } }) - - const activity = { - details: 'Playing on WesterosCraft', - - } } -function shutdownRPC(){ +exports.shutdownRPC = function(){ + rpc.setActivity({}) rpc.destroy() rpc = null } \ No newline at end of file diff --git a/app/assets/westeroscraft.json b/app/assets/westeroscraft.json index 8fd950e..948eda6 100644 --- a/app/assets/westeroscraft.json +++ b/app/assets/westeroscraft.json @@ -1,5 +1,10 @@ { "version": "1.0", + "discord": { + "clientID": 385581240906022916, + "smallImageText": "WesterosCraft", + "smallImageKey": "seal-circle" + }, "servers": [ { "id": "WesterosCraft-1.11.2", @@ -9,6 +14,11 @@ "revision": "0.0.1", "server_ip": "mc.westeroscraft.com", "mc_version": "1.11.2", + "discord": { + "shortId": "Production", + "largeImageText": "WesterosCraft Production Server", + "largeImageKey": "server-prod" + }, "default_selected": true, "autoconnect": true, "modules": [ diff --git a/docs/distro.md b/docs/distro.md index 0930eb3..947d354 100644 --- a/docs/distro.md +++ b/docs/distro.md @@ -5,6 +5,11 @@ The distribution index is written in JSON. The general format of the index is as ```json { "version": "1.0", + "discord": { + "clientID": 12334567890, + "smallImageText": "WesterosCraft", + "smallImageKey": "seal-circle" + }, "servers": [ { "id": "Example_Server", @@ -14,6 +19,11 @@ The distribution index is written in JSON. The general format of the index is as "revision": "0.0.1", "server_ip": "mc.westeroscraft.com:1337", "mc_version": "1.11.2", + "discord": { + "shortId": "Example", + "largeImageText": "WesterosCraft Example Server", + "largeImageKey": "server-example" + }, "default_selected": true, "autoconnect": true, "modules": [ @@ -26,6 +36,8 @@ The distribution index is written in JSON. The general format of the index is as You can declare an unlimited number of servers, however you must provide valid values for the fields listed above. In addition to that, the server can declare modules. +The discord settings are to enable the use of Rich Presence on the launcher. For more details, see [discord's documentation](https://discordapp.com/developers/docs/rich-presence/how-to#updating-presence-update-presence-payload-fields). + Only one server in the array should have the `default_selected` property enabled. This will tell the launcher that this is the default server to select if either the previously selected server is invalid, or there is no previously selected server. This field is not defined by any server (avoid this), the first server will be selected as the default. If multiple servers have `default_selected` enabled, the first one the launcher finds will be the effective value. Servers which are not the default may omit this property rather than explicitly setting it to false. ## Modules