2015-05-24 06:55:12 +02:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-05 03:22:19 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2008-10-16 22:06:06 +00:00
|
|
|
// Emulator state saving support.
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-10 13:54:46 -05:00
|
|
|
#pragma once
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2022-05-24 02:53:03 +02:00
|
|
|
#include <cstddef>
|
2017-04-03 05:39:48 -04:00
|
|
|
#include <functional>
|
2009-07-12 21:58:32 +00:00
|
|
|
#include <string>
|
2023-10-02 00:56:35 -04:00
|
|
|
#include <type_traits>
|
2011-12-28 02:33:41 -06:00
|
|
|
#include <vector>
|
2009-07-12 21:58:32 +00:00
|
|
|
|
2014-10-21 02:01:38 -04:00
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
|
2011-03-17 10:17:45 +00:00
|
|
|
namespace State
|
2009-06-28 21:11:51 +00:00
|
|
|
{
|
2012-11-08 08:40:49 +01:00
|
|
|
// number of states
|
2013-08-02 20:42:30 -04:00
|
|
|
static const u32 NUM_STATES = 10;
|
2012-11-08 08:40:49 +01:00
|
|
|
|
2023-10-02 00:56:35 -04:00
|
|
|
struct StateHeaderLegacy
|
2012-11-08 08:40:49 +01:00
|
|
|
{
|
2023-10-02 00:56:35 -04:00
|
|
|
char game_id[6];
|
|
|
|
char reserved1[2];
|
|
|
|
u32 lzo_size = 0; // Must be zero for new states. Used to support legacy decompression algorithm.
|
|
|
|
char reserved2[4];
|
2012-11-08 08:40:49 +01:00
|
|
|
double time;
|
|
|
|
};
|
2023-10-02 00:56:35 -04:00
|
|
|
constexpr size_t STATE_HEADER_SIZE = sizeof(StateHeaderLegacy);
|
2022-05-24 02:53:03 +02:00
|
|
|
static_assert(STATE_HEADER_SIZE == 24);
|
2023-10-02 00:56:35 -04:00
|
|
|
static_assert(offsetof(StateHeaderLegacy, lzo_size) == 8);
|
|
|
|
static_assert(offsetof(StateHeaderLegacy, time) == 16);
|
|
|
|
static_assert(std::is_trivially_copyable_v<StateHeaderLegacy>);
|
|
|
|
|
|
|
|
struct StateHeaderVersion
|
|
|
|
{
|
|
|
|
u32 version_cookie;
|
|
|
|
u32 version_string_length;
|
|
|
|
};
|
|
|
|
static_assert(std::is_trivially_copyable_v<StateHeaderVersion>);
|
|
|
|
|
|
|
|
struct StateHeader
|
|
|
|
{
|
|
|
|
StateHeaderLegacy legacy_header;
|
|
|
|
StateHeaderVersion version_header;
|
|
|
|
std::string version_string;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum CompressionType : u16
|
|
|
|
{
|
|
|
|
Uncompressed = 0,
|
|
|
|
LZ4 = 1,
|
|
|
|
// Add new compression types after this, as the compression type
|
|
|
|
// is numerically stored in the state file.
|
|
|
|
};
|
|
|
|
|
|
|
|
struct StateExtendedBaseHeader
|
|
|
|
{
|
|
|
|
u16 header_version;
|
|
|
|
u16 compression_type;
|
|
|
|
u32 payload_offset;
|
|
|
|
u64 uncompressed_size;
|
|
|
|
};
|
|
|
|
constexpr size_t EXTENDED_BASE_HEADER_SIZE = sizeof(StateExtendedBaseHeader);
|
|
|
|
static_assert(EXTENDED_BASE_HEADER_SIZE == 16);
|
|
|
|
static_assert(offsetof(StateExtendedBaseHeader, payload_offset) == 4);
|
|
|
|
static_assert(offsetof(StateExtendedBaseHeader, uncompressed_size) == 8);
|
|
|
|
static_assert(std::is_trivially_copyable_v<StateExtendedBaseHeader>);
|
|
|
|
|
|
|
|
struct StateExtendedHeader
|
|
|
|
{
|
|
|
|
StateExtendedBaseHeader base_header;
|
|
|
|
// Feel free to add new fields here, adjusting COMPRESSED_DATA_OFFSET accordingly, as well as
|
|
|
|
// CreateExtendedHeader(). Add the appropriate IOFile read/write calls within LoadFileStateData()
|
|
|
|
// and WriteHeadersToFile()
|
|
|
|
};
|
2012-11-08 08:40:49 +01:00
|
|
|
|
2011-03-17 10:17:45 +00:00
|
|
|
void Init();
|
2011-10-15 22:19:42 +11:00
|
|
|
|
2011-03-17 10:17:45 +00:00
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
void EnableCompression(bool compression);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-23 23:03:39 +01:00
|
|
|
bool ReadHeader(const std::string& filename, StateHeader& header);
|
2012-11-08 08:40:49 +01:00
|
|
|
|
2015-07-12 01:49:12 +02:00
|
|
|
// Returns a string containing information of the savestate in the given slot
|
|
|
|
// which can be presented to the user for identification purposes
|
2015-12-19 14:36:09 +01:00
|
|
|
std::string GetInfoStringOfSlot(int slot, bool translate = true);
|
2015-07-12 01:49:12 +02:00
|
|
|
|
2020-10-21 19:49:56 +02:00
|
|
|
// Returns when the savestate in the given slot was created, or 0 if the slot is empty.
|
|
|
|
u64 GetUnixTimeOfSlot(int slot);
|
|
|
|
|
2008-08-30 13:06:17 +00:00
|
|
|
// These don't happen instantly - they get scheduled as events.
|
2014-11-13 21:28:27 -05:00
|
|
|
// ...But only if we're not in the main CPU thread.
|
|
|
|
// If we're in the main CPU thread then they run immediately instead
|
2010-04-17 21:02:03 +00:00
|
|
|
// because some things (like Lua) need them to run immediately.
|
2008-08-30 22:06:53 +00:00
|
|
|
// Slots from 0-99.
|
2012-11-08 08:40:49 +01:00
|
|
|
void Save(int slot, bool wait = false);
|
2011-03-17 10:17:45 +00:00
|
|
|
void Load(int slot);
|
2009-06-28 01:11:35 +00:00
|
|
|
|
2012-11-08 08:40:49 +01:00
|
|
|
void SaveAs(const std::string& filename, bool wait = false);
|
2011-03-17 10:17:45 +00:00
|
|
|
void LoadAs(const std::string& filename);
|
2009-09-15 18:54:10 +00:00
|
|
|
|
2011-12-28 02:33:41 -06:00
|
|
|
void SaveToBuffer(std::vector<u8>& buffer);
|
|
|
|
void LoadFromBuffer(std::vector<u8>& buffer);
|
2009-06-28 01:11:35 +00:00
|
|
|
|
2012-11-08 08:40:49 +01:00
|
|
|
void LoadLastSaved(int i = 1);
|
|
|
|
void SaveFirstSaved();
|
2011-03-17 10:17:45 +00:00
|
|
|
void UndoSaveState();
|
|
|
|
void UndoLoadState();
|
2009-11-07 20:01:39 +00:00
|
|
|
|
2011-12-18 01:15:59 -08:00
|
|
|
// for calling back into UI code without introducing a dependency on it in core
|
2017-04-03 05:39:48 -04:00
|
|
|
using AfterLoadCallbackFunc = std::function<void()>;
|
|
|
|
void SetOnAfterLoadCallback(AfterLoadCallbackFunc callback);
|
2019-05-05 23:48:12 +00:00
|
|
|
} // namespace State
|