Skip to content

Commit

Permalink
Merge pull request #5 from reagcz/develop
Browse files Browse the repository at this point in the history
Merge fixes into master
  • Loading branch information
reagcz authored Nov 17, 2022
2 parents e2319b6 + c8c9360 commit dac018a
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 46 deletions.
74 changes: 74 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Release

on:
workflow_dispatch:
inputs:
version:
description: 'Version'
required: true

jobs:
build:
runs-on: windows-latest

env:
App_Packages_Archive: IdeapadToolkit.zip
App_Publish_Directory: bin\Release\net6.0-windows10.0.20348.0\publish\win-x64
App_Project_Directory: IdeapadToolkit\
Solution_Path: IdeapadToolkit.sln
Project_Path: IdeapadToolkit\IdeapadToolkit.csproj
Actions_Allow_Unsecure_Commands: true # Allows AddPAth and SetEnv commands
Configuration: Release
RuntimeIdentifier: win-x64

steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0 # avoid shallow clone so nbgv can do its work.

# Install the .NET Core workload
- name: Install .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: '6.0.x'

# Add MsBuild to the PATH: https://github.com/microsoft/setup-msbuild
- name: Setup MSBuild.exe
uses: microsoft/[email protected]

# Restore the application
- name: Restore the Wpf application to populate the obj folder
run: msbuild $env:Solution_Path /t:Restore /p:Configuration=$env:Configuration /p:RuntimeIdentifier=$env:RuntimeIdentifier

# Build the Windows Application Packaging project for Prod_Store
- name: Build using the publish profile
run: dotnet publish /p:Configuration=Release /p:PublishProfile=FolderProfile

# Create archive
- name: Create archive
run: Compress-Archive -Path $env:App_Project_Directory\$env:App_Publish_Directory\* -DestinationPath $env:App_Project_Directory\$env:App_Publish_Directory\$env:App_Packages_Archive

# Create the release: https://github.com/actions/create-release
- name: Create release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
with:
tag_name: ${{ github.event.inputs.version }}
release_name: Release ${{ github.event.inputs.version }}
draft: false
prerelease: false

# Upload release asset: https://github.com/actions/upload-release-asset
- name: Update release asset
id: upload-release-asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
asset_path: ${{ env.App_Project_Directory }}\${{ env.App_Publish_Directory }}\${{ env.App_Packages_Archive }}
asset_name: ${{ env.App_Packages_Archive }}
asset_content_type: application/zip
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ publish/
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
# *.pubxml
*.publishproj

# Microsoft Azure Web App publish settings. Comment the next line if you want to
Expand Down
24 changes: 22 additions & 2 deletions IdeapadToolkit/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Serilog;
using System.Windows;
using Serilog.Core;

