diff --git a/VERSION b/VERSION index a8fdfda..53adb84 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.8.1 +1.8.2 diff --git a/src/torchlight/PlayerManager.py b/src/torchlight/PlayerManager.py index 3f2df69..91d48a8 100644 --- a/src/torchlight/PlayerManager.py +++ b/src/torchlight/PlayerManager.py @@ -5,7 +5,7 @@ from torchlight.AudioManager import AudioManager from torchlight.Constants import Clients from torchlight.Player import Player -from torchlight.Sourcemod import SourcemodConfig +from torchlight.Sourcemod import SourcemodAdmin, SourcemodConfig from torchlight.Torchlight import Torchlight @@ -176,3 +176,31 @@ def FindName(self, name: str) -> Player | None: if player and player.name == name: return player return None + + @staticmethod + def create_console_player() -> Player: + player = Player( + 0, + 0, + "[CONSOLE]", + "127.0.0.1", + "CONSOLE", + ) + player.admin = SourcemodAdmin( + name="CONSOLE", + unique_id=player.unique_id, + level=100, + flag_bits=0, + groups=[], + ) + player.storage = dict( + { + "Audio": { + "Uses": 0, + "LastUse": 0.0, + "LastUseLength": 0.0, + "TimeUsed": 0.0, + } + } + ) + return player diff --git a/src/torchlight/SourceModAPI.py b/src/torchlight/SourceModAPI.py index 7c16fc8..c718abe 100644 --- a/src/torchlight/SourceModAPI.py +++ b/src/torchlight/SourceModAPI.py @@ -24,7 +24,7 @@ async def _MakeCall(self, function: str, *args: Any, **kwargs: Any) -> dict[str, if isinstance(res_raw, dict): res = res_raw - if res["error"]: + if "error" in res and res["error"]: raise Exception("{}({})\n{}".format(function, args, res["error"])) return res diff --git a/src/torchlight/SourceRCONClient.py b/src/torchlight/SourceRCONClient.py index afb6721..736a7fc 100644 --- a/src/torchlight/SourceRCONClient.py +++ b/src/torchlight/SourceRCONClient.py @@ -7,8 +7,7 @@ from typing import Any from torchlight.CommandHandler import CommandHandler -from torchlight.Player import Player -from torchlight.Sourcemod import SourcemodAdmin +from torchlight.PlayerManager import PlayerManager class SourceRCONClient: @@ -73,29 +72,7 @@ def ParsePacket(self, data_raw: bytes) -> None: if data: data = data.strip('"') self.logger.info(sys._getframe().f_code.co_name + f' Exec: "{data}"') - player = Player( - 0, - 0, - "[CONSOLE]", - "127.0.0.1", - "CONSOLE", - ) - player.admin = SourcemodAdmin( - name="CONSOLE", - unique_id=player.unique_id, - level=100, - flag_bits=0, - groups=[], - ) - player.storage = dict( - { - "Audio": { - "Uses": 0, - "LastUse": 0.0, - "LastUseLength": 0.0, - "TimeUsed": 0.0, - } - } - ) + + player = PlayerManager.create_console_player() asyncio.Task(self.command_handler.HandleCommand(data, player)) # self.p_send(p_id, 0, self._server.torchlight.GetLine()) diff --git a/src/torchlight/Torchlight.py b/src/torchlight/Torchlight.py index 4aa2621..014bda6 100644 --- a/src/torchlight/Torchlight.py +++ b/src/torchlight/Torchlight.py @@ -83,6 +83,9 @@ def SayChat(self, message: str, player: Player | None = None) -> None: # @profile def SayPrivate(self, player: Player, message: str) -> None: + if player.index == 0: + return + message = f"{{darkblue}}[Torchlight]: {{default}}{message}" if len(message) > 976: message = message[:973] + "..." diff --git a/src/torchlight/cli.py b/src/torchlight/cli.py index 5c30f55..1ca15b5 100644 --- a/src/torchlight/cli.py +++ b/src/torchlight/cli.py @@ -1,15 +1,30 @@ import asyncio import logging +import signal +import sys +from types import FrameType import click from torchlight.Config import Config +from torchlight.PlayerManager import PlayerManager from torchlight.SourceRCONServer import SourceRCONServer from torchlight.TorchlightHandler import TorchlightHandler logger = logging.getLogger(__name__) +torchlight_handler: TorchlightHandler | None = None + + +def graceful_shutdown(signal: int, frame: FrameType | None) -> None: + if torchlight_handler and torchlight_handler.audio_manager: + logger.info("Stopping all audio sounds") + player = PlayerManager.create_console_player() + torchlight_handler.audio_manager.Stop(player, "") + sys.exit(0) + + @click.command() @click.option("--config-folder", default="config", help="Configuration folder path.") @click.version_option() @@ -23,8 +38,12 @@ def cli(config_folder: str) -> None: datefmt=config["Logging"]["datefmt"], ) + signal.signal(signal.SIGINT, graceful_shutdown) + signal.signal(signal.SIGTERM, graceful_shutdown) + event_loop = asyncio.get_event_loop() + global torchlight_handler torchlight_handler = TorchlightHandler(event_loop, config) # Handles new connections on 0.0.0.0:27015