From 2c9ea14b785741ba551c7b9b75da92fa695fa99b Mon Sep 17 00:00:00 2001 From: Alexander Nell Date: Thu, 21 May 2020 20:51:51 +0200 Subject: [PATCH] New Adhoc-Callbacks which get triggered after the callback that added them --- changelog.txt | 1 + core/Callbacks/CallbackManager.php | 41 +++++++++++++++++++++++++- core/Callbacks/TrackManiaCallbacks.php | 10 ++----- 3 files changed, 44 insertions(+), 8 deletions(-) diff --git a/changelog.txt b/changelog.txt index 43e56256..51772c09 100644 --- a/changelog.txt +++ b/changelog.txt @@ -8,6 +8,7 @@ - add Setting to hide Script-Settings which have the description "" - add Setting to sort GameMode-Settings - CSV-Export of Locals (//exportrecs [map-id]) +- new Adhoc-Callbacks, which get executed after the Callback that added them - new GameModePresetPlugin to save and load GameMode-Settings (//loadmode & //savemode ) #Bug Fixes diff --git a/core/Callbacks/CallbackManager.php b/core/Callbacks/CallbackManager.php index 5bbeade1..83dce9ff 100644 --- a/core/Callbacks/CallbackManager.php +++ b/core/Callbacks/CallbackManager.php @@ -63,6 +63,8 @@ class CallbackManager implements UsageInformationAble { /** @var ManiaControl $maniaControl */ private $maniaControl = null; + /** @var Listening[] $adhocCallbacks */ + private $adhocCallbacks = array(); /** @var Listening[][] $callbackListenings */ private $callbackListenings = array(); /** @var Listening[][] $scriptCallbackListenings */ @@ -221,6 +223,42 @@ class CallbackManager implements UsageInformationAble { return $this->removeCallbackListener($this->scriptCallbackListenings, $listener); } + /** + * @internal + * Adds an adhoc Callback to be executed immediately after other callbacks. + * Should be used for more specialized Callbacks, which need to be executed soon after a more general callback. + * + * @param mixed $callbackName + */ + public function addAdhocCallback($callbackName) { + if (!$this->callbackListeningExists($callbackName)) { + return; + } + + $params = func_get_args(); + $params = array_slice($params, 1, null, true); + array_push($this->adhocCallbacks, array($callbackName, $params)); + } + + /** + * @internal + * Trigger internal adhoc Callbacks between manageCallbacks() + */ + private function manageAdhocCallbacks() { + // TODO add some timing method to determine long loop issues + // currently it adds to the non-specialized callback which added the adhoc callback + + foreach ($this->adhocCallbacks as $callback) { + list($callbackName, $params) = $callback; + foreach ($this->callbackListenings[$callbackName] as $listening) { + /** @var Listening $listening */ + $listening->triggerCallbackWithParams($params); + } + } + + $this->adhocCallbacks = array(); + } + /** * Trigger internal Callbacks and manage Server Callbacks */ @@ -253,6 +291,8 @@ class CallbackManager implements UsageInformationAble { foreach ($callbacks as $key => $callback) { $time1 = microtime(true); $this->handleCallback($callback); + // manage any callbacks added by the previous callback + $this->manageAdhocCallbacks(); $timings[$key] = array($callback[0], microtime(true) - $time1); } @@ -314,7 +354,6 @@ class CallbackManager implements UsageInformationAble { * @param mixed $callbackName */ public function triggerCallback($callbackName) { - if (!$this->callbackListeningExists($callbackName)) { return; } diff --git a/core/Callbacks/TrackManiaCallbacks.php b/core/Callbacks/TrackManiaCallbacks.php index 4dedae7e..5d92e0b8 100644 --- a/core/Callbacks/TrackManiaCallbacks.php +++ b/core/Callbacks/TrackManiaCallbacks.php @@ -22,7 +22,7 @@ use ManiaControl\ManiaControl; * @copyright 2014-2020 ManiaControl Team * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ -class TrackManiaCallbacks implements CallbackListener, CallQueueListener { +class TrackManiaCallbacks implements CallbackListener { /* * Private properties */ @@ -106,13 +106,9 @@ class TrackManiaCallbacks implements CallbackListener, CallQueueListener { */ public function handleWayPointCallback(OnWayPointEventStructure $structure) { if ($structure->getIsEndRace()) { - $this->maniaControl->getCallQueueManager()->registerListening($this, function () use ($structure) { - $this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONFINISHLINE, $structure); - }); + $this->maniaControl->getCallbackManager()->addAdhocCallback(Callbacks::TM_ONFINISHLINE, $structure); } else if ($structure->getIsEndLap()) { - $this->maniaControl->getCallQueueManager()->registerListening($this, function () use ($structure) { - $this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONLAPFINISH, $structure); - }); + $this->maniaControl->getCallbackManager()->addAdhocCallback(Callbacks::TM_ONLAPFINISH, $structure); } } }