Skip to content

Commit ca02e0b

Browse files
committed
use semver library for version parsing
1 parent 8e8c8f2 commit ca02e0b

5 files changed

Lines changed: 31 additions & 17 deletions

File tree

Dat/Objects/Vehicle/VehicleObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public record VehicleObject(
1313
[property: LocoStructOffset(0x00), LocoString, Browsable(false)] string_id Name,
1414
[property: LocoStructOffset(0x02)] TransportMode Mode,
1515
[property: LocoStructOffset(0x03)] VehicleType Type,
16-
[property: LocoStructOffset(0x04)] uint8_t var_04,
16+
[property: LocoStructOffset(0x04)] uint8_t NumCarComponents,
1717
[property: LocoStructOffset(0x05), LocoStructVariableLoad, Browsable(false)] object_id TrackTypeId,
1818
[property: LocoStructOffset(0x06)] uint8_t NumRequiredTrackExtras,
1919
[property: LocoStructOffset(0x07)] uint8_t CostIndex,

Gui/Gui.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@
4949
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
5050
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.2.4" />
5151
<PackageReference Include="Avalonia.ReactiveUI" Version="11.2.4" />
52-
<PackageReference Include="bodong.Avalonia.PropertyGrid" Version="11.2.3.5" />
53-
<PackageReference Include="bodong.PropertyModels" Version="11.2.3.5" />
52+
<PackageReference Include="bodong.Avalonia.PropertyGrid" Version="11.2.4" />
53+
<PackageReference Include="bodong.PropertyModels" Version="11.2.4" />
5454
<PackageReference Include="Material.Icons.Avalonia" Version="2.2.0" />
5555
<PackageReference Include="MessageBox.Avalonia" Version="3.2.0" />
5656
<PackageReference Include="NAudio" Version="2.2.1" />
57+
<PackageReference Include="NuGet.Versioning" Version="6.13.1" />
5758
<PackageReference Include="ReactiveUI.Fody" Version="19.5.41" />
5859
<PackageReference Include="ReactiveUI.Validation" Version="4.1.1" />
5960
<PackageReference Include="System.Text.Json" Version="9.0.2" />

Gui/ViewModels/DatTypes/Objects/VehicleViewModel.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ public class VehicleViewModel : LocoObjectViewModel<VehicleObject>
2222
[Reactive] public VehicleObjectFlags Flags { get; set; }
2323
[Reactive] public S5HeaderViewModel? TrackType { get; set; }
2424
[Reactive] public S5HeaderViewModel? RackRail { get; set; }
25+
[Reactive, Range(0, 4)] public uint8_t NumCarComponents { get; set; }
2526
[Reactive, Length(0, 8)] public BindingList<S5HeaderViewModel> CompatibleVehicles { get; set; }
2627
[Reactive, Length(0, 4)] public BindingList<S5HeaderViewModel> RequiredTrackExtras { get; set; }
2728
[Reactive, Category("Cost"), Range(0, 32)] public uint8_t CostIndex { get; set; }
2829
[Reactive, Category("Cost"), Range(1, int16_t.MaxValue)] public int16_t CostFactor { get; set; }
2930
[Reactive, Category("Cost"), Range(0, 32)] public uint8_t RunCostIndex { get; set; }
3031
[Reactive, Category("Cost"), Range(0, int16_t.MaxValue)] public int16_t RunCostFactor { get; set; }
31-
[Reactive, Category("<unknown>")] public uint8_t var_04 { get; set; }
3232
[Reactive, Category("<unknown>")] public uint8_t var_113 { get; set; }
3333
[Reactive, Category("Sprites")] public CompanyColourType SpecialColourSchemeIndex { get; set; } // called "ColourType" in the loco codebase
3434
[Reactive, Category("Sprites"), Editable(false)] public BindingList<VehicleObjectCar> CarComponents { get; set; }
@@ -55,7 +55,7 @@ public VehicleViewModel(VehicleObject vo)
5555
{
5656
Mode = vo.Mode;
5757
Type = vo.Type;
58-
var_04 = vo.var_04;
58+
NumCarComponents = vo.NumCarComponents;
5959
TrackType = vo.TrackType == null ? null : new(vo.TrackType);
6060
CostIndex = vo.CostIndex;
6161
CostFactor = vo.CostFactor;
@@ -101,7 +101,7 @@ public override VehicleObject GetAsStruct(VehicleObject vo)
101101
{
102102
Mode = Mode,
103103
Type = Type,
104-
var_04 = var_04,
104+
NumCarComponents = NumCarComponents,
105105
TrackType = TrackType?.GetAsUnderlyingType(),
106106
CostIndex = CostIndex,
107107
CostFactor = CostFactor,

Gui/ViewModels/MainWindowViewModel.cs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Avalonia;
22
using Avalonia.Platform;
33
using Avalonia.Platform.Storage;
4+
using NuGet.Versioning;
45
using OpenLoco.Common.Logging;
56
using OpenLoco.Dat;
67
using OpenLoco.Dat.Data;
@@ -66,7 +67,8 @@ public class MainWindowViewModel : ViewModelBase
6667
public string WindowTitle => $"{ObjectEditorModel.ApplicationName} - {ApplicationVersion} ({LatestVersionText})";
6768

6869
[Reactive]
69-
public Version ApplicationVersion { get; set; }
70+
public SemanticVersion ApplicationVersion { get; set; }
71+
public static readonly SemanticVersion UnknownVersion = new(0, 0, 0, "unknown");
7072

7173
[Reactive]
7274
public string LatestVersionText { get; set; } = "Development build";
@@ -291,21 +293,22 @@ async Task LoadLanguage()
291293
}
292294
}
293295

294-
static Version GetCurrentAppVersion(Assembly assembly)
296+
static SemanticVersion GetCurrentAppVersion(Assembly assembly)
295297
{
296-
// grab current appl version from assembly
298+
// grab current app version from assembly
297299
const string versionFilename = "Gui.version.txt";
298300
using (var stream = assembly.GetManifestResourceStream(versionFilename))
299301
using (var ms = new MemoryStream())
300302
{
301303
stream!.CopyTo(ms);
302-
return Version.Parse(Encoding.ASCII.GetString(ms.ToArray()));
304+
var versionText = Encoding.ASCII.GetString(ms.ToArray());
305+
return GetVersionFromText(versionText);
303306
}
304307
}
305308

306309
#if !DEBUG
307310
// thanks for this one @IntelOrca, https://github.com/IntelOrca/PeggleEdit/blob/master/src/peggleedit/Forms/MainMDIForm.cs#L848-L861
308-
Version GetLatestAppVersion()
311+
SemanticVersion GetLatestAppVersion()
309312
{
310313
var client = new HttpClient();
311314
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(GithubApplicationName, ApplicationVersion.ToString()));
@@ -314,11 +317,8 @@ Version GetLatestAppVersion()
314317
{
315318
var jsonResponse = response.Content.ReadAsStringAsync().Result;
316319
var body = JsonSerializer.Deserialize<VersionCheckBody>(jsonResponse);
317-
var tagName = body?.TagName;
318-
if (tagName != null)
319-
{
320-
return Version.Parse(tagName);
321-
}
320+
var versionText = body?.TagName;
321+
return GetVersionFromText(versionText);
322322
}
323323

324324
#pragma warning disable CA2201 // Do not raise reserved exception types
@@ -327,6 +327,19 @@ Version GetLatestAppVersion()
327327
}
328328
#endif
329329

