From 401ee76fbe8e0fe38ed9cf4f3de771533ba379c9 Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Fri, 8 May 2020 13:01:03 +0900 Subject: [PATCH 1/5] Changed _BaseMenu.instances to WeakValueDictionary so that the menu can be created more dynamically. --- addons/source-python/packages/source-python/menus/base.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) mode change 100644 => 100755 addons/source-python/packages/source-python/menus/base.py diff --git a/addons/source-python/packages/source-python/menus/base.py b/addons/source-python/packages/source-python/menus/base.py old mode 100644 new mode 100755 index 1b66fcf43..5d56386ec --- a/addons/source-python/packages/source-python/menus/base.py +++ b/addons/source-python/packages/source-python/menus/base.py @@ -10,10 +10,12 @@ from collections import defaultdict # Math import math +# Weakref +from weakref import WeakValueDictionary # Source.Python Imports # Core -from core import AutoUnload +from core import WeakAutoUnload # Filters from filters.recipients import RecipientFilter # Listeners @@ -36,10 +38,10 @@ def __init__(self): self.options = {} -class _BaseMenu(AutoUnload, list): +class _BaseMenu(WeakAutoUnload, list): """The base menu. Every menu class should inherit from this class.""" - _instances = {} + _instances = WeakValueDictionary() def __init__(self, data=None, select_callback=None, build_callback=None, close_callback=None): """Initialize the menu. From 44bf1750261fccd0a8ecbdacfcd07811aba262be Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Fri, 8 May 2020 13:07:45 +0900 Subject: [PATCH 2/5] Add translations to TypedServerCommand. --- .../source-python/packages/source-python/commands/typed.py | 6 ++++++ 1 file changed, 6 insertions(+) mode change 100644 => 100755 addons/source-python/packages/source-python/commands/typed.py diff --git a/addons/source-python/packages/source-python/commands/typed.py b/addons/source-python/packages/source-python/commands/typed.py old mode 100644 new mode 100755 index dc2cde999..3d182a91d --- a/addons/source-python/packages/source-python/commands/typed.py +++ b/addons/source-python/packages/source-python/commands/typed.py @@ -32,6 +32,8 @@ from messages import SayText2 from messages import TextMsg from messages import HudDestination +# Translations +from translations.strings import TranslationStrings # ============================================================================= @@ -644,6 +646,10 @@ class TypedServerCommand(_TypedCommand): @staticmethod def send_message(command_info, message): + # Translate the message if it's a :class:`TranslationStrings` object. + if isinstance(message, TranslationStrings): + message = message.get_string() + logger.log_message(message) @classmethod From 8c61cf7cca90cfd8c517edb3c17d805e32fe31de Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Mon, 11 May 2020 10:44:15 +0900 Subject: [PATCH 3/5] Changed CommandInfo.reply and send_message to accept language and tokens. --- .../packages/source-python/commands/typed.py | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/addons/source-python/packages/source-python/commands/typed.py b/addons/source-python/packages/source-python/commands/typed.py index 3d182a91d..bc15bf3eb 100755 --- a/addons/source-python/packages/source-python/commands/typed.py +++ b/addons/source-python/packages/source-python/commands/typed.py @@ -490,13 +490,17 @@ def __init__(self, command, typed_command_cls, index=None, team_only=None): self.index = index self.team_only = team_only - def reply(self, msg): + def reply(self, msg, language=None, **tokens): """Reply to the command issuer. - :param str msg: + :param str/TranslationStrings msg: Message to send. + :param str language: + Language to be used. + :param tokens: + Translation tokens for message. """ - self.typed_command_cls.send_message(self, msg) + self.typed_command_cls.send_message(self, msg, language, **tokens) def is_private_command(self): """Return ``True`` if it's a private command. @@ -625,7 +629,7 @@ def manager(self): raise NotImplementedError('Needs to be implemented by a sub class.') @staticmethod - def send_message(command_info, message): + def send_message(command_info, message, language=None, **tokens): """Send a message.""" raise NotImplementedError('Needs to be implemented by a sub class.') @@ -645,10 +649,10 @@ class TypedServerCommand(_TypedCommand): manager = server_command_manager @staticmethod - def send_message(command_info, message): + def send_message(command_info, message, language=None, **tokens): # Translate the message if it's a :class:`TranslationStrings` object. if isinstance(message, TranslationStrings): - message = message.get_string() + message = message.get_string(language, **tokens) logger.log_message(message) @@ -690,8 +694,8 @@ class TypedClientCommand(_TypedPlayerCommand): manager = client_command_manager @staticmethod - def send_message(command_info, message): - TextMsg(message, HudDestination.CONSOLE).send(command_info.index) + def send_message(command_info, message, language=None, **tokens): + TextMsg(message, HudDestination.CONSOLE).send(command_info.index, **tokens) @classmethod def get_auto_command_return(cls, info): @@ -705,8 +709,8 @@ class TypedSayCommand(_TypedPlayerCommand): manager = say_command_manager @staticmethod - def send_message(command_info, message): - SayText2(message).send(command_info.index) + def send_message(command_info, message, language=None, **tokens): + SayText2(message).send(command_info.index, **tokens) @classmethod def get_auto_command_return(cls, info): From 436b0fa64f39f91bdb604bc99315eb106bed7cb2 Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Mon, 11 May 2020 11:08:07 +0900 Subject: [PATCH 4/5] Added a comment about instance deletion. --- addons/source-python/packages/source-python/menus/base.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/addons/source-python/packages/source-python/menus/base.py b/addons/source-python/packages/source-python/menus/base.py index 5d56386ec..6da8e9492 100755 --- a/addons/source-python/packages/source-python/menus/base.py +++ b/addons/source-python/packages/source-python/menus/base.py @@ -80,10 +80,12 @@ def __init__(self, data=None, select_callback=None, build_callback=None, close_c def _unload_instance(self): """Close this menu object for every player.""" # Just close all open menus, which will remove all instances from the - # queues + # queues. self.close() - # Also remove the instance from the _instances dict + # Also remove the instance from the _instances dict. + # This process is necessary because there is no guarantee that + # the instance will be destroyed when the plugin is unloaded. del self._instances[id(self)] def _unload_player(self, player_index): From db326bde77c90c18582192d37ebafce5425775b3 Mon Sep 17 00:00:00 2001 From: Jonathan <30329245+CookStar@users.noreply.github.com> Date: Wed, 27 May 2020 15:35:33 +0900 Subject: [PATCH 5/5] Changed ValidationError to accept language and tokens. --- .../packages/source-python/commands/typed.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/addons/source-python/packages/source-python/commands/typed.py b/addons/source-python/packages/source-python/commands/typed.py index bc15bf3eb..d89438d32 100755 --- a/addons/source-python/packages/source-python/commands/typed.py +++ b/addons/source-python/packages/source-python/commands/typed.py @@ -84,11 +84,11 @@ # ============================================================================= # >> EXCEPTIONS # ============================================================================= -# TODO: -# We probably need to update these exceptions if we want to add translations. class ValidationError(Exception): - def __init__(self, message=''): + def __init__(self, message='', language=None, **tokens): self.message = message + self.language = language + self.tokens = tokens class ArgumentError(ValidationError): pass class ArgumentNumberMismatch(ArgumentError): pass @@ -588,13 +588,12 @@ def on_command(cls, command, *args): Parse the command, clean its arguments and execute the callback. """ - # TODO: Translations! info = CommandInfo(command, cls, *args) try: command_node, args = cls.parser.parse_command(info.command) result = cls.on_clean_command(info, command_node, args) except ValidationError as e: - info.reply(e.message) + info.reply(e.message, e.language, **e.tokens) else: if result is None: return info.auto_command_return