Created
March 11, 2022 19:26
-
-
Save book000/a054b6ac60e5d20c50aace8babd17243 to your computer and use it in GitHub Desktop.
手動でMinecraft(Spigot)プラグインをアップデートするのを助けるスクリプト
This file contains hidden or 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 | |
/* | |
plugins-oldディレクトリに古いプラグインjarをすべて入れて、実行するとplugin.ymlの内容をもとに関連サイトを開いたり、ダウンロード処理を行ったりする | |
*/ | |
function getFiles($dir) | |
{ | |
$files = scandir($dir); | |
$files = array_diff($files, array('.', '..')); | |
$files = array_filter($files, function ($file) use ($dir) { | |
return is_file($dir . DIRECTORY_SEPARATOR . $file); | |
}); | |
$files = array_values($files); | |
return $files; | |
} | |
function getPlugin($path) | |
{ | |
$yml = file_get_contents("zip://" . $path . "#plugin.yml"); | |
preg_match("/name: (.*)/", $yml, $matches); | |
$name = $matches[1]; | |
preg_match("/version: (.*)/", $yml, $matches); | |
$version = $matches[1]; | |
preg_match("/website: (.*)/", $yml, $matches); | |
$website = isset($matches[1]) ? $matches[1] : null; | |
return [ | |
"name" => trim($name), | |
"version" => trim($version), | |
"website" => $website, | |
"path" => $path | |
]; | |
} | |
function fileNameReplacement($path) | |
{ | |
$path = str_replace("\\", "_", $path); | |
$path = str_replace("/", "_", $path); | |
$path = str_replace(":", "_", $path); | |
$path = str_replace("*", "_", $path); | |
$path = str_replace("?", "_", $path); | |
$path = str_replace("\"", "_", $path); | |
$path = str_replace("<", "_", $path); | |
$path = str_replace(">", "_", $path); | |
$path = str_replace("|", "_", $path); | |
return $path; | |
} | |
$files = getFiles(__DIR__ . "/plugins/"); | |
$already_plugins = array_map(function ($path) { | |
return getPlugin(__DIR__ . "/plugins/" . $path); | |
}, $files); | |
$old_files = getFiles(__DIR__ . "/plugins-old/"); | |
foreach ($old_files as $file) { | |
$plugin = getPlugin(__DIR__ . "/plugins-old/" . $file); | |
echo "name: " . $plugin["name"] . "\n"; | |
echo "version: " . $plugin["version"] . "\n"; | |
echo "website: " . $plugin["website"] . "\n"; | |
echo "path: " . $plugin["path"] . "\n"; | |
$already = array_filter($already_plugins, function ($p) use ($plugin) { | |
return $p["name"] == $plugin["name"]; | |
}); | |
$already = array_values($already); | |
if (count($already) > 0) { | |
$v = $already[0]["version"]; | |
echo "Already exists({$v}). skip\n\n\n"; | |
continue; | |
} | |
if ($plugin["website"] !== null) { | |
exec("start \"\" \"" . $plugin["website"] . "\""); | |
} else { | |
exec("start \"\" \"https://google.com/search?q=" . $plugin["name"] . "\""); | |
} | |
$url = ""; | |
while ($url === "") { | |
echo "\nDL URL(s:skip/c:copy): "; | |
$url = trim(fgets(STDIN)); | |
} | |
if ($url === "c") { | |
copy(__DIR__ . "/plugins-old/" . $file, __DIR__ . "/plugins/" . $file); | |
echo "Copy...\n\n"; | |
continue; | |
} | |
if ($url === "s") { | |
echo "Skip...\n\n"; | |
continue; | |
} | |
system("wget -O " . __DIR__ . "/temp.jar " . $url); | |
$newPluginName = fileNameReplacement(getPlugin(__DIR__ . "/temp.jar")["name"]); | |
$newPluginVersion = fileNameReplacement(getPlugin(__DIR__ . "/temp.jar")["version"]); | |
rename(__DIR__ . "/temp.jar", __DIR__ . "/plugins/" . $newPluginName . "-" . $newPluginVersion . ".jar"); | |
echo $newPluginName . "-" . $newPluginVersion . ".jar\n"; | |
echo "-> Download successfully\n"; | |
echo "\n\n\n"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment