|
| 1 | +import ctypes |
| 2 | +import logging |
| 3 | + |
| 4 | +from pymhf import Mod |
| 5 | +from pymhf.gui import no_gui |
| 6 | +from pymhf.core.hooking import hook_manager |
| 7 | +from pymhf.core._types import DetourTime, CustomTriggerProtocol |
| 8 | +import nmspy.data.types as nms |
| 9 | +import nmspy.data.basic_types as basic |
| 10 | +from nmspy.terminal_parser import TerminalCommand, generate_full_description, split_key |
| 11 | +from nmspy.decorators import terminal_command |
| 12 | + |
| 13 | +logger = logging.getLogger("chat_bot") |
| 14 | + |
| 15 | + |
| 16 | +MOD_OPTION = "\n<TITLE>'/mod <><TITLE_BRIGHT>name option(s)<><TITLE>': configure a mod. Enter /mod <><TITLE_BRIGHT>name<><TITLE> to see options for each mod<>" |
| 17 | +MOD_LIST_OPTION = "\n<TITLE>'/modlist: display list of mods which can be configured via the terminal<>" |
| 18 | + |
| 19 | + |
| 20 | +@no_gui |
| 21 | +class textChatManager(Mod): |
| 22 | + @terminal_command("Say some words.", "example") |
| 23 | + def say(self, *words): |
| 24 | + logger.info(f"I am saying the words: {' '.join(words)}") |
| 25 | + |
| 26 | + @nms.cGcTextChatInput.ParseTextForCommands.before |
| 27 | + def intercept_chat_message( |
| 28 | + self, |
| 29 | + this: ctypes._Pointer[nms.cGcTextChatManager], |
| 30 | + lMessageText: ctypes._Pointer[basic.cTkFixedString[0x3FF]], |
| 31 | + ): |
| 32 | + """Parse the command and do something with it. |
| 33 | + We will check for the message starting with `/mod` and recognise this as a command. |
| 34 | + Use the custom callback system in pyMHF to send these commands to anything registered and detect if |
| 35 | + nothing is registered for a given command and raise a helpful message. |
| 36 | + """ |
| 37 | + if not lMessageText: |
| 38 | + return |
| 39 | + msg = str(lMessageText.contents) |
| 40 | + if msg.startswith("/pymodlist"): |
| 41 | + # List the available mods which have commands. |
| 42 | + # Filter the keys then generate a set |
| 43 | + mod_command_keys = list( |
| 44 | + filter( |
| 45 | + lambda x: x.startswith("tc::"), hook_manager.custom_callbacks.keys() |
| 46 | + ) |
| 47 | + ) |
| 48 | + mod_names = list(set(split_key(x)[0] for x in mod_command_keys)) |
| 49 | + mod_names.sort() |
| 50 | + desc = "<TITLE>Available mods with commands<>" |
| 51 | + for mod_name in mod_names: |
| 52 | + desc += f"\n- <TITLE>{mod_name}<>" |
| 53 | + lMessageText.contents.set(desc) |
| 54 | + elif msg.startswith("/mod"): |
| 55 | + # Get the mod name. |
| 56 | + split_msg = msg.split(" ")[1:] |
| 57 | + if len(split_msg) == 0: |
| 58 | + # Invalid... |
| 59 | + lMessageText.contents.set("Need to specify a mod!") |
| 60 | + return |
| 61 | + else: |
| 62 | + if len(split_msg) == 1: |
| 63 | + # This is just the mod name. Show the help. |
| 64 | + mod_name = split_msg[0] |
| 65 | + mod_command_keys = list( |
| 66 | + filter( |
| 67 | + lambda x: x.startswith(f"tc::{mod_name}"), |
| 68 | + hook_manager.custom_callbacks.keys(), |
| 69 | + ) |
| 70 | + ) |
| 71 | + if not mod_command_keys: |
| 72 | + lMessageText.contents.set( |
| 73 | + f"The mod {mod_name!r} either doesn't exist or has no " |
| 74 | + "terminal commands registered" |
| 75 | + ) |
| 76 | + else: |
| 77 | + command_funcs: dict[str, CustomTriggerProtocol] = {} |
| 78 | + for key in mod_command_keys: |
| 79 | + funcs = hook_manager.custom_callbacks[key].get( |
| 80 | + DetourTime.NONE, [] |
| 81 | + ) |
| 82 | + if len(funcs) > 1: |
| 83 | + logger.warning( |
| 84 | + f"Multiple terminal commands have been defined with the key {key}" |
| 85 | + ) |
| 86 | + return |
| 87 | + elif len(funcs) == 1: |
| 88 | + command_funcs[key] = list(funcs)[0] |
| 89 | + mod_desc = f"<TITLE>{mod_name!r} mod options:<>" |
| 90 | + for key, func in command_funcs.items(): |
| 91 | + _, command = split_key(key) |
| 92 | + mod_desc += "\n" + generate_full_description( |
| 93 | + command, func._description |
| 94 | + ) |
| 95 | + lMessageText.contents.set(mod_desc) |
| 96 | + return |
| 97 | + elif len(split_msg) == 2: |
| 98 | + parsed_command = TerminalCommand(split_msg[0], split_msg[1], []) |
| 99 | + elif len(split_msg) > 2: |
| 100 | + parsed_command = TerminalCommand( |
| 101 | + split_msg[0], split_msg[1], split_msg[2:] |
| 102 | + ) |
| 103 | + try: |
| 104 | + # Call the custom callback with the generated key. |
| 105 | + # If it doesn't exist we raise an exception which we catch and then show a message in the |
| 106 | + # terminal. |
| 107 | + hook_manager.call_custom_callbacks( |
| 108 | + parsed_command.callback_key, |
| 109 | + args=parsed_command.args, |
| 110 | + alert_nonexist=True, |
| 111 | + ) |
| 112 | + lMessageText.contents.set( |
| 113 | + f"Ran command {parsed_command.command_name!r} for mod {parsed_command.mod_name!r}" |
| 114 | + ) |
| 115 | + except ValueError: |
| 116 | + lMessageText.contents.set( |
| 117 | + f"Invalid command {parsed_command.command_name!r} for mod {parsed_command.mod_name!r}" |
| 118 | + ) |
| 119 | + # Let's get the list of actual commands for this mod. |
| 120 | + mod_command_keys = list( |
| 121 | + filter( |
| 122 | + lambda x: x.startswith(f"tc::{parsed_command.mod_name}"), |
| 123 | + hook_manager.custom_callbacks.keys(), |
| 124 | + ) |
| 125 | + ) |
| 126 | + if not mod_command_keys: |
| 127 | + lMessageText.contents.set( |
| 128 | + f"The mod {parsed_command.mod_name!r} either doesn't exist or has no " |
| 129 | + "terminal commands registered" |
| 130 | + ) |
| 131 | + else: |
| 132 | + command_funcs: dict[str, CustomTriggerProtocol] = {} |
| 133 | + for key in mod_command_keys: |
| 134 | + funcs = hook_manager.custom_callbacks[key].get( |
| 135 | + DetourTime.NONE, [] |
| 136 | + ) |
| 137 | + if len(funcs) > 1: |
| 138 | + logger.warning( |
| 139 | + f"Multiple terminal commands have been defined with the key {key}" |
| 140 | + ) |
| 141 | + return |
| 142 | + elif len(funcs) == 1: |
| 143 | + command_funcs[key] = list(funcs)[0] |
| 144 | + mod_desc = f"<TITLE>{parsed_command.mod_name!r} mod options:<>" |
| 145 | + for key, func in command_funcs.items(): |
| 146 | + _, command = split_key(key) |
| 147 | + mod_desc += "\n" + generate_full_description( |
| 148 | + command, func._description |
| 149 | + ) |
| 150 | + lMessageText.contents.set(mod_desc) |
| 151 | + |
| 152 | + @nms.cGcTextChatManager.Say.before |
| 153 | + def say_chat_message( |
| 154 | + self, |
| 155 | + this: ctypes._Pointer[nms.cGcTextChatManager], |
| 156 | + lsMessageBody: ctypes._Pointer[basic.cTkFixedString[0x3FF]], |
| 157 | + lbSystemMessage: bool, |
| 158 | + ): |
| 159 | + if lsMessageBody: |
| 160 | + msg = str(lsMessageBody.contents) |
| 161 | + # Check for the message which the game generates for an invalid command and add the mod command. |
| 162 | + if msg.startswith("<TITLE>") and msg.split("\n")[0].endswith("commands:"): |
| 163 | + msg = msg + MOD_OPTION + MOD_LIST_OPTION |
| 164 | + lsMessageBody.contents.set(msg) |
0 commit comments