This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NVIDIAGeForceNowEnabler.cs
370 lines (337 loc) · 15.3 KB
/
NVIDIAGeForceNowEnabler.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
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using Newtonsoft.Json;
namespace NVIDIAGeForceNowEnabler
{
public class NVIDIAGeForceNowEnabler : Plugin
{
private static readonly ILogger logger = LogManager.GetLogger();
private NVIDIAGeForceNowEnablerSettings settings { get; set; }
public override Guid Id { get; } = Guid.Parse("5f2dfd12-5f13-46fe-bcdd-64eb53ace26a");
public NVIDIAGeForceNowEnabler(IPlayniteAPI api) : base(api)
{
settings = new NVIDIAGeForceNowEnablerSettings(this);
}
public override void OnApplicationStarted()
{
if (settings.ExecuteOnStartup == true)
{
MainMethod(false);
}
}
public override void OnLibraryUpdated()
{
if (settings.ExecuteOnLibraryUpdate == true)
{
MainMethod(false);
}
}
public override ISettings GetSettings(bool firstRunSettings)
{
return settings;
}
public override UserControl GetSettingsView(bool firstRunSettings)
{
return new NVIDIAGeForceNowEnablerSettingsView();
}
public override List<MainMenuItem> GetMainMenuItems(GetMainMenuItemsArgs menuArgs)
{
return new List<MainMenuItem>
{
new MainMenuItem
{
Description = "Update enabled status of games",
MenuSection = "@NVIDIA GeForce NOW Enabler",
Action = args => {
MainMethod(true);
}
},
new MainMenuItem
{
Description = "Remove Play Action from all games",
MenuSection = "@NVIDIA GeForce NOW Enabler",
Action = args => {
LibraryRemoveAllPlayActions();
}
},
};
}
public bool RemoveFeature(Game game, GameFeature feature)
{
if (game.FeatureIds != null)
{
if (game.FeatureIds.Contains(feature.Id))
{
game.FeatureIds.Remove(feature.Id);
PlayniteApi.Database.Games.Update(game);
bool featureRemoved = true;
return featureRemoved;
}
else
{
bool featureRemoved = false;
return featureRemoved;
}
}
else
{
bool featureRemoved = false;
return featureRemoved;
}
}
public bool AddFeature(Game game, GameFeature feature)
{
if (game.FeatureIds == null)
{
game.FeatureIds = new List<Guid> { feature.Id };
PlayniteApi.Database.Games.Update(game);
bool featureAdded = true;
return featureAdded;
}
else if (game.FeatureIds.Contains(feature.Id) == false)
{
game.FeatureIds.AddMissing(feature.Id);
PlayniteApi.Database.Games.Update(game);
bool featureAdded = true;
return featureAdded;
}
else
{
bool featureAdded = false;
return featureAdded;
}
}
public void AddOtherAction(Game game, GameAction gameAction)
{
if (game.OtherActions == null)
{
game.OtherActions = new System.Collections.ObjectModel.ObservableCollection<GameAction> { gameAction };
}
else
{
game.OtherActions.AddMissing(gameAction);
}
}
public string UpdateNvidiaAction(Game game, GeforceGame supportedGame, string geforceNowWorkingPath, string geforceNowExecutablePath)
{
GameAction geforceNowAction = null;
if (game.OtherActions != null)
{
geforceNowAction = game.OtherActions.Where(x => x.Arguments != null).Where(x => Regex.IsMatch(x.Arguments, @"--url-route=""#\?cmsId=\d+&launchSource=External&shortName=game_gfn_pc&parentGameId=""")).FirstOrDefault();
}
if (supportedGame == null && geforceNowAction != null)
{
game.OtherActions.Remove(geforceNowAction);
PlayniteApi.Database.Games.Update(game);
string result = "playActionRemoved";
return result;
}
else if (supportedGame != null && geforceNowAction == null)
{
GameAction nvidiaGameAction = new GameAction();
nvidiaGameAction.Name = "Launch in Nvidia GeForce NOW";
var playActionArguments = String.Format("--url-route=\"#?cmsId={0}&launchSource=External&shortName=game_gfn_pc&parentGameId=\"", supportedGame.Id);
nvidiaGameAction.Arguments = playActionArguments;
nvidiaGameAction.Path = geforceNowExecutablePath;
nvidiaGameAction.WorkingDir = geforceNowWorkingPath;
AddOtherAction(game, nvidiaGameAction);
PlayniteApi.Database.Games.Update(game);
string result = "playActionAdded";
return result;
}
else
{
string result = null;
return result;
}
}
public List<GeforceGame> DownloadGameList(string uri, bool showDialogs)
{
var supportedGames = new List<GeforceGame>();
var progRes = PlayniteApi.Dialogs.ActivateGlobalProgress((a) => {
using (var webClient = new WebClient())
{
try
{
webClient.Encoding = Encoding.UTF8;
string downloadedString = webClient.DownloadString(uri);
supportedGames = JsonConvert.DeserializeObject<List<GeforceGame>>(downloadedString);
foreach (var supportedGame in supportedGames)
{
supportedGame.Title = Regex.Replace(supportedGame.Title, @"[^\p{L}\p{Nd}]", "").ToLower();
}
}
catch (Exception e)
{
logger.Error(e, e.Message);
if (showDialogs == true)
{
PlayniteApi.Dialogs.ShowErrorMessage(e.Message, "NVIDIA GeForce NOW Enabler");
}
}
}
}, new GlobalProgressOptions("Downloading NVIDIA GeForce Now database..."));
return supportedGames;
}
public IEnumerable<Game> GetGamesSupportedLibraries()
{
List<Guid> supportedLibraries = new List<Guid>()
{
BuiltinExtensions.GetIdFromExtension(BuiltinExtension.EpicLibrary),
BuiltinExtensions.GetIdFromExtension(BuiltinExtension.OriginLibrary),
BuiltinExtensions.GetIdFromExtension(BuiltinExtension.SteamLibrary),
BuiltinExtensions.GetIdFromExtension(BuiltinExtension.UplayLibrary),
BuiltinExtensions.GetIdFromExtension(BuiltinExtension.GogLibrary)
};
var gameDatabase = PlayniteApi.Database.Games.Where(g => supportedLibraries.Contains(g.PluginId));
return gameDatabase;
}
public void MainMethod(bool showDialogs)
{
string featureName = "NVIDIA GeForce NOW";
GameFeature feature = PlayniteApi.Database.Features.Add(featureName);
string localAppData = Environment.GetEnvironmentVariable("LocalAppData");
string geforceNowWorkingPath = Path.Combine(localAppData, "NVIDIA Corporation", "GeForceNOW", "CEF");
string geforceNowExecutablePath = Path.Combine(geforceNowWorkingPath, "GeForceNOWStreamer.exe");
var supportedGames = DownloadGameList("https://static.nvidiagrid.net/supported-public-game-list/gfnpc.json", showDialogs);
if (supportedGames.Count() == 0)
{
return;
}
var supportedSteamGames = supportedGames.Where(g => g.Store == "Steam");
var supportedEpicGames = supportedGames.Where(g => g.Store == "Epic");
var supportedOriginGames = supportedGames.Where(g => g.Store == "Origin");
var supportedUplayGames = supportedGames.Where(g => g.Store == "Ubisoft Connect");
var supportedGogGames = supportedGames.Where(g => g.Store == "GOG");
int enabledGamesCount = 0;
int featureAddedCount = 0;
int featureRemovedCount = 0;
int playActionAddedCount = 0;
int playActionRemovedCount = 0;
int setAsInstalledCount = 0;
int setAsUninstalledCount = 0;
var progRes = PlayniteApi.Dialogs.ActivateGlobalProgress((a) => {
var gameDatabase = GetGamesSupportedLibraries();
foreach (var game in gameDatabase)
{
var gameName = Regex.Replace(game.Name, @"[^\p{L}\p{Nd}]", "").ToLower();
GeforceGame supportedGame = null;
switch (BuiltinExtensions.GetExtensionFromId(game.PluginId))
{
case BuiltinExtension.SteamLibrary:
var steamUrl = String.Format("https://store.steampowered.com/app/{0}", game.GameId);
supportedGame = supportedSteamGames.Where(g => g.SteamUrl == steamUrl).FirstOrDefault();
break;
case BuiltinExtension.EpicLibrary:
supportedGame = supportedEpicGames.Where(g => g.Title == gameName).FirstOrDefault();
break;
case BuiltinExtension.OriginLibrary:
supportedGame = supportedOriginGames.Where(g => g.Title == gameName).FirstOrDefault();
break;
case BuiltinExtension.UplayLibrary:
supportedGame = supportedUplayGames.Where(g => g.Title == gameName).FirstOrDefault();
break;
case BuiltinExtension.GogLibrary:
supportedGame = supportedGogGames.Where(g => g.Title == gameName).FirstOrDefault();
break;
default:
break;
}
if (supportedGame == null)
{
bool featureRemoved = RemoveFeature(game, feature);
if (featureRemoved == true)
{
featureRemovedCount++;
logger.Info(String.Format("Feature removed from \"{0}\"", game.Name));
if (settings.SetEnabledGamesAsInstalled == true && game.IsInstalled == true)
{
game.IsInstalled = true;
setAsUninstalledCount++;
PlayniteApi.Database.Games.Update(game);
logger.Info(String.Format("Set \"{0}\" as uninstalled", game.Name));
}
}
}
else
{
enabledGamesCount++;
bool featureAdded = AddFeature(game, feature);
if (featureAdded == true)
{
featureAddedCount++;
logger.Info(String.Format("Feature added to \"{0}\"", game.Name));
}
if (settings.SetEnabledGamesAsInstalled == true && game.IsInstalled == false)
{
game.IsInstalled = true;
setAsInstalledCount++;
PlayniteApi.Database.Games.Update(game);
logger.Info(String.Format("Set \"{0}\" as installed", game.Name));
}
}
if (settings.UpdatePlayActions == true)
{
string updatePlayAction = UpdateNvidiaAction(game, supportedGame, geforceNowWorkingPath, geforceNowExecutablePath);
if (updatePlayAction == "playActionAdded")
{
playActionAddedCount++;
logger.Info(String.Format("Play Action added to \"{0}\"", game.Name));
}
else if (updatePlayAction == "playActionRemoved")
{
playActionRemovedCount++;
logger.Info(String.Format("Play Action removed from \"{0}\"", game.Name));
}
}
} }, new GlobalProgressOptions("Updating NVIDIA GeForce NOW Enabled games"));
if (showDialogs == true)
{
string results = String.Format("NVIDIA GeForce NOW enabled games in library: {0}\n\nAdded \"{1}\" feature to {2} games.\nRemoved \"{3}\" feature from {4} games.",
enabledGamesCount, featureName, featureAddedCount, featureName, featureRemovedCount);
if (settings.UpdatePlayActions == true)
{
results += String.Format("\n\nPlay Action added to {0} games.\nPlay Action removed from {1} games.",
playActionAddedCount, playActionRemovedCount);
}
if (settings.SetEnabledGamesAsInstalled == true)
{
results += String.Format("\n\nSet {0} games as Installed.\nSet {1} games as uninstalled.", setAsInstalledCount, setAsUninstalledCount);
}
PlayniteApi.Dialogs.ShowMessage(results, "NVIDIA GeForce NOW Enabler");
}
}
public void LibraryRemoveAllPlayActions()
{
int playActionRemovedCount = 0;
var gameDatabase = GetGamesSupportedLibraries();
foreach (var game in gameDatabase)
{
GameAction geforceNowAction = null;
if (game.OtherActions != null)
{
geforceNowAction = game.OtherActions.Where(x => x.Arguments != null).Where(x => Regex.IsMatch(x.Arguments, @"--url-route=""#\?cmsId=\d+&launchSource=External&shortName=game_gfn_pc&parentGameId=""")).FirstOrDefault();
}
if (geforceNowAction != null)
{
game.OtherActions.Remove(geforceNowAction);
PlayniteApi.Database.Games.Update(game);
playActionRemovedCount++;
logger.Info(String.Format("Play Action removed from \"{0}\"", game.Name));
}
}
string results = String.Format("Play Action removed from {0} games", playActionRemovedCount);
PlayniteApi.Dialogs.ShowMessage(results, "NVIDIA GeForce NOW Enabler");
}
}
}