2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2021-07-04 19:22:19 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-02-10 11:54:46 -07:00
|
|
|
#pragma once
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2008-08-30 10:05:32 -06:00
|
|
|
// Extremely simple serialization framework.
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2008-08-30 10:05:32 -06:00
|
|
|
// (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.
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-09-20 05:32:04 -06:00
|
|
|
#include <array>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <cstddef>
|
2017-09-09 13:52:35 -06:00
|
|
|
#include <cstring>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <deque>
|
|
|
|
#include <list>
|
2008-08-21 17:28:07 -06:00
|
|
|
#include <map>
|
2019-12-03 20:15:30 -07:00
|
|
|
#include <optional>
|
2013-08-25 18:06:58 -06:00
|
|
|
#include <set>
|
2008-11-03 04:03:36 -07:00
|
|
|
#include <string>
|
2013-04-09 17:57:39 -06:00
|
|
|
#include <type_traits>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <utility>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <vector>
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2021-11-10 15:42:49 -07:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Assert.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2021-04-24 20:26:27 -06:00
|
|
|
#include "Common/EnumMap.h"
|
2014-08-24 17:38:33 -06:00
|
|
|
#include "Common/Flag.h"
|
2020-05-03 11:42:12 -06:00
|
|
|
#include "Common/Inline.h"
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Wrapper class
|
2008-08-30 06:11:25 -06:00
|
|
|
class PointerWrap
|
2008-08-21 17:28:07 -06:00
|
|
|
{
|
|
|
|
public:
|
2022-05-21 22:18:27 -06:00
|
|
|
enum class Mode
|
2013-03-02 02:28:44 -07:00
|
|
|
{
|
2022-05-21 22:18:27 -06:00
|
|
|
Read,
|
|
|
|
Write,
|
|
|
|
Measure,
|
|
|
|
Verify,
|
2008-08-21 17:28:07 -06:00
|
|
|
};
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2022-04-17 19:41:14 -06:00
|
|
|
private:
|
2022-04-17 20:13:25 -06:00
|
|
|
u8** m_ptr_current;
|
|
|
|
u8* m_ptr_end;
|
2022-05-17 23:29:05 -06:00
|
|
|
Mode m_mode;
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2008-08-21 17:28:07 -06:00
|
|
|
public:
|
2022-05-17 23:29:05 -06:00
|
|
|
PointerWrap(u8** ptr, size_t size, Mode mode)
|
|
|
|
: m_ptr_current(ptr), m_ptr_end(*ptr + size), m_mode(mode)
|
2022-04-17 20:13:25 -06:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-21 22:18:27 -06:00
|
|
|
void SetMeasureMode() { m_mode = Mode::Measure; }
|
|
|
|
void SetVerifyMode() { m_mode = Mode::Verify; }
|
|
|
|
bool IsReadMode() const { return m_mode == Mode::Read; }
|
|
|
|
bool IsWriteMode() const { return m_mode == Mode::Write; }
|
|
|
|
bool IsMeasureMode() const { return m_mode == Mode::Measure; }
|
|
|
|
bool IsVerifyMode() const { return m_mode == Mode::Verify; }
|
2022-05-17 23:29:05 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename K, class V>
|
|
|
|
void Do(std::map<K, V>& x)
|
2008-08-30 06:11:25 -06:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
u32 count = (u32)x.size();
|
|
|
|
Do(count);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-05-17 23:29:05 -06:00
|
|
|
switch (m_mode)
|
2013-03-02 02:28:44 -07:00
|
|
|
{
|
2022-05-21 22:18:27 -06:00
|
|
|
case Mode::Read:
|
2013-03-02 02:28:44 -07:00
|
|
|
for (x.clear(); count != 0; --count)
|
2009-11-24 10:10:38 -07:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
std::pair<K, V> pair;
|
|
|
|
Do(pair.first);
|
|
|
|
Do(pair.second);
|
|
|
|
x.insert(pair);
|
2009-11-24 10:10:38 -07:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-05-21 22:18:27 -06:00
|
|
|
case Mode::Write:
|
|
|
|
case Mode::Measure:
|
|
|
|
case Mode::Verify:
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& elem : x)
|
2009-11-24 10:10:38 -07:00
|
|
|
{
|
2013-10-28 23:09:01 -06:00
|
|
|
Do(elem.first);
|
|
|
|
Do(elem.second);
|
2009-11-24 10:10:38 -07:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2009-11-24 10:10:38 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-08-25 18:06:58 -06:00
|
|
|
template <typename V>
|
|
|
|
void Do(std::set<V>& x)
|
|
|
|
{
|
|
|
|
u32 count = (u32)x.size();
|
|
|
|
Do(count);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-05-17 23:29:05 -06:00
|
|
|
switch (m_mode)
|
2013-08-25 18:06:58 -06:00
|
|
|
{
|
2022-05-21 22:18:27 -06:00
|
|
|
case Mode::Read:
|
2013-08-25 18:06:58 -06:00
|
|
|
for (x.clear(); count != 0; --count)
|
|
|
|
{
|
2022-05-01 02:12:28 -06:00
|
|
|
V value = {};
|
2013-08-25 18:06:58 -06:00
|
|
|
Do(value);
|
|
|
|
x.insert(value);
|
|
|
|
}
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-05-21 22:18:27 -06:00
|
|
|
case Mode::Write:
|
|
|
|
case Mode::Measure:
|
|
|
|
case Mode::Verify:
|
2017-01-14 09:38:35 -07:00
|
|
|
for (const V& val : x)
|
2013-08-25 18:06:58 -06:00
|
|
|
{
|
2014-02-12 08:00:34 -07:00
|
|
|
Do(val);
|
2013-08-25 18:06:58 -06:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2013-08-25 18:06:58 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::vector<T>& x)
|
2011-03-22 01:27:23 -06:00
|
|
|
{
|
2019-01-31 19:02:22 -07:00
|
|
|
DoContiguousContainer(x);
|
2008-08-21 17:28:07 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::list<T>& x)
|
2011-05-27 13:55:07 -06:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
DoContainer(x);
|
2011-05-27 13:55:07 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::deque<T>& x)
|
2008-11-03 04:03:36 -07:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
DoContainer(x);
|
2008-11-03 04:03:36 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::basic_string<T>& x)
|
2011-12-18 23:01:46 -07:00
|
|
|
{
|
2019-01-31 19:02:22 -07:00
|
|
|
DoContiguousContainer(x);
|
2011-12-18 23:01:46 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-08-25 18:06:58 -06:00
|
|
|
template <typename T, typename U>
|
|
|
|
void Do(std::pair<T, U>& x)
|
|
|
|
{
|
|
|
|
Do(x.first);
|
|
|
|
Do(x.second);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-12-03 20:15:30 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::optional<T>& x)
|
|
|
|
{
|
|
|
|
bool present = x.has_value();
|
|
|
|
Do(present);
|
|
|
|
|
2022-05-17 23:29:05 -06:00
|
|
|
switch (m_mode)
|
2019-12-03 20:15:30 -07:00
|
|
|
{
|
2022-05-21 22:18:27 -06:00
|
|
|
case Mode::Read:
|
2019-12-03 20:15:30 -07:00
|
|
|
if (present)
|
|
|
|
{
|
|
|
|
x = std::make_optional<T>();
|
|
|
|
Do(x.value());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x = std::nullopt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-05-21 22:18:27 -06:00
|
|
|
case Mode::Write:
|
|
|
|
case Mode::Measure:
|
|
|
|
case Mode::Verify:
|
2019-12-03 20:15:30 -07:00
|
|
|
if (present)
|
|
|
|
Do(x.value());
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-20 05:32:04 -06:00
|
|
|
template <typename T, std::size_t N>
|
2015-02-15 12:43:31 -07:00
|
|
|
void DoArray(std::array<T, N>& x)
|
2014-09-20 05:32:04 -06:00
|
|
|
{
|
2019-01-31 19:02:22 -07:00
|
|
|
DoArray(x.data(), static_cast<u32>(x.size()));
|
2014-09-20 05:32:04 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-04-24 20:26:27 -06:00
|
|
|
template <typename V, auto last_member, typename = decltype(last_member)>
|
|
|
|
void DoArray(Common::EnumMap<V, last_member>& x)
|
|
|
|
{
|
|
|
|
DoArray(x.data(), static_cast<u32>(x.size()));
|
|
|
|
}
|
|
|
|
|
2021-05-13 11:30:30 -06:00
|
|
|
template <typename T, typename std::enable_if_t<std::is_trivially_copyable_v<T>, int> = 0>
|
2013-03-02 02:28:44 -07:00
|
|
|
void DoArray(T* x, u32 count)
|
|
|
|
{
|
2014-08-27 17:50:52 -06:00
|
|
|
DoVoid(x, count * sizeof(T));
|
2008-10-25 11:09:06 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-05-13 11:30:30 -06:00
|
|
|
template <typename T, typename std::enable_if_t<!std::is_trivially_copyable_v<T>, int> = 0>
|
2019-01-31 19:02:22 -07:00
|
|
|
void DoArray(T* x, u32 count)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < count; ++i)
|
|
|
|
Do(x[i]);
|
|
|
|
}
|
|
|
|
|
2015-09-29 10:35:30 -06:00
|
|
|
template <typename T, std::size_t N>
|
|
|
|
void DoArray(T (&arr)[N])
|
|
|
|
{
|
|
|
|
DoArray(arr, static_cast<u32>(N));
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2021-04-14 11:29:06 -06:00
|
|
|
// The caller is required to inspect the mode of this PointerWrap
|
|
|
|
// and deal with the pointer returned from this function themself.
|
|
|
|
[[nodiscard]] u8* DoExternal(u32& count)
|
|
|
|
{
|
|
|
|
Do(count);
|
2022-04-17 20:13:25 -06:00
|
|
|
u8* current = *m_ptr_current;
|
|
|
|
*m_ptr_current += count;
|
2022-05-17 23:29:05 -06:00
|
|
|
if (!IsMeasureMode() && *m_ptr_current > m_ptr_end)
|
2022-04-17 20:13:25 -06:00
|
|
|
{
|
|
|
|
// trying to read/write past the end of the buffer, prevent this
|
2022-05-17 23:29:05 -06:00
|
|
|
SetMeasureMode();
|
2022-04-17 20:13:25 -06:00
|
|
|
}
|
2021-04-14 11:29:06 -06:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2014-08-24 17:38:33 -06:00
|
|
|
void Do(Common::Flag& flag)
|
|
|
|
{
|
|
|
|
bool s = flag.IsSet();
|
|
|
|
Do(s);
|
2022-05-17 23:29:05 -06:00
|
|
|
if (IsReadMode())
|
2014-08-24 17:38:33 -06:00
|
|
|
flag.Set(s);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-03-31 02:09:33 -06:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::atomic<T>& atomic)
|
|
|
|
{
|
2021-05-13 10:44:59 -06:00
|
|
|
T temp = atomic.load(std::memory_order_relaxed);
|
2015-03-31 02:09:33 -06:00
|
|
|
Do(temp);
|
2022-05-17 23:29:05 -06:00
|
|
|
if (IsReadMode())
|
2021-05-13 10:44:59 -06:00
|
|
|
atomic.store(temp, std::memory_order_relaxed);
|
2015-03-31 02:09:33 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(T& x)
|
|
|
|
{
|
2021-05-13 11:30:30 -06:00
|
|
|
static_assert(std::is_trivially_copyable_v<T>, "Only sane for trivially copyable types");
|
2014-08-27 17:50:52 -06: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 02:28:44 -07:00
|
|
|
DoVoid((void*)&x, sizeof(x));
|
2008-08-21 17:28:07 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-04-09 17:57:39 -06:00
|
|
|
template <typename T>
|
|
|
|
void DoPOD(T& x)
|
|
|
|
{
|
|
|
|
DoVoid((void*)&x, sizeof(x));
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-04-14 20:22:34 -06: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 02:43:46 -06:00
|
|
|
|
2016-04-14 20:22:34 -06:00
|
|
|
Do(stable);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-05-17 23:29:05 -06:00
|
|
|
if (IsReadMode())
|
2016-04-14 20:22:34 -06:00
|
|
|
x = stable != 0;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void DoPointer(T*& x, T* const base)
|
|
|
|
{
|
2012-01-04 01:42:22 -07: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 02:03:46 -07:00
|
|
|
ptrdiff_t offset = x - base;
|
2012-01-04 01:42:22 -07:00
|
|
|
Do(offset);
|
2022-05-17 23:29:05 -06:00
|
|
|
if (IsReadMode())
|
2013-11-13 02:03:46 -07:00
|
|
|
{
|
2012-01-04 01:42:22 -07:00
|
|
|
x = base + offset;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2013-11-13 02:03:46 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
void DoMarker(const std::string& prevName, u32 arbitraryNumber = 0x42)
|
2011-12-17 17:49:24 -07:00
|
|
|
{
|
|
|
|
u32 cookie = arbitraryNumber;
|
|
|
|
Do(cookie);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-05-17 23:29:05 -06:00
|
|
|
if (IsReadMode() && cookie != arbitraryNumber)
|
2011-12-17 17:49:24 -07:00
|
|
|
{
|
2020-12-02 11:17:27 -07:00
|
|
|
PanicAlertFmtT(
|
|
|
|
"Error: After \"{0}\", found {1} ({2:#x}) instead of save marker {3} ({4:#x}). Aborting "
|
|
|
|
"savestate load...",
|
|
|
|
prevName, cookie, cookie, arbitraryNumber, arbitraryNumber);
|
2022-05-17 23:29:05 -06:00
|
|
|
SetMeasureMode();
|
2011-12-17 17:49:24 -07:00
|
|
|
}
|
|
|
|
}
|
2013-03-19 19:51:12 -06:00
|
|
|
|
2016-09-01 00:42:52 -06: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 02:28:44 -07:00
|
|
|
private:
|
2019-01-31 19:02:22 -07:00
|
|
|
template <typename T>
|
|
|
|
void DoContiguousContainer(T& container)
|
|
|
|
{
|
|
|
|
u32 size = static_cast<u32>(container.size());
|
|
|
|
Do(size);
|
|
|
|
container.resize(size);
|
|
|
|
|
2019-02-09 20:26:46 -07:00
|
|
|
if (size > 0)
|
|
|
|
DoArray(&container[0], size);
|
2019-01-31 19:02:22 -07:00
|
|
|
}
|
|
|
|
|
2014-07-05 21:03:43 -06:00
|
|
|
template <typename T>
|
|
|
|
void DoContainer(T& x)
|
|
|
|
{
|
2016-09-01 00:42:52 -06:00
|
|
|
DoEachElement(x, [](PointerWrap& p, typename T::value_type& elem) { p.Do(elem); });
|
2014-07-05 21:03:43 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-06-09 09:47:01 -06:00
|
|
|
DOLPHIN_FORCE_INLINE void DoVoid(void* data, u32 size)
|
2013-03-02 02:28:44 -07:00
|
|
|
{
|
2022-05-17 23:29:05 -06:00
|
|
|
if (!IsMeasureMode() && (*m_ptr_current + size) > m_ptr_end)
|
2022-04-17 20:13:25 -06:00
|
|
|
{
|
|
|
|
// trying to read/write past the end of the buffer, prevent this
|
2022-05-17 23:29:05 -06:00
|
|
|
SetMeasureMode();
|
2022-04-17 20:13:25 -06:00
|
|
|
}
|
|
|
|
|
2022-05-17 23:29:05 -06:00
|
|
|
switch (m_mode)
|
2013-03-02 02:28:44 -07:00
|
|
|
{
|
2022-05-21 22:18:27 -06:00
|
|
|
case Mode::Read:
|
2022-04-17 20:13:25 -06:00
|
|
|
memcpy(data, *m_ptr_current, size);
|
2013-03-02 02:28:44 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-05-21 22:18:27 -06:00
|
|
|
case Mode::Write:
|
2022-04-17 20:13:25 -06:00
|
|
|
memcpy(*m_ptr_current, data, size);
|
2013-03-02 02:28:44 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-05-21 22:18:27 -06:00
|
|
|
case Mode::Measure:
|
2013-03-02 02:28:44 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-05-21 22:18:27 -06:00
|
|
|
case Mode::Verify:
|
2022-04-17 20:13:25 -06:00
|
|
|
DEBUG_ASSERT_MSG(COMMON, !memcmp(data, *m_ptr_current, size),
|
2021-11-10 15:42:49 -07:00
|
|
|
"Savestate verification failure: buf {} != {} (size {}).\n", fmt::ptr(data),
|
2022-04-17 20:13:25 -06:00
|
|
|
fmt::ptr(*m_ptr_current), size);
|
2013-03-02 02:28:44 -07:00
|
|
|
break;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2022-04-17 20:13:25 -06:00
|
|
|
*m_ptr_current += size;
|
2013-03-02 02:28:44 -07:00
|
|
|
}
|
2008-08-21 17:28:07 -06:00
|
|
|
};
|