layer and layers
This commit is contained in:
25
config-serializer/config-items/Layer.ts
Normal file
25
config-serializer/config-items/Layer.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
class Layer extends Serializable<Layer> {
|
||||
|
||||
modules: Serializable<Modules>;
|
||||
|
||||
_fromJsObject(jsObject: any): Layer {
|
||||
this.modules = new Modules().fromJsObject(jsObject.modules);
|
||||
return this;
|
||||
}
|
||||
|
||||
_fromBinary(buffer: UhkBuffer): Layer {
|
||||
this.modules = new Modules().fromBinary(buffer);
|
||||
return this;
|
||||
}
|
||||
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
modules: this.modules.toJsObject()
|
||||
};
|
||||
}
|
||||
|
||||
_toBinary(buffer: UhkBuffer): void {
|
||||
this.modules.toBinary(buffer);
|
||||
}
|
||||
|
||||
}
|
||||
11
config-serializer/config-items/Layers.ts
Normal file
11
config-serializer/config-items/Layers.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
class Layers extends ClassArray<Layer> {
|
||||
|
||||
jsObjectToClass(jsObject: any): Serializable<Layer> {
|
||||
return new Layer().fromJsObject(jsObject);
|
||||
}
|
||||
|
||||
binaryToClass(buffer: UhkBuffer): Serializable<Layer> {
|
||||
return new Layer().fromBinary(buffer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
enum Layer {
|
||||
enum KeyLayer {
|
||||
mod,
|
||||
fn,
|
||||
mouse
|
||||
@@ -10,12 +10,12 @@ class SwitchLayerAction extends KeyAction {
|
||||
|
||||
isLayerToggleable: boolean;
|
||||
|
||||
// @assertEnum(Layer)
|
||||
private layer: Layer;
|
||||
// @assertEnum(KeyLayer)
|
||||
private layer: KeyLayer;
|
||||
|
||||
_fromJsObject(jsObject: any): SwitchLayerAction {
|
||||
this.assertKeyActionType(jsObject);
|
||||
this.layer = Layer[<string> jsObject.layer];
|
||||
this.layer = KeyLayer[<string> jsObject.layer];
|
||||
this.isLayerToggleable = jsObject.toggle;
|
||||
return this;
|
||||
}
|
||||
@@ -31,7 +31,7 @@ class SwitchLayerAction extends KeyAction {
|
||||
_toJsObject(): any {
|
||||
return {
|
||||
keyActionType: keyActionType.SwitchLayerAction,
|
||||
layer: Layer[this.layer],
|
||||
layer: KeyLayer[this.layer],
|
||||
toggle: this.isLayerToggleable
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,3 +11,5 @@
|
||||
/// <reference path="NoneAction.ts" />
|
||||
/// <reference path="Module.ts" />
|
||||
/// <reference path="Modules.ts" />
|
||||
/// <reference path="Layer.ts" />
|
||||
/// <reference path="Layers.ts" />
|
||||
|
||||
@@ -10,31 +10,31 @@ let fs = require('fs');
|
||||
|
||||
let uhkConfig = JSON.parse(fs.readFileSync('uhk-config.json'));
|
||||
|
||||
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);
|
||||
let config1Js = uhkConfig.keymaps[0].layers;
|
||||
let config1Ts: Serializable<Layers> = new Layers().fromJsObject(config1Js);
|
||||
let config1Buffer = new UhkBuffer();
|
||||
config1Ts.toBinary(config1Buffer);
|
||||
let config1BufferContent = config1Buffer.getBufferContent();
|
||||
fs.writeFileSync('uhk-config.bin', config1BufferContent);
|
||||
|
||||
modules1Buffer.offset = 0;
|
||||
config1Buffer.offset = 0;
|
||||
console.log();
|
||||
let modules2Ts = new Modules().fromBinary(modules1Buffer);
|
||||
let config2Ts = new Layers().fromBinary(config1Buffer);
|
||||
console.log('\n');
|
||||
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);
|
||||
let config2Js = config2Ts.toJsObject();
|
||||
let config2Buffer = new UhkBuffer();
|
||||
config2Ts.toBinary(config2Buffer);
|
||||
fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(config2Js, undefined, 4));
|
||||
let config2BufferContent = config1Buffer.getBufferContent();
|
||||
fs.writeFileSync('uhk-config-serialized.bin', config2BufferContent);
|
||||
|
||||
console.log('\n');
|
||||
try {
|
||||
assert.deepEqual(modules1Js, modules2Js);
|
||||
assert.deepEqual(config1Js, config2Js);
|
||||
console.log('JSON configurations are identical.');
|
||||
} catch (error) {
|
||||
console.log('JSON configurations differ.');
|
||||
}
|
||||
|
||||
let buffersContentsAreEqual = Buffer.compare(modules1BufferContent, modules2BufferContent) === 0;
|
||||
let buffersContentsAreEqual = Buffer.compare(config1BufferContent, config2BufferContent) === 0;
|
||||
console.log('Binary configurations ' + (buffersContentsAreEqual ? 'are identical' : 'differ') + '.');
|
||||
|
||||
Reference in New Issue
Block a user