Skip to content

Conversation

tokKurumi
Copy link

TODO: implement all listeners and events

TODO: implement all listeners and events
// how can we get all instances of ListenerBase?
// reflection/source generator should be used
// TODO: implement reflection/source generator
private static readonly List<ListenerBase> _listeners = [
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gonna create separate project with source gen to analyze all Listeners in code to remove manual resolving


public static class EventsExtensions
{
private static readonly List<IEventBase> _events = [
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a source gen

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what source gen?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing that allows to write code which generator another code. Here I want to implement custom attribute [Listener] or [Event] and mark class with it:

[Event]
public class PlayerHurtEvent : EventBase<EventPlayerHurt>
{
    public override GameEventHandler<EventPlayerHurt> Handler => OnPlayerHurt;

    private HookResult OnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
    {
        Console.WriteLine($"Player {@event.Userid} hurt with {@event.DmgHealth} damage.");
        return HookResult.Continue;
    }
}

then it should analyze code and take each class with attribute [Event]. Then we have a collection of these classes and we can use them with existing:

public static class EventsExtensions
{
-   private static readonly List<IEventBase> _events = [
-      new PlayerHurtEvent(),
-      // other events
-   ];
+   private static readonly List<IEventBase> _events = SourceGenerators.Events; // collection of events provided by source gen

    public static void RegisterAllEventHandlers(this BasePlugin plugin)
    {
        foreach (var @event in _events)
        {
            @event.Register(plugin);
        }
    }

    public static void RemoveAllEventHandlers(this BasePlugin plugin)
    {
        foreach (var eventInstance in _events)
        {
            eventInstance.Deregister(plugin);
        }
    }
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output of source gen should be

public static partial class SourceGenerators {
    public static readonly List<IEventBase> = [
        // all events here which were created by source generator, but not manual resolving
    ];
}

@oylsister
Copy link
Owner

oylsister commented Dec 18, 2024

There is a bunch of code has been implemented recently, you probably have to sync them to latest one. Anyway, does your implementation work well when trying to print message from event and delegate?

@tokKurumi
Copy link
Author

There is a bunch of code has been implemented recently, you probably have to sync them to latest one. Anyway, does your implementation work well when trying to print message from event and delegate?

I did not test it yet. But my guesses it should improve code read ability and it will remove not necessary class Events

@tokKurumi
Copy link
Author

There is a bunch of code has been implemented recently, you probably have to sync them to latest one.

Yeah, this is the main problem, the second is source gen. I do it in this weekend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants