This commit is contained in:
Sam Rang
2016-04-15 09:17:28 -05:00
6 changed files with 708 additions and 821 deletions

View File

@@ -14,7 +14,6 @@ abstract class ClassArray<T> extends Serializable<T> {
let arrayLength = buffer.readCompactLength();
if (buffer.enableDump) {
process.stdout.write(']\n');
buffer.enableDump = false;
}
@@ -36,7 +35,6 @@ abstract class ClassArray<T> extends Serializable<T> {
buffer.writeCompactLength(this.elements.length);
if (buffer.enableDump) {
process.stdout.write(']\n');
buffer.enableDump = false;
}

View File

@@ -5,58 +5,43 @@ abstract class Serializable<T> {
private static enableDump = true;
fromJsObject(jsObject: any): Serializable<T> {
let isArray = this instanceof ClassArray;
this.dumpMethodName('fromJsObject');
this.dump(`${this.strintifyJsObject(jsObject)}` + (isArray ? '\n' : ` => `));
this.dump(`${this.getIndentation()}${this.constructor.name}.fromJsObject: ` +
`${this.strintifyJsObject(jsObject)}\n`);
Serializable.depth++;
let value = this._fromJsObject(jsObject);
Serializable.depth--;
if (!isArray) {
this.dump(`${value}\n`);
}
this.dump(`${this.getIndentation()}=> ${value}\n`);
return value;
}
fromBinary(buffer: UhkBuffer): Serializable<T> {
let isArray = this instanceof ClassArray;
this.dumpMethodName('fromBinary');
this.dump('[');
this.dump(`\n${this.getIndentation()}${this.constructor.name}.fromBinary: [`);
Serializable.depth++;
buffer.enableDump = Serializable.enableDump;
let value = this._fromBinary(buffer);
buffer.enableDump = false;
Serializable.depth--;
if (!isArray) {
this.dump(`] => ${value}\n`);
}
this.dump(`]\n${this.getIndentation()}=> ${value}`);
return value;
}
toJsObject(): any {
let isArray = this instanceof ClassArray;
this.dumpMethodName('toJsObject');
this.dump(`${this}` + (isArray ? '\n' : ` => `));
this.dump(`${this.getIndentation()}${this.constructor.name}.toJsObject: ${this}\n`);
Serializable.depth++;
let value = this._toJsObject();
Serializable.depth--;
if (!isArray) {
this.dump(`${this.strintifyJsObject(value)}\n`);
}
this.dump(`${this.getIndentation()}=> ${this.strintifyJsObject(value)}\n`);
return value;
}
toBinary(buffer: UhkBuffer): void {
let isArray = this instanceof ClassArray;
this.dumpMethodName('toBinary');
this.dump(`${this} => ['`);
this.dump(`\n${this.getIndentation()}${this.constructor.name}.toBinary: ${this} [`);
Serializable.depth++;
buffer.enableDump = Serializable.enableDump;
let value = this._toBinary(buffer);
buffer.enableDump = false;
Serializable.depth--;
if (!isArray) {
this.dump(`]\n`);
}
this.dump(`]`);
return value;
}
@@ -71,9 +56,8 @@ abstract class Serializable<T> {
}
}
private dumpMethodName(methodName: string) {
let indentation = new Array(Serializable.depth + 1).join(' ');
this.dump(`${indentation}${this.constructor.name}.${methodName}: `);
private getIndentation() {
return new Array(Serializable.depth + 1).join(' ');
}
private strintifyJsObject(jsObject: any): string {

View File

@@ -1,4 +1,5 @@
class UhkBuffer {
private static eepromSize = 32 * 1024;
private static maxCompactLength = 0xFFFF;
private static longCompactLengthPrefix = 0xFF;
@@ -156,21 +157,25 @@ class UhkBuffer {
}
set enableDump(value) {
UhkBuffer.isFirstElementToDump = true;
if (value) {
UhkBuffer.isFirstElementToDump = true;
}
this._enableDump = value;
}
dump(value) {
if (this.enableDump) {
if (!UhkBuffer.isFirstElementToDump) {
process.stdout.write(', ');
}
process.stdout.write(value);
if (UhkBuffer.isFirstElementToDump) {
UhkBuffer.isFirstElementToDump = false;
}
} else {
UhkBuffer.isFirstElementToDump = true;
if (!this.enableDump) {
return;
}
if (!UhkBuffer.isFirstElementToDump) {
process.stdout.write(', ');
}
process.stdout.write(value);
if (UhkBuffer.isFirstElementToDump) {
UhkBuffer.isFirstElementToDump = false;
}
}
}

View File

@@ -18,7 +18,9 @@ let keyActions1BufferContent = keyActions1Buffer.getBufferContent();
fs.writeFileSync('uhk-config.bin', keyActions1BufferContent);
keyActions1Buffer.offset = 0;
console.log();
let keyActions2Ts = new KeyActions().fromBinary(keyActions1Buffer);
console.log('\n');
let keyActions2Js = keyActions2Ts.toJsObject();
let keyActions2Buffer = new UhkBuffer();
keyActions2Ts.toBinary(keyActions2Buffer);
@@ -45,6 +47,7 @@ fs.writeFileSync('uhk-config-serialized.json', JSON.stringify(modules2Js, undefi
let modules2BufferContent = modules1Buffer.getBufferContent();
fs.writeFileSync('uhk-config-serialized.bin', modules2BufferContent);
console.log('\n');
try {
/* wanted to also compare class->json & class->binary->class->json with original json */
assert.deepEqual(modulesbaseJs, modules1Js);

File diff suppressed because it is too large Load Diff

Before

Width:  |  Height:  |  Size: 56 KiB

After

Width:  |  Height:  |  Size: 63 KiB

File diff suppressed because it is too large Load Diff

Before

Width:  |  Height:  |  Size: 84 KiB

After

Width:  |  Height:  |  Size: 91 KiB