Created
November 5, 2023 19:24
-
-
Save dadodasyra/1e031289fa50ba6c057022641bf5abd1 to your computer and use it in GitHub Desktop.
Convert world with plugin code to leveldb (can be used with sort of a TestCommand) it freezes the server until the end of the conversion and automatically detect the base format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$originalPath = "/root/servers/miscs/converting/lobby"; | |
$backupPath = "/root/servers/miscs/converting/backup"; //this will create a folder under this one with the name of the world | |
$providerManager = new WorldProviderManager(); | |
$writableFormats = array_filter($providerManager->getAvailableProviders(), fn(WorldProviderManagerEntry $class) => $class instanceof WritableWorldProviderManagerEntry); | |
$requiredOpts = [ | |
"world" => "path to the input world for conversion", | |
"backup" => "path to back up the original files", | |
"format" => "desired output format (can be one of: " . implode(",", array_keys($writableFormats)) . ")" | |
]; | |
$usageMessage = "Options:\n"; | |
foreach($requiredOpts as $_opt => $_desc){ | |
$usageMessage .= "\t--$_opt : $_desc\n"; | |
} | |
$plainArgs = [ | |
"format" => "leveldb", | |
"world" => $originalPath, | |
"backup" => $backupPath | |
]; | |
foreach($requiredOpts as $opt => $desc){ | |
if(!isset($plainArgs[$opt]) || !is_string($plainArgs[$opt])){ | |
fwrite(STDERR, $usageMessage); | |
exit(1); | |
} | |
$args[$opt] = $plainArgs[$opt]; | |
} | |
if(!array_key_exists($args["format"], $writableFormats)){ | |
fwrite(STDERR, $usageMessage); | |
exit(1); | |
} | |
$inputPath = realpath($args["world"]); | |
if($inputPath === false){ | |
fwrite(STDERR, "Cannot find input world at location: " . $args["world"] . PHP_EOL); | |
exit(1); | |
} | |
$backupPath = realpath($args["backup"]); | |
if($backupPath === false || (!@mkdir($backupPath, 0777, true) && !is_dir($backupPath)) || !is_writable($backupPath)){ | |
fwrite(STDERR, "Backup file path " . $args["backup"] . " is not writable (permission error or doesn't exist), aborting" . PHP_EOL); | |
exit(1); | |
} | |
$oldProviderClasses = $providerManager->getMatchingProviders($inputPath); | |
if(count($oldProviderClasses) === 0){ | |
fwrite(STDERR, "Unknown input world format" . PHP_EOL); | |
exit(1); | |
} | |
if(count($oldProviderClasses) > 1){ | |
fwrite(STDERR, "Ambiguous input world format: matched " . count($oldProviderClasses) . " (" . implode(array_keys($oldProviderClasses)) . ")" . PHP_EOL); | |
exit(1); | |
} | |
$oldProviderClass = array_shift($oldProviderClasses); | |
$oldProvider = $oldProviderClass->fromPath($inputPath, new \PrefixedLogger(\GlobalLogger::get(), "Old World Provider")); | |
$converter = new FormatConverter($oldProvider, $writableFormats[$args["format"]], $backupPath, GlobalLogger::get()); | |
$converter->execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment