diff --git a/src/config-serializer/Serializable.ts b/src/config-serializer/Serializable.ts deleted file mode 100644 index 1ed1fd32..00000000 --- a/src/config-serializer/Serializable.ts +++ /dev/null @@ -1,51 +0,0 @@ -/// - -import { UhkBuffer } from './UhkBuffer'; - -export abstract class Serializable { - - private static depth = 0; - private static maxDisplayedJsonLength = 160; - private static enableDump = false; - - toJsonObject(): any { - this.dump(`${this.getIndentation()}${this.constructor.name}.toJsObject: ${this}\n`); - Serializable.depth++; - let value = this._toJsonObject(); - Serializable.depth--; - this.dump(`${this.getIndentation()}=> ${this.stringifyJsonObject(value)}\n`); - return value; - } - - // TODO: remove parameter and return the buffer - toBinary(buffer: UhkBuffer): void { - this.dump(`\n${this.getIndentation()}${this.constructor.name}.toBinary: ${this} [`); - Serializable.depth++; - buffer.enableDump = Serializable.enableDump; - let value = this._toBinary(buffer); - buffer.enableDump = false; - Serializable.depth--; - this.dump(`]`); - return value; - } - - abstract _toJsonObject(): any; - abstract _toBinary(buffer: UhkBuffer): void; - - private dump(value: any) { - if (Serializable.enableDump) { - process.stdout.write(value); - } - } - - private getIndentation() { - return new Array(Serializable.depth + 1).join(' '); - } - - private stringifyJsonObject(jsonObject: any): string { - let json = JSON.stringify(jsonObject); - return json.length > Serializable.maxDisplayedJsonLength - ? json.substr(0, Serializable.maxDisplayedJsonLength) + '...' - : json; - } -} diff --git a/src/config-serializer/UhkBuffer.ts b/src/config-serializer/UhkBuffer.ts index aa9866bf..48443e02 100644 --- a/src/config-serializer/UhkBuffer.ts +++ b/src/config-serializer/UhkBuffer.ts @@ -1,5 +1,3 @@ -import { Serializable } from './Serializable'; - export class UhkBuffer { private static eepromSize = 32 * 1024; @@ -153,7 +151,7 @@ export class UhkBuffer { this.writeUInt8(bool ? 1 : 0); } - readArray>(elementReader: (buffer: UhkBuffer) => T): T[] { + readArray(elementReader: (buffer: UhkBuffer) => T): T[] { let array: T[] = []; let length = this.readCompactLength(); for (let i = 0; i < length; ++i) { @@ -162,10 +160,10 @@ export class UhkBuffer { return array; } - writeArray>(array: T[]): void { + writeArray(array: T[]): void { this.writeCompactLength(array.length); for (let element of array) { - element.toBinary(this); + (element).toBinary(this); // TODO: Remove any } } diff --git a/src/config-serializer/config-items/Keymap.ts b/src/config-serializer/config-items/Keymap.ts index e963b2a7..e27fc6ca 100644 --- a/src/config-serializer/config-items/Keymap.ts +++ b/src/config-serializer/config-items/Keymap.ts @@ -1,9 +1,8 @@ -import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; import { Layer } from './Layer'; import { Macro } from './Macro'; -export class Keymap extends Serializable { +export class Keymap { name: string; @@ -16,7 +15,6 @@ export class Keymap extends Serializable { layers: Layer[]; constructor(keymap?: Keymap) { - super(); if (!keymap) { return; } @@ -48,7 +46,7 @@ export class Keymap extends Serializable { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { isDefault: this.isDefault, abbreviation: this.abbreviation, @@ -58,7 +56,7 @@ export class Keymap extends Serializable { }; } - _toBinary(buffer: UhkBuffer): void { + toBinary(buffer: UhkBuffer): void { buffer.writeString(this.abbreviation); buffer.writeBoolean(this.isDefault); buffer.writeString(this.name); diff --git a/src/config-serializer/config-items/Layer.ts b/src/config-serializer/config-items/Layer.ts index df7e311e..ba6f178e 100644 --- a/src/config-serializer/config-items/Layer.ts +++ b/src/config-serializer/config-items/Layer.ts @@ -1,15 +1,13 @@ -import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; import { Keymap } from './Keymap'; import { Macro } from './Macro'; import { Module } from './Module'; -export class Layer extends Serializable { +export class Layer { modules: Module[]; constructor(layers?: Layer) { - super(); if (!layers) { return; } @@ -28,13 +26,13 @@ export class Layer extends Serializable { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { modules: this.modules.map(module => module.toJsonObject()) }; } - _toBinary(buffer: UhkBuffer): void { + toBinary(buffer: UhkBuffer): void { buffer.writeArray(this.modules); } diff --git a/src/config-serializer/config-items/Macro.ts b/src/config-serializer/config-items/Macro.ts index a6abc24a..836804ed 100644 --- a/src/config-serializer/config-items/Macro.ts +++ b/src/config-serializer/config-items/Macro.ts @@ -1,9 +1,8 @@ import { assertUInt8 } from '../assert'; -import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; import { Helper as MacroActionHelper, MacroAction } from './macro-action'; -export class Macro extends Serializable { +export class Macro { @assertUInt8 id: number; @@ -17,7 +16,6 @@ export class Macro extends Serializable { macroActions: MacroAction[]; constructor(other?: Macro) { - super(); if (!other) { return; } @@ -50,7 +48,7 @@ export class Macro extends Serializable { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { id: this.id, isLooped: this.isLooped, @@ -60,7 +58,7 @@ export class Macro extends Serializable { }; } - _toBinary(buffer: UhkBuffer): void { + toBinary(buffer: UhkBuffer): void { buffer.writeUInt8(this.id); buffer.writeBoolean(this.isLooped); buffer.writeBoolean(this.isPrivate); diff --git a/src/config-serializer/config-items/Module.ts b/src/config-serializer/config-items/Module.ts index 99fdd207..be4eaa09 100644 --- a/src/config-serializer/config-items/Module.ts +++ b/src/config-serializer/config-items/Module.ts @@ -1,5 +1,4 @@ import { assertEnum, assertUInt8 } from '../assert'; -import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; import { Helper as KeyActionHelper, KeyAction, NoneAction } from './key-action'; import { Keymap } from './Keymap'; @@ -11,7 +10,7 @@ enum PointerRole { scroll } -export class Module extends Serializable { +export class Module { @assertUInt8 id: number; @@ -22,7 +21,6 @@ export class Module extends Serializable { pointerRole: PointerRole; constructor(other?: Module) { - super(); if (!other) { return; } @@ -51,7 +49,7 @@ export class Module extends Serializable { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { id: this.id, pointerRole: PointerRole[this.pointerRole], @@ -63,7 +61,7 @@ export class Module extends Serializable { }; } - _toBinary(buffer: UhkBuffer): void { + toBinary(buffer: UhkBuffer): void { buffer.writeUInt8(this.id); buffer.writeUInt8(this.pointerRole); diff --git a/src/config-serializer/config-items/ModuleConfiguration.ts b/src/config-serializer/config-items/ModuleConfiguration.ts index 257afecb..fa4f8827 100644 --- a/src/config-serializer/config-items/ModuleConfiguration.ts +++ b/src/config-serializer/config-items/ModuleConfiguration.ts @@ -1,8 +1,7 @@ import { assertUInt8 } from '../assert'; -import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; -export class ModuleConfiguration extends Serializable { +export class ModuleConfiguration { /* * module id enumeration is a separate story @@ -36,7 +35,7 @@ export class ModuleConfiguration extends Serializable { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { id: this.id, initialPointerSpeed: this.initialPointerSpeed, @@ -45,7 +44,7 @@ export class ModuleConfiguration extends Serializable { }; } - _toBinary(buffer: UhkBuffer): void { + toBinary(buffer: UhkBuffer): void { buffer.writeUInt8(this.id); buffer.writeUInt8(this.initialPointerSpeed); buffer.writeUInt8(this.pointerAcceleration); diff --git a/src/config-serializer/config-items/UhkConfiguration.ts b/src/config-serializer/config-items/UhkConfiguration.ts index 7e497341..e0d76871 100644 --- a/src/config-serializer/config-items/UhkConfiguration.ts +++ b/src/config-serializer/config-items/UhkConfiguration.ts @@ -1,11 +1,10 @@ import { assertUInt16, assertUInt32, assertUInt8 } from '../assert'; -import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; import { Keymap } from './Keymap'; import { Macro } from './Macro'; import { ModuleConfiguration } from './ModuleConfiguration'; -export class UhkConfiguration extends Serializable { +export class UhkConfiguration { signature: string; @@ -60,7 +59,7 @@ export class UhkConfiguration extends Serializable { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { signature: this.signature, dataModelVersion: this.dataModelVersion, @@ -74,7 +73,7 @@ export class UhkConfiguration extends Serializable { }; } - _toBinary(buffer: UhkBuffer): void { + toBinary(buffer: UhkBuffer): void { buffer.writeString(this.signature); buffer.writeUInt16(this.dataModelVersion); buffer.writeUInt32(this.prologue); diff --git a/src/config-serializer/config-items/key-action/KeyAction.ts b/src/config-serializer/config-items/key-action/KeyAction.ts index e4b09b93..0c2af432 100644 --- a/src/config-serializer/config-items/key-action/KeyAction.ts +++ b/src/config-serializer/config-items/key-action/KeyAction.ts @@ -1,6 +1,5 @@ /// -import { Serializable } from '../../Serializable'; import { UhkBuffer } from '../../UhkBuffer'; export enum KeyActionId { @@ -29,7 +28,7 @@ export let keyActionType = { PlayMacroAction : 'playMacro' }; -export abstract class KeyAction extends Serializable { +export abstract class KeyAction { assertKeyActionType(jsObject: any): void { let keyActionClassname: string = this.constructor.name; @@ -53,6 +52,6 @@ export abstract class KeyAction extends Serializable { return readKeyActionId; } - abstract _toJsonObject(): any; - abstract _toBinary(buffer: UhkBuffer): void; + abstract toJsonObject(): any; + abstract toBinary(buffer: UhkBuffer): void; } diff --git a/src/config-serializer/config-items/key-action/KeystrokeAction.ts b/src/config-serializer/config-items/key-action/KeystrokeAction.ts index 36c0c773..27dffd7d 100644 --- a/src/config-serializer/config-items/key-action/KeystrokeAction.ts +++ b/src/config-serializer/config-items/key-action/KeystrokeAction.ts @@ -63,7 +63,7 @@ export class KeystrokeAction extends KeyAction { return this; } - _toJsonObject(): JsonObjectKeystrokeAction { + toJsonObject(): JsonObjectKeystrokeAction { let jsonObject: JsonObjectKeystrokeAction = { keyActionType: keyActionType.KeystrokeAction }; @@ -83,7 +83,7 @@ export class KeystrokeAction extends KeyAction { return jsonObject; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { let flags = 0; let bufferData: number[] = []; diff --git a/src/config-serializer/config-items/key-action/MouseAction.ts b/src/config-serializer/config-items/key-action/MouseAction.ts index dea12860..91afcbf1 100644 --- a/src/config-serializer/config-items/key-action/MouseAction.ts +++ b/src/config-serializer/config-items/key-action/MouseAction.ts @@ -43,14 +43,14 @@ export class MouseAction extends KeyAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { keyActionType: keyActionType.MouseAction, mouseAction: MouseActionParam[this.mouseAction] }; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { buffer.writeUInt8(KeyActionId.MouseAction); buffer.writeUInt8(this.mouseAction); } diff --git a/src/config-serializer/config-items/key-action/NoneAction.ts b/src/config-serializer/config-items/key-action/NoneAction.ts index 908b3c08..11a9eae7 100644 --- a/src/config-serializer/config-items/key-action/NoneAction.ts +++ b/src/config-serializer/config-items/key-action/NoneAction.ts @@ -19,13 +19,13 @@ export class NoneAction extends KeyAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { keyActionType: keyActionType.NoneAction }; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { buffer.writeUInt8(KeyActionId.NoneAction); } diff --git a/src/config-serializer/config-items/key-action/PlayMacroAction.ts b/src/config-serializer/config-items/key-action/PlayMacroAction.ts index 3d7baaca..5986c4bf 100644 --- a/src/config-serializer/config-items/key-action/PlayMacroAction.ts +++ b/src/config-serializer/config-items/key-action/PlayMacroAction.ts @@ -32,14 +32,14 @@ export class PlayMacroAction extends KeyAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { keyActionType: keyActionType.PlayMacroAction, macroId: this.macroId }; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { buffer.writeUInt8(KeyActionId.PlayMacroAction); buffer.writeUInt8(this.macroId); } diff --git a/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts b/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts index aa98c08a..fcedfa8c 100644 --- a/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts +++ b/src/config-serializer/config-items/key-action/SwitchKeymapAction.ts @@ -30,14 +30,14 @@ export class SwitchKeymapAction extends KeyAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { keyActionType: keyActionType.SwitchKeymapAction, keymapAbbreviation: this.keymapAbbreviation }; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { buffer.writeUInt8(KeyActionId.SwitchKeymapAction); buffer.writeString(this.keymapAbbreviation); } diff --git a/src/config-serializer/config-items/key-action/SwitchLayerAction.ts b/src/config-serializer/config-items/key-action/SwitchLayerAction.ts index d750a4fc..0dceed62 100644 --- a/src/config-serializer/config-items/key-action/SwitchLayerAction.ts +++ b/src/config-serializer/config-items/key-action/SwitchLayerAction.ts @@ -38,7 +38,7 @@ export class SwitchLayerAction extends KeyAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { keyActionType: keyActionType.SwitchLayerAction, layer: LayerName[this.layer], @@ -46,7 +46,7 @@ export class SwitchLayerAction extends KeyAction { }; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { buffer.writeUInt8(KeyActionId.SwitchLayerAction); buffer.writeUInt8(this.layer); buffer.writeBoolean(this.isLayerToggleable); diff --git a/src/config-serializer/config-items/macro-action/DelayMacroAction.ts b/src/config-serializer/config-items/macro-action/DelayMacroAction.ts index d942399c..5085b5ae 100644 --- a/src/config-serializer/config-items/macro-action/DelayMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/DelayMacroAction.ts @@ -27,14 +27,14 @@ export class DelayMacroAction extends MacroAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { macroActionType: macroActionType.DelayMacroAction, delay: this.delay }; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { buffer.writeUInt8(MacroActionId.DelayMacroAction); buffer.writeUInt16(this.delay); } diff --git a/src/config-serializer/config-items/macro-action/KeyMacroAction.ts b/src/config-serializer/config-items/macro-action/KeyMacroAction.ts index 4029002e..563854f9 100644 --- a/src/config-serializer/config-items/macro-action/KeyMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/KeyMacroAction.ts @@ -55,7 +55,7 @@ export class KeyMacroAction extends MacroAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { let jsObject: JsObjectKeyMacroAction = { macroActionType: macroActionType.KeyMacroAction, action: MacroSubAction[this.action] @@ -72,7 +72,7 @@ export class KeyMacroAction extends MacroAction { return jsObject; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { let keyMacroType: number = MacroActionId.KeyMacroAction; keyMacroType += NUM_OF_COMBINATIONS * this.action; diff --git a/src/config-serializer/config-items/macro-action/MacroAction.ts b/src/config-serializer/config-items/macro-action/MacroAction.ts index 62b7a2ad..2eba32f5 100644 --- a/src/config-serializer/config-items/macro-action/MacroAction.ts +++ b/src/config-serializer/config-items/macro-action/MacroAction.ts @@ -1,4 +1,3 @@ -import { Serializable } from '../../Serializable'; import { UhkBuffer } from '../../UhkBuffer'; export enum MacroActionId { @@ -45,7 +44,7 @@ export let macroActionType = { TextMacroAction : 'text' }; -export abstract class MacroAction extends Serializable { +export abstract class MacroAction { assertMacroActionType(jsObject: any) { let macroActionClassname = this.constructor.name; let macroActionTypeString = macroActionType[macroActionClassname]; @@ -73,6 +72,6 @@ export abstract class MacroAction extends Serializable { return readMacroActionId; } - abstract _toJsonObject(): any; - abstract _toBinary(buffer: UhkBuffer): void; + abstract toJsonObject(): any; + abstract toBinary(buffer: UhkBuffer): void; } diff --git a/src/config-serializer/config-items/macro-action/MouseButtonMacroAction.ts b/src/config-serializer/config-items/macro-action/MouseButtonMacroAction.ts index 562598da..a5626c29 100644 --- a/src/config-serializer/config-items/macro-action/MouseButtonMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/MouseButtonMacroAction.ts @@ -44,7 +44,7 @@ export class MouseButtonMacroAction extends MacroAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { macroActionType: macroActionType.MouseButtonMacroAction, action: MacroSubAction[this.action], @@ -52,7 +52,7 @@ export class MouseButtonMacroAction extends MacroAction { }; } - _toBinary(buffer: UhkBuffer): void { + toBinary(buffer: UhkBuffer): void { buffer.writeUInt8(MacroActionId.MouseButtonMacroAction + this.action); buffer.writeUInt8(this.mouseButtonsMask); } diff --git a/src/config-serializer/config-items/macro-action/MoveMouseMacroAction.ts b/src/config-serializer/config-items/macro-action/MoveMouseMacroAction.ts index 3dd74fd7..56dadc37 100644 --- a/src/config-serializer/config-items/macro-action/MoveMouseMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/MoveMouseMacroAction.ts @@ -33,7 +33,7 @@ export class MoveMouseMacroAction extends MacroAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { macroActionType: macroActionType.MoveMouseMacroAction, x: this.x, @@ -41,7 +41,7 @@ export class MoveMouseMacroAction extends MacroAction { }; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { buffer.writeUInt8(MacroActionId.MoveMouseMacroAction); buffer.writeInt16(this.x); buffer.writeInt16(this.y); diff --git a/src/config-serializer/config-items/macro-action/ScrollMouseMacroAction.ts b/src/config-serializer/config-items/macro-action/ScrollMouseMacroAction.ts index c7734841..ac736cc4 100644 --- a/src/config-serializer/config-items/macro-action/ScrollMouseMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/ScrollMouseMacroAction.ts @@ -33,7 +33,7 @@ export class ScrollMouseMacroAction extends MacroAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { macroActionType: macroActionType.ScrollMouseMacroAction, x: this.x, @@ -41,7 +41,7 @@ export class ScrollMouseMacroAction extends MacroAction { }; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { buffer.writeUInt8(MacroActionId.ScrollMouseMacroAction); buffer.writeInt16(this.x); buffer.writeInt16(this.y); diff --git a/src/config-serializer/config-items/macro-action/TextMacroAction.ts b/src/config-serializer/config-items/macro-action/TextMacroAction.ts index 7c98268f..d49b76e1 100644 --- a/src/config-serializer/config-items/macro-action/TextMacroAction.ts +++ b/src/config-serializer/config-items/macro-action/TextMacroAction.ts @@ -25,14 +25,14 @@ export class TextMacroAction extends MacroAction { return this; } - _toJsonObject(): any { + toJsonObject(): any { return { macroActionType: macroActionType.TextMacroAction, text: this.text }; } - _toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer) { buffer.writeUInt8(MacroActionId.TextMacroAction); buffer.writeString(this.text); } diff --git a/test-serializer/test-serializer.ts b/test-serializer/test-serializer.ts index d361b048..b150a260 100644 --- a/test-serializer/test-serializer.ts +++ b/test-serializer/test-serializer.ts @@ -1,5 +1,4 @@ import { UhkConfiguration } from '../src/config-serializer/config-items/UhkConfiguration'; -import { Serializable } from '../src/config-serializer/Serializable'; import { UhkBuffer } from '../src/config-serializer/UhkBuffer'; let assert = require('assert'); @@ -8,7 +7,7 @@ let fs = require('fs'); let uhkConfig = JSON.parse(fs.readFileSync('../src/config-serializer/uhk-config.json')); let config1Js = uhkConfig; -let config1Ts: Serializable = new UhkConfiguration().fromJsonObject(config1Js); +let config1Ts: UhkConfiguration = new UhkConfiguration().fromJsonObject(config1Js); let config1Buffer = new UhkBuffer(); config1Ts.toBinary(config1Buffer); let config1BufferContent = config1Buffer.getBufferContent();