New Adhoc-Callbacks which get triggered after the callback that added them

This commit is contained in:
Alexander Nell 2020-05-21 20:51:51 +02:00
parent 14c03d6323
commit 2c9ea14b78
3 changed files with 44 additions and 8 deletions

View File

@ -8,6 +8,7 @@
- add Setting to hide Script-Settings which have the description "<hidden>" - add Setting to hide Script-Settings which have the description "<hidden>"
- add Setting to sort GameMode-Settings - add Setting to sort GameMode-Settings
- CSV-Export of Locals (//exportrecs <filename> [map-id]) - CSV-Export of Locals (//exportrecs <filename> [map-id])
- new Adhoc-Callbacks, which get executed after the Callback that added them
- new GameModePresetPlugin to save and load GameMode-Settings (//loadmode <name> & //savemode <name>) - new GameModePresetPlugin to save and load GameMode-Settings (//loadmode <name> & //savemode <name>)
#Bug Fixes #Bug Fixes

View File

@ -63,6 +63,8 @@ class CallbackManager implements UsageInformationAble {
/** @var ManiaControl $maniaControl */ /** @var ManiaControl $maniaControl */
private $maniaControl = null; private $maniaControl = null;
/** @var Listening[] $adhocCallbacks */
private $adhocCallbacks = array();
/** @var Listening[][] $callbackListenings */ /** @var Listening[][] $callbackListenings */
private $callbackListenings = array(); private $callbackListenings = array();
/** @var Listening[][] $scriptCallbackListenings */ /** @var Listening[][] $scriptCallbackListenings */
@ -221,6 +223,42 @@ class CallbackManager implements UsageInformationAble {
return $this->removeCallbackListener($this->scriptCallbackListenings, $listener); 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 * Trigger internal Callbacks and manage Server Callbacks
*/ */
@ -253,6 +291,8 @@ class CallbackManager implements UsageInformationAble {
foreach ($callbacks as $key => $callback) { foreach ($callbacks as $key => $callback) {
$time1 = microtime(true); $time1 = microtime(true);
$this->handleCallback($callback); $this->handleCallback($callback);
// manage any callbacks added by the previous callback
$this->manageAdhocCallbacks();
$timings[$key] = array($callback[0], microtime(true) - $time1); $timings[$key] = array($callback[0], microtime(true) - $time1);
} }
@ -314,7 +354,6 @@ class CallbackManager implements UsageInformationAble {
* @param mixed $callbackName * @param mixed $callbackName
*/ */
public function triggerCallback($callbackName) { public function triggerCallback($callbackName) {
if (!$this->callbackListeningExists($callbackName)) { if (!$this->callbackListeningExists($callbackName)) {
return; return;
} }

View File

@ -22,7 +22,7 @@ use ManiaControl\ManiaControl;
* @copyright 2014-2020 ManiaControl Team * @copyright 2014-2020 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
class TrackManiaCallbacks implements CallbackListener, CallQueueListener { class TrackManiaCallbacks implements CallbackListener {
/* /*
* Private properties * Private properties
*/ */
@ -106,13 +106,9 @@ class TrackManiaCallbacks implements CallbackListener, CallQueueListener {
*/ */
public function handleWayPointCallback(OnWayPointEventStructure $structure) { public function handleWayPointCallback(OnWayPointEventStructure $structure) {
if ($structure->getIsEndRace()) { if ($structure->getIsEndRace()) {
$this->maniaControl->getCallQueueManager()->registerListening($this, function () use ($structure) { $this->maniaControl->getCallbackManager()->addAdhocCallback(Callbacks::TM_ONFINISHLINE, $structure);
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONFINISHLINE, $structure);
});
} else if ($structure->getIsEndLap()) { } else if ($structure->getIsEndLap()) {
$this->maniaControl->getCallQueueManager()->registerListening($this, function () use ($structure) { $this->maniaControl->getCallbackManager()->addAdhocCallback(Callbacks::TM_ONLAPFINISH, $structure);
$this->maniaControl->getCallbackManager()->triggerCallback(Callbacks::TM_ONLAPFINISH, $structure);
});
} }
} }
} }