diff --git a/config-serializer/Serializable.ts b/config-serializer/Serializable.ts index 5949e77e..ac056fc9 100644 --- a/config-serializer/Serializable.ts +++ b/config-serializer/Serializable.ts @@ -1,6 +1,35 @@ -interface Serializable { - fromJsObject(jsObject: any): T; - fromBinary(buffer: UhkBuffer): T; - toJsObject(): any; - toBinary(buffer: UhkBuffer); +interface Function { + name: string; +} + +abstract class Serializable { + + 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; } diff --git a/config-serializer/config-items/DualRoleKeystrokeAction.ts b/config-serializer/config-items/DualRoleKeystrokeAction.ts index 93aaeae1..b05e8d27 100644 --- a/config-serializer/config-items/DualRoleKeystrokeAction.ts +++ b/config-serializer/config-items/DualRoleKeystrokeAction.ts @@ -12,7 +12,7 @@ enum LongPressAction { mouse } -class DualRoleKeystrokeAction extends KeyAction implements Serializable { +class DualRoleKeystrokeAction extends KeyAction { public scancode; @@ -33,21 +33,21 @@ class DualRoleKeystrokeAction extends KeyAction implements SerializablejsObject.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`; + } } diff --git a/config-serializer/config-items/KeyAction.ts b/config-serializer/config-items/KeyAction.ts index d2c4be30..7f1e1fc0 100644 --- a/config-serializer/config-items/KeyAction.ts +++ b/config-serializer/config-items/KeyAction.ts @@ -23,12 +23,7 @@ let KeyActionType = { PlayMacroAction : 'playMacro' } -abstract class KeyAction implements Serializable { - abstract fromJsObject(jsObject: any): KeyAction; - abstract fromBinary(buffer: UhkBuffer): KeyAction; - abstract toJsObject(): any; - abstract toBinary(buffer: UhkBuffer); - +abstract class KeyAction extends Serializable { 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 { throw `Invalid ${classname} first byte: ${readKeyActionId}`; } } + + abstract _fromJsObject(jsObject: any): KeyAction; + abstract _fromBinary(buffer: UhkBuffer): KeyAction; + abstract _toJsObject(): any; + abstract _toBinary(buffer: UhkBuffer): void; } diff --git a/config-serializer/config-items/KeyActions.ts b/config-serializer/config-items/KeyActions.ts index 16f9977d..8b4bc640 100644 --- a/config-serializer/config-items/KeyActions.ts +++ b/config-serializer/config-items/KeyActions.ts @@ -1,15 +1,15 @@ -class KeyActions implements Serializable { +class KeyActions extends Serializable { keyActions: Serializable[] = []; - 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 { 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 { 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 ``; + } } diff --git a/config-serializer/config-items/KeystrokeAction.ts b/config-serializer/config-items/KeystrokeAction.ts index d529d239..06ab8ce1 100644 --- a/config-serializer/config-items/KeystrokeAction.ts +++ b/config-serializer/config-items/KeystrokeAction.ts @@ -1,4 +1,4 @@ -class KeystrokeAction extends KeyAction implements Serializable { +class KeystrokeAction extends KeyAction { private _scancode: number; @@ -13,27 +13,31 @@ class KeystrokeAction extends KeyAction implements Serializable 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 ``; + } } diff --git a/config-serializer/config-items/KeystrokeWithModifiersAction.ts b/config-serializer/config-items/KeystrokeWithModifiersAction.ts index a4376bc8..646b8eee 100644 --- a/config-serializer/config-items/KeystrokeWithModifiersAction.ts +++ b/config-serializer/config-items/KeystrokeWithModifiersAction.ts @@ -1,4 +1,4 @@ -class KeystrokeWithModifiersAction extends KeyAction implements Serializable { +class KeystrokeWithModifiersAction extends KeyAction { static keyActionTypeString = 'keystrokeWithModifiers'; @@ -17,7 +17,7 @@ class KeystrokeWithModifiersAction extends KeyAction implements Serializable`; + } } diff --git a/config-serializer/config-items/MouseAction.ts b/config-serializer/config-items/MouseAction.ts index 706e3e95..b1400f4d 100644 --- a/config-serializer/config-items/MouseAction.ts +++ b/config-serializer/config-items/MouseAction.ts @@ -14,7 +14,7 @@ enum MouseActionParam { decelerate } -class MouseAction extends KeyAction implements Serializable { +class MouseAction extends KeyAction { private _mouseAction: MouseActionParam; get mouseAction(): MouseActionParam { @@ -32,13 +32,13 @@ class MouseAction extends KeyAction implements Serializable { // return MouseActionParam[keyActionParam] !== undefined; // } - fromJsObject(jsObject: any): MouseAction { + _fromJsObject(jsObject: any): MouseAction { this.assertKeyActionType(jsObject, KeyActionType.MouseAction, 'MouseAction'); this.mouseAction = MouseActionParam[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 { 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 ``; + } } diff --git a/config-serializer/config-items/NoneAction.ts b/config-serializer/config-items/NoneAction.ts index d190d635..16312074 100644 --- a/config-serializer/config-items/NoneAction.ts +++ b/config-serializer/config-items/NoneAction.ts @@ -1,22 +1,26 @@ -class NoneAction extends KeyAction implements Serializable { +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 ''; + } } diff --git a/config-serializer/config-items/PlayMacroAction.ts b/config-serializer/config-items/PlayMacroAction.ts index 292eed56..d1713634 100644 --- a/config-serializer/config-items/PlayMacroAction.ts +++ b/config-serializer/config-items/PlayMacroAction.ts @@ -1,4 +1,4 @@ -class PlayMacroAction extends KeyAction implements Serializable { +class PlayMacroAction extends KeyAction { private _macroId: number; @@ -13,27 +13,31 @@ class PlayMacroAction extends KeyAction implements Serializable 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 ``; + } } diff --git a/config-serializer/config-items/SwitchKeymapAction.ts b/config-serializer/config-items/SwitchKeymapAction.ts index 91cf6a02..7bde3bd8 100644 --- a/config-serializer/config-items/SwitchKeymapAction.ts +++ b/config-serializer/config-items/SwitchKeymapAction.ts @@ -1,4 +1,4 @@ -class SwitchKeymapAction extends KeyAction implements Serializable { +class SwitchKeymapAction extends KeyAction { private _keymapId: number; @@ -13,27 +13,31 @@ class SwitchKeymapAction extends KeyAction implements Serializable`; + } } diff --git a/config-serializer/config-items/SwitchLayerAction.ts b/config-serializer/config-items/SwitchLayerAction.ts index a81581d4..89b92399 100644 --- a/config-serializer/config-items/SwitchLayerAction.ts +++ b/config-serializer/config-items/SwitchLayerAction.ts @@ -4,7 +4,7 @@ enum Layer { mouse } -class SwitchLayerAction extends KeyAction implements Serializable { +class SwitchLayerAction extends KeyAction { static toggleFlag = 0x80; @@ -28,14 +28,14 @@ class SwitchLayerAction extends KeyAction implements SerializablejsObject.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`; + } }