Skip to content

Commit

Permalink
Global rewrite of the class structure.
Browse files Browse the repository at this point in the history
Added 19133 version support.
Implemented direct NBT serialization.
Implemented PHP serialization.
Declared NBT source provider interface.
Unify NbtTag::readFrom() return values.
Turned abstract Tag into parsing root.
  • Loading branch information
AnrDaemon committed Apr 2, 2018
1 parent 90c501a commit e4fb5ef
Show file tree
Hide file tree
Showing 26 changed files with 672 additions and 663 deletions.
201 changes: 201 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
------------------------------------------------------------------------
r282 | anrdaemon | 2018-03-27 23:19:29 +0300 (Tue, 27 Mar 2018) | 2 lines

+ \Serializable interface implementation for NBT\Tag descendants.

------------------------------------------------------------------------
r280 | anrdaemon | 2018-03-27 19:05:51 +0300 (Tue, 27 Mar 2018) | 7 lines

* Resolve reader mess, reduce stream provider to a single ::fread() method.
+ Add NbtSource provider interface.
* Start tree parsing from the Tag head.
* Use stricter validation for various tags both on load and save.
* Do all parsing conversion in Dictionary.
* Use forward NBT serialization.

------------------------------------------------------------------------
r279 | anrdaemon | 2018-03-27 09:43:02 +0300 (Tue, 27 Mar 2018) | 3 lines

* Reinstate common parent constructor.
* Simplify iterator key handling.

------------------------------------------------------------------------
r278 | anrdaemon | 2018-03-27 09:07:14 +0300 (Tue, 27 Mar 2018) | 2 lines

* Set TAG_List type by the first assignment.

------------------------------------------------------------------------
r277 | anrdaemon | 2018-03-27 08:51:16 +0300 (Tue, 27 Mar 2018) | 2 lines

* Tweak TAG_Array constructor to use defined assignment.

------------------------------------------------------------------------
r276 | anrdaemon | 2018-03-27 08:04:29 +0300 (Tue, 27 Mar 2018) | 3 lines

* NbtTag::readFrom() returns value tags (tags with null names) instead of raw values for simple tag types.
Makes it consistent with complex tags.

------------------------------------------------------------------------
r275 | anrdaemon | 2018-03-27 07:12:43 +0300 (Tue, 27 Mar 2018) | 4 lines

- Unbind TAG_End from Tag. They are not related.
- Removed bogus Tag constructor and all references to its works.
* Fixed TAG_Array constructor declaration.

------------------------------------------------------------------------
r273 | anrdaemon | 2018-03-25 19:39:16 +0300 (Sun, 25 Mar 2018) | 2 lines

* Fix ignore list on root directory.

------------------------------------------------------------------------
r272 | anrdaemon | 2018-03-25 19:39:16 +0300 (Sun, 25 Mar 2018) | 2 lines

= Standardize files layout.

------------------------------------------------------------------------
r271 | anrdaemon | 2018-03-25 19:17:22 +0300 (Sun, 25 Mar 2018) | 2 lines

* Move and update bootstrap autoloader.

------------------------------------------------------------------------
r263 | anrdaemon | 2018-02-21 06:42:21 +0300 (Wed, 21 Feb 2018) | 4 lines

* Use php://temp instead of php://memory.
Even hough an average compressed chunk is way less than 2MB,
safety is safety and data loss is not safe.

------------------------------------------------------------------------
r261 | anrdaemon | 2018-02-12 06:26:29 +0300 (Mon, 12 Feb 2018) | 3 lines

* Use fully static initial dictionary map.
+ Add 19133 version support.

------------------------------------------------------------------------
r260 | anrdaemon | 2018-02-12 06:18:01 +0300 (Mon, 12 Feb 2018) | 2 lines

* Reorder tags' methods for clarity.

------------------------------------------------------------------------
r259 | anrdaemon | 2018-02-12 06:08:53 +0300 (Mon, 12 Feb 2018) | 4 lines

+ Self-init Dictionary. Reduce runtime conditional checks.
* Fix TAG_Array::__construct() prototype.
- Remove unconditional lists sorting.

------------------------------------------------------------------------
r258 | anrdaemon | 2018-02-12 05:51:25 +0300 (Mon, 12 Feb 2018) | 3 lines

- Heavy classes cleanup.
* Take advantage of new pack() codes.

------------------------------------------------------------------------
r257 | anrdaemon | 2018-02-12 04:57:19 +0300 (Mon, 12 Feb 2018) | 3 lines

