Skip to content

Commit 4b7da73

Browse files
committed
fix vehicle cargo categories not saving correctly
1 parent 797f2e7 commit 4b7da73

File tree

4 files changed

+25
-23
lines changed

4 files changed

+25
-23
lines changed

Dat/Objects/Vehicle/VehicleObject.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public record VehicleObject(
3333
[property: LocoStructOffset(0xDC)] Speed16 RackSpeed,
3434
[property: LocoStructOffset(0xDE)] uint16_t Weight,
3535
[property: LocoStructOffset(0xE0)] VehicleObjectFlags Flags,
36-
[property: LocoStructOffset(0xE2), LocoArrayLength(2), LocoStructVariableLoad] List<uint8_t> MaxCargo,
36+
[property: LocoStructOffset(0xE2), LocoArrayLength(VehicleObject.CompatibleCargoTypesLength), LocoStructVariableLoad] List<uint8_t> MaxCargo,
3737
[property: LocoStructOffset(0xE4), LocoArrayLength(VehicleObject.CompatibleCargoTypesLength), LocoStructVariableLoad, Browsable(false)] List<List<CargoCategory>> CompatibleCargoCategories,
3838
[property: LocoStructOffset(0xEC), LocoArrayLength(VehicleObject.CargoTypeSpriteOffsetsLength), LocoStructVariableLoad] Dictionary<CargoCategory, uint8_t> CargoTypeSpriteOffsets,
3939
[property: LocoStructOffset(0x10C), LocoStructVariableLoad, Browsable(false)] uint8_t _NumSimultaneousCargoTypes,
@@ -163,8 +163,7 @@ public ReadOnlySpan<byte> Load(ReadOnlySpan<byte> remainingData)
163163
continue;
164164
}
165165

166-
var ptr = BitConverter.ToUInt16(remainingData[0..2]);
167-
while (ptr != (uint16_t)CargoCategory.NULL)
166+
while ((CargoCategory)BitConverter.ToUInt16(remainingData[0..2]) != CargoCategory.NULL)
168167
{
169168
var cargoCategory = (CargoCategory)BitConverter.ToUInt16(remainingData[0..2]);
170169
remainingData = remainingData[2..]; // uint16_t
@@ -178,9 +177,6 @@ public ReadOnlySpan<byte> Load(ReadOnlySpan<byte> remainingData)
178177
{
179178
// invalid object - shouldn't have 2 cargo types that are the same
180179
}
181-
182-
// advance ptr
183-
ptr = BitConverter.ToUInt16(remainingData[0..2]);
184180
}
185181

186182
remainingData = remainingData[2..]; // uint16_t, skips the 0xFFFF bytes
@@ -261,7 +257,7 @@ public ReadOnlySpan<byte> Save()
261257
// cargo types
262258
for (var i = 0; i < CompatibleCargoTypesLength; ++i) // CompatibleCargoTypesLength should == CompatibleCargoCategories.Length
263259
{
264-
if (MaxCargo.Count < i)
260+
if (MaxCargo.Count < i || MaxCargo[i] == 0)
265261
{
266262
ms.WriteByte(0); // write a 0 for MaxCargo - this indicates no more cargo and we skip the rest
267263
continue;
@@ -277,8 +273,7 @@ public ReadOnlySpan<byte> Save()
277273
ms.WriteByte(CargoTypeSpriteOffsets[cc]);
278274
}
279275

280-
ms.WriteByte(0xFF);
281-
ms.WriteByte(0xFF);
276+
ms.Write(BitConverter.GetBytes((uint16_t)CargoCategory.NULL));
282277
}
283278

284279
// animation

Dat/Objects/Vehicle/VehicleObjectFlags.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public enum VehicleObjectFlags : uint16_t
1515
// Alternates between sprite 0 and sprite 1 for each vehicle of this type in a train
1616
// NOTE: This is for vehicles and not vehicle components (which can also do similar)
1717
AlternatingCarSprite = 1 << 7,
18-
unk_08 = 1 << 8,
1918
AircraftIsTailDragger = 1 << 8,
2019
AnyRoadType = 1 << 9, // set on all road vehicles except trams
2120
SpeedControl = 1 << 10,

Gui/ViewModels/DatTypes/DatObjectEditorViewModel.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class DatObjectEditorViewModel : BaseLocoFileViewModel
4141
[Reactive]
4242
public UiDatLocoFile? CurrentObject { get; private set; }
4343

