Skip to content

Commit a87a92c

Browse files
authored
Use absolute path when displaying objects (#174)
* make absolute path a requirement for FileSystemItem * cleanup
1 parent 77f4d1d commit a87a92c

File tree

5 files changed

+26
-27
lines changed

5 files changed

+26
-27
lines changed

Dat/ObjectIndex.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public record ObjectIndexEntry(
166166
ObjectSource ObjectSource,
167167
VehicleType? VehicleType = null)
168168
{
169-
public string SimpleText => $"{DatName} | {Filename}";
169+
public string SimpleText
170+
=> $"{DatName} | {Filename}";
170171
}
171172
}

Gui/Models/FileSystemItems.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ public record FileSystemItem(string Filename, string DisplayName, FileLocation F
2424
public record FileSystemItemObject(string Filename, string DisplayName, FileLocation FileLocation, ObjectSource ObjectSource)
2525
: FileSystemItem(Filename, DisplayName, FileLocation);
2626

27-
//public record FileSystemDatGroup(string Path, DatFileType DatFileType, ObservableCollection<FileSystemItemBase> SubNodes)
28-
// : FileSystemItemBase(Path, DatFileType.ToString(), SubNodes);
29-
3027
public record FileSystemItemGroup(string Filename, ObjectType ObjectType, ObservableCollection<FileSystemItemBase> SubNodes)
3128
: FileSystemItemBase(Filename, ObjectType.ToString(), SubNodes);
3229

Gui/ViewModels/DatTypes/DatObjectEditorViewModel.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using Avalonia;
22
using Avalonia.Controls;
33
using Avalonia.Controls.ApplicationLifetimes;
4-
using OpenLoco.Dat;
54
using OpenLoco.Dat.Data;
65
using OpenLoco.Dat.FileParsing;
76
using OpenLoco.Dat.Objects.Sound;
@@ -46,7 +45,7 @@ public class DatObjectEditorViewModel : BaseLocoFileViewModel
4645
public ReactiveCommand<Unit, Unit> ViewHexCommand { get; }
4746
public Interaction<HexWindowViewModel, HexWindowViewModel?> HexViewerShowDialog { get; }
4847

49-
public ReactiveCommand<Unit, ObjectIndexEntry?> SelectObjectCommand { get; }
48+
//public ReactiveCommand<Unit, ObjectIndexEntry?> SelectObjectCommand { get; }
5049
public Interaction<ObjectSelectionWindowViewModel, ObjectSelectionWindowViewModel?> SelectObjectShowDialog { get; }
5150

5251
public DatObjectEditorViewModel(FileSystemItemObject currentFile, ObjectEditorModel model)
@@ -61,20 +60,20 @@ public DatObjectEditorViewModel(FileSystemItemObject currentFile, ObjectEditorMo
6160

6261
ViewHexCommand = ReactiveCommand.CreateFromTask(async () =>
6362
{
64-
var filename = Path.Combine(Model.Settings.ObjDataDirectory, CurrentFile.Filename);
65-
var vm = new HexWindowViewModel(filename, logger);
63+
var vm = new HexWindowViewModel(CurrentFile.Filename, logger);
6664
_ = await HexViewerShowDialog.Handle(vm);
6765
});
6866

6967
SelectObjectShowDialog = new();
7068
_ = SelectObjectShowDialog.RegisterHandler(DoShowDialogAsync<ObjectSelectionWindowViewModel, ObjectSelectionWindow>);
71-
SelectObjectCommand = ReactiveCommand.CreateFromTask(async () =>
72-
{
73-
var objects = model.ObjectIndex.Objects.Where(x => x.ObjectType == ObjectType.Tree);
74-
var vm = new ObjectSelectionWindowViewModel(objects);
75-
var result = await SelectObjectShowDialog.Handle(vm);
76-
return result.SelectedObject;
77-
});
69+
70+
//SelectObjectCommand = ReactiveCommand.CreateFromTask(async () =>
71+
//{
72+
// var objects = model.ObjectIndex.Objects.Where(x => x.ObjectType == ObjectType.Tree);
73+
// var vm = new ObjectSelectionWindowViewModel(objects);
74+
// var result = await SelectObjectShowDialog.Handle(vm);
75+
// return result.SelectedObject;
76+
//});
7877
}
7978

8079
static async Task DoShowDialogAsync<TViewModel, TWindow>(IInteractionContext<TViewModel, TViewModel?> interaction) where TWindow : Window, new()
@@ -168,25 +167,25 @@ public override void Delete()
168167
}
169168

170169
// delete file
171-
var path = Path.Combine(Model.Settings.ObjDataDirectory, CurrentFile.Filename);
172-
if (File.Exists(path))
170+
if (File.Exists(CurrentFile.Filename))
173171
{
174-
logger.Info($"Deleting file \"{path}\"");
175-
File.Delete(path);
172+
logger.Info($"Deleting file \"{CurrentFile.Filename}\"");
173+
File.Delete(CurrentFile.Filename);
176174
}
177175
else
178176
{
179-
logger.Info($"File already deleted \"{path}\"");
177+
logger.Info($"File already deleted \"{CurrentFile.Filename}\"");
180178
}
181179

