From 943b855f20a97dccde24d009bfe43c8dad2c2b4a Mon Sep 17 00:00:00 2001 From: maj00r Date: Tue, 17 Nov 2026 14:55:04 +0100 Subject: [PATCH] feat(warehouse): the old starter's magazyn.ini joins the warehouse at every start The old starter read starter\magazyn.ini each time it opened and wrote it on every change; this one kept its warehouse apart and took the file in only from the list's menu, so a consist saved there never showed here. - At the start, ahead of the scenery list, the consists of magazyn.ini not taken in before join the warehouse. Each one taken is remembered in userpresets.json by its name and vehicles, so one removed here stays removed while the old starter keeps it as it was. - A consist changed there replaces the one here when that one is as an earlier start brought it; one of the same name made here stays, and the old one comes in beside it as "name (magazyn.ini)". - A warehouse file that cannot be read is left alone by the import. - The file is read as UTF-8 or code page 1250, whichever it is, by hand too. --- StarterNG.Tests/OldStarterWarehouseTests.cs | 132 ++++++++++++++++++ StarterNG/App.axaml.cs | 2 + StarterNG/Classes/PresetStore.cs | 142 ++++++++++++++++---- 3 files changed, 250 insertions(+), 26 deletions(-) create mode 100644 StarterNG.Tests/OldStarterWarehouseTests.cs diff --git a/StarterNG.Tests/OldStarterWarehouseTests.cs b/StarterNG.Tests/OldStarterWarehouseTests.cs new file mode 100644 index 0000000..240c36e --- /dev/null +++ b/StarterNG.Tests/OldStarterWarehouseTests.cs @@ -0,0 +1,132 @@ +using StarterNG.Classes; +using StarterNG.Tests.Fakes; + +namespace StarterNG.Tests; + +// The old starter's magazyn.ini taken in at a start, over the fake disk. +public class OldStarterWarehouseTests +{ + private const string PresetsPath = "/home/user/.config/MaSzyna/starter/userpresets.json"; + + // As the old starter writes it: CRLF, code page 1250, the scenery's path for a name. + private const string Magazyn = + "[TRAINSET0=baltyk/baltyk_km/mpj15120]\r\n" + + "00=node -1 0 EN57AKM-1682RA dynamic pkp\\en57akm_2013_v1 EN57AKM-1682ra akm_2013_ra_male_okna 0 headdriver 255 0 enddynamic\r\n" + + "01=node -1 0 EN57AKM-1682S dynamic pkp\\en57akm_2013_v1 EN57AKM-1682s akm_2013_s 0 nobody 255 0 enddynamic\r\n" + + "02=node -1 0 EN57AKM-1682RB dynamic pkp\\en57akm_2013_v1 EN57AKM-1682rb akm_2013_rb_male_okna 0 nobody 0 0 enddynamic\r\n"; + + private static (PresetFile Presets, InMemoryFileSystem Files) Rig() + { + var files = new InMemoryFileSystem(); + return (new PresetFile(files, TestLog.Over(files), PresetsPath), files); + } + + private static List Consist(string name, string vehicle) => + OldStarterWarehouse.Read($"[TRAINSET0={name}]\r\n00=node -1 0 {vehicle} dynamic pkp\\x {vehicle} x 0 headdriver 3 0 enddynamic\r\n"); + + [Fact] + public void A_consist_of_the_old_starter_reads_as_a_trainset_with_its_vehicles() + { + var consist = Assert.Single(OldStarterWarehouse.Read(Magazyn)); + + Assert.Equal("baltyk/baltyk_km/mpj15120", consist.Name); + Assert.StartsWith("trainset baltyk/baltyk_km/mpj15120 none 0 0\n", consist.Entry); + Assert.EndsWith("endtrainset\n", consist.Entry); + var vehicles = ConsistText.VehiclesFrom(consist.Entry); + Assert.NotNull(vehicles); + Assert.Equal(new[] { "EN57AKM-1682RA", "EN57AKM-1682S", "EN57AKM-1682RB" }, vehicles!.Select(v => v.Name)); + } + + [Fact] + public void A_consist_new_in_the_old_starter_joins_the_warehouse() + { + var (presets, _) = Rig(); + presets.Save("mine", "trainset mine"); + + int added = presets.TakeFromOldStarter(OldStarterWarehouse.Read(Magazyn)); + + Assert.Equal(1, added); + Assert.Equal(new[] { "baltyk/baltyk_km/mpj15120", "mine" }, presets.Presets().Select(p => p.Name)); + } + + [Fact] + public void A_consist_removed_here_does_not_come_back_at_the_next_start() + { + var (presets, _) = Rig(); + var found = OldStarterWarehouse.Read(Magazyn); + presets.TakeFromOldStarter(found); + presets.Delete("baltyk/baltyk_km/mpj15120"); + + int added = presets.TakeFromOldStarter(found); + + Assert.Equal(0, added); + Assert.Empty(presets.Presets()); + } + + [Fact] + public void Nothing_new_leaves_the_file_unwritten() + { + var (presets, files) = Rig(); + var found = OldStarterWarehouse.Read(Magazyn); + presets.TakeFromOldStarter(found); + string before = files.ReadAllText(PresetsPath); + files.WithFile(PresetsPath, before + " "); + + presets.TakeFromOldStarter(found); + + Assert.Equal(before + " ", files.ReadAllText(PresetsPath)); + } + + [Fact] + public void A_consist_imported_by_hand_before_is_not_taken_twice() + { + var (presets, _) = Rig(); + var found = OldStarterWarehouse.Read(Magazyn); + presets.Save(found[0].Name, found[0].Entry); + + int added = presets.TakeFromOldStarter(found); + + Assert.Equal(0, added); + Assert.Single(presets.Presets()); + } + + [Fact] + public void A_consist_of_the_same_name_made_here_stays_and_the_old_one_comes_in_beside_it() + { + var (presets, _) = Rig(); + presets.Save("osobowy", "trainset osobowy none 0 0\nendtrainset\n"); + + presets.TakeFromOldStarter(Consist("osobowy", "SM42")); + + var all = presets.Presets(); + Assert.Equal(new[] { "osobowy", "osobowy" + PresetFile.OldStarterSuffix }, all.Select(p => p.Name)); + Assert.Equal("trainset osobowy none 0 0\nendtrainset\n", all[0].Entry); + Assert.Contains("SM42", all[1].Entry); + } + + [Fact] + public void A_consist_changed_in_the_old_starter_and_untouched_here_is_replaced() + { + var (presets, _) = Rig(); + presets.TakeFromOldStarter(Consist("osobowy", "SM42")); + + presets.TakeFromOldStarter(Consist("osobowy", "SU45")); + + var only = Assert.Single(presets.Presets()); + Assert.Equal("osobowy", only.Name); + Assert.Contains("SU45", only.Entry); + } + + [Fact] + public void A_warehouse_file_that_cannot_be_read_is_left_as_it_is() + { + var (presets, files) = Rig(); + files.WithFile(PresetsPath, "{ cut"); + + int added = presets.TakeFromOldStarter(OldStarterWarehouse.Read(Magazyn)); + + Assert.Equal(0, added); + Assert.Equal("{ cut", files.ReadAllText(PresetsPath)); + Assert.False(files.FileExists(PresetsPath + ".unreadable")); + } +} diff --git a/StarterNG/App.axaml.cs b/StarterNG/App.axaml.cs index 86b9234..20b1b35 100644 --- a/StarterNG/App.axaml.cs +++ b/StarterNG/App.axaml.cs @@ -163,6 +163,8 @@ public partial class App : Avalonia.Application // Ahead of the sceneries, so the list knows which still have their .sbt. try { AppServices.Current.BinaryTerrain.CleanUpOnce(); } catch (Exception ex) { Diagnostics.Log("sbt cleanup", ex); } + // Before any window lists the warehouse; it catches what it meets itself. + PresetStore.TakeFromOldStarter(); AppServices.Current.Library.Load(progress); }).ContinueWith(load => { diff --git a/StarterNG/Classes/PresetStore.cs b/StarterNG/Classes/PresetStore.cs index 69b7e82..47a4bc8 100644 --- a/StarterNG/Classes/PresetStore.cs +++ b/StarterNG/Classes/PresetStore.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; +using System.Security.Cryptography; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; @@ -28,6 +30,9 @@ public sealed class TrainsetPreset public sealed class PresetCollection { public List Presets { get; set; } = new(); + + /// The old starter's consists taken in at a start, by . + public List OldStarterSeen { get; set; } = new(); } public static class PresetStore @@ -81,33 +86,9 @@ public static class PresetStore int added = 0; try { - string text = File.ReadAllText(path, LegacyText.CodePage1250); - var section = Regex.Matches(text, - @"\[TRAINSET\d+\s*=\s*([^\]]*)\](.*?)(?=\[TRAINSET|\z)", - RegexOptions.IgnoreCase | RegexOptions.Singleline); - - foreach (Match m in section) + foreach (var consist in OldStarterWarehouse.Read(LegacyText.Decode(File.ReadAllBytes(path)))) { - string name = m.Groups[1].Value.Trim(); - if (name.Length == 0) name = $"import_{added + 1}"; - - var nodes = new List(); - foreach (Match line in Regex.Matches(m.Groups[2].Value, @"(?m)^\s*\d+\s*=\s*(.+)$")) - { - string body = line.Groups[1].Value.Trim(); - if (!body.Contains("enddynamic", StringComparison.OrdinalIgnoreCase)) - body += " enddynamic"; - nodes.Add(body); - } - if (nodes.Count == 0) continue; - - var sb = new StringBuilder(); - sb.Append("trainset ").Append(Sanitize(name)).Append(" none 0 0\n"); - foreach (string n in nodes) - sb.Append(n).Append('\n'); - sb.Append("endtrainset\n"); - - Save(name, sb.ToString()); + Save(consist.Name, consist.Entry); added++; } } @@ -118,6 +99,26 @@ public static class PresetStore return added; } + /// + /// At the start: what the old starter put in its warehouse since the last start + /// joins this one, as that starter showed its magazyn.ini each time it was opened. + /// + public static void TakeFromOldStarter() + { + string? path = FindMagazynPath(); + if (path is null) + return; + + try + { + Disk.TakeFromOldStarter(OldStarterWarehouse.Read(LegacyText.Decode(File.ReadAllBytes(path)))); + } + catch (Exception ex) + { + Infrastructure.Diagnostics.Log($"Import {path}", ex); + } + } + private static string? FindMagazynPath() { string cwd = Directory.GetCurrentDirectory(); @@ -139,6 +140,46 @@ public static class PresetStore var m = Regex.Match(p.Entry ?? "", @"^\s*trainset\s+\S+\s+(\S+)", RegexOptions.Multiline | RegexOptions.IgnoreCase); return m.Success ? m.Groups[1].Value : ""; } +} + +/// +/// The old starter's magazyn.ini: a [TRAINSETn=name] line for each consist, then +/// its vehicles as numbered "node … enddynamic" lines. +/// +public static class OldStarterWarehouse +{ + public static List Read(string text) + { + var consists = new List(); + var sections = Regex.Matches(text, + @"\[TRAINSET\d+\s*=\s*([^\]]*)\](.*?)(?=\[TRAINSET|\z)", + RegexOptions.IgnoreCase | RegexOptions.Singleline); + + foreach (Match m in sections) + { + string name = m.Groups[1].Value.Trim(); + if (name.Length == 0) name = $"import_{consists.Count + 1}"; + + var nodes = new List(); + foreach (Match line in Regex.Matches(m.Groups[2].Value, @"(?m)^\s*\d+\s*=\s*(.+)$")) + { + string body = line.Groups[1].Value.Trim(); + if (!body.Contains("enddynamic", StringComparison.OrdinalIgnoreCase)) + body += " enddynamic"; + nodes.Add(body); + } + if (nodes.Count == 0) continue; + + var sb = new StringBuilder(); + sb.Append("trainset ").Append(Sanitize(name)).Append(" none 0 0\n"); + foreach (string n in nodes) + sb.Append(n).Append('\n'); + sb.Append("endtrainset\n"); + + consists.Add(new TrainsetPreset { Name = name, Entry = sb.ToString() }); + } + return consists; + } private static string Sanitize(string name) => string.IsNullOrWhiteSpace(name) ? "preset" : name.Replace(' ', '_'); @@ -193,6 +234,55 @@ public sealed class PresetFile Persist(col, read); } + /// Where a consist of the old starter goes when one of its name with other vehicles is here. + public const string OldStarterSuffix = " (magazyn.ini)"; + + /// + /// Takes in the old starter's consists not taken at an earlier start, so one + /// removed here stays removed while the old starter keeps it as it was. One + /// whose name is here with other vehicles comes in beside it, unless what is + /// here is that consist as an earlier start brought it, untouched since. + /// + /// How many came in. + public int TakeFromOldStarter(IReadOnlyList found) + { + // A file that cannot be read is not replaced by an import nobody asked for. + if (found.Count == 0 || !TryRead(out var col)) + return 0; + + var seenBefore = new HashSet(col.OldStarterSeen, StringComparer.Ordinal); + var seen = new HashSet(seenBefore, StringComparer.Ordinal); + int added = 0; + foreach (var consist in found) + { + if (!seen.Add(Fingerprint(consist))) + continue; + + var here = col.Presets.Find(p => string.Equals(p.Name, consist.Name, StringComparison.OrdinalIgnoreCase)); + if (here is not null && here.Entry == consist.Entry) + continue; + + string name = here is null || seenBefore.Contains(Fingerprint(here)) + ? consist.Name + : consist.Name + OldStarterSuffix; + col.Presets.RemoveAll(p => string.Equals(p.Name, name, StringComparison.OrdinalIgnoreCase)); + col.Presets.Add(new TrainsetPreset { Name = name, Entry = consist.Entry }); + added++; + } + + if (seen.Count == seenBefore.Count) + return 0; + + col.OldStarterSeen = seen.Order(StringComparer.Ordinal).ToList(); + col.Presets.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase)); + Persist(col, read: true); + return added; + } + + /// A consist by its name and vehicles, short enough to keep each one ever taken in. + public static string Fingerprint(TrainsetPreset preset) => + Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(preset.Name + "\n" + preset.Entry))); + /// The file's presets; false, with none, when there is a file but they cannot be had from it. private bool TryRead(out PresetCollection col) {