Skip to content

Commit

Permalink
Addon Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
RuriYS committed Aug 26, 2024
1 parent 449af4f commit 4e6ba09
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
namespace Pterodactyl\Http\Controllers\Api\Client\Servers\AddonManager;

use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Repositories\Wings\DaemonFileRepository;
use Illuminate\Support\Facades\Cache;
use Aternos\Taskmaster\Taskmaster;
use Pterodactyl\Models\Server;
use Illuminate\Http\Request;
use Yosymfony\Toml\Toml;
use ReadAndParseTask;
use Http;

class AddonController extends ClientApiController
Expand Down Expand Up @@ -40,15 +42,36 @@ public function index(Request $request, Server $server)
return response()->json(['error' => 'Unsupported egg'], 400);
}

$indexDirectory = $serverFileRepo->getDirectory("$addonDirectory.index/");
$addons = [];
$cacheKey = "server_{$server->id}_addons";
$taskmaster = new Taskmaster();
$taskmaster->autoDetectWorkers(8);

foreach ($indexDirectory as $file) {
$filename = $file['name'];
$content = $serverFileRepo->getContent("$addonDirectory.index/$filename");
$addonData = Toml::parse($content);
$addons[] = $addonData;
}
$addons = Cache::remember($cacheKey, now()->addMinutes(30), function () use ($serverFileRepo, $addonDirectory, $taskmaster) {
$addons = [];
$tasks = [];
$indexDirectory = $serverFileRepo->getDirectory("$addonDirectory.index/");

foreach ($indexDirectory as $file) {
$filename = $file['name'];
$filePath = "$addonDirectory.index/$filename";

// Create a new task for each file and run them
$task = new ReadAndParseTask($serverFileRepo, $filePath);
$tasks[] = $task;
$taskmaster->runTask($task);
}

$taskmaster->wait();

// Collect results from all tasks
foreach ($tasks as $task) {
$addons[] = $task->getResult();
}

$taskmaster->stop();

return $addons;
});

return response()->json($addons);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Aternos\Taskmaster\Task\Task;
use Yosymfony\Toml\Toml;

class ReadAndParseTask extends Task
{
protected $serverFileRepo;
protected $filePath;

public function __construct($serverFileRepo, $filePath)
{
$this->serverFileRepo = $serverFileRepo;
$this->filePath = $filePath;
}

public function run(): mixed
{
$content = $this->serverFileRepo->getContent($this->filePath);
return Toml::parse($content);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"ext-pdo_mysql": "*",
"ext-posix": "*",
"ext-zip": "*",
"aternos/taskmaster": "^1.2",
"aws/aws-sdk-php": "~3.260.1",
"doctrine/dbal": "~3.6.0",
"guzzlehttp/guzzle": "~7.5.0",
Expand Down
50 changes: 49 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified package-lock.json
100644 → 100755
Empty file.
Empty file modified resources/scripts/components/elements/AddonItem.tsx
100644 → 100755
Empty file.
26 changes: 15 additions & 11 deletions resources/scripts/components/server/addons/AddonsContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Addon, index as getIndex, search, limit, providers } from '@/api/server/addons';
import ServerContentBlock from '@/components/elements/ServerContentBlock';
import AddonItem from '@/components/elements/AddonItem';
import Spinner from '@/components/elements/Spinner';
import { Addon, index as getIndex, search, limit, providers } from '@/api/server/addons';
import React, { useState, useEffect } from 'react';
import { ServerContext } from '@/state/server';
import Select from '@/components/elements/Select';
Expand All @@ -25,19 +25,23 @@ const AddonContainer = () => {
const handler = setTimeout(async () => {
const fetchAddons = async () => {
setLoading(true);
const searchData = (await search(uuid, searchTerm, pageSize, provider)).data;
const indexData = (await getIndex(uuid)).data;
try {
const searchData = (await search(uuid, searchTerm, pageSize, provider)).data;
const indexData = (await getIndex(uuid)).data;

const addonsData = searchData.hits.map((hit) => {
const isInstalled = indexData.some((indexed) => {
if (!indexed.update.modrinth) return false;
return indexed.update.modrinth['mod-id'] === hit.project_id;
});
const addonsData = searchData.hits.map((hit) => {
const isInstalled = indexData.some((indexed) => {
if (!indexed.update.modrinth) return false;
return indexed.update.modrinth['mod-id'] === hit.project_id;
});

return { ...hit, isInstalled };
});
return { ...hit, isInstalled };
});

setAddons(addonsData);
setAddons(addonsData);
} catch (error) {
console.error(error);
}
setLoading(false);
};

Expand Down
Empty file modified resources/scripts/components/server/console/Console.tsx
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions routes/api-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@
});

Route::group(['prefix' => '/addons'], function () {
Route::get('/', [Client\Servers\AddonController::class, 'index']);
Route::get('/search', [Client\Servers\AddonController::class, 'search']);
Route::get('/download', [Client\Servers\AddonController::class, 'download']);
Route::get('/versions', [Client\Servers\AddonController::class, 'getVersions']);
Route::get('/', [Client\Servers\AddonManager\AddonController::class, 'index']);
Route::get('/search', [Client\Servers\AddonManager\AddonController::class, 'search']);
Route::get('/download', [Client\Servers\AddonManager\AddonController::class, 'download']);
Route::get('/versions', [Client\Servers\AddonManager\AddonController::class, 'getVersions']);
});

Route::group(['prefix' => '/schedules'], function () {
Expand Down

0 comments on commit 4e6ba09

Please sign in to comment.