-
Notifications
You must be signed in to change notification settings - Fork 0
/
taiko.py
164 lines (129 loc) · 6.13 KB
/
taiko.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import configparser
import json
import sys
import os
import time
from binascii import hexlify
if sys.version_info < (3, 0):
sys.exit('This program only runs on Python 3.')
try:
import tcpgecko
except ModuleNotFoundError:
sys.exit('tcpgecko.py not found! Download it (and common.py) from https://github.com/wiiudev/pyGecko\n'
'Make sure to also convert tcpgecko.py to Python 3 code - use the 2to3 tool for this')
try:
import pypresence
except ModuleNotFoundError:
sys.exit('pypresence not found! Install it using pip, or from https://github.com/qwertyquerty/pypresence')
parser = argparse.ArgumentParser()
parser.add_argument('server', help='console IP address', nargs='?')
parser.add_argument('-c', '--client-id', help='Discord client ID')
parser.add_argument('-l', '--launch-auto', help='launch title automatically if not running', nargs='?', default=argparse.SUPPRESS)
parser.add_argument('-j', '--jump', help='allow title jumping with --launch-auto', action='store_true')
parser.add_argument('-n', '--nihongo', help='use Japanese strings for RPC', action='store_true')
args = parser.parse_args()
config = configparser.ConfigParser()
config.read('config.ini')
def get_current_title(gecko):
ver = gecko.getversion()
if ver == 550:
loc = 0x10013C10
elif ver < 550 and ver >= 532:
loc = 0x100136D0
elif ver < 532 and ver >= 500:
loc = 0x10013010
elif ver == 410:
loc = 0x1000ECB0
else:
sys.exit('Your Wii U firmware version is not supported. Please update.')
return int(hexlify(gecko.readmem(loc, 8)), 16)
def is_title_installed(gecko, title):
return gecko.get_symbol('sysapp.rpl', 'SYSCheckTitleExists', True)(title >> 32, title & 0xFFFFFFFF)
def launch_title(gecko, title):
gecko.get_symbol('sysapp.rpl', 'SYSLaunchTitle', True)(title >> 32, title & 0xFFFFFFFF)
# tcpgecko restarts when title changes, so we need to reconnect
time.sleep(30) # safe enough?
print('Title changed; reconnecting...')
gecko = tcpgecko.TCPGecko(server)
return gecko
if __name__ == '__main__':
titles = os.listdir('data')
server = args.server or config.get('main', 'ip', fallback=None)
if not server:
sys.exit('No IP address supplied')
try:
gecko = tcpgecko.TCPGecko(server)
except TimeoutError:
sys.exit('Unable to connect to tcpGecko - are you sure it\'s running on your console?')
current = get_current_title(gecko)
cur = None
all_ids = []
for title in titles:
vals = json.loads(open('data/%s/values.json' % title, encoding='utf8').read())
all_ids.append((title, vals['title_id'], vals['title']))
if vals['title_id'] == current:
songlist = json.loads(open('data/%s/song_data.json' % title).read())
cur = vals
if args.jump or not cur:
if hasattr(args, 'launch_auto') is not False:
launchable = []
for tid in all_ids:
if is_title_installed(gecko, tid[1]):
if args.launch_auto is None or args.launch_auto == tid[0]:
launchable.append(tid)
if len(launchable) > 1:
print('There is more than one Taiko no Tatsujin title installed on your Wii U. '
'Supply the desired title to launch it, eg. --launch-auto wiiu3\n\n'
'Installed titles:')
for title in launchable:
print('%s = %s' % (title[0], title[2]))
sys.exit()
elif not launchable:
sys.exit('There are no Taiko no Tatsujin titles installed on your Wii U.')
elif launchable[0][1] != current:
gecko = launch_title(gecko, launchable[0][1])
cur = json.loads(open('data/%s/values.json' % launchable[0][0], encoding='utf8').read())
songlist = json.loads(open('data/%s/song_data.json' % launchable[0][0]).read())
else:
sys.exit('There is no Taiko no Tatsujin title running on your Wii U. Launch one first, or use --launch-auto')
print('Connecting to Discord RPC...')
rpc = pypresence.client(args.client_id or str(cur['default_client_id']))
rpc.start()
use_ja = args.nihongo or config.get('main', 'lang', fallback=None) == 'ja'
modes = cur['modes_ja'] if use_ja else cur['modes']
song_modes = cur['song_modes_ja'] if use_ja else cur['song_modes']
levels = cur['levels_ja'] if use_ja else cur['levels']
last_event = None
while True:
event = str(int(hexlify(gecko.readmem(cur['pointers']['mode'], 4)), 16))
if event != last_event:
last_event = event
mode = modes[event] if event in modes else None
song_mode = None
mode_id = str(int(hexlify(gecko.readmem(cur['pointers']['song_mode'], 4)), 16))
if mode_id in song_modes:
song_mode = song_modes[mode_id]
if event == '11':
for idx in range(len(cur['pointers']['song'])):
course = int(hexlify(gecko.readmem(cur['pointers']['song'][idx], 4)), 16)
difficulty = str(int(hexlify(gecko.readmem(cur['pointers']['difficulty'][idx], 4)), 16))
try:
level = levels[difficulty]
break
except KeyError:
continue
song_title = '???'
if str(course) in songlist:
song = songlist[str(course)]
song_title = song['title']
song_stars = song['stars'][str(difficulty)]
level = '%s (%s\u2605)' % (level, song_stars)
rpc.set_activity(state=song_mode, details=song_title, large_image='taiko',
small_image='level_%s' % difficulty, small_text=level)
elif event == '12':
rpc.set_activity(state=song_mode, details=mode[0], large_image='taiko')
elif mode:
rpc.set_activity(state=mode[0], details=mode[1] if len(mode) > 1 else None, large_image='taiko')