Commit 3874e26b authored by insert's avatar insert

Added a better way to check whether GW2 is running or not.

parent 8158346e
const MumbleLink = require('../');
const mumbleLink = new MumbleLink();
// Ensure we clean up at the end !!!
process.stdin.resume();
process.on('exit', () => {
mumbleLink.close();
});
if (!mumbleLink.init()) {
console.log('Could not create shared memory location!');
process.exit(1);
}
// --> At this point GW2 should be closed.
// Here we just report what we know every 3 seconds.
setInterval(() => {
mumbleLink.isGuildWars2Running().then(() => {
if (mumbleLink.isGuildWars2()) {
console.log('[S] The user is in-game and has selected a character.');
} else {
console.log('[O] The user has the game open, but is not in-game.');
}
}).catch(() => {
console.log('[F] The user is not in-game and does not have the game open.');
// Ideally, if it reaches this condition after you did something in the other, now is the time to revert it.
});
}, 3000);
\ No newline at end of file
......@@ -2,6 +2,7 @@ const TextDecoder = require('text-encoding').TextDecoder;
const nbind = require('nbind');
const textDecoder = new TextDecoder("utf-8");
const lib = nbind.init(__dirname).lib;
const spawn = require('cross-spawn');
Object.prototype.getKey = function(value) {
let object = this;
......@@ -46,13 +47,16 @@ const collectFloats = (mL, attribute, size = 3) => {
class MumbleLink {
constructor() {
this.mL = new lib.MumbleLink();
this.active = false;
}
/**
* Initiates the Mumble Link.
* @returns {boolean} Whether it has worked or not.
*/
init() {
return this.mL.init();
let result = this.mL.init();
if (result) this.active = true;
return result;
}
/**
* Checks if the client is connected to GW2.
......@@ -63,6 +67,24 @@ class MumbleLink {
// Neither .includes, /Guild Wars 2/, /Guild\wWars\w2/ or /Guild*Wars*2/ wanted to work. :/
return /Guild/.test(this.getName()) && /Wars/.test(this.getName()) && /2/.test(this.getName());
}
/**
* Check if GW2 is actually running or not.
* Useful for situations where isGuildWars2() dosen't mention any closure.
* @returns {Promise} Resolves if running, rejects if not.
*/
isGuildWars2Running() {
return new Promise((resolve, reject) => {
let command = 'ps';
if (/^win/.test(process.platform)) command = `powershell -command "& {&'${command}'}"`;
let child = spawn(command);
let collect = '';
child.stdout.on('data', data => collect += `${data}\n`);
child.on('close', () => {
if (collect.includes('Gw2-64')) resolve();
else reject();
});
});
}
/**
* Checks if the user has selected a character yet.
* Needed to call getIdentity without error.
......@@ -152,7 +174,8 @@ class MumbleLink {
* Probably important.
*/
close() {
this.mL.close();
if (this.active) this.mL.close();
this.active = false;
}
}
......
This diff is collapsed.
{
"name": "gw2mumblelink",
"version": "1.0.3",
"version": "1.0.4",
"description": "Guild Wars 2 Mumble Link integration for Node.js",
"main": "index.js",
"scripts": {
......@@ -10,13 +10,14 @@
"emcc-path": "emcc-path",
"copyasm": "copyasm",
"ndts": "ndts",
"install": "autogypi && node-gyp configure build",
"rebuild": "node-gyp configure build"
"install": "autogypi && node-gyp configure build",
"rebuild": "node-gyp configure build"
},
"author": "insertish <[email protected]>",
"license": "ISC",
"dependencies": {
"autogypi": "^0.2.2",
"cross-spawn": "^6.0.5",
"nbind": "^0.3.15",
"node-gyp": "^3.6.2",
"text-encoding": "^0.6.4"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment