Files
ppsspp/Core/SaveStateRewind.h
T
Henrik RydgårdandClaude Opus 5.5 e3916c0acc SaveState: Fix rewind ring and savestate slot bugs
Rewind:
- first_ never wrapped, so after about 40 snapshots rewinding past the
  oldest state jumped back to the newest ones. Track a count instead.
- States whose base had been replaced stayed in the ring, and each one
  failed a rewind with an empty error. Drop them when the base goes.
- Rewinding after pausing for longer than the snapshot interval restored a
  snapshot taken just before the rewind ran.
- Rewind snapshots (and verification) counted as saves for IsStale().

Slots:
- Loading an empty slot replaced the load undo backup before failing.
- A refused save (hardcore mode) rotated the slot's screenshots first.
- Refused or dropped operations never called their callback, so auto-load
  didn't notice it had failed.
- The slot listing went stale after a save until something rescanned.

Also: Verify always failed (its write pass never set the buffer start),
and corrupt or truncated files were reported as being from an older
version.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-19 17:29:01 +02:00

87 lines
2.4 KiB
C++

#pragma once
#include <mutex>
#include <thread>
#include "Common/Serialize/Serializer.h"
#include "Common/CommonTypes.h"
#include "Common/TimeUtil.h"
namespace SaveState {
// This ring buffer of states is for rewind save states, which are kept in RAM.
// Save states are compressed against one of two reference saves (bases_), and the reference
// is switched to a fresh save every N saves, where N is BASE_USAGE_INTERVAL.
// The compression is a simple block based scheme where 0 means to copy a block from the base,
// and 1 means that the following bytes are the next block. See Compress/LockedDecompress.
class StateRingbuffer {
public:
StateRingbuffer() {
size_ = REWIND_NUM_STATES;
states_.resize(size_);
baseMapping_.resize(size_);
}
~StateRingbuffer() {
if (compressThread_.joinable()) {
compressThread_.join();
}
}
CChunkFileReader::Error Save();
CChunkFileReader::Error Restore(std::string *errorString, std::string *metadata);
void ScheduleCompress(std::vector<u8> *result, const std::vector<u8> *state, const std::vector<u8> *base);
void Compress(std::vector<u8> &result, const std::vector<u8> &state, const std::vector<u8> &base);
void LockedDecompress(std::vector<u8> &result, const std::vector<u8> &compressed, const std::vector<u8> &base);
void Clear();
bool Empty() const {
return count_ == 0;
}
void Process();
void NotifyState();
double NextStateTimestamp() const;
private:
bool BaseAlive(int generation) const;
const int BLOCK_SIZE = 8192;
const int REWIND_NUM_STATES = 20;
// TODO: Instead, based on size of compressed state?
const int BASE_USAGE_INTERVAL = 15;
typedef std::vector<u8> StateBuffer;
struct RewindState {
StateBuffer stateBuffer;
double savedTime;
bool empty() const { return stateBuffer.empty(); }
void clear() { stateBuffer.clear(); savedTime = 0.0; }
};
// The slot the next save goes in, and how many slots before it hold states we can restore.
int next_ = 0;
int count_ = 0;
int size_;
std::vector<RewindState> states_;
StateBuffer bases_[2];
// Which generation each base slot currently holds, and which generation each state was
// compressed against. There are more states than bases, so states do go stale.
int baseGeneration_[2] = {-1, -1};
int nextBaseGeneration_ = 0;
std::vector<int> baseMapping_;
std::mutex lock_;
std::thread compressThread_;
std::vector<u8> buffer_;
int base_ = -1;
int baseUsage_ = 0;
double rewindLastTime_ = 0.0f;
};
} // namespace SaveState