Dump Serializable.fromBinary()
This commit is contained in:
@@ -7,20 +7,34 @@ abstract class Serializable<T> {
|
|||||||
private static depth = 0;
|
private static depth = 0;
|
||||||
|
|
||||||
fromJsObject(jsObject: any): T {
|
fromJsObject(jsObject: any): T {
|
||||||
let identation = new Array(Serializable.depth + 1).join(' ');
|
let indentation = new Array(Serializable.depth + 1).join(' ');
|
||||||
let isArray = this instanceof UhkArray;
|
let isArray = this instanceof UhkArray;
|
||||||
process.stdout.write(`${identation}* ${this.constructor.name}.fromJsObject(${JSON.stringify(jsObject)}) ` + (isArray ? ':\n' : `=> `));
|
process.stdout.write(`${indentation}${this.constructor.name}.fromJsObject: ${JSON.stringify(jsObject)}` + (isArray ? '\n' : ` => `));
|
||||||
Serializable.depth++;
|
Serializable.depth++;
|
||||||
let value = this._fromJsObject(jsObject);
|
let value = this._fromJsObject(jsObject);
|
||||||
Serializable.depth--;
|
Serializable.depth--;
|
||||||
if (!isArray) {
|
if (!isArray) {
|
||||||
process.stdout.write(`${value.toString()}\n`);
|
process.stdout.write(`${value}\n`);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
fromBinary(buffer: UhkBuffer): T {
|
fromBinary(buffer: UhkBuffer): T {
|
||||||
return this._fromBinary(buffer);
|
let indentation = new Array(Serializable.depth + 1).join(' ');
|
||||||
|
let isArray = this instanceof UhkArray;
|
||||||
|
process.stdout.write(`${indentation}${this.constructor.name}.fromBinary: `);
|
||||||
|
if (isArray) {
|
||||||
|
process.stdout.write('\n');
|
||||||
|
}
|
||||||
|
Serializable.depth++;
|
||||||
|
buffer.enableDump = !isArray;
|
||||||
|
let value = this._fromBinary(buffer);
|
||||||
|
buffer.enableDump = false;
|
||||||
|
Serializable.depth--;
|
||||||
|
if (!isArray) {
|
||||||
|
process.stdout.write(`=> ${value}\n`);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJsObject(): any {
|
toJsObject(): any {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ class UhkBuffer {
|
|||||||
private static stringEncoding = 'utf8';
|
private static stringEncoding = 'utf8';
|
||||||
|
|
||||||
offset: number;
|
offset: number;
|
||||||
|
enableDump = false;
|
||||||
|
|
||||||
private buffer: Buffer;
|
private buffer: Buffer;
|
||||||
private bytesToBacktrack: number;
|
private bytesToBacktrack: number;
|
||||||
@@ -32,12 +33,14 @@ class UhkBuffer {
|
|||||||
let value = this.buffer.readUInt8(this.offset);
|
let value = this.buffer.readUInt8(this.offset);
|
||||||
this.bytesToBacktrack = 1;
|
this.bytesToBacktrack = 1;
|
||||||
this.offset += this.bytesToBacktrack;
|
this.offset += this.bytesToBacktrack;
|
||||||
|
this.dump(`uint8(${value}) `);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
writeUInt8(value: number): void {
|
writeUInt8(value: number): void {
|
||||||
this.buffer.writeUInt8(value, this.offset);
|
this.buffer.writeUInt8(value, this.offset);
|
||||||
this.offset += 1;
|
this.offset += 1;
|
||||||
|
this.dump(`uint8(${value}) `);
|
||||||
}
|
}
|
||||||
|
|
||||||
readInt16(): number {
|
readInt16(): number {
|
||||||
@@ -134,4 +137,10 @@ class UhkBuffer {
|
|||||||
getBufferContent(): Buffer {
|
getBufferContent(): Buffer {
|
||||||
return this.buffer.slice(0, this.offset);
|
return this.buffer.slice(0, this.offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dump(value) {
|
||||||
|
if (this.enableDump) {
|
||||||
|
process.stdout.write(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user