mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-19 15:02:15 +01:00
Rework the depot, warehouse and vehicle base
A consist added to the warehouse gets a name there. The crew type stays through loading a consist. The vehicle base window replaces the texture base. The depot follows consists changed on the scenarios page, also with the warehouse tab left open.
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Avalonia.Input;
|
||||
@@ -40,6 +39,9 @@ public static class Shortcuts
|
||||
public const string ClearConsist = "consist.clear";
|
||||
public const string SetNumber = "vehicle.number";
|
||||
public const string FocusSearch = "search";
|
||||
public const string Undo = "consist.undo";
|
||||
public const string Redo = "consist.redo";
|
||||
public const string SaveToScenery = "consist.savescenery";
|
||||
|
||||
public static readonly IReadOnlyList<Shortcut> All = new[]
|
||||
{
|
||||
@@ -64,6 +66,12 @@ public static class Shortcuts
|
||||
Key.Delete, KeyModifiers.Control),
|
||||
new Shortcut(SetNumber, "Ctrl+N", "ShortcutSetNumber", ShortcutScope.Depot,
|
||||
Key.N, KeyModifiers.Control),
|
||||
new Shortcut(Undo, "Ctrl+Z", "ShortcutUndo", ShortcutScope.Depot,
|
||||
Key.Z, KeyModifiers.Control),
|
||||
new Shortcut(Redo, "Ctrl+Y", "ShortcutRedo", ShortcutScope.Depot,
|
||||
Key.Y, KeyModifiers.Control),
|
||||
new Shortcut(SaveToScenery, "Ctrl+S", "SaveToScenery", ShortcutScope.Depot,
|
||||
Key.S, KeyModifiers.Control),
|
||||
|
||||
new Shortcut("consist.prevnext", "← →", "ShortcutPrevNext", ShortcutScope.Consist),
|
||||
new Shortcut("consist.firstlast", "Home / End", "ShortcutFirstLast", ShortcutScope.Consist),
|
||||
|
||||
@@ -29,8 +29,11 @@ public sealed class FizPhysicsRepository : IPhysicsRepository
|
||||
private readonly ConcurrentDictionary<string, VehiclePhysics?> _cache =
|
||||
new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
// Names repeat between folders: 214m.fiz is one vehicle in 21xm_v1, another in sa134_v1.
|
||||
private sealed record FizIndex(Dictionary<string, string> ByFolderAndName, Dictionary<string, string> ByName);
|
||||
|
||||
private readonly object _indexGate = new();
|
||||
private Dictionary<string, string>? _index;
|
||||
private FizIndex? _index;
|
||||
|
||||
public FizPhysicsRepository(IFileSystem files, IGamePaths paths, IDiagnosticsLog log)
|
||||
{
|
||||
@@ -41,7 +44,7 @@ public sealed class FizPhysicsRepository : IPhysicsRepository
|
||||
|
||||
public void Preload() => Index();
|
||||
|
||||
public int IndexedCount => _index?.Count ?? 0;
|
||||
public int IndexedCount => _index?.ByName.Count ?? 0;
|
||||
|
||||
public VehiclePhysics? For(string? dataFolder, string? model)
|
||||
{
|
||||
@@ -52,20 +55,34 @@ public sealed class FizPhysicsRepository : IPhysicsRepository
|
||||
if (name.Length == 0)
|
||||
return null;
|
||||
|
||||
if (_cache.TryGetValue(name, out var cached))
|
||||
string folder = FolderKey(dataFolder);
|
||||
string key = FizKey(folder, name);
|
||||
if (_cache.TryGetValue(key, out var cached))
|
||||
return cached;
|
||||
|
||||
var physics = Read(name);
|
||||
_cache[name] = physics;
|
||||
var physics = Read(folder, name);
|
||||
_cache[key] = physics;
|
||||
return physics;
|
||||
}
|
||||
|
||||
private VehiclePhysics? Read(string name)
|
||||
private static string FizKey(string folder, string name) => $"{folder}/{name}";
|
||||
|
||||
/// <summary>A vehicle's folder under dynamic, however the caller spells it.</summary>
|
||||
private static string FolderKey(string? folder)
|
||||
{
|
||||
const string dynamicPrefix = "dynamic/";
|
||||
string key = (folder ?? "").Replace('\\', '/').Trim('/');
|
||||
return key.StartsWith(dynamicPrefix, StringComparison.OrdinalIgnoreCase) ? key[dynamicPrefix.Length..] : key;
|
||||
}
|
||||
|
||||
private VehiclePhysics? Read(string folder, string name)
|
||||
{
|
||||
var index = Index();
|
||||
|
||||
if (!index.TryGetValue(name, out string? path) &&
|
||||
!index.TryGetValue(name + "dumb", out path))
|
||||
if (!index.ByFolderAndName.TryGetValue(FizKey(folder, name), out string? path) &&
|
||||
!index.ByFolderAndName.TryGetValue(FizKey(folder, name + "dumb"), out path) &&
|
||||
!index.ByName.TryGetValue(name, out path) &&
|
||||
!index.ByName.TryGetValue(name + "dumb", out path))
|
||||
return null;
|
||||
|
||||
var physics = new VehiclePhysics { AllowedFlagB = UndeclaredCoupler };
|
||||
@@ -88,26 +105,33 @@ public sealed class FizPhysicsRepository : IPhysicsRepository
|
||||
return physics;
|
||||
}
|
||||
|
||||
private Dictionary<string, string> Index()
|
||||
private FizIndex Index()
|
||||
{
|
||||
lock (_indexGate)
|
||||
{
|
||||
if (_index is not null)
|
||||
return _index;
|
||||
|
||||
var index = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
var byFolder = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
var byName = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||
try
|
||||
{
|
||||
foreach (string file in _files.GetFilesRecursive(_paths.Dynamic, "*.fiz"))
|
||||
index[Path.GetFileNameWithoutExtension(file)] = file;
|
||||
// Sorted, so which of two namesakes a name alone finds is the same on every machine.
|
||||
foreach (string file in _files.GetFilesRecursive(_paths.Dynamic, "*.fiz").Order(StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
string name = Path.GetFileNameWithoutExtension(file);
|
||||
string folder = FolderKey(Path.GetDirectoryName(Path.GetRelativePath(_paths.Dynamic, file)));
|
||||
byFolder[FizKey(folder, name)] = file;
|
||||
byName.TryAdd(name, file);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Log($"indexing {_paths.Dynamic}", ex);
|
||||
}
|
||||
|
||||
_index = index;
|
||||
return index;
|
||||
_index = new FizIndex(byFolder, byName);
|
||||
return _index;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using StarterNG.Application.Abstractions;
|
||||
using StarterNG.Domain.Vehicles;
|
||||
using StarterNG.Infrastructure.Adapters;
|
||||
@@ -53,8 +52,8 @@ public sealed class TexturesTxtVehicleRepository : IVehicleRepository
|
||||
/// </summary>
|
||||
public IEnumerable<string> Sources()
|
||||
{
|
||||
foreach (string maker in Sorted(_files.GetDirectories(_paths.Dynamic)))
|
||||
foreach (string vehicle in Sorted(_files.GetDirectories(maker)))
|
||||
foreach (string maker in Subfolders(_paths.Dynamic))
|
||||
foreach (string vehicle in Subfolders(maker))
|
||||
{
|
||||
string path = Path.Combine(vehicle, TexturesFileName);
|
||||
if (_files.FileExists(path))
|
||||
@@ -62,8 +61,19 @@ public sealed class TexturesTxtVehicleRepository : IVehicleRepository
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<string> Sorted(IEnumerable<string> directories) =>
|
||||
directories.OrderBy(Path.GetFileName, StringComparer.OrdinalIgnoreCase);
|
||||
// A folder that cannot be listed costs what is in it, not all that comes after it.
|
||||
private IReadOnlyList<string> Subfolders(string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _files.GetDirectories(path).OrderBy(Path.GetFileName, StringComparer.OrdinalIgnoreCase).ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Log($"dynamic: skipping {path}", ex);
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Reads one textures.txt; a file that will not parse is logged and skipped.</summary>
|
||||
public VehicleEntry? Read(string path)
|
||||
|
||||
@@ -43,21 +43,10 @@ public sealed class VehicleCacheRepository : IVehicleRepository
|
||||
_directory = directory;
|
||||
}
|
||||
|
||||
/// <summary>What the last load did, for the developer tab to report.</summary>
|
||||
public VehicleCacheStatus Status { get; private set; } = VehicleCacheStatus.None;
|
||||
|
||||
public int Load(VehicleCatalog catalog) => Load(catalog, useCopy: true);
|
||||
|
||||
/// <summary>Throws the copy away and reads the installation again.</summary>
|
||||
public int Rebuild(VehicleCatalog catalog) => Load(catalog, useCopy: false);
|
||||
|
||||
private int Load(VehicleCatalog catalog, bool useCopy)
|
||||
public int Load(VehicleCatalog catalog)
|
||||
{
|
||||
var (stamp, sources) = Stamp();
|
||||
var cached = useCopy ? Cached(stamp) : null;
|
||||
var entries = cached ?? Migrate(stamp);
|
||||
|
||||
Status = new VehicleCacheStatus(cached is not null, sources, WrittenAt(), _directory);
|
||||
string stamp = Stamp();
|
||||
var entries = Cached(stamp) ?? Migrate(stamp);
|
||||
|
||||
catalog.BeginLoad();
|
||||
|
||||
@@ -77,15 +66,13 @@ public sealed class VehicleCacheRepository : IVehicleRepository
|
||||
/// installation can move on - a file edited, added, removed, or the whole
|
||||
/// installation swapped for another one - shows up as a different text.
|
||||
/// </summary>
|
||||
private (string Text, int Sources) Stamp()
|
||||
private string Stamp()
|
||||
{
|
||||
var stamp = new StringBuilder().Append("root|").Append(Path.GetFullPath(_paths.Root)).Append('\n')
|
||||
.Append("parser|").Append(TexturesTxtParser.Version).Append('\n');
|
||||
int sources = 0;
|
||||
|
||||
foreach (string path in _legacy.Sources())
|
||||
{
|
||||
sources++;
|
||||
long ticks = 0;
|
||||
long size = 0;
|
||||
try
|
||||
@@ -102,20 +89,7 @@ public sealed class VehicleCacheRepository : IVehicleRepository
|
||||
.Append(path.Replace('\\', '/')).Append('\n');
|
||||
}
|
||||
|
||||
return (stamp.ToString(), sources);
|
||||
}
|
||||
|
||||
private DateTime? WrittenAt()
|
||||
{
|
||||
try
|
||||
{
|
||||
return _files.FileExists(DatabasePath) ? _files.GetLastWriteTimeUtc(DatabasePath) : null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_log.Log($"{DatabaseFileName}: nie znam daty kopii", ex);
|
||||
return null;
|
||||
}
|
||||
return stamp.ToString();
|
||||
}
|
||||
|
||||
private List<VehicleEntry>? Cached(string stamp)
|
||||
@@ -179,12 +153,3 @@ public sealed class VehicleCacheRepository : IVehicleRepository
|
||||
|
||||
private string SourcesPath => Path.Combine(_directory, SourcesFileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// How the catalogue was last filled: from the starter's copy or by reading the
|
||||
/// installation, over how many textures.txt, and when the copy was last written.
|
||||
/// </summary>
|
||||
public sealed record VehicleCacheStatus(bool ServedFromCopy, int Sources, DateTime? Written, string Directory)
|
||||
{
|
||||
public static readonly VehicleCacheStatus None = new(false, 0, null, "");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user