-
Notifications
You must be signed in to change notification settings - Fork 3
/
RomM.cs
422 lines (353 loc) · 17.1 KB
/
RomM.cs
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Web;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Linq;
using Newtonsoft.Json;
using RomM.Settings;
using Playnite.SDK.Events;
using RomM.Games;
using RomM.Models.RomM.Platform;
using RomM.Models.RomM.Rom;
namespace RomM
{
public static class HttpClientSingleton
{
private static readonly HttpClient httpClient = new HttpClient();
static HttpClientSingleton()
{
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public static void ConfigureBasicAuth(string username, string password)
{
var base64Credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{username}:{password}"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Credentials);
}
public static HttpClient Instance
{
get { return httpClient; }
}
}
public static class JsonSerializerSingleton
{
public static JsonSerializer Instance { get; } = new JsonSerializer();
}
public class RomM : LibraryPlugin, IRomM
{
private const string s_pluginName = "RomM";
internal static readonly string Icon = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"icon.png");
internal static readonly Guid PluginId = Guid.Parse("9700aa21-447d-41b4-a989-acd38f407d9f");
internal static readonly MetadataNameProperty SourceName = new MetadataNameProperty(s_pluginName);
public override Guid Id { get; } = PluginId;
public override string Name { get; } = s_pluginName;
public override string LibraryIcon { get; } = Icon;
public ILogger Logger => LogManager.GetLogger();
public IPlayniteAPI Playnite { get; private set; }
public SettingsViewModel Settings { get; private set; }
// Implementing Client adds ability to open it via special menu in playnite.
public override LibraryClient Client { get; } = new RomMClient();
public RomM(IPlayniteAPI api) : base(api)
{
Playnite = api;
Properties = new LibraryPluginProperties
{
HasSettings = true
};
}
internal IList<RomMPlatform> FetchPlatforms()
{
string apiPlatformsUrl = $"{Settings.RomMHost}/api/platforms";
try
{
// Make the request and get the response
HttpResponseMessage response = HttpClientSingleton.Instance.GetAsync(apiPlatformsUrl).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
// Assuming the response is in JSON format
string body = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
return JsonConvert.DeserializeObject<List<RomMPlatform>>(body);
}
catch (HttpRequestException e)
{
Logger.Error($"Request exception: {e.Message}");
return new List<RomMPlatform>();
}
}
internal RomMRom FetchRom(string romId)
{
string romUrl = $"{Settings.RomMHost}/api/roms/{romId}";
try
{
// Fetch the rom info from RomM
HttpResponseMessage response = HttpClientSingleton.Instance.GetAsync(romUrl).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
// Assuming the response is in JSON format
string body = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
return JsonConvert.DeserializeObject<RomMRom>(body);
}
catch (HttpRequestException e)
{
Logger.Error($"Request exception: {e.Message}");
return null;
}
}
// Playnite url is in the format playnite://romm/<action>/<platform_igdb_id>/<rom_id>
internal void HandleRommUri(PlayniteUriEventArgs args)
{
var action = args.Arguments[0];
var platformIgdbId = args.Arguments[1];
var romId = args.Arguments[2];
Logger.Debug($"Received Playnite URI: {action}/{platformIgdbId}/{romId}");
string romUrl = $"{Settings.RomMHost}/api/roms/{romId}";
RomMRom rom = FetchRom(romId);
if (rom == null)
{
Logger.Warn($"Game {romId} not found in RomM.");
return;
}
foreach (var mapping in SettingsViewModel.Instance.Mappings?.Where(m => m.Enabled))
{
if (mapping.Platform.IgdbId.ToString() == platformIgdbId)
{
var gameName = rom.Name;
var game = Playnite.Database.Games.FirstOrDefault(g => g.Source.Name == SourceName.ToString() &&
g.Platforms.Any(p => p.Name == mapping.Platform.Name) &&
g.Name == gameName);
if (game == null)
{
Logger.Warn($"Game {gameName} not found in Playnite database.");
}
PlayniteApi.MainView.SwitchToLibraryView();
PlayniteApi.MainView.SelectGame(game.Id);
switch (action)
{
case "view":
// We always open the game in the webview
return;
case "play":
PlayniteApi.StartGame(game.Id);
break;
}
}
}
}
public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
{
base.OnApplicationStarted(args);
Settings = new SettingsViewModel(this, this);
HttpClientSingleton.ConfigureBasicAuth(Settings.RomMUsername, Settings.RomMPassword);
Playnite.UriHandler.RegisterSource("romm", HandleRommUri);
}
public static async Task<HttpResponseMessage> GetAsync(string baseUrl, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
{
return await HttpClientSingleton.Instance.GetAsync(baseUrl, completionOption);
}
public static async Task<HttpResponseMessage> GetAsyncWithParams(string baseUrl, NameValueCollection queryParams)
{
var uriBuilder = new UriBuilder(baseUrl);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
foreach (string key in queryParams)
{
query[key] = queryParams[key];
}
uriBuilder.Query = query.ToString();
return await HttpClientSingleton.Instance.GetAsync(uriBuilder.Uri);
}
public override IEnumerable<GameMetadata> GetGames(LibraryGetGamesArgs args)
{
if (Playnite.ApplicationInfo.Mode == ApplicationMode.Fullscreen && !Settings.ScanGamesInFullScreen)
{
return new List<GameMetadata>();
}
// Return early if host, username or password is not set
if (string.IsNullOrEmpty(Settings.RomMHost) || string.IsNullOrEmpty(Settings.RomMUsername) || string.IsNullOrEmpty(Settings.RomMPassword))
{
Logger.Warn("RomM host, username or password is not set.");
return new List<GameMetadata>();
}
IList<RomMPlatform> apiPlatforms = FetchPlatforms();
List<GameMetadata> games = new List<GameMetadata>();
IEnumerable<EmulatorMapping> enabledMappings = SettingsViewModel.Instance.Mappings?.Where(m => m.Enabled);
if (enabledMappings == null)
{
Logger.Warn("No enabled mappings found.");
return games;
}
foreach (var mapping in enabledMappings)
{
if (args.CancelToken.IsCancellationRequested)
break;
if (mapping.Emulator == null)
{
Logger.Warn($"Emulator {mapping.EmulatorId} not found, skipping.");
continue;
}
if (mapping.EmulatorProfile == null)
{
Logger.Warn($"Emulator profile {mapping.EmulatorProfileId} for emulator {mapping.EmulatorId} not found, skipping.");
continue;
}
if (mapping.Platform == null)
{
Logger.Warn($"Platform {mapping.PlatformId} not found, skipping.");
continue;
}
string url = $"{Settings.RomMHost}/api/roms";
RomMPlatform apiPlatform = apiPlatforms.FirstOrDefault(p => p.IgdbId == mapping.Platform.IgdbId);
if (apiPlatform == null)
{
Logger.Warn($"Platform {mapping.Platform.Name} with IGDB ID {mapping.Platform.IgdbId} not found in RomM, skipping.");
continue;
}
Logger.Debug($"Starting to fetch games for {apiPlatform.Name}.");
NameValueCollection queryParams = new NameValueCollection
{
{ "limit", "10000" },
{ "platform_id", apiPlatform.Id.ToString() }
};
var responseGameIDs = new HashSet<string>();
try
{
// Make the request and get the response
HttpResponseMessage response = GetAsyncWithParams(url, queryParams).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
Logger.Debug($"Starting to parse response for {apiPlatform.Name}.");
// Assuming the response is in JSON format
Stream body = response.Content.ReadAsStreamAsync().GetAwaiter().GetResult();
List<RomMRom> roms;
using (StreamReader reader = new StreamReader(body))
using (JsonTextReader jsonReader = new JsonTextReader(reader))
{
roms = JsonSerializerSingleton.Instance.Deserialize<List<RomMRom>>(jsonReader);
}
Logger.Debug($"Finished parsing response for {apiPlatform.Name}.");
var rootInstallDir = PlayniteApi.Paths.IsPortable
? mapping.DestinationPathResolved.Replace(PlayniteApi.Paths.ApplicationPath, ExpandableVariables.PlayniteDirectory)
: mapping.DestinationPathResolved;
// Return a GameMetadata for each item in the response
foreach (var item in roms)
{
var gameName = item.Name;
var fileName = item.FileName;
var urlCover = item.UrlCover;
var gameInstallDir = Path.Combine(rootInstallDir, Path.GetFileNameWithoutExtension(fileName));
var pathToGame = Path.Combine(gameInstallDir, fileName);
var info = new RomMGameInfo
{
MappingId = mapping.MappingId,
FileName = fileName,
DownloadUrl = $"{Settings.RomMHost}/api/roms/{item.Id}/content/{fileName}",
IsMulti = item.Multi
};
var gameId = info.AsGameId();
responseGameIDs.Add(gameId);
// Check if the game is already installed
if (Playnite.Database.Games.Any(g => g.GameId == gameId))
{
continue;
}
var gameNameWithTags = $"{gameName}{(item.Regions.Count > 0 ? $" ({string.Join(", ", item.Regions)})" : "")}{(!string.IsNullOrEmpty(item.Revision) ? $" (Rev {item.Revision})" : "")}{(item.Tags.Count > 0 ? $" ({string.Join(", ", item.Tags)})" : "")}";
// Add newly found game
games.Add(new GameMetadata
{
Source = SourceName,
Name = gameNameWithTags,
Roms = new List<GameRom> { new GameRom(gameNameWithTags, pathToGame) },
InstallDirectory = gameInstallDir,
IsInstalled = File.Exists(pathToGame),
GameId = gameId,
Platforms = new HashSet<MetadataProperty> { new MetadataNameProperty(mapping.Platform.Name ?? "") },
Regions = new HashSet<MetadataProperty>(item.Regions.Where(r => !string.IsNullOrEmpty(r)).Select(r => new MetadataNameProperty(r.ToString()))),
InstallSize = item.FileSizeBytes,
Description = item.Summary,
Icon = !string.IsNullOrEmpty(urlCover) ? new MetadataFile(urlCover) : null,
GameActions = new List<GameAction>
{
new GameAction
{
Name = $"Play in {mapping.Emulator.Name}",
Type = GameActionType.Emulator,
EmulatorId = mapping.EmulatorId,
EmulatorProfileId = mapping.EmulatorProfileId,
IsPlayAction = true,
},
new GameAction
{
Type = GameActionType.URL,
Name = "View in RomM",
Path = $"{Settings.RomMHost}/rom/{item.Id}",
IsPlayAction = false
}
}
});
}
Logger.Debug($"Finished adding new games for {apiPlatform.Name}");
// Find games in the database that are not in the response
var gamesInDatabase = Playnite.Database.Games.Where(g =>
g.Source != null && g.Source.Name == SourceName.ToString() &&
g.Platforms != null && g.Platforms.Any(p => p.Name == mapping.Platform.Name)
);
Logger.Debug($"Starting to remove not found games for {apiPlatform.Name}.");
foreach (var game in gamesInDatabase)
{
if (args.CancelToken.IsCancellationRequested)
break;
if (responseGameIDs.Contains(game.GameId))
{
continue;
}
// Remove from the playnite database
Playnite.Database.Games.Remove(game.Id);
}
Logger.Debug($"Finished removing not found games for {apiPlatform.Name}");
}
catch (HttpRequestException e)
{
Logger.Error($"Request exception: {e.Message}");
return games;
}
}
return games;
}
public override ISettings GetSettings(bool firstRunSettings)
{
return Settings;
}
public override UserControl GetSettingsView(bool firstRunSettings)
{
return new SettingsView();
}
public override IEnumerable<InstallController> GetInstallActions(GetInstallActionsArgs args)
{
if (args.Game.PluginId == Id)
{
yield return args.Game.GetRomMGameInfo().GetInstallController(args.Game, this);
}
}
public override IEnumerable<UninstallController> GetUninstallActions(GetUninstallActionsArgs args)
{
if (args.Game.PluginId == Id)
{
yield return args.Game.GetRomMGameInfo().GetUninstallController(args.Game, this);
}
}
public override void OnGameInstalled(OnGameInstalledEventArgs args)
{
base.OnGameInstalled(args);
if (args.Game.PluginId == PluginId && Settings.NotifyOnInstallComplete)
{
Playnite.Notifications.Add(args.Game.GameId, $"Download of \"{args.Game.Name}\" is complete", NotificationType.Info);
}
}
}
}