Skip to content

Commit

Permalink
Implement SafeVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
Drommedhar committed Nov 10, 2024
1 parent 6833a97 commit d661c2e
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 10 deletions.
19 changes: 19 additions & 0 deletions DLSSUpdater.Tests/DLSSUpdater.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0-windows</TargetFrameworks>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DlssUpdater\DLSSUpdater.csproj" />
</ItemGroup>

</Project>
87 changes: 87 additions & 0 deletions DLSSUpdater.Tests/Helpers/SafeVersionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using System;
using DLSSUpdater.Helpers;
using NUnit.Framework;

namespace DLSSUpdater.Tests.Helpers;

[TestFixture]
[TestOf(typeof(SafeVersion))]
public class SafeVersionTest
{
[Test]
public void StringToSafeVersion()
{
var version = "1.2.3.4";
var safeVersion = new SafeVersion(version);
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4"));

version = "10.20.30.40";
safeVersion = new SafeVersion(version);
Assert.That(safeVersion.ToString(), Is.EqualTo("10.20.30.40"));

version = "1.2.3.4.u";
safeVersion = new SafeVersion(version);
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4"));

version = "1-2-3-4";
safeVersion = new SafeVersion(version);
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4"));

version = "1a2ab3ac4aa";
safeVersion = new SafeVersion(version);
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4"));

version = "xx1a-2abx3ac*4aa+";
safeVersion = new SafeVersion(version);
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4"));

version = "xx1a-2abx3ac*4aa+5rtuz;";
safeVersion = new SafeVersion(version);
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.4"));
}

[Test]
public void VersionToSafeVersion()
{
var version = new Version(1, 2, 3,4);
var safeVersion = new SafeVersion(version);
Assert.That((Version)safeVersion, Is.EqualTo(version));

version = new Version(1, 0, 0, 1);
safeVersion = new SafeVersion(version);
Assert.That((Version)safeVersion, Is.EqualTo(version));

version = new Version(1, 0);
safeVersion = new SafeVersion(version);
Assert.That((Version)safeVersion, Is.EqualTo(version));

version = new Version(1, 0, 0, 0);
safeVersion = new SafeVersion(version);
Assert.That((Version)safeVersion, Is.EqualTo(version));

version = new Version(1, 2, 3, 0);
safeVersion = new SafeVersion(version);
Assert.That((Version)safeVersion, Is.EqualTo(version));

version = new Version(1, 2, 3);
safeVersion = new SafeVersion(version);
Assert.That((Version)safeVersion, Is.EqualTo(version));

version = new Version(1, 2);
safeVersion = new SafeVersion(version);
Assert.That((Version)safeVersion, Is.EqualTo(version));
}

[Test]
public void CheckIfVersionIsSafe()
{
var safeVersion = new SafeVersion("1");
Assert.That(safeVersion.ToString(), Is.EqualTo("1.0.0.0"));

safeVersion = new SafeVersion("1.2");
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.0.0"));

safeVersion = new SafeVersion("1.2.3");
Assert.That(safeVersion.ToString(), Is.EqualTo("1.2.3.0"));
}
}
6 changes: 6 additions & 0 deletions DLSSUpdater.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ VisualStudioVersion = 17.9.34607.119
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DLSSUpdater", "DlssUpdater\DLSSUpdater.csproj", "{891674A2-74FE-4E71-A18E-859680E25E98}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DLSSUpdater.Tests", "DLSSUpdater.Tests\DLSSUpdater.Tests.csproj", "{43C6713F-2567-4400-A617-0684C22F987C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand All @@ -15,6 +17,10 @@ Global
{891674A2-74FE-4E71-A18E-859680E25E98}.Debug|x64.Build.0 = Debug|x64
{891674A2-74FE-4E71-A18E-859680E25E98}.Release|x64.ActiveCfg = Release|x64
{891674A2-74FE-4E71-A18E-859680E25E98}.Release|x64.Build.0 = Release|x64
{43C6713F-2567-4400-A617-0684C22F987C}.Debug|x64.ActiveCfg = Debug|Any CPU
{43C6713F-2567-4400-A617-0684C22F987C}.Debug|x64.Build.0 = Debug|Any CPU
{43C6713F-2567-4400-A617-0684C22F987C}.Release|x64.ActiveCfg = Release|Any CPU
{43C6713F-2567-4400-A617-0684C22F987C}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
18 changes: 18 additions & 0 deletions DLSSUpdater.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AAssert_002Ecs_002Fl_003AC_0021_003FUsers_003Fdomin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F53539022a55f40bd95b3200c33b231c074e00_003F65_003F7669dcf3_003FAssert_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AEnumerable_002Ecs_002Fl_003AC_0021_003FUsers_003Fdomin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fcca5cfb955e146648d91eb22ffe4627a84930_003Fa0_003Ff6ea3ed0_003FEnumerable_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AFileSystemEnumerator_002EWindows_002Ecs_002Fl_003AC_0021_003FUsers_003Fdomin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F422299c1e1233f1a20b8c0654281f74792424fe87e29abaf4ecfcd0cd64d718_003FFileSystemEnumerator_002EWindows_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AVersion_002Ecs_002Fl_003AC_0021_003FUsers_003Fdomin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003Fbda1a8aceb88de87254065a2ded795c3953da7c42a269ae5bbb443973798b75_003FVersion_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:Boolean x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/ShowAdvancedOptions/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/TestProjectMapping/=891674A2_002D74FE_002D4E71_002DA18E_002D859680E25E98/@EntryIndexedValue">DLSSUpdater.Tests</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/CreateUnitTestDialog/TestTemplateMapping/=NUnit3x/@EntryIndexedValue">db4927dd-2e12-48a7-9a84-2b7e3e31b9c8</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=35466c2e_002Df322_002D4413_002D8d9b_002D0b19d0251add/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="StringToSafeVersion" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;NUnit3x::43C6713F-2567-4400-A617-0684C22F987C::net8.0-windows::DLSSUpdater.Tests.Helpers.SafeVersionTest&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String>




</wpf:ResourceDictionary>
151 changes: 151 additions & 0 deletions DlssUpdater/Helpers/SafeVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System.Text.RegularExpressions;

namespace DLSSUpdater.Helpers;

public partial class SafeVersion : IComparable<SafeVersion>, IEquatable<SafeVersion?>, ICloneable, IComparable
{
public int Major => _versionParts.Count > 0 ? _versionParts[0] : 0;
public int Minor => _versionParts.Count > 1 ? _versionParts[1] : 0;
public int Build => _versionParts.Count > 2 ? _versionParts[2] : 0;
public int Revision => _versionParts.Count > 3 ? _versionParts[3] : 0;

private readonly List<int> _versionParts = [];

public SafeVersion(string? version)
{
if (string.IsNullOrEmpty(version))
{
return;
}

// We need to parse all elements that might be there
// Use regex to extract digits and group into version format
_versionParts = VersionRegex().Matches(version)
.Select(m => int.Parse(m.Value))
.ToList();
}

public SafeVersion(Version? version)
{
if (version == null)
{
return;
}

FillParts(version.Major);
FillParts(version.Minor);
FillParts(version.Build);
FillParts(version.Revision);
}

private SafeVersion(SafeVersion version)
{
foreach (var part in version._versionParts)
{
_versionParts.Add(part);
}
}

public void AddVersionPart(int part)
{
_versionParts.Add(part);
}

public int CompareTo(SafeVersion? other)
{
return other == null ? 1 : ((Version)this).CompareTo((Version)other);
}

public int CompareTo(object? obj)
{
return CompareTo(obj as SafeVersion);
}

public bool Equals(SafeVersion? other)
{
return other != null && ((Version)this).Equals((Version)other);
}

public object Clone()
{
return new SafeVersion(this);
}

public override bool Equals(object? obj)
{
return Equals(obj as SafeVersion);
}

public static bool operator ==(SafeVersion? v1, SafeVersion? v2)
{
// Test "right" first to allow branch elimination when inlined for null checks (== null)
// so it can become a simple test
if (v2 is null)
{
return v1 is null;
}

// Quick reference equality test prior to calling the virtual Equality
return ReferenceEquals(v2, v1) || v2.Equals(v1);
}

public static bool operator !=(SafeVersion? v1, SafeVersion? v2) => !(v1 == v2);

public static bool operator <(SafeVersion? v1, SafeVersion? v2)
{
if (v1 is null)
{
return v2 is not null;
}

return v1.CompareTo(v2) < 0;
}

public static bool operator <=(SafeVersion? v1, SafeVersion? v2)
{
if (v1 is null)
{
return true;
}

return v1.CompareTo(v2) <= 0;
}

public static bool operator >(SafeVersion? v1, SafeVersion? v2) => v2 < v1;

public static bool operator >=(SafeVersion? v1, SafeVersion? v2) => v2 <= v1;

public override int GetHashCode()
{
return HashCode.Combine(_versionParts);
}

public override string ToString()
{
return $"{Major}.{Minor}.{Build}.{Revision}";
}

public static implicit operator Version(SafeVersion version)
{
return version._versionParts.Count switch
{
2 => new Version(version.Major, version.Minor),
3 => new Version(version.Major, version.Minor, version.Build),
4 => new Version(version.Major, version.Minor, version.Build, version.Revision),
_ => new Version()
};
}

private void FillParts(int part)
{
if (part == -1)
{
return;
}

_versionParts.Add(part);
}

[GeneratedRegex(@"\d+")]
private static partial Regex VersionRegex();
}
16 changes: 6 additions & 10 deletions DlssUpdater/Singletons/DllUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.Json.Serialization;
using DlssUpdater.Defines;
using DlssUpdater.Helpers;
using DLSSUpdater.Helpers;
using Microsoft.Security.Extensions;
using NLog;
using static DlssUpdater.Defines.DlssTypes;
Expand Down Expand Up @@ -71,8 +72,8 @@ public bool IsNewerVersionAvailable(DllType type)
OnlinePackages.TryGetValue(type, out var highestOnline) && highestInstalled.FirstOrDefault() != null
&& highestOnline.FirstOrDefault() != null)
{
return new Version(highestInstalled.FirstOrDefault()!.Version) <
new Version(highestOnline.FirstOrDefault()!.Version);
return new SafeVersion(highestInstalled.FirstOrDefault()!.Version) <
new SafeVersion(highestOnline.FirstOrDefault()!.Version);
}

