|
| 1 | +import { ComponentHash } from '../../ComponentHash'; |
| 2 | +import { BinaryReader, createBinaryWriter } from '../../utils'; |
| 3 | + |
| 4 | +type Gift = null | { |
| 5 | + data: number[]; |
| 6 | + messageSizeInBytes: number; |
| 7 | + hash: number; |
| 8 | + chunkVersioning: number[]; |
| 9 | +}; |
| 10 | + |
| 11 | +type SentGiftTag = null | { |
| 12 | + from: number; |
| 13 | + to: number; |
| 14 | +}; |
| 15 | + |
| 16 | +export type SentGift = { |
| 17 | + receiverName?: string; |
| 18 | + senderName?: string; |
| 19 | + gifts?: Gift[]; |
| 20 | + senderTag?: SentGiftTag; |
| 21 | +}; |
| 22 | + |
| 23 | +export const decode = (reader: BinaryReader): SentGift => { |
| 24 | + const receiverName = reader.string(); |
| 25 | + const senderName = reader.string(); |
| 26 | + |
| 27 | + /* Get the gifts array. */ |
| 28 | + const giftsLength = reader.uInt(); |
| 29 | + const gifts: Gift[] = []; |
| 30 | + |
| 31 | + for (let g = 0; g < giftsLength; ++g) { |
| 32 | + /* Skip gift if is null. */ |
| 33 | + const isNull = reader.boolean(); |
| 34 | + if (isNull) { |
| 35 | + gifts.push(null); |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + /* Get the data array. */ |
| 40 | + const dataLength = reader.uInt(); |
| 41 | + const data: number[] = []; |
| 42 | + |
| 43 | + for (let d = 0; d < dataLength; ++d) { |
| 44 | + data.push(reader.uInt()); |
| 45 | + } |
| 46 | + |
| 47 | + const messageSizeInBytes = reader.uInt(); |
| 48 | + const hash = reader.uInt(); |
| 49 | + |
| 50 | + /* Get the chunkVersioning array. */ |
| 51 | + const chunkVersioningLength = reader.uInt(); |
| 52 | + const chunkVersioning: number[] = []; |
| 53 | + |
| 54 | + for (let c = 0; c < chunkVersioningLength; ++c) { |
| 55 | + chunkVersioning.push(reader.uInt()); |
| 56 | + } |
| 57 | + |
| 58 | + gifts.push({ |
| 59 | + data, |
| 60 | + messageSizeInBytes, |
| 61 | + hash, |
| 62 | + chunkVersioning |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + const senderTagIsNull = reader.boolean(); |
| 67 | + |
| 68 | + const senderTag: SentGiftTag = senderTagIsNull |
| 69 | + ? null |
| 70 | + : { |
| 71 | + from: reader.int(), |
| 72 | + to: reader.int() |
| 73 | + }; |
| 74 | + |
| 75 | + const result: SentGift = { |
| 76 | + receiverName, |
| 77 | + senderName, |
| 78 | + gifts, |
| 79 | + senderTag |
| 80 | + }; |
| 81 | + |
| 82 | + return result; |
| 83 | +}; |
| 84 | + |
| 85 | +export const encode = ({ receiverName = '', senderName = '', gifts = [], senderTag = null }: SentGift): string => { |
| 86 | + const writer = createBinaryWriter(); |
| 87 | + |
| 88 | + /* Component hash. */ |
| 89 | + writer.uInt(ComponentHash.SentGift); |
| 90 | + const hashBits = writer.flush(); |
| 91 | + |
| 92 | + /* Component data. */ |
| 93 | + writer.string(receiverName); |
| 94 | + writer.string(senderName); |
| 95 | + writer.uInt(gifts.length); |
| 96 | + |
| 97 | + for (const gift of gifts) { |
| 98 | + if (gift === null) { |
| 99 | + writer.boolean(true); // isNull bit |
| 100 | + } else { |
| 101 | + writer.boolean(false); // isNull bit |
| 102 | + writer.uInt(gift.data.length); |
| 103 | + |
| 104 | + for (const d of gift.data) writer.uInt(d); |
| 105 | + |
| 106 | + writer.uInt(gift.messageSizeInBytes); |
| 107 | + writer.uInt(gift.hash); |
| 108 | + writer.uInt(gift.chunkVersioning.length); |
| 109 | + |
| 110 | + for (const c of gift.chunkVersioning) writer.uInt(c); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (senderTag === null) { |
| 115 | + writer.boolean(true); // isNull bit |
| 116 | + } else { |
| 117 | + writer.boolean(false); // isNull bit |
| 118 | + writer.int(senderTag.from); |
| 119 | + writer.int(senderTag.to); |
| 120 | + } |
| 121 | + |
| 122 | + const dataBits = writer.flush(); |
| 123 | + |
| 124 | + /* Component data length. */ |
| 125 | + writer.uInt(dataBits.length); |
| 126 | + const sizeBits = writer.flush(); |
| 127 | + |
| 128 | + /* Return encoded component. */ |
| 129 | + writer.binary(hashBits); |
| 130 | + writer.binary(sizeBits); |
| 131 | + writer.binary(dataBits); |
| 132 | + |
| 133 | + return writer.flush(); |
| 134 | +}; |
0 commit comments