Skip to content

Instantly share code, notes, and snippets.

@dadodasyra
Created November 5, 2023 19:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadodasyra/1e031289fa50ba6c057022641bf5abd1 to your computer and use it in GitHub Desktop.
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.
<?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