-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·115 lines (94 loc) · 2.55 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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const ps3 = require("./ps3.js");
const uinput = require("./uinput.js");
const vm = require("vm");
const _ = require("lodash");
const keyboardEvents = [];
const keyboard = {};
const keyboardPrev = {};
const vjoyA = {};
const vjoyAPrev = {};
const vjoyB = {};
const vjoyBPrev = {};
var scriptGlobal = {
console,
exports: {},
module: {
exports: {}
},
keyboard,
keyboardEvents,
vjoyA,
vjoyB,
ps3: {}
};
scriptGlobal.exports = scriptGlobal.module.exports;
var running;
async function onPs3Data(data) {
if (running) return;
running = true;
try {
scriptGlobal.ps3 = data;
scriptGlobal.module.exports.loop()
for (const code of keyboardEvents) {
if (typeof(code) === "string") {
await uinput.keyPress(code);
} else {
await uinput.keyCombo(code);
}
}
keyboardEvents.splice(0, keyboardEvents.length);
for (const code in keyboard) {
const nextValue = keyboard[code];
const prevValue = keyboardPrev[code];
if (nextValue === prevValue) continue;
await uinput.key(code, nextValue);
keyboardPrev[code] = nextValue;
}
for (const code in vjoyA) {
const nextValue = vjoyA[code];
const prevValue = vjoyAPrev[code];
if (nextValue === prevValue) continue;
const value = nextValue * 500 + 500;
await uinput.axis(code, value);
vjoyAPrev[code] = nextValue;
}
for (const code in vjoyB) {
const nextValue = vjoyB[code];
const prevValue = vjoyBPrev[code];
if (nextValue === prevValue) continue;
await uinput.button(code, nextValue);
vjoyBPrev[code] = nextValue;
}
await uinput.sync();
} catch (err) {
console.log(err)
} finally {
running = false;
}
}
async function main() {
await ps3.setup();
await uinput.setup();
console.info("Ready");
const scriptCode = fs.readFileSync(path.join(__dirname, "scripts/descent.js"));
const script = new vm.Script(scriptCode);
script.runInNewContext(scriptGlobal);
ps3.on("data", onPs3Data);
}
function mainSync() {
(async() => {
try {
await main();
} catch (err) {
console.log(err);
}
})();
}
process.on("SIGINT", function() {
uinput.teardown();
process.exit();
});
mainSync();