Skip to content

Commit

Permalink
Big code cleanup
Browse files Browse the repository at this point in the history
Added type specifications where possible.
  • Loading branch information
AnrDaemon committed Feb 16, 2025
1 parent a7192e8 commit 7c4ad91
Show file tree
Hide file tree
Showing 24 changed files with 330 additions and 240 deletions.
8 changes: 6 additions & 2 deletions src/Interfaces/NbtSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

interface NbtSource
{
// The byte stream provider.
public function fread($len);
/** The byte stream provider
*
* @param int $len Bytes count to read.
* @return string
*/
public function fread(int $len): string;
}
30 changes: 23 additions & 7 deletions src/Interfaces/NbtTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@

namespace AnrDaemon\Minecraft\Interfaces;

use AnrDaemon\Minecraft\NBT\Tag;

interface NbtTag
{
// Read the tag data from a given source past the tag type/name.
// The function returns "value tag" - a tag with null name.
public static function readFrom(NbtSource $file);
// Create the tag from a given source, starting from the name.
public static function createFrom(NbtSource $file);
// Creates an NBT presentation of the tag.
public function nbtSerialize();
/** Read the tag data from a given source past the tag type/name
*
* The function returns "value tag" - a tag with null name.
*
* @param NbtSource $file
* @return Tag
*/
public static function readFrom(NbtSource $file): NbtTag;

/** Create the tag from a given source, starting from the name
*
* @param NbtSource $file
* @return Tag
*/
public static function createFrom(NbtSource $file): NbtTag;

/** Creates an NBT presentation of the tag
*
* @return string
*/
public function nbtSerialize(): string;
}
11 changes: 5 additions & 6 deletions src/NBT/CompressedWriter.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?php

/** Minecraft NBT compressed writer class.
*
*
*
* @version $Id$
*/
*
* @version $Id$
*/

namespace AnrDaemon\Minecraft\NBT;

