2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
// Extremely simple serialization framework.
|
|
|
|
|
|
|
|
// (mis)-features:
|
|
|
|
// + Super fast
|
|
|
|
// + Very simple
|
|
|
|
// + Same code is used for serialization and deserializaition (in most cases)
|
|
|
|
// - Zero backwards/forwards compatibility
|
|
|
|
// - Serialization code for anything complex has to be manually written.
|
|
|
|
|
2014-09-20 11:32:04 +00:00
|
|
|
#include <array>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstddef>
|
2017-09-09 19:52:35 +00:00
|
|
|
#include <cstring>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <deque>
|
|
|
|
#include <list>
|
2008-12-08 04:46:09 +00:00
|
|
|
#include <map>
|
2019-12-04 03:15:30 +00:00
|
|
|
#include <optional>
|
2013-08-26 00:06:58 +00:00
|
|
|
#include <set>
|
2008-12-08 04:46:09 +00:00
|
|
|
#include <string>
|
2013-04-09 23:57:39 +00:00
|
|
|
#include <type_traits>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <utility>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <vector>
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2015-09-26 20:39:47 +00:00
|
|
|
#include "Common/Assert.h"
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-08-24 23:38:33 +00:00
|
|
|
#include "Common/Flag.h"
|
2020-05-03 17:42:12 +00:00
|
|
|
#include "Common/Inline.h"
|
2015-09-26 20:39:47 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2017-12-26 01:18:13 +00:00
|
|
|
// XXX: Replace this with std::is_trivially_copyable<T> once we stop using volatile
|
|
|
|
// on things that are put in savestates, as volatile types are not trivially copyable.
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool IsTriviallyCopyable = std::is_trivially_copyable<std::remove_volatile_t<T>>::value;
|
2013-08-26 00:06:58 +00:00
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
// Wrapper class
|
2008-12-08 04:46:09 +00:00
|
|
|
class PointerWrap
|
|
|
|
{
|
|
|
|
public:
|
2013-03-02 09:28:44 +00:00
|
|
|
enum Mode
|
|
|
|
{
|
2010-04-17 21:02:03 +00:00
|
|
|
MODE_READ = 1, // load
|
|
|
|
MODE_WRITE, // save
|
|
|
|
MODE_MEASURE, // calculate size
|
|
|
|
MODE_VERIFY, // compare
|
2008-12-08 04:46:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
u8** ptr;
|
|
|
|
Mode mode;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PointerWrap(u8** ptr_, Mode mode_) : ptr(ptr_), mode(mode_) {}
|
2013-03-02 09:28:44 +00:00
|
|
|
void SetMode(Mode mode_) { mode = mode_; }
|
|
|
|
Mode GetMode() const { return mode; }
|
|
|
|
template <typename K, class V>
|
|
|
|
void Do(std::map<K, V>& x)
|
2008-12-08 04:46:09 +00:00
|
|
|
{
|
2013-03-02 09:28:44 +00:00
|
|
|
u32 count = (u32)x.size();
|
|
|
|
Do(count);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
switch (mode)
|
|
|
|
{
|
2009-11-24 17:10:38 +00:00
|
|
|
case MODE_READ:
|
2013-03-02 09:28:44 +00:00
|
|
|
for (x.clear(); count != 0; --count)
|
2009-11-24 17:10:38 +00:00
|
|
|
{
|
2013-03-02 09:28:44 +00:00
|
|
|
std::pair<K, V> pair;
|
|
|
|
Do(pair.first);
|
|
|
|
Do(pair.second);
|
|
|
|
x.insert(pair);
|
2009-11-24 17:10:38 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2009-11-24 17:10:38 +00:00
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
2010-04-17 21:02:03 +00:00
|
|
|
case MODE_VERIFY:
|
2013-10-29 05:09:01 +00:00
|
|
|
for (auto& elem : x)
|
2009-11-24 17:10:38 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
Do(elem.first);
|
|
|
|
Do(elem.second);
|
2009-11-24 17:10:38 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2009-11-24 17:10:38 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-08-26 00:06:58 +00:00
|
|
|
template <typename V>
|
|
|
|
void Do(std::set<V>& x)
|
|
|
|
{
|
|
|
|
u32 count = (u32)x.size();
|
|
|
|
Do(count);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-08-26 00:06:58 +00:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MODE_READ:
|
|
|
|
for (x.clear(); count != 0; --count)
|
|
|
|
{
|
|
|
|
V value;
|
|
|
|
Do(value);
|
|
|
|
x.insert(value);
|
|
|
|
}
|
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-08-26 00:06:58 +00:00
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
|
|
|
case MODE_VERIFY:
|
2017-01-14 16:38:35 +00:00
|
|
|
for (const V& val : x)
|
2013-08-26 00:06:58 +00:00
|
|
|
{
|
2014-02-12 15:00:34 +00:00
|
|
|
Do(val);
|
2013-08-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2013-08-26 00:06:58 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::vector<T>& x)
|
2011-03-22 07:27:23 +00:00
|
|
|
{
|
2019-02-01 02:02:22 +00:00
|
|
|
DoContiguousContainer(x);
|
2008-12-08 04:46:09 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::list<T>& x)
|
2011-05-27 19:55:07 +00:00
|
|
|
{
|
2013-03-02 09:28:44 +00:00
|
|
|
DoContainer(x);
|
2011-05-27 19:55:07 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::deque<T>& x)
|
2008-12-08 04:46:09 +00:00
|
|
|
{
|
2013-03-02 09:28:44 +00:00
|
|
|
DoContainer(x);
|
2008-12-08 04:46:09 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::basic_string<T>& x)
|
2011-12-19 06:01:46 +00:00
|
|
|
{
|
2019-02-01 02:02:22 +00:00
|
|
|
DoContiguousContainer(x);
|
2011-12-19 06:01:46 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-08-26 00:06:58 +00:00
|
|
|
template <typename T, typename U>
|
|
|
|
void Do(std::pair<T, U>& x)
|
|
|
|
{
|
|
|
|
Do(x.first);
|
|
|
|
Do(x.second);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-12-04 03:15:30 +00:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::optional<T>& x)
|
|
|
|
{
|
|
|
|
bool present = x.has_value();
|
|
|
|
Do(present);
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MODE_READ:
|
|
|
|
if (present)
|
|
|
|
{
|
|
|
|
x = std::make_optional<T>();
|
|
|
|
Do(x.value());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = std::nullopt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
|
|
|
case MODE_VERIFY:
|
|
|
|
if (present)
|
|
|
|
Do(x.value());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-20 11:32:04 +00:00
|
|
|
template <typename T, std::size_t N>
|
2015-02-15 19:43:31 +00:00
|
|
|
void DoArray(std::array<T, N>& x)
|
2014-09-20 11:32:04 +00:00
|
|
|
{
|
2019-02-01 02:02:22 +00:00
|
|
|
DoArray(x.data(), static_cast<u32>(x.size()));
|
2014-09-20 11:32:04 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-02-01 02:02:22 +00:00
|
|
|
template <typename T, typename std::enable_if_t<IsTriviallyCopyable<T>, int> = 0>
|
2013-03-02 09:28:44 +00:00
|
|
|
void DoArray(T* x, u32 count)
|
|
|
|
{
|
2014-08-27 23:50:52 +00:00
|
|
|
DoVoid(x, count * sizeof(T));
|
2013-03-20 01:51:12 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2019-02-01 02:02:22 +00:00
|
|
|
template <typename T, typename std::enable_if_t<!IsTriviallyCopyable<T>, int> = 0>
|
|
|
|
void DoArray(T* x, u32 count)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < count; ++i)
|
|
|
|
Do(x[i]);
|
|
|
|
}
|
|
|
|
|
2015-09-29 16:35:30 +00:00
|
|
|
template <typename T, std::size_t N>
|
|
|
|
void DoArray(T (&arr)[N])
|
|
|
|
{
|
|
|
|
DoArray(arr, static_cast<u32>(N));
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-24 23:38:33 +00:00
|
|
|
void Do(Common::Flag& flag)
|
|
|
|
{
|
|
|
|
bool s = flag.IsSet();
|
|
|
|
Do(s);
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
flag.Set(s);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2015-03-31 08:09:33 +00:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::atomic<T>& atomic)
|
|
|
|
{
|
|
|
|
T temp = atomic.load();
|
|
|
|
Do(temp);
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
atomic.store(temp);
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
template <typename T>
|
|
|
|
void Do(T& x)
|
|
|
|
{
|
2017-12-26 01:18:13 +00:00
|
|
|
static_assert(IsTriviallyCopyable<T>, "Only sane for trivially copyable types");
|
2014-08-27 23:50:52 +00:00
|
|
|
// Note:
|
|
|
|
// Usually we can just use x = **ptr, etc. However, this doesn't work
|
|
|
|
// for unions containing BitFields (long story, stupid language rules)
|
|
|
|
// or arrays. This will get optimized anyway.
|
2013-03-02 09:28:44 +00:00
|
|
|
DoVoid((void*)&x, sizeof(x));
|
2008-12-08 04:46:09 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-04-09 23:57:39 +00:00
|
|
|
template <typename T>
|
|
|
|
void DoPOD(T& x)
|
|
|
|
{
|
|
|
|
DoVoid((void*)&x, sizeof(x));
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-15 02:22:34 +00:00
|
|
|
void Do(bool& x)
|
|
|
|
{
|
|
|
|
// bool's size can vary depending on platform, which can
|
|
|
|
// cause breakages. This treats all bools as if they were
|
|
|
|
// 8 bits in size.
|
|
|
|
u8 stable = static_cast<u8>(x);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-15 02:22:34 +00:00
|
|
|
Do(stable);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2016-04-15 02:22:34 +00:00
|
|
|
if (mode == MODE_READ)
|
|
|
|
x = stable != 0;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
template <typename T>
|
|
|
|
void DoPointer(T*& x, T* const base)
|
|
|
|
{
|
2012-01-04 08:42:22 +00:00
|
|
|
// pointers can be more than 2^31 apart, but you're using this function wrong if you need that
|
|
|
|
// much range
|
2013-11-13 09:03:46 +00:00
|
|
|
ptrdiff_t offset = x - base;
|
2012-01-04 08:42:22 +00:00
|
|
|
Do(offset);
|
|
|
|
if (mode == MODE_READ)
|
2013-11-13 09:03:46 +00:00
|
|
|
{
|
2012-01-04 08:42:22 +00:00
|
|
|
x = base + offset;
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2013-11-13 09:03:46 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
void DoMarker(const std::string& prevName, u32 arbitraryNumber = 0x42)
|
2011-12-18 00:49:24 +00:00
|
|
|
{
|
|
|
|
u32 cookie = arbitraryNumber;
|
|
|
|
Do(cookie);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
if (mode == PointerWrap::MODE_READ && cookie != arbitraryNumber)
|
2011-12-18 00:49:24 +00:00
|
|
|
{
|
2020-12-02 18:17:27 +00:00
|
|
|
PanicAlertFmtT(
|
|
|
|
"Error: After \"{0}\", found {1} ({2:#x}) instead of save marker {3} ({4:#x}). Aborting "
|
|
|
|
"savestate load...",
|
|
|
|
prevName, cookie, cookie, arbitraryNumber, arbitraryNumber);
|
2011-12-18 00:49:24 +00:00
|
|
|
mode = PointerWrap::MODE_MEASURE;
|
|
|
|
}
|
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2016-09-01 06:42:52 +00:00
|
|
|
template <typename T, typename Functor>
|
|
|
|
void DoEachElement(T& container, Functor member)
|
|
|
|
{
|
|
|
|
u32 size = static_cast<u32>(container.size());
|
|
|
|
Do(size);
|
|
|
|
container.resize(size);
|
|
|
|
|
|
|
|
for (auto& elem : container)
|
|
|
|
member(*this, elem);
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
private:
|
2019-02-01 02:02:22 +00:00
|
|
|
template <typename T>
|
|
|
|
void DoContiguousContainer(T& container)
|
|
|
|
{
|
|
|
|
u32 size = static_cast<u32>(container.size());
|
|
|
|
Do(size);
|
|
|
|
container.resize(size);
|
|
|
|
|
2019-02-10 03:26:46 +00:00
|
|
|
if (size > 0)
|
|
|
|
DoArray(&container[0], size);
|
2019-02-01 02:02:22 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 03:03:43 +00:00
|
|
|
template <typename T>
|
|
|
|
void DoContainer(T& x)
|
|
|
|
{
|
2016-09-01 06:42:52 +00:00
|
|
|
DoEachElement(x, [](PointerWrap& p, typename T::value_type& elem) { p.Do(elem); });
|
2014-07-06 03:03:43 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2018-06-09 15:47:01 +00:00
|
|
|
DOLPHIN_FORCE_INLINE void DoVoid(void* data, u32 size)
|
2013-03-02 09:28:44 +00:00
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MODE_READ:
|
2014-08-27 23:50:52 +00:00
|
|
|
memcpy(data, *ptr, size);
|
2013-03-02 09:28:44 +00:00
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
case MODE_WRITE:
|
2014-08-27 23:50:52 +00:00
|
|
|
memcpy(*ptr, data, size);
|
2013-03-02 09:28:44 +00:00
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
case MODE_MEASURE:
|
|
|
|
break;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
case MODE_VERIFY:
|
2018-03-15 00:34:35 +00:00
|
|
|
DEBUG_ASSERT_MSG(COMMON, !memcmp(data, *ptr, size),
|
2014-08-27 23:50:52 +00:00
|
|
|
"Savestate verification failure: buf %p != %p (size %u).\n", data, *ptr,
|
|
|
|
size);
|
2013-03-02 09:28:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2014-08-27 23:50:52 +00:00
|
|
|
*ptr += size;
|
2013-03-02 09:28:44 +00:00
|
|
|
}
|
2008-12-08 04:46:09 +00:00
|
|
|
};
|