Skip to content

Commit

Permalink
Fix Version Comparisons
Browse files Browse the repository at this point in the history
Merge pull request #13 from DavidCarbon/master
  • Loading branch information
Zacam authored Oct 24, 2022
2 parents 1d591e7 + 4640e40 commit 3721f77
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 5 deletions.
105 changes: 104 additions & 1 deletion GameLauncherUpdater/App/Classes/UpdaterCore/Support/Strings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System;
using System.Text;

namespace GameLauncherUpdater.App.Classes.UpdaterCore.Support
{
Expand All @@ -8,5 +9,107 @@ public static string Encode(string Value)
{
return Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(Value));
}
/// <summary>
/// Compare two version strings, e.g. "3.2.1.0.b40" and "3.10.1.a".
/// V1 and V2 can have different number of components.
/// Components must be delimited by dot.
/// </summary>
/// <remarks>
/// Modified Version based off <see href="https://stackoverflow.com/a/68595578/17539426">Stack Overflow</see>
/// </remarks>
/// <param name="v1"></param>
/// <param name="v2"></param>
/// <returns>
/// <b>-1</b> if v1 is lower version number than v2<br></br>
/// <b>0</b> if v1 == v2<br></br>
/// <b>1</b> if v1 is higher version number than v2<br></br>
/// <b>-1000</b> if we couldn't figure it out (something went wrong)
/// </returns>
public static int Comparisons(string v1, string v2)
{
if (string.IsNullOrWhiteSpace(v1) || string.IsNullOrWhiteSpace(v2))
{
throw new ArgumentNullException();
}
else
{
v1 = v1.ToLower();
v2 = v2.ToLower();

if (v1 == v2)
{
return 0;
}
else
{
int rc = -1000;
string[] v1_parts = v1.Split('.');
string[] v2_parts = v2.Split('.');

for (int i = 0; i < v1_parts.Length; i++)
{
if (v2_parts.Length < i + 1)
{
/* we're done here */
break;
}
else
{
string v1_Token = v1_parts[i];
string v2_Token = v2_parts[i];

bool v1_Numeric = int.TryParse(v1_Token, out int oh);
bool v2_Numeric = int.TryParse(v2_Token, out int hi);

/* handle scenario {"2" versus "20"} by prepending zeroes, e.g. it would become {"02" versus "20"} */
if (v1_Numeric && v2_Numeric)
{
while (v1_Token.Length < v2_Token.Length)
{
v1_Token = "0" + v2_Token;
}

while (v2_Token.Length < v1_Token.Length)
{
v2_Token = "0" + v2_Token;
}
}

rc = string.Compare(v1_Token, v2_Token, StringComparison.Ordinal);

if (rc != 0)
{
break;
}
}
}

if (rc == 0)
{
/* catch this scenario: v1="1.0.1" v2="1.0" */
if (v1_parts.Length > v2_parts.Length)
{
/* v1 is higher version than v2 */
rc = 1;
}
/* catch this scenario: v1="1.0" v2="1.0.1" */
else if (v2_parts.Length > v1_parts.Length)
{
/* v1 is lower version than v2 */
rc = -1;
}
}

if (rc == 0 || rc == -1000)
{
return rc;
}
else
{
return rc < 0 ? -1 : 1;
}
}
}
}
}
}
4 changes: 2 additions & 2 deletions GameLauncherUpdater/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
[assembly: ComVisible(false)]
[assembly: Guid("c3dfd242-2688-4228-88e8-d4588cba1833")]

[assembly: AssemblyVersion("1.0.1.16")]
[assembly: AssemblyFileVersion("1.0.1.16")]
[assembly: AssemblyVersion("1.0.1.18")]
[assembly: AssemblyFileVersion("1.0.1.18")]
[assembly: NeutralResourcesLanguage("en-US")]
5 changes: 3 additions & 2 deletions GameLauncherUpdater/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,9 @@ public void DoUpdate()
GitHubReleaseSchema LatestLauncherBuild = (UsingPreview) ?
Insider_Release_Tag(JSONFile, Version) :
new JavaScriptSerializer().Deserialize<GitHubReleaseSchema>(JSONFile);
if ((UsingDevelopment ? Version_Build : Version).CompareTo(LatestLauncherBuild.tag_name) < 0)
int Revision = UsingDevelopment ?
Version_Build.CompareTo(LatestLauncherBuild.tag_name) : Strings.Comparisons(Version, LatestLauncherBuild.tag_name);
if (Revision < 0)
{
WebClient client2 = new WebClient();
client2.Headers.Add("user-agent", "GameLauncherUpdater " + Application.ProductVersion +
Expand Down

0 comments on commit 3721f77

Please sign in to comment.