class CompressedWriter
extends Writer
{
public function write(Tag $tag)
public function write(Tag $tag): int
{
return $this->file->fwrite(gzencode($tag->nbtSerialize(), 9, FORCE_GZIP));
}
Expand Down
47 changes: 23 additions & 24 deletions src/NBT/Dictionary.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

/** NBT dictionary and conversion tools
*
*
*
* @version $Id$
*/
*
* @version $Id$
*/

namespace AnrDaemon\Minecraft\NBT;

Expand All @@ -13,19 +12,19 @@
final class Dictionary
{
private static $typeMap = array(
"\x0" => __NAMESPACE__ . '\TAG_End',
"\x1" => __NAMESPACE__ . '\TAG_Byte',
"\x2" => __NAMESPACE__ . '\TAG_Short',
"\x3" => __NAMESPACE__ . '\TAG_Int',
"\x4" => __NAMESPACE__ . '\TAG_Long',
"\x5" => __NAMESPACE__ . '\TAG_Float',
"\x6" => __NAMESPACE__ . '\TAG_Double',
"\x7" => __NAMESPACE__ . '\TAG_Byte_Array',
"\x8" => __NAMESPACE__ . '\TAG_String',
"\x9" => __NAMESPACE__ . '\TAG_List',
"\xA" => __NAMESPACE__ . '\TAG_Compound',
"\xB" => __NAMESPACE__ . '\TAG_Int_Array',
"\xC" => __NAMESPACE__ . '\TAG_Long_Array',
"\x0" => __NAMESPACE__ . '\TAG_End',
"\x1" => __NAMESPACE__ . '\TAG_Byte',
"\x2" => __NAMESPACE__ . '\TAG_Short',
"\x3" => __NAMESPACE__ . '\TAG_Int',
"\x4" => __NAMESPACE__ . '\TAG_Long',
"\x5" => __NAMESPACE__ . '\TAG_Float',
"\x6" => __NAMESPACE__ . '\TAG_Double',
"\x7" => __NAMESPACE__ . '\TAG_Byte_Array',
"\x8" => __NAMESPACE__ . '\TAG_String',
"\x9" => __NAMESPACE__ . '\TAG_List',
"\xA" => __NAMESPACE__ . '\TAG_Compound',
"\xB" => __NAMESPACE__ . '\TAG_Int_Array',
"\xC" => __NAMESPACE__ . '\TAG_Long_Array',
);
private static $nameMap;

Expand All @@ -34,18 +33,18 @@ private static function init()
self::$nameMap = array_flip(self::$typeMap);
}

public static function mapType($type)
public static function mapType($type): string
{
if(!isset(self::$typeMap[$type]))
if (!isset(self::$typeMap[$type]))
throw new \OutOfBoundsException("Unknown tag type 0x" . bin2hex($type));

return self::$typeMap[$type];
}

public static function mapName($name)
public static function mapName($name): string
{
$tag = self::$nameMap[$name] ?? self::$nameMap[__NAMESPACE__ . "\\$name"] ?? null;
if(!isset($tag))
if (!isset($tag))
throw new \OutOfBoundsException("Unknown tag name '$name'");

return $tag;
Expand All @@ -56,15 +55,15 @@ public static function convert($value)
return _IS_BE ? $value : strrev($value);
}

// unpack() wrapper, because damned "machine byte order"
// unpack() wrapper, because damned "machine byte order"
public static function unpack($format, $value)
{
return unpack($format, static::convert($value))[1];
}

public function __construct()
{
if(!isset(self::$nameMap))
if (!isset(self::$nameMap))
self::init();
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/NBT/Reader.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php

/** Minecraft NBT reader class.
*
* @version $Id$
*/
*
* @version $Id$
*/

namespace AnrDaemon\Minecraft\NBT;

if(!method_exists('SplFileObject', 'fread'))
if (!method_exists('SplFileObject', 'fread'))
trigger_error('Requires SplFileObject::fread(). Upgrade your PHP.', E_USER_ERROR);

use
AnrDaemon\Minecraft\Interfaces\NbtSource;
use AnrDaemon\Minecraft\Interfaces\NbtTag;

class Reader
implements NbtSource
Expand All @@ -22,25 +24,25 @@ public function __construct(\SplFileObject $file)
$this->file = $file;
}

final public function read()
final public function read(): NbtTag
{
return Tag::createFrom($this);
}

public function fread($length)
public function fread(int $length): string
{
$length = (int)$length;
if($length < 0)
if ($length < 0)
throw new \RuntimeException("Backward reads are not supported.");

if($length)
if ($length)
{
$pos = $this->file->ftell();
$data = $this->file->fread($length);
if($data === false)
if ($data === false)
throw new \RuntimeException("Error while reading from file pointer.");

if(strlen($data) < $length)
if (strlen($data) < $length)
throw new \UnderflowException("Read " . strlen($data) . " out of {$length} requested bytes from file pointer @{$pos}.");
}
else
Expand Down
66 changes: 34 additions & 32 deletions src/NBT/TAG_Array.php
Original file line number Diff line number Diff line change
@@ -1,132 +1,134 @@
<?php

/** Minecraft NBT TAG_Array base class.
*
* @version $Id$
*/
*
* @version $Id$
*/

namespace AnrDaemon\Minecraft\NBT;

use
AnrDaemon\Minecraft\Interfaces\NbtSource;
use AnrDaemon\Minecraft\Interfaces\NbtTag;

abstract class TAG_Array
extends Tag
implements \ArrayAccess, \Countable, \Iterator
{
public $name = null;
protected $content = array();
protected $position = 0;

abstract protected function validate($value);
abstract protected function store();
abstract protected function store(): iterable;

public function __construct($name = null, array $content = array())
{
parent::__construct($name);
foreach($content as $value)
foreach ($content as $value)
{
$this[] = $value;
}
}

// Tag
// Tag

public function __debugInfo()
{
return ['name' => $this->name, 'content' => $this->content];
}

// NbtTag
// NbtTag

public static function createFrom(NbtSource $file)
public static function createFrom(NbtSource $file): NbtTag
{
$self = new static(TAG_String::readFrom($file));
return static::readFrom($file, $self);
}

public function nbtSerialize()
public function nbtSerialize(): string
{
$result = parent::nbtSerialize();
foreach($this->store() as $value)
foreach ($this->store() as $value)
$result .= $value;

return $result;
}

// ArrayAccess
// ArrayAccess

public function offsetSet($offset, $value)
public function offsetSet($offset, $value): void
{
$value = $this->validate($value);
if(is_null($offset))
if (is_null($offset))
$this->content[] = $value;
else
$this->content[$offset] = $value;
}

public function offsetExists($offset)
public function offsetExists($offset): bool
{
return isset($this->content[$offset]);
}

public function offsetUnset($offset)
public function offsetUnset($offset): void
{
unset($this->content[$offset]);
}

public function offsetGet($offset)
public function offsetGet($offset): mixed
{
return isset($this->content[$offset]) ? $this->content[$offset] : null;
}

// Countable
// Countable

public function count()
public function count(): int
{
return count($this->content);
}

// Iterator
// Iterator

public function current()
public function current(): mixed
{
return $this->valid() ? current($this->content) : null;
}

public function key()
public function key(): mixed
{
return key($this->content);
}

public function next()
public function next(): void
{
if($this->valid())
if ($this->valid())
{
$this->position++;
return next($this->content);
next($this->content);
}
return null;
}

public function rewind()
public function rewind(): void
{
$this->position = 0;
return reset($this->content);
reset($this->content);
}

public function valid()
public function valid(): bool
{
return $this->position < count($this->content);
}

// JsonSerializable
// JsonSerializable

public function jsonSerialize()
public function jsonSerialize(): mixed
{
error_log(__METHOD__);
//return (object)[];
return [$this->name => $this->content,];
}

// Serializable
// Serializable

public function serialize()
{
Expand Down
Loading

0 comments on commit 7c4ad91

Please sign in to comment.