44+
public ReactiveCommand<Unit, Unit> ExportUncompressedCommand { get; }
45+
4446
public ReactiveCommand<Unit, Unit> ViewHexCommand { get; }
4547
public Interaction<HexWindowViewModel, HexWindowViewModel?> HexViewerShowDialog { get; }
4648

@@ -52,6 +54,8 @@ public DatObjectEditorViewModel(FileSystemItemObject currentFile, ObjectEditorMo
5254
{
5355
Load();
5456

57+
ExportUncompressedCommand = ReactiveCommand.Create(SaveAsUncompressedDat);
58+
5559
HexViewerShowDialog = new();
5660
_ = HexViewerShowDialog.RegisterHandler(DoShowDialogAsync<HexWindowViewModel, HexViewerWindow>);
5761

@@ -188,15 +192,21 @@ public override void Save()
188192
}
189193

190194
public override void SaveAs()
195+
=> SaveAsCore();
196+
197+
void SaveAsUncompressedDat()
198+
=> SaveAsCore(SawyerEncoding.Uncompressed);
199+
200+
void SaveAsCore(SawyerEncoding? encodingToUse = null)
191201
{
192202
var saveFile = Task.Run(async () => await PlatformSpecific.SaveFilePicker(PlatformSpecific.DatFileTypes)).Result;
193203
if (saveFile != null)
194204
{
195-
SaveCore(saveFile.Path.LocalPath);
205+
SaveCore(saveFile.Path.LocalPath, encodingToUse);
196206
}
197207
}
198208

199-
void SaveCore(string filename)
209+
void SaveCore(string filename, SawyerEncoding? encodingToUse = null)
200210
{
201211
if (CurrentObject?.LocoObject == null)
202212
{
@@ -243,7 +253,7 @@ void SaveCore(string filename)
243253
SawyerStreamWriter.Save(filename,
244254
S5HeaderViewModel?.Name ?? CurrentObject.DatFileInfo.S5Header.Name,
245255
S5HeaderViewModel?.SourceGame ?? CurrentObject.DatFileInfo.S5Header.SourceGame,
246-
ObjectHeaderViewModel?.Encoding ?? SawyerEncoding.Uncompressed,
256+
encodingToUse ?? ObjectHeaderViewModel?.Encoding ?? SawyerEncoding.Uncompressed,
247257
CurrentObject.LocoObject,
248258
logger,
249259
Model.Settings.AllowSavingAsVanillaObject);

Gui/Views/DatObjectEditorView.axaml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
x:Class="OpenLoco.Gui.Views.DatObjectEditorView"
99
x:DataType="vm:DatObjectEditorViewModel">
1010
<DockPanel>
11-
<Grid ColumnDefinitions="*, Auto, 384">
12-
<TabControl Grid.Column="0">
11+
<Grid ColumnDefinitions="*, Auto, 384" RowDefinitions="Auto, *">
12+
<StackPanel Grid.Row="0" Grid.ColumnSpan="3" Orientation="Horizontal">
13+
<Button Margin="4" Command="{Binding ExportUncompressedCommand}" ToolTip.Tip="Equivalent to saving the object with 'Uncompressed' encoding">Export as uncompressed DAT file</Button>
14+
<Button Margin="4" Command="{Binding ViewHexCommand}">Hex Viewer</Button>
15+
</StackPanel>
16+
<TabControl Grid.Column="0" Grid.Row="1">
1317
<TabControl.Styles>
1418
<Style Selector="TabItem">
1519
<Setter Property="Padding" Value="8"/>
@@ -157,8 +161,8 @@
157161
</Border>
158162
</TabItem>
159163
</TabControl>
160-
<GridSplitter Grid.Column="1" />
161-
<TabControl Grid.Column="2">
164+
<GridSplitter Grid.Column="1" Grid.Row="1" />
165+
<TabControl Grid.Column="2" Grid.Row="1">
162166
<TabControl.Styles>
163167
<Style Selector="TabItem">
164168
<Setter Property="Padding" Value="8"/>
@@ -173,12 +177,6 @@
173177
</StackPanel>
174178
</Border>
175179
</TabItem>
176-
<TabItem Header="Hex Dump" >
177-
<StackPanel Orientation="Vertical">
178-
<Button Command="{Binding ViewHexCommand}">Hex Viewer</Button>
179-
<!--<Button Command="{Binding SelectObjectCommand}">Object Selector</Button>-->
180-
</StackPanel>
181-
</TabItem>
182180
</TabControl>
183181
</Grid>
184182
</DockPanel>

0 commit comments

Comments
 (0)