Skip to content

Commit

Permalink
Add debug reminder if version number doesn't match
Browse files Browse the repository at this point in the history
Show message when debugging if VERSION variable doesn't match assembly info
  • Loading branch information
ThioJoe committed Sep 9, 2024
1 parent 8cf00d8 commit 16eed2c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
13 changes: 13 additions & 0 deletions MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
Expand All @@ -15,6 +16,7 @@ namespace F_Key_Sender
{
public partial class MainForm : Form
{
const string VERSION = "1.1.0";

// Dictionary to store virtual key codes and scan codes. Will want to use wscan codes for SendInput
private static readonly Dictionary<string, (ushort vk, ushort scan)> keyCodes = new Dictionary<string, (ushort, ushort)>
Expand Down Expand Up @@ -57,6 +59,11 @@ public MainForm()
this.TopMost = true;
stopwatch.Start();
dropdownMethod.SelectedIndex = 0;

VerifyAssemblyVersion();

// Set Version Label
labelVersion.Text = $"Version: {VERSION}";
}

// Using WaitHandle instead of Thread.Sleep to avoid wasting system resources and also more accurate
Expand Down Expand Up @@ -1006,5 +1013,42 @@ private void buttonCustomInfo_Click(object sender, EventArgs e)
MessageBoxIcon.None
);
}

// If in debug mode and the assembly version does not match the file version, display a warning message
private void VerifyAssemblyVersion()
{
#if DEBUG
var assembly = Assembly.GetExecutingAssembly();
var assemblyVersion = assembly.GetName().Version.ToString();
var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
var fileVersion = new Version(fileVersionInfo.FileVersion).ToString();

// Ignore the last digit of the version number if it's 0 and the file version doesn't use it by counting periods in the file version
if (assemblyVersion.EndsWith(".0") && VERSION.Count(c => c == '.') < 3)
{
assemblyVersion = assemblyVersion.Substring(0, assemblyVersion.Length - 2);
}
if (fileVersion.EndsWith(".0") && VERSION.Count(c => c == '.') < 3)
{
fileVersion = fileVersion.Substring(0, fileVersion.Length - 2);
}

if (assemblyVersion != VERSION || fileVersion != VERSION)
{
string warningMessage = $"WARNING: Version mismatch detected!\n" +
$"Expected version: {VERSION}\n" +
$"Assembly version: {assemblyVersion}\n" +
$"File version: {fileVersion}";

Debug.WriteLine(warningMessage);
MessageBox.Show(warningMessage, "Version Mismatch",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
Debug.WriteLine($"Version check passed. Current version: {VERSION}");
}
#endif
}
}
}

0 comments on commit 16eed2c

Please sign in to comment.