Created
October 4, 2026 10:11
-
-
Save cetteup/54b209bf22fb8d5c040987be4bdf7ccc to your computer and use it in GitHub Desktop.
Simple in-game chat logger for Battlefield 2 servers (built using Copilot)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """" | |
| Vanilla BF2 admin chat logger (Python 2.3 compatible) | |
| Setup | |
| - add to standard_admin as admin/standard_admin/chatlogger.py | |
| - in admin/standard_admin/__init__.py, add two lines: | |
| import chatlogger | |
| chatlogger.init() | |
| """ | |
| import os | |
| import time | |
| import host | |
| import bf2 | |
| LOG_DIR = 'logs' | |
| LOG_FILE = os.path.join(LOG_DIR, 'chat.log') | |
| FLUSH_INTERVAL = 60 # seconds | |
| _fh = None | |
| _timer = None | |
| def _stamp(): | |
| return time.strftime('%Y-%m-%d %H:%M:%S') | |
| def _ensure_open(): | |
| global _fh | |
| if _fh is None: | |
| if not os.path.isdir(LOG_DIR): | |
| os.makedirs(LOG_DIR) | |
| _fh = open(LOG_FILE, 'a') | |
| def _write(line): | |
| try: | |
| _ensure_open() | |
| _fh.write(line + '\n') | |
| except Exception, e: | |
| host.rcon_invoke('echo "chatlogger error: %s"' % str(e)) | |
| def _flush(data=None): | |
| if _fh is not None: | |
| try: | |
| _fh.flush() | |
| except Exception: | |
| pass | |
| def _clean_text(text): | |
| text = text.replace('\r', ' ').replace('\n', ' ').replace('\xa7', '').strip() | |
| marker = 'HUD_TEXT_CHAT_' | |
| if text.startswith(marker): | |
| payload = text[len(marker):] | |
| # Only known BF2 non-global chat prefixes | |
| prefixes = ('TEAM', 'SQUAD', 'COMMANDER') | |
| for p in prefixes: | |
| if payload.startswith(p): | |
| # Keep everything after the prefix as the actual message | |
| return payload[len(p):].strip() | |
| # Unknown HUD_TEXT_CHAT_ variant: keep payload so we don't lose data | |
| return payload.strip() | |
| return text | |
| def onChatMessage(playerId, text, channel, flags): | |
| name = 'SERVER' | |
| if playerId != -1: | |
| player = bf2.playerManager.getPlayerByIndex(playerId) | |
| if player is not None: | |
| name = player.getName() | |
| text = _clean_text(text) | |
| if not text: | |
| return | |
| _write('[%s] [%s] %s: %s' % (_stamp(), channel, name, text)) | |
| def onGameStatusChanged(status): | |
| if status == bf2.GameStatus.EndGame: | |
| _flush() | |
| def init(): | |
| global _timer | |
| host.registerHandler('ChatMessage', onChatMessage, 1) | |
| host.registerGameStatusHandler(onGameStatusChanged) | |
| _timer = bf2.Timer(_flush, FLUSH_INTERVAL, 1) | |
| _timer.setRecurring(FLUSH_INTERVAL) | |
| _write('[%s] --- chat logger started ---' % _stamp()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment