Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pocketmine\world\format\SubChunk natively #24

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions chunkutils2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "src/PhpLightArray.h"
#include "src/PhpPalettedBlockArray.h"
#include "src/PhpSubChunk.h"
#include "src/PhpSubChunkConverter.h"

extern "C" {
Expand All @@ -38,6 +39,7 @@ PHP_MINIT_FUNCTION(chunkutils2)
{
register_light_array_class();
register_paletted_block_array_class();
register_sub_chunk_class();
register_sub_chunk_converter_class();
return SUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if test "$PHP_CHUNKUTILS2" != "no"; then
PHP_REQUIRE_CXX()

dnl the 6th parameter here is required for C++ shared extensions
PHP_NEW_EXTENSION(chunkutils2, chunkutils2.cpp src/PhpLightArray.cpp src/PhpPalettedBlockArray.cpp src/PhpSubChunkConverter.cpp, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -std=c++14 -fno-strict-aliasing -DGSL_THROW_ON_CONTRACT_VIOLATION=1, yes)
PHP_NEW_EXTENSION(chunkutils2, chunkutils2.cpp src/PhpLightArray.cpp src/PhpPalettedBlockArray.cpp src/PhpSubChunk.cpp src/PhpSubChunkConverter.cpp, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -std=c++14 -fno-strict-aliasing -DGSL_THROW_ON_CONTRACT_VIOLATION=1, yes)
PHP_SUBST(CHUNKUTILS2_SHARED_LIBADD)
PHP_ADD_BUILD_DIR($ext_builddir/src, 1)
PHP_ADD_BUILD_DIR($ext_builddir/lib, 1)
Expand Down
2 changes: 1 addition & 1 deletion config.w32
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (PHP_CHUNKUTILS2 != "no") {
ADD_FLAG("CFLAGS_CHUNKUTILS2", "/EHsc");
ADD_SOURCES(
configure_module_dirname + "/src",
"PhpLightArray.cpp PhpPalettedBlockArray.cpp PhpSubChunkConverter.cpp",
"PhpLightArray.cpp PhpPalettedBlockArray.cpp PhpSubChunk.cpp PhpSubChunkConverter.cpp",
"chunkutils2"
);
}
Expand Down
12 changes: 5 additions & 7 deletions src/PhpLightArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ extern "C" {
zend_class_entry* light_array_entry;
static zend_object_handlers light_array_handlers;

typedef struct {
LightArray lightArray;
zend_object std;
} light_array_obj;

/* internal object handlers */

static zend_object* light_array_new(zend_class_entry* class_type) {
Expand Down Expand Up @@ -71,6 +66,10 @@ static int light_array_unserialize(zval* obj, zend_class_entry* ce, const unsign
return SUCCESS;
}

void light_array_fill(light_array_obj* object, zend_long level) {
new (&object->lightArray) LightArray(static_cast<uint8_t>(level));
}

#define LIGHT_ARRAY_METHOD(name) PHP_METHOD(pocketmine_world_format_LightArray, name)

LIGHT_ARRAY_METHOD(__construct) {
Expand Down Expand Up @@ -105,8 +104,7 @@ LIGHT_ARRAY_METHOD(fill) {

object_init_ex(return_value, light_array_entry);
auto object = fetch_from_zend_object<light_array_obj>(Z_OBJ_P(return_value));

new (&object->lightArray) LightArray(static_cast<uint8_t>(level));
light_array_fill(object, level);
}

LIGHT_ARRAY_METHOD(get) {
Expand Down
11 changes: 11 additions & 0 deletions src/PhpLightArray.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#ifndef HAVE_PHP_LIGHT_ARRAY_H
#define HAVE_PHP_LIGHT_ARRAY_H

#include "lib/LightArray.h"

extern "C" {
#include "php.h"
}

typedef struct {
LightArray lightArray;
zend_object std;
} light_array_obj;

PHP_METHOD(PhpLightArray, __construct);
PHP_METHOD(PhpLightArray, fill);
PHP_METHOD(PhpLightArray, get);
Expand All @@ -13,6 +20,10 @@ PHP_METHOD(PhpLightArray, getData);
PHP_METHOD(PhpLightArray, collectGarbage);
PHP_METHOD(PhpLightArray, isUniform);

extern zend_class_entry* light_array_entry;

void light_array_fill(light_array_obj* object, zend_long level);

void register_light_array_class();

#endif
26 changes: 14 additions & 12 deletions src/PhpPalettedBlockArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,18 @@ static int paletted_block_array_unserialize(zval *object, zend_class_entry *ce,
return result;
}


bool paletted_block_array_fill(paletted_block_array_obj *intern, zend_long fillEntry) {
if (!checkPaletteEntrySize(fillEntry)) {
return false;
}
try {
new(&intern->container) NormalBlockArrayContainer((Block)fillEntry, 0);
} catch (std::exception& e) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "%s", e.what());
return false;
}
return true;
}
/* PHP-land PalettedBlockArray methods */
#define PALETTED_BLOCK_ARRAY_METHOD(name) PHP_METHOD(pocketmine_world_format_PalettedBlockArray, name)

Expand All @@ -207,17 +218,8 @@ PALETTED_BLOCK_ARRAY_METHOD(__construct) {
Z_PARAM_LONG(fillEntry)
ZEND_PARSE_PARAMETERS_END();

if (!checkPaletteEntrySize(fillEntry)) {
return;
}

paletted_block_array_obj *intern = fetch_from_zend_object<paletted_block_array_obj>(Z_OBJ_P(getThis()));
try {
new(&intern->container) NormalBlockArrayContainer((Block)fillEntry, 0);
}
catch (std::exception& e) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "%s", e.what());
}
paletted_block_array_fill(intern, fillEntry);
}

PALETTED_BLOCK_ARRAY_METHOD(fromData) {
Expand Down Expand Up @@ -345,4 +347,4 @@ void register_paletted_block_array_class() {
ce.unserialize = paletted_block_array_unserialize;
paletted_block_array_entry = zend_register_internal_class(&ce);
paletted_block_array_entry->ce_flags |= ZEND_ACC_FINAL;
}
}
2 changes: 0 additions & 2 deletions src/PhpPalettedBlockArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ PHP_METHOD(PhpPalettedBlockArray, replace);
PHP_METHOD(PhpPalettedBlockArray, replaceAll);
PHP_METHOD(PhpPalettedBlockArray, getExpectedWordArraySize);

extern zend_class_entry *paletted_block_array_entry;

void register_paletted_block_array_class();

#endif
3 changes: 3 additions & 0 deletions src/PhpPalettedBlockArrayObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ typedef struct {
zend_object std;
} paletted_block_array_obj;

extern zend_class_entry* paletted_block_array_entry;

bool paletted_block_array_fill(paletted_block_array_obj* intern, zend_long fillEntry);
#endif
Loading