diff --git a/src/core/core.cpp b/src/core/core.cpp index 41fcb6855..fa6d0c30d 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -534,6 +534,17 @@ void System::Reset() { template void System::serialize(Archive& ar, const unsigned int file_version) { + if (Archive::is_loading::value) { + // When loading, we want to make sure any lingering state gets cleared out before we begin. + // Shutdown, but persist a few things between loads... + Shutdown(true); + + // Re-initialize everything like it was before + auto system_mode = this->app_loader->LoadKernelSystemMode(); + auto n3ds_mode = this->app_loader->LoadKernelN3dsMode(); + Init(*m_emu_window, *system_mode.first, *n3ds_mode.first); + } + u32 num_cores; if (Archive::is_saving::value) { num_cores = this->GetNumCores(); @@ -565,11 +576,14 @@ void System::serialize(Archive& ar, const unsigned int file_version) { ar&* memory.get(); ar&* kernel.get(); + VideoCore::serialize(ar, file_version); + ar& Movie::GetInstance(); // This needs to be set from somewhere - might as well be here! if (Archive::is_loading::value) { Service::GSP::SetGlobalModule(*this); memory->SetDSP(*dsp_core); + cheat_engine->Connect(); } } diff --git a/src/core/movie.h b/src/core/movie.h index f1be86946..e578b909c 100644 --- a/src/core/movie.h +++ b/src/core/movie.h @@ -5,6 +5,7 @@ #pragma once #include +#include #include "common/common_types.h" namespace Service { @@ -41,8 +42,8 @@ public: return s_instance; } - void StartPlayback(const std::string& movie_file, - std::function completion_callback = [] {}); + void StartPlayback( + const std::string& movie_file, std::function completion_callback = [] {}); void StartRecording(const std::string& movie_file); /// Prepare to override the clock before playing back movies @@ -132,5 +133,16 @@ private: u64 init_time; std::function playback_completion_callback; std::size_t current_byte = 0; + + template + void serialize(Archive& ar, const unsigned int) { + // Only serialize what's needed to make savestates useful for TAS: + u64 _current_byte = static_cast(current_byte); + ar& _current_byte; + current_byte = static_cast(_current_byte); + ar& recorded_input; + ar& init_time; + } + friend class boost::serialization::access; }; } // namespace Core \ No newline at end of file diff --git a/src/core/savestate.cpp b/src/core/savestate.cpp index 6a87569c7..ece6406a1 100644 --- a/src/core/savestate.cpp +++ b/src/core/savestate.cpp @@ -84,13 +84,8 @@ std::vector ListSaveStates(u64 program_id) { void System::SaveState(u32 slot) const { std::ostringstream sstream{std::ios_base::binary}; try { - - { - oarchive oa{sstream}; - oa&* this; - } - VideoCore::Save(sstream); - + oarchive oa{sstream}; + oa&* this; } catch (const std::exception& e) { LOG_ERROR(Core, "Error saving: {}", e.what()); } @@ -159,24 +154,9 @@ void System::LoadState(u32 slot) { std::ios_base::binary}; decompressed.clear(); - // When loading, we want to make sure any lingering state gets cleared out before we begin. - // Shutdown, but persist a few things between loads... - Shutdown(true); - - // Re-initialize everything like it was before - auto system_mode = this->app_loader->LoadKernelSystemMode(); - auto n3ds_mode = this->app_loader->LoadKernelN3dsMode(); - Init(*m_emu_window, *system_mode.first, *n3ds_mode.first); - cheat_engine->Connect(); - try { - - { - iarchive ia{sstream}; - ia&* this; - } - VideoCore::Load(sstream); - + iarchive ia{sstream}; + ia&* this; } catch (const std::exception& e) { LOG_ERROR(Core, "Error loading: {}", e.what()); } diff --git a/src/video_core/video_core.cpp b/src/video_core/video_core.cpp index 5eccd8c7e..f33ec7d57 100644 --- a/src/video_core/video_core.cpp +++ b/src/video_core/video_core.cpp @@ -88,15 +88,11 @@ u16 GetResolutionScaleFactor() { } } -void Save(std::ostream& stream) { - oarchive oa{stream}; - oa& Pica::g_state; -} - -void Load(std::istream& stream) { - iarchive ia{stream}; - ia& Pica::g_state; - // TODO: Flush/reset things +template +void serialize(Archive& ar, const unsigned int) { + ar& Pica::g_state; } } // namespace VideoCore + +SERIALIZE_IMPL(VideoCore) diff --git a/src/video_core/video_core.h b/src/video_core/video_core.h index 10b0d39b6..46cf1e86d 100644 --- a/src/video_core/video_core.h +++ b/src/video_core/video_core.h @@ -62,7 +62,7 @@ void RequestScreenshot(void* data, std::function callback, u16 GetResolutionScaleFactor(); -void Save(std::ostream& stream); -void Load(std::istream& stream); +template +void serialize(Archive& ar, const unsigned int file_version); } // namespace VideoCore