convertToData(\Phar::TAR, \Phar::NONE); unset($phar); \Phar::unlinkArchive($tmpPharPath); return $tmpName . ".tar"; } /** * Locks a phar tmp cache to prevent it from being deleted by other server instances. * This code looks similar to Filesystem::createLockFile(), but we can't use that because it's inside the compressed * phar. */ function lockPharCache(string $lockFilePath) : void{ //this static variable will keep the file(s) locked until the process ends static $lockFiles = []; $lockFile = fopen($lockFilePath, "wb"); if($lockFile === false){ throw new \RuntimeException("Failed to open temporary file"); } flock($lockFile, LOCK_EX); //this tells other server instances not to delete this cache file fwrite($lockFile, (string) getmypid()); //maybe useful for debugging fflush($lockFile); $lockFiles[$lockFilePath] = $lockFile; } /** * Prepares a decompressed .tar of PocketMine-MP.phar in the system temp directory for loading code from. * * @return string path to the temporary decompressed phar (actually a .tar) */ function preparePharCache(string $tmpPath, string $pharPath) : string{ clearstatcache(); $tmpName = tempnam($tmpPath, "PMMP"); if($tmpName === false){ throw new \RuntimeException("Failed to create temporary file"); } lockPharCache($tmpName . ".lock"); return convertPharToTar($tmpName, $pharPath); } $tmpDir = preparePharCacheDirectory(); cleanupPharCache($tmpDir); echo "Preparing PocketMine-MP.phar decompressed cache...\n"; $start = hrtime(true); $cacheName = preparePharCache($tmpDir, __FILE__); echo "Cache ready at $cacheName in " . number_format((hrtime(true) - $start) / 1e9, 2) . "s\n"; define('pocketmine\ORIGINAL_PHAR_PATH', __FILE__); require 'phar://' . str_replace(DIRECTORY_SEPARATOR, '/', $cacheName) . '/src/PocketMine.php';