Skip to content

Commit

Permalink
Merge pull request #40 from Nexus-Mods/feat/better-fileversioninfo
Browse files Browse the repository at this point in the history
Update `FileVersionInfo`
  • Loading branch information
erri120 authored Mar 5, 2024
2 parents abbae60 + 118d479 commit d163fe7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
17 changes: 11 additions & 6 deletions src/NexusMods.Paths/FileSystemAbstraction/FileVersionInfo.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
using System;
using JetBrains.Annotations;

namespace NexusMods.Paths;

/// <summary>
/// Represents version information for a file on disk.
/// </summary>
/// <param name="ProductVersion">Gets the version of the product this file is distributed with.</param>
public record struct FileVersionInfo(Version ProductVersion)
/// <param name="FileVersion">Gets the file version number.</param>
[PublicAPI]
public record struct FileVersionInfo(Version ProductVersion, Version FileVersion)
{
private static readonly Version Zero = new(0, 0, 0, 0);

/// <summary>
/// Parses the input as a <see cref="Version"/>.
/// Returns the first non-zero version.
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static Version ParseVersionString(string? input)
public Version GetBestVersion()
{
return input is null ? Version.Parse("1.0.0.0") : Version.Parse(input);
return ProductVersion.Equals(Zero)
? FileVersion
: ProductVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,20 @@ public FileEntry(IFileSystem fileSystem, AbsolutePath path)
public FileVersionInfo GetFileVersionInfo()
{
var fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(_path.GetFullPath());
return new FileVersionInfo(FileVersionInfo.ParseVersionString(fvi.ProductVersion));

var sProductVersion = fvi.ProductVersion;
if (!Version.TryParse(sProductVersion, out var productVersion))
{
productVersion = new Version(fvi.ProductMajorPart, fvi.ProductMinorPart, fvi.ProductBuildPart, fvi.ProductPrivatePart);
}

var sFileVersion = fvi.FileVersion;
if (!Version.TryParse(sFileVersion, out var fileVersion))
{
fileVersion = new Version(fvi.FileMajorPart, fvi.FileMinorPart, fvi.FileBuildPart, fvi.FilePrivatePart);
}

return new FileVersionInfo(productVersion, fileVersion);
}
}
}

0 comments on commit d163fe7

Please sign in to comment.