1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #include <functional>
19 #include <string>
20 #include <vector>
21 
22 #include "Common/File/Path.h"
23 #include "Common/Serialize/Serializer.h"
24 
25 namespace SaveState
26 {
27 	enum class Status {
28 		FAILURE,
29 		WARNING,
30 		SUCCESS,
31 	};
32 	typedef std::function<void(Status status, const std::string &message, void *cbUserData)> Callback;
33 
34 	static const int NUM_SLOTS = 5;
35 	static const char *STATE_EXTENSION = "ppst";
36 	static const char *SCREENSHOT_EXTENSION = "jpg";
37 	static const char *UNDO_STATE_EXTENSION = "undo.ppst";
38 	static const char *UNDO_SCREENSHOT_EXTENSION = "undo.jpg";
39 
40 	static const char *LOAD_UNDO_NAME = "load_undo.ppst";
41 
42 	void Init();
43 	void Shutdown();
44 
45 	// Cycle through the 5 savestate slots
46 	void NextSlot();
47 	void SaveSlot(const Path &gameFilename, int slot, Callback callback, void *cbUserData = 0);
48 	void LoadSlot(const Path &gameFilename, int slot, Callback callback, void *cbUserData = 0);
49 	bool UndoSaveSlot(const Path &gameFilename, int slot);
50 	bool UndoLastSave(const Path &gameFilename);
51 	bool UndoLoad(const Path &gameFilename, Callback callback, void *cbUserData = 0);
52 	// Checks whether there's an existing save in the specified slot.
53 	bool HasSaveInSlot(const Path &gameFilename, int slot);
54 	bool HasUndoSaveInSlot(const Path &gameFilename, int slot);
55 	bool HasUndoLastSave(const Path &gameFilename);
56 	bool HasUndoLoad(const Path &gameFilename);
57 	bool HasScreenshotInSlot(const Path &gameFilename, int slot);
58 
59 	int GetCurrentSlot();
60 
61 	// Returns -1 if there's no oldest/newest slot.
62 	int GetNewestSlot(const Path &gameFilename);
63 	int GetOldestSlot(const Path &gameFilename);
64 
65 	std::string GetSlotDateAsString(const Path &gameFilename, int slot);
66 	std::string GenerateFullDiscId(const Path &gameFilename);
67 	Path GenerateSaveSlotFilename(const Path &gameFilename, int slot, const char *extension);
68 
69 	std::string GetTitle(const Path &filename);
70 
71 	// Load the specified file into the current state (async.)
72 	// Warning: callback will be called on a different thread.
73 	void Load(const Path &filename, int slot, Callback callback = Callback(), void *cbUserData = 0);
74 
75 	// Save the current state to the specified file (async.)
76 	// Warning: callback will be called on a different thread.
77 	void Save(const Path &filename, int slot, Callback callback = Callback(), void *cbUserData = 0);
78 
79 	CChunkFileReader::Error SaveToRam(std::vector<u8> &state);
80 	CChunkFileReader::Error LoadFromRam(std::vector<u8> &state, std::string *errorString);
81 
82 	// For testing / automated tests.  Runs a save state verification pass (async.)
83 	// Warning: callback will be called on a different thread.
84 	void Verify(Callback callback = Callback(), void *cbUserData = 0);
85 
86 	// To go back to a previous snapshot (only if enabled.)
87 	// Warning: callback will be called on a different thread.
88 	void Rewind(Callback callback = Callback(), void *cbUserData = 0);
89 
90 	// Returns true if there are rewind snapshots available.
91 	bool CanRewind();
92 
93 	// Returns true if a savestate has been used during this session.
94 	bool HasLoadedState();
95 
96 	// Returns true if the state has been reused instead of real saves many times.
97 	bool IsStale();
98 
99 	// Returns true if state is from an older PPSSPP version.
100 	bool IsOldVersion();
101 
102 	// Check if there's any save stating needing to be done.  Normally called once per frame.
103 	void Process();
104 
105 	// Notify save state code that new save data has been written.
106 	void NotifySaveData();
107 
108 	// Cleanup by triggering a restart if needed.
109 	void Cleanup();
110 };
111