Move the gist of KeyActions to UhkArray and rename it as ClassArray. Make KeyActions descend from ClassArray. Improve array buffer dumping.
This commit is contained in:
47
config-serializer/ClassArray.ts
Normal file
47
config-serializer/ClassArray.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
abstract class ClassArray<T> extends Serializable<T> {
|
||||||
|
|
||||||
|
elements: Serializable<T>[] = [];
|
||||||
|
|
||||||
|
_fromJsObject(jsObjects: any): Serializable<T> {
|
||||||
|
for (let jsObject of jsObjects) {
|
||||||
|
this.elements.push(this.jsObjectToClass(jsObject));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
_fromBinary(buffer: UhkBuffer): Serializable<T> {
|
||||||
|
let arrayLength = buffer.readCompactLength();
|
||||||
|
for (let i = 0; i < arrayLength; i++) {
|
||||||
|
this.elements.push(this.binaryToClass(buffer));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
_toJsObject(): any {
|
||||||
|
let array = [];
|
||||||
|
for (let element of this.elements) {
|
||||||
|
array.push(element.toJsObject());
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
_toBinary(buffer: UhkBuffer) {
|
||||||
|
buffer.writeCompactLength(this.elements.length);
|
||||||
|
|
||||||
|
if (buffer.enableDump) {
|
||||||
|
process.stdout.write(']\n');
|
||||||
|
buffer.enableDump = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let element of this.elements) {
|
||||||
|
element.toBinary(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toString(): string {
|
||||||
|
return `<${this.constructor.name} length="${this.elements.length}">`;
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract jsObjectToClass(jsObject: any): Serializable<T>;
|
||||||
|
abstract binaryToClass(buffer: UhkBuffer): Serializable<T>;
|
||||||
|
}
|
||||||
@@ -14,9 +14,9 @@ abstract class Serializable<T> {
|
|||||||
: json;
|
: json;
|
||||||
}
|
}
|
||||||
|
|
||||||
fromJsObject(jsObject: any): T {
|
fromJsObject(jsObject: any): Serializable<T> {
|
||||||
let indentation = new Array(Serializable.depth + 1).join(' ');
|
let indentation = new Array(Serializable.depth + 1).join(' ');
|
||||||
let isArray = this instanceof UhkArray;
|
let isArray = this instanceof ClassArray;
|
||||||
process.stdout.write(`${indentation}${this.constructor.name}.fromJsObject: ${this.strintifyJsObject(jsObject)}` + (isArray ? '\n' : ` => `));
|
process.stdout.write(`${indentation}${this.constructor.name}.fromJsObject: ${this.strintifyJsObject(jsObject)}` + (isArray ? '\n' : ` => `));
|
||||||
Serializable.depth++;
|
Serializable.depth++;
|
||||||
let value = this._fromJsObject(jsObject);
|
let value = this._fromJsObject(jsObject);
|
||||||
@@ -27,9 +27,9 @@ abstract class Serializable<T> {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
fromBinary(buffer: UhkBuffer): T {
|
fromBinary(buffer: UhkBuffer): Serializable<T> {
|
||||||
let indentation = new Array(Serializable.depth + 1).join(' ');
|
let indentation = new Array(Serializable.depth + 1).join(' ');
|
||||||
let isArray = this instanceof UhkArray;
|
let isArray = this instanceof ClassArray;
|
||||||
process.stdout.write(`${indentation}${this.constructor.name}.fromBinary:` + (isArray ? '\n' : ' ['));
|
process.stdout.write(`${indentation}${this.constructor.name}.fromBinary:` + (isArray ? '\n' : ' ['));
|
||||||
Serializable.depth++;
|
Serializable.depth++;
|
||||||
buffer.enableDump = !isArray;
|
buffer.enableDump = !isArray;
|
||||||
@@ -44,7 +44,7 @@ abstract class Serializable<T> {
|
|||||||
|
|
||||||
toJsObject(): any {
|
toJsObject(): any {
|
||||||
let indentation = new Array(Serializable.depth + 1).join(' ');
|
let indentation = new Array(Serializable.depth + 1).join(' ');
|
||||||
let isArray = this instanceof UhkArray;
|
let isArray = this instanceof ClassArray;
|
||||||
process.stdout.write(`${indentation}${this.constructor.name}.toJsObject: ${this}` + (isArray ? '\n' : ` => `));
|
process.stdout.write(`${indentation}${this.constructor.name}.toJsObject: ${this}` + (isArray ? '\n' : ` => `));
|
||||||
Serializable.depth++;
|
Serializable.depth++;
|
||||||
let value = this._toJsObject();
|
let value = this._toJsObject();
|
||||||
@@ -57,10 +57,10 @@ abstract class Serializable<T> {
|
|||||||
|
|
||||||
toBinary(buffer: UhkBuffer): void {
|
toBinary(buffer: UhkBuffer): void {
|
||||||
let indentation = new Array(Serializable.depth + 1).join(' ');
|
let indentation = new Array(Serializable.depth + 1).join(' ');
|
||||||
let isArray = this instanceof UhkArray;
|
let isArray = this instanceof ClassArray;
|
||||||
process.stdout.write(`${indentation}${this.constructor.name}.toBinary: ${this}` + (isArray ? '\n' : ' => ['));
|
process.stdout.write(`${indentation}${this.constructor.name}.toBinary: ${this} => ['`);
|
||||||
Serializable.depth++;
|
Serializable.depth++;
|
||||||
buffer.enableDump = !isArray;
|
buffer.enableDump = true;
|
||||||
let value = this._toBinary(buffer);
|
let value = this._toBinary(buffer);
|
||||||
buffer.enableDump = false;
|
buffer.enableDump = false;
|
||||||
Serializable.depth--;
|
Serializable.depth--;
|
||||||
@@ -70,8 +70,8 @@ abstract class Serializable<T> {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract _fromJsObject(jsObject: any): T;
|
abstract _fromJsObject(jsObject: any): Serializable<T>;
|
||||||
abstract _fromBinary(buffer: UhkBuffer): T;
|
abstract _fromBinary(buffer: UhkBuffer): Serializable<T>;
|
||||||
abstract _toJsObject(): any;
|
abstract _toJsObject(): any;
|
||||||
abstract _toBinary(buffer: UhkBuffer): void;
|
abstract _toBinary(buffer: UhkBuffer): void;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
abstract class UhkArray<T> extends Serializable<T> {
|
|
||||||
abstract _fromJsObject(jsObject: any): T;
|
|
||||||
abstract _fromBinary(buffer: UhkBuffer): T;
|
|
||||||
abstract _toJsObject(): any;
|
|
||||||
abstract _toBinary(buffer: UhkBuffer): void;
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
class KeyActionFactory {
|
class KeyActionFactory implements SubclassFactory<KeyAction> {
|
||||||
|
|
||||||
static fromJsObject(jsObject: any): KeyAction {
|
fromJsObject(jsObject: any): KeyAction {
|
||||||
switch (jsObject.keyActionType) {
|
switch (jsObject.keyActionType) {
|
||||||
case KeyActionType.NoneAction:
|
case KeyActionType.NoneAction:
|
||||||
return new NoneAction().fromJsObject(jsObject);
|
return new NoneAction().fromJsObject(jsObject);
|
||||||
@@ -23,7 +23,7 @@ class KeyActionFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromBinary(buffer: UhkBuffer): KeyAction {
|
fromBinary(buffer: UhkBuffer): KeyAction {
|
||||||
let keyActionFirstByte = buffer.readUInt8();
|
let keyActionFirstByte = buffer.readUInt8();
|
||||||
buffer.backtrack();
|
buffer.backtrack();
|
||||||
|
|
||||||
|
|||||||
@@ -1,38 +1,51 @@
|
|||||||
class KeyActions extends UhkArray<KeyActions> {
|
class KeyActions extends ClassArray<KeyAction> {
|
||||||
|
|
||||||
keyActions: Serializable<KeyAction>[] = [];
|
jsObjectToClass(jsObject: any): Serializable<KeyAction> {
|
||||||
|
switch (jsObject.keyActionType) {
|
||||||
_fromJsObject(jsObjects: any): KeyActions {
|
case KeyActionType.NoneAction:
|
||||||
for (let jsObject of jsObjects) {
|
return new NoneAction().fromJsObject(jsObject);
|
||||||
this.keyActions.push(KeyActionFactory.fromJsObject(jsObject));
|
case KeyActionType.KeystrokeAction:
|
||||||
}
|
return new KeystrokeAction().fromJsObject(jsObject);
|
||||||
return this;
|
case KeyActionType.KeystrokeWithModifiersAction:
|
||||||
}
|
return new KeystrokeWithModifiersAction().fromJsObject(jsObject);
|
||||||
|
case KeyActionType.DualRoleKeystrokeAction:
|
||||||
_fromBinary(buffer: UhkBuffer): KeyActions {
|
return new DualRoleKeystrokeAction().fromJsObject(jsObject);
|
||||||
let arrayLength = buffer.readCompactLength();
|
case KeyActionType.SwitchLayerAction:
|
||||||
for (let i = 0; i < arrayLength; i++) {
|
return new SwitchLayerAction().fromJsObject(jsObject);
|
||||||
this.keyActions.push(KeyActionFactory.fromBinary(buffer));
|
case KeyActionType.SwitchKeymapAction:
|
||||||
}
|
return new SwitchKeymapAction().fromJsObject(jsObject);
|
||||||
return this;
|
case KeyActionType.MouseAction:
|
||||||
}
|
return new MouseAction().fromJsObject(jsObject);
|
||||||
|
case KeyActionType.PlayMacroAction:
|
||||||
_toJsObject(): any {
|
return new PlayMacroAction().fromJsObject(jsObject);
|
||||||
let array = [];
|
default:
|
||||||
for (let keyAction of this.keyActions) {
|
throw `Invalid KeyAction.keyActionType: "${jsObject.actionType}"`;
|
||||||
array.push(keyAction.toJsObject());
|
|
||||||
}
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
_toBinary(buffer: UhkBuffer) {
|
|
||||||
buffer.writeCompactLength(this.keyActions.length);
|
|
||||||
for (let keyAction of this.keyActions) {
|
|
||||||
keyAction.toBinary(buffer);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
toString(): string {
|
binaryToClass(buffer: UhkBuffer): Serializable<KeyAction> {
|
||||||
return `<KeyActions length="${this.keyActions.length}">`;
|
let keyActionFirstByte = buffer.readUInt8();
|
||||||
|
buffer.backtrack();
|
||||||
|
|
||||||
|
switch (keyActionFirstByte) {
|
||||||
|
case KeyActionId.NoneAction:
|
||||||
|
return new NoneAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.KeystrokeAction:
|
||||||
|
return new KeystrokeAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.KeystrokeWithModifiersAction:
|
||||||
|
return new KeystrokeWithModifiersAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.DualRoleKeystrokeAction:
|
||||||
|
return new DualRoleKeystrokeAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.SwitchLayerAction:
|
||||||
|
return new SwitchLayerAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.SwitchKeymapAction:
|
||||||
|
return new SwitchKeymapAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.MouseAction:
|
||||||
|
return new MouseAction().fromBinary(buffer);
|
||||||
|
case KeyActionId.PlayMacroAction:
|
||||||
|
return new PlayMacroAction().fromBinary(buffer);
|
||||||
|
default:
|
||||||
|
throw `Invalid KeyAction first byte: ${keyActionFirstByte}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
/// <reference path="KeyAction.ts" />
|
/// <reference path="KeyAction.ts" />
|
||||||
/// <reference path="KeyActionFactory.ts" />
|
|
||||||
/// <reference path="KeyActions.ts" />
|
/// <reference path="KeyActions.ts" />
|
||||||
/// <reference path="KeystrokeAction.ts" />
|
/// <reference path="KeystrokeAction.ts" />
|
||||||
/// <reference path="KeystrokeWithModifiersAction.ts" />
|
/// <reference path="KeystrokeWithModifiersAction.ts" />
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/// <reference path="assert.ts" />
|
/// <reference path="assert.ts" />
|
||||||
/// <reference path="TypeChecker.ts" />
|
/// <reference path="TypeChecker.ts" />
|
||||||
/// <reference path="Serializable.ts" />
|
/// <reference path="Serializable.ts" />
|
||||||
/// <reference path="UhkArray.ts" />
|
/// <reference path="ClassArray.ts" />
|
||||||
/// <reference path="UhkBuffer.ts" />
|
/// <reference path="UhkBuffer.ts" />
|
||||||
/// <reference path="config-items/config-items.ts" />
|
/// <reference path="config-items/config-items.ts" />
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ let fs = require('fs');
|
|||||||
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
|
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
|
||||||
|
|
||||||
let keyActions1Js = uhkConfig.keymaps[0].layers[0].modules[0].keyActions;
|
let keyActions1Js = uhkConfig.keymaps[0].layers[0].modules[0].keyActions;
|
||||||
let keyActions1Ts: KeyActions = new KeyActions().fromJsObject(keyActions1Js);
|
let keyActions1Ts: Serializable<KeyActions> = new KeyActions().fromJsObject(keyActions1Js);
|
||||||
let keyActions1Buffer = new UhkBuffer();
|
let keyActions1Buffer = new UhkBuffer();
|
||||||
keyActions1Ts.toBinary(keyActions1Buffer);
|
keyActions1Ts.toBinary(keyActions1Buffer);
|
||||||
let keyActions1BufferContent = keyActions1Buffer.getBufferContent();
|
let keyActions1BufferContent = keyActions1Buffer.getBufferContent();
|
||||||
|
|||||||
Reference in New Issue
Block a user