diff --git a/tests/unit/test_odometer.cpp b/tests/unit/test_odometer.cpp index 5e287116b..5ec1b572f 100644 --- a/tests/unit/test_odometer.cpp +++ b/tests/unit/test_odometer.cpp @@ -434,3 +434,22 @@ TEST_CASE("odometer: only the vehicles with a driver's cab are recorded") { CHECK(read.find("old wagon").value() == 300.0); } } + +TEST_CASE("odometer: the files of both kinds of python screens are read, by the name each used") { + scratch_directory directory{"odometer_legacy_screens"}; + auto const vehicles{directory.path / "dynamic" / "elf_v1"}; + auto const scripts{directory.path / "scripts"}; + // the renderers of the units, next to the .mmd: state['name'][:-1], the cab cars of a unit share the file + write_file(vehicles / "kilometry" / "EN76-022_przebieg.txt", "250000.5"); + CHECK(odometer::legacy_state(vehicles, "EN76-022a", scripts).value() == 250000.5); + CHECK(odometer::legacy_state(vehicles, "EN76-022d", scripts).value() == 250000.5); + // the shared screens of scripts/ (koliber_*): the whole name + write_file(scripts / "kilometry" / "EU07-424_przebieg.txt", "123456.0"); + CHECK(odometer::legacy_state(vehicles, "EU07-424", scripts).value() == 123456.0); + auto const started{odometer::starting_state(odometer::store{}, "EU07-424", vehicles, scripts)}; + CHECK(started.source == odometer::origin::legacy); + CHECK(started.km == 123456.0); + // neither + CHECK_FALSE(odometer::legacy_state(vehicles, "EU07-425", scripts).has_value()); + CHECK_FALSE(odometer::legacy_state(vehicles, "", scripts).has_value()); +} diff --git a/vehicle/odometer.cpp b/vehicle/odometer.cpp index 60c119f12..8ecf9a9c1 100644 --- a/vehicle/odometer.cpp +++ b/vehicle/odometer.cpp @@ -178,19 +178,34 @@ std::optional read_legacy(std::istream &Input) { return parse_number(content.str()); } -std::optional legacy_state(std::filesystem::path const &Vehicledirectory, std::string const &Vehicle) { - if (Vehicle.size() < 2) - return std::nullopt; - auto const path{Vehicledirectory / "kilometry" / (Vehicle.substr(0, Vehicle.size() - 1) + "_przebieg.txt")}; +namespace { + +std::optional read_legacy_file(std::filesystem::path const &Path) { std::error_code error; - if (false == std::filesystem::is_regular_file(path, error)) + if (false == std::filesystem::is_regular_file(Path, error)) return std::nullopt; - std::ifstream input{path, std::ios::binary}; + std::ifstream input{Path, std::ios::binary}; if (false == input.is_open()) return std::nullopt; return read_legacy(input); } +} // namespace + +std::optional legacy_state(std::filesystem::path const &Vehicledirectory, std::string const &Vehicle, + std::filesystem::path const &Scriptsdirectory) { + if (Vehicle.empty()) + return std::nullopt; + // the renderers of the units drop the letter of the car, the file is the unit's (see the header) + if (Vehicle.size() > 1) { + auto const unit{read_legacy_file(Vehicledirectory / "kilometry" / (Vehicle.substr(0, Vehicle.size() - 1) + "_przebieg.txt"))}; + if (unit.has_value()) + return unit; + } + // the shared screens of scripts/ keep the whole name + return read_legacy_file(Scriptsdirectory / "kilometry" / (Vehicle + "_przebieg.txt")); +} + double guessed_state(std::string const &Vehicle) { // FNV-1a, stable between the builds unlike std::hash std::uint64_t hash{14695981039346656037ull}; @@ -201,7 +216,8 @@ double guessed_state(std::string const &Vehicle) { return 10000.0 + static_cast(hash % 490000ull); } -reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory) { +reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory, + std::filesystem::path const &Scriptsdirectory) { reading result; if (auto const saved{States.find(Vehicle)}; saved.has_value()) { if (States.guessed(Vehicle)) { @@ -212,7 +228,7 @@ reading starting_state(store const &States, std::string const &Vehicle, std::fil } else { result.begin(*saved, origin::saved); } - } else if (auto const legacy{legacy_state(Vehicledirectory, Vehicle)}; legacy.has_value()) { + } else if (auto const legacy{legacy_state(Vehicledirectory, Vehicle, Scriptsdirectory)}; legacy.has_value()) { result.begin(*legacy, origin::legacy); } else { result.begin(guessed_state(Vehicle), origin::guessed); diff --git a/vehicle/odometer.h b/vehicle/odometer.h index 84e18b1f1..83396abfd 100644 --- a/vehicle/odometer.h +++ b/vehicle/odometer.h @@ -95,10 +95,17 @@ private: // store already holds for any of them stay as they are void record(store &States, std::string const &Vehicle, bool Drivercab, reading const &Odometer); -// state kept in the file of the python screens before (/kilometry/_przebieg.txt, -// where the screens dropped the last character of the vehicle name), when it holds a number +// state kept in the file of the python screens before, when it holds a number. the screens opened the file in the +// directory of their script (lookup_path), and two kinds of them kept one: +// - the renderers of the units, next to the .mmd (elf_v1: EN62, EN76, EN96, SKM; impuls_v1: EN63), in +// /kilometry/_przebieg.txt (state['name'][:-1]). the cars of +// these units are named after the unit and the letter of the car (EN76-022a .. EN76-022d, ED78-019A .. ED78-019D), +// so the cab cars at both ends of a unit share one file, the unit's odometer +// - the shared screens in scripts/ (koliber_hasler_elektroniczny, koliber_unicontrols_tramex[_ic]), in +// /kilometry/_przebieg.txt, the whole name std::optional read_legacy(std::istream &Input); -std::optional legacy_state(std::filesystem::path const &Vehicledirectory, std::string const &Vehicle); +std::optional legacy_state(std::filesystem::path const &Vehicledirectory, std::string const &Vehicle, + std::filesystem::path const &Scriptsdirectory = "scripts"); // made up state of a vehicle nothing is known about: a number from the name, in [10 000, 500 000) km, // the same each session until the vehicle runs and gets its state saved @@ -106,7 +113,8 @@ double guessed_state(std::string const &Vehicle); // state a vehicle starts the session with: the saved one, else the legacy one, else a guessed one. a saved state // counted from a guessed one stays guessed, so the cab's counter can still replace the made up part of it -reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory); +reading starting_state(store const &States, std::string const &Vehicle, std::filesystem::path const &Vehicledirectory, + std::filesystem::path const &Scriptsdirectory = "scripts"); // state the cab's counter with offset Offset and scale Scale shows, once its offset no longer applies // (the counter shows the odometer). nothing for a counter without an offset