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) { $this->line(" [debug] Cannot connect to Wings: " . $e->getMessage()); 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; // FTB: version.json $ftb = $this->detectFtb($server); if ($ftb) return $ftb; return null; } private function detectCurseForge(Server $server): ?array { $paths = ['manifest.json', 'minecraftinstance.json']; foreach ($paths as $path) { try { $content = $this->fileRepository->getContent($path); $data = json_decode($content, true); if (!is_array($data)) continue; // Standard manifest.json (CurseForge export) if ($path === 'manifest.json' && ($data['manifestType'] ?? '') === 'minecraftModpack') { $projectId = $data['projectID'] ?? null; if ($projectId) { $this->info(" Detected CurseForge via {$path} (projectID: {$projectId})"); return [ 'platform' => 'curseforge', 'modpack_id' => (string) $projectId, 'name' => $data['name'] ?? null, ]; } } // minecraftinstance.json (CurseForge launcher install) if ($path === 'minecraftinstance.json' && isset($data['projectID'])) { $this->info(" Detected CurseForge via {$path} (projectID: {$data['projectID']})"); return [ 'platform' => 'curseforge', 'modpack_id' => (string) $data['projectID'], 'name' => $data['name'] ?? null, ]; } } catch (\Exception $e) { $this->line(" [debug] {$path}: " . $e->getMessage()); } } 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) { $this->line(" [debug] modrinth.index.json: " . $e->getMessage()); } return null; } private function detectFtb(Server $server): ?array { try { $content = $this->fileRepository->getContent('version.json'); $data = json_decode($content, true); if (is_array($data) && isset($data['id']) && isset($data['parent'])) { $this->info(" Detected FTB pack (id: {$data['parent']})"); return [ 'platform' => 'ftb', 'modpack_id' => (string) $data['parent'], 'name' => $data['name'] ?? null, ]; } } catch (\Exception $e) { $this->line(" [debug] version.json (FTB): " . $e->getMessage()); } 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()]) ); } }