-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowerPlanSwitcher.cs
130 lines (117 loc) · 3.88 KB
/
PowerPlanSwitcher.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
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Events;
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.Diagnostics;
using System.IO;
namespace PowerPlanSwitcher
{
public class PowerPlanSwitcher : GenericPlugin
{
private static readonly ILogger logger = LogManager.GetLogger();
private PowerPlanSwitcherSettingsViewModel settings { get; set; }
public override Guid Id { get; } = Guid.Parse("8d0af913-8f5f-4f8c-adef-527be337a619");
private const string POWERCFG_EXEC = "powercfg.exe";
private List<Guid> runningGames;
private void ChangePowerPlan(string powerCfgId)
{
string args = "/s " + powerCfgId;
ProcessStartInfo start = new ProcessStartInfo
{
FileName = POWERCFG_EXEC,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
using (Process process = Process.Start(start))
{
process.WaitForExit();
}
}
private void RegisterRunningGame(Game game)
{
runningGames.Add(game.Id);
}
private void UnregisterRunningGames(Game game)
{
if (!runningGames.Remove(game.Id))
{
throw new Exception("Game with ID \"" + game.Id + "\" was not previously running.");
}
}
public PowerPlanSwitcher(IPlayniteAPI api) : base(api)
{
settings = new PowerPlanSwitcherSettingsViewModel(this);
runningGames = new List<Guid>();
Properties = new GenericPluginProperties
{
HasSettings = true
};
}
public override void OnGameStarting(OnGameStartingEventArgs args)
{
string gameSource;
if (args.Game.Source == null)
{
gameSource = "";
}
else
{
gameSource = args.Game.Source.Name;
}
string[] ignoredSources = settings.Settings.IgnoredSources.Split(',');
if (new List<string>(ignoredSources).Any(x => x.Equals(gameSource, StringComparison.InvariantCultureIgnoreCase)))
{
// Cancel power plan change.
return;
}
RegisterRunningGame(args.Game);
ChangePowerPlan(settings.Settings.GamingPowerPlan);
}
public override void OnGameStopped(OnGameStoppedEventArgs args)
{
string gameSource;
if (args.Game.Source == null)
{
gameSource = "";
}
else
{
gameSource = args.Game.Source.Name;
}
string[] ignoredSources = settings.Settings.IgnoredSources.Split(',');
if (new List<string>(ignoredSources).Any(x => x.Equals(gameSource, StringComparison.InvariantCultureIgnoreCase)))
{
// Cancel power plan change.
return;
}
try
{
UnregisterRunningGames(args.Game);
}
catch (Exception e)
{
PlayniteApi.Dialogs.ShowErrorMessage(e.Message, "Power Plan Switcher");
}
if (runningGames.Count == 0)
{
ChangePowerPlan(settings.Settings.DefaultPowerPlan);
}
}
public override ISettings GetSettings(bool firstRunSettings)
{
return settings;
}
public override UserControl GetSettingsView(bool firstRunSettings)
{
return new PowerPlanSwitcherSettingsView();
}
}
}