-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
187 lines (154 loc) · 4.77 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const activeWindows = require('active-windows');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const SockJS = require('sockjs-client');
printInfo('Streamlabs Settings -> Remote Control -> Click the QR Code -> Restart this Client');
printInfo('Attempting Socket Connection to Streamlabs...');
const sock = new SockJS('http://127.0.0.1:59650/api');
const currentDirectory = process.cwd().replace(/\\/g, '/');
let nextConfigRefreshTime = Date.now();
let pendingTransactions = [];
let lastWindow = '';
let paused = false;
let oldSize = 0;
let config = {
token: 'slobs settings -> remote control -> click qr code -> show details -> copy api token',
printWindowNames: false,
scenes: [
{
windowClassName: 'Code.exe',
targetScene: `CodeScene`
},
{
windowClassName: 'GTA5.exe',
targetScene: `GTAScene`
}
]
};
function readConfiguration() {
if (!fs.existsSync(path.join(currentDirectory, 'config.json'))) {
fs.writeFileSync(path.join(currentDirectory, `config.json`), JSON.stringify(config, null, '\t'));
}
const configStats = fs.statSync(path.join(currentDirectory, 'config.json'));
if (oldSize === configStats.size) {
return;
}
oldSize = configStats.size;
config = JSON.parse(fs.readFileSync(path.join(currentDirectory, `config.json`)).toString());
printInfo('Configuration has been updated.');
printInfo(`Current Scene Count: ${config.scenes.length}`);
}
function printInfo(msg) {
console.log(chalk.blueBright(`[INFO] ${msg}`));
}
async function sendMessage(message) {
let requestBody = message;
if (typeof message === 'string') {
try {
requestBody = JSON.parse(message);
} catch (e) {
alert('Invalid JSON');
return;
}
}
sock.send(JSON.stringify(requestBody));
}
async function request(resourceId, methodName, ...args) {
let requestBody = {
jsonrpc: '2.0',
id: 2,
method: methodName,
params: { resource: resourceId, args }
};
await sendMessage(requestBody);
}
sock.onopen = async () => {
printInfo('Connected to Streamlabs Successfully!');
readConfiguration();
if (config.token.includes('slobs')) {
await new Promise((resolve) => {
printInfo(`Please Provide an API Token in your 'config.json.'`);
printInfo(config.token);
const interval = setInterval(() => {
readConfiguration();
if (config.token.includes('slobs')) {
return;
}
clearInterval(interval);
resolve();
}, 2500);
});
}
await request('TcpServerService', 'auth', config.token);
setInterval(checkCurrentWindow, 200);
};
function checkCurrentWindow() {
if (paused) {
return;
}
if (!config) {
return;
}
if (Date.now() > nextConfigRefreshTime) {
nextConfigRefreshTime = Date.now() + 5000;
readConfiguration();
}
const currentWindow = activeWindows.getActiveWindow();
if (!currentWindow) {
return;
}
if (config.printWindowNames) {
printInfo(`DEBUG WINDOW CLASS NAME: ${currentWindow.windowClass}`);
}
const configIndex = config.scenes.findIndex((option) => option.windowClassName === currentWindow.windowClass);
if (configIndex <= -1) {
return;
}
const sceneInfo = config.scenes[configIndex];
if (lastWindow === currentWindow.windowClass) {
return;
}
lastWindow = currentWindow.windowClass;
pendingTransactions.push({ type: 'sceneRequest', sceneName: sceneInfo.targetScene });
sock.send(
JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'getScenes',
params: {
resource: 'ScenesService'
}
})
);
}
sock.onmessage = (e) => {
if (pendingTransactions.length <= 0) {
return;
}
const transactionType = pendingTransactions.shift();
const response = JSON.parse(e.data);
if (transactionType.type !== 'sceneRequest') {
return;
}
if (response.result[0].name === undefined) {
printInfo(`Was Unable to Parse a Result`);
return;
}
const foundScene = response.result.find((x) => x.name === transactionType.sceneName);
if (foundScene === undefined) {
return;
}
printInfo(`Transition to Scene: ${transactionType.sceneName}`);
sock.send(
JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'makeSceneActive',
params: {
resource: 'ScenesService',
args: [foundScene.id]
}
})
);
};