Skip to content

Commit 8aeb256

Browse files
committed
Add SentGift component
1 parent 9fd9bf3 commit 8aeb256

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

src/components/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type PhysicalMaterialPart = transcoders.PhysicalMaterialPart.PhysicalMate
1313
export type Pickup = transcoders.Pickup.Pickup;
1414
export type PickupDock = transcoders.PickupDock.PickupDock;
1515
export type PopulationSpawnArea = transcoders.PopulationSpawnArea.PopulationSpawnArea;
16+
export type SentGift = transcoders.SentGift.SentGift;
1617
export type SpawnArea = transcoders.SpawnArea.SpawnArea;
1718
export type StatManager = transcoders.StatManager.StatManager;
1819
export type WoodcutTree = transcoders.WoodcutTree.WoodcutTree;
@@ -29,6 +30,7 @@ export type KnownComponent =
2930
| Pickup
3031
| PickupDock
3132
| PopulationSpawnArea
33+
| SentGift
3234
| SpawnArea
3335
| StatManager
3436
| WoodcutTree;

src/components/transcoders/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export * as PhysicalMaterialPart from './physicalMaterialPart';
99
export * as Pickup from './pickup';
1010
export * as PickupDock from './pickupDock';
1111
export * as PopulationSpawnArea from './populationSpawnArea';
12+
export * as SentGift from './sentGift';
1213
export * as SpawnArea from './spawnArea';
1314
export * as StatManager from './statManager';
1415
export * as WoodcutTree from './woodcutTree';
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)