Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in Part compatibility deserialization and add a test #3199

Merged
merged 2 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions ksp_plugin/part.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,17 @@ not_null<std::unique_ptr<Part>> Part::ReadFromMessage(
}
}
} else if (is_pre_ζήνων) {
DiscreteTrajectorySegmentIterator<Barycentric> history;
DiscreteTrajectorySegmentIterator<Barycentric> psychohistory;
auto prehistory = DiscreteTraject0ry<Barycentric>::ReadFromMessage(
message.prehistory(),
/*tracked=*/{&part->history_, &part->psychohistory_});
part->trajectory_ = prehistory.DetachSegments(part->history_);
/*tracked=*/{&history, &psychohistory});
// We want to get rid of the prehistory segment, so the easiest is to detach
// the history and psychohistory. We must take care of the segment
// iterators as they get invalidated in this case.
part->trajectory_ = prehistory.DetachSegments(history);
part->history_ = part->trajectory_.segments().begin();
part->psychohistory_ = std::next(part->history_);
} else {
part->trajectory_ = DiscreteTraject0ry<Barycentric>::ReadFromMessage(
message.prehistory(),
Expand Down
24 changes: 24 additions & 0 deletions ksp_plugin_test/plugin_compatibility_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ using ksp_plugin::Barycentric;
using ksp_plugin::Plugin;
using physics::DiscreteTraject0ry;
using quantities::Speed;
using quantities::si::Degree;
using quantities::si::Kilo;
using quantities::si::Second;
using testing_utilities::operator""_⑴;
using testing_utilities::IsNear;
using testing_utilities::ReadFromBinaryFile;
Expand Down Expand Up @@ -386,6 +388,28 @@ TEST_F(PluginCompatibilityTest, DISABLED_Lpg) {
WriteAndReadBack(std::move(plugin));
}

TEST_F(PluginCompatibilityTest, DISABLED_Egg) {
StringLogSink log_warning(google::WARNING);
not_null<std::unique_ptr<Plugin const>> plugin = ReadPluginFromFile(
R"(P:\Public Mockingbird\Principia\Saves\3136\3136b.proto.b64)",
/*compressor=*/"gipfeli",
/*decoder=*/"base64");
EXPECT_THAT(log_warning.string(),
AllOf(HasSubstr(u8"pre-Ζήνων"), Not(HasSubstr("pre-Haar"))));

auto& mutable_plugin = const_cast<Plugin&>(*plugin);

// This would fail if segment iterators were invalidated during part
// deserialization.
mutable_plugin.AdvanceTime(
mutable_plugin.GameEpoch() + 133218.91123694609 * Second,
295.52698460805016 * Degree);
mutable_plugin.CatchUpVessel("1e07aaa2-d1f8-4f6d-8b32-495b46109d98");

// Make sure that we can upgrade, save, and reload.
WriteAndReadBack(std::move(plugin));
}

// Use for debugging saves given by users.
TEST_F(PluginCompatibilityTest, DISABLED_SECULAR_Debug) {
CheckSaveCompatibility(
Expand Down
2 changes: 2 additions & 0 deletions physics/discrete_traject0ry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class DiscreteTraject0ry : public Trajectory<Frame> {

SegmentIterator NewSegment();

// All the point and segment iterators in the attached/detached segments are
// invalidated.
DiscreteTraject0ry DetachSegments(SegmentIterator begin);
SegmentIterator AttachSegments(DiscreteTraject0ry&& trajectory);
void DeleteSegments(SegmentIterator& begin);
Expand Down