@@ -138,10 +138,8 @@ void EnableCompression(bool compression)
138138 s_use_compression = compression;
139139}
140140
141- static void DoState (PointerWrap& p)
141+ static void DoState (Core::System& system, PointerWrap& p)
142142{
143- auto & system = Core::System::GetInstance ();
144-
145143 bool is_wii = system.IsWii () || system.IsMIOS ();
146144 const bool is_wii_currently = is_wii;
147145 p.Do (is_wii);
@@ -202,7 +200,7 @@ static void DoState(PointerWrap& p)
202200 p.DoMarker (" Gecko" );
203201}
204202
205- void LoadFromBuffer (std::vector<u8 >& buffer)
203+ void LoadFromBuffer (Core::System& system, std::vector<u8 >& buffer)
206204{
207205 if (NetPlay::IsNetPlayRunning ())
208206 {
@@ -222,25 +220,25 @@ void LoadFromBuffer(std::vector<u8>& buffer)
222220 [&] {
223221 u8 * ptr = buffer.data ();
224222 PointerWrap p (&ptr, buffer.size (), PointerWrap::Mode::Read);
225- DoState (p);
223+ DoState (system, p);
226224 },
227225 true );
228226}
229227
230- void SaveToBuffer (std::vector<u8 >& buffer)
228+ void SaveToBuffer (Core::System& system, std::vector<u8 >& buffer)
231229{
232230 Core::RunOnCPUThread (
233231 [&] {
234232 u8 * ptr = nullptr ;
235233 PointerWrap p_measure (&ptr, 0 , PointerWrap::Mode::Measure);
236234
237- DoState (p_measure);
235+ DoState (system, p_measure);
238236 const size_t buffer_size = reinterpret_cast <size_t >(ptr);
239237 buffer.resize (buffer_size);
240238
241239 ptr = buffer.data ();
242240 PointerWrap p (&ptr, buffer_size, PointerWrap::Mode::Write);
243- DoState (p);
241+ DoState (system, p);
244242 },
245243 true );
246244}
@@ -382,7 +380,7 @@ static void WriteHeadersToFile(size_t uncompressed_size, File::IOFile& f)
382380 // If StateExtendedHeader is amended to include more than the base, add WriteBytes() calls here.
383381}
384382
385- static void CompressAndDumpState (CompressAndDumpState_args& save_args)
383+ static void CompressAndDumpState (Core::System& system, CompressAndDumpState_args& save_args)
386384{
387385 const u8 * const buffer_data = save_args.buffer_vector .data ();
388386 const size_t buffer_size = save_args.buffer_vector .size ();
@@ -442,7 +440,7 @@ static void CompressAndDumpState(CompressAndDumpState_args& save_args)
442440 }
443441 }
444442
445- auto & movie = Core::System::GetInstance () .GetMovie ();
443+ auto & movie = system .GetMovie ();
446444 if ((movie.IsMovieActive ()) && !movie.IsJustStartingRecordingInputFromSaveState ())
447445 movie.SaveRecording (dtmname);
448446 else if (!movie.IsMovieActive ())
@@ -468,7 +466,7 @@ static void CompressAndDumpState(CompressAndDumpState_args& save_args)
468466 Host_UpdateMainFrame ();
469467}
470468
471- void SaveAs (const std::string& filename, bool wait)
469+ void SaveAs (Core::System& system, const std::string& filename, bool wait)
472470{
473471 std::unique_lock lk (s_load_or_save_in_progress_mutex, std::try_to_lock);
474472 if (!lk)
@@ -484,15 +482,15 @@ void SaveAs(const std::string& filename, bool wait)
484482 // Measure the size of the buffer.
485483 u8 * ptr = nullptr ;
486484 PointerWrap p_measure (&ptr, 0 , PointerWrap::Mode::Measure);
487- DoState (p_measure);
485+ DoState (system, p_measure);
488486 const size_t buffer_size = reinterpret_cast <size_t >(ptr);
489487
490488 // Then actually do the write.
491489 std::vector<u8 > current_buffer;
492490 current_buffer.resize (buffer_size);
493491 ptr = current_buffer.data ();
494492 PointerWrap p (&ptr, buffer_size, PointerWrap::Mode::Write);
495- DoState (p);
493+ DoState (system, p);
496494
497495 if (p.IsWriteMode ())
498496 {
@@ -849,7 +847,7 @@ static void LoadFileStateData(const std::string& filename, std::vector<u8>& ret_
849847 ret_data.swap (buffer);
850848}
851849
852- void LoadAs (const std::string& filename)
850+ void LoadAs (Core::System& system, const std::string& filename)
853851{
854852 if (!Core::IsRunning ())
855853 return ;
@@ -875,11 +873,11 @@ void LoadAs(const std::string& filename)
875873 Core::RunOnCPUThread (
876874 [&] {
877875 // Save temp buffer for undo load state
878- auto & movie = Core::System::GetInstance () .GetMovie ();
876+ auto & movie = system .GetMovie ();
879877 if (!movie.IsJustStartingRecordingInputFromSaveState ())
880878 {
881879 std::lock_guard lk2 (s_undo_load_buffer_mutex);
882- SaveToBuffer (s_undo_load_buffer);
880+ SaveToBuffer (system, s_undo_load_buffer);
883881 const std::string dtmpath = File::GetUserPath (D_STATESAVES_IDX) + " undo.dtm" ;
884882 if (movie.IsMovieActive ())
885883 movie.SaveRecording (dtmpath);
@@ -899,7 +897,7 @@ void LoadAs(const std::string& filename)
899897 {
900898 u8 * ptr = buffer.data ();
901899 PointerWrap p (&ptr, buffer.size (), PointerWrap::Mode::Read);
902- DoState (p);
900+ DoState (system, p);
903901 loaded = true ;
904902 loadedSuccessfully = p.IsReadMode ();
905903 }
@@ -923,7 +921,7 @@ void LoadAs(const std::string& filename)
923921 Core::DisplayMessage (" The savestate could not be loaded" , OSD::Duration::NORMAL);
924922
925923 // since we could be in an inconsistent state now (and might crash or whatever), undo.
926- UndoLoadState ();
924+ UndoLoadState (system );
927925 }
928926 }
929927
@@ -938,10 +936,10 @@ void SetOnAfterLoadCallback(AfterLoadCallbackFunc callback)
938936 s_on_after_load_callback = std::move (callback);
939937}
940938
941- void Init ()
939+ void Init (Core::System& system )
942940{
943- s_save_thread.Reset (" Savestate Worker" , [](CompressAndDumpState_args args) {
944- CompressAndDumpState (args);
941+ s_save_thread.Reset (" Savestate Worker" , [&system ](CompressAndDumpState_args args) {
942+ CompressAndDumpState (system, args);
945943
946944 {
947945 std::lock_guard lk (s_state_writes_in_queue_mutex);
@@ -973,17 +971,17 @@ static std::string MakeStateFilename(int number)
973971 SConfig::GetInstance ().GetGameID (), number);
974972}
975973
976- void Save (int slot, bool wait)
974+ void Save (Core::System& system, int slot, bool wait)
977975{
978- SaveAs (MakeStateFilename (slot), wait);
976+ SaveAs (system, MakeStateFilename (slot), wait);
979977}
980978
981- void Load (int slot)
979+ void Load (Core::System& system, int slot)
982980{
983- LoadAs (MakeStateFilename (slot));
981+ LoadAs (system, MakeStateFilename (slot));
984982}
985983
986- void LoadLastSaved (int i)
984+ void LoadLastSaved (Core::System& system, int i)
987985{
988986 if (i <= 0 )
989987 {
@@ -999,38 +997,38 @@ void LoadLastSaved(int i)
999997 }
1000998
1001999 std::stable_sort (used_slots.begin (), used_slots.end (), CompareTimestamp);
1002- Load ((used_slots.end () - i)->slot );
1000+ Load (system, (used_slots.end () - i)->slot );
10031001}
10041002
10051003// must wait for state to be written because it must know if all slots are taken
1006- void SaveFirstSaved ()
1004+ void SaveFirstSaved (Core::System& system )
10071005{
10081006 std::vector<SlotWithTimestamp> used_slots = GetUsedSlotsWithTimestamp ();
10091007 if (used_slots.size () < NUM_STATES)
10101008 {
10111009 // save to an empty slot
1012- Save (GetEmptySlot (used_slots), true );
1010+ Save (system, GetEmptySlot (used_slots), true );
10131011 return ;
10141012 }
10151013
10161014 // overwrite the oldest state
10171015 std::stable_sort (used_slots.begin (), used_slots.end (), CompareTimestamp);
1018- Save (used_slots.front ().slot , true );
1016+ Save (system, used_slots.front ().slot , true );
10191017}
10201018
10211019// Load the last state before loading the state
1022- void UndoLoadState ()
1020+ void UndoLoadState (Core::System& system )
10231021{
10241022 std::lock_guard lk (s_undo_load_buffer_mutex);
10251023 if (!s_undo_load_buffer.empty ())
10261024 {
1027- auto & movie = Core::System::GetInstance () .GetMovie ();
1025+ auto & movie = system .GetMovie ();
10281026 if (movie.IsMovieActive ())
10291027 {
10301028 const std::string dtmpath = File::GetUserPath (D_STATESAVES_IDX) + " undo.dtm" ;
10311029 if (File::Exists (dtmpath))
10321030 {
1033- LoadFromBuffer (s_undo_load_buffer);
1031+ LoadFromBuffer (system, s_undo_load_buffer);
10341032 movie.LoadInput (dtmpath);
10351033 }
10361034 else
@@ -1040,7 +1038,7 @@ void UndoLoadState()
10401038 }
10411039 else
10421040 {
1043- LoadFromBuffer (s_undo_load_buffer);
1041+ LoadFromBuffer (system, s_undo_load_buffer);
10441042 }
10451043 }
10461044 else
@@ -1050,9 +1048,9 @@ void UndoLoadState()
10501048}
10511049
10521050// Load the state that the last save state overwritten on
1053- void UndoSaveState ()
1051+ void UndoSaveState (Core::System& system )
10541052{
1055- LoadAs (File::GetUserPath (D_STATESAVES_IDX) + " lastState.sav" );
1053+ LoadAs (system, File::GetUserPath (D_STATESAVES_IDX) + " lastState.sav" );
10561054}
10571055
10581056} // namespace State
0 commit comments