Add readArray and WriteArray to UhkBuffer
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
import { Serializable } from './Serializable';
|
||||||
|
|
||||||
export class UhkBuffer {
|
export class UhkBuffer {
|
||||||
|
|
||||||
private static eepromSize = 32 * 1024;
|
private static eepromSize = 32 * 1024;
|
||||||
@@ -151,6 +153,23 @@ export class UhkBuffer {
|
|||||||
this.writeUInt8(bool ? 1 : 0);
|
this.writeUInt8(bool ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See: How to create a new object from type parameter in generic class in typescript? http://stackoverflow.com/q/17382143
|
||||||
|
readArray<T extends Serializable<T>>(type: { new (): T }): T[] {
|
||||||
|
let array: T[] = [];
|
||||||
|
let length = this.readCompactLength();
|
||||||
|
for (let i = 0; i < length; ++i) {
|
||||||
|
array.push(new type().fromBinary(this));
|
||||||
|
}
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeArray<T extends Serializable<T>>(array: T[]): void {
|
||||||
|
this.writeCompactLength(array.length);
|
||||||
|
for (let element of array) {
|
||||||
|
element.toBinary(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
backtrack(): void {
|
backtrack(): void {
|
||||||
this.offset -= this.bytesToBacktrack;
|
this.offset -= this.bytesToBacktrack;
|
||||||
this.bytesToBacktrack = 0;
|
this.bytesToBacktrack = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user