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.
125 lines
3.5 KiB
PHP
125 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Launcher\FileAction;
|
|
use App\Enums\Launcher\Patchline;
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Filesystem\FilesystemAdapter;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
/**
|
|
* @mixin IdeHelperGameFile
|
|
*/
|
|
class GameFile extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'patchline' => Patchline::class,
|
|
'action' => FileAction::class,
|
|
];
|
|
|
|
protected static ?FilesystemAdapter $disk = null;
|
|
|
|
protected static function booted()
|
|
{
|
|
static::deleting(function (GameFile $gameFile) {
|
|
static::getDisk()->delete($gameFile->getDiskPath());
|
|
});
|
|
|
|
static::saved(fn () => static::forgetModListCache());
|
|
static::deleted(fn () => static::forgetModListCache());
|
|
}
|
|
|
|
public static function modListCacheKey(Patchline $patchline): string
|
|
{
|
|
return "game-mod-files.{$patchline->value}";
|
|
}
|
|
|
|
/**
|
|
* Clears the cached mod lists of every patchline, so a file moved between patchlines can't stay listed in the old one.
|
|
*/
|
|
public static function forgetModListCache(): void
|
|
{
|
|
foreach (Patchline::cases() as $patchline) {
|
|
Cache::forget(static::modListCacheKey($patchline));
|
|
}
|
|
}
|
|
|
|
public static function getDisk(): FilesystemAdapter
|
|
{
|
|
return static::$disk ?? static::$disk = Storage::disk('patches');
|
|
}
|
|
|
|
public function getDiskPath(): string
|
|
{
|
|
return strtolower($this->patchline->name).'/'.$this->filename;
|
|
}
|
|
|
|
public function fileExists(): bool
|
|
{
|
|
return static::getDisk()->exists($this->getDiskPath());
|
|
}
|
|
|
|
/**
|
|
* @return int File size in bytes
|
|
*/
|
|
public function getFileSize(): int
|
|
{
|
|
return static::getDisk()->size($this->getDiskPath());
|
|
}
|
|
|
|
public function child(): BelongsTo
|
|
{
|
|
return $this->belongsTo(GameFile::class);
|
|
}
|
|
|
|
public function getTopParent(): GameFile
|
|
{
|
|
$current = $this;
|
|
while ($current->child) {
|
|
$current = $current->child;
|
|
}
|
|
|
|
return $current;
|
|
}
|
|
|
|
public function scopeWithFileHistory($query): Collection
|
|
{
|
|
return $query->latest()->get()->pipe(function ($allFiles) {
|
|
$filesById = $allFiles->keyBy('id');
|
|
|
|
// Find files that are not referenced as child_id (these are the latest in their chains)
|
|
$referencedIds = $allFiles->pluck('child_id')->filter();
|
|
$latestFiles = $allFiles->whereNotIn('id', $referencedIds);
|
|
|
|
// Build complete history chains for each latest file
|
|
return $latestFiles->map(function ($latestFile) use ($filesById) {
|
|
$history = collect();
|
|
|
|
// Walk backwards through the chain to collect all history
|
|
$currentId = $latestFile->child_id;
|
|
while ($currentId && isset($filesById[$currentId])) {
|
|
$historyFile = $filesById[$currentId];
|
|
$history->push($historyFile);
|
|
$currentId = $historyFile->child_id;
|
|
}
|
|
|
|
$latestFile->children = $history;
|
|
|
|
return $latestFile;
|
|
})->sortByDesc('updated_at');
|
|
});
|
|
}
|
|
}
|