From 4bf631e34ddcec9529058c8b780aa2b6e740ae60 Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Sat, 6 May 2023 17:50:05 +0800 Subject: [PATCH] Add feature to custom bypass process list 1. Add feature to custom bypass process list 2. Add example config form winsw --- EnergyStar/EnergyManager.cs | 13 ++++++++- EnergyStar/Program.cs | 57 +++++++++++++++++++++++++++++++++++++ examples/winsw/winsw.xml | 53 ++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 examples/winsw/winsw.xml diff --git a/EnergyStar/EnergyManager.cs b/EnergyStar/EnergyManager.cs index 0b93002..2b7bc68 100644 --- a/EnergyStar/EnergyManager.cs +++ b/EnergyStar/EnergyManager.cs @@ -7,7 +7,7 @@ namespace EnergyStar { public unsafe class EnergyManager { - public static readonly HashSet BypassProcessList = new HashSet + public static readonly HashSet DefaultBypassProcessList = new HashSet { // Not ourselves "EnergyStar.exe".ToLowerInvariant(), @@ -44,6 +44,7 @@ public unsafe class EnergyManager // WUDF "WUDFRd.exe".ToLowerInvariant(), }; + public static HashSet BypassProcessList = DefaultBypassProcessList; // Speical handling needs for UWP to get the child window process public const string UWPFrameHostApp = "ApplicationFrameHost.exe"; @@ -184,5 +185,15 @@ public static void ThrottleAllUserBackgroundProcesses() Win32Api.CloseHandle(hProcess); } } + + public static void SetBypassProcessList(IEnumerable newBypassList) + { + var list = new List(); + + list.AddRange(DefaultBypassProcessList); + list.AddRange(newBypassList); + + BypassProcessList = list.Distinct().ToHashSet(); + } } } diff --git a/EnergyStar/Program.cs b/EnergyStar/Program.cs index 6f791a4..186ee85 100644 --- a/EnergyStar/Program.cs +++ b/EnergyStar/Program.cs @@ -24,6 +24,46 @@ static async void HouseKeepingThreadProc() } } + static void OnBypassListChanged(object sender, FileSystemEventArgs e) + { + var path = e.FullPath; + if (path == null) + return; + + LoadBypassList(path); + } + + static void LoadBypassList(string path) + { + if (File.Exists(path)) + { + var file = File.OpenText(path); + var content = file.ReadToEnd(); + file.Close(); + + if (content != null) + { + var srcBypassList = content.Replace("\r\n", "\n").Split('\n'); + var bypassList = new List { }; + // Remove empty items + foreach (var item in srcBypassList) + { + var trimed = item.Trim().ToLowerInvariant(); + if (trimed.Length > 0) + { + bypassList.Add(trimed); + } + } + + if (bypassList != null) + { + EnergyManager.SetBypassProcessList(bypassList); + Console.WriteLine("Bypass list updated"); + } + } + } + } + static void Main(string[] args) { // Well, this program only works for Windows Version starting with Cobalt... @@ -38,6 +78,23 @@ static void Main(string[] args) Environment.Exit(120); } + // Load bypass list and watch for changes. + var baseDir = AppDomain.CurrentDomain.BaseDirectory; + var bypassListFile = Path.Combine(baseDir, "bypass.txt"); + using var bypassListWatcher = new FileSystemWatcher(baseDir); + + bypassListWatcher.Changed += OnBypassListChanged; + bypassListWatcher.Created += OnBypassListChanged; + bypassListWatcher.Deleted += OnBypassListChanged; + bypassListWatcher.Renamed += OnBypassListChanged; + + bypassListWatcher.Filter = "bypass.txt"; + bypassListWatcher.IncludeSubdirectories = true; + bypassListWatcher.EnableRaisingEvents = true; + + LoadBypassList(bypassListFile); + + HookManager.SubscribeToWindowEvents(); EnergyManager.ThrottleAllUserBackgroundProcesses(); diff --git a/examples/winsw/winsw.xml b/examples/winsw/winsw.xml new file mode 100644 index 0000000..75d950c --- /dev/null +++ b/examples/winsw/winsw.xml @@ -0,0 +1,53 @@ + + + + + + + energystar + + EnergyStar + + A terrible application setting SV2 Efficiency Mode for inactive Windows apps and user background apps + + + %BASE%\EnergyStar.exe + + %BASE%\logs + + + 1024 + 1 + +