1
config-serializer/.gitignore
vendored
1
config-serializer/.gitignore
vendored
@@ -3,3 +3,4 @@ uhk-config.bin
|
||||
uhk-config-serialized.json
|
||||
uhk-config-serialized.bin
|
||||
test-serializer.js
|
||||
*.sw*
|
||||
|
||||
@@ -14,10 +14,10 @@ enum LongPressAction {
|
||||
|
||||
class DualRoleKeystrokeAction extends KeyAction {
|
||||
|
||||
@assertUInt8
|
||||
// @assertUInt8
|
||||
scancode: number;
|
||||
|
||||
@assertEnum(LongPressAction)
|
||||
// @assertEnum(LongPressAction)
|
||||
private longPressAction: LongPressAction;
|
||||
|
||||
_fromJsObject(jsObject: any): DualRoleKeystrokeAction {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class KeystrokeAction extends KeyAction {
|
||||
|
||||
@assertUInt8
|
||||
// @assertUInt8
|
||||
scancode: number;
|
||||
|
||||
_fromJsObject(jsObject: any): KeystrokeAction {
|
||||
|
||||
@@ -11,7 +11,7 @@ enum KeyModifiers {
|
||||
|
||||
class KeystrokeModifiersAction extends KeyAction {
|
||||
|
||||
@assertUInt8
|
||||
// @assertUInt8
|
||||
modifierMask: number;
|
||||
|
||||
_fromJsObject(jsObject: any): KeystrokeModifiersAction {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
class KeystrokeWithModifiersAction extends KeyAction {
|
||||
|
||||
@assertUInt8
|
||||
// @assertUInt8
|
||||
modifierMask: number;
|
||||
|
||||
@assertUInt8
|
||||
// @assertUInt8
|
||||
scancode: number;
|
||||
|
||||
_fromJsObject(jsObject: any): KeystrokeWithModifiersAction {
|
||||
|
||||
49
config-serializer/config-items/Module.ts
Normal file
49
config-serializer/config-items/Module.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
enum PointerRole {
|
||||
none,
|
||||
move,
|
||||
scroll
|
||||
}
|
||||
|
||||
class Module extends Serializable<Module> {
|
||||
|
||||
// @assertUInt8
|
||||
id: number;
|
||||
|
||||
keyActions: Serializable<KeyActions>;
|
||||
|
||||
// @assertEnum(PointerRole)
|
||||
private pointerRole: PointerRole;
|
||||
|
||||
_fromJsObject(jsObject: any): Module {
|
||||
this.id = jsObject.id;
|
||||
this.pointerRole = PointerRole[<string> jsObject.pointerRole];
|
||||
this.keyActions = new KeyActions().fromJsObject(jsObject.keyActions);
|
||||
return this;
|
||||
}
|
||||
|
||||
_fromBinary(buffer: UhkBuffer): Module {
|
||||
this.id = buffer.readUInt8();
|
||||
this.pointerRole = buffer.readUInt8();
|
||||
this.keyActions = new KeyActions().fromBinary(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
id: this.id,
|
||||
pointerRole: PointerRole[this.pointerRole],
|
||||
keyActions: this.keyActions.toJsObject()
|
||||
};
|
||||
}
|
||||
|
||||
_toBinary(buffer: UhkBuffer): void {
|
||||
buffer.writeUInt8(this.id);
|
||||
buffer.writeUInt8(this.pointerRole);
|
||||
this.keyActions.toBinary(buffer);
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return `<Module id="${this.id}" pointerRole="${this.pointerRole}">`;
|
||||
}
|
||||
|
||||
}
|
||||
11
config-serializer/config-items/Modules.ts
Normal file
11
config-serializer/config-items/Modules.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
class Modules extends ClassArray<Module> {
|
||||
|
||||
jsObjectToClass(jsObject: any): Serializable<Module> {
|
||||
return new Module().fromJsObject(jsObject);
|
||||
}
|
||||
|
||||
binaryToClass(buffer: UhkBuffer): Serializable<Module> {
|
||||
return new Module().fromBinary(buffer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,7 +16,7 @@ enum MouseActionParam {
|
||||
|
||||
class MouseAction extends KeyAction {
|
||||
|
||||
@assertUInt8
|
||||
// @assertUInt8
|
||||
mouseAction: MouseActionParam;
|
||||
|
||||
_fromJsObject(jsObject: any): MouseAction {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class PlayMacroAction extends KeyAction {
|
||||
|
||||
@assertUInt8
|
||||
// @assertUInt8
|
||||
macroId: number;
|
||||
|
||||
_fromJsObject(jsObject: any): PlayMacroAction {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class SwitchKeymapAction extends KeyAction {
|
||||
|
||||
@assertUInt8
|
||||
// @assertUInt8
|
||||
keymapId: number;
|
||||
|
||||
_fromJsObject(jsObject: any): SwitchKeymapAction {
|
||||
|
||||
@@ -10,7 +10,7 @@ class SwitchLayerAction extends KeyAction {
|
||||
|
||||
isLayerToggleable: boolean;
|
||||
|
||||
@assertEnum(Layer)
|
||||
// @assertEnum(Layer)
|
||||
private layer: Layer;
|
||||
|
||||
_fromJsObject(jsObject: any): SwitchLayerAction {
|
||||
|
||||
@@ -9,3 +9,5 @@
|
||||
/// <reference path="SwitchLayerAction.ts" />
|
||||
/// <reference path="SwitchKeymapAction.ts" />
|
||||
/// <reference path="NoneAction.ts" />
|
||||
/// <reference path="Module.ts" />
|
||||
/// <reference path="Modules.ts" />
|
||||
|
||||
@@ -10,31 +10,31 @@ let fs = require('fs');
|
||||
|
||||
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
|
||||
|
||||
let keyActions1Js = uhkConfig.keymaps[0].layers[0].modules[0].keyActions;
|
||||
let keyActions1Ts: Serializable<KeyActions> = new KeyActions().fromJsObject(keyActions1Js);
|
||||
let keyActions1Buffer = new UhkBuffer();
|
||||
keyActions1Ts.toBinary(keyActions1Buffer);
|
||||
let keyActions1BufferContent = keyActions1Buffer.getBufferContent();
|
||||
fs.writeFileSync('uhk-config.bin', keyActions1BufferContent);
|
||||
let modules1Js = uhkConfig.keymaps[0].layers[0].modules;
|
||||
let modules1Ts: Serializable<Modules> = new Modules().fromJsObject(modules1Js);
|
||||
let modules1Buffer = new UhkBuffer();
|
||||
modules1Ts.toBinary(modules1Buffer);
|
||||
let modules1BufferContent = modules1Buffer.getBufferContent();
|
||||
fs.writeFileSync('uhk-config.bin', modules1BufferContent);
|
||||
|
||||
keyActions1Buffer.offset = 0;
|
||||
modules1Buffer.offset = 0;
|
||||
console.log();
|
||||
let keyActions2Ts = new KeyActions().fromBinary(keyActions1Buffer);
|
||||
let modules2Ts = new Modules().fromBinary(modules1Buffer);
|
||||
console.log('\n');
|
||||
let keyActions2Js = keyActions2Ts.toJsObject();
|
||||
let keyActions2Buffer = new UhkBuffer();
|
||||
keyActions2Ts.toBinary(keyActions2Buffer);
|
||||
fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(keyActions2Js, undefined, 4));
|
||||
let keyActions2BufferContent = keyActions1Buffer.getBufferContent();
|
||||
fs.writeFileSync('uhk-config-serialized.bin', keyActions2BufferContent);
|
||||
let modules2Js = modules2Ts.toJsObject();
|
||||
let modules2Buffer = new UhkBuffer();
|
||||
modules2Ts.toBinary(modules2Buffer);
|
||||
fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(modules2Js, undefined, 4));
|
||||
let modules2BufferContent = modules1Buffer.getBufferContent();
|
||||
fs.writeFileSync('uhk-config-serialized.bin', modules2BufferContent);
|
||||
|
||||
console.log('\n');
|
||||
try {
|
||||
assert.deepEqual(keyActions1Js, keyActions2Js);
|
||||
assert.deepEqual(modules1Js, modules2Js);
|
||||
console.log('JSON configurations are identical.');
|
||||
} catch (error) {
|
||||
console.log('JSON configurations differ.');
|
||||
}
|
||||
|
||||
let buffersContentsAreEqual = Buffer.compare(keyActions1BufferContent, keyActions2BufferContent) === 0;
|
||||
let buffersContentsAreEqual = Buffer.compare(modules1BufferContent, modules2BufferContent) === 0;
|
||||
console.log('Binary configurations ' + (buffersContentsAreEqual ? 'are identical' : 'differ') + '.');
|
||||
|
||||
Reference in New Issue
Block a user