namespace IdeapadToolkit
{
Expand All @@ -23,9 +25,21 @@ namespace IdeapadToolkit
public partial class App : Application
{
private Container _container;
private ILogger _logger;

public static void ConfigureServices(Container container)
public void ConfigureServices(Container container)
{
// For later use
var logLevel = new LoggingLevelSwitch();
logLevel.MinimumLevel = Serilog.Events.LogEventLevel.Error;
var loggerConfig = new LoggerConfiguration()
.MinimumLevel.ControlledBy(logLevel)
.WriteTo.File("log.txt", rollingInterval: RollingInterval.Day, retainedFileCountLimit: 7, levelSwitch: logLevel);
var logger = _logger = loggerConfig.CreateLogger();

container.RegisterInstance(logger);
container.RegisterInstance(logLevel);

container.RegisterSingleton<INavigationService, NavigationService>();
container.RegisterSingleton<ILenovoPowerSettingsService, LenovoPowerSettingsService>();
container.RegisterSingleton<IUEFISettingsService, UEFISettingsService>();
Expand All @@ -46,6 +60,7 @@ protected override void OnStartup(StartupEventArgs e)
base.OnStartup(e);
Container container = _container = new Container();
ConfigureServices(container);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
bool exists = Process.GetProcessesByName(Path.GetFileNameWithoutExtension(Environment.ProcessPath)).Length > 1;
if (exists && !e.Args.Contains("ignoreRunning"))
{
Expand All @@ -67,12 +82,17 @@ protected override void OnStartup(StartupEventArgs e)
{
ShowMainWindow(null, null);
}

var iconview = _container.GetInstance<TrayIconView>();
iconview.MakeVisible();
iconview.TrayIconClicked += ShowMainWindow;
}

private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
_logger.Fatal((Exception)e.ExceptionObject, "Unhandled exception");
}

public void ShowMainWindow(object? sender, EventArgs? e)
{
if (App.Current.MainWindow == null)
Expand Down
4 changes: 3 additions & 1 deletion IdeapadToolkit/IdeapadToolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.1.0" />
<PackageReference Include="ModernWpfUI" Version="0.9.7-preview.2" />
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="SimpleInjector" Version="5.4.1" />
</ItemGroup>

Expand All @@ -34,7 +36,7 @@

<ItemGroup>
<None Update="PowerBattery.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

Expand Down
18 changes: 18 additions & 0 deletions IdeapadToolkit/Properties/PublishProfiles/FolderProfile.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\net6.0-windows10.0.20348.0\publish\win-x64\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net6.0-windows10.0.20348.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<SelfContained>false</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
</Project>
42 changes: 26 additions & 16 deletions IdeapadToolkit/Services/LenovoPowerSettingsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,54 @@ namespace IdeapadToolkit.Services
{
public class LenovoPowerSettingsService : ILenovoPowerSettingsService
{
[DllImport("PowerBattery.dll", EntryPoint = "#142", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?SetITSMode@CIntelligentCooling@PowerBattery@@QEAAHAEAW4ITSMode@12@@Z", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int SetITSMode(ref CIntelligentCooling var1, ref PowerPlan var2);


[DllImport("PowerBattery.dll", EntryPoint = "#8", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "??0CIntelligentCooling@PowerBattery@@QEAA@XZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern CIntelligentCooling CIntelligentCooling(ref CIntelligentCooling var1);


[DllImport("PowerBattery.dll", EntryPoint = "#82", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?GetITSMode@CIntelligentCooling@PowerBattery@@QEAAHAEAHAEAW4ITSMode@12@@Z", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int GetITSMode(ref CIntelligentCooling var1, ref int var2, ref PowerPlan var3);


[DllImport("PowerBattery.dll", EntryPoint = "#6", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "??0CChargingMode@PowerBattery@@QEAA@XZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern CChargingMode CChargingMode(ref CChargingMode var1);


[DllImport("PowerBattery.dll", EntryPoint = "#74", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?GetChargingMode@CChargingMode@PowerBattery@@QEBAHXZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int GetChargingMode(ref CChargingMode var1);


[DllImport("PowerBattery.dll", EntryPoint = "#139", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?SetChargingMode@CChargingMode@PowerBattery@@QEBAHH@Z", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int SetChargingMode(ref CChargingMode var1, int var2);

[DllImport("PowerBattery.dll", EntryPoint = "#16", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?SetChargingMode@CChargingMode@PowerBattery@@QEBAHH_N@Z", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
// I don't know what the newly added boolean does, but it doesn't seem to matter
internal static extern int SetChargingModeFallBack(ref CChargingMode var1, int var2, bool var3);

[DllImport("PowerBattery.dll", EntryPoint = "??0CUSBCharger@PowerBattery@@QEAA@XZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern CUSBCharger CUSBCharger(ref CUSBCharger var1);

[DllImport("PowerBattery.dll", EntryPoint = "#14", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "??0CUSBBatteryCharger@PowerBattery@@QEAA@XZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern CUSBBatteryCharger CUSBBatteryCharger(ref CUSBBatteryCharger var1);

[DllImport("PowerBattery.dll", EntryPoint = "#107", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?OpenOrClose@CUSBBatteryCharger@PowerBattery@@UEAAHXZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int OpenOrClose(ref CUSBBatteryCharger var1);

[DllImport("PowerBattery.dll", EntryPoint = "#108", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?OpenOrClose@CUSBCharger@PowerBattery@@UEAAHXZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int OpenOrClose(ref CUSBCharger var1);

[DllImport("PowerBattery.dll", EntryPoint = "#106", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?OpenFeature@CUSBCharger@PowerBattery@@UEAAHXZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int OpenFeature(ref CUSBCharger var1);

[DllImport("PowerBattery.dll", EntryPoint = "#105", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?OpenFeature@CUSBBatteryCharger@PowerBattery@@UEAAHXZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int OpenFeature(ref CUSBBatteryCharger var1);

[DllImport("PowerBattery.dll", EntryPoint = "#53", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?CloseFeature@CUSBCharger@PowerBattery@@UEAAHXZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int CloseFeature(ref CUSBCharger var1);

[DllImport("PowerBattery.dll", EntryPoint = "#52", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
[DllImport("PowerBattery.dll", EntryPoint = "?CloseFeature@CUSBBatteryCharger@PowerBattery@@UEAAHXZ", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
internal static extern int CloseFeature(ref CUSBBatteryCharger var1);


Expand Down Expand Up @@ -90,7 +93,14 @@ public void SetChargingMode(ChargingMode chargingMode)
{
CChargingMode instance = new();
instance = CChargingMode(ref instance);
_ = SetChargingMode(ref instance, (int)chargingMode);
try
{
_ = SetChargingMode(ref instance, (int)chargingMode);
}
catch (SystemException)
{
_ = SetChargingModeFallBack(ref instance, (int)chargingMode, false);
}
}

public bool IsAlwaysOnUsbEnabled()
Expand Down
17 changes: 13 additions & 4 deletions IdeapadToolkit/Services/UEFISettingsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using IdeapadToolkitService.Helpers;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -14,8 +15,13 @@ public class UEFISettingsService : IUEFISettingsService
private static readonly string Guid = "{D743491E-F484-4952-A87D-8D5DD189B70C}";
private static readonly string ScopeName = "FBSWIF";
private static readonly int ScopeAttribute = 7;
private readonly ILogger _logger;

private static bool SetPrivilege(bool enable)
public UEFISettingsService(ILogger logger)
{
this._logger = logger;
}
private bool SetPrivilege(bool enable)
{
try
{
Expand All @@ -37,8 +43,9 @@ private static bool SetPrivilege(bool enable)
return false;
}
}
catch (Exception)
catch (Exception ex)
{
_logger.Error(ex, "Exception while getting UEFI privilege");
return false;
}
return true;
Expand Down Expand Up @@ -68,8 +75,9 @@ public bool GetFlipToBootStatus()
dataFromUefi = lastWin32Error * -1;
}
}
catch (Exception)
catch (Exception ex)
{
_logger.Error(ex, "Exception while getting UEFI FlipToBoot status");
}
finally
{
Expand Down Expand Up @@ -100,8 +108,9 @@ public int SetFlipToBootStatus(bool newStatus)
var res = (Win32.SetFirmwareEnvironmentVariableExW(ScopeName, Guid, ref structure, Marshal.SizeOf<LenovoFlipToBootSwInterface>(), ScopeAttribute));
if (res != 0) return 0;
}
catch (Exception)
catch (Exception ex)
{
_logger.Error(ex, "Error while setting UEFI FlipToBoot status");
}
finally { _ = SetPrivilege(false); }
return num1;
Expand Down
Loading

0 comments on commit dac018a

Please sign in to comment.