mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-11-06 23:13:00 +01:00
fix(editor): release the scenario file after load, undo a failed save
This commit is contained in:
@@ -55,6 +55,9 @@ bool scenarioloader_mode::update() {
|
|||||||
ErrorLog( "Bad init: scenario loading failed" );
|
ErrorLog( "Bad init: scenario loading failed" );
|
||||||
Application.pop_mode();
|
Application.pop_mode();
|
||||||
}
|
}
|
||||||
|
// the loading state holds the scenario file open, which on some systems keeps the file from being replaced
|
||||||
|
// when the scenery is saved in the editor. it'd also get in the way of another load
|
||||||
|
state.reset();
|
||||||
|
|
||||||
WriteLog( "Scenario loading time: " + std::to_string( std::chrono::duration_cast<std::chrono::seconds>( std::chrono::system_clock::now() - timestart ).count() ) + " seconds" );
|
WriteLog( "Scenario loading time: " + std::to_string( std::chrono::duration_cast<std::chrono::seconds>( std::chrono::system_clock::now() - timestart ).count() ) + " seconds" );
|
||||||
// TODO: implement and use next mode cue
|
// TODO: implement and use next mode cue
|
||||||
|
|||||||
@@ -418,6 +418,7 @@ bool save(std::string const &File, include_info const &Info, std::string &Error)
|
|||||||
if (error)
|
if (error)
|
||||||
{
|
{
|
||||||
Error = "can't replace file \"" + File + "\": " + error.message();
|
Error = "can't replace file \"" + File + "\": " + error.message();
|
||||||
|
std::filesystem::remove(filepath + ".tmp", error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -862,6 +862,7 @@ save_result node_layers::save(std::vector<std::string> const &Rootstatements)
|
|||||||
}
|
}
|
||||||
written.emplace_back(output);
|
written.emplace_back(output);
|
||||||
}
|
}
|
||||||
|
std::vector<layer_handle> replaced;
|
||||||
for (auto const output : written)
|
for (auto const output : written)
|
||||||
{
|
{
|
||||||
auto const filepath{path(output)};
|
auto const filepath{path(output)};
|
||||||
@@ -873,10 +874,35 @@ save_result node_layers::save(std::vector<std::string> const &Rootstatements)
|
|||||||
std::filesystem::rename(filepath + ".tmp", filepath, error);
|
std::filesystem::rename(filepath + ".tmp", filepath, error);
|
||||||
if (error)
|
if (error)
|
||||||
{
|
{
|
||||||
return fail("can't replace file \"" + layer(output).name + "\": " + error.message());
|
// e.g. the file is held open by another program. take back what was done so far: the scenery files
|
||||||
|
// have to stay in step with each other, and with what we know about them
|
||||||
|
auto const cause{error.message()};
|
||||||
|
for (auto const restored : replaced)
|
||||||
|
{
|
||||||
|
auto const lookup{state.content.find(restored)};
|
||||||
|
if (lookup != state.content.end())
|
||||||
|
{
|
||||||
|
std::ofstream{path(restored), std::ios_base::binary | std::ios_base::trunc} << lookup->second;
|
||||||
|
stat(restored);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::filesystem::remove(path(restored), error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (auto const leftover : written)
|
||||||
|
{
|
||||||
|
std::filesystem::remove(path(leftover) + ".tmp", error);
|
||||||
|
}
|
||||||
|
result.files.clear();
|
||||||
|
return fail("can't replace file \"" + layer(output).name + "\": " + cause);
|
||||||
}
|
}
|
||||||
|
replaced.emplace_back(output);
|
||||||
result.files.emplace_back(layer(output).name);
|
result.files.emplace_back(layer(output).name);
|
||||||
WriteLog("Scenery save: file \"" + layer(output).name + "\" updated");
|
}
|
||||||
|
for (auto const &file : result.files)
|
||||||
|
{
|
||||||
|
WriteLog("Scenery save: file \"" + file + "\" updated");
|
||||||
}
|
}
|
||||||
|
|
||||||
// the scenery files now match the scene, update the bookkeeping to reflect it
|
// the scenery files now match the scene, update the bookkeeping to reflect it
|
||||||
|
|||||||
Reference in New Issue
Block a user