mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 05:57:08 +01:00
c912ed2ab1
Continues E3 (editor-tracks-domain.md §9, "Ręczna zmiana wygenerowanego węzła ... wykrywana po sumie kontrolnej"): the generated
section of the project file and what the scenery files hold of it now. Nothing of the editor writes the tracks yet (E4).
- generated: a record a track the generator wrote, its name, its line, the file it's written into and the FNV-1a checksum (64 bits)
of its text as the simulator reads it: the words joined by one blank, the comments left out, so the line ends and the blanks
another tool writes aren't a change; the switch track of a turnout kept by the main track of the turnout.
track l1_s11 line 1 file trasa/tory.scm sum 0123456789abcdef
switch t1 line 2 file trasa/tory.scm sum 00000000000000ff
hand 3
record_generated() puts the pieces of a line, or the switch track of a turnout, in the place of the ones the project had of it;
find_hand_changes() looks the names up in the text of the files now: a text of another checksum, or a track gone, or a file which
can't be read; mark_hand_changes() marks their lines changed by hand (hand <line>, kept by the file), may_overwrite() tells the
editor to leave them, until the line is written again by the editor (the author decided).
- editor/project/sceneryText: the text of the scenery files read as cParser and state_serializer read it, with no scene: the words
(blanks and semicolons between them, // and /* */ comments, the quotes), the node of a name and its words up to the end word of its
type, and the statements of a scenery with its includes followed relative to the scenery folder (the words of each file taken as
they're needed, the includes no deeper than 32), the parameters of a template in place of (p1)..., the placement of origin, rotate
and scale at each include, the terrain files passed over for a binary terrain. The scenario index and the migration read the files
with it.
Tests: U: two lines generated into one file among other nodes: as written nothing is changed, through the project file too; the speed
of a track of the second line edited: that line and that track told, marked changed by hand once, kept by the file, the first line
not; written again, not marked. The switch track of a turnout: its speed edited marks its main track. Blanks doubled, the line ends of
windows and comments inside a track aren't a change; a track taken away, a file which can't be read are. The node of a name found by
the words.
98 lines
2.9 KiB
C++
98 lines
2.9 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "editor/project/projectGenerated.hpp"
|
|
#include "editor/project/sceneryText.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <map>
|
|
|
|
namespace project
|
|
{
|
|
|
|
std::uint64_t checksum(std::string_view const Text)
|
|
{
|
|
auto const text{scenery_text::joined(scenery_text::split(Text))};
|
|
std::uint64_t result{14695981039346656037ull};
|
|
for (auto const c : text)
|
|
{
|
|
result ^= static_cast<unsigned char>(c);
|
|
result *= 1099511628211ull;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void record_generated(document &Project, layout::generated_line const &Line, std::string const &File)
|
|
{
|
|
std::erase_if(Project.generated, [&](generated_node const &Node) { return Node.kind == "track" && Node.line == Line.line; });
|
|
for (auto const &piece : Line.pieces)
|
|
{
|
|
generated_node node;
|
|
node.name = piece.name;
|
|
node.line = Line.line;
|
|
node.file = File;
|
|
node.sum = checksum(piece.text);
|
|
Project.generated.push_back(std::move(node));
|
|
}
|
|
// written again by the editor: the author decided
|
|
Project.hand_changed.erase(Line.line);
|
|
}
|
|
|
|
void record_generated(document &Project, layout::generated_turnout const &Turnout, std::string const &File)
|
|
{
|
|
auto const *turnout{Project.network.find(Turnout.turnout)};
|
|
if (turnout == nullptr)
|
|
return;
|
|
auto const main{turnout->main};
|
|
std::erase_if(Project.generated, [&](generated_node const &Node) { return Node.kind == "switch" && Node.line == main; });
|
|
generated_node node;
|
|
node.kind = "switch";
|
|
node.name = Turnout.name;
|
|
node.line = main;
|
|
node.file = File;
|
|
node.sum = checksum(Turnout.text);
|
|
Project.generated.push_back(std::move(node));
|
|
Project.hand_changed.erase(main);
|
|
}
|
|
|
|
std::vector<hand_change> find_hand_changes(document const &Project, std::function<std::optional<std::string>(std::string const &)> const &Files)
|
|
{
|
|
std::vector<hand_change> result;
|
|
std::map<std::string, std::optional<std::string>> texts;
|
|
for (auto const &node : Project.generated)
|
|
{
|
|
auto found{texts.find(node.file)};
|
|
if (found == texts.end())
|
|
found = texts.emplace(node.file, Files(node.file)).first;
|
|
auto const &text{found->second};
|
|
auto const now{text.has_value() ? scenery_text::node_text(*text, node.name) : std::nullopt};
|
|
if (now.has_value() && checksum(*now) == node.sum)
|
|
continue;
|
|
result.push_back({node.line, node.name, node.file, false == now.has_value()});
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::set<layout::line_id> mark_hand_changes(document &Project, std::vector<hand_change> const &Changes)
|
|
{
|
|
std::set<layout::line_id> result;
|
|
for (auto const &change : Changes)
|
|
if (Project.hand_changed.insert(change.line).second)
|
|
result.insert(change.line);
|
|
return result;
|
|
}
|
|
|
|
bool may_overwrite(document const &Project, layout::line_id const Line)
|
|
{
|
|
return false == Project.hand_changed.contains(Line);
|
|
}
|
|
|
|
} // namespace project
|