-
Notifications
You must be signed in to change notification settings - Fork 18
Refactor event handling and improve code formatting #55
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
base: dev2.0
Are you sure you want to change the base?
Conversation
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 = [ |
There was a problem hiding this comment.
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 = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need a source gen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what source gen?
There was a problem hiding this comment.
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);
}
}
}
There was a problem hiding this comment.
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
];
}
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 |
Yeah, this is the main problem, the second is source gen. I do it in this weekend |
TODO: implement all listeners and events