Make Serializable dump fromJsObject() calls.
This commit is contained in:
@@ -1,6 +1,35 @@
|
||||
interface Serializable<T> {
|
||||
fromJsObject(jsObject: any): T;
|
||||
fromBinary(buffer: UhkBuffer): T;
|
||||
toJsObject(): any;
|
||||
toBinary(buffer: UhkBuffer);
|
||||
interface Function {
|
||||
name: string;
|
||||
}
|
||||
|
||||
abstract class Serializable<T> {
|
||||
|
||||
private static depth = 0;
|
||||
|
||||
fromJsObject(jsObject: any): T {
|
||||
let identation = new Array(Serializable.depth + 1).join(' ');
|
||||
process.stdout.write(`${identation}* ${this.constructor.name}.fromJsObject(${JSON.stringify(jsObject)}) => `);
|
||||
Serializable.depth++;
|
||||
let value = this._fromJsObject(jsObject);
|
||||
Serializable.depth--;
|
||||
process.stdout.write(`${value.toString()}\n`);
|
||||
return value;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): T {
|
||||
return this._fromBinary(buffer);
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
return this._toJsObject();
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer): void {
|
||||
this._toBinary(buffer);
|
||||
}
|
||||
|
||||
abstract _fromJsObject(jsObject: any): T;
|
||||
abstract _fromBinary(buffer: UhkBuffer): T;
|
||||
abstract _toJsObject(): any;
|
||||
abstract _toBinary(buffer: UhkBuffer): void;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ enum LongPressAction {
|
||||
mouse
|
||||
}
|
||||
|
||||
class DualRoleKeystrokeAction extends KeyAction implements Serializable<DualRoleKeystrokeAction> {
|
||||
class DualRoleKeystrokeAction extends KeyAction {
|
||||
|
||||
public scancode;
|
||||
|
||||
@@ -33,21 +33,21 @@ class DualRoleKeystrokeAction extends KeyAction implements Serializable<DualRole
|
||||
return LongPressAction[keyActionIdParam] !== undefined;
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): DualRoleKeystrokeAction {
|
||||
_fromJsObject(jsObject: any): DualRoleKeystrokeAction {
|
||||
this.assertKeyActionType(jsObject, KeyActionType.DualRoleKeystrokeAction, 'DualRoleKeystrokeAction');
|
||||
this.scancode = jsObject.scancode;
|
||||
this.longPressAction = LongPressAction[<string>jsObject.longPressAction];
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): DualRoleKeystrokeAction {
|
||||
_fromBinary(buffer: UhkBuffer): DualRoleKeystrokeAction {
|
||||
this.readAndAssertKeyActionId(buffer, KeyActionId.DualRoleKeystrokeAction, 'DualRoleKeystrokeAction');
|
||||
this.scancode = buffer.readUInt8();
|
||||
this.longPressAction = buffer.readUInt8();
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeyActionType.DualRoleKeystrokeAction,
|
||||
scancode: this.scancode,
|
||||
@@ -55,9 +55,13 @@ class DualRoleKeystrokeAction extends KeyAction implements Serializable<DualRole
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
_toBinary(buffer: UhkBuffer): void {
|
||||
buffer.writeUInt8(KeyActionId.DualRoleKeystrokeAction);
|
||||
buffer.writeUInt8(this.scancode);
|
||||
buffer.writeUInt8(this.longPressAction);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<DualRoleKeystrokeAction scancode="${this.scancode}" longPressAction="${this.longPressAction}">`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,12 +23,7 @@ let KeyActionType = {
|
||||
PlayMacroAction : 'playMacro'
|
||||
}
|
||||
|
||||
abstract class KeyAction implements Serializable<KeyAction> {
|
||||
abstract fromJsObject(jsObject: any): KeyAction;
|
||||
abstract fromBinary(buffer: UhkBuffer): KeyAction;
|
||||
abstract toJsObject(): any;
|
||||
abstract toBinary(buffer: UhkBuffer);
|
||||
|
||||
abstract class KeyAction extends Serializable<KeyAction> {
|
||||
assertKeyActionType(jsObject: any, keyActionTypeString: string, classname: string) {
|
||||
if (jsObject.keyActionType !== keyActionTypeString) {
|
||||
console.log(arguments.callee.prototype.name);
|
||||
@@ -42,4 +37,9 @@ abstract class KeyAction implements Serializable<KeyAction> {
|
||||
throw `Invalid ${classname} first byte: ${readKeyActionId}`;
|
||||
}
|
||||
}
|
||||
|
||||
abstract _fromJsObject(jsObject: any): KeyAction;
|
||||
abstract _fromBinary(buffer: UhkBuffer): KeyAction;
|
||||
abstract _toJsObject(): any;
|
||||
abstract _toBinary(buffer: UhkBuffer): void;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
class KeyActions implements Serializable<KeyActions> {
|
||||
class KeyActions extends Serializable<KeyActions> {
|
||||
|
||||
keyActions: Serializable<KeyAction>[] = [];
|
||||
|
||||
fromJsObject(jsObjects: any): KeyActions {
|
||||
_fromJsObject(jsObjects: any): KeyActions {
|
||||
for (let jsObject of jsObjects) {
|
||||
this.keyActions.push(KeyActionFactory.fromJsObject(jsObject));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): KeyActions {
|
||||
_fromBinary(buffer: UhkBuffer): KeyActions {
|
||||
let arrayLength = buffer.readCompactLength();
|
||||
for (let i = 0; i < arrayLength; i++) {
|
||||
this.keyActions.push(KeyActionFactory.fromBinary(buffer));
|
||||
@@ -17,7 +17,7 @@ class KeyActions implements Serializable<KeyActions> {
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
_toJsObject(): any {
|
||||
let array = [];
|
||||
for (let keyAction of this.keyActions) {
|
||||
array.push(keyAction.toJsObject());
|
||||
@@ -25,10 +25,14 @@ class KeyActions implements Serializable<KeyActions> {
|
||||
return array;
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeCompactLength(this.keyActions.length);
|
||||
for (let keyAction of this.keyActions) {
|
||||
keyAction.toBinary(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<KeyActions length="${this.keyActions.length}">`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction> {
|
||||
class KeystrokeAction extends KeyAction {
|
||||
|
||||
private _scancode: number;
|
||||
|
||||
@@ -13,27 +13,31 @@ class KeystrokeAction extends KeyAction implements Serializable<KeystrokeAction>
|
||||
this._scancode = value;
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): KeystrokeAction {
|
||||
_fromJsObject(jsObject: any): KeystrokeAction {
|
||||
this.assertKeyActionType(jsObject, KeyActionType.KeystrokeAction, 'KeystrokeAction');
|
||||
this.scancode = jsObject.scancode;
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): KeystrokeAction {
|
||||
_fromBinary(buffer: UhkBuffer): KeystrokeAction {
|
||||
this.readAndAssertKeyActionId(buffer, KeyActionId.KeystrokeAction, 'KeystrokeAction');
|
||||
this.scancode = buffer.readUInt8();
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeyActionType.KeystrokeAction,
|
||||
scancode: this.scancode
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(KeyActionId.KeystrokeAction);
|
||||
buffer.writeUInt8(this.scancode);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<KeystrokeAction scancode="${this.scancode}">`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class KeystrokeWithModifiersAction extends KeyAction implements Serializable<KeystrokeWithModifiersAction> {
|
||||
class KeystrokeWithModifiersAction extends KeyAction {
|
||||
|
||||
static keyActionTypeString = 'keystrokeWithModifiers';
|
||||
|
||||
@@ -17,7 +17,7 @@ class KeystrokeWithModifiersAction extends KeyAction implements Serializable<Key
|
||||
this._scancode = value;
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): KeystrokeWithModifiersAction {
|
||||
_fromJsObject(jsObject: any): KeystrokeWithModifiersAction {
|
||||
this.assertKeyActionType(
|
||||
jsObject, KeystrokeWithModifiersAction.keyActionTypeString, 'KeystrokeWithModifiersAction');
|
||||
this.scancode = jsObject.scancode;
|
||||
@@ -25,14 +25,14 @@ class KeystrokeWithModifiersAction extends KeyAction implements Serializable<Key
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): KeystrokeWithModifiersAction {
|
||||
_fromBinary(buffer: UhkBuffer): KeystrokeWithModifiersAction {
|
||||
this.readAndAssertKeyActionId(buffer, KeyActionId.KeystrokeWithModifiersAction, 'KeystrokeWithModifiersAction');
|
||||
this.scancode = buffer.readUInt8();
|
||||
this.modifierMask = buffer.readUInt8();
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeystrokeWithModifiersAction.keyActionTypeString,
|
||||
scancode: this.scancode,
|
||||
@@ -40,9 +40,13 @@ class KeystrokeWithModifiersAction extends KeyAction implements Serializable<Key
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(KeyActionId.KeystrokeWithModifiersAction);
|
||||
buffer.writeUInt8(this.scancode);
|
||||
buffer.writeUInt8(this.modifierMask);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<KeystrokeWithModifiersAction scancode="${this.scancode}" modifierMask="${this.modifierMask}">`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ enum MouseActionParam {
|
||||
decelerate
|
||||
}
|
||||
|
||||
class MouseAction extends KeyAction implements Serializable<MouseAction> {
|
||||
class MouseAction extends KeyAction {
|
||||
private _mouseAction: MouseActionParam;
|
||||
|
||||
get mouseAction(): MouseActionParam {
|
||||
@@ -32,13 +32,13 @@ class MouseAction extends KeyAction implements Serializable<MouseAction> {
|
||||
// return MouseActionParam[<string>keyActionParam] !== undefined;
|
||||
// }
|
||||
|
||||
fromJsObject(jsObject: any): MouseAction {
|
||||
_fromJsObject(jsObject: any): MouseAction {
|
||||
this.assertKeyActionType(jsObject, KeyActionType.MouseAction, 'MouseAction');
|
||||
this.mouseAction = MouseActionParam[<string>jsObject.mouseAction];
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): MouseAction {
|
||||
_fromBinary(buffer: UhkBuffer): MouseAction {
|
||||
this.readAndAssertKeyActionId(buffer, KeyActionId.MouseAction, 'MouseAction');
|
||||
|
||||
this.mouseAction = buffer.readUInt8();
|
||||
@@ -49,15 +49,19 @@ class MouseAction extends KeyAction implements Serializable<MouseAction> {
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeyActionType.MouseAction,
|
||||
mouseAction: MouseActionParam[this.mouseAction]
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(KeyActionId.MouseAction);
|
||||
buffer.writeUInt8(this.mouseAction);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<MouseAction mouseAction="${this.mouseAction}">`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,26 @@
|
||||
class NoneAction extends KeyAction implements Serializable<NoneAction> {
|
||||
class NoneAction extends KeyAction {
|
||||
|
||||
fromJsObject(jsObject: any): NoneAction {
|
||||
_fromJsObject(jsObject: any): NoneAction {
|
||||
this.assertKeyActionType(jsObject, KeyActionType.NoneAction, 'NoneAction');
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): NoneAction {
|
||||
_fromBinary(buffer: UhkBuffer): NoneAction {
|
||||
this.readAndAssertKeyActionId(buffer, KeyActionId.NoneAction, 'NoneAction');
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeyActionType.NoneAction
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(KeyActionId.NoneAction);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return '<NoneAction>';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class PlayMacroAction extends KeyAction implements Serializable<PlayMacroAction> {
|
||||
class PlayMacroAction extends KeyAction {
|
||||
|
||||
private _macroId: number;
|
||||
|
||||
@@ -13,27 +13,31 @@ class PlayMacroAction extends KeyAction implements Serializable<PlayMacroAction>
|
||||
this._macroId = value;
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): PlayMacroAction {
|
||||
_fromJsObject(jsObject: any): PlayMacroAction {
|
||||
this.assertKeyActionType(jsObject, KeyActionType.PlayMacroAction, 'PlayMacroAction');
|
||||
this.macroId = jsObject.macroId;
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): PlayMacroAction {
|
||||
_fromBinary(buffer: UhkBuffer): PlayMacroAction {
|
||||
this.readAndAssertKeyActionId(buffer, KeyActionId.PlayMacroAction, 'PlayMacroAction');
|
||||
this.macroId = buffer.readUInt8();
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeyActionType.PlayMacroAction,
|
||||
macroId: this.macroId
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(KeyActionId.PlayMacroAction);
|
||||
buffer.writeUInt8(this.macroId);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<PlayMacroAction macroId="${this.macroId}">`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
class SwitchKeymapAction extends KeyAction implements Serializable<SwitchKeymapAction> {
|
||||
class SwitchKeymapAction extends KeyAction {
|
||||
|
||||
private _keymapId: number;
|
||||
|
||||
@@ -13,27 +13,31 @@ class SwitchKeymapAction extends KeyAction implements Serializable<SwitchKeymapA
|
||||
this._keymapId = value;
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): SwitchKeymapAction {
|
||||
_fromJsObject(jsObject: any): SwitchKeymapAction {
|
||||
this.assertKeyActionType(jsObject, KeyActionType.SwitchKeymapAction, 'SwitchKeymapAction');
|
||||
this.keymapId = jsObject.keymapId;
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): SwitchKeymapAction {
|
||||
_fromBinary(buffer: UhkBuffer): SwitchKeymapAction {
|
||||
this.readAndAssertKeyActionId(buffer, KeyActionId.SwitchKeymapAction, 'SwitchKeymapAction');
|
||||
this.keymapId = buffer.readUInt8();
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeyActionType.SwitchKeymapAction,
|
||||
keymapId: this.keymapId
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(KeyActionId.SwitchKeymapAction);
|
||||
buffer.writeUInt8(this.keymapId);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<SwitchKeymapAction keymapId="${this.keymapId}">`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ enum Layer {
|
||||
mouse
|
||||
}
|
||||
|
||||
class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAction> {
|
||||
class SwitchLayerAction extends KeyAction {
|
||||
|
||||
static toggleFlag = 0x80;
|
||||
|
||||
@@ -28,14 +28,14 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
|
||||
return this.isLayerToggleable ? SwitchLayerAction.toggleFlag : 0;
|
||||
}
|
||||
|
||||
fromJsObject(jsObject: any): SwitchLayerAction {
|
||||
_fromJsObject(jsObject: any): SwitchLayerAction {
|
||||
this.assertKeyActionType(jsObject, KeyActionType.SwitchLayerAction, 'SwitchLayerAction');
|
||||
this.layer = Layer[<string>jsObject.layer];
|
||||
this.isLayerToggleable = jsObject.toggle;
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer): SwitchLayerAction {
|
||||
_fromBinary(buffer: UhkBuffer): SwitchLayerAction {
|
||||
this.readAndAssertKeyActionId(buffer, KeyActionId.SwitchLayerAction, 'SwitchLayerAction');
|
||||
let layer = buffer.readUInt8();
|
||||
this.isLayerToggleable = (layer & SwitchLayerAction.toggleFlag) !== 0;
|
||||
@@ -44,7 +44,7 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
|
||||
return this;
|
||||
}
|
||||
|
||||
toJsObject(): any {
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
keyActionType: KeyActionType.SwitchLayerAction,
|
||||
layer: Layer[this.layer],
|
||||
@@ -52,8 +52,12 @@ class SwitchLayerAction extends KeyAction implements Serializable<SwitchLayerAct
|
||||
};
|
||||
}
|
||||
|
||||
toBinary(buffer: UhkBuffer) {
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(KeyActionId.SwitchLayerAction);
|
||||
buffer.writeUInt8(this.layer | this.getToggleFlag());
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<SwitchLayerAction layer="${this.layer}" toggle="${this.isLayerToggleable}">`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user