/
plugin.py
309 lines (257 loc) · 10.5 KB
/
plugin.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
305
306
307
308
309
import sys
import subprocess
import struct
import threading
from http.server import HTTPServer, BaseHTTPRequestHandler
from os import listdir, environ
from dataclasses import dataclass
from urllib.parse import parse_qs, urlparse
from galaxy.api.plugin import Plugin, create_and_run_plugin
from galaxy.api.types import Game, LicenseInfo, LicenseType, Authentication, LocalGame, NextStep, GameTime
from galaxy.api.consts import Platform, LocalGameState
# Manually override if you dare
roms_path = ""
emulator_path = ""
class AuthenticationHandler(BaseHTTPRequestHandler):
def _set_headers(self, content_type='text/html'):
self.send_response(200)
self.send_header('Content-type', content_type)
self.end_headers()
def do_GET(self):
if "setpath" in self.path:
self._set_headers()
parse_result = urlparse(self.path)
params = parse_qs(parse_result.query)
global roms_path, emulator_path
roms_path = params['path'][0]
emulator_path = params['emulator_path'][0]
self.wfile.write("<script>window.location=\"/end\";</script>".encode("utf8"))
return
self._set_headers()
self.wfile.write("""
<!DOCTYPE html>
<html>
<head>
<title>Cemu Integration</title>
<link href="https://fonts.googleapis.com/css?family=Lato:300&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.5/css/bulma.min.css" integrity="sha256-vK3UTo/8wHbaUn+dTQD0X6dzidqc5l7gczvH+Bnowwk=" crossorigin="anonymous" />
<style>
@charset "UTF-8";
html, body {
padding: 0;
margin: 0;
border: 0;
background: rgb(40, 39, 42) !important;
}
html {
font-size: 12px;
line-height: 1.5;
font-family: 'Lato', sans-serif;
}
html {
overflow: scroll;
overflow-x: hidden;
}
::-webkit-scrollbar {
width: 0px; /* Remove scrollbar space */
background: transparent; /* Optional: just make scrollbar invisible */
}
.header {
background: rgb(46, 45, 48);
height: 66px;
line-height: 66px;
font-weight: 600;
text-align: center;
vertical-align: middle;
padding: 0;
margin: 0;
border: 0;
font-size: 16px;
box-sizing: border-box;
border-bottom: 1px solid rgba(0, 0, 0, 0.08);
color: white !important;
}
.sub-container {
width: 90%;
min-width: 200px;
}
</style>
</head>
<body>
<div class="header">
Cemu Plugin Configuration
</div>
<br />
<div class="sub-container container">
<form method="GET" action="/setpath">
<div class="field">
<label class="label has-text-light">Games Location</label>
<div class="control">
<input class="input" name="path" type="text" class="has-text-light" placeholder="Enter absolute ROM path">
</div>
</div>
<div class="field">
<label class="label has-text-light">Cemu Location</label>
<div class="control">
<input class="input" name="emulator_path" type="text" class="has-text-light" placeholder="Enter absolute Cemu path">
</div>
</div>
<div class="field is-grouped">
<div class="control">
<input type="submit" class="button is-link" value="Enable Plugin" />
</div>
</div>
</form>
</div>
</body>
</html>
""".encode('utf8'))
class AuthenticationServer(threading.Thread):
def __init__(self, port = 0):
super().__init__()
self.path = ""
server_address = ('localhost', port)
self.httpd = HTTPServer(server_address, AuthenticationHandler)#partial(AuthenticationHandler, self))
self.port = self.httpd.server_port
def run(self):
self.httpd.serve_forever()
class CemuPlugin(Plugin):
def __init__(self, reader, writer, token):
super().__init__(
Platform.NintendoWiiU, # Choose platform from available list
"0.2", # Version
reader,
writer,
token
)
self.games = []
self.game_times = {}
self.server = AuthenticationServer()
self.server.start()
def parse_games(self):
self.games = get_games(roms_path)
self.game_times = get_game_times()
def shutdown(self):
self.server.httpd.shutdown()
async def launch_game(self, game_id):
from os.path import join,abspath
from os import listdir, chdir
# Find game - lookup table would be good :P
for game in self.games:
if game.program_id == game_id:
for f in listdir(game.path + "/code"):
if f.endswith(".rpx"):
game_path = abspath(join(game.path + "/code",f))
break
chdir(emulator_path)
subprocess.Popen(["./cemu.exe","-f", "-g", game_path])
break
return
def finish_login(self):
some_dict = dict()
some_dict["roms_path"] = roms_path
some_dict["emulator_path"] = emulator_path
self.store_credentials(some_dict)
self.parse_games()
return Authentication(user_id="a_high_quality_cemu_user", user_name=roms_path)
# implement methods
async def authenticate(self, stored_credentials=None):
global roms_path, emulator_path
# See if we have the path in the cache
if len(roms_path) == 0 and stored_credentials is not None and "roms_path" in stored_credentials:
roms_path = stored_credentials["roms_path"]
if len(emulator_path) == 0 and stored_credentials is not None and "emulator_path" in stored_credentials:
emulator_path = stored_credentials["emulator_path"]
if (len(roms_path) == 0) or (len(emulator_path) == 0):
PARAMS = {
"window_title": "Configure Cemu Plugin",
"window_width": 400,
"window_height": 300,
"start_uri": "http://localhost:" + str(self.server.port),
"end_uri_regex": ".*/end.*"
}
return NextStep("web_session", PARAMS)
return self.finish_login()
async def pass_login_credentials(self, step, credentials, cookies):
return self.finish_login()
async def get_owned_games(self):
owned_games = []
for game in self.games:
license_info = LicenseInfo(LicenseType.OtherUserLicense, None)
owned_games.append(Game(game_id=game.program_id, game_title=game.game_title, dlcs=None,
license_info=license_info))
return owned_games
async def get_local_games(self):
local_games = []
for game in self.games:
local_game = LocalGame(game.program_id, LocalGameState.Installed)
local_games.append(local_game)
return local_games
async def get_game_time(self, game_id, context = None):
if game_id in self.game_times:
game_time = self.game_times[game_id]
return GameTime(game_id, game_time[0], game_time[1])
@dataclass
class NUSGame():
program_id: str
game_title: str
path: str
def probe_game(path):
import logging
from xml.etree import ElementTree as ET
from os.path import exists
if exists(path + "/meta/meta.xml"):
root = ET.parse(path + "/meta/meta.xml").getroot()
else:
return None
if int(root.find("title_version").text) != 0: #filter out updates
return None
if root.find("product_code").text.startswith("WUP-M"): #filter out dlc
return None
# Check if English title is valid
title = root.find("longname_en").text
if len(title) == 0:
#logging.debug("No English title for" + path + "- using Japanese")
title = root.find("longname_ja").text
program_id = root.find("title_id").text
#logging.debug(path + "=" + title + "(" + program_id + ")")
return NUSGame(program_id=program_id, game_title=title, path=path)
def get_files_in_dir(path):
from os.path import isfile, join
from os import walk
games_path = []
for root, dirs, files in walk(path):
for folder in dirs:
games_path.append(join(root, folder))
return games_path
def get_games(path):
games_path = get_files_in_dir(path)
games = []
for game_path in games_path:
game = probe_game(game_path)
if game is not None:
games.append(game)
return games
def get_game_times():
import logging
from xml.etree import ElementTree as ET
from os.path import exists
game_times = {}
if exists(emulator_path + "./settings.xml"):
root = ET.parse(emulator_path + "./settings.xml").getroot()
else:
return
#logging.debug("Extracting play time for games...")
for game in root.find("GameCache"):
#logging.debug(str(game))
title_id = str(hex(int(game.find("title_id").text)).split('x')[-1]).rjust(16,'0').upper() #convert to hex, remove 0x, add padding
time_played = int(game.find("time_played").text)//60
last_time_played = int(game.find("last_played").text)
game_times[title_id] = [time_played, last_time_played]
#logging.debug("Title ID = {}, Time Played = {}, Last Time Played = {}".format(title_id, time_played, last_time_played))
return game_times
def main():
create_and_run_plugin(CemuPlugin, sys.argv)
# run plugin event loop
if __name__ == "__main__":
main()