Skip to content

Commit e156cf7

Browse files
committed
add groundwork for multiple dat file types
1 parent 17533c5 commit e156cf7

File tree

6 files changed

+96
-32
lines changed

6 files changed

+96
-32
lines changed

AvaGui/Models/FileSystemItemGroup.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace AvaGui.Models
66
{
7-
87
public abstract record FileSystemItemBase(string Path, string Name, ObservableCollection<FileSystemItemBase>? SubNodes = null)
98
{
109
public string NameComputed
@@ -14,9 +13,13 @@ public string NameComputed
1413
public record FileSystemItem(string Path, string Name, SourceGame SourceGame)
1514
: FileSystemItemBase(Path, Name, null);
1615

16+
public record FileSystemDatGroup(string Path, DatFileType DatFileType, ObservableCollection<FileSystemItemBase> SubNodes)
17+
: FileSystemItemBase(Path, DatFileType.ToString(), SubNodes);
18+
1719
public record FileSystemItemGroup(string Path, ObjectType ObjectType, ObservableCollection<FileSystemItemBase> SubNodes)
1820
: FileSystemItemBase(Path, ObjectType.ToString(), SubNodes);
1921

2022
public record FileSystemVehicleGroup(string Path, VehicleType VehicleType, ObservableCollection<FileSystemItemBase> SubNodes)
2123
: FileSystemItemBase(Path, VehicleType.ToString(), SubNodes);
24+
2225
}

AvaGui/Models/IndexObjectHeader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
namespace AvaGui.Models
66
{
7-
public record IndexObjectHeader(string Name, ObjectType ObjectType, SourceGame SourceGame, uint32_t Checksum, VehicleType? VehicleType);
7+
public record IndexObjectHeader(string Name, DatFileType DatFileType, ObjectType ObjectType, SourceGame SourceGame, uint32_t Checksum, VehicleType? VehicleType);
88
}

AvaGui/Models/ObjectEditorModel.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@
2020

2121
namespace AvaGui.Models
2222
{
23+
public enum DatFileType
24+
{
25+
Object,
26+
Scenario,
27+
SaveGame,
28+
Tutorial,
29+
G1,
30+
Music,
31+
SoundEffect, // css*.dat
32+
Language,
33+
Scores,
34+
}
35+
2336
public class ObjectEditorModel
2437
{
2538
public EditorSettings Settings { get; private set; }
@@ -206,6 +219,7 @@ int LoadAndIndexFile(int count, string filename)
206219
{
207220
_ = ccHeaderIndex.TryAdd(filename, new IndexObjectHeader(
208221
uiLocoFile.DatFileInfo.S5Header.Name,
222+
DatFileType.Object,
209223
uiLocoFile.DatFileInfo.S5Header.ObjectType,
210224
uiLocoFile.DatFileInfo.S5Header.SourceGame,
211225
uiLocoFile.DatFileInfo.S5Header.Checksum,

AvaGui/Models/ObjectTypeToMaterialIconConverter.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Avalonia.Data.Converters;
1+
using Avalonia.Data.Converters;
22
using OpenLoco.ObjectEditor.Data;
33
using OpenLoco.ObjectEditor.Objects;
44
using System;
@@ -11,6 +11,11 @@ public class ObjectTypeToMaterialIconConverter : IValueConverter
1111
{
1212
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1313
{
14+
if (Enum.TryParse<DatFileType>(value as string, out var datType) && DatTypeMapping.TryGetValue(datType, out var datIcon))
15+
{
16+
return datIcon;
17+
}
18+
1419
if (Enum.TryParse<ObjectType>(value as string, out var objType) && ObjectMapping.TryGetValue(objType, out var objectIcon))
1520
{
1621
return objectIcon;
@@ -86,5 +91,18 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
8691
{ SourceGame.DataFile, "File" },
8792
{ SourceGame.Vanilla, "AccountTieHat" },
8893
};
94+
95+
public static Dictionary<DatFileType, string> DatTypeMapping = new Dictionary<DatFileType, string>()
96+
{
97+
{ DatFileType.Object, "Apps" },
98+
{ DatFileType.Scenario, "MapClock" },
99+
{ DatFileType.SaveGame, "ContentSave" },
100+
{ DatFileType.Tutorial, "School" },
101+
{ DatFileType.G1, "ImageAlbum" },
102+
{ DatFileType.Music, "Music" },
103+
{ DatFileType.SoundEffect, "Bugle" },
104+
{ DatFileType.Language, "TranslateVariant" },
105+
{ DatFileType.Scores, "Scoreboard" },
106+
};
89107
}
90108
}

AvaGui/ViewModels/FolderTreeViewModel.cs

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using OpenLoco.ObjectEditor.Data;
1010
using ReactiveUI.Fody.Helpers;
1111
using System.Reactive;
12+
using System.Security.Cryptography;
1213

1314
namespace AvaGui.ViewModels
1415
{
@@ -56,6 +57,12 @@ private void LoadObjDirectory(string directory, bool useExistingIndex)
5657
{
5758
DirectoryItems = new(LoadObjDirectoryCore(directory, useExistingIndex));
5859

60+
// really just for debugging - puts all dat file types in the collection, even if they don't have anything in them
61+
//foreach (var dat in Enum.GetValues<DatFileType>().Except(DirectoryItems.Select(x => ((FileSystemDatGroup)x).DatFileType)))
62+
//{
63+
// DirectoryItems.Add(new FileSystemDatGroup("", dat, new ObservableCollection<FileSystemItemBase>()));
64+
//}
65+
5966
IEnumerable<FileSystemItemBase> LoadObjDirectoryCore(string directory, bool useExistingIndex)
6067
{
6168
if (string.IsNullOrEmpty(directory))
@@ -75,46 +82,60 @@ IEnumerable<FileSystemItemBase> LoadObjDirectoryCore(string directory, bool useE
7582

7683
Model.LoadObjDirectory(directory, null, useExistingIndex);
7784

78-
var groupedObjects = Model.HeaderIndex
85+
var groupedDatObjects = Model.HeaderIndex
7986
.Where(o => (string.IsNullOrEmpty(FilenameFilter) || o.Value.Name.Contains(FilenameFilter, StringComparison.CurrentCultureIgnoreCase)) && (!DisplayVanillaOnly || o.Value.SourceGame == SourceGame.Vanilla))
80-
.GroupBy(o => o.Value.ObjectType)
81-
.OrderBy(fsg => fsg.Key.ToString());
87+
.GroupBy(o => o.Value.DatFileType)
88+
.OrderBy(x => x.Key.ToString());
8289

83-
foreach (var objGroup in groupedObjects)
90+
foreach (var datObjGroup in groupedDatObjects)
8491
{
85-
ObservableCollection<FileSystemItemBase> subNodes; //(objGroup.Select(o => new FileSystemItemBase(o.Key, o.Value.DatFileInfo.S5Header.Name.Trim())));
86-
if (objGroup.Key == ObjectType.Vehicle)
92+
ObservableCollection<FileSystemItemBase> groups = [];
93+
94+
var groupedObjects = datObjGroup
95+
.GroupBy(x => x.Value.ObjectType)
96+
.OrderBy(x => x.Key.ToString());
97+
98+
foreach (var objGroup in groupedObjects)
8799
{
88-
subNodes = [];
89-
foreach (var vg in objGroup
90-
.GroupBy(o => o.Value.VehicleType)
91-
.OrderBy(vg => vg.Key.ToString()))
100+
ObservableCollection<FileSystemItemBase> subNodes;
101+
if (objGroup.Key == ObjectType.Vehicle)
92102
{
93-
var vehicleSubNodes = new ObservableCollection<FileSystemItemBase>(vg.Select(o => new FileSystemItem(o.Key, o.Value.Name.Trim(), o.Value.SourceGame)));
94-
95-
if (vg.Key == null)
103+
subNodes = [];
104+
foreach (var vg in objGroup
105+
.GroupBy(o => o.Value.VehicleType)
106+
.OrderBy(vg => vg.Key.ToString()))
96107
{
97-
// this should be impossible - object says its a vehicle but doesn't have a vehicle type
98-
// todo: move validation into the loading stage or cstr of IndexObjectHeader
99-
continue;
108+
var vehicleSubNodes = new ObservableCollection<FileSystemItemBase>(vg.Select(o => new FileSystemItem(o.Key, o.Value.Name.Trim(), o.Value.SourceGame)));
109+
110+
if (vg.Key == null)
111+
{
112+
// this should be impossible - object says its a vehicle but doesn't have a vehicle type
113+
// todo: move validation into the loading stage or cstr of IndexObjectHeader
114+
continue;
115+
}
116+
117+
subNodes.Add(new FileSystemVehicleGroup(
118+
string.Empty,
119+
vg.Key.Value,
120+
vehicleSubNodes));
100121
}
101-
102-
subNodes.Add(new FileSystemVehicleGroup(
103-
string.Empty,
104-
vg.Key.Value,
105-
vehicleSubNodes));
106122
}
107-
}
108-
else
109-
{
110-
subNodes = new ObservableCollection<FileSystemItemBase>(
111-
objGroup.Select(o => new FileSystemItem(o.Key, o.Value.Name.Trim(), o.Value.SourceGame)));
123+
else
124+
{
125+
subNodes = new ObservableCollection<FileSystemItemBase>(
126+
objGroup.Select(o => new FileSystemItem(o.Key, o.Value.Name.Trim(), o.Value.SourceGame)));
127+
}
128+
129+
groups.Add(new FileSystemItemGroup(
130+
string.Empty,
131+
objGroup.Key,
132+
subNodes));
112133
}
113134

114-
yield return new FileSystemItemGroup(
135+
yield return new FileSystemDatGroup(
115136
string.Empty,
116-
objGroup.Key,
117-
subNodes);
137+
datObjGroup.Key,
138+
groups);
118139
}
119140
}
120141
}

AvaGui/Views/MainWindow.axaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828

2929
<Window.DataTemplates>
3030
<!-- Mind the order of the Templates. Begin with the most specific first. -->
31+
32+
<DataTemplate DataType="mo:FileSystemDatGroup">
33+
<StackPanel Orientation="Horizontal">
34+
<materialIcons:MaterialIcon Kind="{Binding Name, Converter={StaticResource ObjectTypeToMaterialIconConverter}}" Width="24" Height="24" Margin="2" />
35+
<TextBlock VerticalAlignment="Center" Text="{Binding NameComputed}"/>
36+
</StackPanel>
37+
</DataTemplate>
38+
3139
<DataTemplate DataType="mo:FileSystemItemGroup">
3240
<StackPanel Orientation="Horizontal">
3341
<materialIcons:MaterialIcon Kind="{Binding Name, Converter={StaticResource ObjectTypeToMaterialIconConverter}}" Width="24" Height="24" Margin="2" />

0 commit comments

Comments
 (0)