Remove macro ids from JSON and binary representation
This commit is contained in:
committed by
József Farkas
parent
e40dbb83b0
commit
cc9081fe81
@@ -12,6 +12,10 @@ export class UhkBuffer {
|
||||
private buffer: Buffer;
|
||||
private bytesToBacktrack: number;
|
||||
|
||||
static simpleElementWriter<T>(buffer: UhkBuffer, element: T): void {
|
||||
(<any>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<T>(elementReader: (buffer: UhkBuffer) => T): T[] {
|
||||
readArray<T>(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<T>(array: T[]): void {
|
||||
this.writeCompactLength(array.length);
|
||||
for (let element of array) {
|
||||
(<any>element).toBinary(this); // TODO: Remove any
|
||||
writeArray<T>(
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<Layer>(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 {
|
||||
|
||||
@@ -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<Module>(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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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[<string>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 {
|
||||
|
||||
@@ -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<ModuleConfiguration>(uhkBuffer => {
|
||||
return new ModuleConfiguration().fromBinary(uhkBuffer);
|
||||
});
|
||||
this.macros = buffer.readArray<Macro>(uhkBuffer => new Macro().fromBinary(uhkBuffer));
|
||||
this.keymaps = buffer.readArray<Keymap>(uhkBuffer => new Keymap().fromBinary(uhkBuffer));
|
||||
this.macros = buffer.readArray<Macro>((uhkBuffer, index) => {
|
||||
const macro = new Macro().fromBinary(uhkBuffer);
|
||||
macro.id = index;
|
||||
return macro;
|
||||
});
|
||||
this.keymaps = buffer.readArray<Keymap>(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);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
/// <reference path="../../Function.d.ts" />
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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}"`;
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@
|
||||
"playMacro"
|
||||
]
|
||||
},
|
||||
"macroId": {
|
||||
"macroIndex": {
|
||||
"type": "integer"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -758,10 +758,7 @@
|
||||
"keyActionType": "mouse",
|
||||
"mouseAction": "scrollDown"
|
||||
},
|
||||
{
|
||||
"keyActionType": "playMacro",
|
||||
"macroId": 0
|
||||
},
|
||||
null,
|
||||
{
|
||||
"keyActionType": "switchKeymap",
|
||||
"keymapAbbreviation": "QTY"
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user