-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobs_client.py
123 lines (99 loc) · 4.19 KB
/
obs_client.py
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
import logging
import sys
from pathlib import Path
from time import sleep, time
import click
import obsws_python as obsws
from rich import print
from config import config
from obs_tools.sc2client import sc2client
from obs_tools.types import Screen
log = logging.getLogger(__name__)
log_file = Path("logs/obs_client.log")
log.addHandler(logging.FileHandler(log_file, mode="a", encoding="utf-8"))
def log_uncaught_exceptions(exc_type, exc_value, exc_traceback):
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
log.error("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
sys.excepthook = log_uncaught_exceptions
sys.stdout = open(log_file, "a", encoding="utf-8")
# we set this up as a standalone process so that OBS can run and react to SC2 UI changes without the need
# to run the rest of the project.
# This will send the currently visible screen(s) in SC2 menus to OBS via the AdvancedSceneSwitcher plugin
# If ingame, it will send "In game" to OBS
# AdvancedSceneSwitcher can use these messages in macro conditions
@click.command()
@click.option("--verbose", is_flag=True)
@click.option("--debug", is_flag=True)
def main(verbose, debug):
"""Monitor SC2 UI through client API and let OBS know when loading screen is active"""
menu_screens = set([Screen.background, Screen.foreground, Screen.navigation])
if debug:
while True:
ui = sc2client.get_uiinfo()
print(ui.activeScreens)
sleep(1)
try:
with obsws.ReqClient(
host="localhost", port=4455, password=config.obs_ws_pw, timeout=3
) as obs:
resp = obs.get_version()
print(f"OBS Version: {resp.obs_version}")
last_ui = None
while True:
ui = sc2client.get_uiinfo()
with open("logs/time.log", "r") as f:
last_time = float(f.read())
diff = time() - last_time
if diff > 15:
data = {"message": "fadelog"}
else:
data = {"message": "showlog"}
obs.call_vendor_request(
vendor_name="AdvancedSceneSwitcher",
request_type="AdvancedSceneSwitcherMessage",
request_data=data,
)
if ui is None:
print(":warning: SC2 not running?")
sleep(5)
continue
if ui == last_ui:
# only notify OBS on changes
sleep(1)
continue
if verbose:
print(ui.activeScreens)
if len(ui.activeScreens) == 0:
print("In game")
data = {"message": "In game"}
obs.call_vendor_request(
vendor_name="AdvancedSceneSwitcher",
request_type="AdvancedSceneSwitcherMessage",
request_data=data,
)
elif Screen.loading in ui.activeScreens:
print(Screen.loading)
data = {"message": Screen.loading}
obs.call_vendor_request(
vendor_name="AdvancedSceneSwitcher",
request_type="AdvancedSceneSwitcherMessage",
request_data=data,
)
elif menu_screens < ui.activeScreens:
menues = ui.activeScreens - menu_screens
print("In menues " + str(menues))
data = {"message": "\n".join(sorted(menues))}
obs.call_vendor_request(
vendor_name="AdvancedSceneSwitcher",
request_type="AdvancedSceneSwitcherMessage",
request_data=data,
)
else:
pass
last_ui = ui
except (ConnectionRefusedError, ConnectionError) as e:
print(":x: Can't connect to OBS; Not running or web sockets off?")
if __name__ == "__main__":
main()