Skip to content

Commit

Permalink
Added support checks for WMI methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszCichecki committed Nov 19, 2021
1 parent 0cea566 commit 7eb8570
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
25 changes: 22 additions & 3 deletions Lib/Features/AbstractWmiFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@

namespace LenovoLegionToolkit.Lib.Features
{
public class AbstractWmiFeature<T> : IFeature<T> where T : struct, IComparable
public abstract class AbstractWmiFeature<T> : IFeature<T> where T : struct, IComparable
{
private readonly string _methodNameSuffix;
private readonly int _offset;
private readonly string _supportMethodName;
private readonly int _supportOffset;

protected AbstractWmiFeature(string methodNameSuffix, int offset)
protected AbstractWmiFeature(string methodNameSuffix, int offset, string supportMethodName = null, int supportOffset = 0)
{
_methodNameSuffix = methodNameSuffix;
_offset = offset;
_supportMethodName = supportMethodName;
_supportOffset = supportOffset;
}

public T GetState() => FromInternal(ExecuteGamezone("Get" + _methodNameSuffix, "Data"));
public T GetState()
{
if (!IsSupported())
throw new NotSupportedException($"Feature {_methodNameSuffix} is not supported.");

return FromInternal(ExecuteGamezone("Get" + _methodNameSuffix, "Data"));
}

public void SetState(T state)
{
Expand All @@ -26,6 +36,15 @@ public void SetState(T state)
});
}

private bool IsSupported()
{
if (_supportMethodName == null)
return true;

var value = ExecuteGamezone(_supportMethodName, "Data");
return value > _supportOffset;
}

private int ToInternal(T state) => (int)(object)state + _offset;

private T FromInternal(int state) => (T)(object)(state - _offset);
Expand Down
2 changes: 1 addition & 1 deletion Lib/Features/HybridModeFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public enum HybridModeState

public class HybridModeFeature : AbstractWmiFeature<HybridModeState>
{
public HybridModeFeature() : base("GSyncStatus", 0) { }
public HybridModeFeature() : base("GSyncStatus", 0, "IsSupportGSync") { }
}
}
2 changes: 1 addition & 1 deletion Lib/Features/OverDriveFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public enum OverDriveState

public class OverDriveFeature : AbstractWmiFeature<OverDriveState>
{
public OverDriveFeature() : base("ODStatus", 0) { }
public OverDriveFeature() : base("ODStatus", 0, "IsSupportOD") { }
}
}
2 changes: 1 addition & 1 deletion Lib/Features/PowerModeFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public static string GetPowerPlanGuid(this PowerModeState state)

public class PowerModeFeature : AbstractWmiFeature<PowerModeState>
{
public PowerModeFeature() : base("SmartFanMode", 1) { }
public PowerModeFeature() : base("SmartFanMode", 1, "IsSupportSmartFan") { }
}
}
2 changes: 1 addition & 1 deletion Lib/Features/TouchpadLockFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public enum TouchpadLockState

public class TouchpadLockFeature : AbstractWmiFeature<TouchpadLockState>
{
public TouchpadLockFeature() : base("TPStatus", 0) { }
public TouchpadLockFeature() : base("TPStatus", 0, "IsSupportDisableTP") { }
}
}

0 comments on commit 7eb8570

Please sign in to comment.