Skip to content

Commit

Permalink
- added update notification
Browse files Browse the repository at this point in the history
- update to .net 9
  • Loading branch information
Der-Floh committed Jan 3, 2025
1 parent a910853 commit 064877b
Show file tree
Hide file tree
Showing 12 changed files with 279 additions and 53 deletions.
14 changes: 7 additions & 7 deletions Cursor_Installer_Creator.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net9.0-windows7.0</TargetFramework>
<Nullable>enable</Nullable>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<ApplicationManifest>app.manifest</ApplicationManifest>
Expand All @@ -24,15 +24,15 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia" Version="11.1.0" />
<PackageReference Include="Avalonia.Desktop" Version="11.1.0" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.1.0" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.1.0" />
<PackageReference Include="Avalonia" Version="11.2.3" />
<PackageReference Include="Avalonia.Desktop" Version="11.2.3" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.2.3" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.2.3" />
<!--Condition
below is needed to remove Avalonia.Diagnostics package from build output in Release
configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.0" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.2.3" />
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="System.Drawing.Common" Version="8.0.7" />
<PackageReference Include="System.Drawing.Common" Version="9.0.0" />
</ItemGroup>
</Project>
7 changes: 4 additions & 3 deletions CCursor.cs → Data/CCursor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using Cursor_Installer_Creator.Utils;
using System.IO;

namespace Cursor_Installer_Creator;
namespace Cursor_Installer_Creator.Data;

