Merge pull request #5448 from zhaowenlan1779/rerecording

Implement basic rerecording features
This commit is contained in:
bunnei 2022-02-18 20:29:36 -07:00 committed by GitHub
commit 62753e882e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 971 additions and 241 deletions

View file

@ -630,6 +630,7 @@ void System::serialize(Archive& ar, const unsigned int file_version) {
// This needs to be set from somewhere - might as well be here!
if (Archive::is_loading::value) {
timing->UnlockEventQueue();
Service::GSP::SetGlobalModule(*this);
memory->SetDSP(*dsp_core);
cheat_engine->Connect();

View file

@ -49,6 +49,10 @@ TimingEventType* Timing::RegisterEvent(const std::string& name, TimedCallback ca
void Timing::ScheduleEvent(s64 cycles_into_future, const TimingEventType* event_type, u64 userdata,
std::size_t core_id) {
if (event_queue_locked) {
return;
}
ASSERT(event_type != nullptr);
Timing::Timer* timer = nullptr;
if (core_id == std::numeric_limits<std::size_t>::max()) {
@ -74,6 +78,9 @@ void Timing::ScheduleEvent(s64 cycles_into_future, const TimingEventType* event_
}
void Timing::UnscheduleEvent(const TimingEventType* event_type, u64 userdata) {
if (event_queue_locked) {
return;
}
for (auto timer : timers) {
auto itr = std::remove_if(
timer->event_queue.begin(), timer->event_queue.end(),
@ -89,6 +96,9 @@ void Timing::UnscheduleEvent(const TimingEventType* event_type, u64 userdata) {
}
void Timing::RemoveEvent(const TimingEventType* event_type) {
if (event_queue_locked) {
return;
}
for (auto timer : timers) {
auto itr = std::remove_if(timer->event_queue.begin(), timer->event_queue.end(),
[&](const Event& e) { return e.type == event_type; });

View file

@ -280,6 +280,11 @@ public:
std::shared_ptr<Timer> GetTimer(std::size_t cpu_id);
// Used after deserializing to unprotect the event queue.
void UnlockEventQueue() {
event_queue_locked = false;
}
private:
// unordered_map stores each element separately as a linked list node so pointers to
// elements remain stable regardless of rehashes/resizing.
@ -292,6 +297,10 @@ private:
// under/overclocking the guest cpu
double cpu_clock_scale = 1.0;
// When true, the event queue can't be modified. Used while deserializing to workaround
// destructor side effects.
bool event_queue_locked = false;
template <class Archive>
void serialize(Archive& ar, const unsigned int file_version) {
// event_types set during initialization of other things
@ -303,6 +312,9 @@ private:
} else {
ar& current_timer;
}
if (Archive::is_loading::value) {
event_queue_locked = true;
}
}
friend class boost::serialization::access;
};

View file

@ -12,7 +12,6 @@
#include "common/logging/log.h"
#include "core/3ds.h"
#include "core/core.h"
#include "core/core_timing.h"
#include "core/hle/ipc_helpers.h"
#include "core/hle/kernel/event.h"
#include "core/hle/kernel/handle_table.h"
@ -55,11 +54,6 @@ void Module::serialize(Archive& ar, const unsigned int file_version) {
}
SERIALIZE_IMPL(Module)
// Updating period for each HID device. These empirical values are measured from a 11.2 3DS.
constexpr u64 pad_update_ticks = BASE_CLOCK_RATE_ARM11 / 234;
constexpr u64 accelerometer_update_ticks = BASE_CLOCK_RATE_ARM11 / 104;
constexpr u64 gyroscope_update_ticks = BASE_CLOCK_RATE_ARM11 / 101;
constexpr float accelerometer_coef = 512.0f; // measured from hw test result
constexpr float gyroscope_coef = 14.375f; // got from hwtest GetGyroscopeLowRawToDpsCoefficient call

View file

@ -13,6 +13,7 @@
#include "common/bit_field.h"
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "core/core_timing.h"
#include "core/frontend/input.h"
#include "core/hle/service/service.h"
#include "core/settings.h"
@ -299,6 +300,11 @@ public:
const PadState& GetState() const;
// Updating period for each HID device. These empirical values are measured from a 11.2 3DS.
static constexpr u64 pad_update_ticks = BASE_CLOCK_RATE_ARM11 / 234;
static constexpr u64 accelerometer_update_ticks = BASE_CLOCK_RATE_ARM11 / 104;
static constexpr u64 gyroscope_update_ticks = BASE_CLOCK_RATE_ARM11 / 101;
private:
void LoadInputDevices();
void UpdatePadCallback(u64 userdata, s64 cycles_late);

View file

@ -2,11 +2,14 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <algorithm>
#include <cstring>
#include <stdexcept>
#include <string>
#include <vector>
#include <boost/optional.hpp>
#include <cryptopp/hex.h>
#include <cryptopp/osrng.h>
#include "common/bit_field.h"
#include "common/common_types.h"
#include "common/file_util.h"
@ -25,8 +28,6 @@ namespace Core {
/*static*/ Movie Movie::s_instance;
enum class PlayMode { None, Recording, Playing };
enum class ControllerStateType : u8 {
PadAndCircle,
Touch,
@ -117,24 +118,120 @@ struct CTMHeader {
u64_le program_id; /// ID of the ROM being executed. Also called title_id
std::array<u8, 20> revision; /// Git hash of the revision this movie was created with
u64_le clock_init_time; /// The init time of the system clock
u64_le id; /// Unique identifier of the movie, used to support separate savestate slots
std::array<char, 32> author; /// Author of the movie
u32_le rerecord_count; /// Number of rerecords when making the movie
u64_le input_count; /// Number of inputs (button and pad states) when making the movie
std::array<u8, 216> reserved; /// Make heading 256 bytes so it has consistent size
std::array<u8, 164> reserved; /// Make heading 256 bytes so it has consistent size
};
static_assert(sizeof(CTMHeader) == 256, "CTMHeader should be 256 bytes");
#pragma pack(pop)
bool Movie::IsPlayingInput() const {
return play_mode == PlayMode::Playing;
static u64 GetInputCount(const std::vector<u8>& input) {
u64 input_count = 0;
for (std::size_t pos = 0; pos < input.size(); pos += sizeof(ControllerState)) {
if (input.size() < pos + sizeof(ControllerState)) {
break;
}
ControllerState state;
std::memcpy(&state, input.data() + pos, sizeof(ControllerState));
if (state.type == ControllerStateType::PadAndCircle) {
input_count++;
}
}
return input_count;
}
bool Movie::IsRecordingInput() const {
return play_mode == PlayMode::Recording;
template <class Archive>
void Movie::serialize(Archive& ar, const unsigned int file_version) {
// Only serialize what's needed to make savestates useful for TAS:
u64 _current_byte = static_cast<u64>(current_byte);
ar& _current_byte;
current_byte = static_cast<std::size_t>(_current_byte);
if (file_version > 0) {
ar& current_input;
}
std::vector<u8> recorded_input_ = recorded_input;
ar& recorded_input_;
ar& init_time;
if (file_version > 0) {
if (Archive::is_loading::value) {
u64 savestate_movie_id;
ar& savestate_movie_id;
if (id != savestate_movie_id) {
if (savestate_movie_id == 0) {
throw std::runtime_error("You must close your movie to load this state");
} else {
throw std::runtime_error("You must load the same movie to load this state");
}
}
} else {
ar& id;
}
}
// Whether the state was made in MovieFinished state
bool post_movie = play_mode == PlayMode::MovieFinished;
if (file_version > 0) {
ar& post_movie;
}
if (Archive::is_loading::value && id != 0) {
if (!read_only) {
recorded_input = std::move(recorded_input_);
}
if (post_movie) {
play_mode = PlayMode::MovieFinished;
return;
}
if (read_only) {
if (play_mode == PlayMode::Recording) {
SaveMovie();
}
if (recorded_input_.size() >= recorded_input.size()) {
throw std::runtime_error("Future event savestate not allowed in R/O mode");
}
// Ensure that the current movie and savestate movie are in the same timeline
if (std::mismatch(recorded_input_.begin(), recorded_input_.end(),
recorded_input.begin())
.first != recorded_input_.end()) {
throw std::runtime_error("Timeline mismatch not allowed in R/O mode");
}
play_mode = PlayMode::Playing;
total_input = GetInputCount(recorded_input);
} else {
play_mode = PlayMode::Recording;
rerecord_count++;
}
}
}
SERIALIZE_IMPL(Movie)
Movie::PlayMode Movie::GetPlayMode() const {
return play_mode;
}
u64 Movie::GetCurrentInputIndex() const {
return current_input;
}
u64 Movie::GetTotalInputCount() const {
return total_input;
}
void Movie::CheckInputEnd() {
if (current_byte + sizeof(ControllerState) > recorded_input.size()) {
LOG_INFO(Movie, "Playback finished");
play_mode = PlayMode::None;
init_time = 0;
play_mode = PlayMode::MovieFinished;
playback_completion_callback();
}
}
@ -143,6 +240,7 @@ void Movie::Play(Service::HID::PadState& pad_state, s16& circle_pad_x, s16& circ
ControllerState s;
std::memcpy(&s, &recorded_input[current_byte], sizeof(ControllerState));
current_byte += sizeof(ControllerState);
current_input++;
if (s.type != ControllerStateType::PadAndCircle) {
LOG_ERROR(Movie,
@ -270,6 +368,8 @@ void Movie::Record(const ControllerState& controller_state) {
void Movie::Record(const Service::HID::PadState& pad_state, const s16& circle_pad_x,
const s16& circle_pad_y) {
current_input++;
ControllerState s;
s.type = ControllerStateType::PadAndCircle;
@ -358,21 +458,13 @@ u64 Movie::GetOverrideInitTime() const {
return init_time;
}
Movie::ValidationResult Movie::ValidateHeader(const CTMHeader& header, u64 program_id) const {
Movie::ValidationResult Movie::ValidateHeader(const CTMHeader& header) const {
if (header_magic_bytes != header.filetype) {
LOG_ERROR(Movie, "Playback file does not have valid header");
return ValidationResult::Invalid;
}
std::string revision = fmt::format("{:02x}", fmt::join(header.revision, ""));
if (!program_id)
Core::System::GetInstance().GetAppLoader().ReadProgramId(program_id);
if (program_id != header.program_id) {
LOG_WARNING(Movie, "This movie was recorded using a ROM with a different program id");
return ValidationResult::GameDismatch;
}
if (revision != Common::g_scm_rev) {
LOG_WARNING(Movie,
"This movie was created on a different version of Citra, playback may desync");
@ -382,6 +474,12 @@ Movie::ValidationResult Movie::ValidateHeader(const CTMHeader& header, u64 progr
return ValidationResult::OK;
}
Movie::ValidationResult Movie::ValidateInput(const std::vector<u8>& input,
u64 expected_count) const {
return GetInputCount(input) == expected_count ? ValidationResult::OK
: ValidationResult::InputCountDismatch;
}
void Movie::SaveMovie() {
LOG_INFO(Movie, "Saving recorded movie to '{}'", record_movie_file);
FileUtil::IOFile save_record(record_movie_file, "wb");
@ -393,9 +491,15 @@ void Movie::SaveMovie() {
CTMHeader header = {};
header.filetype = header_magic_bytes;
header.program_id = program_id;
header.clock_init_time = init_time;
header.id = id;
Core::System::GetInstance().GetAppLoader().ReadProgramId(header.program_id);
std::memcpy(header.author.data(), record_movie_author.data(),
std::min(header.author.size(), record_movie_author.size()));
header.rerecord_count = rerecord_count;
header.input_count = GetInputCount(recorded_input);
std::string rev_bytes;
CryptoPP::StringSource(Common::g_scm_rev, true,
@ -410,8 +514,11 @@ void Movie::SaveMovie() {
}
}
void Movie::StartPlayback(const std::string& movie_file,
std::function<void()> completion_callback) {
void Movie::SetPlaybackCompletionCallback(std::function<void()> completion_callback) {
playback_completion_callback = completion_callback;
}
void Movie::StartPlayback(const std::string& movie_file) {
LOG_INFO(Movie, "Loading Movie for playback");
FileUtil::IOFile save_record(movie_file, "rb");
const u64 size = save_record.GetSize();
@ -421,20 +528,49 @@ void Movie::StartPlayback(const std::string& movie_file,
save_record.ReadArray(&header, 1);
if (ValidateHeader(header) != ValidationResult::Invalid) {
play_mode = PlayMode::Playing;
record_movie_file = movie_file;
std::array<char, 33> author{}; // Add a null terminator
std::memcpy(author.data(), header.author.data(), header.author.size());
record_movie_author = author.data();
rerecord_count = header.rerecord_count;
total_input = header.input_count;
recorded_input.resize(size - sizeof(CTMHeader));
save_record.ReadArray(recorded_input.data(), recorded_input.size());
current_byte = 0;
playback_completion_callback = completion_callback;
current_input = 0;
id = header.id;
program_id = header.program_id;
LOG_INFO(Movie, "Loaded Movie, ID: {:016X}", id);
}
} else {
LOG_ERROR(Movie, "Failed to playback movie: Unable to open '{}'", movie_file);
}
}
void Movie::StartRecording(const std::string& movie_file) {
LOG_INFO(Movie, "Enabling Movie recording");
void Movie::StartRecording(const std::string& movie_file, const std::string& author) {
play_mode = PlayMode::Recording;
record_movie_file = movie_file;
record_movie_author = author;
rerecord_count = 1;
// Generate a random ID
CryptoPP::AutoSeededRandomPool rng;
rng.GenerateBlock(reinterpret_cast<CryptoPP::byte*>(&id), sizeof(id));
// Get program ID
program_id = 0;
Core::System::GetInstance().GetAppLoader().ReadProgramId(program_id);
LOG_INFO(Movie, "Enabling Movie recording, ID: {:016X}", id);
}
void Movie::SetReadOnly(bool read_only_) {
read_only = read_only_;
}
static boost::optional<CTMHeader> ReadHeader(const std::string& movie_file) {
@ -469,25 +605,51 @@ void Movie::PrepareForRecording() {
: Settings::values.init_time);
}
Movie::ValidationResult Movie::ValidateMovie(const std::string& movie_file, u64 program_id) const {
Movie::ValidationResult Movie::ValidateMovie(const std::string& movie_file) const {
LOG_INFO(Movie, "Validating Movie file '{}'", movie_file);
auto header = ReadHeader(movie_file);
if (header == boost::none)
return ValidationResult::Invalid;
return ValidateHeader(header.value(), program_id);
FileUtil::IOFile save_record(movie_file, "rb");
const u64 size = save_record.GetSize();
if (!save_record || size <= sizeof(CTMHeader)) {
return ValidationResult::Invalid;
}
CTMHeader header;
save_record.ReadArray(&header, 1);
if (header_magic_bytes != header.filetype) {
return ValidationResult::Invalid;
}
auto result = ValidateHeader(header);
if (result != ValidationResult::OK) {
return result;
}
if (!header.input_count) { // Probably created by an older version.
return ValidationResult::OK;
}
std::vector<u8> input(size - sizeof(header));
save_record.ReadArray(input.data(), input.size());
return ValidateInput(input, header.input_count);
}
u64 Movie::GetMovieProgramID(const std::string& movie_file) const {
Movie::MovieMetadata Movie::GetMovieMetadata(const std::string& movie_file) const {
auto header = ReadHeader(movie_file);
if (header == boost::none)
return 0;
return {};
return static_cast<u64>(header.value().program_id);
std::array<char, 33> author{}; // Add a null terminator
std::memcpy(author.data(), header->author.data(), header->author.size());
return {header->program_id, std::string{author.data()}, header->rerecord_count,
header->input_count};
}
void Movie::Shutdown() {
if (IsRecordingInput()) {
if (play_mode == PlayMode::Recording) {
SaveMovie();
}
@ -495,16 +657,18 @@ void Movie::Shutdown() {
recorded_input.resize(0);
record_movie_file.clear();
current_byte = 0;
current_input = 0;
init_time = 0;
id = 0;
}
template <typename... Targs>
void Movie::Handle(Targs&... Fargs) {
if (IsPlayingInput()) {
if (play_mode == PlayMode::Playing) {
ASSERT(current_byte + sizeof(ControllerState) <= recorded_input.size());
Play(Fargs...);
CheckInputEnd();
} else if (IsRecordingInput()) {
} else if (play_mode == PlayMode::Recording) {
Record(Fargs...);
}
}

View file

@ -24,14 +24,14 @@ union PadState;
namespace Core {
struct CTMHeader;
struct ControllerState;
enum class PlayMode;
class Movie {
public:
enum class PlayMode { None, Recording, Playing, MovieFinished };
enum class ValidationResult {
OK,
RevisionDismatch,
GameDismatch,
InputCountDismatch,
Invalid,
};
/**
@ -42,9 +42,21 @@ public:
return s_instance;
}
void StartPlayback(
const std::string& movie_file, std::function<void()> completion_callback = [] {});
void StartRecording(const std::string& movie_file);
void SetPlaybackCompletionCallback(std::function<void()> completion_callback);
void StartPlayback(const std::string& movie_file);
void StartRecording(const std::string& movie_file, const std::string& author);
/**
* Sets the read-only status.
* When true, movies will be opened in read-only mode. Loading a state will resume playback
* from that state.
* When false, movies will be opened in read/write mode. Loading a state will start recording
* from that state (rerecording). To start rerecording without loading a state, one can save
* and then immediately load while in R/W.
*
* The default is true.
*/
void SetReadOnly(bool read_only);
/// Prepare to override the clock before playing back movies
void PrepareForPlayback(const std::string& movie_file);
@ -52,11 +64,23 @@ public:
/// Prepare to override the clock before recording movies
void PrepareForRecording();
ValidationResult ValidateMovie(const std::string& movie_file, u64 program_id = 0) const;
ValidationResult ValidateMovie(const std::string& movie_file) const;
/// Get the init time that would override the one in the settings
u64 GetOverrideInitTime() const;
u64 GetMovieProgramID(const std::string& movie_file) const;
struct MovieMetadata {
u64 program_id;
std::string author;
u32 rerecord_count;
u64 input_count;
};
MovieMetadata GetMovieMetadata(const std::string& movie_file) const;
/// Get the current movie's unique ID. Used to provide separate savestate slots for movies.
u64 GetCurrentMovieID() const {
return id;
}
void Shutdown();
@ -96,8 +120,16 @@ public:
* When playing: Replaces the given input states with the ones stored in the playback file
*/
void HandleExtraHidResponse(Service::IR::ExtraHIDResponse& extra_hid_response);
bool IsPlayingInput() const;
bool IsRecordingInput() const;
PlayMode GetPlayMode() const;
u64 GetCurrentInputIndex() const;
u64 GetTotalInputCount() const;
/**
* Saves the movie immediately, in its current state.
* This is called in Shutdown.
*/
void SaveMovie();
private:
static Movie s_instance;
@ -123,26 +155,33 @@ private:
void Record(const Service::IR::PadState& pad_state, const s16& c_stick_x, const s16& c_stick_y);
void Record(const Service::IR::ExtraHIDResponse& extra_hid_response);
ValidationResult ValidateHeader(const CTMHeader& header, u64 program_id = 0) const;
void SaveMovie();
ValidationResult ValidateHeader(const CTMHeader& header) const;
ValidationResult ValidateInput(const std::vector<u8>& input, u64 expected_count) const;
PlayMode play_mode;
std::string record_movie_file;
std::string record_movie_author;
u64 init_time; // Clock init time override for RNG consistency
std::vector<u8> recorded_input;
u64 init_time;
std::function<void()> playback_completion_callback;
std::size_t current_byte = 0;
u64 current_input = 0;
// Total input count of the current movie being played. Not used for recording.
u64 total_input = 0;
u64 id = 0; // ID of the current movie loaded
u64 program_id = 0;
u32 rerecord_count = 1;
bool read_only = true;
std::function<void()> playback_completion_callback = [] {};
template <class Archive>
void serialize(Archive& ar, const unsigned int) {
// Only serialize what's needed to make savestates useful for TAS:
u64 _current_byte = static_cast<u64>(current_byte);
ar& _current_byte;
current_byte = static_cast<std::size_t>(_current_byte);
ar& recorded_input;
ar& init_time;
}
void serialize(Archive& ar, const unsigned int file_version);
friend class boost::serialization::access;
};
} // namespace Core
} // namespace Core
BOOST_CLASS_VERSION(Core::Movie, 1)

View file

@ -169,6 +169,10 @@ void FrameLimiter::DoFrameLimiting(microseconds current_system_time_us) {
previous_walltime = now;
}
bool FrameLimiter::IsFrameAdvancing() const {
return frame_advancing_enabled;
}
void FrameLimiter::SetFrameAdvancing(bool value) {
const bool was_enabled = frame_advancing_enabled.exchange(value);
if (was_enabled && !value) {

View file

@ -90,6 +90,7 @@ public:
void DoFrameLimiting(std::chrono::microseconds current_system_time_us);
bool IsFrameAdvancing() const;
/**
* Sets whether frame advancing is enabled or not.
* Note: The frontend must cancel frame advancing before shutting down in order

View file

@ -11,6 +11,7 @@
#include "common/zstd_compression.h"
#include "core/cheats/cheats.h"
#include "core/core.h"
#include "core/movie.h"
#include "core/savestate.h"
#include "network/network.h"
#include "video_core/video_core.h"
@ -37,8 +38,15 @@ static_assert(sizeof(CSTHeader) == 256, "CSTHeader should be 256 bytes");
constexpr std::array<u8, 4> header_magic_bytes{{'C', 'S', 'T', 0x1B}};
std::string GetSaveStatePath(u64 program_id, u32 slot) {
return fmt::format("{}{:016X}.{:02d}.cst", FileUtil::GetUserPath(FileUtil::UserPath::StatesDir),
program_id, slot);
const u64 movie_id = Movie::GetInstance().GetCurrentMovieID();
if (movie_id) {
return fmt::format("{}{:016X}.movie{:016X}.{:02d}.cst",
FileUtil::GetUserPath(FileUtil::UserPath::StatesDir), program_id,
movie_id, slot);
} else {
return fmt::format("{}{:016X}.{:02d}.cst",
FileUtil::GetUserPath(FileUtil::UserPath::StatesDir), program_id, slot);
}
}
std::vector<SaveStateInfo> ListSaveStates(u64 program_id) {