|
| 1 | +import threading |
| 2 | +import uuid |
| 3 | + |
| 4 | +from bot import * |
| 5 | + |
| 6 | + |
| 7 | +class Handler(logging.StreamHandler): |
| 8 | + def __init__(self, *args, **kwargs): |
| 9 | + super().__init__(*args, **kwargs) |
| 10 | + |
| 11 | + def emit(self, record): |
| 12 | + colors = {logging.DEBUG: "grey10", logging.INFO: "white"} |
| 13 | + if isinstance(record.msg, str) and record.msg in global_settings.messages: |
| 14 | + record.msg = global_settings.messages[record.msg] |
| 15 | + Sg.cprint(self.format(record), text_color=colors.get(record.levelno, "red")) |
| 16 | + |
| 17 | + |
| 18 | +class GUI: |
| 19 | + def __init__(self): |
| 20 | + self.queue = queue.Queue() |
| 21 | + self.bot_thread = None |
| 22 | + self.downloading_new_version = False |
| 23 | + |
| 24 | + self.last_window_check = time() |
| 25 | + |
| 26 | + self.window = self.create_window() |
| 27 | + |
| 28 | + display_changelog() |
| 29 | + |
| 30 | + handler = Handler() |
| 31 | + handler.setFormatter(MyFormatter()) |
| 32 | + global_settings.gui_handler = handler |
| 33 | + global_settings.set_debug_state() |
| 34 | + logger.addHandler(handler) |
| 35 | + |
| 36 | + Config.load() # Display old config warning on bot launch and not on settings opened |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def create_window(): |
| 40 | + Sg.theme("DarkGrey10") |
| 41 | + layout = [ |
| 42 | + [Sg.Text(" ", size=(1, 1), key="title", font=(global_settings.font, 20))], |
| 43 | + [Sg.Text(" ", size=(1, 1), key="version", font=(global_settings.font, 13))], |
| 44 | + [ |
| 45 | + Sg.Text( |
| 46 | + " ", |
| 47 | + size=(1, 1), |
| 48 | + key="press_start", |
| 49 | + font=(global_settings.font, 14), |
| 50 | + metadata=0, |
| 51 | + ) |
| 52 | + ], |
| 53 | + [ |
| 54 | + Sg.Multiline( |
| 55 | + size=(90, 10), |
| 56 | + key="log", |
| 57 | + disabled=True, |
| 58 | + autoscroll=True, |
| 59 | + write_only=True, |
| 60 | + reroute_cprint=True, |
| 61 | + reroute_stderr=global_settings.compiled, |
| 62 | + font=(global_settings.font, 12), |
| 63 | + text_color="red", |
| 64 | + ) |
| 65 | + ], |
| 66 | + ] |
| 67 | + |
| 68 | + buttons = [ |
| 69 | + [ |
| 70 | + Sg.Button( |
| 71 | + "", key="toggle", font=(global_settings.font, 12), metadata=0 |
| 72 | + ), |
| 73 | + Sg.Button("", key="instructions", font=(global_settings.font, 12)), |
| 74 | + Sg.Button("", key="settings", font=(global_settings.font, 12)), |
| 75 | + Sg.Button("", key="exit", font=(global_settings.font, 12)), |
| 76 | + ], |
| 77 | + [ |
| 78 | + Sg.Button( |
| 79 | + "", |
| 80 | + key="delayed_stop", |
| 81 | + font=(global_settings.font, 12), |
| 82 | + metadata=0, |
| 83 | + visible=False, |
| 84 | + ), |
| 85 | + Sg.Button("", key="take_screenshot", font=(global_settings.font, 12)), |
| 86 | + ], |
| 87 | + ] |
| 88 | + if not global_settings.compiled: |
| 89 | + buttons[0].append( |
| 90 | + Sg.Button("", key="test", font=(global_settings.font, 12)) |
| 91 | + ) |
| 92 | + |
| 93 | + layout += buttons |
| 94 | + layout.append([Sg.Text(" ", font="Any 50")]) |
| 95 | + |
| 96 | + if global_settings.new_version: |
| 97 | + global_settings.new_version.cleanup() |
| 98 | + update_column_layout = [ |
| 99 | + [ |
| 100 | + Sg.Text( |
| 101 | + " ", |
| 102 | + size=(1, 1), |
| 103 | + key="update_available_title", |
| 104 | + font=(global_settings.font, 25), |
| 105 | + ) |
| 106 | + ], |
| 107 | + [ |
| 108 | + Sg.Text( |
| 109 | + " ", |
| 110 | + size=(1, 1), |
| 111 | + key="update_available_version", |
| 112 | + font=(global_settings.font, 15), |
| 113 | + ) |
| 114 | + ], |
| 115 | + [ |
| 116 | + Sg.Button( |
| 117 | + "", |
| 118 | + key="update_available_button", |
| 119 | + font=(global_settings.font, 12), |
| 120 | + ) |
| 121 | + ], |
| 122 | + [Sg.Text(" ", font="Any 50")], |
| 123 | + ] |
| 124 | + layout.append([Sg.Column(update_column_layout)]) |
| 125 | + |
| 126 | + window = Sg.Window( |
| 127 | + "", layout, icon=global_settings.icon, metadata="main_window_title" |
| 128 | + ).Finalize() |
| 129 | + global_settings.update_window(window) |
| 130 | + return window |
| 131 | + |
| 132 | + def toggle_bot(self): |
| 133 | + if self.bot_thread and self.bot_thread.is_alive(): |
| 134 | + logger.info("stop_bot") |
| 135 | + self.queue.put_nowait("STOP") |
| 136 | + else: |
| 137 | + logger.info("start_bot") |
| 138 | + config = Config.load() |
| 139 | + logger.debug(global_settings) |
| 140 | + logger.debug(config) |
| 141 | + bot = BrawlhallaBot(config, Hotkeys.load(), self.queue) |
| 142 | + self.bot_thread = threading.Thread(target=bot.main_loop, daemon=True) |
| 143 | + self.bot_thread.start() |
| 144 | + |
| 145 | + def clear_queue(self): |
| 146 | + while not self.queue.empty(): |
| 147 | + try: |
| 148 | + self.queue.get_nowait() |
| 149 | + except queue.Empty: |
| 150 | + continue |
| 151 | + self.queue.task_done() |
| 152 | + |
| 153 | + def delayed_stop(self): |
| 154 | + if self.bot_thread and self.bot_thread.is_alive(): |
| 155 | + if self.window["delayed_stop"].metadata: |
| 156 | + logger.info("cancel_stop") |
| 157 | + self.clear_queue() |
| 158 | + else: |
| 159 | + logger.info("delayed_stop") |
| 160 | + self.queue.put_nowait("DELAYED_STOP") |
| 161 | + self.window["delayed_stop"].metadata = not self.window[ |
| 162 | + "delayed_stop" |
| 163 | + ].metadata |
| 164 | + |
| 165 | + def refresh_buttons(self): |
| 166 | + if self.downloading_new_version: |
| 167 | + self.window["toggle"].update(disabled=True) |
| 168 | + self.window["delayed_stop"].update(visible=False) |
| 169 | + self.window["instructions"].update(disabled=True) |
| 170 | + self.window["settings"].update(disabled=True) |
| 171 | + self.window["update_available_button"].update(disabled=True) |
| 172 | + self.window["exit"].update(disabled=True) |
| 173 | + elif self.bot_thread and self.bot_thread.is_alive(): |
| 174 | + self.window["toggle"].metadata = 1 |
| 175 | + self.window["press_start"].metadata = 1 |
| 176 | + self.window["delayed_stop"].Update(visible=True) |
| 177 | + self.window["instructions"].Update(disabled=True) |
| 178 | + self.window["settings"].Update(disabled=True) |
| 179 | + else: |
| 180 | + self.window["toggle"].metadata = 0 |
| 181 | + self.window["press_start"].metadata = 0 |
| 182 | + self.window["delayed_stop"].metadata = 0 |
| 183 | + self.window["delayed_stop"].Update(visible=False) |
| 184 | + self.window["instructions"].Update(disabled=False) |
| 185 | + self.window["settings"].Update(disabled=False) |
| 186 | + self.clear_queue() |
| 187 | + |
| 188 | + @staticmethod |
| 189 | + def display_instructions(): |
| 190 | + contents = global_settings.language.LAYOUT_MAPPING.get( |
| 191 | + "instructions_contents", |
| 192 | + global_settings.get_language("English").LAYOUT_MAPPING.get( |
| 193 | + "instructions_contents", "Should be stuff here" |
| 194 | + ), |
| 195 | + ) |
| 196 | + Sg.popup( |
| 197 | + *contents, |
| 198 | + font=(global_settings.font, 13), |
| 199 | + title=global_settings.language.LAYOUT_MAPPING.get( |
| 200 | + "instructions_window_title", "Instructions" |
| 201 | + ), |
| 202 | + icon=global_settings.icon, |
| 203 | + ) |
| 204 | + |
| 205 | + def configure(self): |
| 206 | + settings = GUIConfig() |
| 207 | + self.window.disable() |
| 208 | + self.window.hide() |
| 209 | + settings.start_loop() |
| 210 | + self.window.enable() |
| 211 | + self.window.un_hide() |
| 212 | + |
| 213 | + def autostart(self): |
| 214 | + if not global_settings.autostart: |
| 215 | + return |
| 216 | + if self.bot_thread and self.bot_thread.is_alive(): |
| 217 | + self.last_window_check = time() |
| 218 | + elif time() - self.last_window_check > 300: |
| 219 | + logger.debug("autostart_check") |
| 220 | + bh = BrawlhallaProcess.find() |
| 221 | + if not bh: |
| 222 | + self.toggle_bot() |
| 223 | + self.last_window_check = time() |
| 224 | + |
| 225 | + def main_loop(self): |
| 226 | + while True: |
| 227 | + event, values = self.window.Read(timeout=50) |
| 228 | + if event in (Sg.WINDOW_CLOSED, "exit"): |
| 229 | + break |
| 230 | + elif event == "toggle": |
| 231 | + self.toggle_bot() |
| 232 | + elif event == "delayed_stop": |
| 233 | + self.delayed_stop() |
| 234 | + elif event == "instructions": |
| 235 | + self.display_instructions() |
| 236 | + elif event == "settings": |
| 237 | + self.configure() |
| 238 | + elif event == "test": |
| 239 | + logger.info("test") |
| 240 | + logger.debug("test") |
| 241 | + elif event == "take_screenshot": |
| 242 | + bh = BrawlhallaProcess.find() |
| 243 | + if bh is None: |
| 244 | + logger.error("bh_not_running") |
| 245 | + else: |
| 246 | + screenshot_name = f"screenshot-{uuid.uuid4()}.png" |
| 247 | + screenshot_path = ( |
| 248 | + Path(os.getenv("LOCALAPPDATA")) / "BHBot" / screenshot_name |
| 249 | + ) |
| 250 | + bh.make_screenshot().save(screenshot_path) |
| 251 | + logger.info("took_screenshot", str(screenshot_path)) |
| 252 | + |
| 253 | + elif event == "update_available_button": |
| 254 | + global_settings.new_version.download(background=True) |
| 255 | + self.downloading_new_version = True |
| 256 | + elif not self.downloading_new_version: |
| 257 | + self.autostart() |
| 258 | + if ( |
| 259 | + global_settings.compiled |
| 260 | + and global_settings.new_version |
| 261 | + and self.downloading_new_version |
| 262 | + and global_settings.new_version.is_downloaded() |
| 263 | + ): |
| 264 | + global_settings.new_version.extract_restart() |
| 265 | + self.refresh_buttons() |
| 266 | + global_settings.update_window(self.window) |
| 267 | + |
| 268 | + self.window.close() |
| 269 | + |
| 270 | + |
| 271 | +if __name__ == "__main__": |
| 272 | + Singleton() |
| 273 | + gui = GUI() |
| 274 | + gui.main_loop() |
0 commit comments