- CheckModpackUpdates now scans ALL servers, not just egg-configured ones - Step 1: egg variables (fastest) - Step 2: DaemonFileRepository file detection (manifest.json, modrinth.index.json) - Step 3: mark unconfigured if nothing found - Respects is_user_overridden flag for manual configs - New migration adds detection_method + is_user_overridden columns - Per Gemini hybrid detection consultation (2026-04-06) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
220 lines
7.5 KiB
PHP
220 lines
7.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* ModpackChecker Cron Command — hybrid "Magic & Manual" detection.
|
|
*
|
|
* 1. Egg variables (fastest)
|
|
* 2. File detection via DaemonFileRepository (manifest.json, modrinth.index.json)
|
|
* 3. API version check for detected packs
|
|
*
|
|
* USAGE: php artisan modpackchecker:check
|
|
* CRON: 0 0,6,12,18 * * * cd /var/www/pterodactyl && php artisan modpackchecker:check
|
|
*/
|
|
|
|
namespace Pterodactyl\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Pterodactyl\Models\Server;
|
|
use Pterodactyl\Repositories\Wings\DaemonFileRepository;
|
|
use Pterodactyl\Services\ModpackApiService;
|
|
|
|
class CheckModpackUpdates extends Command
|
|
{
|
|
protected $signature = 'modpackchecker:check';
|
|
protected $description = 'Check all servers for modpack updates (hybrid detection)';
|
|
|
|
public function __construct(
|
|
private ModpackApiService $apiService,
|
|
private DaemonFileRepository $fileRepository
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
public function handle(): int
|
|
{
|
|
$this->info('Starting modpack update check (hybrid detection)...');
|
|
|
|
$servers = Server::all();
|
|
$this->info("Scanning {$servers->count()} servers");
|
|
|
|
foreach ($servers as $server) {
|
|
$this->processServer($server);
|
|
sleep(2);
|
|
}
|
|
|
|
$this->info('Modpack update check complete!');
|
|
return 0;
|
|
}
|
|
|
|
private function processServer(Server $server): void
|
|
{
|
|
$this->line("Checking: {$server->name} ({$server->uuid})");
|
|
|
|
// Skip user-overridden servers (manual config)
|
|
$existing = DB::table('modpackchecker_servers')
|
|
->where('server_uuid', $server->uuid)
|
|
->first();
|
|
|
|
if ($existing && $existing->is_user_overridden) {
|
|
// Still check for updates, just don't re-detect
|
|
if ($existing->platform && $existing->modpack_id) {
|
|
$this->checkVersion($server, $existing->platform, $existing->modpack_id, 'manual');
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Step 1: Try egg variables
|
|
$platform = $this->getVariable($server, 'MODPACK_PLATFORM');
|
|
$modpackId = $this->getVariable($server, 'MODPACK_ID');
|
|
|
|
if (!empty($modpackId)) {
|
|
$modpackId = $modpackId ?: match($platform) {
|
|
'curseforge' => $this->getVariable($server, 'CURSEFORGE_ID'),
|
|
'modrinth' => $this->getVariable($server, 'MODRINTH_PROJECT_ID'),
|
|
'ftb' => $this->getVariable($server, 'FTB_MODPACK_ID'),
|
|
'technic' => $this->getVariable($server, 'TECHNIC_SLUG'),
|
|
default => null
|
|
};
|
|
}
|
|
|
|
if (!empty($platform) && !empty($modpackId)) {
|
|
$this->checkVersion($server, $platform, $modpackId, 'egg');
|
|
return;
|
|
}
|
|
|
|
// Step 2: File-based detection via DaemonFileRepository
|
|
$detected = $this->detectFromFiles($server);
|
|
if ($detected) {
|
|
$this->checkVersion($server, $detected['platform'], $detected['modpack_id'], 'file');
|
|
return;
|
|
}
|
|
|
|
// Step 3: Nothing found
|
|
$this->warn(" No modpack detected");
|
|
$this->updateDatabase($server, [
|
|
'status' => 'unconfigured',
|
|
'detection_method' => 'unknown',
|
|
'last_checked' => now(),
|
|
]);
|
|
}
|
|
|
|
private function detectFromFiles(Server $server): ?array
|
|
{
|
|
try {
|
|
$this->fileRepository->setServer($server);
|
|
} catch (\Exception $e) {
|
|
return null;
|
|
}
|
|
|
|
// CurseForge: manifest.json
|
|
$cf = $this->detectCurseForge($server);
|
|
if ($cf) return $cf;
|
|
|
|
// Modrinth: modrinth.index.json
|
|
$mr = $this->detectModrinth($server);
|
|
if ($mr) return $mr;
|
|
|
|
return null;
|
|
}
|
|
|
|
private function detectCurseForge(Server $server): ?array
|
|
{
|
|
try {
|
|
$content = $this->fileRepository->getContent('manifest.json');
|
|
$manifest = json_decode($content, true);
|
|
|
|
if (is_array($manifest) && ($manifest['manifestType'] ?? '') === 'minecraftModpack') {
|
|
$projectId = $manifest['projectID'] ?? null;
|
|
if ($projectId) {
|
|
$this->info(" Detected CurseForge pack (projectID: {$projectId})");
|
|
return [
|
|
'platform' => 'curseforge',
|
|
'modpack_id' => (string) $projectId,
|
|
'name' => $manifest['name'] ?? null,
|
|
];
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
// File doesn't exist or node offline
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private function detectModrinth(Server $server): ?array
|
|
{
|
|
try {
|
|
$content = $this->fileRepository->getContent('modrinth.index.json');
|
|
$data = json_decode($content, true);
|
|
|
|
if (is_array($data) && isset($data['formatVersion'])) {
|
|
$slug = isset($data['name'])
|
|
? preg_replace('/[^a-z0-9-]/', '', strtolower(str_replace(' ', '-', $data['name'])))
|
|
: null;
|
|
if ($slug) {
|
|
$this->info(" Detected Modrinth pack (slug: {$slug})");
|
|
return [
|
|
'platform' => 'modrinth',
|
|
'modpack_id' => $slug,
|
|
'name' => $data['name'] ?? null,
|
|
];
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
// File doesn't exist or node offline
|
|
}
|
|
return null;
|
|
}
|
|
|
|
private function checkVersion(Server $server, string $platform, string $modpackId, string $method): void
|
|
{
|
|
try {
|
|
$latestData = $this->apiService->fetchLatestVersion($platform, $modpackId);
|
|
$currentVersion = $this->getVariable($server, 'MODPACK_CURRENT_VERSION');
|
|
$updateAvailable = $currentVersion && $currentVersion !== $latestData['version'];
|
|
|
|
$this->updateDatabase($server, [
|
|
'platform' => $platform,
|
|
'modpack_id' => $modpackId,
|
|
'modpack_name' => $latestData['name'],
|
|
'current_version' => $currentVersion,
|
|
'latest_version' => $latestData['version'],
|
|
'status' => $updateAvailable ? 'update_available' : 'up_to_date',
|
|
'detection_method' => $method,
|
|
'error_message' => null,
|
|
'last_checked' => now(),
|
|
]);
|
|
|
|
$icon = $updateAvailable ? '🟠 UPDATE' : '🟢 OK';
|
|
$this->info(" {$icon}: {$latestData['name']} — {$latestData['version']} [{$method}]");
|
|
|
|
} catch (\Exception $e) {
|
|
$this->error(" Error: {$e->getMessage()}");
|
|
$this->updateDatabase($server, [
|
|
'platform' => $platform,
|
|
'modpack_id' => $modpackId,
|
|
'detection_method' => $method,
|
|
'status' => 'error',
|
|
'error_message' => $e->getMessage(),
|
|
'last_checked' => now(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function getVariable(Server $server, string $name): ?string
|
|
{
|
|
$variable = $server->variables()
|
|
->where('env_variable', $name)
|
|
->first();
|
|
return $variable?->server_value;
|
|
}
|
|
|
|
private function updateDatabase(Server $server, array $data): void
|
|
{
|
|
DB::table('modpackchecker_servers')->updateOrInsert(
|
|
['server_uuid' => $server->uuid],
|
|
array_merge($data, ['updated_at' => now()])
|
|
);
|
|
}
|
|
}
|