-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicroSDInstallStatusUpdater.cs
129 lines (114 loc) · 5.36 KB
/
MicroSDInstallStatusUpdater.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
using Playnite.SDK;
using Playnite.SDK.Events;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace MicroSDInstallStatusUpdater
{
public class MicroSDInstallStatusUpdater : GenericPlugin
{
private static readonly ILogger logger = LogManager.GetLogger();
private MicroSDInstallStatusUpdaterSettingsViewModel settings { get; set; }
public override Guid Id { get; } = Guid.Parse("216a1883-0794-4381-b5e3-8ccf42ad1463");
public MicroSDInstallStatusUpdater(IPlayniteAPI api) : base(api)
{
settings = new MicroSDInstallStatusUpdaterSettingsViewModel(this);
Properties = new GenericPluginProperties
{
HasSettings = false
};
}
public override void OnApplicationStarted(OnApplicationStartedEventArgs args)
{
var insertWatcher = new ManagementEventWatcher(new WqlEventQuery(
"__InstanceCreationEvent", new TimeSpan(0, 0, 1),
"TargetInstance isa 'Win32_DiskDrive' AND TargetInstance.Model LIKE '%SDXC Card%'"));
insertWatcher.EventArrived += SDCardInserted;
logger.Info("[SDCardInstallStatusUpdater] SDCard Insert Watcher Registered!");
var removeWatcher = new ManagementEventWatcher(new WqlEventQuery(
"__InstanceDeletionEvent", new TimeSpan(0, 0, 1),
"TargetInstance isa 'Win32_DiskDrive' AND TargetInstance.Model LIKE '%SDXC Card%'"));
removeWatcher.EventArrived += SDCardRemoved;
logger.Info("[SDCardInstallStatusUpdater] SDCard Remove Watcher Registered!");
logger.Info("[SDCardInstallStatusUpdater] SDCard Watc");
insertWatcher.Start();
removeWatcher.Start();
logger.Info("[SDCardInstallStatusUpdater] Playnite Started! Updating Database!");
foreach (var game in PlayniteApi.Database.Games)
{
var InstallDir = game.InstallDirectory;
if(!Directory.Exists(InstallDir) || InstallDir == null)
{
logger.Info("[SDCardInstallStatusUpdater] Game '" + game.Name + "' is not found anymore. Updateing Status to Uninstalled");
game.IsInstalled = false;
PlayniteApi.Database.Games.Update(game);
}
if (Directory.Exists(InstallDir))
{
logger.Info("[SDCardInstallStatusUpdater] Game '" + game.Name + "' is now found!. Updateing Status to Installed");
game.IsInstalled = true;
PlayniteApi.Database.Games.Update(game);
}
}
}
private void SDCardRemoved(object sender, EventArrivedEventArgs e)
{
logger.Info("[SDCardInstallStatusUpdater] SDCard Removed! Updating Database!");
Thread.Sleep(2000);
foreach (var game in PlayniteApi.Database.Games)
{
var InstallDir = game.InstallDirectory;
if (!Directory.Exists(InstallDir) || InstallDir == null)
{
logger.Info("[SDCardInstallStatusUpdater] Game '" + game.Name + "' is not found anymore. Updateing Status to Uninstalled");
game.IsInstalled = false;
PlayniteApi.Database.Games.Update(game);
}
if (Directory.Exists(InstallDir))
{
logger.Info("[SDCardInstallStatusUpdater] Game '" + game.Name + "' is now found!. Updateing Status to Installed");
game.IsInstalled = true;
PlayniteApi.Database.Games.Update(game);
}
}
}
private void SDCardInserted(object sender, EventArrivedEventArgs e)
{
logger.Info("[SDCardInstallStatusUpdater] SDCard Inserted! Sleeping a bit to let Windows handle and mount shit :3 Updating Database!");
Thread.Sleep(2000);
logger.Info("[SDCardInstallStatusUpdater] Sleep Done! Updating Database!");
foreach (var game in PlayniteApi.Database.Games)
{
var InstallDir = game.InstallDirectory;
if(!Directory.Exists(InstallDir) || InstallDir == null)
{
logger.Info("[SDCardInstallStatusUpdater] Game '" + game.Name + "' is not found anymore. Updateing Status to Uninstalled");
game.IsInstalled = false;
PlayniteApi.Database.Games.Update(game);
}
if (Directory.Exists(InstallDir))
{
logger.Info("[SDCardInstallStatusUpdater] Game '" + game.Name + "' is now found!. Updateing Status to Installed");
game.IsInstalled = true;
PlayniteApi.Database.Games.Update(game);
}
}
}
public override ISettings GetSettings(bool firstRunSettings)
{
return settings;
}
public override UserControl GetSettingsView(bool firstRunSettings)
{
return new MicroSDInstallStatusUpdaterSettingsView();
}
}
}