mirror of
https://github.com/Deathgarden-Rebirth/Deathgarden_Rebirth-Rewrite.git
synced 2026-12-05 08:44:59 -06:00
Launchers request the mod file list every 10 seconds. Cache it per patchline as a plain array and clear every patchline's entry whenever a GameFile is saved or deleted, so a file moved between patchlines can't stay listed in the old one. Also applies Pint formatting to PatchController and GameFile.
110 lines
3.8 KiB
PHP
110 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Enums\Launcher\Patchline;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\GameFile;
|
|
use Auth;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
class PatchController extends Controller
|
|
{
|
|
public function getFileWithPatchline(string $patchlineName, string $hash): BinaryFileResponse|JsonResponse
|
|
{
|
|
$patchline = Patchline::tryFromName(str($patchlineName)->upper());
|
|
|
|
$gameFile = GameFile::select(['filename'])->wherePatchline($patchline)->whereHash($hash)->latest()->first();
|
|
$disk = Storage::disk('patches');
|
|
|
|
if ($gameFile === null) {
|
|
return response()->json('File not found', 404);
|
|
}
|
|
|
|
$filePath = DIRECTORY_SEPARATOR.str($patchlineName)->lower().DIRECTORY_SEPARATOR.$gameFile->filename;
|
|
|
|
if (! $disk->exists($filePath)) {
|
|
return response()->json('File not found', 404);
|
|
}
|
|
|
|
return response()->download($disk->path($filePath));
|
|
}
|
|
|
|
public function getFile(string $hash): BinaryFileResponse|JsonResponse
|
|
{
|
|
return $this->getFileWithPatchline(Patchline::LIVE->name, $hash);
|
|
}
|
|
|
|
public function getGameFileList(?string $patchlineName = null): JsonResponse
|
|
{
|
|
$patchline = Patchline::tryFromName(str($patchlineName)->upper()) ?? Patchline::LIVE;
|
|
|
|
if (! $patchline) {
|
|
return response()->json(['error' => 'Invalid patchline'], 404);
|
|
}
|
|
|
|
$neededRole = $patchline->getNeededRole();
|
|
|
|
if ($neededRole !== null && (! Auth::check() || ! Auth::user()->hasRole($neededRole->value))) {
|
|
return response()->json(['error' => 'Unauthorized'], 401);
|
|
}
|
|
|
|
$gameFiles = GameFile::where('patchline', $patchline->value)
|
|
->where('is_additional', 0)
|
|
->withFileHistory()
|
|
->map(function ($latestFile) {
|
|
$processedHashes = [];
|
|
$history = collect();
|
|
|
|
foreach ($latestFile->children as $historyFile) {
|
|
if (in_array($historyFile->hash, $processedHashes)) {
|
|
continue;
|
|
}
|
|
|
|
$processedHashes[] = $historyFile->hash;
|
|
$history->push($historyFile->only(['hash']));
|
|
}
|
|
|
|
$selectedFile = $latestFile->only(['filename', 'hash', 'game_path', 'action']);
|
|
$selectedFile['history'] = $history;
|
|
|
|
return $selectedFile;
|
|
});
|
|
|
|
if (count($gameFiles) <= 0) {
|
|
return response()->json(['error' => 'No files found'], 404);
|
|
}
|
|
|
|
return response()->json($gameFiles, 200);
|
|
}
|
|
|
|
public function getGameModFileList(?string $patchlineName = null): JsonResponse
|
|
{
|
|
$patchline = Patchline::tryFromName(str($patchlineName)->upper()) ?? Patchline::LIVE;
|
|
|
|
if (! $patchline) {
|
|
return response()->json(['error' => 'Invalid patchline'], 404);
|
|
}
|
|
|
|
$neededRole = $patchline->getNeededRole();
|
|
|
|
if ($neededRole !== null && (! Auth::check() || ! Auth::user()->hasRole($neededRole->value))) {
|
|
return response()->json(['error' => 'Unauthorized'], 401);
|
|
}
|
|
|
|
// Launchers poll this every 10 seconds. Plain arrays are cached, GameFile clears the entry when a file changes.
|
|
$gameFiles = Cache::rememberForever(GameFile::modListCacheKey($patchline), fn () => GameFile::select(['name', 'description', 'filename', 'hash', 'game_path', 'action'])
|
|
->where('patchline', $patchline->value)
|
|
->where('is_additional', 1)->latest()->get()->toArray());
|
|
|
|
if (count($gameFiles) <= 0) {
|
|
return response()->json(['error' => 'No files found'], 404);
|
|
}
|
|
|
|
return response()->json($gameFiles, 200);
|
|
}
|
|
}
|