130 $this->needsTranslation =
CommonTypes::getBool($in);
132 $category = Byte::readUnsigned($in);
133 $expectedDummyStrings = self::CATEGORY_DUMMY_STRINGS[$category] ??
throw new PacketDecodeException(
"Unknown category ID $category");
134 foreach($expectedDummyStrings as $k => $expectedDummyString){
135 $actual = CommonTypes::getString($in);
136 if($expectedDummyString !== $actual){
137 throw new PacketDecodeException(
"Dummy string mismatch for category $category at position $k: expected $expectedDummyString, got $actual");
141 $this->type = Byte::readUnsigned($in);
143 case self::TYPE_CHAT:
144 case self::TYPE_WHISPER:
146 case self::TYPE_ANNOUNCEMENT:
147 if($category !== self::CATEGORY_AUTHORED_MESSAGE){
148 throw new PacketDecodeException(
"Decoded TextPacket has invalid structure: type {$this->type} requires category CATEGORY_AUTHORED_MESSAGE");
150 $this->sourceName = CommonTypes::getString($in);
151 $this->message = CommonTypes::getString($in);
155 case self::TYPE_SYSTEM:
156 case self::TYPE_JSON_WHISPER:
157 case self::TYPE_JSON:
158 case self::TYPE_JSON_ANNOUNCEMENT:
159 if($category !== self::CATEGORY_MESSAGE_ONLY){
160 throw new PacketDecodeException(
"Decoded TextPacket has invalid structure: type {$this->type} requires category CATEGORY_MESSAGE_ONLY");
162 $this->message = CommonTypes::getString($in);
164 case self::TYPE_TRANSLATION:
165 case self::TYPE_POPUP:
166 case self::TYPE_JUKEBOX_POPUP:
167 if($category !== self::CATEGORY_MESSAGE_WITH_PARAMETERS){
168 throw new PacketDecodeException(
"Decoded TextPacket has invalid structure: type {$this->type} requires category CATEGORY_MESSAGE_WITH_PARAMETERS");
170 $this->message = CommonTypes::getString($in);
171 $count = VarInt::readUnsignedInt($in);
172 for($i = 0; $i < $count; ++$i){
173 $this->parameters[] = CommonTypes::getString($in);
178 $this->xboxUserId = CommonTypes::getString($in);
179 $this->platformChatId = CommonTypes::getString($in);
180 $this->filteredMessage = CommonTypes::readOptional($in, CommonTypes::getString(...));
184 CommonTypes::putBool($out, $this->needsTranslation);
186 $category = match ($this->type) {
190 self::TYPE_JSON_WHISPER,
191 self::TYPE_JSON_ANNOUNCEMENT,
192 self::TYPE_JSON => self::CATEGORY_MESSAGE_ONLY,
196 self::TYPE_ANNOUNCEMENT => self::CATEGORY_AUTHORED_MESSAGE,
198 self::TYPE_TRANSLATION,
200 self::TYPE_JUKEBOX_POPUP => self::CATEGORY_MESSAGE_WITH_PARAMETERS,
202 default =>
throw new \LogicException(
"Invalid TextPacket type: $this->type")
204 Byte::writeUnsigned($out, $category);
205 foreach(self::CATEGORY_DUMMY_STRINGS[$category] as $dummyString){
206 CommonTypes::putString($out, $dummyString);
209 Byte::writeUnsigned($out, $this->type);
211 case self::TYPE_CHAT:
212 case self::TYPE_WHISPER:
214 case self::TYPE_ANNOUNCEMENT:
215 CommonTypes::putString($out, $this->sourceName);
218 case self::TYPE_SYSTEM:
219 case self::TYPE_JSON_WHISPER:
220 case self::TYPE_JSON:
221 case self::TYPE_JSON_ANNOUNCEMENT:
222 CommonTypes::putString($out, $this->message);
225 case self::TYPE_TRANSLATION:
226 case self::TYPE_POPUP:
227 case self::TYPE_JUKEBOX_POPUP:
228 CommonTypes::putString($out, $this->message);
229 VarInt::writeUnsignedInt($out, count($this->parameters));
230 foreach($this->parameters as $p){
231 CommonTypes::putString($out, $p);
236 CommonTypes::putString($out, $this->xboxUserId);
237 CommonTypes::putString($out, $this->platformChatId);
238 CommonTypes::writeOptional($out, $this->filteredMessage, CommonTypes::putString(...));