Add KeyActionFactory. Add compact length related methods to UhkBuffer. Add KeyActions.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
class UhkBuffer {
|
class UhkBuffer {
|
||||||
private static eepromSize = 32 * 1024;
|
private static eepromSize = 32 * 1024;
|
||||||
private static maxStringByteLength = 0xFFFF;
|
private static maxCompactLength = 0xFFFF;
|
||||||
private static longStringPrefix = 0xFF;
|
private static longCompactLengthPrefix = 0xFF;
|
||||||
private static stringEncoding = 'utf8';
|
private static stringEncoding = 'utf8';
|
||||||
|
|
||||||
buffer: Buffer;
|
buffer: Buffer;
|
||||||
@@ -87,13 +87,25 @@ class UhkBuffer {
|
|||||||
this.offset += 4;
|
this.offset += 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
readString(): string {
|
readCompactLength(): number {
|
||||||
let stringByteLength = this.readUInt8();
|
let length = this.readUInt8();
|
||||||
|
if (length === UhkBuffer.longCompactLengthPrefix) {
|
||||||
if (stringByteLength === UhkBuffer.longStringPrefix) {
|
length += this.readUInt8() << 8;
|
||||||
stringByteLength += this.readUInt8() << 8;
|
|
||||||
}
|
}
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeCompactLength(length: number) {
|
||||||
|
if (length >= UhkBuffer.longCompactLengthPrefix) {
|
||||||
|
this.writeUInt8(UhkBuffer.longCompactLengthPrefix);
|
||||||
|
this.writeUInt16(length);
|
||||||
|
} else {
|
||||||
|
this.writeUInt8(length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
readString(): string {
|
||||||
|
let stringByteLength = this.readCompactLength();
|
||||||
let str = this.buffer.toString(UhkBuffer.stringEncoding, this.offset, stringByteLength);
|
let str = this.buffer.toString(UhkBuffer.stringEncoding, this.offset, stringByteLength);
|
||||||
this.bytesToBacktrack = stringByteLength;
|
this.bytesToBacktrack = stringByteLength;
|
||||||
this.offset += stringByteLength;
|
this.offset += stringByteLength;
|
||||||
@@ -103,18 +115,12 @@ class UhkBuffer {
|
|||||||
writeString(str: string): void {
|
writeString(str: string): void {
|
||||||
let stringByteLength = Buffer.byteLength(str, UhkBuffer.stringEncoding);
|
let stringByteLength = Buffer.byteLength(str, UhkBuffer.stringEncoding);
|
||||||
|
|
||||||
if (stringByteLength > UhkBuffer.maxStringByteLength) {
|
if (stringByteLength > UhkBuffer.maxCompactLength) {
|
||||||
throw 'Cannot serialize string: ${stringByteLength} bytes is larger ' +
|
throw 'Cannot serialize string: ${stringByteLength} bytes is larger ' +
|
||||||
'than the maximum allowed length of ${UhkBuffer.maxStringByteLength} bytes';
|
'than the maximum allowed length of ${UhkBuffer.maxStringByteLength} bytes';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (stringByteLength >= UhkBuffer.longStringPrefix) {
|
this.writeCompactLength(stringByteLength);
|
||||||
this.writeUInt8(UhkBuffer.longStringPrefix);
|
|
||||||
this.writeUInt16(stringByteLength);
|
|
||||||
} else {
|
|
||||||
this.writeUInt8(stringByteLength);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.buffer.write(str, this.offset, stringByteLength, UhkBuffer.stringEncoding);
|
this.buffer.write(str, this.offset, stringByteLength, UhkBuffer.stringEncoding);
|
||||||
this.offset += stringByteLength;
|
this.offset += stringByteLength;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,52 +12,11 @@ enum KeyActionId {
|
|||||||
PlayMacroAction = 7
|
PlayMacroAction = 7
|
||||||
}
|
}
|
||||||
|
|
||||||
class KeyAction {
|
abstract class KeyAction implements Serializable<KeyAction> {
|
||||||
|
abstract fromJsObject(jsObject: any): KeyAction;
|
||||||
static fromJsObject(jsObject: any): KeyAction {
|
abstract fromBinary(buffer: UhkBuffer): KeyAction;
|
||||||
switch (jsObject.keyActionType) {
|
abstract toJsObject(): any;
|
||||||
case NoneAction.keyActionTypeString:
|
abstract toBinary(buffer: UhkBuffer);
|
||||||
return new NoneAction().fromJsObject(jsObject);
|
|
||||||
case KeystrokeAction.keyActionTypeString:
|
|
||||||
return new KeystrokeAction().fromJsObject(jsObject);
|
|
||||||
case KeystrokeWithModifiersAction.keyActionTypeString:
|
|
||||||
return new KeystrokeWithModifiersAction().fromJsObject(jsObject);
|
|
||||||
case DualRoleKeystrokeAction.keyActionTypeString:
|
|
||||||
return new DualRoleKeystrokeAction().fromJsObject(jsObject);
|
|
||||||
case SwitchLayerAction.keyActionTypeString:
|
|
||||||
return new SwitchLayerAction().fromJsObject(jsObject);
|
|
||||||
case SwitchKeymapAction.keyActionTypeString:
|
|
||||||
return new SwitchKeymapAction().fromJsObject(jsObject);
|
|
||||||
case MouseAction.keyActionTypeString:
|
|
||||||
return new MouseAction().fromJsObject(jsObject);
|
|
||||||
default:
|
|
||||||
throw `Invalid KeyAction.keyActionType: "${jsObject.actionType}"`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static fromBinary(buffer: UhkBuffer): KeyAction {
|
|
||||||
let keyActionFirstByte = buffer.readUInt8();
|
|
||||||
buffer.backtrack();
|
|
||||||
|
|
||||||
switch (keyActionFirstByte) {
|
|
||||||
case KeyActionId.NoneAction:
|
|
||||||
return new NoneAction().fromBinary(buffer);
|
|
||||||
case KeyActionId.KeystrokeAction:
|
|
||||||
return new KeystrokeAction().fromBinary(buffer);
|
|
||||||
case KeyActionId.KeystrokeWithModifiersAction:
|
|
||||||
return new KeystrokeWithModifiersAction().fromBinary(buffer);
|
|
||||||
case KeyActionId.DualRoleKeystrokeAction:
|
|
||||||
return new DualRoleKeystrokeAction().fromBinary(buffer);
|
|
||||||
case KeyActionId.SwitchLayerAction:
|
|
||||||
return new SwitchLayerAction().fromBinary(buffer);
|
|
||||||
case KeyActionId.SwitchKeymapAction:
|
|
||||||
return new SwitchKeymapAction().fromBinary(buffer);
|
|
||||||
case KeyActionId.MouseAction:
|
|
||||||
return new MouseAction().fromBinary(buffer);
|
|
||||||
default:
|
|
||||||
throw `Invalid KeyAction first byte: ${keyActionFirstByte}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assertKeyActionType(jsObject: any, keyActionTypeString: string, classname: string) {
|
assertKeyActionType(jsObject: any, keyActionTypeString: string, classname: string) {
|
||||||
if (jsObject.keyActionType !== keyActionTypeString) {
|
if (jsObject.keyActionType !== keyActionTypeString) {
|
||||||
|
|||||||
47
config-serializer/config-items/KeyActionFactory.ts
Normal file
47
config-serializer/config-items/KeyActionFactory.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
class KeyActionFactory {
|
||||||
|
|
||||||
|
static fromJsObject(jsObject: any): KeyAction {
|
||||||
|
switch (jsObject.keyActionType) {
|
||||||
|
case NoneAction.keyActionTypeString:
|
||||||
|
return new NoneAction().fromJsObject(jsObject);
|
||||||
|
case KeystrokeAction.keyActionTypeString:
|
||||||
|
return new KeystrokeAction().fromJsObject(jsObject);
|
||||||
|
case KeystrokeWithModifiersAction.keyActionTypeString:
|
||||||
|
return new KeystrokeWithModifiersAction().fromJsObject(jsObject);
|
||||||
|
case DualRoleKeystrokeAction.keyActionTypeString:
|
||||||
|
return new DualRoleKeystrokeAction().fromJsObject(jsObject);
|
||||||
|
case SwitchLayerAction.keyActionTypeString:
|
||||||
|
return new SwitchLayerAction().fromJsObject(jsObject);
|
||||||
|
case SwitchKeymapAction.keyActionTypeString:
|
||||||
|
return new SwitchKeymapAction().fromJsObject(jsObject);
|
||||||
|
case MouseAction.keyActionTypeString:
|
||||||
|
return new MouseAction().fromJsObject(jsObject);
|
||||||
|
default:
|
||||||
|
throw `Invalid KeyAction.keyActionType: "${jsObject.actionType}"`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromBinary(buffer: UhkBuffer): KeyAction {
|
||||||
|
let keyActionFirstByte = buffer.readUInt8();
|
||||||
|
buffer.backtrack();
|
||||||
|
|
||||||
|
switch (keyActionFirstByte) {
|
||||||
|
case KeyActionId.NoneAction:
|
||||||
|
return new NoneAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.KeystrokeAction:
|
||||||
|
return new KeystrokeAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.KeystrokeWithModifiersAction:
|
||||||
|
return new KeystrokeWithModifiersAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.DualRoleKeystrokeAction:
|
||||||
|
return new DualRoleKeystrokeAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.SwitchLayerAction:
|
||||||
|
return new SwitchLayerAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.SwitchKeymapAction:
|
||||||
|
return new SwitchKeymapAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.MouseAction:
|
||||||
|
return new MouseAction().fromBinary(buffer);
|
||||||
|
default:
|
||||||
|
throw `Invalid KeyAction first byte: ${keyActionFirstByte}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
config-serializer/config-items/KeyActions.ts
Normal file
34
config-serializer/config-items/KeyActions.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
class KeyActions implements Serializable<KeyActions> {
|
||||||
|
|
||||||
|
keyActions: Serializable<KeyAction>[];
|
||||||
|
|
||||||
|
fromJsObject(jsObjects: any): KeyActions {
|
||||||
|
for (let jsObject of jsObjects) {
|
||||||
|
this.keyActions.push(KeyActionFactory.fromJsObject(jsObject));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
fromBinary(buffer: UhkBuffer): KeyActions {
|
||||||
|
let arrayLength = buffer.readCompactLength();
|
||||||
|
for (let i = 0; i < arrayLength; i++) {
|
||||||
|
this.keyActions.push(KeyActionFactory.fromBinary(buffer));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
toJsObject(): any {
|
||||||
|
let array = [];
|
||||||
|
for (let keyAction of this.keyActions) {
|
||||||
|
keyAction.toJsObject();
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
toBinary(buffer: UhkBuffer) {
|
||||||
|
buffer.writeCompactLength(this.keyActions.length);
|
||||||
|
for (let keyAction of this.keyActions) {
|
||||||
|
keyAction.toBinary(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
/// <reference path="KeyAction.ts" />
|
/// <reference path="KeyAction.ts" />
|
||||||
|
/// <reference path="KeyActionFactory.ts" />
|
||||||
|
/// <reference path="KeyActions.ts" />
|
||||||
/// <reference path="KeystrokeAction.ts" />
|
/// <reference path="KeystrokeAction.ts" />
|
||||||
/// <reference path="KeystrokeWithModifiersAction.ts" />
|
/// <reference path="KeystrokeWithModifiersAction.ts" />
|
||||||
/// <reference path="DualRoleKeystrokeAction.ts" />
|
/// <reference path="DualRoleKeystrokeAction.ts" />
|
||||||
|
|||||||
@@ -9,9 +9,6 @@ let writer = new UhkBuffer();
|
|||||||
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
|
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
|
||||||
let keyActions = uhkConfig.keymaps[0].layers[0].modules[0].keyActions;
|
let keyActions = uhkConfig.keymaps[0].layers[0].modules[0].keyActions;
|
||||||
|
|
||||||
new SwitchLayerAction().fromJsObject({
|
let keyActionObjects: KeyActions = new KeyActions().fromJsObject(keyActions);
|
||||||
keyActionType: 'switchLayer',
|
|
||||||
layerId: 10022
|
|
||||||
});
|
|
||||||
|
|
||||||
fs.writeFileSync('uhk-config.bin', writer.buffer);
|
fs.writeFileSync('uhk-config.bin', writer.buffer);
|
||||||
|
|||||||
Reference in New Issue
Block a user