17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-08 22:07:20 +01:00

fix(editor): a scenery saved under another name keeps one file of turntable controls, not the old one beside its own (#251)

The editor writes the turntable controls on each save to <scenery>_obrotnice.scm and includes it. A copy of a
scenery, saved under another name, still included the file of the scenery it came from. Its save wrote a file of
its own and included it too, so each turntable had its events twice.

Now the save leaves out of the scenery the other files of turntable controls it includes, told by the line the
editor writes at their start. Their files stay on the disk, for the scenery they were made for. A file of
turntables made by hand, without that line, stays included.

Co-authored-by: maj00r <maj00r@host>
This commit is contained in:
maj00r
2026-11-07 12:32:02 +01:00
committed by GitHub
parent 349e94aef8
commit 088eeaa743

View File

@@ -19,6 +19,7 @@ http://mozilla.org/MPL/2.0/.
#include "scene/scenelayers.h"
#include "simulation/simulation.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/translation.h"
#include "utilities/utilities.h"
#include "world/Track.h"
@@ -186,6 +187,20 @@ void label(screen_projection const &Projection, glm::dvec3 const &Point, std::st
drawlist->AddText(ImVec2(at.x + 12.f, at.y), IM_COL32(255, 255, 255, 235), Text.c_str());
}
// the first line of the file of the turntable controls, which tells it from the files of the scenery made by hand
char const *const kControlsHeader{"// made by the scenery editor on each save: T turns each turntable to its next position, Shift+T to the previous one"};
// true if the file is one of the turntable controls the editor writes
bool controls_file(std::string const &Path)
{
std::ifstream input{Path, std::ios_base::binary};
std::string first;
std::getline(input, first);
if (false == first.empty() && first.back() == '\r')
first.pop_back();
return first == kControlsHeader;
}
} // namespace
void editor_mode::scan_turntables()
@@ -1143,7 +1158,7 @@ bool editor_mode::turntable_controls(std::vector<std::string> &Rootstatements)
auto const exists{std::filesystem::exists(path, error)};
if (tables == 0 && false == exists)
return false;
auto const content{"// made by the scenery editor on each save: T turns each turntable to its next position, Shift+T to the previous one\r\n" + text.str()};
auto const content{std::string{kControlsHeader} + "\r\n" + text.str()};
std::string current;
{
std::ifstream input{path, std::ios_base::binary};
@@ -1154,6 +1169,21 @@ bool editor_mode::turntable_controls(std::vector<std::string> &Rootstatements)
std::ofstream output{path, std::ios_base::binary | std::ios_base::trunc};
output << content;
}
// the controls written for this scenery under another name, the file it was copied from, give way to these: with both
// included each turntable would have its events twice. their files stay on the disk, for the scenery they were made for
for (std::size_t handle = 1; handle <= scene::Layers.size(); ++handle)
{
auto const layer{static_cast<scene::layer_handle>(handle)};
if (false == scene::Layers.listed(layer) || scene::Layers.layer(layer).removed)
continue;
auto name{scene::Layers.layer(layer).name};
std::replace(name.begin(), name.end(), '\\', '/');
auto const lower{ToLower(name)};
if (lower == ToLower(file) || lower.ends_with("/" + ToLower(file)) || false == lower.ends_with("_obrotnice.scm"))
continue;
if (controls_file(Global.asCurrentSceneryPath + scene::Layers.layer(layer).name) && scene::Layers.remove(layer))
WriteLog("Editor: " + name + " left out of the scenery, the turntable controls are in " + file + " now");
}
auto included{m_turntable_included};
for (std::size_t handle = 1; handle <= scene::Layers.size() && false == included; ++handle)
{