From 40edf74dbcc2491be6fa6a26a30e5c97794bf385 Mon Sep 17 00:00:00 2001 From: Andrei Date: Sat, 9 Mar 2024 18:36:43 +0300 Subject: [PATCH] refactor SendExceptionEvent --- .../mods/wot_stat/common/exceptionSending.py | 29 +++++++++ .../gui/mods/wot_stat/common/modAutoUpdate.py | 9 ++- .../mods/wot_stat/common/modNotification.py | 11 ++-- .../wot_stat/{ => common}/serverLogger.py | 2 +- .../client/gui/mods/wot_stat/load_mod.py | 5 +- .../wot_stat/logger/battleEventSession.py | 4 +- .../gui/mods/wot_stat/logger/eventLogger.py | 16 ++--- .../logger/loggers/onBattleResultLogger.py | 11 ++-- .../client/gui/mods/wot_stat/logger/utils.py | 2 + .../gui/mods/wot_stat/logger/wotHookEvents.py | 64 +++++++------------ .../scripts/client/gui/mods/wot_stat/utils.py | 2 +- 11 files changed, 84 insertions(+), 71 deletions(-) create mode 100644 WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/exceptionSending.py rename WOTSTAT/res/scripts/client/gui/mods/wot_stat/{ => common}/serverLogger.py (98%) diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/exceptionSending.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/exceptionSending.py new file mode 100644 index 0000000..5c19cbc --- /dev/null +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/exceptionSending.py @@ -0,0 +1,29 @@ +from Event import Event +from debug_utils import LOG_CURRENT_EXCEPTION +from serverLogger import send_current_exception + + +def with_exception_sending(f): + def wrapper(*args, **kwargs): + try: + return f(*args, **kwargs) + except: + send_current_exception() + LOG_CURRENT_EXCEPTION() + + return wrapper + + +class SendExceptionEvent(Event): + __slots__ = () + + def __init__(self, manager=None): + super(SendExceptionEvent, self).__init__(manager) + + def __call__(self, *args, **kwargs): + for delegate in self[:]: + try: + delegate(*args, **kwargs) + except: + send_current_exception() + LOG_CURRENT_EXCEPTION() diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/modAutoUpdate.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/modAutoUpdate.py index 60d9340..c6ddecf 100644 --- a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/modAutoUpdate.py +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/modAutoUpdate.py @@ -1,17 +1,19 @@ +import BigWorld +import json import os import shutil -import json -import BigWorld -from helpers import getShortClientVersion from asyncResponse import get_async +from helpers import getShortClientVersion from ..utils import print_log +from .exceptionSending import with_exception_sending def num_game_version(): return getShortClientVersion().split('v.')[1].strip() +@with_exception_sending def update_game_version(full_mod_name): gameVersion = num_game_version() currentMod = os.path.join(os.path.abspath('./mods/'), gameVersion, full_mod_name) @@ -36,6 +38,7 @@ def b(x, y): 'Accept': 'application/vnd.github+json'} +@with_exception_sending def update_mod_version(url, mod_name, current_version, on_start_update=None, on_updated=None, on_success_check=None): latest_version = '' diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/modNotification.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/modNotification.py index ec2f7ab..ac8f01f 100644 --- a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/modNotification.py +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/modNotification.py @@ -1,12 +1,12 @@ # coding=utf-8 -from gui import SystemMessages -from ..utils import print_log - import BigWorld -from notification.actions_handlers import NotificationsActionsHandlers +from gui import SystemMessages from gui.DialogsInterface import showDialog from gui.Scaleform.daapi.view.dialogs import DIALOG_BUTTON_ID from gui.Scaleform.daapi.view.dialogs import SimpleDialogMeta +from notification.actions_handlers import NotificationsActionsHandlers +from .exceptionSending import with_exception_sending +from ..utils import print_log OPEN_PERSONAL_WOTSTAT_EVENT = 'OPEN_PERSONAL_WOTSTAT_EVENT_' @@ -20,11 +20,13 @@ def getLabels(self): ] +@with_exception_sending def show_url_dialog(title=None, message=None, url=None): meta = SimpleDialogMeta(title=title, message=message, buttons=UrlDialogButtons()) showDialog(meta, lambda proceed: BigWorld.wg_openWebBrowser(url) if proceed else None) +@with_exception_sending def __wotstat_events_handleAction(self, model, typeID, entityID, actionName): try: if actionName.startswith(OPEN_PERSONAL_WOTSTAT_EVENT): @@ -43,6 +45,7 @@ def __wotstat_events_handleAction(self, model, typeID, entityID, actionName): NotificationsActionsHandlers.handleAction = __wotstat_events_handleAction +@with_exception_sending def show_notification(msg, message_type=SystemMessages.SM_TYPE.Information): print_log('show notification: %s. With type: %s' % (msg, message_type)) SystemMessages.pushMessage(msg, type=message_type) diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/serverLogger.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/serverLogger.py similarity index 98% rename from WOTSTAT/res/scripts/client/gui/mods/wot_stat/serverLogger.py rename to WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/serverLogger.py index a3307b4..1c3fe09 100644 --- a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/serverLogger.py +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/common/serverLogger.py @@ -12,7 +12,7 @@ from account_shared import readClientServerVersion from constants import AUTH_REALM from debug_utils import _addTagsToMsg, _makeMsgHeader, LOG_CURRENT_EXCEPTION, _src_file_trim_to, _g_logLock -from .common.asyncResponse import post_async +from .asyncResponse import post_async logger = None # type: ServerLogger modVersion = 'unknown' diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/load_mod.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/load_mod.py index e6d8a1b..aac6aaf 100644 --- a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/load_mod.py +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/load_mod.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- -import BigWorld import json +import BigWorld from .common.config import Config configPath = './mods/configs/wot_stat/config.cfg' config = Config(configPath) # type: Config @@ -12,10 +12,9 @@ from .common.asyncResponse import get_async from .utils import print_log -from .logger.eventLogger import eventLogger from .logger.wotHookEvents import wotHookEvents from .logger.sessionStorage import sessionStorage -from .serverLogger import setupLogger, send +from .common.serverLogger import setupLogger, send is_success_check = None api_server_time = None diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/battleEventSession.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/battleEventSession.py index 60b8807..e291bec 100644 --- a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/battleEventSession.py +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/battleEventSession.py @@ -1,8 +1,9 @@ import json -import BigWorld +import BigWorld from events import Event, OnBattleStart, OnBattleResult from ..common.asyncResponse import post_async +from ..common.exceptionSending import with_exception_sending from ..utils import print_log, print_debug try: @@ -64,6 +65,7 @@ def __send_event_loop(self): if self.enable: BigWorld.callback(self.send_interval, self.__send_event_loop) + @with_exception_sending def __post_events(self, events, callback=None): if events and len(events) > 0: data = { diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/eventLogger.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/eventLogger.py index 4fc2874..3d07846 100644 --- a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/eventLogger.py +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/eventLogger.py @@ -1,24 +1,18 @@ -import Event as BigWorldEvent -from constants import ARENA_PERIOD - from battleEventSession import BattleEventSession -from ..load_mod import config -from ..utils import print_log, print_debug -from .utils import * +from constants import ARENA_PERIOD from events import Event +from .utils import * +from ..common.exceptionSending import SendExceptionEvent +from ..utils import print_debug class EventLogger: old_battle_event_sessions = {} battle_event_session = None # type: BattleEventSession start_battle_time = 0 - on_session_created = BigWorldEvent.Event() + on_session_created = SendExceptionEvent() def __init__(self): - self.old_battle_event_sessions = {} - self.battle_event_session = None - self.start_battle_time = 0 - self.on_session_created = BigWorldEvent.Event() print_debug('INIT EVENT LOGGER') def emit_event(self, event, arena_id=None): diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/loggers/onBattleResultLogger.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/loggers/onBattleResultLogger.py index 5286aa4..45e0982 100644 --- a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/loggers/onBattleResultLogger.py +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/loggers/onBattleResultLogger.py @@ -1,14 +1,13 @@ -from typing import Optional, Any - import BattleReplay import BigWorld from PlayerEvents import g_playerEvents +from items import vehicles as vehiclesWG from ..eventLogger import eventLogger -from ..sessionStorage import sessionStorage from ..events import OnBattleResult -from ...utils import print_log, print_debug -from items import vehicles as vehiclesWG +from ..sessionStorage import sessionStorage from ..utils import short_tank_type, setup_dynamic_battle_info, setup_session_meta +from ...common.exceptionSending import with_exception_sending +from ...utils import print_log, print_debug class OnBattleResultLogger: @@ -30,11 +29,13 @@ def on_session_created(self, battleEventSession): setup_dynamic_battle_info(event) self.precreated_battle_result_event[battleEventSession.arenaID] = event + @with_exception_sending def on_battle_results_received(self, isPlayerVehicle, results): if not isPlayerVehicle or BattleReplay.isPlaying(): return self.process_battle_result(results) + @with_exception_sending def battle_result_cache_checker(self): BigWorld.callback(3, self.battle_result_cache_checker) diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/utils.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/utils.py index d06e73d..a4091ec 100644 --- a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/utils.py +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/utils.py @@ -6,6 +6,7 @@ from constants import ARENA_BONUS_TYPE, ARENA_GAMEPLAY_NAMES, AUTH_REALM from gui.battle_control.battle_constants import FEEDBACK_EVENT_ID from .sessionStorage import sessionStorage +from ..common.exceptionSending import with_exception_sending from ..load_mod import config @@ -43,6 +44,7 @@ def get_tank_type(vehicleTags): return res +@with_exception_sending def setup_dynamic_battle_info(dynamicBattleEvent): """ @type dynamicBattleEvent: DynamicBattleEvent diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/wotHookEvents.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/wotHookEvents.py index aa4d720..3901ebf 100644 --- a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/wotHookEvents.py +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/logger/wotHookEvents.py @@ -1,33 +1,13 @@ -import BigWorld - -from Avatar import PlayerAvatar from Account import Account -from VehicleGunRotator import VehicleGunRotator -from gui.Scaleform.daapi.view.lobby.battle_queue import BattleQueue +from Avatar import PlayerAvatar from ProjectileMover import ProjectileMover from Vehicle import Vehicle +from VehicleGunRotator import VehicleGunRotator +from gui.Scaleform.daapi.view.lobby.battle_queue import BattleQueue from helpers import dependency from skeletons.connection_mgr import IConnectionManager -from Event import Event -from debug_utils import LOG_CURRENT_EXCEPTION - +from ..common.exceptionSending import SendExceptionEvent from ..common.hook import g_overrideLib -from ..serverLogger import send_current_exception - - -class SafeEvent(Event): - __slots__ = () - - def __init__(self, manager=None): - super(SafeEvent, self).__init__(manager) - - def __call__(self, *args, **kwargs): - for delegate in self[:]: - try: - delegate(*args, **kwargs) - except: - send_current_exception() - LOG_CURRENT_EXCEPTION() class WotHookEvents: @@ -39,29 +19,29 @@ def __init__(self): self.listeners = {} # ------------------INIT------------------# - self.onConnected = SafeEvent() - self.Account_onBecomePlayer = SafeEvent() - self.BattleQueue_populate = SafeEvent() - self.PlayerAvatar_onEnterWorld = SafeEvent() - self.PlayerAvatar_updateTargetingInfo = SafeEvent() - self.PlayerAvatar_onArenaPeriodChange = SafeEvent() + self.onConnected = SendExceptionEvent() + self.Account_onBecomePlayer = SendExceptionEvent() + self.BattleQueue_populate = SendExceptionEvent() + self.PlayerAvatar_onEnterWorld = SendExceptionEvent() + self.PlayerAvatar_updateTargetingInfo = SendExceptionEvent() + self.PlayerAvatar_onArenaPeriodChange = SendExceptionEvent() # -------------------MOVE------------------# - self.VehicleGunRotator_setShotPosition = SafeEvent() - self.VehicleGunRotator_updateGunMarker = SafeEvent() - self.PlayerAvatar_updateGunMarker = SafeEvent() + self.VehicleGunRotator_setShotPosition = SendExceptionEvent() + self.VehicleGunRotator_updateGunMarker = SendExceptionEvent() + self.PlayerAvatar_updateGunMarker = SendExceptionEvent() # -------------------SHOT------------------# - self.PlayerAvatar_shoot = SafeEvent() - self.PlayerAvatar_showTracer = SafeEvent() - self.PlayerAvatar_showShotResults = SafeEvent() - self.Vehicle_onHealthChanged = SafeEvent() - self.PlayerAvatar_showOwnVehicleHitDirection = SafeEvent() + self.PlayerAvatar_shoot = SendExceptionEvent() + self.PlayerAvatar_showTracer = SendExceptionEvent() + self.PlayerAvatar_showShotResults = SendExceptionEvent() + self.Vehicle_onHealthChanged = SendExceptionEvent() + self.PlayerAvatar_showOwnVehicleHitDirection = SendExceptionEvent() # -------------------EXPLOSION------------------# - self.PlayerAvatar_explodeProjectile = SafeEvent() - self.Vehicle_showDamageFromShot = SafeEvent() + self.PlayerAvatar_explodeProjectile = SendExceptionEvent() + self.Vehicle_showDamageFromShot = SendExceptionEvent() # -------------------PROJECTILE-------------------# - self.ProjectileMover_killProjectile = SafeEvent() + self.ProjectileMover_killProjectile = SendExceptionEvent() # -------------------HELP-------------------# - self.PlayerAvatar_enableServerAim = SafeEvent() + self.PlayerAvatar_enableServerAim = SendExceptionEvent() def __onConnected(self): self.onConnected() diff --git a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/utils.py b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/utils.py index b4a1883..eda8d0f 100644 --- a/WOTSTAT/res/scripts/client/gui/mods/wot_stat/utils.py +++ b/WOTSTAT/res/scripts/client/gui/mods/wot_stat/utils.py @@ -1,5 +1,5 @@ import BigWorld -from .serverLogger import send, LEVELS +from .common.serverLogger import send, LEVELS def print_log(log):