* Update license file with original unmodified version.
* Update composer requirements.

------------------------------------------------------------------------
r256 | anrdaemon | 2018-02-11 07:01:12 +0300 (Sun, 11 Feb 2018) | 3 lines

- Remove ad hoc debug code.
- Remove unnecessary NbtTag interface references.

------------------------------------------------------------------------
r255 | anrdaemon | 2018-02-11 06:07:13 +0300 (Sun, 11 Feb 2018) | 3 lines

* Directly reference classes from root namespace.
* Tweak entry formatting.

------------------------------------------------------------------------
r254 | anrdaemon | 2018-02-11 06:03:50 +0300 (Sun, 11 Feb 2018) | 3 lines

+ README
* Alias Subversion trunk.

------------------------------------------------------------------------
r249 | anrdaemon | 2017-12-06 00:21:51 +0300 (Wed, 06 Dec 2017) | 2 lines

Pin todo.

------------------------------------------------------------------------
r246 | anrdaemon | 2017-12-06 00:04:15 +0300 (Wed, 06 Dec 2017) | 2 lines

Layout.

------------------------------------------------------------------------
r193 | anrdaemon | 2016-07-21 04:58:39 +0300 (Thu, 21 Jul 2016) | 2 lines

+ License.

------------------------------------------------------------------------
r190 | anrdaemon | 2016-07-20 00:45:04 +0300 (Wed, 20 Jul 2016) | 2 lines

+ Plug another debug hole.

------------------------------------------------------------------------
r189 | anrdaemon | 2016-07-19 23:38:29 +0300 (Tue, 19 Jul 2016) | 2 lines

+ Plug the built-in debug logging the right way.

------------------------------------------------------------------------
r188 | anrdaemon | 2016-07-19 23:03:31 +0300 (Tue, 19 Jul 2016) | 2 lines

+ Fix the loader for good.

------------------------------------------------------------------------
r187 | anrdaemon | 2016-07-19 22:25:51 +0300 (Tue, 19 Jul 2016) | 3 lines

- CompressedWriter: remove superfluous version check.
+ Tag: Add hotplug for integrated debugger tracing.

------------------------------------------------------------------------
r186 | anrdaemon | 2016-07-19 21:55:11 +0300 (Tue, 19 Jul 2016) | 2 lines

+ Wordings.

------------------------------------------------------------------------
r184 | anrdaemon | 2016-07-19 20:15:06 +0300 (Tue, 19 Jul 2016) | 2 lines

* Spelling.

------------------------------------------------------------------------
r183 | anrdaemon | 2016-07-18 20:09:02 +0300 (Mon, 18 Jul 2016) | 2 lines

* Universal stackable classloader.

------------------------------------------------------------------------
r182 | anrdaemon | 2016-07-18 19:57:09 +0300 (Mon, 18 Jul 2016) | 2 lines

* Fix classloader for better compatibility with future language changes.

------------------------------------------------------------------------
r181 | anrdaemon | 2016-07-18 18:39:23 +0300 (Mon, 18 Jul 2016) | 3 lines

+ __debugInfo() for easier visual debugging.
Print only important data, name before content.

------------------------------------------------------------------------
r180 | anrdaemon | 2016-07-18 18:17:08 +0300 (Mon, 18 Jul 2016) | 2 lines

+ Composer metadata update.

------------------------------------------------------------------------
r179 | anrdaemon | 2016-07-18 17:20:28 +0300 (Mon, 18 Jul 2016) | 2 lines

+ Fix props on composer.json.

------------------------------------------------------------------------
r177 | anrdaemon | 2016-07-18 02:33:03 +0300 (Mon, 18 Jul 2016) | 6 lines

+ Use SplFileObject in Reader/Writer for compatibility with future
implementations.
+ Formatting fixes.
+ tag->save->file rather than the other way around.
+ Reimplement CompressedWriter in a more straightforward fashion.

------------------------------------------------------------------------
r174 | anrdaemon | 2016-07-17 23:12:39 +0300 (Sun, 17 Jul 2016) | 2 lines

+ Sort simple arrays before saving.

------------------------------------------------------------------------
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,25 @@
}
],
"require": {
"php-64bit": "^7"
"php-64bit": "^7.0.15"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
},
"extra": {
"branch-alias": {
"dev-trunk": "0.x-dev",
"dev-master": "0.x-dev"
}
},
"autoload": {
"psr-4": {
"AnrDaemon\\Minecraft\\": "Minecraft/"
"AnrDaemon\\Minecraft\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"AnrDaemon\\Tests\\Minecraft\\": "test/"
}
}
}
9 changes: 9 additions & 0 deletions src/Interfaces/NbtSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AnrDaemon\Minecraft\Interfaces;

