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

Setting handlers, only if it is required by plugins #124

Open
em92 opened this issue Aug 13, 2023 · 2 comments
Open

Setting handlers, only if it is required by plugins #124

em92 opened this issue Aug 13, 2023 · 2 comments

Comments

@em92
Copy link
Collaborator

em92 commented Aug 13, 2023

For example:

  1. we have accepted PR with new damage hook added new hook, handler and dispatcher for any damage events happening #120
  2. QLDS server does not use plugin, which uses introduced "damage" hook.
  3. minqlx.register_handler("damage", handle_damage) is executed from PR above.

When damage is registered QLDS runs python code around handle_damage and

def dispatch(self, *args, **kwargs):
"""Calls all the handlers that have been registered when hooking this event.
The recommended way to use this for events that inherit this class is to
override the method with explicit arguments (as opposed to the this one's)
and call this method by using ``super().dispatch()``.
Handlers have several options for return values that can affect the flow:
- minqlx.RET_NONE or None -- Continue execution normally.
- minqlx.RET_STOP -- Stop any further handlers from being called.
- minqlx.RET_STOP_EVENT -- Let handlers process it, but stop the event
at the engine-level.
- minqlx.RET_STOP_ALL -- Stop handlers **and** the event.
- Any other value -- Passed on to :func:`self.handle_return`, which will
by default simply send a warning to the logger about an unknown value
being returned. Can be overridden so that events can have their own
special return values.
:param args: Any arguments.
:param kwargs: Any keyword arguments.
"""
# Allow subclasses of this to edit the arguments without having
# to reimplement this method. Whenever an unknown return value
# is returned, we pass it on to handle_return.
self.args = args
self.kwargs = kwargs
logger = minqlx.get_logger()
# Log the events as they come in.
if self.name not in self.no_debug:
dbgstr = "{}{}".format(self.name, args)
if len(dbgstr) > 100:
dbgstr = dbgstr[0:99] + ")"
logger.debug(dbgstr)
plugins = self.plugins.copy()
self.return_value = True
for i in range(5):
for plugin in plugins:
for handler in plugins[plugin][i]:
try:
res = handler(*self.args, **self.kwargs)
if res == minqlx.RET_NONE or res is None:
continue
elif res == minqlx.RET_STOP:
return True
elif res == minqlx.RET_STOP_EVENT:
self.return_value = False
elif res == minqlx.RET_STOP_ALL:
return False
else: # Got an unknown return value.
return_handler = self.handle_return(handler, res)
if return_handler is not None:
return return_handler
except:
minqlx.log_exception(plugin)
continue
return self.return_value
, which wastes extra CPU time and clearly does not do anything useful.

So it would be better, if minqlx.register_handler("damage", handle_damage) only if plugin or minqlx itself requires it.

That also concerns to other hooks below, but comparing to damage hook, they are executed less frequently

  • player_connect
  • player_loaded
  • player_disconnect
  • player_spawn
  • kamikaze_use
  • kamikaze_explode
@mgaertne
Copy link
Contributor

Hmmmm, that should be possible by modifying the last_init method, and plugin_load mechanism, and modifying add_hook in the plugin class to register the handler for the given hook.

@mgaertne
Copy link
Contributor

Note that I also added the handle_frame event which is called once per server frame, i.e. 40 times per second on default server settings to your list of events that maybe should be hot pluggable.

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

No branches or pull requests

2 participants