330+
static SemanticVersion GetVersionFromText(string? versionText)
331+
{
332+
if (string.IsNullOrEmpty(versionText))
333+
{
334+
return UnknownVersion;
335+
}
336+
337+
return
338+
SemanticVersion.TryParse(versionText.Trim(), out var version)
339+
? version
340+
: UnknownVersion;
341+
}
342+
330343
public async Task SelectNewFolder()
331344
{
332345
var folders = await PlatformSpecific.OpenFolderPicker();

Tests/LoadSaveTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ void assertFunc(ILocoObject obj, VehicleObject struc) => Assert.Multiple(() =>
998998

999999
Assert.That(struc.Mode, Is.EqualTo(TransportMode.Air), nameof(struc.Mode));
10001000
Assert.That(struc.Type, Is.EqualTo(VehicleType.Aircraft), nameof(struc.Type));
1001-
Assert.That(struc.var_04, Is.EqualTo(1), nameof(struc.var_04));
1001+
Assert.That(struc.NumCarComponents, Is.EqualTo(1), nameof(struc.NumCarComponents));
10021002
// Assert.That(struc.TrackType, Is.EqualTo(0xFF), nameof(struc.TrackType)); // is changed after load from 0 to 255
10031003
Assert.That(struc.NumRequiredTrackExtras, Is.EqualTo(0), nameof(struc.NumRequiredTrackExtras));
10041004
Assert.That(struc.CostIndex, Is.EqualTo(8), nameof(struc.CostIndex));

0 commit comments

Comments
 (0)