forked from Elite-Kode/EDMC-Discord-Presence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.py
278 lines (241 loc) · 9.65 KB
/
load.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
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
#
# KodeBlox Copyright 2019 Sayak Mukhopadhyay
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http: //www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import functools
import logging
import threading
import tkinter as tk
from os.path import dirname, join
import semantic_version
import sys
import time
import l10n
import myNotebook as nb
from config import config, appname, appversion
import compat
from py_discord_sdk import discordsdk as dsdk
plugin_name = "DiscordPresence"
logger = logging.getLogger(f'{appname}.{plugin_name}')
_ = functools.partial(l10n.Translations.translate, context=__file__)
CLIENT_ID = 1169952579745230888
VERSION = '3.3.0'
# Add global var for Planet name (landing + around)
planet = '<Hidden>'
landingPad = '2'
cursystem = 'none'
shutdown = True
this = sys.modules[__name__] # For holding module globals
def callback(result):
logger.info(f'Callback: {result}')
if result == dsdk.Result.ok:
logger.info("Successfully set the activity!")
elif result == dsdk.Result.transaction_aborted:
logger.warning(f'Transaction aborted due to SDK shutting down: {result}')
else:
logger.error(f'Error in callback: {result}')
raise Exception(result)
def update_presence():
global shutdown
if config.get_int("disable_presence") == 0 and shutdown == False:
this.activity.state = this.presence_state
this.activity.details = this.presence_details
this.activity.timestamps.start = int(this.time_start)
this.activity.assets.large_image = this.largeimage
this.activity.assets.large_text = this.largetext
this.activity.assets.small_image = this.smallimage
this.activity.assets.small_text = this.smalltext
this.activity_manager.update_activity(this.activity, callback)
else:
this.activity_manager.clear_activity(callback)
def plugin_prefs(parent, cmdr, is_beta):
"""
Return a TK Frame for adding to the EDMC settings dialog.
"""
this.disablePresence = tk.IntVar(value=config.get_int("disable_presence"))
frame = nb.Frame(parent)
nb.Checkbutton(frame, text="Disable Presence", variable=this.disablePresence).grid()
nb.Label(frame, text='Version %s' % VERSION).grid(padx=10, pady=10, sticky=tk.W)
return frame
def prefs_changed(cmdr, is_beta):
"""
Save settings.
"""
config.set('disable_presence', this.disablePresence.get())
update_presence()
def plugin_start3(plugin_dir):
this.plugin_dir = plugin_dir
this.discord_thread = threading.Thread(target=check_run, args=(plugin_dir,))
this.discord_thread.setDaemon(True)
this.discord_thread.start()
return 'DiscordPresence'
def plugin_stop():
this.activity_manager.clear_activity(callback)
this.call_back_thread = None
def journal_entry(cmdr, is_beta, system, station, entry, state):
global planet
global landingPad
global cursystem
global shutdown
presence_state = this.presence_state
presence_details = this.presence_details
small_image = this.smallimage
small_text = this.smalltext
large_text = f'CMDR {cmdr}'
shutdown = False
landed = False
if system != cursystem:
this.time_start = time.time()
cursystem = system
if entry['event'] in ['LoadGame', 'Startup', 'StartUp']:
presence_state = _('In system {system}').format(system=system)
if station is None:
presence_details = _('Flying in normal space')
small_image = 'system'
small_text = system
else:
presence_details = _('Docked at {station}').format(station=station)
small_image = 'station'
small_text = station
if 'StartLanded' in entry:
landed = entry['StartLanded']
if 'Body' in entry and entry['Body'] != '' and landed == True:
planet = entry['Body']
presence_details = _('Landed on {body}').format(body=planet)
small_image = 'planet'
small_text = planet
this.time_start = time.time()
elif entry['event'] == 'Location':
presence_state = _('In system {system}').format(system=system)
if station is None:
presence_details = _('Flying in normal space')
small_image = 'system'
small_text = system
else:
presence_details = _('Docked at {station}').format(station=station)
small_image = 'station'
small_text = station
if 'Body' in entry and entry['Body'] != '' and landed == True:
planet = entry['Body']
presence_details = _('Landed on {body}').format(body=planet)
small_image = 'planet'
small_text = planet
this.time_start = time.time()
elif entry['event'] == 'StartJump':
presence_state = _('Jumping')
if entry['JumpType'] == 'Hyperspace':
presence_details = _('Jumping to system {system}').format(system=entry['StarSystem'])
elif entry['JumpType'] == 'Supercruise':
presence_details = _('Preparing for supercruise')
elif entry['event'] == 'SupercruiseEntry':
presence_state = _('In system {system}').format(system=system)
presence_details = _('Supercruising')
small_image = 'system'
small_text = system
elif entry['event'] == 'SupercruiseExit':
presence_state = _('In system {system}').format(system=system)
presence_details = _('Flying in normal space')
elif entry['event'] == 'FSDJump':
presence_state = _('In system {system}').format(system=system)
presence_details = _('Supercruising')
small_image = 'system'
small_text = system
elif entry['event'] == 'Docked':
presence_state = _('In system {system}').format(system=system)
presence_details = _('Docked at {station}').format(station=station)
small_image = 'station'
this.time_start = time.time()
elif entry['event'] == 'Undocked':
presence_state = _('In system {system}').format(system=system)
presence_details = _('Flying in normal space')
small_image = 'system'
this.time_start = time.time()
elif entry['event'] in ['ShutDown', 'Shutdown']:
presence_state = _('Connecting CMDR Interface')
presence_details = ''
small_image = ''
small_text = ''
shutdown = True
elif entry['event'] == 'DockingGranted':
landingPad = entry['LandingPad']
elif entry['event'] == 'Music':
if entry['MusicTrack'] == 'MainMenu':
presence_state = _('Connecting CMDR Interface')
presence_details = ''
# Todo: This elif might not be executed on undocked. Functionality can be improved
elif entry['event'] in ['Undocked', 'DockingCancelled', 'DockingTimeout']:
presence_details = _('Flying near {station}').format(station=entry['StationName'])
# Planetary events
elif entry['event'] == 'ApproachBody':
planet = entry['Body']
presence_details = _('Approaching {body}').format(body=planet)
small_image = 'planet'
small_text = planet
elif entry['event'] == 'Touchdown' and entry['PlayerControlled']:
presence_details = _('Landed on {body}').format(body=planet)
elif entry['event'] == 'Liftoff':
if entry['PlayerControlled']:
presence_details = _('Flying around {body}').format(body=planet)
else:
presence_details = _('In SRV on {body}, ship in orbit').format(body=planet)
elif entry['event'] == 'LeaveBody':
presence_details = _('Supercruising')
small_image = 'system'
small_text = system
# EXTERNAL VEHICLE EVENTS
elif entry['event'] == 'LaunchSRV':
presence_details = _('In SRV on {body}').format(body=planet)
elif entry['event'] == 'DockSRV':
presence_details = _('Landed on {body}').format(body=planet)
if presence_state != this.presence_state or presence_details != this.presence_details:
this.presence_state = presence_state
this.presence_details = presence_details
this.largetext = large_text
this.smallimage = small_image
this.smalltext = small_text
update_presence()
elif shutdown == True:
this.activity_manager.clear_activity(callback)
def check_run(plugin_dir):
plugin_path = join(dirname(plugin_dir), plugin_name)
retry = True
while retry:
time.sleep(1 / 10)
try:
this.app = dsdk.Discord(CLIENT_ID, dsdk.CreateFlags.no_require_discord, plugin_path)
retry = False
except Exception:
pass
this.activity_manager = this.app.get_activity_manager()
this.activity = dsdk.Activity()
this.activity_manager.register_steam(359320)
this.call_back_thread = threading.Thread(target=run_callbacks)
this.call_back_thread.setDaemon(True)
this.call_back_thread.start()
this.presence_state = _('Connecting CMDR Interface')
this.presence_details = ''
this.time_start = time.time()
this.largeimage = 'elite'
this.largetext = ''
this.smallimage = ''
this.smalltext = ''
this.disablePresence = None
update_presence()
def run_callbacks():
try:
while True:
time.sleep(1 / 10)
this.app.run_callbacks()
except Exception:
check_run(this.plugin_dir)