|
| 1 | +import { TankPacket } from "./TankPacket.js"; |
| 2 | + |
| 3 | +// Types |
| 4 | +import type { VariantArg, VariantArray, VariantOptions } from "../../types"; |
| 5 | +import { VariantTypes } from "../Constants.js"; |
| 6 | + |
| 7 | +/** |
| 8 | + * Represents the Variant class. |
| 9 | + */ |
| 10 | +export class Variant { |
| 11 | + public index: number = 0; |
| 12 | + |
| 13 | + /** |
| 14 | + * Creates a new instance of the Variant class. |
| 15 | + * @param options The options for the variant. |
| 16 | + * @param args The arguments of the Variant. |
| 17 | + */ |
| 18 | + constructor(public options: VariantOptions = {}, public args: VariantArg[]) {} |
| 19 | + |
| 20 | + /** |
| 21 | + * Creates a new Variant class. |
| 22 | + * @param opts The options for the variant. |
| 23 | + * @param args The arguments of the Variant. |
| 24 | + */ |
| 25 | + public static from(opts?: VariantOptions | VariantArg, ...args: VariantArg[]) { |
| 26 | + if (typeof opts === "string" || typeof opts === "number" || Array.isArray(opts)) { |
| 27 | + args.unshift(opts); |
| 28 | + opts = { netID: -1, delay: 0 }; |
| 29 | + } |
| 30 | + |
| 31 | + return new Variant(opts as VariantOptions, args); |
| 32 | + } |
| 33 | + |
| 34 | + public static toArray(data: Buffer): VariantArray[] { |
| 35 | + let arr: VariantArray[] = []; |
| 36 | + |
| 37 | + let pos = 60; |
| 38 | + const count = data.readUint8(60); |
| 39 | + pos += 1; |
| 40 | + |
| 41 | + for (let i = 1; i <= count; i++) { |
| 42 | + const index = data.readUint8(pos); |
| 43 | + pos += 1; |
| 44 | + |
| 45 | + const type = data.readUint8(pos); |
| 46 | + const typeName = VariantTypes[type]; |
| 47 | + |
| 48 | + pos += 1; |
| 49 | + |
| 50 | + switch (type) { |
| 51 | + case VariantTypes.STRING: { |
| 52 | + const strLength = data.readUint32LE(pos); |
| 53 | + pos += 4; |
| 54 | + |
| 55 | + const value = data.subarray(pos, pos + strLength).toString(); |
| 56 | + pos += strLength; |
| 57 | + |
| 58 | + arr.push({ index, type, typeName, value }); |
| 59 | + break; |
| 60 | + } |
| 61 | + case VariantTypes.UNSIGNED_INT: { |
| 62 | + const value = data.readUint32LE(pos); |
| 63 | + pos += 4; |
| 64 | + |
| 65 | + arr.push({ index, type, typeName, value }); |
| 66 | + |
| 67 | + break; |
| 68 | + } |
| 69 | + case VariantTypes.SIGNED_INT: { |
| 70 | + const value = data.readInt32LE(pos); |
| 71 | + pos += 4; |
| 72 | + |
| 73 | + arr.push({ index, type, typeName, value }); |
| 74 | + break; |
| 75 | + } |
| 76 | + case VariantTypes.FLOAT_1: { |
| 77 | + const value = [data.readFloatLE(pos)]; |
| 78 | + |
| 79 | + pos += 4; |
| 80 | + arr.push({ index, type, typeName, value }); |
| 81 | + break; |
| 82 | + } |
| 83 | + case VariantTypes.FLOAT_2: { |
| 84 | + let value = []; |
| 85 | + |
| 86 | + for (let i = 1; i <= 2; i++) { |
| 87 | + value.push(data.readFloatLE(pos)); |
| 88 | + pos += 4; |
| 89 | + } |
| 90 | + arr.push({ index, type, typeName, value }); |
| 91 | + break; |
| 92 | + } |
| 93 | + case VariantTypes.FLOAT_3: { |
| 94 | + let value = []; |
| 95 | + |
| 96 | + for (let i = 1; i <= 3; i++) { |
| 97 | + value.push(data.readFloatLE(pos)); |
| 98 | + pos += 4; |
| 99 | + } |
| 100 | + arr.push({ index, type, typeName, value }); |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return arr; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Parses the data of the Variant and returns a TankPacket from it. |
| 110 | + */ |
| 111 | + public parse() { |
| 112 | + let buf = [this.args.length]; |
| 113 | + |
| 114 | + this.args.forEach((arg) => { |
| 115 | + buf.push(this.index++); |
| 116 | + |
| 117 | + switch (typeof arg) { |
| 118 | + case "string": { |
| 119 | + buf.push(VariantTypes.STRING); |
| 120 | + |
| 121 | + const bytes = new Uint32Array(1); |
| 122 | + bytes[0] = arg.length; |
| 123 | + |
| 124 | + const uint8_buf = new Uint8ClampedArray(bytes.buffer); |
| 125 | + const text_buf = new TextEncoder().encode(arg); |
| 126 | + |
| 127 | + buf = [...buf, ...uint8_buf, ...text_buf]; |
| 128 | + break; |
| 129 | + } |
| 130 | + |
| 131 | + case "number": { |
| 132 | + let bytes; |
| 133 | + |
| 134 | + if (arg < 0) { |
| 135 | + bytes = new Int32Array(1); |
| 136 | + buf.push(VariantTypes.SIGNED_INT); |
| 137 | + } else { |
| 138 | + bytes = new Uint32Array(1); |
| 139 | + buf.push(VariantTypes.UNSIGNED_INT); |
| 140 | + } |
| 141 | + |
| 142 | + bytes[0] = arg; |
| 143 | + |
| 144 | + const uint8_buf = new Uint8ClampedArray(bytes.buffer); |
| 145 | + buf = [...buf, ...uint8_buf]; |
| 146 | + break; |
| 147 | + } |
| 148 | + |
| 149 | + case "object": { |
| 150 | + if (!Array.isArray(arg)) return; |
| 151 | + |
| 152 | + const type = VariantTypes[`FLOAT_${arg.length}` as "FLOAT_1" | "FLOAT_2"]; |
| 153 | + if (!type) return; |
| 154 | + |
| 155 | + buf.push(type); |
| 156 | + |
| 157 | + arg.forEach((float) => { |
| 158 | + const bytes = new Float32Array(1); |
| 159 | + bytes[0] = float; |
| 160 | + |
| 161 | + const uint8_buf = new Uint8ClampedArray(bytes.buffer); |
| 162 | + buf = [...buf, ...uint8_buf]; |
| 163 | + }); |
| 164 | + break; |
| 165 | + } |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + return TankPacket.from({ |
| 170 | + type: 0x1, |
| 171 | + netID: this.options.netID ?? -1, |
| 172 | + info: this.options.delay ?? 0, |
| 173 | + data: () => Buffer.from(buf) |
| 174 | + }); |
| 175 | + } |
| 176 | +} |
0 commit comments