public enum CCursorType
{
Expand All @@ -16,7 +17,7 @@ public sealed class CCursor

public CCursorType GetCursorType()
{
if (string.IsNullOrEmpty(CursorPath))
if (string.IsNullOrWhiteSpace(CursorPath))
return CCursorType.unknown;

if (CursorPath.EndsWith(".ani"))
Expand Down
4 changes: 2 additions & 2 deletions CursorAssignment.cs → Data/CursorAssignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.IO;
using System.Linq;

namespace Cursor_Installer_Creator;
namespace Cursor_Installer_Creator.Data;

public enum CursorAssignmentType
{
Expand Down Expand Up @@ -40,7 +40,7 @@ public static Dictionary<int, CursorAssignment> ReadCsvString(string fileContent
var dictionary = new Dictionary<int, CursorAssignment>();
foreach (var record in records)
{
if (string.IsNullOrEmpty(record.Name))
if (string.IsNullOrWhiteSpace(record.Name))
continue;
dictionary[record.ID] = record;
}
Expand Down
101 changes: 101 additions & 0 deletions Data/Version.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Linq;

namespace Cursor_Installer_Creator.Data;

public sealed class Version
{
public int Major { get; set; } = -1;
public int Minor { get; set; } = -1;
public int Patch { get; set; } = -1;
public bool IsPrerelease { get; set; }

public Version() { }

public Version(int major, int minor, int patch, bool isPrerelease = false)
{
Major = major;
Minor = minor;
Patch = patch;
IsPrerelease = isPrerelease;
}

public Version(string? version)
{
if (string.IsNullOrWhiteSpace(version))
return;

version = version.ToLower();
version = version.TrimStart('v');

var parts = version.Split('-');
IsPrerelease = parts.Length > 1;

var versionParts = parts[0].Split('.').Select(int.Parse).ToArray();
Major = versionParts[0];
Minor = versionParts[1];
Patch = versionParts[2];
}

public override string ToString() => IsPrerelease ? $"{Major}.{Minor}.{Patch}-alpha" : $"{Major}.{Minor}.{Patch}";

public override bool Equals(object? obj)
{
if (obj is Version otherVersion)
{
return Major == otherVersion.Major
&& Minor == otherVersion.Minor
&& Patch == otherVersion.Patch
&& IsPrerelease == otherVersion.IsPrerelease;
}
return base.Equals(obj);
}

public override int GetHashCode() => HashCode.Combine(Major, Minor, Patch, IsPrerelease);

public static bool operator >(Version v1, Version v2)
{
if (v1.Major != v2.Major)
return v1.Major > v2.Major;
if (v1.Minor != v2.Minor)
return v1.Minor > v2.Minor;
if (v1.Patch != v2.Patch)
return v1.Patch > v2.Patch;
return !v1.IsPrerelease && v2.IsPrerelease;
}

public static bool operator <(Version v1, Version v2)
{
return !(v1 > v2) && !v1.Equals(v2);
}

public static bool operator >=(Version v1, Version v2)
{
return v1 > v2 || v1.Equals(v2);
}

public static bool operator <=(Version v1, Version v2)
{
return v1 < v2 || v1.Equals(v2);
}

public static Version operator +(Version v1, Version v2)
{
return new Version(
v1.Major + v2.Major,
v1.Minor + v2.Minor,
v1.Patch + v2.Patch,
v1.IsPrerelease || v2.IsPrerelease
);
}

public static Version operator -(Version v1, Version v2)
{
return new Version(
Math.Max(v1.Major - v2.Major, 0),
Math.Max(v1.Minor - v2.Minor, 0),
Math.Max(v1.Patch - v2.Patch, 0),
v1.IsPrerelease
);
}
}
35 changes: 9 additions & 26 deletions CursorHelper.cs → Utils/CursorHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Win32;
using Cursor_Installer_Creator.Data;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -11,7 +12,7 @@
using System.Text;
using Cursor = System.Windows.Forms.Cursor;

namespace Cursor_Installer_Creator;
namespace Cursor_Installer_Creator.Utils;

public static class CursorHelper
{
Expand All @@ -28,19 +29,17 @@ public static List<CCursor> GetSelectedCursors()
foreach (var valueName in valueNames)
{
var cursorPath = key.GetValue(valueName)?.ToString();
if (string.IsNullOrEmpty(cursorPath) || !File.Exists(cursorPath))
if (string.IsNullOrWhiteSpace(cursorPath) || !File.Exists(cursorPath))
{
var assignment = CursorAssignment.FromName(valueName, CursorAssignmentType.WindowsReg);
cursorPath = $"C:/Windows/Cursors/{assignment?.Windows}.cur";
cursorPath = @$"C:\Windows\Cursors\{assignment?.Windows}.cur";
}

if (File.Exists(cursorPath))
{
var ccursor = ConvertCursorFile(cursorPath, valueName);
if (ccursor is not null)
{
ccursors.Add(ccursor);
}
}
}
}
Expand All @@ -58,9 +57,7 @@ public static List<CCursor> GetSelectedCursors()
};
ccursor.CursorPath = ConvertCursorFile(ccursor.CursorPath, ccursor.Assignment)?.CursorPath ?? ccursor.CursorPath;
if (File.Exists(ccursor.CursorPath))
{
ccursors.Add(ccursor);
}
}
}

Expand All @@ -74,9 +71,7 @@ public static List<CCursor> GetSelectedCursors()
{
var cursorPath = key.GetValue(assignment.WindowsReg)?.ToString();
if (!File.Exists(cursorPath))
{
cursorPath = @$"C:\Windows\Cursors\{assignment.Windows}.cur";
}

return ConvertCursorFile(cursorPath, assignment.ID);
}
Expand Down Expand Up @@ -108,9 +103,7 @@ private static Dictionary<string, string> ParseInstallerInfStrings(string filePa
var value = parts[1].Trim().TrimStart('\"').TrimEnd('\"');
var assignment = CursorAssignment.FromName(key, CursorAssignmentType.WindowsInstall);
if (assignment is not null)
{
stringsDictionary[assignment.WindowsReg] = value;
}
}
}
}
Expand All @@ -129,9 +122,7 @@ public static IEnumerable<CCursor> CursorsFromInstallerInf(string filePath)
{
var ccursor = ConvertCursorFile(Path.Combine(Path.GetDirectoryName(filePath)!, kvp.Value), assignment);
if (ccursor is not null)
{
ccursors.Add(ccursor);
}
}
}
return ccursors;
Expand Down Expand Up @@ -163,9 +154,7 @@ public static void RemoveCursorDisplayImage(CCursor ccursor)
{
var prevCursorImagePath = Path.ChangeExtension(ccursor.CursorPath, ".png");
if (File.Exists(prevCursorImagePath))
{
File.Delete(prevCursorImagePath);
}
}

public static CCursor? ConvertCursorFile(string cursorPath, int cursorID) => ConvertCursorFile(cursorPath, CursorAssignment.CursorAssignments[cursorID]);
Expand All @@ -188,9 +177,7 @@ public static void RemoveCursorDisplayImage(CCursor ccursor)

var prevCursorImagePath = Path.ChangeExtension(destinationFullPath, ".png");
if (File.Exists(prevCursorImagePath))
{
File.Delete(prevCursorImagePath);
}

var ccursor = new CCursor
{
Expand All @@ -207,24 +194,24 @@ public static void RemoveCursorDisplayImage(CCursor ccursor)
}

