Skip to content

Commit

Permalink
Merge pull request #3 from Virenbar/v2.4
Browse files Browse the repository at this point in the history
V2.4
  • Loading branch information
Virenbar authored Jun 21, 2024
2 parents deb32be + 09ef71b commit 8115351
Show file tree
Hide file tree
Showing 34 changed files with 3,006 additions and 2,327 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build-artifact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ jobs:
version: ${{steps.version.outputs.version}}
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.x

Expand All @@ -46,7 +46,7 @@ jobs:
run: .\.github\scripts\version.ps1

- name: Restore cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~\.nuget\packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/packages.lock.json') }}
Expand All @@ -62,7 +62,7 @@ jobs:
run: dotnet publish -c Release -p:PublishProfile=$env:PROFILE -f $env:FRAMEWORK /property:Version=$env:VERSION

- name: Upload artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: GDPIControl(${{ matrix.framework }})
path: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-artifact.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
run: echo "Version builded ${{ needs.build.outputs.version }}"

- name: Download artifacts
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4

- name: List files
run: ls -R
Expand All @@ -28,7 +28,7 @@ jobs:
zip -r "GDPIControl-4.7.2.zip" "GDPIControl(net472)"
- name: Create Release
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
with:
files: |
GDPIControl.zip
Expand Down
14 changes: 13 additions & 1 deletion GDPIControl/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ internal static class Config
{
public static ControlSettings Current { get; set; }

private static ControlSettings Default => new() { Modeset = Modeset.M5, GDPISettings = GDPISettings.ModesetSettings(Modeset.M5) };
private static ControlSettings Default => new()
{
Modeset = Modeset.M5,
CustomSettings1 = new GDPISettings(),
CustomSettings2 = new GDPISettings(),
CustomSettings3 = new GDPISettings()
};

public static void Load()
{
Expand All @@ -20,6 +26,12 @@ public static void Load()
var XS = new XmlSerializer(typeof(ControlSettings));
using var SR = new StreamReader(Constants.ConfigPath);
Current = (ControlSettings)XS.Deserialize(SR);

var def = Default;
if (Current.Modeset == Modeset.Custom) { Current.Modeset = Modeset.Custom1; }
Current.CustomSettings1 ??= Current.GDPISettings ?? def.CustomSettings1;
Current.CustomSettings2 ??= def.CustomSettings2;
Current.CustomSettings3 ??= def.CustomSettings3;
}
catch (Exception)
{
Expand Down
14 changes: 9 additions & 5 deletions GDPIControl/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ internal static class Constants
public static string BlacklistPath => Path.Combine(StartupPath, BlacklistName);
public static string BlacklistTempPath => Path.Combine(StartupPath, BlacklistTempName);
public static string ConfigPath => Path.Combine(StartupPath, ConfigName);

/// <summary>
/// .NET 6 uses TEMP directory for GDPI
/// </summary>
public static string GDPIPath => Path.Combine(Application.StartupPath, Environment.Is64BitOperatingSystem ? x86_64 : x86);
public static string UserlistPath => Path.Combine(StartupPath, UserlistName);

#region StartupPath
/*
.NET 6 uses TEMP directory for GDPIControl
Application.StartupPath - Real executable path
Environment.ProcessPath - Packed executable path
*/

#if NET6_0_OR_GREATER
public static string StartupPath => Path.GetDirectoryName(Environment.ProcessPath);
#else
public static string StartupPath => Application.StartupPath;
#endif
public static string UserlistPath => Path.Combine(StartupPath, UserlistName);
#endregion StartupPath
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Windows.Forms;

namespace GDPIControl
namespace GDPIControl.Extensions
{
internal static class Extensions
internal static class FormExtensions
{
public static DialogResult ShowError(this Form form, Exception exception)
{
Expand Down
86 changes: 86 additions & 0 deletions GDPIControl/Extensions/IOExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace GDPIControl.Extensions
{
internal static class IOExtensions
{
public static Task CopyToAsync(this Stream source, Stream destination, int bufferSize) => CopyToAsync(source, destination, bufferSize, default, default);

public static Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress<long> progress) => CopyToAsync(source, destination, bufferSize, progress, default);

/// <summary>
/// https://stackoverflow.com/a/46497896
/// </summary>
public static async Task CopyToAsync(this Stream source, Stream destination, int bufferSize, IProgress<long> progress, CancellationToken cancellationToken)
{
if (source is null) { throw new ArgumentNullException(nameof(source)); }
if (!source.CanRead) { throw new ArgumentException("Has to be readable", nameof(source)); }
if (destination is null) { throw new ArgumentNullException(nameof(destination)); }
if (!destination.CanWrite) { throw new ArgumentException("Has to be writable", nameof(destination)); }
if (bufferSize < 0) { throw new ArgumentOutOfRangeException(nameof(bufferSize)); }

var buffer = new byte[bufferSize];
long totalBytesRead = 0;
int bytesRead;
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
totalBytesRead += bytesRead;
progress?.Report(totalBytesRead);
}
}

/// <summary>
/// Асинхронно скачать файл
/// </summary>
public static Task DownloadAsync(this HttpClient client, string requestUri, Stream destination) => DownloadAsync(client, requestUri, destination, default(CancellationToken));

/// <summary>
/// Асинхронно скачать файл
/// </summary>
public static async Task DownloadAsync(this HttpClient client, string requestUri, Stream destination, CancellationToken cancellationToken)
{
// Считать только заголовок
using var response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
response.EnsureSuccessStatusCode();
// А содержимое записать прямо в файл
using var download = await response.Content.ReadAsStreamAsync();
await download.CopyToAsync(destination);
}

/// <summary>
/// Асинхронно скачать файл с отчётом о прогрессе
/// </summary>
public static Task DownloadAsync(this HttpClient client, string requestUri, Stream destination, IProgress<float> progress) => DownloadAsync(client, requestUri, destination, progress, default);

/// <summary>
/// Асинхронно скачать файл с отчётом о прогрессе
/// </summary>
public static async Task DownloadAsync(this HttpClient client, string requestUri, Stream destination, IProgress<float> progress, CancellationToken cancellationToken)
{
// Get the http headers first to examine the content length
using var response = await client.GetAsync(requestUri, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
response.EnsureSuccessStatusCode();
var contentLength = response.Content.Headers.ContentLength;
using var download = await response.Content.ReadAsStreamAsync();
// Ignore progress reporting when no progress reporter was
// passed or when the content length is unknown
if (!contentLength.HasValue)
{
await download.CopyToAsync(destination);
progress.Report(1);
return;
}

// Convert absolute progress (bytes downloaded) into relative progress (0% - 100%)
var relativeProgress = new Progress<long>(totalBytes => progress.Report((float)totalBytes / contentLength.Value));
// Use extension method to report progress while downloading
await download.CopyToAsync(destination, 81920, relativeProgress, cancellationToken);
progress.Report(1);
}
}
}
Loading

0 comments on commit 8115351

Please sign in to comment.