Skip to content

Commit

Permalink
Add new update api (#409)
Browse files Browse the repository at this point in the history
* Add new update api
  • Loading branch information
manuelmayer-dev authored Jun 8, 2023
1 parent a95c63b commit cc3ab76
Show file tree
Hide file tree
Showing 29 changed files with 1,360 additions and 1,199 deletions.
2 changes: 1 addition & 1 deletion MacroDeck.Setup/Macro Deck.iss
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ Name: "{autodesktop}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; Tasks: de

[Run]
Filename: "{tmp}\VC_redist.x64.exe"; StatusMsg: "Installing VC2019 redist..."; Parameters: "/quiet"; Check: VC2019RedistNeedsInstall ; Flags: waituntilterminated
Filename: "{app}\{#MyAppExeName}"; Parameters: "--show"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent
Filename: "{app}\{#MyAppExeName}"; Parameters: "--show"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall

7 changes: 6 additions & 1 deletion MacroDeck.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ VisualStudioVersion = 17.3.32811.315
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MacroDeck", "MacroDeck\MacroDeck.csproj", "{119E86B4-9738-4684-AF88-CDBBF0315BBF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MacroDeck.Tests", "MacroDeck.Tests\MacroDeck.Tests.csproj", "{49F1CAC2-158D-45AC-B07C-52394B10A7E2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MacroDeck.Tests", "MacroDeck.Tests\MacroDeck.Tests.csproj", "{49F1CAC2-158D-45AC-B07C-52394B10A7E2}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{A87511C0-A46B-40E0-A47A-81AEE2097F5D}"
ProjectSection(SolutionItems) = preProject
.github\workflows\build-installer.yml = .github\workflows\build-installer.yml
.github\workflows\tests.yml = .github\workflows\tests.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MacroDeck.Setup", "MacroDeck.Setup", "{8C1FC7A7-364F-4CE5-B59D-9A7EFC88CFDC}"
ProjectSection(SolutionItems) = preProject
MacroDeck.Setup\Macro Deck.iss = MacroDeck.Setup\Macro Deck.iss
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
73 changes: 73 additions & 0 deletions MacroDeck/DataTypes/Core/Version.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Text.RegularExpressions;

namespace SuchByte.MacroDeck.DataTypes.Core;

public partial struct Version
{
public int Major { get; set; }
public int Minor { get; set; }
public int Patch { get; set; }
public int? BetaNo { get; set; }

public Version(int major, int minor, int patch, int? betaNo = null)
{
Major = major;
Minor = minor;
Patch = patch;
BetaNo = betaNo;
}

public bool IsBetaVersion => BetaNo.HasValue;

public string VersionName => BetaNo.HasValue
? $"{Major}.{Minor}.{Patch}-b{BetaNo}"
: $"{Major}.{Minor}.{Patch}";

public override string ToString()
{
return VersionName;
}

public static bool TryParse(string versionString, out Version result)
{
try
{
result = Parse(versionString);
return true;
}
catch
{
result = default;
return false;
}
}

public static Version Parse(string? versionString)
{
if (string.IsNullOrWhiteSpace(versionString))
{
throw new FormatException("Version string was empty");
}

var match = VersionRegex().Match(versionString);
if (!match.Success)
{
throw new FormatException("Invalid version string");
}

var major = int.Parse(match.Groups["major"].Value);
var minor = int.Parse(match.Groups["minor"].Value);
var patch = int.Parse(match.Groups["patch"].Value);

int? previewNo = null;
if (match.Groups["beta"].Success)
{
previewNo = int.Parse(match.Groups["beta"].Value);
}

return new Version(major, minor, patch, previewNo);
}

[GeneratedRegex("^(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+)(-b(?<beta>\\d+))?$")]
private static partial Regex VersionRegex();
}
9 changes: 9 additions & 0 deletions MacroDeck/DataTypes/FileDownloader/DownloadProgressInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SuchByte.MacroDeck.DataTypes.FileDownloader;

public class DownloadProgressInfo
{
public int Percentage { get; set; } = 0;
public long TotalBytes { get; set; } = 0;
public long DownloadedBytes { get; set; } = 0;
public double DownloadSpeed { get; set; } = 0;
}
7 changes: 7 additions & 0 deletions MacroDeck/DataTypes/Updater/UpdateApiCheckResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SuchByte.MacroDeck.DataTypes.Updater;

public class UpdateApiCheckResult
{
public bool? NewerVersionAvailable { get; set; }
public UpdateApiVersionInfo? Version { get; set; }
}
8 changes: 8 additions & 0 deletions MacroDeck/DataTypes/Updater/UpdateApiVersionFileInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SuchByte.MacroDeck.DataTypes.Updater;

public class UpdateApiVersionFileInfo
{
public string? DownloadUrl { get; set; }
public string? FileHash { get; set; }
public long FileSize { get; set; }
}
11 changes: 11 additions & 0 deletions MacroDeck/DataTypes/Updater/UpdateApiVersionInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using SuchByte.MacroDeck.Enums;

namespace SuchByte.MacroDeck.DataTypes.Updater;

public class UpdateApiVersionInfo
{
public string? Version { get; set; }
public bool? IsBeta { get; set; }
public string? ChangeNotesUrl { get; set; }
public Dictionary<PlatformIdentifier, UpdateApiVersionFileInfo>? Platforms { get; set; }
}
8 changes: 8 additions & 0 deletions MacroDeck/DataTypes/Updater/UpdateServiceProgress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SuchByte.MacroDeck.DataTypes.Updater;

public class UpdateServiceProgress
{
public int Percentage { get; set; } = 0;
public long TotalBytes { get; set; } = 0;
public long DownloadedBytes { get; set; } = 0;
}
11 changes: 11 additions & 0 deletions MacroDeck/Enums/PlatformIdentifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SuchByte.MacroDeck.Enums;

public enum PlatformIdentifier
{
WinX64 = 1000,
MacX64 = 2000,
MacArm64 = 2100,
LinuxX64 = 3000,
LinuxArm64 = 3100,
LinuxArm32 = 3110
}
15 changes: 15 additions & 0 deletions MacroDeck/Extension/DoubleExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuchByte.MacroDeck.Extension;

public static class DoubleExtensions
{
public static double ConvertBytesToMegabytes(this double bytes)
{
return bytes / 1024.0f / 1024.0f;
}
}
15 changes: 15 additions & 0 deletions MacroDeck/Extension/LongExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuchByte.MacroDeck.Extension;

public static class LongExtensions
{
public static double ConvertBytesToMegabytes(this long bytes)
{
return bytes / 1024f / 1024f;
}
}
32 changes: 32 additions & 0 deletions MacroDeck/Extension/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.IO;
using System.Security.Cryptography;
using System;

namespace SuchByte.MacroDeck.Extension;

public static class StreamExtensions
{
public static async ValueTask<string> CalculateSha256Hash(this Stream stream)
{
stream.Position = 0;

var bufferedStream = new BufferedStream(stream);
using var sha256 = SHA256.Create();

var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await bufferedStream.ReadAsync(buffer)) > 0)
{
sha256.TransformBlock(buffer, 0, bytesRead, buffer, 0);
}
sha256.TransformFinalBlock(buffer, 0, 0);

if (sha256.Hash == null)
{
throw new InvalidOperationException("Hash was null");
}

stream.Position = 0;
return BitConverter.ToString(sha256.Hash).Replace("-", "").ToLower();
}
}
24 changes: 24 additions & 0 deletions MacroDeck/Extension/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Security.Cryptography;

namespace SuchByte.MacroDeck.Extension;

public static class StringExtensions
{
public static bool EqualsCryptographically(this string? str1, string? str2)
{
if (str1 == null || str2 == null)
{
return false;
}

var hash1 = SHA256.HashData(Encoding.UTF8.GetBytes(str1));
var hash2 = SHA256.HashData(Encoding.UTF8.GetBytes(str2));

if (hash1.Length != hash2.Length)
{
return false;
}

return !hash1.Where((t, i) => t != hash2[i]).Any();
}
}
5 changes: 4 additions & 1 deletion MacroDeck/GUI/CustomControls/ButtonPrimary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public Image Icon

public bool Spinner = false;

public bool WriteProgress { get; set; } = true;


//This method begins the animation.
public void AnimateImage()
Expand Down Expand Up @@ -242,13 +244,14 @@ protected override void OnPaint(PaintEventArgs pe)
Region = new Region(pathSurface);
pe.Graphics.DrawPath(penSurface, pathSurface);
var flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
if (progress > 0)
if (progress > 0 && WriteProgress)
{
TextRenderer.DrawText(pe.Graphics, string.Format("{0}%", progress), Font, ClientRectangle, ForeColor, flags);
} else
{
TextRenderer.DrawText(pe.Graphics, text, Font, ClientRectangle, ForeColor, flags);
}

if (Spinner)
{
AnimateImage();
Expand Down
Loading

0 comments on commit cc3ab76

Please sign in to comment.