[DllImport("User32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr LoadCursorFromFile(string str);
private static extern nint LoadCursorFromFile(string str);
private static Cursor GetCursorFromFile(string filename)
{
var hCursor = LoadCursorFromFile(filename);
return !IntPtr.Zero.Equals(hCursor)
return !nint.Zero.Equals(hCursor)
? new Cursor(hCursor)
: throw new ApplicationException("Could not create cursor from file " + filename);
}

public static void CreateInstaller(string packageName, string folderPath, IEnumerable<CCursor> ccursors, bool createZip = true)
{
using (var writer = new StreamWriter($"{Program.TempPath}/installer.inf"))
using (var writer = new StreamWriter(@$"{Program.TempPath}\installer.inf"))
{
writer.Write(CreateInstallerInfString(packageName, ccursors));
}

var files = ccursors.Select(x => x.CursorPath).ToList();
files.Add($"{Program.TempPath}/installer.inf");
files.Add(@$"{Program.TempPath}\installer.inf");

if (createZip)
{
Expand Down Expand Up @@ -291,9 +278,7 @@ private static string CreateInstallerInfString(string packageName, IEnumerable<C
private static void CreateZipFile(string zipPath, IEnumerable<string> files)
{
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
using var archive = ZipFile.Open(zipPath, ZipArchiveMode.Create);
var folder = Path.GetFileNameWithoutExtension(zipPath);
foreach (var file in files)
Expand Down Expand Up @@ -331,9 +316,7 @@ public static void InstallCursor(string installerFilePath)
catch (System.ComponentModel.Win32Exception ex)
{
if (ex.NativeErrorCode != 1223)
{
throw new Exception(ex.Message + Environment.NewLine + ex.ErrorCode);
}
}
}
}
45 changes: 45 additions & 0 deletions Utils/GitHubUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Cursor_Installer_Creator.Data;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

namespace Cursor_Installer_Creator.Utils;

public sealed class GitHubUpdater
{
public const string RepoOwner = "Der-Floh";
public const string RepoName = "Cursor-Installer-Creator";

public static Version CurrentVersion { get; } = new Version(2, 1, 0);
public static string RepoUrl => $"https://github.com/{RepoOwner}/{RepoName}";
public static string LatestReleaseUrl => $"https://github.com/{RepoOwner}/{RepoName}/releases/latest";

private static readonly HttpClient _client = new HttpClient();

public static async Task<bool> HasUpdateAsync()
{
try
{
// GitHub API endpoint for the latest release
var url = $"https://api.github.com/repos/{RepoOwner}/{RepoName}/releases/latest";
_client.DefaultRequestHeaders.UserAgent.ParseAdd(RepoName.Replace('-', '_'));

// Fetch latest release
var response = await _client.GetAsync(url);
response.EnsureSuccessStatusCode();

// Deserialize response
var jsonString = await response.Content.ReadAsStringAsync();
var jsonDocument = JsonDocument.Parse(jsonString);
var latestVersionString = jsonDocument.RootElement.GetProperty("tag_name").GetString();
var latestVersion = new Version(latestVersionString);

// Compare versions
return latestVersion > CurrentVersion;
}
catch
{
return false;
}
}
}
11 changes: 7 additions & 4 deletions Views/CursorInstallerMainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="10*"/>
<RowDefinition Height="5*"/>
<RowDefinition Height="5*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>

Expand All @@ -23,11 +24,11 @@
<TextBox Name="CursorPackagenameTextBox" Watermark="My Custom Cursor" MinWidth="146" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
</StackPanel>

<views:CursorListView Grid.Row="1" Grid.ColumnSpan="2" Name="CursorListViewElem"/>
<views:CursorListView Grid.Row="1" Grid.RowSpan="2" Grid.ColumnSpan="2" Name="CursorListViewElem"/>

<Button Grid.Row="2" Content="Install Cursor" Cursor="Hand" Click="CursorInstallButton_Click" Margin="8,0,0,0"/>
<Button Grid.Row="3" Content="Install Cursor" Cursor="Hand" Click="CursorInstallButton_Click" Margin="8,0,0,0"/>

<StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,8,0">
<StackPanel Grid.Column="1" Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,8,0">
<TextBlock Name="OperationSuccessTextBlock" Text="Finished" FontWeight="Bold" Foreground="Green" IsVisible="False" VerticalAlignment="Center" Margin="0,0,16,0"/>
<ComboBox Name="CursorPackageTypeComboBox" SelectedIndex="0" Cursor="Hand" MinWidth="196" VerticalAlignment="Center" Margin="0,0,16,0">
<ComboBoxItem Content="Target Folder"/>
Expand All @@ -36,6 +37,8 @@
</ComboBox>
<Button Name="CreateCursorPackageButton" Content="Create Package" Cursor="Hand" Click="CreateCursorPackageButton_Click" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</StackPanel>

<views:UpdateNotifyView Name="UpdateNotifyViewElem" IsVisible="False" Grid.Column="1" Grid.RowSpan="2" VerticalAlignment="Top" HorizontalAlignment="Right"/>
</Grid>

</UserControl>
Loading

0 comments on commit 064877b

Please sign in to comment.