-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathegcapilite.py
251 lines (202 loc) · 5.93 KB
/
egcapilite.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
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
import dataclasses
import json
import os
import pathlib
@dataclasses.dataclass()
class Capabilities:
stream_command: bool = False
record: bool = False
screenshot: bool = False
flashback_record: bool = False
stream: bool = False
live_commentary: bool = False
JSON_PATH = pathlib.Path(os.getenv("APPDATA")) / "Elgato/GameCapture/EGCAPILite/EGCAPILite.json"
if not JSON_PATH.is_file():
raise FileNotFoundError(f"Couldn't find {JSON_PATH}.")
def _read_json():
"""
Reads EGCAPILite.json and returns its JSON data
:return: Parsed JSON data read from file
"""
with open(JSON_PATH) as file:
return json.load(file)
def _write_json(json_data):
"""
Writes JSON data to EGCAPILite.json
:param json_data: JSON data to write
"""
with open(JSON_PATH, "w") as file:
json.dump(json_data, file)
def capabilities() -> Capabilities:
"""
Returns the capabilities that Game Capture is able to use right now
:rtype: Capabilities
:return: Parsed capability flags
"""
json_data = _read_json()
flags = json_data["server"]["capabilityFlags"]
parsed_capabilities = Capabilities()
parsed_capabilities.stream_command = flags & 1 != 0
parsed_capabilities.record = flags & 2 != 0
parsed_capabilities.screenshot = flags & 4 != 0
parsed_capabilities.flashback_record = flags & 8 != 0
parsed_capabilities.stream = flags & 16 != 0
parsed_capabilities.live_commentary = flags & 32 != 0
return parsed_capabilities
def features() -> Capabilities:
"""
Returns the features that Game Capture supports
Game Capture sets all of these to 1/True when it starts, so there's not much point in reading these flags
:rtype: Capabilities
:return: Parsed feature flags
"""
json_data = _read_json()
flags = json_data["server"]["featureFlags"]
parsed_features = Capabilities()
parsed_features.stream_command = flags & 1 != 0
parsed_features.record = flags & 2 != 0
parsed_features.screenshot = flags & 4 != 0
parsed_features.flashback_record = flags & 8 != 0
parsed_features.stream = flags & 16 != 0
parsed_features.live_commentary = flags & 32 != 0
return parsed_features
def is_commentary_active() -> bool:
"""
Returns whether Live Commentary is currently enabled
:rtype: bool
:return: State of Live Commentary
"""
json_data = _read_json()
return json_data["server"]["isCommentaryActive"]
def is_recording() -> bool:
"""
Returns whether Game Capture is currently recording
:rtype: bool
:return: State of recording
"""
json_data = _read_json()
return json_data["server"]["isRecording"]
def is_running() -> bool:
"""
Returns whether Game Capture is currently running
:rtype: bool
:return: State of running
"""
json_data = _read_json()
return json_data["server"]["isRunning"]
def is_streaming() -> bool:
"""
Returns whether Game Capture is currently streaming
:rtype: bool
:return: State of streaming
"""
json_data = _read_json()
return json_data["server"]["isStreaming"]
def num_scenes() -> int:
"""
Returns the number of Stream Command scenes
Game Capture is hardcoded to have 10 scenes, so this will always be 10
:rtype: int
:return: Number of scenes
"""
json_data = _read_json()
return json_data["server"]["numScenes"]
def selected_scene_index() -> int:
"""
Returns the index of the currently selected Stream Command scene
:rtype: int
:return: Current scene index
"""
json_data = _read_json()
return json_data["server"]["selectedSceneIndex"]
def select_scene(scene_index: int):
"""
Switches to a different Stream Command scene
:param scene_index: Index of the scene to select
"""
json_data = _read_json()
json_data["client"]["selectScene"] = True
json_data["client"]["selectSceneIndex"] = scene_index
_write_json(json_data)
def start_recording():
"""
Starts recording
"""
json_data = _read_json()
json_data["client"]["startRecording"] = True
_write_json(json_data)
def stop_recording():
"""
Stops recording
"""
json_data = _read_json()
json_data["client"]["stopRecording"] = True
_write_json(json_data)
def toggle_recording():
"""
Toggles recording
"""
if is_recording():
stop_recording()
else:
start_recording()
def save_screenshot():
"""
Saves a screenshot
"""
json_data = _read_json()
json_data["client"]["saveScreenshot"] = True
_write_json(json_data)
def save_flashback_buffer(length_seconds: int):
"""
Saves a Flashback Recording of the given length in seconds
:param length_seconds: Length of recording
"""
json_data = _read_json()
json_data["client"]["saveFlashbackBuffer"] = True
json_data["client"]["saveFlashbackBufferSeconds"] = length_seconds
_write_json(json_data)
def start_streaming():
"""
Starts streaming
"""
json_data = _read_json()
json_data["client"]["startStreaming"] = True
_write_json(json_data)
def stop_streaming():
"""
Stops streaming
"""
json_data = _read_json()
json_data["client"]["stopStreaming"] = True
_write_json(json_data)
def toggle_streaming():
"""
Toggles streaming
"""
if is_streaming():
stop_streaming()
else:
start_streaming()
def activate_live_commentary():
"""
Activates Live Commentary
"""
json_data = _read_json()
json_data["client"]["activateCommentary"] = True
_write_json(json_data)
def deactivate_live_commentary():
"""
Deactivates Live Commentary
"""
json_data = _read_json()
json_data["client"]["deactivateCommentary"] = True
_write_json(json_data)
def toggle_live_commentary():
"""
Toggles Live Commentary
"""
if is_commentary_active():
deactivate_live_commentary()
else:
activate_live_commentary()