forked from veldenb/plugin.program.moonlight-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.py
304 lines (241 loc) · 9.42 KB
/
addon.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
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
import configparser
import moonlight
import sys
import urllib.parse
import xbmc
import xbmcaddon
import xbmcgui
import xbmcplugin
from xbmcvfs import translatePath
def build_url(query):
return base_url + '?' + urllib.parse.urlencode(query)
def get_addon_path(sub_path):
return translatePath(addon.getAddonInfo('path') + sub_path)
def get_addon_data_path(sub_path=''):
return translatePath('special://profile/addon_data/plugin.program.moonlight-qt' + sub_path)
def get_resource_path(sub_path):
return get_addon_path('/resources' + sub_path)
def get_icon_path():
return get_resource_path('/icon.png')
def get_poster_path():
return get_resource_path('/poster.jpg')
def get_fanart_path():
return get_resource_path('/fanart.jpg')
def get_boxart_path(host, app):
sampleBoxart = '/moonlight-home/.cache/Moonlight Game Streaming Project/Moonlight/boxart/{}/{}.png'.format(
host.get('uuid'), app.get('id')
)
return get_addon_data_path(sampleBoxart)
def get_moonlight_config():
# Load config
moonlight_full_config_path = get_addon_data_path(
'/moonlight-home/.config/Moonlight Game Streaming Project/Moonlight.conf'
)
return parse_moonlight_config(moonlight_full_config_path)
def parse_moonlight_config(moonlight_config_file):
config = configparser.ConfigParser()
config.read(moonlight_config_file)
parsedOptions = {}
for section in config.sections():
parsedOptions[section] = {}
for option in config.options(section):
options = option.split('\\')
optionValue = config.get(section, option)
# It would be nice if the code below could be simplified, essentially you want to convert and merge
# \\a\\b\\c\\d1\\val1 and \\a\\b\\c\\d2\\val2 to { a: { b: { c: { d1: val1, d2: val2 }}}}
if len(options) == 4:
o1, o2, o3, o4 = options
if o1 not in parsedOptions[section]:
parsedOptions[section][o1] = {}
if o2 not in parsedOptions[section][o1]:
parsedOptions[section][o1][o2] = {}
if o3 not in parsedOptions[section][o1][o2]:
parsedOptions[section][o1][o2][o3] = {}
parsedOptions[section][o1][o2][o3][o4] = optionValue
elif len(options) == 3:
o1, o2, o3 = options
if o1 not in parsedOptions[section]:
parsedOptions[section][o1] = {}
if o2 not in parsedOptions[section][o1]:
parsedOptions[section][o1][o2] = {}
parsedOptions[section][o1][o2][o3] = optionValue
elif len(options) == 2:
o1, o2 = options
if o1 not in parsedOptions[section]:
parsedOptions[section][o1] = {}
parsedOptions[section][o1][o2] = optionValue
elif len(options) == 1:
o1 = options[0]
parsedOptions[section][o1] = optionValue
return parsedOptions
def show_hosts(handle, hosts):
# Show hosts
for tag_id in hosts:
if 'apps' in hosts[tag_id]:
host = hosts[tag_id]
url = build_url({'mode': 'host', 'host_id': tag_id})
li = xbmcgui.ListItem(host.get('hostname'))
xbmcplugin.addDirectoryItem(handle, url, li, isFolder=True)
def show_moonlight(handle):
url = build_url({'mode': 'launch'})
li = xbmcgui.ListItem(addon.getLocalizedString(30001))
icon = get_icon_path()
li.setArt({
'thumb': icon,
'poster': get_poster_path(),
'fanart': get_fanart_path(),
'icon': icon
})
xbmcplugin.addDirectoryItem(handle, url, li)
def show_games(handle, hosts, host_id):
# Show games for host
host = hosts[host_id]
if 'apps' in host:
for app_id, app in host['apps'].items():
if 'name' in app:
url = build_url({'mode': 'launch', 'host_id': host_id, 'game_id': app_id})
boxart = get_boxart_path(host, app)
li = xbmcgui.ListItem()
li.setLabel(app.get('name'))
li.setArt({
'thumb': boxart,
'poster': boxart
})
xbmcplugin.addDirectoryItem(handle, url, li)
def show_gui(handle, mode, host_id, game_id):
# Get hosts from moonlight config
hosts = get_moonlight_config().get('hosts')
if mode is None:
if hosts:
# If there is only one host and a size-item then show the games immediately
if len(hosts) == 2:
host_id = list(hosts.keys())[0]
show_games(handle, hosts, host_id)
else:
show_hosts(handle, hosts)
# Always show Moonlight
show_moonlight(handle)
# Finish list
xbmcplugin.endOfDirectory(handle)
elif mode == 'host' and host_id in hosts:
# Show games
show_games(handle, hosts, host_id)
# Always show Moonlight
show_moonlight(handle)
# Finish list
xbmcplugin.endOfDirectory(handle)
elif mode == 'launch':
# Launch game on host
if host_id is not None and game_id is not None:
host_name = hosts[host_id]['localaddress']
game_name = hosts[host_id]['apps'][game_id]['name']
# Launch game
moonlight.launch(addon, host_name, game_name)
else:
# Launch Moonlight
moonlight.launch(addon)
elif mode == 'update':
# Update Moonlight
moonlight.update(addon)
else:
xbmc.log('Unknown GUI mode: {}'.format(mode), xbmc.LOGDEBUG)
# Arguments
base_url = sys.argv[0]
addon_handle = int(sys.argv[1])
args = urllib.parse.parse_qs(sys.argv[2][1:])
# Configure addon
addon = xbmcaddon.Addon()
if addon_handle != -1:
xbmcplugin.setContent(addon_handle, 'games')
# Debug stuff
# See https://kodi.wiki/view/Audio-video_add-on_tutorial
# xbmc.log(sys.argv.__str__(), xbmc.LOGDEBUG)
# xbmc.log(args.__str__(), xbmc.LOGDEBUG)
# xbmc.log('CONFIG: ' + moonlight_config.__str__(), level=xbmc.LOGDEBUG)
page_mode = args.get('mode', None)
if page_mode is not None:
page_mode = page_mode[0]
page_host_id = args.get('host_id', None)
if page_host_id is not None:
page_host_id = page_host_id[0]
page_game_id = args.get('game_id', None)
if page_game_id is not None:
page_game_id = page_game_id[0]
# Show GUI
show_gui(addon_handle, page_mode, page_host_id, page_game_id)
# Parsed config example
# example_moonlight_config = {
# 'General': {
# 'abstouchmode': 'true',
# 'audiocfg': '1',
# 'backgroundgamepad': 'false',
# 'bitrate': '20000',
# 'capturesyskeys': '0',
# 'certificate': '"@ByteArray(-----BEGIN CERTIFICATE-----\\nABCDEFFF\\nABCDEF=\\n-----END CERTIFICATE-----\\n)"',
# 'connwarnings': 'true',
# 'defaultver': '1',
# 'detectnetblocking': 'false',
# 'fps': '60',
# 'framepacing': 'false',
# 'gameopts': 'false',
# 'gamepadmouse': 'true',
# 'height': '1080',
# 'hostaudio': 'true',
# 'key': '@ByteArray(-----BEGIN PRIVATE KEY-----\\nABCDEFFFFF\\ABCDEF\\n-----END PRIVATE KEY-----\\n)',
# 'language': '0',
# 'latestsupportedversion-v1': '99.99.99.99',
# 'mdns': 'false',
# 'mouseacceleration': 'false',
# 'multicontroller': 'true',
# 'muteonfocusloss': 'false',
# 'packetsize': '0',
# 'quitappafter': 'true',
# 'reversescroll': 'false',
# 'richpresence': 'true',
# 'swapfacebuttons': 'false',
# 'swapmousebuttons': 'false',
# 'uidisplaymode': '0',
# 'unsupportedfps': 'false',
# 'videocfg': '0',
# 'videodec': '0',
# 'vsync': 'false',
# 'width': '1920',
# 'windowmode': '0'
# },
# 'gcmapping': {
# '1': {'guid': '00001000010000100001000010000100',
# 'mapping': '"dev:xb1:Some joypad,platform:Linux,b:b1,y:b0,x:b3,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b6,rightshoulder:b7,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a2,lefttrigger:b4,righttrigger:b5,a:b2,"'},
# 'size': '1'},
# 'hosts': {
# '1': {
# 'apps': {
# '1': {
# 'appcollector': 'false',
# 'directlaunch': 'false',
# 'hdr': 'false',
# 'hidden': 'false',
# 'id': '1234567890',
# 'name': 'Some game 1'
# },
# '2': {
# 'appcollector': 'false',
# 'directlaunch': 'false',
# 'hdr': 'true',
# 'hidden': 'false',
# 'id': '0123456789',
# 'name': 'Some game 2'
# }, 'size': '2'
# },
# 'customname': 'false',
# 'hostname': 'MYHOSTNAME',
# 'ipv6address': '1000:0000:11:2:33:4444:5555:6666',
# 'localaddress': '192.168.0.2',
# 'mac': '@ByteArray(AA\\0\\aa0A])',
# 'manualaddress': '192.168.1.141',
# 'remoteaddress': '123.123.123.123',
# 'srvcert': '"@ByteArray(-----BEGIN CERTIFICATE-----\\nABCDEFFFFFF=\\n-----END CERTIFICATE-----\\n)"',
# 'uuid': 'aaaaaaaa-1234-aaaa-1234-aaaaaaaaaaaa'
# },
# 'size': '1'
# }
# }