Skip to content

Commit 3948ac9

Browse files
Merge pull request dolphin-emu#12606 from mitaclaw/state-global-system
State: Avoid Global System Accessor
2 parents f814dc5 + 0d4cb5d commit 3948ac9

10 files changed

Lines changed: 79 additions & 75 deletions

File tree

Source/Android/jni/MainAndroid.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,29 +315,29 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SaveState(JN
315315
jboolean wait)
316316
{
317317
HostThreadLock guard;
318-
State::Save(slot, wait);
318+
State::Save(Core::System::GetInstance(), slot, wait);
319319
}
320320

321321
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SaveStateAs(JNIEnv* env, jclass,
322322
jstring path,
323323
jboolean wait)
324324
{
325325
HostThreadLock guard;
326-
State::SaveAs(GetJString(env, path), wait);
326+
State::SaveAs(Core::System::GetInstance(), GetJString(env, path), wait);
327327
}
328328

329329
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_LoadState(JNIEnv*, jclass,
330330
jint slot)
331331
{
332332
HostThreadLock guard;
333-
State::Load(slot);
333+
State::Load(Core::System::GetInstance(), slot);
334334
}
335335

336336
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_LoadStateAs(JNIEnv* env, jclass,
337337
jstring path)
338338
{
339339
HostThreadLock guard;
340-
State::LoadAs(GetJString(env, path));
340+
State::LoadAs(Core::System::GetInstance(), GetJString(env, path));
341341
}
342342

343343
JNIEXPORT jlong JNICALL

Source/Core/Core/Core.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ static void CpuThread(Core::System& system, const std::optional<std::string>& sa
389389

390390
if (savestate_path)
391391
{
392-
::State::LoadAs(*savestate_path);
392+
::State::LoadAs(system, *savestate_path);
393393
if (delete_savestate)
394394
File::Delete(*savestate_path);
395395
}

Source/Core/Core/HW/HW.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void Init(Core::System& system, const Sram* override_sram)
3535
system.GetCoreTiming().Init();
3636
system.GetSystemTimers().PreInit();
3737

38-
State::Init();
38+
State::Init(system);
3939

4040
// Init the whole Hardware
4141
system.GetAudioInterface().Init();

Source/Core/Core/Movie.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ bool MovieManager::BeginRecordingInput(const ControllerTypeArray& controllers,
541541
if (File::Exists(save_path))
542542
File::Delete(save_path);
543543

544-
State::SaveAs(save_path);
544+
State::SaveAs(m_system, save_path);
545545
m_recording_from_save_state = true;
546546

547547
std::thread md5thread(&MovieManager::GetMD5, this);

Source/Core/Core/State.cpp

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Source/Core/Core/State.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
#include "Common/CommonTypes.h"
1515

16+
namespace Core
17+
{
18+
class System;
19+
}
20+
1621
namespace State
1722
{
1823
// number of states
@@ -75,7 +80,7 @@ struct StateExtendedHeader
7580
// and WriteHeadersToFile()
7681
};
7782

78-
void Init();
83+
void Init(Core::System& system);
7984

8085
void Shutdown();
8186

@@ -95,19 +100,19 @@ u64 GetUnixTimeOfSlot(int slot);
95100
// If we're in the main CPU thread then they run immediately instead
96101
// because some things (like Lua) need them to run immediately.
97102
// Slots from 0-99.
98-
void Save(int slot, bool wait = false);
99-
void Load(int slot);
103+
void Save(Core::System& system, int slot, bool wait = false);
104+
void Load(Core::System& system, int slot);
100105

101-
void SaveAs(const std::string& filename, bool wait = false);
102-
void LoadAs(const std::string& filename);
106+
void SaveAs(Core::System& system, const std::string& filename, bool wait = false);
107+
void LoadAs(Core::System& system, const std::string& filename);
103108

104-
void SaveToBuffer(std::vector<u8>& buffer);
105-
void LoadFromBuffer(std::vector<u8>& buffer);
109+
void SaveToBuffer(Core::System& system, std::vector<u8>& buffer);
110+
void LoadFromBuffer(Core::System& system, std::vector<u8>& buffer);
106111

107-
void LoadLastSaved(int i = 1);
108-
void SaveFirstSaved();
109-
void UndoSaveState();
110-
void UndoLoadState();
112+
void LoadLastSaved(Core::System& system, int i = 1);
113+
void SaveFirstSaved(Core::System& system);
114+
void UndoSaveState(Core::System& system);
115+
void UndoLoadState(Core::System& system);
111116

112117
// for calling back into UI code without introducing a dependency on it in core
113118
using AfterLoadCallbackFunc = std::function<void()>;

Source/Core/DolphinNoGUI/PlatformMacos.mm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,27 @@ - (void)saveScreenShot
5555

5656
- (void)loadLastSaved
5757
{
58-
State::LoadLastSaved();
58+
State::LoadLastSaved(Core::System::GetInstance());
5959
}
6060

6161
- (void)undoLoadState
6262
{
63-
State::UndoLoadState();
63+
State::UndoLoadState(Core::System::GetInstance());
6464
}
6565

6666
- (void)undoSaveState
6767
{
68-
State::UndoSaveState();
68+
State::UndoSaveState(Core::System::GetInstance());
6969
}
7070

7171
- (void)loadState:(id)sender
7272
{
73-
State::Load([sender tag]);
73+
State::Load(Core::System::GetInstance(), [sender tag]);
7474
}
7575

7676
- (void)saveState:(id)sender
7777
{
78-
State::Save([sender tag]);
78+
State::Save(Core::System::GetInstance(), [sender tag]);
7979
}
8080
@end
8181

0 commit comments

Comments
 (0)