2022-03-10 14:05:08 +01:00
|
|
|
const config = require('../config.json');
|
|
|
|
|
2022-03-03 03:35:34 +01:00
|
|
|
const LINE_START = 'Event: time';
|
2022-02-15 04:33:19 +01:00
|
|
|
|
|
|
|
const ACTION_KEYUP = { id: 0, action: 'keyup' };
|
|
|
|
const ACTION_KEYDOWN = { id: 1, action: 'keydown' };
|
|
|
|
const ACTION_KEYHOLD = { id: 2, action: 'keyhold' };
|
|
|
|
|
2022-02-16 03:28:17 +01:00
|
|
|
const VARIABLE_KEY = '{{ key }}';
|
|
|
|
const VARIABLE_TYPE = '{{ type }}';
|
|
|
|
|
2022-02-15 04:33:19 +01:00
|
|
|
class Keyfilter {
|
2022-03-03 03:35:34 +01:00
|
|
|
constructor(keys, combos) {
|
|
|
|
if ((keys === undefined || keys.length === 0) && (combos === undefined || combos.length === 0)) {
|
2022-02-15 04:33:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.actions = new Map();
|
2022-03-10 12:29:41 +01:00
|
|
|
for (let index = 0; index < keys.length; index++) {
|
2022-03-03 03:35:34 +01:00
|
|
|
this.setAction(keys[index]);
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
2022-03-03 03:35:34 +01:00
|
|
|
this.currentCombo = undefined;
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
2022-03-03 03:35:34 +01:00
|
|
|
setAction(config) {
|
2022-03-10 12:29:41 +01:00
|
|
|
let type = ACTION_KEYDOWN;
|
2022-03-03 03:35:34 +01:00
|
|
|
switch (config.type.toLowerCase()) {
|
|
|
|
case ACTION_KEYUP.action:
|
|
|
|
type = ACTION_KEYUP;
|
|
|
|
break;
|
|
|
|
case ACTION_KEYHOLD.action:
|
|
|
|
type = ACTION_KEYHOLD;
|
|
|
|
break;
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
2022-03-03 03:35:34 +01:00
|
|
|
this.actions.set(config.key.toUpperCase(),
|
|
|
|
{
|
|
|
|
type: type,
|
|
|
|
event: config.event,
|
2022-03-10 14:05:08 +01:00
|
|
|
command: getCommand(config.command),
|
2022-03-03 03:35:34 +01:00
|
|
|
combo: config.combo,
|
|
|
|
delay: function () {
|
|
|
|
if (config.combo === undefined) {
|
|
|
|
return config.delay;
|
|
|
|
}
|
|
|
|
return config.delay || 1000;
|
|
|
|
}(),
|
|
|
|
}
|
|
|
|
);
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
|
|
|
filter(input) {
|
2022-03-03 03:35:34 +01:00
|
|
|
if (input === undefined || input.length === 0) {
|
2022-02-15 04:33:19 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
input = input.toString();
|
2022-03-10 12:29:41 +01:00
|
|
|
let lines = input.split("\n");
|
|
|
|
for (let index = 0; index < lines.length; index++) {
|
|
|
|
let line = lines[index];
|
2022-03-03 03:35:34 +01:00
|
|
|
if (line.length === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!line.startsWith(LINE_START)) {
|
2022-02-15 04:33:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-03 03:35:34 +01:00
|
|
|
const parsedEvent = this.parseLine(line);
|
|
|
|
if (parsedEvent === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-10 12:29:41 +01:00
|
|
|
for (let [key, event] of this.actions) {
|
2022-03-03 03:35:34 +01:00
|
|
|
if (this.currentCombo === undefined && !this.isParsedEventValid(key, event, parsedEvent)) {
|
2022-02-15 04:33:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-03 03:35:34 +01:00
|
|
|
if (this.isStartOfCombo(key, event)) {
|
|
|
|
return this.getFilterResult(key, event, 'combo', this.currentCombo);
|
|
|
|
}
|
|
|
|
if (this.isPartOfCombo(parsedEvent)) {
|
|
|
|
if (parsedEvent.ignore) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const result = this.getFilterResult(key, event, 'combo', this.currentCombo);
|
|
|
|
if (this.hasComboFinished()) {
|
|
|
|
this.resetCurrentCombo();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (!this.isParsedEventValid(key, event, parsedEvent)) {
|
2022-02-15 04:33:19 +01:00
|
|
|
continue;
|
|
|
|
}
|
2022-03-03 03:35:34 +01:00
|
|
|
if (this.shouldBeDelayed(event)) {
|
|
|
|
return this.getFilterResult(key, event, 'delayed', true);
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
2022-03-03 03:35:34 +01:00
|
|
|
event.captured = new Date().getTime();
|
|
|
|
return this.getFilterResult(key, event);
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-03 03:35:34 +01:00
|
|
|
isParsedEventValid(key, value, parsed) {
|
|
|
|
return key === parsed.key && (value.event === undefined || value.event === parsed.event) && value.type.id === parsed.type;
|
|
|
|
}
|
|
|
|
getFilterResult(key, event, extraName, extra) {
|
|
|
|
if (key === undefined || event === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-10 14:05:08 +01:00
|
|
|
let result =
|
|
|
|
// this.replaceVariables(
|
2022-03-03 03:35:34 +01:00
|
|
|
{
|
|
|
|
key: key,
|
|
|
|
type: event.type.action,
|
|
|
|
command: event.command,
|
|
|
|
delay: event.delay
|
|
|
|
}
|
2022-03-10 14:05:08 +01:00
|
|
|
// )
|
2022-03-03 03:35:34 +01:00
|
|
|
if (extraName !== undefined && extra !== undefined) {
|
|
|
|
result[extraName] = extra;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
parseLine(line) {
|
|
|
|
try {
|
|
|
|
const parts = line.split(',');
|
|
|
|
const event = parts[1].substring(parts[1].indexOf('(') + 1, parts[1].lastIndexOf(')'));
|
|
|
|
const key = parts[2].substring(parts[2].indexOf('(') + 1, parts[2].indexOf(')'));
|
|
|
|
const type = parseInt(parts[3].split(' ').pop());
|
|
|
|
return { event: event, key: key, type: type };
|
|
|
|
} catch (err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shouldBeDelayed(event) {
|
|
|
|
if (event.delay === undefined || event.delay === 0 || event.captured === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return new Date().getTime() - event.captured < event.delay;
|
|
|
|
}
|
|
|
|
isStartOfCombo(key, event) {
|
|
|
|
if (this.currentCombo !== undefined || event.combo === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.currentCombo = {
|
|
|
|
key: key,
|
|
|
|
type: event.type,
|
2022-03-03 03:52:34 +01:00
|
|
|
delay: event.delay,
|
|
|
|
timestamp: new Date().getTime(),
|
2022-03-03 03:35:34 +01:00
|
|
|
done: [key],
|
|
|
|
remaining: JSON.parse(JSON.stringify(event.combo)),
|
|
|
|
finished: this.hasComboFinished()
|
|
|
|
};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
isPartOfCombo(parsedEvent) {
|
|
|
|
if (this.hasComboTimedOut()) {
|
|
|
|
this.resetCurrentCombo();
|
|
|
|
}
|
|
|
|
if (this.currentCombo === undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this.currentCombo.done.includes(parsedEvent.key)) {
|
|
|
|
parsedEvent.ignore = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (this.currentCombo.type.id !== parsedEvent.type) {
|
2022-02-15 04:33:19 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-03-03 03:35:34 +01:00
|
|
|
if (this.currentCombo.remaining[0].toUpperCase() !== parsedEvent.key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this.currentCombo.key = parsedEvent.key;
|
2022-03-03 03:52:34 +01:00
|
|
|
this.currentCombo.timestamp = new Date().getTime();
|
2022-03-03 03:35:34 +01:00
|
|
|
this.currentCombo.done.push(parsedEvent.key);
|
|
|
|
this.currentCombo.remaining.shift();
|
|
|
|
this.currentCombo.finished = this.hasComboFinished();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
hasComboFinished() {
|
|
|
|
return this.currentCombo !== undefined &&
|
|
|
|
this.currentCombo.remaining !== undefined &&
|
|
|
|
this.currentCombo.remaining.length === 0;
|
|
|
|
}
|
|
|
|
hasComboTimedOut() {
|
2022-03-03 03:52:34 +01:00
|
|
|
const result = this.currentCombo !== undefined &&
|
2022-03-03 03:35:34 +01:00
|
|
|
this.currentCombo.delay !== undefined &&
|
2022-03-03 03:52:34 +01:00
|
|
|
this.currentCombo.timestamp !== undefined &&
|
|
|
|
new Date().getTime() - this.currentCombo.timestamp > this.currentCombo.delay;
|
|
|
|
return result;
|
2022-03-03 03:35:34 +01:00
|
|
|
}
|
|
|
|
resetCurrentCombo() {
|
|
|
|
this.currentCombo = undefined;
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
2022-02-16 02:08:21 +01:00
|
|
|
isValid() {
|
|
|
|
return this.actions != undefined && this.actions.size > 0;
|
|
|
|
}
|
2022-03-10 14:05:08 +01:00
|
|
|
|
2022-02-15 04:33:19 +01:00
|
|
|
}
|
|
|
|
|
2022-03-10 14:05:08 +01:00
|
|
|
function getCommand(command) {
|
|
|
|
if (command === undefined || config.commands === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const result = config.commands[command];
|
|
|
|
if (result === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
result.name = command;
|
|
|
|
return result;
|
|
|
|
}
|
2022-03-03 03:35:34 +01:00
|
|
|
|
|
|
|
|
2022-02-15 04:33:19 +01:00
|
|
|
module.exports = Keyfilter;
|