Use ids in key actions instead of references (#239)
Partially reverts e48fdea
This commit is contained in:
@@ -15,7 +15,7 @@ export class Keymap extends Serializable<Keymap> {
|
||||
|
||||
layers: Layer[];
|
||||
|
||||
constructor(keymap?: Keymap, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) {
|
||||
constructor(keymap?: Keymap) {
|
||||
super();
|
||||
if (!keymap) {
|
||||
return;
|
||||
@@ -25,25 +25,25 @@ export class Keymap extends Serializable<Keymap> {
|
||||
this.description = keymap.description;
|
||||
this.abbreviation = keymap.abbreviation;
|
||||
this.isDefault = keymap.isDefault;
|
||||
this.layers = keymap.layers.map(layer => new Layer(layer, getKeymap, getMacro));
|
||||
this.layers = keymap.layers.map(layer => new Layer(layer));
|
||||
}
|
||||
|
||||
fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Keymap {
|
||||
fromJsonObject(jsonObject: any): 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, getKeymap, getMacro));
|
||||
this.layers = jsonObject.layers.map((layer: any) => new Layer().fromJsonObject(layer));
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Keymap {
|
||||
fromBinary(buffer: UhkBuffer): 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, getKeymap, getMacro);
|
||||
return new Layer().fromBinary(uhkBuffer);
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -8,22 +8,22 @@ export class Layer extends Serializable<Layer> {
|
||||
|
||||
modules: Module[];
|
||||
|
||||
constructor(layers?: Layer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) {
|
||||
constructor(layers?: Layer) {
|
||||
super();
|
||||
if (!layers) {
|
||||
return;
|
||||
}
|
||||
this.modules = layers.modules.map(module => new Module(module, getKeymap, getMacro));
|
||||
this.modules = layers.modules.map(module => new Module(module));
|
||||
}
|
||||
|
||||
fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Layer {
|
||||
this.modules = jsonObject.modules.map((module: any) => new Module().fromJsonObject(module, getKeymap, getMacro));
|
||||
fromJsonObject(jsonObject: any): Layer {
|
||||
this.modules = jsonObject.modules.map((module: any) => new Module().fromJsonObject(module));
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Layer {
|
||||
fromBinary(buffer: UhkBuffer): Layer {
|
||||
this.modules = buffer.readArray<Module>(uhkBuffer => {
|
||||
return new Module().fromBinary(uhkBuffer, getKeymap, getMacro);
|
||||
return new Module().fromBinary(uhkBuffer);
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -21,32 +21,32 @@ export class Module extends Serializable<Module> {
|
||||
@assertEnum(PointerRole)
|
||||
pointerRole: PointerRole;
|
||||
|
||||
constructor(other?: Module, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro) {
|
||||
constructor(other?: Module) {
|
||||
super();
|
||||
if (!other) {
|
||||
return;
|
||||
}
|
||||
this.id = other.id;
|
||||
this.keyActions = other.keyActions.map(keyAction => KeyActionHelper.createKeyAction(keyAction, getKeymap, getMacro));
|
||||
this.keyActions = other.keyActions.map(keyAction => KeyActionHelper.createKeyAction(keyAction));
|
||||
this.pointerRole = other.pointerRole;
|
||||
}
|
||||
|
||||
fromJsonObject(jsonObject: any, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Module {
|
||||
fromJsonObject(jsonObject: any): Module {
|
||||
this.id = jsonObject.id;
|
||||
this.pointerRole = PointerRole[<string>jsonObject.pointerRole];
|
||||
this.keyActions = jsonObject.keyActions.map((keyAction: any) => {
|
||||
return KeyActionHelper.createKeyAction(keyAction, getKeymap, getMacro);
|
||||
return KeyActionHelper.createKeyAction(keyAction);
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer, getKeymap?: (abbrevation: string) => Keymap, getMacro?: (macroId: number) => Macro): Module {
|
||||
fromBinary(buffer: UhkBuffer): 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, getKeymap, getMacro));
|
||||
this.keyActions.push(KeyActionHelper.createKeyAction(buffer));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -40,18 +40,7 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
|
||||
return new ModuleConfiguration().fromJsonObject(moduleConfiguration);
|
||||
});
|
||||
this.macros = jsonObject.macros.map((macro: any) => new Macro().fromJsonObject(macro));
|
||||
this.keymaps = jsonObject.keymaps.map((keymap: any) => {
|
||||
const newKeymap = new Keymap();
|
||||
newKeymap.abbreviation = keymap.abbreviation;
|
||||
return newKeymap;
|
||||
});
|
||||
for (let i = 0; i < this.keymaps.length; ++i) {
|
||||
this.keymaps[i].fromJsonObject(
|
||||
jsonObject.keymaps[i],
|
||||
abbrevation => this.getKeymap(abbrevation, true),
|
||||
this.getMacro.bind(this)
|
||||
);
|
||||
}
|
||||
this.keymaps = jsonObject.keymaps.map((keymap: any) => new Keymap().fromJsonObject(keymap));
|
||||
this.epilogue = jsonObject.epilogue;
|
||||
return this;
|
||||
}
|
||||
@@ -66,10 +55,7 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
|
||||
return new ModuleConfiguration().fromBinary(uhkBuffer);
|
||||
});
|
||||
this.macros = buffer.readArray<Macro>(uhkBuffer => new Macro().fromBinary(uhkBuffer));
|
||||
this.keymaps = [];
|
||||
this.keymaps = buffer.readArray<Keymap>(uhkBuffer => {
|
||||
return new Keymap().fromBinary(uhkBuffer, abbrevation => this.getKeymap(abbrevation, true), this.getMacro.bind(this));
|
||||
});
|
||||
this.keymaps = buffer.readArray<Keymap>(uhkBuffer => new Keymap().fromBinary(uhkBuffer));
|
||||
this.epilogue = buffer.readUInt32();
|
||||
return this;
|
||||
}
|
||||
@@ -104,14 +90,8 @@ export class UhkConfiguration extends Serializable<UhkConfiguration> {
|
||||
return `<UhkConfiguration signature="${this.signature}">`;
|
||||
}
|
||||
|
||||
getKeymap(keymapAbbreviation: string, createIfNotExist = false): Keymap {
|
||||
let resultKeymap = this.keymaps.find(keymap => keymapAbbreviation === keymap.abbreviation);
|
||||
if (createIfNotExist && !resultKeymap) {
|
||||
resultKeymap = new Keymap();
|
||||
resultKeymap.abbreviation = keymapAbbreviation;
|
||||
this.keymaps.push(resultKeymap);
|
||||
}
|
||||
return resultKeymap;
|
||||
getKeymap(keymapAbbreviation: string): Keymap {
|
||||
return this.keymaps.find(keymap => keymapAbbreviation === keymap.abbreviation);
|
||||
}
|
||||
|
||||
getMacro(macroId: number): Macro {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { assertUInt8 } from '../../assert';
|
||||
import { UhkBuffer } from '../../UhkBuffer';
|
||||
import { Macro } from '../Macro';
|
||||
import { KeyAction, KeyActionId, keyActionType } from './KeyAction';
|
||||
|
||||
export class PlayMacroAction extends KeyAction {
|
||||
|
||||
macro: Macro;
|
||||
@assertUInt8
|
||||
macroId: number;
|
||||
|
||||
constructor(parameter?: PlayMacroAction | Macro) {
|
||||
super();
|
||||
@@ -12,38 +14,37 @@ export class PlayMacroAction extends KeyAction {
|
||||
return;
|
||||
}
|
||||
if (parameter instanceof PlayMacroAction) {
|
||||
this.macro = parameter.macro;
|
||||
this.macroId = parameter.macroId;
|
||||
} else {
|
||||
this.macro = parameter;
|
||||
this.macroId = parameter.id;
|
||||
}
|
||||
}
|
||||
|
||||
fromJsonObject(jsonObject: any, getMacro: (macroId: number) => Macro): PlayMacroAction {
|
||||
fromJsonObject(jsonObject: any): PlayMacroAction {
|
||||
this.assertKeyActionType(jsonObject);
|
||||
this.macro = getMacro(jsonObject.macroId);
|
||||
this.macroId = jsonObject.macroId;
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer, getMacro: (macroId: number) => Macro): PlayMacroAction {
|
||||
fromBinary(buffer: UhkBuffer): PlayMacroAction {
|
||||
this.readAndAssertKeyActionId(buffer);
|
||||
const macroId = buffer.readUInt8();
|
||||
this.macro = getMacro(macroId);
|
||||
this.macroId = buffer.readUInt8();
|
||||
return this;
|
||||
}
|
||||
|
||||
_toJsonObject(): any {
|
||||
return {
|
||||
keyActionType: keyActionType.PlayMacroAction,
|
||||
macroId: this.macro.id
|
||||
macroId: this.macroId
|
||||
};
|
||||
}
|
||||
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(KeyActionId.PlayMacroAction);
|
||||
buffer.writeUInt8(this.macro.id);
|
||||
buffer.writeUInt8(this.macroId);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<PlayMacroAction macroId="${this.macro.id}">`;
|
||||
return `<PlayMacroAction macroId="${this.macroId}">`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { KeyAction, KeyActionId, keyActionType } from './KeyAction';
|
||||
|
||||
export class SwitchKeymapAction extends KeyAction {
|
||||
|
||||
keymap: Keymap;
|
||||
keymapAbbreviation: string;
|
||||
|
||||
constructor(parameter?: SwitchKeymapAction | Keymap) {
|
||||
super();
|
||||
@@ -12,38 +12,37 @@ export class SwitchKeymapAction extends KeyAction {
|
||||
return;
|
||||
}
|
||||
if (parameter instanceof SwitchKeymapAction) {
|
||||
this.keymap = parameter.keymap;
|
||||
this.keymapAbbreviation = parameter.keymapAbbreviation;
|
||||
} else {
|
||||
this.keymap = parameter;
|
||||
this.keymapAbbreviation = parameter.abbreviation;
|
||||
}
|
||||
}
|
||||
|
||||
fromJsonObject(jsonObject: any, getKeymap: (abbrevation: string) => Keymap): SwitchKeymapAction {
|
||||
fromJsonObject(jsonObject: any): SwitchKeymapAction {
|
||||
this.assertKeyActionType(jsonObject);
|
||||
this.keymap = getKeymap(jsonObject.keymapAbbreviation);
|
||||
this.keymapAbbreviation = jsonObject.keymapAbbreviation;
|
||||
return this;
|
||||
}
|
||||
|
||||
fromBinary(buffer: UhkBuffer, getKeymap: (abbrevation: string) => Keymap): SwitchKeymapAction {
|
||||
fromBinary(buffer: UhkBuffer): SwitchKeymapAction {
|
||||
this.readAndAssertKeyActionId(buffer);
|
||||
const keymapAbbreviation = buffer.readString();
|
||||
this.keymap = getKeymap(keymapAbbreviation);
|
||||
this.keymapAbbreviation = buffer.readString();
|
||||
return this;
|
||||
}
|
||||
|
||||
_toJsonObject(): any {
|
||||
return {
|
||||
keyActionType: keyActionType.SwitchKeymapAction,
|
||||
keymapAbbreviation: this.keymap.abbreviation
|
||||
keymapAbbreviation: this.keymapAbbreviation
|
||||
};
|
||||
}
|
||||
|
||||
_toBinary(buffer: UhkBuffer) {
|
||||
buffer.writeUInt8(KeyActionId.SwitchKeymapAction);
|
||||
buffer.writeString(this.keymap.abbreviation);
|
||||
buffer.writeString(this.keymapAbbreviation);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<SwitchKeymapAction keymapAbbreviation="${this.keymap.abbreviation}">`;
|
||||
return `<SwitchKeymapAction keymapAbbreviation="${this.keymapAbbreviation}">`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,25 +15,17 @@ import { Macro } from '../Macro';
|
||||
|
||||
export class Helper {
|
||||
|
||||
static createKeyAction(
|
||||
source: KeyAction | UhkBuffer | any,
|
||||
getKeymap?: (abbrevation: string) => Keymap,
|
||||
getMacro?: (macroId: number) => Macro
|
||||
): KeyAction {
|
||||
static createKeyAction(source: KeyAction | UhkBuffer | any): KeyAction {
|
||||
if (source instanceof KeyAction) {
|
||||
return Helper.fromKeyAction(source);
|
||||
} else if (source instanceof UhkBuffer) {
|
||||
return Helper.fromUhkBuffer(source, getKeymap, getMacro);
|
||||
return Helper.fromUhkBuffer(source);
|
||||
} else {
|
||||
return Helper.fromJSONObject(source, getKeymap, getMacro);
|
||||
return Helper.fromJSONObject(source);
|
||||
}
|
||||
}
|
||||
|
||||
private static fromUhkBuffer(
|
||||
buffer: UhkBuffer,
|
||||
getKeymap?: (abbrevation: string) => Keymap,
|
||||
getMacro?: (macroId: number) => Macro
|
||||
): KeyAction {
|
||||
private static fromUhkBuffer(buffer: UhkBuffer): KeyAction {
|
||||
let keyActionFirstByte = buffer.readUInt8();
|
||||
buffer.backtrack();
|
||||
|
||||
@@ -48,11 +40,11 @@ export class Helper {
|
||||
case KeyActionId.SwitchLayerAction:
|
||||
return new SwitchLayerAction().fromBinary(buffer);
|
||||
case KeyActionId.SwitchKeymapAction:
|
||||
return new SwitchKeymapAction().fromBinary(buffer, getKeymap);
|
||||
return new SwitchKeymapAction().fromBinary(buffer);
|
||||
case KeyActionId.MouseAction:
|
||||
return new MouseAction().fromBinary(buffer);
|
||||
case KeyActionId.PlayMacroAction:
|
||||
return new PlayMacroAction().fromBinary(buffer, getMacro);
|
||||
return new PlayMacroAction().fromBinary(buffer);
|
||||
default:
|
||||
throw `Invalid KeyAction first byte: ${keyActionFirstByte}`;
|
||||
}
|
||||
@@ -74,11 +66,7 @@ export class Helper {
|
||||
return newKeyAction;
|
||||
}
|
||||
|
||||
private static fromJSONObject(
|
||||
keyAction: any,
|
||||
getKeymap?: (abbrevation: string) => Keymap,
|
||||
getMacro?: (macroId: number) => Macro
|
||||
): KeyAction {
|
||||
private static fromJSONObject(keyAction: any): KeyAction {
|
||||
if (!keyAction) {
|
||||
return;
|
||||
}
|
||||
@@ -89,11 +77,11 @@ export class Helper {
|
||||
case keyActionType.SwitchLayerAction:
|
||||
return new SwitchLayerAction().fromJsonObject(keyAction);
|
||||
case keyActionType.SwitchKeymapAction:
|
||||
return new SwitchKeymapAction().fromJsonObject(keyAction, getKeymap);
|
||||
return new SwitchKeymapAction().fromJsonObject(keyAction);
|
||||
case keyActionType.MouseAction:
|
||||
return new MouseAction().fromJsonObject(keyAction);
|
||||
case keyActionType.PlayMacroAction:
|
||||
return new PlayMacroAction().fromJsonObject(keyAction, getMacro);
|
||||
return new PlayMacroAction().fromJsonObject(keyAction);
|
||||
default:
|
||||
throw `Invalid KeyAction.keyActionType: "${keyAction.keyActionType}"`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user