diff --git a/src/mod_tessumod.py b/src/mod_tessumod.py index 91d4a62..cde1416 100644 --- a/src/mod_tessumod.py +++ b/src/mod_tessumod.py @@ -116,7 +116,7 @@ def init(): g_messengerEvents.users.onUsersListReceived += on_users_list_received - add_onPlayerSpeaking_filter(g_messengerEvents.voip.onPlayerSpeaking) + g_messengerEvents.voip.onPlayerSpeaking = wrap_onPlayerSpeaking(g_messengerEvents.voip.onPlayerSpeaking) notifications.add_event_handler(notifications.TSPLUGIN_DOWNLOAD, on_tsplugin_download) notifications.add_event_handler(notifications.TSPLUGIN_IGNORED, on_tsplugin_ignore_toggled) @@ -141,7 +141,7 @@ def fini(): g_playerEvents.onAvatarReady -= g_positional_audio.enable g_playerEvents.onAvatarBecomeNonPlayer -= g_positional_audio.disable - remove_onPlayerSpeaking_filter(g_messengerEvents.voip.onPlayerSpeaking) + g_messengerEvents.voip.onPlayerSpeaking = g_messengerEvents.voip.onPlayerSpeaking.get_original_event() g_authentication_error = None g_keyvaluestorage = None @@ -503,25 +503,15 @@ def wrapper(*args, **kwargs): return orig_method(*args, **kwargs) return wrapper -def add_onPlayerSpeaking_filter(obj): - class FilterEvent(type(obj)): - def __call__(self, dbid, talking): - if talking: - self.unfiltered_call(dbid, talking) - elif has_speak_feedback(dbid): - pass - else: - self.unfiltered_call(dbid, talking) - - def unfiltered_call(self, *args, **kwargs): - self.original_class.__call__(self, *args, **kwargs) - - obj.original_class = obj.__class__ - obj.__class__ = FilterEvent - -def remove_onPlayerSpeaking_filter(obj): - obj.__class__ = obj.original_class - del obj.original_class +def wrap_onPlayerSpeaking(obj): + def filter_func(self, orig_func, dbid, talking): + if talking: + orig_func(dbid, talking) + elif has_speak_feedback(dbid): + pass + else: + orig_func(dbid, talking) + return utils.EventFilter(obj, filter_func) def on_users_list_received(tags): '''This function populates user cache with friends and clan members from diff --git a/src/tessumod/utils.py b/src/tessumod/utils.py index 492583b..ad2e792 100644 --- a/src/tessumod/utils.py +++ b/src/tessumod/utils.py @@ -273,6 +273,31 @@ def id(self): def __repr__(self): return "Player(name={0}, id={1})".format(self._name, self._id) +class EventFilter(object): + def __init__(self, orig_event, filter_func): + self._orig_event = orig_event + self._filter_func = filter_func + + def __call__(self, *args, **kwargs): + return self._filter_func(self, self.unfiltered_call, *args, **kwargs) + + def __iadd__(self, delegate): + self._orig_event.__iadd__(delegate) + return self + + def __isub__(self, delegate): + self._orig_event.__isub__(delegate) + return self + + def clear(self): + self._orig_event.clear() + + def unfiltered_call(self, *args, **kwargs): + return self._orig_event.__call__(*args, **kwargs) + + def get_original_event(self): + return self._orig_event + class MinimapMarkersController(object): '''MinimapMarkersController class repeatably starts given marker 'action' every 'interval' seconds in minimap over given 'vehicle_id', effectively creating