From cc9081fe81c4187d5433b9a4356d4f70d9098f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Farkas=20J=C3=B3zsef?= Date: Fri, 30 Dec 2016 15:23:24 +0100 Subject: [PATCH] Remove macro ids from JSON and binary representation --- src/config-serializer/UhkBuffer.ts | 21 +++++++++++++----- src/config-serializer/config-items/Keymap.ts | 18 ++++++++------- src/config-serializer/config-items/Layer.ts | 18 ++++++++------- src/config-serializer/config-items/Macro.ts | 4 ---- src/config-serializer/config-items/Module.ts | 18 ++++++++------- .../config-items/UhkConfiguration.ts | 22 ++++++++++++++----- .../config-items/key-action/KeyAction.ts | 6 +++-- .../key-action/PlayMacroAction.ts | 17 +++++++------- .../config-items/key-action/helper.ts | 14 ++++++------ src/config-serializer/config-schema.json | 2 +- src/config-serializer/preset-keymaps.json | 5 +---- src/config-serializer/uhk-config.json | 6 ++--- 12 files changed, 85 insertions(+), 66 deletions(-) diff --git a/src/config-serializer/UhkBuffer.ts b/src/config-serializer/UhkBuffer.ts index 48443e02..a157bdb8 100644 --- a/src/config-serializer/UhkBuffer.ts +++ b/src/config-serializer/UhkBuffer.ts @@ -12,6 +12,10 @@ export class UhkBuffer { private buffer: Buffer; private bytesToBacktrack: number; + static simpleElementWriter(buffer: UhkBuffer, element: T): void { + (element).toBinary(buffer); // TODO: Remove any + } + constructor() { this.offset = 0; this.bytesToBacktrack = 0; @@ -151,19 +155,23 @@ export class UhkBuffer { this.writeUInt8(bool ? 1 : 0); } - readArray(elementReader: (buffer: UhkBuffer) => T): T[] { + readArray(elementReader: (buffer: UhkBuffer, index?: number) => T): T[] { let array: T[] = []; let length = this.readCompactLength(); for (let i = 0; i < length; ++i) { - array.push(elementReader(this)); + array.push(elementReader(this, i)); } return array; } - writeArray(array: T[]): void { - this.writeCompactLength(array.length); - for (let element of array) { - (element).toBinary(this); // TODO: Remove any + writeArray( + array: T[], + elementWriter: (buffer: UhkBuffer, element: T, index?: number) => void = UhkBuffer.simpleElementWriter + ): void { + const length = array.length; + this.writeCompactLength(length); + for (let i = 0; i < length; ++i) { + elementWriter(this, array[i], i); } } @@ -202,4 +210,5 @@ export class UhkBuffer { UhkBuffer.isFirstElementToDump = false; } } + } diff --git a/src/config-serializer/config-items/Keymap.ts b/src/config-serializer/config-items/Keymap.ts index e27fc6ca..6b4893fa 100644 --- a/src/config-serializer/config-items/Keymap.ts +++ b/src/config-serializer/config-items/Keymap.ts @@ -26,42 +26,44 @@ export class Keymap { this.layers = keymap.layers.map(layer => new Layer(layer)); } - fromJsonObject(jsonObject: any): Keymap { + fromJsonObject(jsonObject: any, macros?: Macro[]): Keymap { this.isDefault = jsonObject.isDefault; this.abbreviation = jsonObject.abbreviation; this.name = jsonObject.name; this.description = jsonObject.description; - this.layers = jsonObject.layers.map((layer: any) => new Layer().fromJsonObject(layer)); + this.layers = jsonObject.layers.map((layer: any) => new Layer().fromJsonObject(layer, macros)); return this; } - fromBinary(buffer: UhkBuffer): Keymap { + fromBinary(buffer: UhkBuffer, macros?: Macro[]): Keymap { this.abbreviation = buffer.readString(); this.isDefault = buffer.readBoolean(); this.name = buffer.readString(); this.description = buffer.readString(); this.layers = buffer.readArray(uhkBuffer => { - return new Layer().fromBinary(uhkBuffer); + return new Layer().fromBinary(uhkBuffer, macros); }); return this; } - toJsonObject(): any { + toJsonObject(macros?: Macro[]): any { return { isDefault: this.isDefault, abbreviation: this.abbreviation, name: this.name, description: this.description, - layers: this.layers.map(layer => layer.toJsonObject()) + layers: this.layers.map(layer => layer.toJsonObject(macros)) }; } - toBinary(buffer: UhkBuffer): void { + toBinary(buffer: UhkBuffer, macros?: Macro[]): void { buffer.writeString(this.abbreviation); buffer.writeBoolean(this.isDefault); buffer.writeString(this.name); buffer.writeString(this.description); - buffer.writeArray(this.layers); + buffer.writeArray(this.layers, (uhkBuffer: UhkBuffer, layer: Layer) => { + layer.toBinary(uhkBuffer, macros); + }); } toString(): string { diff --git a/src/config-serializer/config-items/Layer.ts b/src/config-serializer/config-items/Layer.ts index ba6f178e..8f59557b 100644 --- a/src/config-serializer/config-items/Layer.ts +++ b/src/config-serializer/config-items/Layer.ts @@ -14,26 +14,28 @@ export class Layer { this.modules = layers.modules.map(module => new Module(module)); } - fromJsonObject(jsonObject: any): Layer { - this.modules = jsonObject.modules.map((module: any) => new Module().fromJsonObject(module)); + fromJsonObject(jsonObject: any, macros?: Macro[]): Layer { + this.modules = jsonObject.modules.map((module: any) => new Module().fromJsonObject(module, macros)); return this; } - fromBinary(buffer: UhkBuffer): Layer { + fromBinary(buffer: UhkBuffer, macros?: Macro[]): Layer { this.modules = buffer.readArray(uhkBuffer => { - return new Module().fromBinary(uhkBuffer); + return new Module().fromBinary(uhkBuffer, macros); }); return this; } - toJsonObject(): any { + toJsonObject(macros?: Macro[]): any { return { - modules: this.modules.map(module => module.toJsonObject()) + modules: this.modules.map(module => module.toJsonObject(macros)) }; } - toBinary(buffer: UhkBuffer): void { - buffer.writeArray(this.modules); + toBinary(buffer: UhkBuffer, macros?: Macro[]): void { + buffer.writeArray(this.modules, (uhkBuffer: UhkBuffer, module: Module) => { + module.toBinary(uhkBuffer, macros); + }); } toString(): string { diff --git a/src/config-serializer/config-items/Macro.ts b/src/config-serializer/config-items/Macro.ts index 836804ed..f4a90efe 100644 --- a/src/config-serializer/config-items/Macro.ts +++ b/src/config-serializer/config-items/Macro.ts @@ -27,7 +27,6 @@ export class Macro { } fromJsonObject(jsonObject: any): Macro { - this.id = jsonObject.id; this.isLooped = jsonObject.isLooped; this.isPrivate = jsonObject.isPrivate; this.name = jsonObject.name; @@ -36,7 +35,6 @@ export class Macro { } fromBinary(buffer: UhkBuffer): Macro { - this.id = buffer.readUInt8(); this.isLooped = buffer.readBoolean(); this.isPrivate = buffer.readBoolean(); this.name = buffer.readString(); @@ -50,7 +48,6 @@ export class Macro { toJsonObject(): any { return { - id: this.id, isLooped: this.isLooped, isPrivate: this.isPrivate, name: this.name, @@ -59,7 +56,6 @@ export class Macro { } toBinary(buffer: UhkBuffer): void { - buffer.writeUInt8(this.id); buffer.writeBoolean(this.isLooped); buffer.writeBoolean(this.isPrivate); buffer.writeString(this.name); diff --git a/src/config-serializer/config-items/Module.ts b/src/config-serializer/config-items/Module.ts index be4eaa09..39f194c5 100644 --- a/src/config-serializer/config-items/Module.ts +++ b/src/config-serializer/config-items/Module.ts @@ -29,39 +29,39 @@ export class Module { this.pointerRole = other.pointerRole; } - fromJsonObject(jsonObject: any): Module { + fromJsonObject(jsonObject: any, macros?: Macro[]): Module { this.id = jsonObject.id; this.pointerRole = PointerRole[jsonObject.pointerRole]; this.keyActions = jsonObject.keyActions.map((keyAction: any) => { - return KeyActionHelper.createKeyAction(keyAction); + return KeyActionHelper.createKeyAction(keyAction, macros); }); return this; } - fromBinary(buffer: UhkBuffer): Module { + fromBinary(buffer: UhkBuffer, macros?: Macro[]): Module { this.id = buffer.readUInt8(); this.pointerRole = buffer.readUInt8(); let keyActionsLength: number = buffer.readCompactLength(); this.keyActions = []; for (let i = 0; i < keyActionsLength; ++i) { - this.keyActions.push(KeyActionHelper.createKeyAction(buffer)); + this.keyActions.push(KeyActionHelper.createKeyAction(buffer, macros)); } return this; } - toJsonObject(): any { + toJsonObject(macros?: Macro[]): any { return { id: this.id, pointerRole: PointerRole[this.pointerRole], keyActions: this.keyActions.map(keyAction => { if (keyAction) { - return keyAction.toJsonObject(); + return keyAction.toJsonObject(macros); } }) }; } - toBinary(buffer: UhkBuffer): void { + toBinary(buffer: UhkBuffer, macros?: Macro[]): void { buffer.writeUInt8(this.id); buffer.writeUInt8(this.pointerRole); @@ -74,7 +74,9 @@ export class Module { return noneAction; }); - buffer.writeArray(keyActions); + buffer.writeArray(keyActions, (uhkBuffer: UhkBuffer, keyAction: KeyAction) => { + keyAction.toBinary(uhkBuffer, macros); + }); } toString(): string { diff --git a/src/config-serializer/config-items/UhkConfiguration.ts b/src/config-serializer/config-items/UhkConfiguration.ts index e0d76871..e5fad423 100644 --- a/src/config-serializer/config-items/UhkConfiguration.ts +++ b/src/config-serializer/config-items/UhkConfiguration.ts @@ -38,8 +38,12 @@ export class UhkConfiguration { this.moduleConfigurations = jsonObject.moduleConfigurations.map((moduleConfiguration: any) => { return new ModuleConfiguration().fromJsonObject(moduleConfiguration); }); - this.macros = jsonObject.macros.map((macro: any) => new Macro().fromJsonObject(macro)); - this.keymaps = jsonObject.keymaps.map((keymap: any) => new Keymap().fromJsonObject(keymap)); + this.macros = jsonObject.macros.map((macroJsonObject: any, index: number) => { + const macro = new Macro().fromJsonObject(macroJsonObject); + macro.id = index; + return macro; + }); + this.keymaps = jsonObject.keymaps.map((keymap: any) => new Keymap().fromJsonObject(keymap, this.macros)); this.epilogue = jsonObject.epilogue; return this; } @@ -53,8 +57,12 @@ export class UhkConfiguration { this.moduleConfigurations = buffer.readArray(uhkBuffer => { return new ModuleConfiguration().fromBinary(uhkBuffer); }); - this.macros = buffer.readArray(uhkBuffer => new Macro().fromBinary(uhkBuffer)); - this.keymaps = buffer.readArray(uhkBuffer => new Keymap().fromBinary(uhkBuffer)); + this.macros = buffer.readArray((uhkBuffer, index) => { + const macro = new Macro().fromBinary(uhkBuffer); + macro.id = index; + return macro; + }); + this.keymaps = buffer.readArray(uhkBuffer => new Keymap().fromBinary(uhkBuffer, this.macros)); this.epilogue = buffer.readUInt32(); return this; } @@ -67,7 +75,7 @@ export class UhkConfiguration { hardwareId: this.hardwareId, brandId: this.brandId, moduleConfigurations: this.moduleConfigurations.map(moduleConfiguration => moduleConfiguration.toJsonObject()), - keymaps: this.keymaps.map(keymap => keymap.toJsonObject()), + keymaps: this.keymaps.map(keymap => keymap.toJsonObject(this.macros)), macros: this.macros.map(macro => macro.toJsonObject()), epilogue: this.epilogue }; @@ -81,7 +89,9 @@ export class UhkConfiguration { buffer.writeUInt8(this.brandId); buffer.writeArray(this.moduleConfigurations); buffer.writeArray(this.macros); - buffer.writeArray(this.keymaps); + buffer.writeArray(this.keymaps, (uhkBuffer: UhkBuffer, keymap: Keymap) => { + keymap.toBinary(uhkBuffer, this.macros); + }); buffer.writeUInt32(this.epilogue); } diff --git a/src/config-serializer/config-items/key-action/KeyAction.ts b/src/config-serializer/config-items/key-action/KeyAction.ts index 0c2af432..bbcc4783 100644 --- a/src/config-serializer/config-items/key-action/KeyAction.ts +++ b/src/config-serializer/config-items/key-action/KeyAction.ts @@ -1,5 +1,6 @@ /// +import { Macro } from '../Macro'; import { UhkBuffer } from '../../UhkBuffer'; export enum KeyActionId { @@ -52,6 +53,7 @@ export abstract class KeyAction { return readKeyActionId; } - abstract toJsonObject(): any; - abstract toBinary(buffer: UhkBuffer): void; + abstract toJsonObject(macros?: Macro[]): any; + abstract toBinary(buffer: UhkBuffer, macros?: Macro[]): any; + } diff --git a/src/config-serializer/config-items/key-action/PlayMacroAction.ts b/src/config-serializer/config-items/key-action/PlayMacroAction.ts index 5986c4bf..55669d59 100644 --- a/src/config-serializer/config-items/key-action/PlayMacroAction.ts +++ b/src/config-serializer/config-items/key-action/PlayMacroAction.ts @@ -20,28 +20,29 @@ export class PlayMacroAction extends KeyAction { } } - fromJsonObject(jsonObject: any): PlayMacroAction { + fromJsonObject(jsonObject: any, macros: Macro[]): PlayMacroAction { this.assertKeyActionType(jsonObject); - this.macroId = jsonObject.macroId; + this.macroId = macros[jsonObject.macroIndex].id; return this; } - fromBinary(buffer: UhkBuffer): PlayMacroAction { + fromBinary(buffer: UhkBuffer, macros: Macro[]): PlayMacroAction { this.readAndAssertKeyActionId(buffer); - this.macroId = buffer.readUInt8(); + const macroIndex = buffer.readUInt8(); + this.macroId = macros[macroIndex].id; return this; } - toJsonObject(): any { + toJsonObject(macros: Macro[]): any { return { keyActionType: keyActionType.PlayMacroAction, - macroId: this.macroId + macroIndex: macros.findIndex(macro => macro.id === this.macroId) }; } - toBinary(buffer: UhkBuffer) { + toBinary(buffer: UhkBuffer, macros: Macro[]) { buffer.writeUInt8(KeyActionId.PlayMacroAction); - buffer.writeUInt8(this.macroId); + buffer.writeUInt8(macros.findIndex(macro => macro.id === this.macroId)); } toString(): string { diff --git a/src/config-serializer/config-items/key-action/helper.ts b/src/config-serializer/config-items/key-action/helper.ts index 3c6d8deb..93fe383f 100644 --- a/src/config-serializer/config-items/key-action/helper.ts +++ b/src/config-serializer/config-items/key-action/helper.ts @@ -15,17 +15,17 @@ import { Macro } from '../Macro'; export class Helper { - static createKeyAction(source: KeyAction | UhkBuffer | any): KeyAction { + static createKeyAction(source: KeyAction | UhkBuffer | any, macros?: Macro[]): KeyAction { if (source instanceof KeyAction) { return Helper.fromKeyAction(source); } else if (source instanceof UhkBuffer) { - return Helper.fromUhkBuffer(source); + return Helper.fromUhkBuffer(source, macros); } else { - return Helper.fromJSONObject(source); + return Helper.fromJSONObject(source, macros); } } - private static fromUhkBuffer(buffer: UhkBuffer): KeyAction { + private static fromUhkBuffer(buffer: UhkBuffer, macros: Macro[]): KeyAction { let keyActionFirstByte = buffer.readUInt8(); buffer.backtrack(); @@ -44,7 +44,7 @@ export class Helper { case KeyActionId.MouseAction: return new MouseAction().fromBinary(buffer); case KeyActionId.PlayMacroAction: - return new PlayMacroAction().fromBinary(buffer); + return new PlayMacroAction().fromBinary(buffer, macros); default: throw `Invalid KeyAction first byte: ${keyActionFirstByte}`; } @@ -66,7 +66,7 @@ export class Helper { return newKeyAction; } - private static fromJSONObject(keyAction: any): KeyAction { + private static fromJSONObject(keyAction: any, macros: Macro[]): KeyAction { if (!keyAction) { return; } @@ -81,7 +81,7 @@ export class Helper { case keyActionType.MouseAction: return new MouseAction().fromJsonObject(keyAction); case keyActionType.PlayMacroAction: - return new PlayMacroAction().fromJsonObject(keyAction); + return new PlayMacroAction().fromJsonObject(keyAction, macros); default: throw `Invalid KeyAction.keyActionType: "${keyAction.keyActionType}"`; } diff --git a/src/config-serializer/config-schema.json b/src/config-serializer/config-schema.json index 506c83fc..79188621 100644 --- a/src/config-serializer/config-schema.json +++ b/src/config-serializer/config-schema.json @@ -135,7 +135,7 @@ "playMacro" ] }, - "macroId": { + "macroIndex": { "type": "integer" } } diff --git a/src/config-serializer/preset-keymaps.json b/src/config-serializer/preset-keymaps.json index 899779cb..58376139 100644 --- a/src/config-serializer/preset-keymaps.json +++ b/src/config-serializer/preset-keymaps.json @@ -758,10 +758,7 @@ "keyActionType": "mouse", "mouseAction": "scrollDown" }, - { - "keyActionType": "playMacro", - "macroId": 0 - }, + null, { "keyActionType": "switchKeymap", "keymapAbbreviation": "QTY" diff --git a/src/config-serializer/uhk-config.json b/src/config-serializer/uhk-config.json index 6340733b..4509789e 100644 --- a/src/config-serializer/uhk-config.json +++ b/src/config-serializer/uhk-config.json @@ -1,6 +1,6 @@ { "signature": "UHK", - "dataModelVersion": 1, + "dataModelVersion": 2, "prologue": 1234678, "hardwareId": 0, "brandId": 0, @@ -774,7 +774,7 @@ }, { "keyActionType": "playMacro", - "macroId": 0 + "macroIndex": 0 }, { "keyActionType": "switchKeymap", @@ -925,7 +925,6 @@ ], "macros": [ { - "id": 0, "isLooped": false, "isPrivate": true, "name": "My address", @@ -996,7 +995,6 @@ ] }, { - "id": 1, "isLooped": true, "isPrivate": true, "name": "Blah Blah blah",