forked from Lacro59/playnite-successstory-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuccessStory.cs
408 lines (361 loc) · 16.3 KB
/
SuccessStory.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
using LiveCharts;
using LiveCharts.Wpf;
using Newtonsoft.Json;
using Playnite.SDK;
using Playnite.SDK.Events;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using PluginCommon;
using SuccessStory.Clients;
using SuccessStory.Database;
using SuccessStory.Models;
using SuccessStory.Views;
using SuccessStory.Views.Interface;
using System;
using System.Linq;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using Separator = System.Windows.Controls.Separator;
using PluginCommon.PlayniteResources;
using System.Diagnostics;
using SuccessStory.Services;
using System.Windows.Automation;
namespace SuccessStory
{
public class SuccessStory : Plugin
{
private static readonly ILogger logger = LogManager.GetLogger();
private static IResourceProvider resources = new ResourceProvider();
private SuccessStorySettings settings { get; set; }
public override Guid Id { get; } = Guid.Parse("cebe6d32-8c46-4459-b993-5a5189d60788");
private readonly IntegrationUI ui = new IntegrationUI();
private readonly TaskHelper taskHelper = new TaskHelper();
public static string pluginFolder;
public static Game GameSelected { get; set; }
public static SuccessStoryUI successStoryUI { get; set; }
public static AchievementsDatabase achievementsDatabase;
public static GameAchievements SelectedGameAchievements;
CancellationTokenSource tokenSource = new CancellationTokenSource();
public SuccessStory(IPlayniteAPI api) : base(api)
{
settings = new SuccessStorySettings(this);
// Get plugin's location
pluginFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// Add plugin localization in application ressource.
PluginCommon.Localization.SetPluginLanguage(pluginFolder, api.ApplicationSettings.Language);
// Add common in application ressource.
PluginCommon.Common.Load(pluginFolder);
// Check version
if (settings.EnableCheckVersion)
{
CheckVersion cv = new CheckVersion();
if (cv.Check("SuccessStory", pluginFolder))
{
cv.ShowNotification(api, "SuccessStory - " + resources.GetString("LOCUpdaterWindowTitle"));
}
}
// Init ui interagration
successStoryUI = new SuccessStoryUI(api, settings, this.GetPluginUserDataPath(), this);
// Custom theme button
if (settings.EnableIntegrationInCustomTheme)
{
EventManager.RegisterClassHandler(typeof(Button), Button.ClickEvent, new RoutedEventHandler(successStoryUI.OnCustomThemeButtonClick));
}
// Load database
var TaskLoadDatabase = Task.Run(() =>
{
achievementsDatabase = new AchievementsDatabase(this, PlayniteApi, settings, this.GetPluginUserDataPath());
achievementsDatabase.Initialize();
});
// Add Event for WindowBase for get the "WindowSettings".
EventManager.RegisterClassHandler(typeof(Window), Window.LoadedEvent, new RoutedEventHandler(WindowBase_LoadedEvent));
}
private void WindowBase_LoadedEvent(object sender, System.EventArgs e)
{
string WinIdProperty = String.Empty;
try
{
WinIdProperty = ((Window)sender).GetValue(AutomationProperties.AutomationIdProperty).ToString();
if (WinIdProperty == "WindowSettings")
{
#if DEBUG
logger.Debug($"SuccessStory - Reset VerifToAdd");
#endif
AchievementsDatabase.VerifToAddOrShowGog = null;
AchievementsDatabase.VerifToAddOrShowOrigin = null;
AchievementsDatabase.VerifToAddOrShowRetroAchievements = null;
AchievementsDatabase.VerifToAddOrShowSteam = null;
AchievementsDatabase.VerifToAddOrShowXbox = null;
}
}
catch (Exception ex)
{
Common.LogError(ex, "SuccessStory", $"Error on WindowBase_LoadedEvent for {WinIdProperty}");
}
}
// To add new game menu items override GetGameMenuItems
public override List<GameMenuItem> GetGameMenuItems(GetGameMenuItemsArgs args)
{
var GameMenu = args.Games.First();
List<GameMenuItem> gameMenuItems = new List<GameMenuItem>
{
new GameMenuItem {
MenuSection = resources.GetString("LOCSuccessStory"),
Description = resources.GetString("LOCSuccessStoryViewGame"),
Action = (gameMenuItem) =>
{
var ViewExtension = new SuccessView(this, settings, PlayniteApi, this.GetPluginUserDataPath(), false, GameMenu);
Window windowExtension = PlayniteUiHelper.CreateExtensionWindow(PlayniteApi, resources.GetString("LOCSuccessStory"), ViewExtension);
windowExtension.ShowDialog();
}
},
new GameMenuItem {
MenuSection = resources.GetString("LOCSuccessStory"),
Description = resources.GetString("LOCSuccessStoryRefreshData"),
Action = (gameMenuItem) =>
{
if (settings.EnableIntegrationInCustomTheme || settings.EnableIntegrationInDescription)
{
PlayniteUiHelper.ResetToggle();
}
var TaskIntegrationUI = Task.Run(() =>
{
achievementsDatabase.Remove(GameMenu);
successStoryUI.AddElements();
successStoryUI.RefreshElements(GameSelected);
});
}
}
};
#if DEBUG
gameMenuItems.Add(new GameMenuItem
{
MenuSection = resources.GetString("LOCSuccessStory"),
Description = "Test",
Action = (mainMenuItem) => { }
});
#endif
return gameMenuItems;
}
// To add new main menu items override GetMainMenuItems
public override List<MainMenuItem> GetMainMenuItems(GetMainMenuItemsArgs args)
{
string MenuInExtensions = string.Empty;
if (settings.MenuInExtensions)
{
MenuInExtensions = "@";
}
List<MainMenuItem> mainMenuItems = new List<MainMenuItem>
{
new MainMenuItem
{
MenuSection = MenuInExtensions + resources.GetString("LOCSuccessStory"),
Description = resources.GetString("LOCSuccessStoryViewGames"),
Action = (mainMenuItem) =>
{
var ViewExtension = new SuccessView(this, settings, PlayniteApi, this.GetPluginUserDataPath());
Window windowExtension = PlayniteUiHelper.CreateExtensionWindow(PlayniteApi, resources.GetString("LOCSuccessStory"), ViewExtension);
windowExtension.ShowDialog();
}
}
};
if (settings.EnableRetroAchievementsView && settings.EnableRetroAchievements)
{
mainMenuItems.Add(new MainMenuItem
{
MenuSection = MenuInExtensions + resources.GetString("LOCSuccessStory"),
Description = resources.GetString("LOCSuccessStoryViewGames") + " - RetroAchievements",
Action = (mainMenuItem) =>
{
SuccessView ViewExtension = null;
if (settings.EnableRetroAchievementsView && PlayniteTools.IsGameEmulated(PlayniteApi, GameSelected))
{
ViewExtension = new SuccessView(this, settings, PlayniteApi, this.GetPluginUserDataPath(), true, GameSelected);
}
else
{
ViewExtension = new SuccessView(this, settings, PlayniteApi, this.GetPluginUserDataPath(), false, GameSelected);
}
Window windowExtension = PlayniteUiHelper.CreateExtensionWindow(PlayniteApi, resources.GetString("LOCSuccessStory"), ViewExtension);
windowExtension.ShowDialog();
}
});
}
#if DEBUG
mainMenuItems.Add(new MainMenuItem
{
MenuSection = MenuInExtensions + resources.GetString("LOCSuccessStory"),
Description = "Test",
Action = (mainMenuItem) => { }
});
#endif
return mainMenuItems;
}
public override void OnGameInstalled(Game game)
{
// Add code to be executed when game is finished installing.
}
public override void OnGameStarted(Game game)
{
// Add code to be executed when game is started running.
}
public override void OnGameStarting(Game game)
{
// Add code to be executed when game is preparing to be started.
}
public override void OnGameStopped(Game game, long elapsedSeconds)
{
// Add code to be executed when game is preparing to be started.
var TaskGameStopped = Task.Run(() =>
{
// Refresh Achievements database for game played.
achievementsDatabase.Remove(game);
successStoryUI.AddElements();
successStoryUI.RefreshElements(GameSelected);
});
}
public override void OnGameUninstalled(Game game)
{
// Add code to be executed when game is uninstalled.
}
public override void OnApplicationStopped()
{
// Add code to be executed when Playnite is shutting down.
#if DEBUG
logger.Debug($"SuccessStory - Cancel TaskCacheImage");
#endif
tokenSource.Cancel();
}
public override void OnLibraryUpdated()
{
// Add code to be executed when library is updated.
// Get achievements for the new game added in the library.
var TaskLibraryUpdated = Task.Run(() =>
{
foreach (var game in PlayniteApi.Database.Games)
{
if (game.Added == null && ((DateTime)game.Added).ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd"))
{
achievementsDatabase.Remove(game);
successStoryUI.AddElements();
successStoryUI.RefreshElements(GameSelected);
}
}
});
}
public override void OnApplicationStarted()
{
// Add code to be executed when Playnite is initialized.
successStoryUI.AddBtHeader();
// Cache images
if (settings.EnableImageCache)
{
CancellationToken ct = tokenSource.Token;
var TaskCacheImage = Task.Run(() =>
{
// Wait Playnite & extension database are loaded
Thread.Sleep(60000);
#if DEBUG
logger.Debug($"SuccessStory - TaskCacheImage - {PlayniteApi.Database.Games.Count} - Start");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
#endif
foreach (Game game in PlayniteApi.Database.Games)
{
try
{
GameAchievements gameAchievements = achievementsDatabase.Get(game.Id);
if (gameAchievements != null && gameAchievements.HaveAchivements)
{
#if DEBUG
logger.Debug($"SuccessStory - TaskCacheImage - {game.Name} - {gameAchievements.Achievements.Count}");
#endif
foreach (var achievement in gameAchievements.Achievements)
{
if (!achievement.UrlLocked.IsNullOrEmpty() && PlayniteTools.GetCacheFile(achievement.CacheLocked, "SuccessStory").IsNullOrEmpty())
{
#if DEBUG
logger.Debug($"SuccessStory - TaskCacheImage.DownloadFileImage - {game.Name} - GetCacheFile({achievement.Name + "_Locked"})");
#endif
Web.DownloadFileImage(achievement.CacheLocked, achievement.UrlLocked, PlaynitePaths.ImagesCachePath, "SuccessStory").GetAwaiter().GetResult();
}
if (ct.IsCancellationRequested)
{
logger.Info($"IsCancellationRequested for TaskCacheImage()");
break;
}
if (PlayniteTools.GetCacheFile(achievement.CacheUnlocked, "SuccessStory").IsNullOrEmpty())
{
#if DEBUG
logger.Debug($"SuccessStory - TaskCacheImage.DownloadFileImage - {game.Name} - GetCacheFile({achievement.Name + "_Unlocked"})");
#endif
Web.DownloadFileImage(achievement.CacheUnlocked, achievement.UrlUnlocked, PlaynitePaths.ImagesCachePath, "SuccessStory").GetAwaiter().GetResult();
}
if (ct.IsCancellationRequested)
{
logger.Info($"IsCancellationRequested for TaskCacheImage()");
break;
}
}
}
}
catch (Exception ex)
{
#if DEBUG
Common.LogError(ex, "SuccessStory", $"Error on TaskCacheImage");
#endif
}
}
#if DEBUG
stopwatch.Stop();
TimeSpan ts = stopwatch.Elapsed;
logger.Debug($"SuccessStory - TaskCacheImage() - End - {String.Format("{0:00}:{1:00}.{2:00}", ts.Minutes, ts.Seconds, ts.Milliseconds / 10)}");
#endif
}, tokenSource.Token);
}
}
public override void OnGameSelected(GameSelectionEventArgs args)
{
try
{
if (args.NewValue != null && args.NewValue.Count == 1)
{
GameSelected = args.NewValue[0];
#if DEBUG
logger.Debug($"SuccessStory - Game selected: {GameSelected.Name}");
#endif
if (settings.EnableIntegrationInCustomTheme || settings.EnableIntegrationInDescription)
{
PlayniteUiHelper.ResetToggle();
var TaskIntegrationUI = Task.Run(() =>
{
successStoryUI.taskHelper.Check();
successStoryUI.AddElements();
successStoryUI.RefreshElements(GameSelected);
});
}
}
}
catch (Exception ex)
{
Common.LogError(ex, "SuccessStory", $"Error on OnGameSelected()");
}
}
public override ISettings GetSettings(bool firstRunSettings)
{
return settings;
}
public override UserControl GetSettingsView(bool firstRunSettings)
{
return new SuccessStorySettingsView(this, PlayniteApi, this.GetPluginUserDataPath(), settings);
}
}
}