-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
load.py
213 lines (177 loc) · 7.45 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
#
# 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 = 386149818227097610
VERSION = '3.2.0'
# Add global var for Planet name (landing + around)
planet = '<Hidden>'
landingPad = '2'
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():
if config.get_int("disable_presence") == 0:
this.activity.state = this.presence_state
this.activity.details = this.presence_details
this.activity.timestamps.start = int(this.time_start)
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
presence_state = this.presence_state
presence_details = this.presence_details
if entry['event'] == 'StartUp':
presence_state = _('In system {system}').format(system=system)
if station is None:
presence_details = _('Flying in normal space')
else:
presence_details = _('Docked at {station}').format(station=station)
elif entry['event'] == 'Location':
presence_state = _('In system {system}').format(system=system)
if station is None:
presence_details = _('Flying in normal space')
else:
presence_details = _('Docked at {station}').format(station=station)
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')
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')
elif entry['event'] == 'Docked':
presence_state = _('In system {system}').format(system=system)
presence_details = _('Docked at {station}').format(station=station)
elif entry['event'] == 'Undocked':
presence_state = _('In system {system}').format(system=system)
presence_details = _('Flying in normal space')
elif entry['event'] == 'ShutDown':
presence_state = _('Connecting CMDR Interface')
presence_details = ''
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'] == 'Undocked' or entry['event'] == 'DockingCancelled' or entry['event'] == 'DockingTimeout':
presence_details = _('Flying near {station}').format(station=entry['StationName'])
# Planetary events
elif entry['event'] == 'ApproachBody':
presence_details = _('Approaching {body}').format(body=entry['Body'])
planet = entry['Body']
elif entry['event'] == 'Touchdown' and entry['PlayerControlled']:
presence_details = _('Landed on {body}').format(body=planet)
elif entry['event'] == 'Liftoff' and entry['PlayerControlled']:
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')
# 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
update_presence()
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.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.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)