tsinsim
Version:
An InSim library for Node.js (JavaScript runtime environment) with TypeScript support.
29 lines (28 loc) • 941 B
JavaScript
import { getFormat } from "./decorators.js";
import { PacketPack } from "./pack.js";
import { PacketUnpack } from "./unpack.js";
export class Receivable {
unpack(data) {
const keys = Object.getOwnPropertyNames(this);
const vk = {};
for (const key of keys) {
vk[key] = getFormat(this, key) || { type: '', length: 0 };
}
;
Object.assign(this, { ...PacketUnpack(vk, data) });
return this;
}
}
export class Struct extends Receivable {
pack(newSize = 0) {
const keys = Object.getOwnPropertyNames(this);
const vk = {};
for (const key of keys) {
const value = Object.getOwnPropertyDescriptor(this, key)?.value;
vk[key] = { value: (key == 'Size' ? value / 4 : value), ...getFormat(this, key) || { type: '', length: 0 } };
}
return PacketPack(vk, newSize);
}
}
export class Sendable extends Struct {
}