Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre/post events and event cancellation #145

Closed
wants to merge 3 commits into from
Closed

Add pre/post events and event cancellation #145

wants to merge 3 commits into from

Conversation

Alexejhero
Copy link
Contributor

@Alexejhero Alexejhero commented Nov 12, 2020

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 the Cancel property is false, the packet won't be sent and post event listeners won't be called.


Details (click to expand)

Specifications

  • New enum: EventPriority. Possible values: Pre, Post, All.
    • Event listeners for 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 a Pre call step, Pre listeners will run on Post with a warning in the console.
    • Event listeners for Post will be called after the packet has been send to other clients.
    • Event listeners for All will be called on both Pre and Post. 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 a Pre call step.
  • EventPriorityAttribute constructor can now take in an EventCallStep parameter. If none is provided, the default call step is Post.

Example Usage

[EventPriority(EventCallStep.Pre)]
public ValueTask(IPlayerChatEvent e)
{
    if (e.Message.StartsWith("/")) e.Cancel = true;
}

Event Changes

  • IGameAlterEvent: pre/post, can be cancelled
    • sender will receive reverse packet (TODO: make sure this works)
    • other clients won't receive the packet
  • IGameCreatedEvent: pre/post, can be cancelled (TODO: catch exception in Client.cs)
    • sender will be stuck in a black screen for 10s as if servers aren't available (TODO: kick them instead)
    • other clients won't receive the packet
  • 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 cancelled
    • sender will think that the game was destroyed (TODO: should we use a custom message for this?)
    • other clients won't receive the packet
  • IGamePlayerLeftEvent: 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 cancelled
    • idk what happens to the sender (TODO: find out)
    • other clients won't receive the packet

I 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 type EventCallStep.
    • Specifying EventCallStep.Pre will run all event handlers listening on the Pre call step.
    • Specifying EventCallStep.Post will run all event handlers listening on the Post call step.
    • Specifying EventCallStep.All indicates that the event only has a single call step, which is run after packet forwarding. This will run all Post event listeners as usual, and will run all Pre event listeners with a warning in the log.
  • <IEventManager>.CallAsync now returns ValueTask<bool> instead of ValueTask.
    • When running a Pre event, the return value will signify if the packet handling should continue or not. (true for keep going, false to cancel)
    • When running a Post or All event in a perfect world, the return value will always be true. Since we do not live in a perfect world, this return value will be false if someone tries to cancel in a post event. As a result, it is recommended to ignore this value.
  • Renamed IEventCancelable to ICancellableEvent
  • There seemed to be some preparations in the code for cancelling packets. These have been removed.
  • Introduced a bunch of compilation errors which I need to fix. 😃

closes #92
closes #135

@Sculas
Copy link

Sculas commented Nov 12, 2020

This PR looks very nice! I would for sure use this. I hope you continue this 👍

@stale
Copy link

stale bot commented Jan 15, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jan 15, 2021
@stale stale bot closed this Jan 22, 2021
@RobinRMC
Copy link

RobinRMC commented Jun 6, 2022

I would still love to see this getting implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pre and post events; Cancellation API Add event cancelling to chat messages
4 participants