182-
// remove from object index
183-
Model.ObjectIndex.Delete(x => x.Filename == CurrentFile.Filename);
180+
// note: it is not really possible to delete the entry from the index since if the user
181+
// has changed objdata folders but still has this item tab open, then there is no way
182+
// to delete. user can reindex to fix, or rely on automatic reindex at startup
184183
}
185184

186185
public override void Save()
187186
{
188187
var savePath = CurrentFile.FileLocation == FileLocation.Local
189-
? Path.Combine(Model.Settings.ObjDataDirectory, CurrentFile.Filename)
188+
? CurrentFile.Filename
190189
: Path.Combine(Model.Settings.DownloadFolder, Path.ChangeExtension(CurrentFile.DisplayName, ".dat"));
191190
SaveCore(savePath);
192191
}

Gui/ViewModels/FolderTreeViewModel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ async Task LoadObjDirectoryAsync(string directory, bool useExistingIndex)
180180
await Model.LoadObjDirectoryAsync(directory, Progress, useExistingIndex);
181181
LocalDirectoryItems = ConstructTreeView(
182182
Model.ObjectIndex.Objects.Where(x => (int)x.ObjectType < Limits.kMaxObjectTypes),
183+
Model.Settings.ObjDataDirectory,
183184
FilenameFilter,
184185
DisplayMode,
185186
FileLocation.Local);
@@ -204,13 +205,14 @@ async Task LoadOnlineDirectoryAsync(bool useExistingIndex)
204205
{
205206
OnlineDirectoryItems = ConstructTreeView(
206207
Model.ObjectIndexOnline.Objects.Where(x => (int)x.ObjectType < Limits.kMaxObjectTypes),
208+
Model.Settings.DownloadFolder,
207209
FilenameFilter,
208210
DisplayMode,
209211
FileLocation.Online);
210212
}
211213
}
212214

213-
static List<FileSystemItemBase> ConstructTreeView(IEnumerable<ObjectIndexEntry> index, string filenameFilter, ObjectDisplayMode displayMode, FileLocation fileLocation)
215+
static List<FileSystemItemBase> ConstructTreeView(IEnumerable<ObjectIndexEntry> index, string baseDirectory, string filenameFilter, ObjectDisplayMode displayMode, FileLocation fileLocation)
214216
{
215217
var result = new List<FileSystemItemBase>();
216218

@@ -231,7 +233,7 @@ static List<FileSystemItemBase> ConstructTreeView(IEnumerable<ObjectIndexEntry>
231233
.OrderBy(vg => vg.Key.ToString()))
232234
{
233235
var vehicleSubNodes = new ObservableCollection<FileSystemItemBase>(vg
234-
.Select(o => new FileSystemItemObject(o.Filename, o.DatName, fileLocation, o.ObjectSource))
236+
.Select(o => new FileSystemItemObject(Path.Combine(baseDirectory, o.Filename), o.DatName, fileLocation, o.ObjectSource))
235237
.OrderBy(o => o.DisplayName));
236238

237239
if (vg.Key == null)
@@ -250,7 +252,7 @@ static List<FileSystemItemBase> ConstructTreeView(IEnumerable<ObjectIndexEntry>
250252
else
251253
{
252254
subNodes = new ObservableCollection<FileSystemItemBase>(objGroup
253-
.Select(o => new FileSystemItemObject(o.Filename, o.DatName, fileLocation, o.ObjectSource))
255+
.Select(o => new FileSystemItemObject(Path.Combine(baseDirectory, o.Filename), o.DatName, fileLocation, o.ObjectSource))
254256
.OrderBy(o => o.DisplayName));
255257
}
256258

Gui/Views/MainWindow.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@
386386
<TabItem Margin="0" Padding="0" BorderThickness="0" MaxHeight="32">
387387
<TabItem.Header>
388388
<StackPanel Orientation="Horizontal" Background="{DynamicResource ButtonBackground}">
389-
<TextBlock Text="{Binding CurrentFile.DisplayName}" Margin="4" TextAlignment="Left" VerticalAlignment="Center"/>
389+
<TextBlock Text="{Binding CurrentFile.DisplayName}" Margin="4" TextAlignment="Left" VerticalAlignment="Center" ToolTip.Tip="{Binding CurrentFile.Filename}" />
390390
<Button BorderThickness="0" FontSize="12" VerticalAlignment="Center" Command="{Binding $parent[TabControl].((vm:TabViewPageViewModel)DataContext).RemoveTabCommand}" CommandParameter="{Binding}">X</Button>
391391
</StackPanel>
392392
</TabItem.Header>

0 commit comments

Comments
 (0)