/
plugin.py
272 lines (184 loc) · 8.24 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
import asyncio
import json
import subprocess
import sys
import os
import time
import trophy
from trophy import Trophy
from config import Config
from backend import BackendClient
from version import get_version
from galaxy.api.consts import LicenseType, LocalGameState, Platform
from galaxy.api.plugin import Plugin, create_and_run_plugin
from galaxy.api.types import Authentication, Game, GameTime, LicenseInfo, LocalGame, Achievement
class RPCS3Plugin(Plugin):
def __init__(self, reader, writer, token):
super().__init__(Platform.ColecoVision, get_version(), reader, writer, token)
try:
self.config = Config() # If we can't create a good config, we can't run the plugin.
except FileNotFoundError:
self.close()
else:
self.backend_client = BackendClient(self.config)
self.games = []
self.local_games_cache = self.local_games_list()
self.process = None
self.running_game_id = None
async def authenticate(self, stored_credentials=None):
return self.do_auth()
async def pass_login_credentials(self, step, credentials, cookies):
return self.do_auth()
def do_auth(self):
username = ''
with open(self.config.localusername, 'r') as username_file:
username = username_file.read()
user_data = {}
user_data['username'] = username
self.store_credentials(user_data)
return Authentication('rpcs3_user', user_data['username'])
async def launch_game(self, game_id):
args = []
eboot_bin = self.config.joinpath(
self.backend_client.get_game_path(game_id),
'USRDIR',
'EBOOT.BIN')
if self.config.no_gui:
args.append('--no-gui')
command = [self.config.rpcs3_exe, eboot_bin] + args
self.process = subprocess.Popen(command)
self.backend_client.start_game_time()
self.running_game_id = game_id
return
# Only as placeholders so the feature is recognized
async def install_game(self, game_id):
pass
async def uninstall_game(self, game_id):
pass
async def prepare_game_times_context(self, game_ids):
return self.get_game_times(game_ids)
async def prepare_achievements_context(self, game_ids):
return self.get_trophy_achs()
async def get_game_time(self, game_id, context):
game_time = context.get(game_id)
return game_time
async def get_unlocked_achievements(self, game_id, context):
achs = context.get(game_id)
return achs
def get_game_times(self, game_ids):
# Get the path of the game times file.
base_path = os.path.dirname(os.path.realpath(__file__))
game_times_path = os.path.join(base_path, 'game_times.json')
game_times = {}
# If the file does not exist, create it with default values.
if not os.path.exists(game_times_path):
for game in self.games:
game_id = str(game[0])
game_times[game_id] = GameTime(game_id, 0, None)
with open(game_times_path, 'w', encoding='utf-8') as game_times_file:
json.dump(game_times, game_times_file, indent=4)
# If (when) the file exists, read it and return the game times.
with open(game_times_path, 'r', encoding='utf-8') as game_times_file:
game_times_json = json.load(game_times_file)
for game_id in game_times_json:
if game_id in game_ids:
time_played = game_times_json.get(game_id).get('time_played')
last_time_played = game_times_json.get(game_id).get('last_time_played')
game_times[game_id] = GameTime(game_id, time_played, last_time_played)
return game_times
def get_trophy_achs(self):
game_ids = []
for game in self.games:
game_ids.append(game[0])
trophies = None
all_achs = {}
for game_id in game_ids:
game_path = self.backend_client.get_game_path(game_id)
try:
trophies = Trophy(self.config, game_path)
keys = trophies.tropusr.table6.keys()
game_achs = []
for key in keys:
ach = trophies.trop2ach(key)
if ach is not None:
game_achs.append(trophies.trop2ach(key))
all_achs[game_id] = game_achs
# If tropusr doesn't exist, this game has no trophies.
except AttributeError:
all_achs[game_id] = []
return all_achs
def tick(self):
try:
if self.process.poll() is not None:
self.backend_client.end_game_time()
self.update_json_game_time(
self.running_game_id,
self.backend_client.get_session_duration(),
int(time.time()))
# Only update recently played games. Updating all game times every second fills up log way too quickly.
self.create_task(self.update_galaxy_game_times(self.running_game_id), 'Update Galaxy game times')
self.process = None
self.running_game_id = None
except AttributeError:
pass
self.create_task(self.update_local_games(), 'Update local games')
self.create_task(self.update_achievements(), 'Update achievements')
async def update_local_games(self):
loop = asyncio.get_running_loop()
new_list = await loop.run_in_executor(None, self.local_games_list)
notify_list = self.backend_client.get_state_changes(self.local_games_cache, new_list)
self.local_games_cache = new_list
for local_game_notify in notify_list:
self.update_local_game_status(local_game_notify)
async def update_galaxy_game_times(self, game_id):
# Leave time for Galaxy to fetch games before updating times
await asyncio.sleep(60)
loop = asyncio.get_running_loop()
game_times = await loop.run_in_executor(None, self.get_game_times, [game_id])
for game_id in game_times:
self.update_game_time(game_times[game_id])
async def update_achievements(self):
# Leave time for Galaxy to fetch games before updating times
await asyncio.sleep(60)
loop = asyncio.get_running_loop()
achs = await loop.run_in_executor(None, self.get_trophy_achs)
# for ach in achs:
# self.unlock_achievement(ach) # TODO - how/when to handle this?
def update_json_game_time(self, game_id, duration, last_time_played):
# Get the path of the game times file.
base_path = os.path.dirname(os.path.realpath(__file__))
game_times_path = '{}/game_times.json'.format(base_path)
game_times_json = None
with open(game_times_path, 'r', encoding='utf-8') as game_times_file:
game_times_json = json.load(game_times_file)
old_time_played = game_times_json.get(game_id).get('time_played')
new_time_played = old_time_played + duration
game_times_json[game_id]['time_played'] = new_time_played
game_times_json[game_id]['last_time_played'] = last_time_played
with open(game_times_path, 'w', encoding='utf-8') as game_times_file:
json.dump(game_times_json, game_times_file, indent=4)
self.update_game_time(GameTime(game_id, new_time_played, last_time_played))
def local_games_list(self):
local_games = []
for game in self.games:
local_games.append(LocalGame(
game[0],
LocalGameState.Installed))
return local_games
async def get_owned_games(self):
self.games = self.backend_client.get_games()
owned_games = []
for game in self.games:
owned_games.append(Game(
game[0],
game[1],
None,
LicenseInfo(LicenseType.SinglePurchase, None)))
return owned_games
async def get_local_games(self):
return self.local_games_cache
def main():
create_and_run_plugin(RPCS3Plugin, sys.argv)
# run plugin event loop
if __name__ == '__main__':
main()