Skip to content

Commit 8e00712

Browse files
committed
feat: Add latest version information to registry
1 parent 1c4a84d commit 8e00712

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

Source/ORTS.Common/SettingsStore.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ protected static void AssertGetUserValueType(Type expectedType)
5353
Debug.Assert(new[] {
5454
typeof(bool),
5555
typeof(int),
56+
typeof(long),
5657
typeof(DateTime),
5758
typeof(TimeSpan),
5859
typeof(string),
@@ -88,6 +89,13 @@ protected static void AssertGetUserValueType(Type expectedType)
8889
/// <param name="value">value of the setting</param>
8990
public abstract void SetUserValue(string name, int value);
9091

92+
/// <summary>
93+
/// Set a value of a user setting
94+
/// </summary>
95+
/// <param name="name">name of the setting</param>
96+
/// <param name="value">value of the setting</param>
97+
public abstract void SetUserValue(string name, long value);
98+
9199
/// <summary>
92100
/// Set a value of a user setting
93101
/// </summary>
@@ -231,6 +239,16 @@ public override void SetUserValue(string name, int value)
231239
Key.SetValue(name, value, RegistryValueKind.DWord);
232240
}
233241

242+
/// <summary>
243+
/// Set a value of a user setting
244+
/// </summary>
245+
/// <param name="name">name of the setting</param>
246+
/// <param name="value">value of the setting</param>
247+
public override void SetUserValue(string name, long value)
248+
{
249+
Key.SetValue(name, value, RegistryValueKind.QWord);
250+
}
251+
234252
/// <summary>
235253
/// Set a value of a user setting
236254
/// </summary>
@@ -390,6 +408,9 @@ public override object GetUserValue(string name, Type expectedType)
390408
case "int":
391409
userValue = int.Parse(Uri.UnescapeDataString(value[1]), CultureInfo.InvariantCulture);
392410
break;
411+
case "long":
412+
userValue = long.Parse(Uri.UnescapeDataString(value[1]), CultureInfo.InvariantCulture);
413+
break;
393414
case "DateTime":
394415
userValue = DateTime.FromBinary(long.Parse(Uri.UnescapeDataString(value[1]), CultureInfo.InvariantCulture));
395416
break;
@@ -440,6 +461,16 @@ public override void SetUserValue(string name, int value)
440461
NativeMethods.WritePrivateProfileString(Section, name, "int:" + Uri.EscapeDataString(value.ToString(CultureInfo.InvariantCulture)), FilePath);
441462
}
442463

464+
/// <summary>
465+
/// Set a value of a user setting
466+
/// </summary>
467+
/// <param name="name">name of the setting</param>
468+
/// <param name="value">value of the setting</param>
469+
public override void SetUserValue(string name, long value)
470+
{
471+
NativeMethods.WritePrivateProfileString(Section, name, "long:" + Uri.EscapeDataString(value.ToString(CultureInfo.InvariantCulture)), FilePath);
472+
}
473+
443474
/// <summary>
444475
/// Set a value of a user setting
445476
/// </summary>

Source/ORTS.Common/VersionInfo.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,15 @@ public static Version ParseVersion(string version)
132132
// parsedVersion.Build will be -1 if the version only has major and minor, but we need the build number >= 0 here
133133
return new Version(parsedVersion.Major, parsedVersion.Minor, Math.Max(0, parsedVersion.Build), commits);
134134
}
135+
136+
public static long GetVersionLong(Version version)
137+
{
138+
long number = 0;
139+
if (version.Major > 0) number += (long)(version.Major & 0xFFFF) << 48;
140+
if (version.Minor > 0) number += (long)(version.Minor & 0xFFFF) << 32;
141+
if (version.Build > 0) number += (version.Build & 0xFFFF) << 16;
142+
if (version.Revision > 0) number += (version.Revision & 0xFFFF);
143+
return number;
144+
}
135145
}
136146
}

Source/ORTS.Updater/UpdateManager.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ public void Check()
140140
if (Channel == null)
141141
return;
142142

143+
try
144+
{
145+
// Update latest version details in registry
146+
UpdateStoredLatestVersionNumber();
147+
}
148+
catch { }
149+
143150
try
144151
{
145152
// If we're not at the appropriate time for the next check (and we're not forced), we reconstruct the cached update/error and exit.
@@ -193,6 +200,23 @@ public void Check()
193200
}
194201
}
195202

203+
void UpdateStoredLatestVersionNumber()
204+
{
205+
if (Settings.Channel.Length > 0 && VersionInfo.Version.Length > 0)
206+
{
207+
var store = SettingsStore.GetSettingStore(UserSettings.SettingsFilePath, UserSettings.RegistryKey, "Version");
208+
var oldVersion = (long)(store.GetUserValue(Settings.Channel, typeof(long)) ?? 0L);
209+
var newVersion = VersionInfo.GetVersionLong(VersionInfo.ParseVersion(VersionInfo.Version));
210+
if (newVersion > oldVersion)
211+
{
212+
store.SetUserValue(Settings.Channel, newVersion);
213+
store.SetUserValue(Settings.Channel + "_Version", VersionInfo.Version);
214+
store.SetUserValue(Settings.Channel + "_Build", VersionInfo.Build);
215+
store.SetUserValue(Settings.Channel + "_Updated", DateTime.UtcNow);
216+
}
217+
}
218+
}
219+
196220
void ValidateLastUpdate()
197221
{
198222
if (LastUpdate != null)
@@ -550,7 +574,7 @@ static string FormatCertificateForMatching(X509Certificate2 certificate)
550574
var commonName = subjectLines.FirstOrDefault(line => line.StartsWith("CN="));
551575
var country = subjectLines.FirstOrDefault(line => line.StartsWith("C="));
552576
return certificate.Verify() + "\n" + commonName + "\n" + country;
553-
}
577+
}
554578

555579
static List<X509Certificate2> GetCertificatesFromFile(string filename)
556580
{

0 commit comments

Comments
 (0)