ZlibCompressor: use libdeflate if available

I may make libdeflate mandatory later on, but right now we haven't been able to ship it on all platforms yet.
This commit is contained in:
Dylan K. Taylor 2020-10-13 17:30:27 +01:00
parent eabfd2a37b
commit 03837c1b71
2 changed files with 11 additions and 0 deletions

View File

@ -24,6 +24,8 @@ declare(strict_types=1);
namespace pocketmine\network\mcpe\compression;
use pocketmine\utils\SingletonTrait;
use function function_exists;
use function libdeflate_deflate_compress;
use function strlen;
use function zlib_decode;
use function zlib_encode;
@ -72,6 +74,11 @@ final class ZlibCompressor implements Compressor{
}
public function compress(string $payload) : string{
if(function_exists('libdeflate_deflate_compress')){
return $this->willCompress($payload) ?
libdeflate_deflate_compress($payload, $this->level) :
zlib_encode($payload, ZLIB_ENCODING_RAW, 0);
}
return zlib_encode($payload, ZLIB_ENCODING_RAW, $this->willCompress($payload) ? $this->level : 0);
}
}

View File

@ -23,6 +23,10 @@ declare(strict_types=1);
define('pocketmine\_PHPSTAN_ANALYSIS', true);
if(!extension_loaded('libdeflate')){
function libdeflate_deflate_compress(string $data, int $level = 6) : string{}
}
//TODO: these need to be defined properly or removed
define('pocketmine\COMPOSER_AUTOLOADER_PATH', dirname(__DIR__, 2) . '/vendor/autoload.php');
define('pocketmine\PLUGIN_PATH', '');