This commit is contained in:
Frank Harris 2026-01-17 19:45:24 -06:00
parent b2b46b23db
commit 74fbe0773a
5 changed files with 101 additions and 25 deletions

View file

@ -11,6 +11,8 @@ class SteamWorkshopService
private string $adapterDir;
private string $adapterMapFile;
private string $gameAdapterDir;
private string $workshopConfigDir;
private ?array $workshopAppIds = null;
public function __construct(OGPDatabase $db)
{
@ -19,6 +21,7 @@ class SteamWorkshopService
$this->adapterDir = __DIR__ . '/GameAdapters';
$this->adapterMapFile = __DIR__ . '/../data/game_adapter_map.json';
$this->gameAdapterDir = __DIR__ . '/../data/game_adapters';
$this->workshopConfigDir = __DIR__ . '/../game_configs';
if (!is_dir($this->configDir)) {
mkdir($this->configDir, 0775, true);
@ -501,6 +504,11 @@ class SteamWorkshopService
{
$configDir = defined('SERVER_CONFIG_LOCATION') ? SERVER_CONFIG_LOCATION : __DIR__ . '/../../config_games/server_configs';
$groups = [];
$supportedAppIds = $this->getWorkshopConfigAppIds();
if (empty($supportedAppIds)) {
return [];
}
$supportedLookup = array_flip($supportedAppIds);
foreach (glob($configDir . '/*.xml') as $file) {
$xml = @simplexml_load_file($file);
@ -518,6 +526,10 @@ class SteamWorkshopService
continue;
}
if (!isset($supportedLookup[$appId])) {
continue;
}
$groupKey = $this->buildWorkshopGroupKey($appId);
if (!isset($groups[$groupKey])) {
$gameName = isset($xml->game_name) ? trim((string)$xml->game_name) : '';
@ -707,15 +719,41 @@ class SteamWorkshopService
private function ensureDataFiles(): void
{
$dir = dirname($this->adapterMapFile);
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
$directories = [
$this->configDir,
$this->gameAdapterDir,
dirname($this->adapterMapFile),
];
foreach ($directories as $dir) {
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}
}
if (!is_file($this->adapterMapFile)) {
file_put_contents($this->adapterMapFile, json_encode([]));
file_put_contents($this->adapterMapFile, json_encode([], JSON_PRETTY_PRINT));
}
}
public function gameSupportsWorkshop($serverXml): bool
{
if (!($serverXml instanceof SimpleXMLElement)) {
return false;
}
$installer = trim((string)($serverXml->installer ?? ''));
if ($installer !== 'steamcmd') {
return false;
}
$appId = $this->parseSteamAppIdFromConfig($serverXml);
if ($appId === null) {
return false;
}
return in_array($appId, $this->getWorkshopConfigAppIds(), true);
}
private function parseSteamAppIdFromConfig($xml): ?string
{
if (!isset($xml->mods) || !isset($xml->mods->mod)) {
@ -744,6 +782,39 @@ class SteamWorkshopService
return $candidate;
}
private function getWorkshopConfigAppIds(): array
{
if ($this->workshopAppIds !== null) {
return $this->workshopAppIds;
}
$dir = $this->workshopConfigDir;
if (!is_dir($dir)) {
$this->workshopAppIds = [];
return $this->workshopAppIds;
}
$appIds = [];
foreach (glob($dir . '/*.xml') as $file) {
$xml = @simplexml_load_file($file);
if ($xml === false) {
continue;
}
$appId = preg_replace('/[^0-9]/', '', (string)($xml->workshop_id ?? ''));
if ($appId === '') {
continue;
}
$appIds[$appId] = true;
}
$this->workshopAppIds = array_keys($appIds);
sort($this->workshopAppIds);
return $this->workshopAppIds;
}
private function buildWorkshopGroupKey(string $appId): string
{
return 'steamapp_' . $appId;