From 7d63a0dfb8b7cd5ee61bf655d72197ef218e2ca7 Mon Sep 17 00:00:00 2001 From: thrust26 Date: Wed, 11 Nov 2020 13:00:44 +0100 Subject: [PATCH] added individual size to each save state (fixes #727) --- src/common/RewindManager.cxx | 24 ++++++++++++++---------- src/common/RewindManager.hxx | 2 -- src/emucore/Serializer.cxx | 2 ++ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/common/RewindManager.cxx b/src/common/RewindManager.cxx index 70fa481d7..bc6b70813 100644 --- a/src/common/RewindManager.cxx +++ b/src/common/RewindManager.cxx @@ -37,7 +37,6 @@ RewindManager::RewindManager(OSystem& system, StateManager& statemgr) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void RewindManager::setup() { - myStateSize = 0; myLastTimeMachineAdd = false; const string& prefix = myOSystem.settings().getBool("dev.settings") ? "dev." : "plr."; @@ -138,7 +137,6 @@ bool RewindManager::addState(const string& message, bool timeMachine) s.rewind(); // rewind Serializer internal buffers if(myStateManager.saveState(s) && myOSystem.console().tia().saveDisplay(s)) { - myStateSize = std::max(myStateSize, uInt32(s.size())); state.message = message; state.cycles = myOSystem.console().tia().cycles(); myLastTimeMachineAdd = timeMachine; @@ -256,18 +254,22 @@ string RewindManager::saveAllStates() buf.str(""); out.putString(STATE_HEADER); out.putShort(numStates); - out.putInt(myStateSize); - unique_ptr buffer = make_unique(myStateSize); for (uInt32 i = 0; i < numStates; ++i) { RewindState& state = myStateList.current(); Serializer& s = state.data; + uInt32 stateSize = uInt32(s.size()); + unique_ptr buffer = make_unique(stateSize); + + out.putInt(stateSize); + // Rewind Serializer internal buffers s.rewind(); + // Save state - s.getByteArray(buffer.get(), myStateSize); - out.putByteArray(buffer.get(), myStateSize); + s.getByteArray(buffer.get(), stateSize); + out.putByteArray(buffer.get(), stateSize); out.putString(state.message); out.putLong(state.cycles); @@ -310,25 +312,27 @@ string RewindManager::loadAllStates() if (in.getString() != STATE_HEADER) return "Incompatible all states file"; numStates = in.getShort(); - myStateSize = in.getInt(); - unique_ptr buffer = make_unique(myStateSize); for (uInt32 i = 0; i < numStates; ++i) { if (myStateList.full()) compressStates(); + uInt32 stateSize = in.getInt(); + unique_ptr buffer = make_unique(stateSize); + // Add new state at the end of the list (queue adds at end) // This updates the 'current' iterator inside the list myStateList.addLast(); RewindState& state = myStateList.current(); Serializer& s = state.data; + // Rewind Serializer internal buffers s.rewind(); // Fill new state with saved values - in.getByteArray(buffer.get(), myStateSize); - s.putByteArray(buffer.get(), myStateSize); + in.getByteArray(buffer.get(), stateSize); + s.putByteArray(buffer.get(), stateSize); state.message = in.getString(); state.cycles = in.getLong(); } diff --git a/src/common/RewindManager.hxx b/src/common/RewindManager.hxx index 619af7264..5f53083f7 100644 --- a/src/common/RewindManager.hxx +++ b/src/common/RewindManager.hxx @@ -144,7 +144,6 @@ class RewindManager bool atLast() const { return myStateList.atLast(); } void resize(uInt32 size) { myStateList.resize(size); } void clear() { - myStateSize = 0; myStateList.clear(); } @@ -176,7 +175,6 @@ class RewindManager uInt64 myHorizon{0}; double myFactor{0.0}; bool myLastTimeMachineAdd{false}; - uInt32 myStateSize{0}; struct RewindState { Serializer data; // actual save state diff --git a/src/emucore/Serializer.cxx b/src/emucore/Serializer.cxx index 80282bdc2..7f94dced2 100644 --- a/src/emucore/Serializer.cxx +++ b/src/emucore/Serializer.cxx @@ -91,6 +91,8 @@ void Serializer::rewind() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - size_t Serializer::size() const { + myStream->seekp(0, std::ios::end); + return myStream->tellp(); }