interface NbtSource
{
// The byte stream provider.
public function fread($len);
}
14 changes: 7 additions & 7 deletions src/Interfaces/NbtTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace AnrDaemon\Minecraft\Interfaces;

use
AnrDaemon\Minecraft\NBT\Reader,
SplFileObject;

interface NbtTag
{
public static function readFrom(Reader $file);
public static function createFrom(Reader $file);
public function save(SplFileObject $file);
// 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();
}
17 changes: 5 additions & 12 deletions src/NBT/CompressedWriter.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
<?php
/** Minecraft NBT writer class.
/** Minecraft NBT compressed writer class.
*
*
*
* @version $Id: CompressedWriter.php 187 2016-07-19 19:25:51Z anrdaemon $
* @version $Id: CompressedWriter.php 280 2018-03-27 16:05:51Z anrdaemon $
*/

namespace AnrDaemon\Minecraft\NBT;

use
SplFileObject;

class CompressedWriter extends Writer
class CompressedWriter
extends Writer
{
public function write(Tag $tag)
{
\tool::fprint("Writing ... " . get_called_class() . "@{$this->file->ftell()}");
$tmp = new SplFileObject('php://memory', 'wb+');
$tag->save($tmp);
$pos = $tmp->ftell();
$tmp->fseek(0);
return $this->file->fwrite(gzencode($tmp->fread($pos), 9, FORCE_GZIP));
return $this->file->fwrite(gzencode($tag->nbtSerialize(), 9, FORCE_GZIP));
}
}
58 changes: 28 additions & 30 deletions src/NBT/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,16 @@
*
*
*
* @version $Id: Dictionary.php 177 2016-07-17 23:33:03Z anrdaemon $
* @version $Id: Dictionary.php 280 2018-03-27 16:05:51Z anrdaemon $
*/

namespace AnrDaemon\Minecraft\NBT;

define('_IS_BE', unpack('v', pack('S', 1))[1] > 1);

use
BadMethodCallException, OutOfBoundsException;

final class Dictionary
{
private static $typeMap;

private function __construct()
{
throw new BadMethodCallException('May not initialize tools class.');
}

private static function init()
{
self::$typeMap = array(
private static $typeMap = array(
"\x0" => __NAMESPACE__ . '\TAG_End',
"\x1" => __NAMESPACE__ . '\TAG_Byte',
"\x2" => __NAMESPACE__ . '\TAG_Short',
Expand All @@ -36,39 +24,49 @@ private static function init()
"\x8" => __NAMESPACE__ . '\TAG_String',
"\x9" => __NAMESPACE__ . '\TAG_List',
"\xA" => __NAMESPACE__ . '\TAG_Compound',
"\xB" => __NAMESPACE__ . '\TAG_Int_Array'
);
"\xB" => __NAMESPACE__ . '\TAG_Int_Array',
"\xC" => __NAMESPACE__ . '\TAG_Long_Array',
);
private static $nameMap;

private static function init()
{
self::$nameMap = array_flip(self::$typeMap);
}

public static function mapType($type)
{
if(!is_array(self::$typeMap))
self::init();

if(!isset(self::$typeMap[$type]))
throw new OutOfBoundsException("Unknown tag type " . ord($type));
throw new \OutOfBoundsException("Unknown tag type 0x" . bin2hex($type));

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

public static function mapName($name)
{
if(!is_array(self::$typeMap))
self::init();

$tag = array_search($name, self::$typeMap);
if($tag === false)
$tag = array_search(__NAMESPACE__ . "\\$name", self::$typeMap);

if($tag === false)
throw new OutOfBoundsException("Unknown tag name " . ord($type));
$tag = self::$nameMap[$name] ?? self::$nameMap[__NAMESPACE__ . "\\$name"] ?? null;
if(!isset($tag))
throw new \OutOfBoundsException("Unknown tag name '$name'");

return $tag;
}

// unpack() wrapper, because damned "machine byte order"
public static function convert($value)
{
return _IS_BE ? $value : strrev($value);
}

// 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))
self::init();
}
}

return new Dictionary;
Loading

0 comments on commit e4fb5ef

Please sign in to comment.