Fixed #3963, closes #3979, added hash for non-numeric seeds

This commit is contained in:
PEMapModder 2016-02-22 19:37:52 +08:00
parent 41a847567c
commit ed9888a2cb
6 changed files with 34 additions and 15 deletions

View File

@ -76,9 +76,9 @@ use pocketmine\nbt\NBT;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\DoubleTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\FloatTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ListTag;
use pocketmine\nbt\tag\LongTag;
use pocketmine\nbt\tag\ShortTag;
use pocketmine\nbt\tag\StringTag;
@ -91,7 +91,6 @@ use pocketmine\network\protocol\PlayerListPacket;
use pocketmine\network\query\QueryHandler;
use pocketmine\network\RakLibInterface;
use pocketmine\network\rcon\RCON;
use pocketmine\network\SourceInterface;
use pocketmine\network\upnp\UPnP;
use pocketmine\permission\BanList;
use pocketmine\permission\DefaultPermissions;
@ -113,8 +112,6 @@ use pocketmine\utils\Binary;
use pocketmine\utils\Config;
use pocketmine\utils\LevelException;
use pocketmine\utils\MainLogger;
use pocketmine\utils\ServerException;
use pocketmine\utils\ServerKiller;
use pocketmine\utils\Terminal;
use pocketmine\utils\TextFormat;
use pocketmine\utils\Utils;
@ -1624,7 +1621,12 @@ class Server{
$this->setConfigString("level-name", "world");
}
if($this->loadLevel($default) === false){
$seed = $this->getConfigInt("level-seed", time());
$seed = getopt("", ["level-seed::"])["level-seed"] ?? $this->properties->get("level-seed", time());
if(!is_numeric($seed) or bccomp($seed, "9223372036854775807") > 0){
$seed = Utils::javaStringHash($seed);
}elseif(PHP_INT_SIZE === 8){
$seed = (int) $seed;
}
$this->generateLevel($default, $seed === 0 ? time() : $seed);
}

View File

@ -2696,9 +2696,9 @@ class Level implements ChunkManager, Metadatable{
/**
* Gets the level seed
*
* @return int
* @return int|string int value of seed, or the string numeric representation of a long in 32-bit systems
*/
public function getSeed() : int{
public function getSeed(){
return $this->provider->getSeed();
}

View File

@ -194,7 +194,7 @@ interface LevelProvider{
public function setTime($value);
/**
* @return int
* @return int|string int, or the string numeric representation of a long in 32-bit systems
*/
public function getSeed();

View File

@ -51,7 +51,6 @@ abstract class BaseLevelProvider implements LevelProvider{
$levelData = $nbt->getData();
if($levelData->Data instanceof CompoundTag){
$this->levelData = $levelData->Data;
assert(is_int($this->levelData["RandomSeed"]));
}else{
throw new LevelException("Invalid level.dat");
}
@ -94,7 +93,7 @@ abstract class BaseLevelProvider implements LevelProvider{
}
public function setSeed($value){
$this->levelData->RandomSeed = new LongTag("RandomSeed", (int) $value);
$this->levelData->RandomSeed = new LongTag("RandomSeed", $value);
}
public function getSpawn(){

View File

@ -32,7 +32,6 @@ use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\LongTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\tile\Spawnable;
use pocketmine\utils\BinaryStream;
use pocketmine\utils\ChunkException;
@ -92,7 +91,7 @@ class McRegion extends BaseLevelProvider{
"version" => new IntTag("version", 19133),
"DayTime" => new IntTag("DayTime", 0),
"LastPlayed" => new LongTag("LastPlayed", microtime(true) * 1000),
"RandomSeed" => new LongTag("RandomSeed", (int) $seed),
"RandomSeed" => new LongTag("RandomSeed", $seed),
"SizeOnDisk" => new LongTag("SizeOnDisk", 0),
"Time" => new LongTag("Time", 0),
"generatorName" => new StringTag("generatorName", Generator::getGeneratorName($generator)),

View File

@ -23,6 +23,7 @@
* Various Utilities used around the code
*/
namespace pocketmine\utils;
use pocketmine\ThreadManager;
/**
@ -462,8 +463,8 @@ class Utils{
/**
* GETs an URL using cURL
*
* @param $page
* @param int $timeout default 10
* @param $page
* @param int $timeout default 10
* @param array $extraHeaders
*
* @return bool|mixed
@ -496,7 +497,7 @@ class Utils{
* @param $page
* @param array|string $args
* @param int $timeout
* @param array $extraHeaders
* @param array $extraHeaders
*
* @return bool|mixed
*/
@ -524,4 +525,22 @@ class Utils{
return $ret;
}
public static function javaStringHash($string){
$hash = 0;
for($i = 0; $i < strlen($string); $i++){
$ord = ord($string{$i});
if($ord & 0x80){
$ord -= 0x100;
}
$hash = 31 * $hash + $ord;
while($hash > 0x7FFFFFFF){
$hash -= 0x100000000;
}
while($hash < -0x80000000){
$hash += 0x100000000;
}
$hash &= 0xFFFFFFFF;
}
return $hash;
}
}