mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-27 21:45:16 +01:00
fix(odometer): a save lost to another simulation's save is written again (#354)
Two simulations sharing eu07_odometers.txt could lose a save: A reads the file, B renames its file in, A renames its own over it. B had already cleared its modified flag, so its next save returned at once and its states stayed lost (known limit from the review of #283). A write now leaves the store unconfirmed. The next save with nothing recorded reads and merges the file as before, and writes again only when the file lacks a state of the process, the same or newer by the merge rule (compared as written, rounded to the file's digits). Otherwise the store is confirmed and later saves return at once, as before. A process holding an older state takes the file's one, so two processes settle instead of rewriting each other.
This commit is contained in:
@@ -176,6 +176,13 @@ void store::merge(store const &Other) {
|
||||
}
|
||||
}
|
||||
|
||||
bool store::holds(store const &Other) const {
|
||||
return std::all_of(Other.m_states.begin(), Other.m_states.end(), [this](auto const &Entry) {
|
||||
auto const lookup{m_states.find(Entry.first)};
|
||||
return lookup != m_states.end() && false == newer(Entry.second, lookup->second);
|
||||
});
|
||||
}
|
||||
|
||||
std::optional<double> read_legacy(std::istream &Input) {
|
||||
std::stringstream content;
|
||||
content << Input.rdbuf();
|
||||
@@ -277,8 +284,21 @@ store &states() {
|
||||
return odometers;
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
// the states as the file holds them once written, the km rounded to the digits written
|
||||
store as_written(store const &States) {
|
||||
std::stringstream text;
|
||||
States.write(text);
|
||||
store result;
|
||||
result.read(text);
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool save(store &States, std::filesystem::path const &File) {
|
||||
if (false == States.modified())
|
||||
if (false == States.modified() && false == States.unconfirmed())
|
||||
return true;
|
||||
std::error_code error;
|
||||
if (File.has_parent_path())
|
||||
@@ -286,11 +306,19 @@ bool save(store &States, std::filesystem::path const &File) {
|
||||
// other simulations on the machine (multiplayer, parallel runs) keep their states in the same file: what they
|
||||
// saved since this one read it is merged in, not overwritten
|
||||
{
|
||||
store saved;
|
||||
std::ifstream input{File, std::ios::binary};
|
||||
if (input.is_open()) {
|
||||
store saved;
|
||||
if (input.is_open())
|
||||
saved.read(input);
|
||||
States.merge(saved);
|
||||
States.merge(saved);
|
||||
// another simulation that read the file before the last save of this one and put its own in place after it
|
||||
// dropped the states this one wrote. when the file holds them all, the same or newer, there's nothing to write
|
||||
if (false == States.modified()) {
|
||||
if (saved.holds(as_written(States))) {
|
||||
States.confirmed();
|
||||
return true;
|
||||
}
|
||||
WriteLog("Odometer states saved to \"" + File.string() + "\" are missing from it, saving them again");
|
||||
}
|
||||
}
|
||||
// written aside first, a crash in the middle of a write doesn't lose the states. the file aside is this process'
|
||||
|
||||
@@ -46,10 +46,19 @@ public:
|
||||
// the odometers only count up, so of two states of a vehicle the larger is the newer one. they aren't added
|
||||
// up: two simulations on one machine (multiplayer) count the same runs of the same vehicles
|
||||
void merge(store const &Other);
|
||||
// this store holds every state of the other one, the same or a newer one
|
||||
bool holds(store const &Other) const;
|
||||
|
||||
// there are states recorded since they were last saved
|
||||
bool modified() const { return m_modified; }
|
||||
void saved() { m_modified = false; }
|
||||
// the states were written to the file. until a later save finds them there (confirmed()), they may have been lost to
|
||||
// another simulation's save, which read the file before this one wrote it and put its own in place after
|
||||
void saved() {
|
||||
m_modified = false;
|
||||
m_unconfirmed = true;
|
||||
}
|
||||
bool unconfirmed() const { return m_unconfirmed; }
|
||||
void confirmed() { m_unconfirmed = false; }
|
||||
std::size_t size() const { return m_states.size(); }
|
||||
|
||||
private:
|
||||
@@ -62,6 +71,7 @@ private:
|
||||
|
||||
std::map<std::string, state> m_states;
|
||||
bool m_modified{false};
|
||||
bool m_unconfirmed{false};
|
||||
};
|
||||
|
||||
// the vehicle is one the store keeps: one with a driver's cab, which can show its odometer on the cab's counter or a
|
||||
@@ -113,8 +123,9 @@ bool persistent();
|
||||
store &states();
|
||||
std::filesystem::path file();
|
||||
// writes the store to the file when anything was recorded since, merged with what other simulations saved in the
|
||||
// file meanwhile. it's written to a file of this process aside first, then put in place. returns false when the
|
||||
// write failed
|
||||
// file meanwhile. it's written to a file of this process aside first, then put in place. with nothing recorded since,
|
||||
// the file is read once more after a write, and written again when it lacks any of the states (another simulation's
|
||||
// save put its file over this one's); otherwise left as it is. returns false when the write failed
|
||||
bool save(store &States, std::filesystem::path const &File);
|
||||
// saves the store of the simulation to its file. a test run doesn't save it
|
||||
bool save();
|
||||
|
||||
Reference in New Issue
Block a user