Add pre/post events and event cancellation #145
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This pull request adds Pre/Post events and event cancellation as described in the Proposal 1 section of #135.
Cancellable events may be cancelled by any pre event listener by changing the
Cancel
property on it. Similar to harmony patches, even if an event is cancelled, all pre event listeners are still run, and they have the ability to un-cancel the event. If after all of the event listeners have been called theCancel
property is false, the packet won't be sent and post event listeners won't be called.Details (click to expand)
Specifications
EventPriority
. Possible values:Pre
,Post
,All
.Pre
will be called before the packet has been sent to other clients. Certain events can be cancelled in here. If the event doesn't have aPre
call step,Pre
listeners will run onPost
with a warning in the console.Post
will be called after the packet has been send to other clients.All
will be called on bothPre
andPost
. This is useless. I don't know why anyone would ever do this. This is mainly used internally to indicate that an event doesn't have aPre
call step.EventPriorityAttribute
constructor can now take in anEventCallStep
parameter. If none is provided, the default call step isPost
.Example Usage
Event Changes
IGameAlterEvent
: pre/post, can be cancelledIGameCreatedEvent
: pre/post, can be cancelled (TODO: catch exception in Client.cs)IGameDestroyedEvent
: post, cannot be cancelled (TODO: make sure this is actually post)IGameEndedEvent
: post, cannot be cancelled (TODO: make sure this is actually post)IGamePlayerJoinedEvent
: pre/post, can be cancelledIGamePlayerLeftEvent
: post, can't be cancelled (TODO: make sure this is actually post)IGameStartedEvent
: post, cannot be cancelled (TODO: make sure this is actually post)IGameStartingEvent
: pre/post, can be cancelledI will add more events soon. Note that the changed files have some code for other events not listed above. Those are still work in progress, as is this whole pull request.
Internal Changes
<IEventManager>.CallAsync
now has one additional parameter of typeEventCallStep
.EventCallStep.Pre
will run all event handlers listening on thePre
call step.EventCallStep.Post
will run all event handlers listening on thePost
call step.EventCallStep.All
indicates that the event only has a single call step, which is run after packet forwarding. This will run allPost
event listeners as usual, and will run allPre
event listeners with a warning in the log.<IEventManager>.CallAsync
now returnsValueTask<bool>
instead ofValueTask
.Pre
event, the return value will signify if the packet handling should continue or not. (true
for keep going,false
to cancel)Post
orAll
event in a perfect world, the return value will always betrue
. Since we do not live in a perfect world, this return value will befalse
if someone tries to cancel in a post event. As a result, it is recommended to ignore this value.IEventCancelable
toICancellableEvent
closes #92
closes #135