LenovoLegionToolkit

Форк
0
694 строки · 26.1 Кб
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Threading.Tasks;
5
using LenovoLegionToolkit.Lib.Extensions;
6
using LenovoLegionToolkit.Lib.Settings;
7
using LenovoLegionToolkit.Lib.SoftwareDisabler;
8
using LenovoLegionToolkit.Lib.System.Management;
9
using LenovoLegionToolkit.Lib.Utils;
10

11
namespace LenovoLegionToolkit.Lib.Controllers.GodMode;
12

13
public class GodModeControllerV1 : AbstractGodModeController
14
{
15
    public GodModeControllerV1(GodModeSettings settings, VantageDisabler vantageDisabler, LegionZoneDisabler legionZoneDisabler) : base(settings, vantageDisabler, legionZoneDisabler) { }
16

17
    public override Task<bool> NeedsVantageDisabledAsync() => Task.FromResult(false);
18
    public override Task<bool> NeedsLegionZoneDisabledAsync() => Task.FromResult(true);
19

20
    public override async Task ApplyStateAsync()
21
    {
22
        if (await LegionZoneDisabler.GetStatusAsync().ConfigureAwait(false) == SoftwareStatus.Enabled)
23
        {
24
            if (Log.Instance.IsTraceEnabled)
25
                Log.Instance.Trace($"Can't correctly apply state when Legion Zone is running.");
26
            return;
27
        }
28

29
        if (Log.Instance.IsTraceEnabled)
30
            Log.Instance.Trace($"Applying state...");
31

32
        var (presetId, preset) = await GetActivePresetAsync().ConfigureAwait(false);
33

34
        var cpuLongTermPowerLimit = preset.CPULongTermPowerLimit;
35
        var cpuShortTermPowerLimit = preset.CPUShortTermPowerLimit;
36
        var cpuPeakPowerLimit = preset.CPUPeakPowerLimit;
37
        var cpuCrossLoadingPowerLimit = preset.CPUCrossLoadingPowerLimit;
38
        var apuSPPTPowerLimit = preset.APUsPPTPowerLimit;
39
        var cpuTemperatureLimit = preset.CPUTemperatureLimit;
40
        var gpuPowerBoost = preset.GPUPowerBoost;
41
        var gpuConfigurableTgp = preset.GPUConfigurableTGP;
42
        var gpuTemperatureLimit = preset.GPUTemperatureLimit;
43
        var fanTable = preset.FanTable ?? await GetDefaultFanTableAsync().ConfigureAwait(false);
44
        var fanFullSpeed = preset.FanFullSpeed ?? false;
45

46
        if (cpuLongTermPowerLimit is not null)
47
        {
48
            try
49
            {
50
                if (Log.Instance.IsTraceEnabled)
51
                    Log.Instance.Trace($"Applying CPU Long Term Power Limit: {cpuLongTermPowerLimit}");
52

53
                await SetCPULongTermPowerLimitAsync(cpuLongTermPowerLimit.Value.Value).ConfigureAwait(false);
54
            }
55
            catch (Exception ex)
56
            {
57
                if (Log.Instance.IsTraceEnabled)
58
                    Log.Instance.Trace($"Apply failed. [setting=cpuLongTermPowerLimit]", ex);
59
                throw;
60
            }
61
        }
62

63
        if (cpuShortTermPowerLimit is not null)
64
        {
65
            try
66
            {
67
                if (Log.Instance.IsTraceEnabled)
68
                    Log.Instance.Trace($"Applying CPU Short Term Power Limit: {cpuShortTermPowerLimit}");
69

70
                await SetCPUShortTermPowerLimitAsync(cpuShortTermPowerLimit.Value.Value).ConfigureAwait(false);
71
            }
72
            catch (Exception ex)
73
            {
74
                if (Log.Instance.IsTraceEnabled)
75
                    Log.Instance.Trace($"Apply failed. [setting=cpuShortTermPowerLimit]", ex);
76
                throw;
77
            }
78
        }
79

80
        if (cpuPeakPowerLimit is not null)
81
        {
82
            try
83
            {
84
                if (Log.Instance.IsTraceEnabled)
85
                    Log.Instance.Trace($"Applying CPU Peak Power Limit: {cpuPeakPowerLimit}");
86

87
                await SetCPUPeakPowerLimitAsync(cpuPeakPowerLimit.Value.Value).ConfigureAwait(false);
88
            }
89
            catch (Exception ex)
90
            {
91
                if (Log.Instance.IsTraceEnabled)
92
                    Log.Instance.Trace($"Apply failed. [setting=cpuPeakPowerLimit]", ex);
93
                throw;
94
            }
95
        }
96

97
        if (cpuCrossLoadingPowerLimit is not null)
98
        {
99
            try
100
            {
101
                if (Log.Instance.IsTraceEnabled)
102
                    Log.Instance.Trace($"Applying CPU Cross Loading Power Limit: {cpuCrossLoadingPowerLimit}");
103

104
                await SetCPUCrossLoadingPowerLimitAsync(cpuCrossLoadingPowerLimit.Value.Value).ConfigureAwait(false);
105
            }
106
            catch (Exception ex)
107
            {
108
                if (Log.Instance.IsTraceEnabled)
109
                    Log.Instance.Trace($"Apply failed. [setting=cpuCrossLoadingPowerLimit]", ex);
110
                throw;
111
            }
112
        }
113

114
        if (apuSPPTPowerLimit is not null)
115
        {
116
            try
117
            {
118
                if (Log.Instance.IsTraceEnabled)
119
                    Log.Instance.Trace($"Applying APU sPPT Power Limit: {apuSPPTPowerLimit}");
120

121
                await SetAPUSPPTPowerLimitAsync(apuSPPTPowerLimit.Value.Value).ConfigureAwait(false);
122
            }
123
            catch (Exception ex)
124
            {
125
                if (Log.Instance.IsTraceEnabled)
126
                    Log.Instance.Trace($"Apply failed. [setting=apuSPPTPowerLimit]", ex);
127
                throw;
128
            }
129
        }
130

131
        if (cpuTemperatureLimit is not null)
132
        {
133
            try
134
            {
135
                if (Log.Instance.IsTraceEnabled)
136
                    Log.Instance.Trace($"Applying CPU Temperature Limit: {cpuTemperatureLimit}");
137

138
                await SetCPUTemperatureLimitAsync(cpuTemperatureLimit.Value.Value).ConfigureAwait(false);
139
            }
140
            catch (Exception ex)
141
            {
142
                if (Log.Instance.IsTraceEnabled)
143
                    Log.Instance.Trace($"Apply failed. [setting=cpuTemperatureLimit]", ex);
144
                throw;
145
            }
146
        }
147

148
        if (gpuPowerBoost is not null)
149
        {
150
            try
151
            {
152
                if (Log.Instance.IsTraceEnabled)
153
                    Log.Instance.Trace($"Applying GPU Power Boost: {gpuPowerBoost}");
154

155
                await SetGPUPowerBoostAsync(gpuPowerBoost.Value.Value).ConfigureAwait(false);
156
            }
157
            catch (Exception ex)
158
            {
159
                if (Log.Instance.IsTraceEnabled)
160
                    Log.Instance.Trace($"Apply failed. [setting=gpuPowerBoost]", ex);
161
                throw;
162
            }
163
        }
164

165
        if (gpuConfigurableTgp is not null)
166
        {
167
            try
168
            {
169
                if (Log.Instance.IsTraceEnabled)
170
                    Log.Instance.Trace($"Applying GPU Configurable TGP: {gpuConfigurableTgp}");
171

172
                await SetGPUConfigurableTGPAsync(gpuConfigurableTgp.Value.Value).ConfigureAwait(false);
173
            }
174
            catch (Exception ex)
175
            {
176
                if (Log.Instance.IsTraceEnabled)
177
                    Log.Instance.Trace($"Apply failed. [setting=gpuConfigurableTgp]", ex);
178
                throw;
179
            }
180
        }
181

182
        if (gpuTemperatureLimit is not null)
183
        {
184
            try
185
            {
186
                if (Log.Instance.IsTraceEnabled)
187
                    Log.Instance.Trace($"Applying GPU Temperature Limit: {gpuTemperatureLimit}");
188

189
                await SetGPUTemperatureLimitAsync(gpuTemperatureLimit.Value.Value).ConfigureAwait(false);
190
            }
191
            catch (Exception ex)
192
            {
193
                if (Log.Instance.IsTraceEnabled)
194
                    Log.Instance.Trace($"Apply failed. [setting=gpuTemperatureLimit]", ex);
195
                throw;
196
            }
197
        }
198

199
        if (fanFullSpeed)
200
        {
201
            try
202
            {
203
                if (Log.Instance.IsTraceEnabled)
204
                    Log.Instance.Trace($"Applying Fan Full Speed {fanFullSpeed}...");
205

206
                await SetFanFullSpeedAsync(fanFullSpeed).ConfigureAwait(false);
207
            }
208
            catch (Exception ex)
209
            {
210
                if (Log.Instance.IsTraceEnabled)
211
                    Log.Instance.Trace($"Apply failed. [setting=fanFullSpeed]", ex);
212
                throw;
213
            }
214
        }
215
        else
216
        {
217

218
            try
219
            {
220
                if (Log.Instance.IsTraceEnabled)
221
                    Log.Instance.Trace($"Making sure Fan Full Speed is false...");
222

223
                await SetFanFullSpeedAsync(false).ConfigureAwait(false);
224
            }
225
            catch (Exception ex)
226
            {
227
                if (Log.Instance.IsTraceEnabled)
228
                    Log.Instance.Trace($"Apply failed. [setting=fanFullSpeed]", ex);
229
                throw;
230
            }
231

232
            try
233
            {
234
                if (Log.Instance.IsTraceEnabled)
235
                    Log.Instance.Trace($"Applying Fan Table {fanTable}...");
236

237
                if (!await IsValidFanTableAsync(fanTable).ConfigureAwait(false))
238
                {
239
                    if (Log.Instance.IsTraceEnabled)
240
                        Log.Instance.Trace($"Fan table invalid, replacing with default...");
241

242
                    fanTable = await GetDefaultFanTableAsync().ConfigureAwait(false);
243
                }
244

245
                await SetFanTable(fanTable).ConfigureAwait(false);
246
            }
247
            catch (Exception ex)
248
            {
249
                if (Log.Instance.IsTraceEnabled)
250
                    Log.Instance.Trace($"Apply failed. [setting=fanTable]", ex);
251
                throw;
252
            }
253
        }
254

255
        RaisePresetChanged(presetId);
256

257
        if (Log.Instance.IsTraceEnabled)
258
            Log.Instance.Trace($"State applied. [name={preset.Name}, id={presetId}]");
259
    }
260

261
    public override Task<FanTable> GetMinimumFanTableAsync()
262
    {
263
        var fanTable = new FanTable(new ushort[] { 0, 0, 0, 0, 0, 0, 0, 1, 3, 5 });
264
        return Task.FromResult(fanTable);
265
    }
266

267
    public override async Task<Dictionary<PowerModeState, GodModeDefaults>> GetDefaultsInOtherPowerModesAsync()
268
    {
269
        try
270
        {
271
            if (Log.Instance.IsTraceEnabled)
272
                Log.Instance.Trace($"Getting defaults in other power modes...");
273

274
            var result = await GetDefaultValuesInDifferentModeAsync().ConfigureAwait(false);
275

276
            if (Log.Instance.IsTraceEnabled)
277
            {
278
                Log.Instance.Trace($"Defaults in other power modes retrieved:");
279
                foreach (var (powerMode, defaults) in result)
280
                    Log.Instance.Trace($" - {powerMode}: {defaults}");
281
            }
282

283
            return result;
284
        }
285
        catch (Exception ex)
286
        {
287
            if (Log.Instance.IsTraceEnabled)
288
                Log.Instance.Trace($"Failed to get defaults in other power modes.", ex);
289
            return new Dictionary<PowerModeState, GodModeDefaults>();
290
        }
291
    }
292

293
    public override async Task RestoreDefaultsInOtherPowerModeAsync(PowerModeState state)
294
    {
295
        try
296
        {
297
            if (Log.Instance.IsTraceEnabled)
298
                Log.Instance.Trace($"Restoring defaults for {state}...");
299

300
            var result = await GetDefaultValuesInDifferentModeAsync().ConfigureAwait(false);
301

302
            if (!result.TryGetValue(state, out var defaults))
303
            {
304
                if (Log.Instance.IsTraceEnabled)
305
                    Log.Instance.Trace($"Defaults for {state} not found. Skipping...");
306

307
                return;
308
            }
309

310
            if (defaults.CPULongTermPowerLimit is not null)
311
            {
312
                try
313
                {
314
                    if (Log.Instance.IsTraceEnabled)
315
                        Log.Instance.Trace($"Applying CPU Long Term Power Limit: {defaults.CPULongTermPowerLimit}");
316

317
                    await SetCPULongTermPowerLimitAsync(defaults.CPULongTermPowerLimit.Value).ConfigureAwait(false);
318
                }
319
                catch (Exception ex)
320
                {
321
                    if (Log.Instance.IsTraceEnabled)
322
                        Log.Instance.Trace($"Apply failed. [setting=cpuLongTermPowerLimit]", ex);
323
                    throw;
324
                }
325
            }
326

327
            if (defaults.CPUShortTermPowerLimit is not null)
328
            {
329
                try
330
                {
331
                    if (Log.Instance.IsTraceEnabled)
332
                        Log.Instance.Trace($"Applying CPU Short Term Power Limit: {defaults.CPUShortTermPowerLimit}");
333

334
                    await SetCPUShortTermPowerLimitAsync(defaults.CPUShortTermPowerLimit.Value).ConfigureAwait(false);
335
                }
336
                catch (Exception ex)
337
                {
338
                    if (Log.Instance.IsTraceEnabled)
339
                        Log.Instance.Trace($"Apply failed. [setting=cpuShortTermPowerLimit]", ex);
340
                    throw;
341
                }
342
            }
343

344
            if (defaults.CPUPeakPowerLimit is not null)
345
            {
346
                try
347
                {
348
                    if (Log.Instance.IsTraceEnabled)
349
                        Log.Instance.Trace($"Applying CPU Peak Power Limit: {defaults.CPUPeakPowerLimit}");
350

351
                    await SetCPUPeakPowerLimitAsync(defaults.CPUPeakPowerLimit.Value).ConfigureAwait(false);
352
                }
353
                catch (Exception ex)
354
                {
355
                    if (Log.Instance.IsTraceEnabled)
356
                        Log.Instance.Trace($"Apply failed. [setting=cpuPeakPowerLimit]", ex);
357
                    throw;
358
                }
359
            }
360

361
            if (defaults.CPUCrossLoadingPowerLimit is not null)
362
            {
363
                try
364
                {
365
                    if (Log.Instance.IsTraceEnabled)
366
                        Log.Instance.Trace($"Applying CPU Cross Loading Power Limit: {defaults.CPUCrossLoadingPowerLimit}");
367

368
                    await SetCPUCrossLoadingPowerLimitAsync(defaults.CPUCrossLoadingPowerLimit.Value).ConfigureAwait(false);
369
                }
370
                catch (Exception ex)
371
                {
372
                    if (Log.Instance.IsTraceEnabled)
373
                        Log.Instance.Trace($"Apply failed. [setting=cpuCrossLoadingPowerLimit]", ex);
374
                    throw;
375
                }
376
            }
377

378
            if (defaults.APUsPPTPowerLimit is not null)
379
            {
380
                try
381
                {
382
                    if (Log.Instance.IsTraceEnabled)
383
                        Log.Instance.Trace($"Applying APU sPPT Power Limit: {defaults.APUsPPTPowerLimit}");
384

385
                    await SetAPUSPPTPowerLimitAsync(defaults.APUsPPTPowerLimit.Value).ConfigureAwait(false);
386
                }
387
                catch (Exception ex)
388
                {
389
                    if (Log.Instance.IsTraceEnabled)
390
                        Log.Instance.Trace($"Apply failed. [setting=apuSPPTPowerLimit]", ex);
391
                    throw;
392
                }
393
            }
394

395
            if (defaults.CPUTemperatureLimit is not null)
396
            {
397
                try
398
                {
399
                    if (Log.Instance.IsTraceEnabled)
400
                        Log.Instance.Trace($"Applying CPU Temperature Limit: {defaults.CPUTemperatureLimit}");
401

402
                    await SetCPUTemperatureLimitAsync(defaults.CPUTemperatureLimit.Value).ConfigureAwait(false);
403
                }
404
                catch (Exception ex)
405
                {
406
                    if (Log.Instance.IsTraceEnabled)
407
                        Log.Instance.Trace($"Apply failed. [setting=cpuTemperatureLimit]", ex);
408
                    throw;
409
                }
410
            }
411

412
            if (defaults.GPUPowerBoost is not null)
413
            {
414
                try
415
                {
416
                    if (Log.Instance.IsTraceEnabled)
417
                        Log.Instance.Trace($"Applying GPU Power Boost: {defaults.GPUPowerBoost}");
418

419
                    await SetGPUPowerBoostAsync(defaults.GPUPowerBoost.Value).ConfigureAwait(false);
420
                }
421
                catch (Exception ex)
422
                {
423
                    if (Log.Instance.IsTraceEnabled)
424
                        Log.Instance.Trace($"Apply failed. [setting=gpuPowerBoost]", ex);
425
                    throw;
426
                }
427
            }
428

429
            if (defaults.GPUConfigurableTGP is not null)
430
            {
431
                try
432
                {
433
                    if (Log.Instance.IsTraceEnabled)
434
                        Log.Instance.Trace($"Applying GPU Configurable TGP: {defaults.GPUConfigurableTGP}");
435

436
                    await SetGPUConfigurableTGPAsync(defaults.GPUConfigurableTGP.Value).ConfigureAwait(false);
437
                }
438
                catch (Exception ex)
439
                {
440
                    if (Log.Instance.IsTraceEnabled)
441
                        Log.Instance.Trace($"Apply failed. [setting=gpuConfigurableTgp]", ex);
442
                    throw;
443
                }
444
            }
445
        }
446
        catch (Exception ex)
447
        {
448
            if (Log.Instance.IsTraceEnabled)
449
                Log.Instance.Trace($"Failed to restore defaults for {state}.", ex);
450
        }
451
    }
452

453
    protected override async Task<GodModePreset> GetDefaultStateAsync()
454
    {
455
        var fanTableData = await GetFanTableDataAsync().ConfigureAwait(false);
456

457
        var preset = new GodModePreset
458
        {
459
            Name = "Default",
460
            CPULongTermPowerLimit = await GetCPULongTermPowerLimitAsync().OrNullIfException().ConfigureAwait(false),
461
            CPUShortTermPowerLimit = await GetCPUShortTermPowerLimitAsync().OrNullIfException().ConfigureAwait(false),
462
            CPUPeakPowerLimit = await GetCPUPeakPowerLimitAsync().OrNullIfException().ConfigureAwait(false),
463
            CPUCrossLoadingPowerLimit = await GetCPUCrossLoadingPowerLimitAsync().OrNullIfException().ConfigureAwait(false),
464
            APUsPPTPowerLimit = await GetAPUSPPTPowerLimitAsync().OrNullIfException().ConfigureAwait(false),
465
            CPUTemperatureLimit = await GetCPUTemperatureLimitAsync().OrNullIfException().ConfigureAwait(false),
466
            GPUPowerBoost = await GetGPUPowerBoost().OrNullIfException().ConfigureAwait(false),
467
            GPUConfigurableTGP = await GetGPUConfigurableTGPAsync().OrNullIfException().ConfigureAwait(false),
468
            GPUTemperatureLimit = await GetGPUTemperatureLimitAsync().OrNullIfException().ConfigureAwait(false),
469
            FanTableInfo = fanTableData is null ? null : new FanTableInfo(fanTableData, await GetDefaultFanTableAsync().ConfigureAwait(false)),
470
            FanFullSpeed = await GetFanFullSpeedAsync().ConfigureAwait(false),
471
            MinValueOffset = 0,
472
            MaxValueOffset = 0
473
        };
474

475
        if (Log.Instance.IsTraceEnabled)
476
            Log.Instance.Trace($"Default state retrieved: {preset}");
477

478
        return preset;
479
    }
480

481
    #region CPU Long Term Power Limit
482

483
    private static async Task<StepperValue> GetCPULongTermPowerLimitAsync()
484
    {
485
        var defaultValue = await WMI.LenovoCpuMethod.CPUGetDefaultPowerLimitAsync().OrNullIfException().ConfigureAwait(false);
486
        var (value, min, max, step) = await WMI.LenovoCpuMethod.CPUGetLongTermPowerLimitAsync().ConfigureAwait(false);
487
        return new(value, min, max, step, Array.Empty<int>(), defaultValue?.longTerm);
488
    }
489

490
    private static Task SetCPULongTermPowerLimitAsync(int value) => WMI.LenovoCpuMethod.CPUSetLongTermPowerLimitAsync(value);
491

492
    #endregion
493

494
    #region CPU Short Term Power Limit
495

496
    private static async Task<StepperValue> GetCPUShortTermPowerLimitAsync()
497
    {
498
        var defaultValue = await WMI.LenovoCpuMethod.CPUGetDefaultPowerLimitAsync().OrNullIfException().ConfigureAwait(false);
499
        var (value, min, max, step) = await WMI.LenovoCpuMethod.CPUGetShortTermPowerLimitAsync().ConfigureAwait(false);
500
        return new(value, min, max, step, Array.Empty<int>(), defaultValue?.shortTerm);
501
    }
502

503
    private static Task SetCPUShortTermPowerLimitAsync(int value) => WMI.LenovoCpuMethod.CPUSetShortTermPowerLimitAsync(value);
504

505
    #endregion
506

507
    #region CPU Peak Power Limit
508

509
    private static async Task<StepperValue> GetCPUPeakPowerLimitAsync()
510
    {
511
        var (value, min, max, step, defaultValue) = await WMI.LenovoCpuMethod.CPUGetPeakPowerLimitAsync().ConfigureAwait(false);
512
        return new(value, min, max, step, Array.Empty<int>(), defaultValue);
513
    }
514

515
    private static Task SetCPUPeakPowerLimitAsync(int value) => WMI.LenovoCpuMethod.CPUSetPeakPowerLimitAsync(value);
516

517
    #endregion
518

519
    #region CPU Cross Loading Power Limit
520

521
    private static async Task<StepperValue> GetCPUCrossLoadingPowerLimitAsync()
522
    {
523
        var (value, min, max, step, defaultValue) = await WMI.LenovoCpuMethod.CPUGetCrossLoadingPowerLimitAsync().ConfigureAwait(false);
524
        return new(value, min, max, step, Array.Empty<int>(), defaultValue);
525
    }
526

527
    private static Task SetCPUCrossLoadingPowerLimitAsync(int value) => WMI.LenovoCpuMethod.CPUSetCrossLoadingPowerLimitAsync(value);
528

529
    #endregion
530

531
    #region APU sPPT Power Limit
532

533
    private static async Task<StepperValue> GetAPUSPPTPowerLimitAsync()
534
    {
535
        var (value, min, max, step, defaultValue) = await WMI.LenovoCpuMethod.GetAPUSPPTPowerLimitAsync().ConfigureAwait(false);
536
        return new(value, min, max, step, Array.Empty<int>(), defaultValue);
537
    }
538

539
    private static Task SetAPUSPPTPowerLimitAsync(int value) => WMI.LenovoCpuMethod.SetAPUSPPTPowerLimitAsync(value);
540

541
    #endregion
542

543
    #region CPU Temperature Limit
544

545
    private static async Task<StepperValue> GetCPUTemperatureLimitAsync()
546
    {
547
        var (value, min, max, step, defaultValue) = await WMI.LenovoCpuMethod.CPUGetTemperatureControlAsync().ConfigureAwait(false);
548
        return new(value, min, max, step, Array.Empty<int>(), defaultValue);
549
    }
550

551
    private static Task SetCPUTemperatureLimitAsync(int value) => WMI.LenovoCpuMethod.CPUSetTemperatureControlAsync(value);
552

553
    #endregion
554

555
    #region GPU Configurable TGP
556

557
    private static async Task<StepperValue> GetGPUConfigurableTGPAsync()
558
    {
559
        var defaultValue = await WMI.LenovoGpuMethod.GPUGetDefaultPPABcTGPPowerLimit().OrNullIfException().ConfigureAwait(false);
560
        var (value, min, max, step) = await WMI.LenovoGpuMethod.GPUGetCTGPPowerLimitAsync().ConfigureAwait(false);
561
        return new(value, min, max, step, Array.Empty<int>(), defaultValue?.ctgp);
562
    }
563

564
    private static Task SetGPUConfigurableTGPAsync(int value) => WMI.LenovoGpuMethod.GPUSetCTGPPowerLimitAsync(value);
565

566
    #endregion
567

568
    #region GPU Power Boost
569

570
    private static async Task<StepperValue> GetGPUPowerBoost()
571
    {
572
        var defaultValue = await WMI.LenovoGpuMethod.GPUGetDefaultPPABcTGPPowerLimit().OrNullIfException().ConfigureAwait(false);
573
        var (value, min, max, step) = await WMI.LenovoGpuMethod.GPUGetPPABPowerLimitAsync().ConfigureAwait(false);
574
        return new(value, min, max, step, Array.Empty<int>(), defaultValue?.ppab);
575
    }
576

577
    private static Task SetGPUPowerBoostAsync(int value) => WMI.LenovoGpuMethod.GPUSetPPABPowerLimitAsync(value);
578

579
    #endregion
580

581
    #region GPU Temperature Limit
582

583
    private static async Task<StepperValue> GetGPUTemperatureLimitAsync()
584
    {
585
        var (value, min, max, step, defaultValue) = await WMI.LenovoGpuMethod.GPUGetTemperatureLimitAsync().ConfigureAwait(false);
586
        return new(value, min, max, step, Array.Empty<int>(), defaultValue);
587
    }
588

589
    private static Task SetGPUTemperatureLimitAsync(int value) => WMI.LenovoGpuMethod.GPUSetTemperatureLimitAsync(value);
590

591
    #endregion
592

593
    #region Fan Table
594

595
    private static async Task<FanTableData[]?> GetFanTableDataAsync()
596
    {
597
        if (Log.Instance.IsTraceEnabled)
598
            Log.Instance.Trace($"Reading fan table data...");
599

600
        var data = await WMI.LenovoFanTableData.ReadAsync().ConfigureAwait(false);
601

602
        var fanTableData = data
603
            .Select(d => new FanTableData
604
            {
605
                Type = (d.fanId, d.sensorId) switch
606
                {
607
                    (0, 3) => FanTableType.CPU,
608
                    (1, 4) => FanTableType.GPU,
609
                    (0, 0) => FanTableType.CPUSensor,
610
                    _ => FanTableType.Unknown,
611
                },
612
                FanId = d.fanId,
613
                SensorId = d.sensorId,
614
                FanSpeeds = d.fanTableData,
615
                Temps = d.sensorTableData
616
            })
617
            .ToArray();
618

619
        if (fanTableData.Length != 3)
620
        {
621
            if (Log.Instance.IsTraceEnabled)
622
                Log.Instance.Trace($"Bad fan table length: {fanTableData}");
623

624
            return null;
625
        }
626

627
        if (fanTableData.Count(ftd => ftd.FanSpeeds.Length == 10) != 3)
628
        {
629
            if (Log.Instance.IsTraceEnabled)
630
                Log.Instance.Trace($"Bad fan table fan speeds length: {fanTableData}");
631

632
            return null;
633
        }
634

635
        if (fanTableData.Count(ftd => ftd.Temps.Length == 10) != 3)
636
        {
637
            if (Log.Instance.IsTraceEnabled)
638
                Log.Instance.Trace($"Bad fan table temps length: {fanTableData}");
639

640
            return null;
641
        }
642

643
        if (Log.Instance.IsTraceEnabled)
644
            Log.Instance.Trace($"Fan table data: {fanTableData}");
645

646
        return fanTableData;
647
    }
648

649
    private static Task SetFanTable(FanTable fanTable) => WMI.LenovoFanMethod.FanSetTableAsync(fanTable.GetBytes());
650

651
    #endregion
652

653
    #region Fan Full Speed
654

655
    private static Task<bool> GetFanFullSpeedAsync() => WMI.LenovoFanMethod.FanGetFullSpeedAsync();
656

657
    private static Task SetFanFullSpeedAsync(bool enabled) => WMI.LenovoFanMethod.FanSetFullSpeedAsync(enabled ? 1 : 0);
658

659
    #endregion
660

661
    #region Default values
662

663
    private async Task<Dictionary<PowerModeState, GodModeDefaults>> GetDefaultValuesInDifferentModeAsync()
664
    {
665
        var defaultFanTableAsync = await GetDefaultFanTableAsync().ConfigureAwait(false);
666

667
        var result = await WMI.LenovoDefaultValueInDifferentModeData.ReadAsync().ConfigureAwait(false);
668
        return result.Select(d =>
669
            {
670
                var powerMode = (PowerModeState)(d.Mode - 1);
671
                var defaults = new GodModeDefaults
672
                {
673
                    CPULongTermPowerLimit = d.CPULongTermPowerLimit,
674
                    CPUShortTermPowerLimit = d.CPUShortTermPowerLimit,
675
                    CPUPeakPowerLimit = d.CPUPeakPowerLimit,
676
                    CPUCrossLoadingPowerLimit = d.CPUCrossLoadingPowerLimit,
677
                    APUsPPTPowerLimit = d.APUsPPTPowerLimit,
678
                    CPUTemperatureLimit = d.CPUTemperatureLimit,
679
                    GPUPowerBoost = d.GPUPowerBoost,
680
                    GPUConfigurableTGP = d.GPUConfigurableTGP,
681
                    GPUTemperatureLimit = d.GPUTemperatureLimit,
682
                    FanTable = defaultFanTableAsync,
683
                    FanFullSpeed = false
684
                };
685
                return (powerMode, defaults);
686
            })
687
            .Where(d => d.powerMode is PowerModeState.Quiet or PowerModeState.Balance or PowerModeState.Performance)
688
            .DistinctBy(d => d.powerMode)
689
            .ToDictionary(d => d.powerMode, d => d.defaults);
690
    }
691

692
    #endregion
693

694
}
695

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.