public function writeTagDataToStream(AbstractTag $tag, $file) { $genericType = null; if ($tag instanceof \Nbt\Tag\ListTag) { $genericType = TagType::identifyClassName($tag->getGenericType()); } return $this->writeTagValueToStream(TagType::identify($tag), $tag->getValue(), $file, $genericType); }
protected function readTagDataFromStream($file, $tagType) { if ($tagType == TagType::TAG_BYTE) { return new \Nbt\Tag\ByteTag($this->unpackNBytes($file, 1, "c")); } if ($tagType == TagType::TAG_SHORT) { $unsigned = $this->unpackNBytes($file, 2, "n"); if ($unsigned >= pow(2, 15)) { // convert to signed $unsigned -= pow(2, 16); } return new \Nbt\Tag\ShortTag($unsigned); } if ($tagType == TagType::TAG_INT) { $unsigned = $this->unpackNBytes($file, 4, "N"); if ($unsigned >= pow(2, 31)) { // conert to signed $unsigned = $unsigned - pow(2, 32); } return new \Nbt\Tag\IntTag($unsigned); } if ($tagType == TagType::TAG_LONG) { $first = $this->unpackNBytes($file, 4, "N"); $second = $this->unpackNBytes($file, 4, "N"); return new \Nbt\Tag\LongTag(($first << 32) + $second); } if ($tagType == TagType::TAG_FLOAT) { if ($this->isBigEndian()) { return new \Nbt\Tag\FloatTag($this->unpackNBytes($file, 4, 'f')); } return new \Nbt\Tag\FloatTag($this->unpackNBytes($file, 4, 'f', true)); } if ($tagType == TagType::TAG_DOUBLE) { if ($this->isBigEndian()) { return new \Nbt\Tag\DoubleTag($this->unpackNBytes($file, 8, 'd')); } return new \Nbt\Tag\DoubleTag($this->unpackNBytes($file, 8, 'd', true)); } if ($tagType == TagType::TAG_BYTE_ARRAY) { $length = $this->readTagDataFromStream($file, TagType::TAG_INT)->getValue(); $out = new \Nbt\Tag\ByteArrayTag(); for ($i = 0; $i < $length; $i++) { $out->push($this->readTagDataFromStream($file, TagType::TAG_BYTE)); } return $out; } if ($tagType == TagType::TAG_STRING) { if (!($length = $this->readTagDataFromStream($file, TagType::TAG_SHORT)->getValue())) { return new \Nbt\Tag\StringTag(""); } return new \Nbt\Tag\StringTag(fread($file, $length)); } if ($tagType == TagType::TAG_LIST) { $type = $this->readTagDataFromStream($file, TagType::TAG_BYTE)->getValue(); $length = $this->readTagDataFromStream($file, TagType::TAG_INT)->getValue(); $out = new \Nbt\Tag\ListTag(TagType::getTagClassName($type)); for ($i = 0; $i < $length; $i++) { if (feof($file)) { } $out->push($this->readTagDataFromStream($file, $type)); } return $out; } if ($tagType == TagType::TAG_COMPOUND) { $out = new \Nbt\Tag\CompoundTag(); while ($child = $this->readFullTagFromStream($file)) { $out->add($child); } return $out; } throw new \Exception("Unsupported type `{$tagType}`"); }