-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBroadCast-Archipelago.pyw
More file actions
655 lines (551 loc) · 31.8 KB
/
Copy pathBroadCast-Archipelago.pyw
File metadata and controls
655 lines (551 loc) · 31.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import tkinter as tk
from tkinter import ttk, messagebox
import threading
import subprocess
import json
import os
import sys
import ctypes
from ctypes import wintypes
# Settings and Base Path
APP_DIR = os.path.dirname(os.path.abspath(__file__))
SETTINGS_FILE = os.path.join(APP_DIR, "broadcast_settings.json")
def save_settings(gui_settings):
try:
# 1. Load the REAL state from the disk right now
disk_settings = {}
if os.path.exists(SETTINGS_FILE):
with open(SETTINGS_FILE, "r") as f:
try:
disk_settings = json.load(f)
except: pass
# 2. Only update keys that are actually managed by the GUI
# This prevents the GUI from overwriting bridge-only settings like durations
gui_keys = [
"server", "slot", "password", "multi_slots", "sync_mode",
"obs_sync_mode", "enable_overlay", "enable_obs", "display_index",
"tracked_players", "win_w", "win_h", "win_x", "win_y", "disable_hw_accel", "show_locations"
]
for k in gui_keys:
if k in gui_settings:
disk_settings[k] = gui_settings[k]
# 3. Save the merged result
with open(SETTINGS_FILE, "w") as f:
json.dump(disk_settings, f, indent=4)
except Exception as e:
print(f"Error saving settings: {e}")
def load_settings():
defaults = {
"server": "archipelago.gg:", "slot": "", "password": "", "mode": "all",
"win_w": 400, "win_h": 600, "win_x": -1, "win_y": -1, "display_index": 0,
"last_game": "", "multi_slots": "", "tracked_players": "",
"sync_mode": "all", "obs_sync_mode": "all", "enable_overlay": True, "enable_obs": False,
"overlay_duration": 10, "obs_duration": 15, "obs_fade": False, "disable_hw_accel": False, "show_locations": True
}
if os.path.exists(SETTINGS_FILE):
try:
with open(SETTINGS_FILE, "r") as f:
data = json.load(f)
# Ensure all default keys exist
for k, v in defaults.items():
if k not in data: data[k] = v
return data
except: pass
return defaults
def get_monitors():
monitors = []
def callback(hMonitor, hdcMonitor, lprcMonitor, dwData):
rect = lprcMonitor.contents
monitors.append({
'x': rect.left,
'y': rect.top,
'width': rect.right - rect.left,
'height': rect.bottom - rect.top
})
return True
MonitorEnumProc = ctypes.WINFUNCTYPE(ctypes.c_bool, wintypes.HMONITOR, wintypes.HDC, ctypes.POINTER(wintypes.RECT), wintypes.LPARAM)
ctypes.windll.user32.EnumDisplayMonitors(None, None, MonitorEnumProc(callback), 0)
return monitors
return monitors
def kill_port(port):
"""Forcefully kill any process using a specific TCP port."""
try:
import subprocess
# Find PIDs using the port
output = subprocess.check_output(f'netstat -ano | findstr :{port}', shell=True, creationflags=0x08000000).decode()
pids = set()
for line in output.strip().split('\n'):
parts = line.split()
if parts:
pids.add(parts[-1])
for pid in pids:
if pid != "0": # Avoid killing system
subprocess.run(['taskkill', '/F', '/T', '/PID', pid], capture_output=True, creationflags=0x08000000)
print(f"Killed process {pid} using port {port}")
except:
pass
class BroadcastLauncherApp:
def create_dim_field(self, parent, label, val, row, col):
f = tk.Frame(parent, bg="#0d0d0f")
f.grid(row=row, column=col, sticky="ew", padx=2, pady=2)
tk.Label(f, text=label, bg="#0d0d0f", fg="#555555", font=("Segoe UI", 8)).pack(side="left")
e = tk.Entry(f, width=10, bg="#1a1a1c", fg="#ffffff", border=0, font=("Segoe UI", 9))
e.insert(0, str(val))
e.bind("<KeyRelease>", lambda e: self.update_preview())
e.pack(side="right", padx=5)
return e
def __init__(self, root):
self.root = root
self.root.title("BroadCast Archipelago - Control Center")
self.root.geometry("850x640") # Wider but shorter for two columns
self.root.resizable(False, False)
self.root.configure(bg="#0d0d0f")
self.settings = load_settings()
self.is_running = False
self.procs = []
self.log_files = []
self.monitors = get_monitors()
# Ensure logs directory exists
self.logs_dir = os.path.join(APP_DIR, "logs")
if not os.path.exists(self.logs_dir):
os.makedirs(self.logs_dir)
self.drag_start_x = 0
self.drag_start_y = 0
# Determine initial monitor
self.current_monitor_idx = self.settings.get("display_index", 0)
if self.current_monitor_idx >= len(self.monitors) or self.current_monitor_idx < 0:
self.current_monitor_idx = 0
m = self.monitors[self.current_monitor_idx]
self.screen_w = m['width']
self.screen_h = m['height']
self.screen_offset_x = m['x']
self.screen_offset_y = m['y']
# --- HEADER ---
header = tk.Frame(root, bg="#121214", height=60)
header.pack(fill="x", side="top")
tk.Label(header, text="BROADCAST CENTER", bg="#121214", fg="#af99ef", font=("Segoe UI", 14, "bold")).pack(pady=15)
# --- TWO COLUMN CONTAINER ---
content_frame = tk.Frame(root, bg="#0d0d0f", padx=20, pady=10)
content_frame.pack(fill="both", expand=True)
left_pane = tk.Frame(content_frame, bg="#0d0d0f"); left_pane.pack(side="left", fill="both", expand=True, padx=(0, 20))
right_pane = tk.Frame(content_frame, bg="#0d0d0f"); right_pane.pack(side="right", fill="both", expand=True)
def create_label(text, parent=left_pane):
return tk.Label(parent, text=text, bg="#0d0d0f", fg="#888888", font=("Segoe UI", 9))
def create_entry(val, parent=left_pane, width=40):
e = tk.Entry(parent, width=width, bg="#1a1a1c", fg="#ffffff", border=0, font=("Segoe UI", 10))
e.insert(0, str(val))
e.bind("<KeyRelease>", lambda e: self.update_preview())
return e
# --- LEFT PANE ---
tk.Label(left_pane, text="CONNECTION & PROFILES", bg="#0d0d0f", fg="#6d8be8", font=("Segoe UI", 10, "bold")).pack(pady=(0, 10), anchor="w")
create_label("Server Address").pack(anchor="w")
self.server_entry = create_entry(self.settings["server"]); self.server_entry.pack(pady=(0, 10), fill="x", ipady=5)
create_label("Player Slot Name").pack(anchor="w")
self.slot_entry = create_entry(self.settings["slot"]); self.slot_entry.pack(pady=(0, 10), fill="x", ipady=5)
create_label("Server Password (optional)").pack(anchor="w")
self.pass_entry = create_entry(self.settings["password"]); self.pass_entry.config(show="*"); self.pass_entry.pack(pady=(0, 10), fill="x", ipady=5)
create_label("Multi-Slots (Format: Slot1, Slot2, Slot3)").pack(anchor="w")
self.watched_entry = create_entry(self.settings.get("multi_slots", ""))
self.watched_entry.pack(pady=(0, 10), fill="x", ipady=5)
create_label("Tracked Players (Mode Filtered - Comma separated)").pack(anchor="w")
self.tracked_entry = create_entry(self.settings.get("tracked_players", ""))
self.tracked_entry.pack(pady=(0, 10), fill="x", ipady=5)
# Sync Modes (Separated for Overlay and OBS)
sync_container = tk.Frame(left_pane, bg="#0d0d0f"); sync_container.pack(fill="x", pady=(0, 10))
# Overlay Mode
ov_f = tk.Frame(sync_container, bg="#0d0d0f"); ov_f.pack(side="left", fill="x", expand=True)
create_label("Overlay Sync", ov_f).pack(anchor="w")
self.sync_var = tk.StringVar(value=self.settings.get("sync_mode", "personal")) # Desktop default
tk.Radiobutton(ov_f, text="Personal", variable=self.sync_var, value="personal", bg="#0d0d0f", fg="white", selectcolor="#2a2a2c", border=0, font=("Segoe UI", 8)).pack(side="left")
tk.Radiobutton(ov_f, text="Filtered", variable=self.sync_var, value="filtered", bg="#0d0d0f", fg="white", selectcolor="#2a2a2c", border=0, font=("Segoe UI", 8)).pack(side="left")
tk.Radiobutton(ov_f, text="Global", variable=self.sync_var, value="all", bg="#0d0d0f", fg="white", selectcolor="#2a2a2c", border=0, font=("Segoe UI", 8)).pack(side="left")
# OBS Mode
obs_f = tk.Frame(sync_container, bg="#0d0d0f"); obs_f.pack(side="right", fill="x", expand=True)
create_label("OBS Sync", obs_f).pack(anchor="w")
self.obs_sync_var = tk.StringVar(value=self.settings.get("obs_sync_mode", "all")) # OBS default
tk.Radiobutton(obs_f, text="Personal", variable=self.obs_sync_var, value="personal", bg="#0d0d0f", fg="white", selectcolor="#2a2a2c", border=0, font=("Segoe UI", 8)).pack(side="left")
tk.Radiobutton(obs_f, text="Filtered", variable=self.obs_sync_var, value="filtered", bg="#0d0d0f", fg="white", selectcolor="#2a2a2c", border=0, font=("Segoe UI", 8)).pack(side="left")
tk.Radiobutton(obs_f, text="Global", variable=self.obs_sync_var, value="all", bg="#0d0d0f", fg="white", selectcolor="#2a2a2c", border=0, font=("Segoe UI", 8)).pack(side="left")
# Output Features
create_label("Output Features").pack(anchor="w")
feat_f = tk.Frame(left_pane, bg="#0d0d0f"); feat_f.pack(fill="x", pady=(0, 10))
self.use_overlay = tk.BooleanVar(value=self.settings.get("enable_overlay", True))
self.use_obs = tk.BooleanVar(value=self.settings.get("enable_obs", False))
self.disable_hw = tk.BooleanVar(value=self.settings.get("disable_hw_accel", False))
self.show_locs = tk.BooleanVar(value=self.settings.get("show_locations", True))
row1 = tk.Frame(feat_f, bg="#0d0d0f"); row1.pack(fill="x")
tk.Checkbutton(row1, text="Desktop Overlay", variable=self.use_overlay, bg="#0d0d0f", fg="white", selectcolor="#2a2a2c", activebackground="#0d0d0f").pack(side="left", padx=5)
tk.Checkbutton(row1, text="OBS Web Server", variable=self.use_obs, bg="#0d0d0f", fg="white", selectcolor="#2a2a2c", activebackground="#0d0d0f").pack(side="left", padx=5)
tk.Checkbutton(row1, text="Show Locations", variable=self.show_locs, bg="#0d0d0f", fg="white", selectcolor="#2a2a2c", activebackground="#0d0d0f").pack(side="left", padx=5)
row2 = tk.Frame(feat_f, bg="#0d0d0f"); row2.pack(fill="x", pady=(5, 0))
tk.Checkbutton(row2, text="Disable HW Acceleration", variable=self.disable_hw, bg="#0d0d0f", fg="#ff7777", selectcolor="#2a2a2c", activebackground="#0d0d0f").pack(side="left", padx=5)
# --- RIGHT PANE ---
tk.Label(right_pane, text="WINDOW & PREVIEW", bg="#0d0d0f", fg="#6d8be8", font=("Segoe UI", 10, "bold")).pack(pady=(0, 10), anchor="w")
create_label("Target Display", right_pane).pack(anchor="w")
self.monitor_select = ttk.Combobox(right_pane, state="readonly", values=[f"Display {i+1} ({m['width']}x{m['height']})" for i, m in enumerate(self.monitors)])
self.monitor_select.current(self.current_monitor_idx); self.monitor_select.bind("<<ComboboxSelected>>", self.on_monitor_change); self.monitor_select.pack(pady=(0, 10), fill="x")
self.canvas_w = 340; self.canvas_h = (self.canvas_w * self.screen_h) // self.screen_w
self.preview_canvas = tk.Canvas(right_pane, width=self.canvas_w, height=self.canvas_h, bg="#000000", highlightthickness=1, highlightbackground="#333333")
self.preview_canvas.pack(pady=(0, 10)); self.preview_canvas.bind("<Button-1>", self.on_preview_click); self.preview_canvas.bind("<B1-Motion>", self.on_preview_drag); self.preview_canvas.bind("<ButtonRelease-1>", self.on_preview_release)
dim_container = tk.Frame(right_pane, bg="#0d0d0f"); dim_container.pack(fill="x")
self.win_w = self.create_dim_field(dim_container, "W", self.settings["win_w"], 0, 0)
self.win_h = self.create_dim_field(dim_container, "H", self.settings["win_h"], 0, 1)
self.win_x = self.create_dim_field(dim_container, "X", self.settings["win_x"], 1, 0)
self.win_y = self.create_dim_field(dim_container, "Y", self.settings["win_y"], 1, 1)
# --- FOOTER ---
footer = tk.Frame(root, bg="#121214"); footer.pack(fill="x", side="bottom")
self.status_label = tk.Label(footer, text="Status: Ready", bg="#121214", fg="#888888", font=("Segoe UI", 9))
self.status_label.pack(pady=(10, 0))
self.btn_text = tk.StringVar(value="START SYSTEM")
self.start_btn = tk.Button(footer, textvariable=self.btn_text, command=self.toggle_system, bg="#af99ef", fg="#121214", font=("Segoe UI", 11, "bold"), width=30, border=0, cursor="hand2")
self.start_btn.pack(pady=15)
self.update_preview()
tool_frame = tk.Frame(root, bg="#0d0d0f"); tool_frame.pack(side="bottom", fill="x", pady=5)
tk.Button(tool_frame, text="TEST MESSAGES", command=self.trigger_test_fill, bg="#0d0d0f", fg="#55ff55", font=("Segoe UI", 8), border=0, cursor="hand2").pack(side="left", padx=20)
tk.Button(tool_frame, text="VIEW LOGS", command=self.open_logs_folder, bg="#0d0d0f", fg="#6d8be8", font=("Segoe UI", 8), border=0, cursor="hand2").pack(side="left")
tk.Button(tool_frame, text="RESET HISTORY", command=self.trigger_clear_history, bg="#0d0d0f", fg="#ff5555", font=("Segoe UI", 8), border=0, cursor="hand2").pack(side="left", padx=20)
tk.Button(tool_frame, text="🔍 DIAGNOSE", command=self.show_troubleshooting, bg="#0d0d0f", fg="#888888", font=("Segoe UI", 8), border=0, cursor="hand2").pack(side="right", padx=20)
self.update_preview()
def get_error_message(self, hex_code, p_name):
# 0xc0000142 is 3221225794 unsigned, or -1073741502 signed
msgs = {
"3221225794": "DLL Init Failed. Try restarting your PC or checking and installing 'Visual C++ Redistributable'.",
"3221225781": "Missing System DLL. Ensure you have the latest Windows Updates and C++ Runtimes.",
"1": "Script Error. Python or Node modules are missing. Try running INSTALLATION.bat.",
"9009": f"Command not found. Is {'Node.js' if 'Vite' in p_name or 'Overlay' in p_name else 'Python'} installed and in PATH?",
"3": "Path not found. The app couldn't find the 'broadcast-app' folder.",
"127": "Command not found (Linux/Unix).",
"-1073741502": "DLL Init Failed. Try restarting your PC or checking and installing 'Visual C++ Redistributable'."
}
return msgs.get(str(hex_code), "Unknown error. Check logs or contact support.")
def open_logs_folder(self):
os.startfile(self.logs_dir)
def show_troubleshooting(self):
msg = "--- Common Fixes ---\n\n"
msg += "1. Run 'INSTALLATION.bat' to ensure all libraries are present.\n"
msg += "2. Ensure Node.js (v20+) and Python (3.12) are installed.\n"
msg += "3. Don't move the app files out of their folders.\n"
msg += "4. Check if an antivirus is blocking 'electron.exe'.\n"
msg += "5. 'Electron failed to install correctly'? Delete 'broadcast-app/node_modules' and run 'INSTALLATION.bat' again. Or run CMD in 'broadcast-app' and type:\n node node_modules/electron/install.js\n"
msg += "6. If port 8089 is used by another app, the bridge will crash.\n"
msg += "7. SSL Error? If using a local server, ensure you use 'localhost:port'.\n"
msg += "8. If using archipelago.gg, the address should be 'archipelago.gg:PORT'."
messagebox.showinfo("Diagnostic Tool", msg)
def on_monitor_change(self, event=None):
self.current_monitor_idx = self.monitor_select.current()
m = self.monitors[self.current_monitor_idx]
self.screen_w = m['width']
self.screen_h = m['height']
self.screen_offset_x = m['x']
self.screen_offset_y = m['y']
# Automatically move the coordinates to the new monitor (bottom right corner)
try:
w = int(self.win_w.get())
h = int(self.win_h.get())
new_x = self.screen_offset_x + self.screen_w - w - 20
new_y = self.screen_offset_y + self.screen_h - h - 20
self.win_x.delete(0, tk.END)
self.win_x.insert(0, str(new_x))
self.win_y.delete(0, tk.END)
self.win_y.insert(0, str(new_y))
except: pass
# Update canvas aspect ratio
self.canvas_h = (self.canvas_w * self.screen_h) // self.screen_w
self.preview_canvas.config(height=self.canvas_h)
self.update_preview()
def on_preview_click(self, event):
self.drag_start_x = event.x
self.drag_start_y = event.y
self.preview_canvas.config(cursor="fleur")
def on_preview_release(self, event):
self.preview_canvas.config(cursor="")
def on_preview_drag(self, event):
dx = event.x - self.drag_start_x
dy = event.y - self.drag_start_y
# Scale back to screen coordinates
scale_x = self.screen_w / self.canvas_w
scale_y = self.screen_h / self.canvas_h
try:
w = int(self.win_w.get())
h = int(self.win_h.get())
cur_x = int(self.win_x.get())
cur_y = int(self.win_y.get())
# Absolute default positions if -1
if cur_x == -1: cur_x = self.screen_offset_x + self.screen_w - w - 20
if cur_y == -1: cur_y = self.screen_offset_y + self.screen_h - h - 20
new_x = cur_x + int(dx * scale_x)
new_y = cur_y + int(dy * scale_y)
# Snap to pixels
self.win_x.delete(0, tk.END)
self.win_x.insert(0, str(new_x))
self.win_y.delete(0, tk.END)
self.win_y.insert(0, str(new_y))
self.drag_start_x = event.x
self.drag_start_y = event.y
self.update_preview()
except: pass
def update_preview(self):
try:
w = int(self.win_w.get())
h = int(self.win_h.get())
x = int(self.win_x.get())
y = int(self.win_y.get())
# Position logic for preview
if x == -1:
# Centered default if -1 (standard Archipelago logic or bottom-right)
calc_x = self.screen_offset_x + self.screen_w - w - 20
else:
calc_x = x
if y == -1:
calc_y = self.screen_offset_y + self.screen_h - h - 20
else:
calc_y = y
# Positions in preview are relative to the selected monitor's origin
rel_x = calc_x - self.screen_offset_x
rel_y = calc_y - self.screen_offset_y
# Scale to canvas
scale_x = self.canvas_w / self.screen_w
scale_y = self.canvas_h / self.screen_h
pv_x = rel_x * scale_x
pv_y = rel_y * scale_y
pv_w = w * scale_x
pv_h = h * scale_y
self.preview_canvas.delete("all")
# Draw warning border if out of bounds (relative to this screen)
color = "#af99ef"
if rel_x < 0 or rel_y < 0 or (rel_x + w) > self.screen_w or (rel_y + h) > self.screen_h:
color = "#ff5555" # Red warning
# Grid indicator
for i in range(0, self.canvas_w, 20):
self.preview_canvas.create_line(i, 0, i, self.canvas_h, fill="#222222")
for i in range(0, self.canvas_h, 20):
self.preview_canvas.create_line(0, i, self.canvas_w, i, fill="#222222")
self.preview_canvas.create_rectangle(pv_x, pv_y, pv_x + pv_w, pv_y + pv_h, fill=color, outline="white", stipple="gray50")
self.preview_canvas.create_text(self.canvas_w/2, self.canvas_h/2, text=f"{self.screen_w}x{self.screen_h}", fill="#444444", font=("Segoe UI", 8))
except Exception as e:
print(f"Preview error: {e}")
pass
def trigger_clear_history(self):
if not self.is_running:
messagebox.showinfo("Info", "System must be running to clear history on all pages.")
return
def send_clear_cmd():
try:
import asyncio
import websockets
async def send():
async with websockets.connect("ws://127.0.0.1:8089") as ws:
await ws.send(json.dumps({"type": "clear_history"}))
asyncio.run(send())
except: pass
threading.Thread(target=send_clear_cmd, daemon=True).start()
self.status_label.config(text="Status: History Cleared Everywhere!", fg="#ff5555")
def trigger_test_fill(self):
if not self.is_running:
messagebox.showinfo("Info", "System must be running to send test messages.")
return
def send_test_cmd():
try:
import asyncio
import websockets
async def send():
async with websockets.connect("ws://127.0.0.1:8089") as ws:
await ws.send(json.dumps({"type": "test_fill"}))
asyncio.run(send())
except: pass
threading.Thread(target=send_test_cmd, daemon=True).start()
self.status_label.config(text="Status: Test Messages Sent!", fg="#55ff55")
def toggle_system(self):
if not self.is_running: self.start_system()
else: self.stop_system()
def start_system(self):
try:
# Get common settings
self.settings.update({
"server": self.server_entry.get(),
"slot": self.slot_entry.get(),
"password": self.pass_entry.get(),
"multi_slots": self.watched_entry.get(),
"sync_mode": self.sync_var.get(),
"obs_sync_mode": self.obs_sync_var.get(),
"enable_overlay": self.use_overlay.get(),
"enable_obs": self.use_obs.get(),
"disable_hw_accel": self.disable_hw.get(),
"show_locations": self.show_locs.get(),
"display_index": self.monitor_select.current(),
"tracked_players": self.tracked_entry.get()
})
# Dimensions only mandatory if Overlay is enabled
if self.use_overlay.get():
self.settings.update({
"win_w": int(self.win_w.get() or 400),
"win_h": int(self.win_h.get() or 600),
"win_x": int(self.win_x.get() or 0),
"win_y": int(self.win_y.get() or 0)
})
except ValueError:
messagebox.showerror("Error", "Window dimensions (W, H, X, Y) must be numbers.")
return
except Exception as e:
messagebox.showerror("Error", f"Configuration error: {e}")
return
save_settings(self.settings)
if not self.settings["server"] or not self.settings["slot"]:
messagebox.showerror("Error", "Server and Slot are mandatory.")
return
# NEW: Force cleanup of any previous session leftovers and free ports
self.stop_system()
self.status_label.config(text="Status: Cleaning up...", fg="#ffaa00")
self.root.update()
# Force delete dist folder to ensure we use latest dev code (Only in Dev environment)
try:
import shutil
dist_to_clean = os.path.join(APP_DIR, "broadcast-app", "dist")
src_dir = os.path.join(APP_DIR, "broadcast-app", "src")
if os.path.exists(src_dir) and os.path.exists(dist_to_clean):
shutil.rmtree(dist_to_clean)
except: pass
import time
time.sleep(1)
kill_port(8089) # Bridge Port (Always needed)
# Free Vite port if any web feature is needed
if self.use_obs.get() or self.use_overlay.get():
kill_port(5173)
self.is_running = True
self.btn_text.set("STOP SYSTEM")
if hasattr(self, 'start_btn'): self.start_btn.configure(bg="#ff5555", fg="white")
self.status_label.config(text="Status: Launching...", fg="#00eeee")
threading.Thread(target=self.launch_background_tasks, daemon=True).start()
def launch_background_tasks(self):
# Determine best python command
py_cmd = "py -3.12"
try:
subprocess.run(["py", "-3.12", "--version"], capture_output=True, check=True)
except:
py_cmd = "python" # Fallback
# Helpers for logging
def spawn_with_log(cmd, name, cwd=None):
if cwd:
cwd = os.path.join(APP_DIR, cwd)
else:
cwd = APP_DIR
log_path = os.path.join(self.logs_dir, f"{name}.log")
f = open(log_path, "w", encoding="utf-8")
self.log_files.append(f)
# FORCE disable colors for cleaner logs in text files
env = os.environ.copy()
env["FORCE_COLOR"] = "0"
env["NO_COLOR"] = "1"
env["TERM"] = "dumb"
# 0x08000000 is CREATE_NO_WINDOW on Windows
return subprocess.Popen(cmd, cwd=cwd, stdout=f, stderr=f, shell=True if isinstance(cmd, str) else False, creationflags=0x08000000, env=env)
# Start Web Server only if needed
dist_path = os.path.join(APP_DIR, "broadcast-app", "dist")
has_build = os.path.exists(dist_path) and os.path.exists(os.path.join(dist_path, "index.html"))
if self.use_obs.get():
if has_build:
# Use lightweight Python built-in HTTP server to serve static compiled frontend on port 5173
cmd = py_cmd.split() + ["-m", "http.server", "5173", "--directory", dist_path]
self.procs.append(spawn_with_log(cmd, "webserver"))
else:
# Fall back to Vite dev server if dist is missing
self.procs.append(spawn_with_log(["cmd", "/c", "npx vite --no-open"], "vite", cwd="broadcast-app"))
elif self.use_overlay.get() and not has_build:
# Needed for dev mode overlay without pre-built dist
self.procs.append(spawn_with_log(["cmd", "/c", "npx vite --no-open"], "vite", cwd="broadcast-app"))
# Determine bridge mode: If EITHER is 'all' or 'filtered', bridge must be 'all' to get the data
bridge_mode = "all"
if self.settings.get("sync_mode") == "personal" and self.settings.get("obs_sync_mode") == "personal":
bridge_mode = "personal"
bridge_script = os.path.join(APP_DIR, "broadcast", "bridge.py")
bridge_cmd = py_cmd.split() + ["-u", bridge_script, "--server", self.settings["server"], "--slot", self.settings["slot"], "--mode", bridge_mode]
if self.settings["password"]: bridge_cmd.extend(["--password", self.settings["password"]])
cached_game = self.settings.get("last_game")
if cached_game: bridge_cmd.extend(["--game", cached_game])
multi = self.settings.get("multi_slots", "").strip()
if multi: bridge_cmd.extend(["--multi", multi])
tracked = self.settings.get("tracked_players", "").strip()
if tracked: bridge_cmd.extend(["--tracked", tracked])
try:
self.procs.append(spawn_with_log(bridge_cmd, "bridge"))
except Exception as e:
messagebox.showerror("Error", f"Could not launch bridge: {e}")
self.stop_system()
return
import time
for i in range(2, 0, -1):
if not self.is_running: return
self.status_label.config(text=f"Status: Overlay launching in {i}s...")
time.sleep(1)
# Launch Electron Overlay ONLY if enabled
if self.use_overlay.get():
# First, check if there is a pre-packaged standalone production version of the overlay
packaged_exe = os.path.join(APP_DIR, "broadcast-app", "dist-packaged", "win-unpacked", "Broadcast-Overlay.exe")
if os.path.exists(packaged_exe):
self.procs.append(spawn_with_log([packaged_exe], "overlay"))
self.status_label.config(text="Status: Overlay (Standalone) & Bridge Operational", fg="#55ff55")
else:
# Fall back to development node_modules environment
electron_exe = os.path.join(APP_DIR, "broadcast-app", "node_modules", "electron", "dist", "electron.exe")
if not os.path.exists(electron_exe):
messagebox.showerror(
"Electron Missing / Error",
"Electron binary is missing or was not installed correctly!\n\n"
"This usually happens due to connection issues or antivirus block during setup.\n\n"
"To fix this:\n"
"1. Close this app.\n"
"2. Run 'INSTALL_NODE_ONLY.bat' to trigger automatic repair.\n"
"3. If that doesn't work, open CMD in 'broadcast-app' and run:\n"
" node node_modules/electron/install.js"
)
self.stop_system()
return
self.procs.append(spawn_with_log(["cmd", "/c", "npm run overlay"], "overlay", cwd="broadcast-app"))
self.status_label.config(text="Status: Overlay & Bridge Operational", fg="#55ff55")
else:
self.status_label.config(text="Status: Web Server & Bridge Operational", fg="#55ff55")
# Monitor health in background
while self.is_running:
for p in list(self.procs):
retcode = p.poll()
if retcode is not None:
# Process died unexpectedly
if self.is_running:
# Try to identify which process it was
p_args = p.args if hasattr(p, 'args') else []
p_name = "Unknown"
if any("bridge.py" in str(a) for a in p_args): p_name = "Bridge"
elif any("vite" in str(a) for a in p_args): p_name = "Vite Server"
elif any("overlay" in str(a) for a in p_args): p_name = "Overlay"
hint = self.get_error_message(retcode, p_name)
self.status_label.config(text=f"Status: {p_name} Crashed! ({retcode})\n{hint}", fg="#ff5555", font=("Segoe UI", 8))
self.procs.remove(p)
time.sleep(2)
def stop_system(self):
if not self.is_running: return
self.is_running = False
self.status_label.config(text="Status: Stopping...", fg="#ffaa00")
self.root.update()
# Copy the list to avoid race conditions with the monitoring thread
active_procs = list(self.procs)
self.procs = []
# Kill tracked processes in background to avoid blocking the UI
for p in active_procs:
try:
# /T kills child processes too. We use Popen so we don't wait for results.
subprocess.Popen(["taskkill", "/F", "/T", "/PID", str(p.pid)],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
creationflags=0x08000000)
except: pass
# Safety: Close log files
for f in self.log_files:
try: f.close()
except: pass
self.log_files = []
self.btn_text.set("START SYSTEM")
if hasattr(self, 'start_btn'): self.start_btn.configure(bg="#af99ef", fg="#121214")
self.status_label.config(text="Status: Stopped", fg="#6d8be8")
if __name__ == "__main__":
root = tk.Tk()
app = BroadcastLauncherApp(root)
root.mainloop()