UNPKG

node-insim

Version:

An InSim library for NodeJS with TypeScript support

163 lines (162 loc) 6.24 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Struct = void 0; var decorators_1 = require("../../decorators"); var errors_1 = require("../../errors"); var lfspack_1 = require("../../lfspack"); var log_1 = require("../../log"); var log = log_1.log.extend('struct'); var Struct = /** @class */ (function () { function Struct() { /** @ignore */ this.SIZE_MULTIPLIER = 4; /** All string properties in a raw format as it was received from LFS */ this._raw = {}; } Struct.prototype.initialize = function (data) { if (!data) { return; } Object.assign(this, data); }; /** @ignore */ Struct.prototype.getValidPropertyNames = function () { var _this = this; var prototype = Object.getPrototypeOf(this); var ownPropertyNames = Object.getOwnPropertyNames(this); var prototypePropertyNames = Object.keys(Object.getOwnPropertyDescriptors(prototype)); return __spreadArray(__spreadArray([], __read(ownPropertyNames), false), __read(prototypePropertyNames), false).filter(function (propertyName) { return (0, decorators_1.getFormat)(_this, propertyName) !== undefined; }); }; /** @ignore */ Struct.prototype.getFormat = function (propertyFormats) { var _this = this; var propertyNames = this.getValidPropertyNames(); return propertyNames .map(function (propertyName) { var _a; return (_a = propertyFormats === null || propertyFormats === void 0 ? void 0 : propertyFormats[propertyName]) !== null && _a !== void 0 ? _a : (0, decorators_1.getFormat)(_this, propertyName); }) .join(''); }; /** @ignore */ Struct.prototype.getFormatSize = function () { return (0, lfspack_1.determineLength)("<".concat(this.getFormat())); }; /** @ignore */ Struct.prototype.unpack = function (buffer, propertyFormatOverrides) { var _this = this; var format = this.getFormat(propertyFormatOverrides); var data = (0, lfspack_1.unpack)("<".concat(format), buffer.buffer); if (!data) { throw new errors_1.InSimError("Unpacked no data using ".concat(format, " from buffer ").concat(buffer.join())); } var propertyNames = this.getValidPropertyNames(); var i = 0; propertyNames.forEach(function (propertyName) { var value = data[i]; if (i >= data.length) { return; } var propertyType = typeof _this[propertyName]; if (value === undefined) { i++; return; } if (propertyName === 'Size') { _this[propertyName] = value * _this.SIZE_MULTIPLIER; i++; return; } // Tuple of [rawString, decodedString] if (Array.isArray(value) && value.length === 2) { _this[propertyName] = value[1]; _this._raw[propertyName] = value[0]; i++; return; } if (Array.isArray(value) && Array.isArray(_this[propertyName])) { _this[propertyName] = value; i++; return; } if (propertyType == 'object') { i = _this.parseObject(_this[propertyName], data, i, propertyName); return; } _this[propertyName] = value; i++; }); log('Data unpacked:', this); return this; }; Struct.prototype.parseArray = function (instance, data, i, instanceName) { for (var j = 0; j < instance.length; j++) { var item = instance[j]; if (typeof item === 'object') { i = this.parseObject(item, data, i, "".concat(instanceName, "[").concat(j, "]")); } else { instance[j] = data[i]; i++; } } return i; }; Struct.prototype.parseObject = function (instance, data, i, instanceName) { var _this = this; if (Array.isArray(instance)) { return this.parseArray(instance, data, i, instanceName); } if (instance instanceof Struct) { var propertyNames = instance.getValidPropertyNames(); propertyNames.forEach(function (propertyName) { var propInstance = instance[propertyName]; var sval = data[i]; var propType = typeof propInstance; var fullName = "".concat(instanceName, ".").concat(propertyName); if (propType === 'object') { i = _this.parseObject(propInstance, data, i, fullName); return; } // eslint-disable-next-line @typescript-eslint/no-explicit-any instance[propertyName] = sval; i++; }); } else { // should be handled by other parts of unpack function // because instance here is a reference to the target property and setter can't be accessed i++; } return i; }; return Struct; }()); exports.Struct = Struct;