return false;
Expand All @@ -83,8 +84,8 @@ public bool IsNewerVersionAvailable(DllType type, InstalledPackage installed)
if (InstalledPackages.TryGetValue(type, out var highestInstalled)
&& highestInstalled.FirstOrDefault(p => p.Version != DefaultVersion) != null)
{
return new Version(installed.Version) <
new Version(highestInstalled.FirstOrDefault(p => p.Version != DefaultVersion)!.Version);
return new SafeVersion(installed.Version) <
new SafeVersion(highestInstalled.FirstOrDefault(p => p.Version != DefaultVersion)!.Version);
}

return true;
Expand Down Expand Up @@ -188,12 +189,7 @@ public async Task UpdateDlssVersionsAsync()
}

var fileInfo = FileVersionInfo.GetVersionInfo(dllPath);
var versionString = fileInfo.FileVersion!.Replace(',', '.');
var versionParsed = Version.Parse(versionString);
if (versionParsed.Revision == -1)
{
versionParsed = new Version(versionParsed.Major, versionParsed.Minor, versionParsed.Build, 0);
}
var versionParsed = new SafeVersion(fileInfo.FileVersion!);

if (!InstalledPackages.TryGetValue(dllType, out var value))
{
Expand Down

0 comments on commit d661c2e

Please sign in to comment.