-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathProgram.cs
More file actions
1276 lines (1118 loc) · 50.9 KB
/
Program.cs
File metadata and controls
1276 lines (1118 loc) · 50.9 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Hp.Bridge.Client.SDKs.PerformanceControl.DataStructure;
using HP.Omen.Core.Model.Device.Enums;
using HP.Omen.Core.Model.Device.Models;
using Microsoft.Win32;
using static OmenSuperHub.GpuAppManager;
using static OmenSuperHub.OmenHardware;
using static OmenSuperHub.OmenLighting;
using LibreComputer = LibreHardwareMonitor.Hardware.Computer;
using LibreHardwareType = LibreHardwareMonitor.Hardware.HardwareType;
using LibreIHardware = LibreHardwareMonitor.Hardware.IHardware;
using LibreISensor = LibreHardwareMonitor.Hardware.ISensor;
using LibreSensorType = LibreHardwareMonitor.Hardware.SensorType;
namespace OmenSuperHub {
static partial class Program {
[DllImport("user32.dll")]
static extern bool SetProcessDPIAware();
static byte currentAnimSpeed = 1, currentAnimDirection = 0, currentAnimTheme = 0, currentAnimEffect = 2;
// 单键RGB当前选中状态(用于菜单勾选,null/-1 表示未选择)
static string perKeyStaticColorSel = null;
static string perKeyAnimationSel = null;
static int perKeyBrightnessSel = -1;
// 四分区/灯条颜色选择状态(key = device tag, value = 颜色名称;null 表示自定义或未选)
static string zoneGlobalColorSel_Keyboard = null;
static string zoneGlobalColorSel_LightBar = null;
static string[] zoneColorSel_Keyboard = new string[4];
static string[] zoneColorSel_LightBar = new string[4];
// 四分区/灯条 WMI 协议选择(默认 BasicFourZone;用户可在菜单中切换并持久化)
static LightingControlInterface kbControlInterface = LightingControlInterface.BasicFourZone;
static LightingControlInterface lbControlInterface = LightingControlInterface.Dojo;
static int DBVersion = 2, countDB = 0, countDBInit = 5, tryTimes = 0, CPULimitDB = 25;
static ToolStripMenuItem DBMenu;
static int textSize = 48;
static int countRestore = 0, gpuClock = 0;
static int alreadyRead = 0, alreadyReadCode = 1000;
static string fanTable = "cool", fanControl = "auto", tempSensitivity = "high", tppPower = "null", iccMax = "null", acLoadline = "null", cpuPower = "null", tgpPower = "on", ppabPower = "on", dState = "normal", autoStart = "off", customIcon = "original", floatingBar = "off", floatingBarLoc = "left", omenKey = "default", dataLocalize = "off", appLanguage = "zh-CN";
static volatile bool monitorFan = false;
static bool skipCheckedUpdate = false; // action 内拦截时置 true,阻止 CreateMenuItem 覆盖勾选
static bool monitorCPU = true, monitorGPU = true, isConnectedToNVIDIA = true, prevIsConnectedToNVIDIA = true, powerOnline = true, checkFloating = false, isTwoBytePL4 = false;
static bool hasNVIDIAGpu, hasAMDDiscreteGpu; // 启动时一次性检测,硬件状态不会改变
static string monitorRefreshRate = "low"; // 刷新频率:low=1s, high=0.25s
static List<int> fanSpeedNow = new List<int> { 20, 23, 0 };
static float respondSpeed = 0.4f;
static int? maxCPUTemp = null;
static int? maxGPUTemp = null;
static float CPUTemp = 50, GPUTemp = 40, rawTempCPU = 50f, rawTempGPU = 40f;
static float CPUPower = 0, GPUPower = 0, rawPowerCPU = 0f, rawPowerGPU = 0f;
static bool rawGotGPU = false;
static volatile bool tempReady = false; // 子进程首次输出有效温度后置 true
static volatile bool cpuTempReady = false; // CPU 温度已初始化给平滑值,允许参与风扇控制
static volatile bool gpuTempReady = false; // GPU 温度已初始化给平滑值,允许参与风扇控制
static volatile bool hwMonitorStopping = false; // 主动停止时置 true,阻止 Exited 自动重启
static Process hwMonitorProcess;
static StreamWriter hwMonitorIn;
// Cache last written values to avoid unnecessary disk reads/writes
static string lastCpuText = null, lastGpuText = null, lastFanText = null;
static string tempDisplayMode = "smoothed"; // 温度显示方式:smoothed=平滑值, raw=原始值
static int? platformMaxFanSpeed = null; // 平台最大转速(RPM),由LoadDefaultFanConfig获取后缓存
static SortedDictionary<float, List<int>> CPUTempFanMap = new SortedDictionary<float, List<int>>();
static SortedDictionary<float, List<int>> GPUTempFanMap = new SortedDictionary<float, List<int>>();
static System.Threading.Timer fanControlTimer;
static System.Timers.Timer tooltipUpdateTimer; // Timer for updating tooltip
static System.Windows.Forms.Timer checkFloatingTimer, optimiseTimer;
static NotifyIcon trayIcon;
static FloatingForm floatingForm;
static ToolStripMenuItem irSensorMenu;
static ToolStripMenuItem ambientSensorMenu;
static ToolStripMenuItem pchSensorMenu;
static ToolStripMenuItem vrSensorMenu;
static ToolStripTrackBar fanTrackBar, cpuPowerTrackBar, tppTrackBar, gpuClockTrackBar;
static ToolStripMenuItem fanValueLabel, cpuPowerValueLabel, tppValueLabel, gpuClockValueLabel;
static bool Is3FanNb = false, isFanCleanSupported = false, isFanLegacyCleanSupported = false;
static bool isSysInfoMenuOpen = false;
static string systemSSID;
static bool supportAni = false, supportDojo = false, supportLightbar = false;
static DeviceEnums.DeviceType deviceType;
static PlatformSettings platformSettings;
static GraphicsMode NvGraphicsMode;
static NbKeyboardLightingType kbType;
[STAThread]
static void Main(string[] args) {
if (args.Length > 0 && args[0] == "--hwmonitor") {
RunHardwareMonitor();
return;
}
// ── 静默重启模式:由任务计划登录触发器调用
if (args.Length > 0 && args[0] == "--relaunch") {
// 终止其他已有实例
var currentId = Process.GetCurrentProcess().Id;
foreach (var proc in Process.GetProcessesByName("OmenSuperHub")) {
if (proc.Id == currentId) continue;
try { proc.Kill(); proc.WaitForExit(3000); } catch { }
}
// 启动新实例(不带参数,走正常流程)
Process.Start(new ProcessStartInfo {
FileName = Application.ExecutablePath,
UseShellExecute = true
});
return; // 本实例立即退出,不做任何初始化
}
bool isNewInstance;
using (Mutex mutex = new Mutex(true, "MyUniqueAppMutex", out isNewInstance)) {
if (!isNewInstance) {
return;
}
if (Environment.OSVersion.Version.Major >= 6) {
SetProcessDPIAware();
}
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
AppDomain.CurrentDomain.AssemblyResolve += ResolveEmbeddedAssembly;
kbType = GetKeyboardType();
systemSSID = DeviceModel.ThisSystemID; // DeviceModel.OmenPlatform.Name
deviceType = DeviceModel.DeviceType;
string sku = PerformanceControlHelper.GetPlatformSku(isInit: true);
platformSettings = PerformanceControlHelper.GetPlatformSettings(deviceType.ToString(), sku);
if (FourZoneHelper.IsAnimationSupported) {
supportAni = true;
}
if (DeviceModel.OmenPlatform.Feature.Contains("DojoLighting")) {
supportDojo = true;
if (IsLightBarPlatform())
supportLightbar = true;
}
NvGraphicsMode = GetGfxMode();
hasAMDDiscreteGpu = HasAmdDiscreteGpu();
hasNVIDIAGpu = GetNVIDIAModel() != null;
if (hasNVIDIAGpu && (NvGraphicsMode == GraphicsMode.Hybrid || NvGraphicsMode == GraphicsMode.Optimus))
ExtractAndPreloadNativeDll("NvidiaApi.dll");
// 固定为释放全部性能模式
SetUnleashMode();
Is3FanNb = IsThreeFanSupported();
isFanCleanSupported = IsCleanCreekSupported();
isFanLegacyCleanSupported = IsLegacyCleanCreekSupported();
powerOnline = SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online;
monitorQuery();
Version version = Assembly.GetExecutingAssembly().GetName().Version;
string versionString = version.ToString().Replace(".", "");
alreadyReadCode = new Random(int.Parse(versionString)).Next(1000, 10000);
isTwoBytePL4 = IsTwoBytePL4Supported();
// Initialize tray icon
InitMaxTemp();
InitPlatformMaxFanSpeed();
LoadLanguageSetting(); // 必须在 InitTrayIcon 之前,使菜单使用正确语言
InitTrayIcon();
optimiseTimer = new System.Windows.Forms.Timer();
optimiseTimer.Interval = 30000;
optimiseTimer.Tick += (s, e) => optimiseSchedule();
optimiseTimer.Start();
// 立即执行一次
optimiseSchedule();
// Main loop to query CPU and GPU temperature every second
fanControlTimer = new System.Threading.Timer((e) => {
// 自动模式下,首次获取到真实温度数据前不进行转速控制
if (!tempReady && fanControl == "auto") return;
int fanSpeed1 = GetFanSpeedForTemperature(0) / 100;
int fanSpeed2 = GetFanSpeedForTemperature(1) / 100;
int s0, s1;
lock (fanSpeedNow) { s0 = fanSpeedNow[0]; s1 = fanSpeedNow[1]; }
if (Math.Abs(fanSpeed1 - s0) > 1 || Math.Abs(fanSpeed2 - s1) > 1) {
SetFanLevel(fanSpeed1, fanSpeed2, Is3FanNb);
if (!monitorFan) {
lock (fanSpeedNow) { fanSpeedNow[0] = fanSpeed1; fanSpeedNow[1] = fanSpeed2; }
}
}
}, null, 100, 1000);
getOmenKeyTask();
checkFloatingTimer = new System.Windows.Forms.Timer();
checkFloatingTimer.Interval = 100;
checkFloatingTimer.Tick += (s, e) => HandleFloatingBarToggle();
checkFloatingTimer.Start();
// Restore last setting
RestoreConfig();
if (alreadyRead != alreadyReadCode) {
HelpForm.Instance.Show();
alreadyRead = alreadyReadCode;
SaveConfig("AlreadyRead");
}
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerChange);
//PrintSystemDesignData();
//MessageBox.Show($"消息测试", Strings.Hint, MessageBoxButtons.OK, MessageBoxIcon.Warning);
//trayIcon.BalloonTipTitle = "消息测试";
//trayIcon.BalloonTipText = $"消息测试";
//trayIcon.BalloonTipIcon = ToolTipIcon.Warning;
//trayIcon.ShowBalloonTip(3000);
//Console.WriteLine($"DeviceType: {WindowsLightingUtility.IsLightBarPlatform()}");
//Console.WriteLine($"DeviceType: {deviceType}");
//Console.WriteLine($"PlatformSku: {sku}");
//Console.WriteLine($"TppMaxValue: {platformSettings.TppMaxValue}");
//Platform omenPlatform = DeviceModel.OmenPlatform;
//Console.WriteLine($"Platform Name: {omenPlatform.Name}");
//Console.WriteLine($"Display Name: {omenPlatform.DisplayName}");
//Console.WriteLine($"Features: {string.Join(", ", omenPlatform.Feature ?? new List<string>())}");
//Console.WriteLine($"IsNVdGPU: {OmenHsaClient.IsNVdGPU}");
//Console.WriteLine($"IsIntelGPU: {OmenHsaClient.IsIntelGPU}");
//Console.WriteLine($"IsGpuSupport: {OmenHsaClient.IsGpuSupport}");
//Console.WriteLine($"IsIntelGraphics: {OmenHsaClient.IsIntelGraphics()}");
Logger.Info($"version: {version}");
Application.Run();
}
}
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int NvidiaAPI_SYS_UIControl_Delegate(bool on);
public static int LaunchDDS() {
IntPtr hModule = GetModuleHandle("NvidiaApi.dll");
if (hModule == IntPtr.Zero) return -1;
IntPtr proc = GetProcAddress(hModule, "NvidiaAPI_SYS_UIControl");
if (proc == IntPtr.Zero) return -1;
var fn = (NvidiaAPI_SYS_UIControl_Delegate)Marshal.GetDelegateForFunctionPointer(proc, typeof(NvidiaAPI_SYS_UIControl_Delegate));
return fn(true);
}
static string GetBiosVersion() {
using (var searcher = new ManagementObjectSearcher("SELECT SMBIOSBIOSVersion FROM Win32_BIOS"))
using (var collection = searcher.Get())
foreach (ManagementObject obj in collection)
return obj["SMBIOSBIOSVersion"]?.ToString() ?? "未知";
return "未知";
}
static string GetCpuModel() {
using (var searcher = new ManagementObjectSearcher("SELECT Name FROM Win32_Processor"))
using (var collection = searcher.Get())
foreach (ManagementObject obj in collection)
return obj["Name"]?.ToString()?.Trim() ?? "未知";
return "未知";
}
public static bool HasIntelCpu() {
try {
using (var searcher = new ManagementObjectSearcher(
"root\\CIMV2", "SELECT Manufacturer, Name FROM Win32_Processor")) {
foreach (var obj in searcher.Get()) {
string manufacturer = obj["Manufacturer"]?.ToString() ?? "";
string name = obj["Name"]?.ToString() ?? "";
// GenuineIntel 是 Intel CPU 的标准制造商字符串
if (manufacturer.IndexOf("GenuineIntel", StringComparison.OrdinalIgnoreCase) >= 0 ||
name.IndexOf("Intel", StringComparison.OrdinalIgnoreCase) >= 0) {
return true;
}
}
}
} catch { }
return false;
}
public static bool HasAmdGpu() {
try {
using (var searcher = new System.Management.ManagementObjectSearcher(
"root\\CIMV2", "SELECT Name FROM Win32_VideoController")) {
foreach (var obj in searcher.Get()) {
string name = obj["Name"]?.ToString() ?? "";
if (name.IndexOf("AMD", StringComparison.OrdinalIgnoreCase) >= 0 ||
name.IndexOf("Radeon", StringComparison.OrdinalIgnoreCase) >= 0)
return true;
}
}
} catch { }
return false;
}
public static bool HasAmdDiscreteGpu() {
try {
using (var searcher = new ManagementObjectSearcher(
"root\\CIMV2",
"SELECT Name, AdapterCompatibility, VideoProcessor FROM Win32_VideoController")) {
foreach (var obj in searcher.Get()) {
string name = obj["Name"]?.ToString() ?? "";
string vendor = obj["AdapterCompatibility"]?.ToString() ?? "";
string processor = obj["VideoProcessor"]?.ToString() ?? "";
// AMD 显卡供应商 ID 为 1002
bool isAmd = vendor.Contains("1002") || name.IndexOf("AMD", StringComparison.OrdinalIgnoreCase) >= 0;
if (!isAmd) continue;
// 排除典型的集显命名特征(Radeon Graphics 不带 RX/数字型号)
bool isIntegrated = name.Contains("Radeon Graphics") && !name.Contains("RX")
|| name.Contains("AMD Radeon(TM) Graphics");
if (!isIntegrated) // 不是集显,那就是独显
return true;
// 也可以进一步检查 VideoProcessor 字段,独显通常有具体代号如 "Navi", "Ellesmere", "Vega 10"
if (!string.IsNullOrEmpty(processor) && !processor.Contains("Renoir") && !processor.Contains("Cezanne") && !processor.Contains("Rembrandt"))
return true; // 集显 APU 代号为 Renoir/Cezanne/Rembrandt 等
}
}
} catch { }
return false;
}
public static class AmdGpuSwitcher {
// 本地枚举定义(与 DLL 中的值完全对应)
public enum LocalADLSmartMuxEnableState {
ADL_MUXCONTROL_DISABLED = 0,
ADL_MUXCONTROL_ENABLED = 1
}
private static object GetSAGHelper() {
// 获取 SmartAccessGraphicsHelp 类型
Assembly commonAssembly = AppDomain.CurrentDomain.GetAssemblies()
.First(a => a.GetName().Name == "HP.Omen.Core.Common");
Type sagHelpType = commonAssembly.GetType(
"HP.Omen.Core.Common.Utilities.SmartAccessGraphicsHelp.SmartAccessGraphicsHelp");
// 获取 SAGHelper 静态属性(单例)
PropertyInfo sagHelperProp = sagHelpType.GetProperty("SAGHelper",
BindingFlags.Public | BindingFlags.Static);
return sagHelperProp.GetValue(null); // 静态属性,get
}
public static bool IsSupported() {
if (hasNVIDIAGpu || !hasAMDDiscreteGpu) // ★ 先检查硬件,避免触发 ADL
return false;
object helper = GetSAGHelper();
if (helper == null) return false;
PropertyInfo supportProp = helper.GetType().GetProperty(
"SmartAccessGraphicsSupport",
BindingFlags.Public | BindingFlags.Instance);
return (bool)supportProp.GetValue(helper);
}
public static LocalADLSmartMuxEnableState GetMode() {
object helper = GetSAGHelper();
if (helper == null) return LocalADLSmartMuxEnableState.ADL_MUXCONTROL_DISABLED;
PropertyInfo modeProp = helper.GetType().GetProperty(
"SmartAccessGraphicsMode",
BindingFlags.Public | BindingFlags.Instance);
int modeValue = (int)modeProp.GetValue(helper);
return (LocalADLSmartMuxEnableState)modeValue;
}
public static void SetMode(LocalADLSmartMuxEnableState mode) {
object helper = GetSAGHelper();
if (helper == null) return;
// 获取 DLL 中的枚举类型
Assembly commonAssembly = AppDomain.CurrentDomain.GetAssemblies()
.First(a => a.GetName().Name == "HP.Omen.Core.Common");
Type stateEnum = commonAssembly.GetType(
"HP.Omen.Core.Common.Utilities.SmartAccessGraphicsHelp.ADLSmartMuxEnableState");
// 将本地枚举值转换为 DLL 枚举对象
object modeValue = Enum.ToObject(stateEnum, (int)mode);
MethodInfo setMethod = helper.GetType().GetMethod(
"SetSmartAccessGraphicsMode",
BindingFlags.Public | BindingFlags.Instance);
setMethod.Invoke(helper, new[] { modeValue });
}
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern IntPtr LoadLibrary(string lpFileName);
private static void ExtractAndPreloadNativeDll(string dllName) {
var currentAssembly = Assembly.GetExecutingAssembly();
// 在嵌入资源中查找(资源名通常是 "命名空间.文件名")
var resourceName = currentAssembly
.GetManifestResourceNames()
.FirstOrDefault(r => r.EndsWith(dllName, StringComparison.OrdinalIgnoreCase));
if (resourceName == null) {
throw new FileNotFoundException($"嵌入资源中找不到 {dllName}");
}
// 释放到程序目录(或 Temp 目录)
string outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
if (!File.Exists(outputPath)) {
using (var stream = currentAssembly.GetManifestResourceStream(resourceName))
using (var fs = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) {
stream.CopyTo(fs);
}
}
// 提前加载,之后 DllImport 会自动复用
IntPtr handle = LoadLibrary(outputPath);
if (handle == IntPtr.Zero) {
throw new Exception($"LoadLibrary 失败,错误码: {Marshal.GetLastWin32Error()}");
}
}
private static Assembly ResolveEmbeddedAssembly(object sender, ResolveEventArgs args) {
var assemblyName = new AssemblyName(args.Name).Name + ".dll";
var currentAssembly = Assembly.GetExecutingAssembly();
var resourceName = currentAssembly
.GetManifestResourceNames()
.FirstOrDefault(r => r.EndsWith(assemblyName, StringComparison.OrdinalIgnoreCase));
if (resourceName == null)
return null;
using (var stream = currentAssembly.GetManifestResourceStream(resourceName)) {
if (stream == null)
return null;
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
return Assembly.Load(buffer);
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
struct DISPLAY_DEVICE {
[MarshalAs(UnmanagedType.U4)]
public int cb;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DeviceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceString;
[MarshalAs(UnmanagedType.U4)]
public DisplayDeviceStateFlags StateFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string DeviceKey;
}
[Flags()]
enum DisplayDeviceStateFlags : int {
/// <summary>The device is part of the desktop.</summary>
AttachedToDesktop = 0x1,
MultiDriver = 0x2,
/// <summary>The device is part of the desktop.</summary>
PrimaryDevice = 0x4,
/// <summary>Represents a pseudo device used to mirror application drawing for remoting or other purposes.</summary>
MirroringDriver = 0x8,
/// <summary>The device is VGA compatible.</summary>
VGACompatible = 0x10,
/// <summary>The device is removable; it cannot be the primary display.</summary>
Removable = 0x20,
/// <summary>The device has more display devices.</summary>
ModesPruned = 0x8000000,
Remote = 0x4000000,
Disconnect = 0x2000000
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool EnumDisplayDevices(
string lpDevice,
uint iDevNum,
ref DISPLAY_DEVICE lpDisplayDevice,
uint dwFlags);
// 判断独显未工作条件
static void monitorQuery() {
if (Screen.AllScreens.Length != 1)
return;
DISPLAY_DEVICE d = new DISPLAY_DEVICE();
d.cb = Marshal.SizeOf(d);
uint deviceNum = 0;
while (EnumDisplayDevices(null, deviceNum, ref d, 0)) {
if (d.StateFlags.HasFlag(DisplayDeviceStateFlags.AttachedToDesktop)) {
if (d.DeviceString.Contains("Intel") || d.DeviceString.Contains("AMD")) {
isConnectedToNVIDIA = false;
return;
}
}
deviceNum++;
}
isConnectedToNVIDIA = true;
}
[HandleProcessCorruptedStateExceptions]
static void RunHardwareMonitor() {
var computer = new LibreComputer() { IsCpuEnabled = true };
try {
computer.Open();
} catch (Exception ex) {
Console.Error.WriteLine("CRASH: Open failed - " + ex.Message);
Environment.Exit(1);
}
int sleepMs = 1000;
var readThread = new Thread(() => {
while (true) {
string line = Console.ReadLine();
if (line == null) Environment.Exit(0);
if (line == "GPU:ON") computer.IsGpuEnabled = true;
if (line == "GPU:OFF") computer.IsGpuEnabled = false;
if (line == "CPU:ON") computer.IsCpuEnabled = true;
if (line == "CPU:OFF") computer.IsCpuEnabled = false;
if (line.StartsWith("INTERVAL:") && int.TryParse(line.Substring(9), out int ms) && ms > 0)
sleepMs = ms;
}
});
readThread.IsBackground = true;
readThread.Start();
float tCpu = 50, pCpu = 0, tGpu = 40, pGpu = 0;
while (true) {
bool gGpu = false;
try {
foreach (LibreIHardware hw in computer.Hardware) {
if (hw.HardwareType != LibreHardwareType.Cpu && hw.HardwareType != LibreHardwareType.GpuNvidia && hw.HardwareType != LibreHardwareType.GpuAmd) continue;
// 如果底层驱动对象因为驱动更新导致句柄无效,Update会抛出异常。
// 此时我们直接让子进程退出,父进程会重新启动一个新的子进程来进行初始化。
try {
hw.Update();
} catch (Exception ex) {
Console.Error.WriteLine("CRASH: Update failed - " + ex.Message);
Environment.Exit(1);
}
foreach (LibreISensor sensor in hw.Sensors) {
try {
if (hw.HardwareType == LibreHardwareType.Cpu) {
if (sensor.SensorType == LibreSensorType.Temperature && (sensor.Name.Contains("Package") || sensor.Name.Contains("Tctl/Tdie")))
tCpu = sensor.Value.GetValueOrDefault();
if (sensor.SensorType == LibreSensorType.Power && sensor.Name.Contains("Package"))
pCpu = sensor.Value.GetValueOrDefault();
} else if (hw.HardwareType == LibreHardwareType.GpuNvidia || hw.HardwareType == LibreHardwareType.GpuAmd) {
if (sensor.SensorType == LibreSensorType.Temperature && sensor.Name == "GPU Core")
tGpu = sensor.Value.GetValueOrDefault();
if (sensor.SensorType == LibreSensorType.Power && sensor.Name == "GPU Package") {
gGpu = true;
pGpu = sensor.Value.GetValueOrDefault();
}
}
} catch { }
}
}
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:F2};{1:F2};{2:F2};{3:F2};{4}", tCpu, pCpu, tGpu, pGpu, gGpu ? 1 : 0));
} catch (Exception ex) {
Console.Error.WriteLine("CRASH: " + ex.Message);
Environment.Exit(1);
}
Thread.Sleep(sleepMs);
}
}
static void StartHardwareMonitor() {
if (hwMonitorProcess != null && !hwMonitorProcess.HasExited) return;
hwMonitorProcess = new Process {
StartInfo = new ProcessStartInfo {
FileName = Application.ExecutablePath,
Arguments = "--hwmonitor",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
}
};
hwMonitorProcess.OutputDataReceived += (s, e) => {
if (string.IsNullOrEmpty(e.Data)) return;
//Debug.WriteLine("[HWMonitor OUT] " + e.Data); // 将子进程输出重定向到VS的输出窗口
if (e.Data.StartsWith("CRASH:")) return;
var parts = e.Data.Split(';');
if (parts.Length == 5) {
if (float.TryParse(parts[0], NumberStyles.Float, CultureInfo.InvariantCulture, out float tc)) rawTempCPU = tc;
if (float.TryParse(parts[1], NumberStyles.Float, CultureInfo.InvariantCulture, out float pc) && pc < 9999) rawPowerCPU = pc;
if (float.TryParse(parts[2], NumberStyles.Float, CultureInfo.InvariantCulture, out float tg)) rawTempGPU = tg;
if (float.TryParse(parts[3], NumberStyles.Float, CultureInfo.InvariantCulture, out float pg)) rawPowerGPU = pg;
rawGotGPU = parts[4] == "1";
// 首次收到数据时,初始化对应传感器的平滑温度
if (!cpuTempReady) {
smoothedCPUTemp = rawTempCPU;
cpuTempReady = true;
}
if (!gpuTempReady) {
smoothedGPUTemp = rawTempGPU;
gpuTempReady = true;
}
if (!tempReady) {
tempReady = true;
}
}
};
hwMonitorProcess.ErrorDataReceived += (s, e) => {
if (string.IsNullOrEmpty(e.Data)) return;
Logger.Error("HardwareMonitor [HWMonitor ERR] " + e.Data);
};
hwMonitorProcess.EnableRaisingEvents = true;
hwMonitorProcess.Exited += (s, e) => {
if (hwMonitorStopping) {
hwMonitorStopping = false;
return;
}
//Logger.Info("StartHardwareMonitor [HWMonitor] 进程退出,准备重启...");
System.Threading.Tasks.Task.Delay(3000).ContinueWith(_ => {
try { StartHardwareMonitor(); } catch { }
});
};
try {
hwMonitorProcess.Start();
hwMonitorIn = hwMonitorProcess.StandardInput;
hwMonitorProcess.BeginOutputReadLine();
hwMonitorProcess.BeginErrorReadLine(); // 必须读取错误流避免死锁
SetGpuMonitorState(monitorGPU);
SetCpuMonitorState(monitorCPU);
SetMonitorInterval(monitorRefreshRate == "high" ? 250 : 1000);
} catch (Exception) { }
}
static void SetGpuMonitorState(bool enable) {
if (hwMonitorIn != null && hwMonitorProcess != null && !hwMonitorProcess.HasExited) {
try { hwMonitorIn.WriteLine(enable ? "GPU:ON" : "GPU:OFF"); } catch { }
}
}
static void SetCpuMonitorState(bool enable) {
if (hwMonitorIn != null && hwMonitorProcess != null && !hwMonitorProcess.HasExited) {
try { hwMonitorIn.WriteLine(enable ? "CPU:ON" : "CPU:OFF"); } catch { }
}
}
static void SetMonitorInterval(int ms) {
if (hwMonitorIn != null && hwMonitorProcess != null && !hwMonitorProcess.HasExited) {
try { hwMonitorIn.WriteLine($"INTERVAL:{ms}"); } catch { }
}
}
static void StopHardwareMonitor() {
if (hwMonitorProcess != null && !hwMonitorProcess.HasExited) {
hwMonitorStopping = true;
try { hwMonitorProcess.Kill(); } catch { hwMonitorStopping = false; }
}
}
static int flagStart = 0;
static void optimiseSchedule() {
// 延时等待风扇恢复响应
if (flagStart < 5) {
flagStart++;
if (fanControl.Contains("max")) {
SetMaxFanSpeedOn();
} else if (fanControl.Contains(" RPM")) {
SetMaxFanSpeedOff();
int rpmValue = int.Parse(fanControl.Replace(" RPM", "").Trim());
SetFanLevel(rpmValue / 100, rpmValue / 100, Is3FanNb);
}
}
//定时通信避免功耗锁定
if (GetFanCount(out bool ocp, out bool otp)) {
if (ocp || otp) {
Logger.Info($"BIOS 保护状态 - 过流: {ocp}, 过温: {otp}");
}
} else {
Logger.Error("无法读取 BIOS 保护状态");
}
//更新显示器连接到显卡状态
monitorQuery();
}
static void OnPowerChange(object s, PowerModeChangedEventArgs e) {
// 休眠重新启动
if (e.Mode == PowerModes.Resume) {
GetFanCount(out bool ocp, out bool otp);
tooltipUpdateTimer.Start();
countRestore = 3;
}
// 检查电源模式是否发生变化
if (e.Mode == PowerModes.StatusChange) {
// 获取当前电源连接状态
var powerStatus = SystemInformation.PowerStatus;
if (powerStatus.PowerLineStatus == PowerLineStatus.Online) {
//Logger.Info("笔记本已连接到电源。");
RestorePowerConfig();
powerOnline = true;
} else {
//Logger.Info("笔记本未连接到电源。");
powerOnline = false;
}
}
}
static void TrayIcon_MouseClick(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
//MainForm.Instance.Show();
//MainForm.Instance.TopMost = true;
//MainForm.Instance.TopMost = false;
}
}
static bool CheckCustomIcon() {
string currentPath = AppDomain.CurrentDomain.BaseDirectory;
string iconPath = Path.Combine(currentPath, "custom.ico");
// 检查图标文件是否存在
if (File.Exists(iconPath)) {
return true;
} else {
MessageBox.Show(Strings.NoCustomIcon, Strings.Hint, MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
}
static void SetCustomIcon() {
string currentPath = AppDomain.CurrentDomain.BaseDirectory;
string iconPath = Path.Combine(currentPath, "custom.ico");
// 检查图标文件是否存在
if (File.Exists(iconPath)) {
trayIcon.Icon = new Icon(iconPath);
} else {
MessageBox.Show(Strings.NoCustomIcon, Strings.Hint, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
// 根据当前监控状态决定动态图标显示内容:
// CPU监控开 → CPU温度;CPU关GPU开 → GPU温度;均关 → 原版图标(不改 customIcon 设置)
static void UpdateDynamicIcon() {
if (customIcon != "dynamic") return;
if (monitorCPU) {
GenerateDynamicIcon((int)CPUTemp);
} else if (monitorGPU) {
GenerateDynamicIcon((int)GPUTemp);
} else {
trayIcon.Icon = Properties.Resources.smallfan;
}
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);
static void GenerateDynamicIcon(int number) {
// 获取系统推荐的图标尺寸(已适配 DPI)
Size iconSize = SystemInformation.IconSize;
int width = iconSize.Width * 2;
int height = iconSize.Height * 2;
using (Bitmap bitmap = new Bitmap(width, height)) {
using (Graphics graphics = Graphics.FromImage(bitmap)) {
graphics.Clear(Color.Transparent);
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
string text = number.ToString("00");
using (Font font = new Font("Arial", 45.5f, FontStyle.Bold)) {
// 测量文本大小
SizeF textSize = graphics.MeasureString(text, font);
// 计算居中位置
float x = (width - textSize.Width) / 2;
float y = (height - textSize.Height) / 8;
// 绘制文本
graphics.DrawString(text, font, Brushes.Tan, x, y);
}
// 转换为图标
IntPtr hIcon = bitmap.GetHicon();
Icon newIcon = Icon.FromHandle(hIcon);
// 替换托盘图标
trayIcon.Icon = newIcon;
// 销毁旧句柄(注意:不能直接销毁,因为 Icon.FromHandle 需要手动释放)
DestroyIcon(hIcon);
}
}
}
// 状态栏定时更新任务+硬件查询+DB解锁
static void UpdateTooltip() {
try {
QueryHardware();
} catch (Exception ex) {
Logger.Error($"[UpdateTooltip] QueryHardware 异常: {ex.Message}");
}
if (monitorFan)
fanSpeedNow = GetFanLevel();
trayIcon.Text = monitorText();
//Console.WriteLine("UpdateTooltip");
// 同步数据到本地txt
SyncDataToTxt();
UpdateFloatingText();
if (customIcon == "dynamic")
UpdateDynamicIcon();
// Debug/Release模式下可能不支持在非UI线程直接修改MenuItem.Text,因此使用Invoke
// 同时只有当 SysInfo 菜单处于展开状态时,才去进行耗时的查询和更新操作,以节省资源
ToolStrip parentStrip = irSensorMenu?.GetCurrentParent();
if (isSysInfoMenuOpen && parentStrip != null) {
if (parentStrip.InvokeRequired) {
parentStrip.Invoke(new System.Action(() => {
irSensorMenu.Text = $"{Strings.SysIRSensor}: {GetSensorTemperature(0)}°C";
ambientSensorMenu.Text = $"{Strings.SysAmbient}: {GetSensorTemperature(1)}°C";
pchSensorMenu.Text = $"{Strings.SysPCH}: {GetSensorTemperature(2)}°C";
vrSensorMenu.Text = $"{Strings.SysVR}: {GetSensorTemperature(3)}°C";
}));
} else {
irSensorMenu.Text = $"{Strings.SysIRSensor}: {GetSensorTemperature(0)}°C";
ambientSensorMenu.Text = $"{Strings.SysAmbient}: {GetSensorTemperature(1)}°C";
pchSensorMenu.Text = $"{Strings.SysPCH}: {GetSensorTemperature(2)}°C";
vrSensorMenu.Text = $"{Strings.SysVR}: {GetSensorTemperature(3)}°C";
}
}
// 启用再禁用DB驱动
if (countDB > 0) {
countDB--;
if (countDB == 0) {
string deviceId = "\"ACPI\\NVDA0820\\NPCF\"";
string command = $"pnputil /disable-device {deviceId}";
ExecuteCommand(command);
float[] limits = GetGpuPowerLimits(); // limits[0] = Current, limits[1] = Max
// 检查显卡当前功耗限制,离电时当作解锁成功
if (powerOnline && Math.Abs(limits[1] - limits[0]) > 1f) {
tryTimes++;
// 失败时重试一次
if (tryTimes == 2) {
tryTimes = 0;
if (CPUPower > CPULimitDB + 10)
MessageBox.Show(Strings.DbUnlockCpuHighWarning, Strings.Hint, MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
MessageBox.Show(Strings.DbUnlockFailed(limits[0]), Strings.Hint, MessageBoxButtons.OK, MessageBoxIcon.Warning);
command = $"pnputil /enable-device {deviceId}";
ExecuteCommand(command);
DBVersion = 2;
countDB = 0;
DBMenu.Enabled = true;
SaveConfig("DBVersion");
UpdateCheckedState("DBGroup", Strings.DbNormal);
} else {
SetGpuPowerState(true, true);
SetCpuPowerLimit((byte)CPULimitDB);
countDB = countDBInit;
}
} else {
tryTimes = 0;
DBMenu.Enabled = true;
if (autoStart == "off") {
MessageBox.Show(Strings.DbUnlockSuccessNoAutoStart, Strings.Hint, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
//MessageBox.Show($"解锁成功!\n当前最大显卡功耗锁定为:{-powerLimits:F2} W !", Strings.Hint, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (tryTimes == 0) {
// 恢复CPU功耗设定
RestoreCPUPower();
// 恢复GPU功耗设定
SetGpuPowerState(tgpPower == "on", ppabPower == "on", dState == "normal" ? 1 : 2);
}
} else if (countDB == countDBInit - 1) {
// 启用DB驱动
string deviceId = "\"ACPI\\NVDA0820\\NPCF\"";
string command = $"pnputil /enable-device {deviceId}";
ExecuteCommand(command);
}
}
// 从休眠中启动后恢复配置
if (countRestore > 0) {
countRestore--;
if (countRestore == 0) {
RestoreConfig();
}
}
}
static void SyncDataToTxt() {
if (dataLocalize != "on") return;
System.Threading.Tasks.Task.Run(() => {
try {
// 获取程序根目录
string basePath = AppDomain.CurrentDomain.BaseDirectory;
// 将浮点数转换为整数并转换为文本
string cpuText = ((int)Math.Round(CPUTemp)).ToString();
string gpuText = ((int)Math.Round(GPUTemp)).ToString();
string fanText;
// brief lock to avoid potential race on the list
lock (fanSpeedNow) {
fanText = ((fanSpeedNow[0] + fanSpeedNow[1]) * 50).ToString();
}
// 仅当与上次内存中保存的值不同时才写入磁盘,避免不必要的 I/O
try {
if (lastCpuText == null || lastCpuText != cpuText) {
File.WriteAllText(Path.Combine(basePath, "cpu_temp.txt"), cpuText);
lastCpuText = cpuText;
}
} catch (Exception ex) {
Logger.Error($"Sync error when writing cpu_temp.txt: {ex.Message}");
}
try {
if (lastGpuText == null || lastGpuText != gpuText) {
File.WriteAllText(Path.Combine(basePath, "gpu_temp.txt"), gpuText);
lastGpuText = gpuText;
}
} catch (Exception ex) {
Logger.Error($"Sync error when writing gpu_temp.txt: {ex.Message}");
}
try {
if (lastFanText == null || lastFanText != fanText) {
File.WriteAllText(Path.Combine(basePath, "fan_rpm.txt"), fanText);
lastFanText = fanText;
}
} catch (Exception ex) {
Logger.Error($"Sync error when writing fan_rpm.txt: {ex.Message}");
}
} catch (Exception ex) {
// 忽略文件被占用的偶发错误,或者在这里记录日志
Logger.Error("Sync error: " + ex.Message);
}
});
}
// 硬件传感器查询
private static int _isQuerying = 0; // 防重入标志,支持 Interlocked 原子操作
static int countQuery = 0;
static bool autoStartMonitorGPU = true, autoStopMonitorGPU = true;//是否自动根据情况开/关GPU温度监测以节约能源
static bool hasStartAuto = false, hasStopAuto = false;//是否已经自动开/关过GPU温度监测,在手动开/关时重置
// 用于风扇查表的平滑温度(受高中低档影响)
static float smoothedCPUTemp = 50f;
static float smoothedGPUTemp = 40f;
static void QueryHardware() {
// 防止定时器重入:上次查询未完成时直接跳过本次
if (Interlocked.CompareExchange(ref _isQuerying, 1, 0) != 0)
return;
float tempCPU = rawTempCPU;
bool getGPU = false;
if (monitorCPU && cpuTempReady) {
CPUPower = rawPowerCPU;