settings = app(SettingsService::class); $this->settings->clearInstanceCache(); $this->githubUrl = $this->settings->getOrDefault('emulator_github_url', ''); $this->jarDirectUrl = $this->settings->getOrDefault('emulator_jar_direct_url', ''); $basePath = $this->detectBasePath(); $this->jarPath = $this->resolveEmulatorPath($this->settings->getOrDefault('emulator_jar_path', $basePath . '/Emulator')); $this->emulatorService = $this->settings->getOrDefault('emulator_service_name', $this->detectEmulatorService()); $this->emulatorSourcePath = $this->resolveEmulatorPath($this->settings->getOrDefault('emulator_source_path', $basePath . '/emulator-source')); $this->sourceRepo = $this->settings->getOrDefault('emulator_source_repo', ''); $this->parseGitHubUrl($this->githubUrl); $this->parseSourceRepo($this->sourceRepo); $this->ensureGitSafeDirectories(); $settingBranch = $this->settings->getOrDefault('emulator_github_branch', null); if ($settingBranch) { $this->githubBranch = strtolower((string) $settingBranch); $this->sourceBranch = strtolower((string) $settingBranch); } } private function detectBasePath(): string { $possiblePaths = [ base_path(), '/var/www/atomcms', '/var/www/html', '/var/www', dirname(base_path()), ]; foreach ($possiblePaths as $path) { if (is_dir($path)) { return $path; } } return '/var/www'; } private function resolveEmulatorPath(string $path): string { if (str_starts_with($path, '/')) { return $path; } return $this->detectBasePath() . '/' . ltrim($path, '/'); } private function detectEmulatorService(): string { $possibleServices = ['emulator', 'arcturus', 'morningstar', 'habbo', 'hotel', 'game']; foreach ($possibleServices as $service) { $result = Process::timeout(5)->run("systemctl list-unit-files {$service}.service 2>/dev/null | grep -q '{$service}.service' && echo 'found'"); if ($result->successful() && trim($result->output()) === 'found') { return $service; } $result = Process::timeout(5)->run("systemctl list-units --type=service --all 2>/dev/null | grep -q '{$service}.service' && echo 'found'"); if ($result->successful() && trim($result->output()) === 'found') { return $service; } } return 'emulator'; } private function ensureGitSafeDirectories(): void { $directories = [ base_path(), '/var/www/atomcms', '/var/www', $this->emulatorSourcePath, $this->jarPath, dirname((string) $this->emulatorSourcePath), dirname((string) $this->jarPath), ]; foreach (array_unique($directories) as $dir) { if (! empty($dir) && $this->isDirAccessible($dir)) { Process::timeout(5)->run('git config --global --add safe.directory ' . escapeshellarg($dir) . ' 2>/dev/null || true'); } } Process::timeout(5)->run("git config --global --add safe.directory '*' 2>/dev/null || true"); } private function isDirAccessible(string $path): bool { if ($path === '' || $path === '0') { return false; } $result = Process::timeout(5)->run('test -d ' . escapeshellarg($path) . ' && echo "yes" || echo "no"'); return trim($result->output()) === 'yes'; } private function parseGitHubUrl(string $url): void { $parsed = $this->parseGithubRepoUrl($url); $this->githubRepo = $parsed['repo']; $this->githubBranch = $parsed['branch']; } private function parseSourceRepo(string $url): void { $parsed = $this->parseGithubRepoUrl($url); $this->sourceRepo = $parsed['repo']; $this->sourceBranch = $parsed['branch']; } private function parseGithubRepoUrl(string $url): array { if ($url === '' || $url === '0') { return ['repo' => null, 'branch' => 'main']; } if (preg_match('/github\.com\/([^\/]+)\/([^\/\?#]+)/', $url, $matches)) { $repo = $matches[1] . '/' . $matches[2]; $branch = 'main'; if (preg_match('/\/tree\/([^\/]+)/', $url, $branchMatch)) { $branch = $branchMatch[1]; } return ['repo' => $repo, 'branch' => $branch]; } return ['repo' => null, 'branch' => 'main']; } protected function extractVersionFromFilename(string $filename): string { $filename = basename($filename, '.jar'); if (preg_match('/[\d]+\.[\d]+\.[\d]+/', $filename, $matches)) { return $matches[0]; } if (preg_match('/v?([\d]+)/', $filename, $matches)) { return $matches[1]; } return ''; } protected function commandDirExists(string $path): bool { $result = Process::timeout(5)->run('[ -d ' . escapeshellarg($path) . ' ] && echo "1" || echo "0"'); return trim($result->output()) === '1'; } protected function commandFileExists(string $path): bool { $result = Process::timeout(5)->run('ls ' . $path . ' 2>/dev/null | head -1'); return $result->successful() && trim($result->output()) !== ''; } }