2013-09-20 03:28:05 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2013-09-20 03:28:05 +00:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2014-08-17 17:45:50 +00:00
|
|
|
#pragma once
|
2013-09-05 00:17:46 +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.
|
|
|
|
|
2015-06-20 21:24:01 +00:00
|
|
|
#include <cstring>
|
2013-09-05 00:17:46 +00:00
|
|
|
#include <deque>
|
2013-09-20 03:28:05 +00:00
|
|
|
#include <list>
|
2015-06-20 21:24:01 +00:00
|
|
|
#include <map>
|
2013-09-20 03:28:05 +00:00
|
|
|
#include <set>
|
2015-06-20 21:24:01 +00:00
|
|
|
#include <string>
|
2013-09-05 00:17:46 +00:00
|
|
|
#include <type_traits>
|
2015-06-21 14:11:32 +00:00
|
|
|
#include <utility>
|
2015-06-20 21:24:01 +00:00
|
|
|
#include <vector>
|
2015-06-21 14:11:32 +00:00
|
|
|
#include "common/assert.h"
|
2015-05-06 07:06:12 +00:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#include "common/logging/log.h"
|
2013-09-05 00:17:46 +00:00
|
|
|
|
|
|
|
template <class T>
|
2016-09-18 00:38:01 +00:00
|
|
|
struct LinkedListItem : public T {
|
|
|
|
LinkedListItem<T>* next;
|
2013-09-05 00:17:46 +00:00
|
|
|
};
|
|
|
|
|
2013-09-20 03:28:05 +00:00
|
|
|
class PointerWrap;
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
class PointerWrapSection {
|
2013-09-20 03:28:05 +00:00
|
|
|
public:
|
2016-09-18 00:38:01 +00:00
|
|
|
PointerWrapSection(PointerWrap& p, int ver, const char* title)
|
2016-09-19 01:01:46 +00:00
|
|
|
: p_(p), ver_(ver), title_(title) {}
|
2014-04-01 22:20:08 +00:00
|
|
|
~PointerWrapSection();
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
bool operator==(const int& v) const {
|
|
|
|
return ver_ == v;
|
|
|
|
}
|
|
|
|
bool operator!=(const int& v) const {
|
|
|
|
return ver_ != v;
|
|
|
|
}
|
|
|
|
bool operator<=(const int& v) const {
|
|
|
|
return ver_ <= v;
|
|
|
|
}
|
|
|
|
bool operator>=(const int& v) const {
|
|
|
|
return ver_ >= v;
|
|
|
|
}
|
|
|
|
bool operator<(const int& v) const {
|
|
|
|
return ver_ < v;
|
|
|
|
}
|
|
|
|
bool operator>(const int& v) const {
|
|
|
|
return ver_ > v;
|
|
|
|
}
|
2014-04-01 22:20:08 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
operator bool() const {
|
2014-04-01 22:20:08 +00:00
|
|
|
return ver_ > 0;
|
|
|
|
}
|
2013-09-20 03:28:05 +00:00
|
|
|
|
|
|
|
private:
|
2016-09-18 00:38:01 +00:00
|
|
|
PointerWrap& p_;
|
2014-04-01 22:20:08 +00:00
|
|
|
int ver_;
|
2016-09-18 00:38:01 +00:00
|
|
|
const char* title_;
|
2013-09-20 03:28:05 +00:00
|
|
|
};
|
|
|
|
|
2013-09-05 00:17:46 +00:00
|
|
|
// Wrapper class
|
2016-09-18 00:38:01 +00:00
|
|
|
class PointerWrap {
|
|
|
|
// This makes it a compile error if you forget to define DoState() on non-POD.
|
|
|
|
// Which also can be a problem, for example struct tm is non-POD on linux, for whatever reason...
|
2013-09-20 03:28:05 +00:00
|
|
|
#ifdef _MSC_VER
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T, bool isPOD = std::is_pod<T>::value,
|
|
|
|
bool isPointer = std::is_pointer<T>::value>
|
2013-09-20 03:28:05 +00:00
|
|
|
#else
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T, bool isPOD = __is_pod(T), bool isPointer = std::is_pointer<T>::value>
|
2013-09-20 03:28:05 +00:00
|
|
|
#endif
|
2016-09-18 00:38:01 +00:00
|
|
|
struct DoHelper {
|
|
|
|
static void DoArray(PointerWrap* p, T* x, int count) {
|
2014-04-01 22:20:08 +00:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
p->Do(x[i]);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
static void Do(PointerWrap* p, T& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
p->DoClass(x);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <typename T>
|
|
|
|
struct DoHelper<T, true, false> {
|
|
|
|
static void DoArray(PointerWrap* p, T* x, int count) {
|
|
|
|
p->DoVoid((void*)x, sizeof(T) * count);
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
static void Do(PointerWrap* p, T& x) {
|
|
|
|
p->DoVoid((void*)&x, sizeof(x));
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
};
|
2013-09-20 03:28:05 +00:00
|
|
|
|
|
|
|
public:
|
2014-04-01 22:20:08 +00:00
|
|
|
enum Mode {
|
|
|
|
MODE_READ = 1, // load
|
2016-09-18 00:38:01 +00:00
|
|
|
MODE_WRITE, // save
|
|
|
|
MODE_MEASURE, // calculate size
|
|
|
|
MODE_VERIFY, // compare
|
2014-04-01 22:20:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum Error {
|
|
|
|
ERROR_NONE = 0,
|
|
|
|
ERROR_WARNING = 1,
|
|
|
|
ERROR_FAILURE = 2,
|
|
|
|
};
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
u8** ptr;
|
2014-04-01 22:20:08 +00:00
|
|
|
Mode mode;
|
|
|
|
Error error;
|
2013-09-05 00:17:46 +00:00
|
|
|
|
|
|
|
public:
|
2016-09-19 01:01:46 +00:00
|
|
|
PointerWrap(u8** ptr_, Mode mode_) : ptr(ptr_), mode(mode_), error(ERROR_NONE) {}
|
2016-09-18 00:38:01 +00:00
|
|
|
PointerWrap(unsigned char** ptr_, int mode_)
|
2016-09-19 01:01:46 +00:00
|
|
|
: ptr((u8**)ptr_), mode((Mode)mode_), error(ERROR_NONE) {}
|
2014-04-01 22:20:08 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
PointerWrapSection Section(const char* title, int ver) {
|
2014-04-01 22:20:08 +00:00
|
|
|
return Section(title, ver, ver);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The returned object can be compared against the version that was loaded.
|
|
|
|
// This can be used to support versions as old as minVer.
|
|
|
|
// Version = 0 means the section was not found.
|
2016-09-18 00:38:01 +00:00
|
|
|
PointerWrapSection Section(const char* title, int minVer, int ver) {
|
2014-04-01 22:20:08 +00:00
|
|
|
char marker[16] = {0};
|
|
|
|
int foundVersion = ver;
|
|
|
|
|
|
|
|
strncpy(marker, title, sizeof(marker));
|
2016-09-18 00:38:01 +00:00
|
|
|
if (!ExpectVoid(marker, sizeof(marker))) {
|
2014-04-01 22:20:08 +00:00
|
|
|
// Might be before we added name markers for safety.
|
|
|
|
if (foundVersion == 1 && ExpectVoid(&foundVersion, sizeof(foundVersion)))
|
|
|
|
DoMarker(title);
|
|
|
|
// Wasn't found, but maybe we can still load the state.
|
|
|
|
else
|
|
|
|
foundVersion = 0;
|
2016-09-18 00:38:01 +00:00
|
|
|
} else
|
2014-04-01 22:20:08 +00:00
|
|
|
Do(foundVersion);
|
|
|
|
|
|
|
|
if (error == ERROR_FAILURE || foundVersion < minVer || foundVersion > ver) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Common, "Savestate failure: wrong version {} found for {}", foundVersion,
|
2018-06-29 13:56:12 +00:00
|
|
|
title);
|
2014-04-01 22:20:08 +00:00
|
|
|
SetError(ERROR_FAILURE);
|
|
|
|
return PointerWrapSection(*this, -1, title);
|
|
|
|
}
|
|
|
|
return PointerWrapSection(*this, foundVersion, title);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
void SetMode(Mode mode_) {
|
|
|
|
mode = mode_;
|
|
|
|
}
|
|
|
|
Mode GetMode() const {
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
u8** GetPPtr() {
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
void SetError(Error error_) {
|
2014-04-01 22:20:08 +00:00
|
|
|
if (error < error_)
|
|
|
|
error = error_;
|
|
|
|
if (error > ERROR_WARNING)
|
|
|
|
mode = PointerWrap::MODE_MEASURE;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
bool ExpectVoid(void* data, int size) {
|
2014-04-01 22:20:08 +00:00
|
|
|
switch (mode) {
|
2016-09-18 00:38:01 +00:00
|
|
|
case MODE_READ:
|
|
|
|
if (memcmp(data, *ptr, size) != 0)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case MODE_WRITE:
|
|
|
|
memcpy(*ptr, data, size);
|
|
|
|
break;
|
|
|
|
case MODE_MEASURE:
|
|
|
|
break; // MODE_MEASURE - don't need to do anything
|
2014-12-06 01:53:49 +00:00
|
|
|
case MODE_VERIFY:
|
|
|
|
for (int i = 0; i < size; i++) {
|
2016-09-18 00:38:01 +00:00
|
|
|
DEBUG_ASSERT_MSG(
|
|
|
|
((u8*)data)[i] == (*ptr)[i],
|
2018-03-27 15:28:42 +00:00
|
|
|
"Savestate verification failure: {} ({:#X}) (at {}) != {} ({:#X}) (at {}).\n",
|
2016-09-18 00:38:01 +00:00
|
|
|
((u8*)data)[i], ((u8*)data)[i], &((u8*)data)[i], (*ptr)[i], (*ptr)[i],
|
|
|
|
&(*ptr)[i]);
|
2014-12-06 01:53:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-09-18 00:38:01 +00:00
|
|
|
default:
|
|
|
|
break; // throw an error?
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
(*ptr) += size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
void DoVoid(void* data, int size) {
|
2014-04-01 22:20:08 +00:00
|
|
|
switch (mode) {
|
2016-09-18 00:38:01 +00:00
|
|
|
case MODE_READ:
|
|
|
|
memcpy(data, *ptr, size);
|
|
|
|
break;
|
|
|
|
case MODE_WRITE:
|
|
|
|
memcpy(*ptr, data, size);
|
|
|
|
break;
|
|
|
|
case MODE_MEASURE:
|
|
|
|
break; // MODE_MEASURE - don't need to do anything
|
2014-12-06 01:53:49 +00:00
|
|
|
case MODE_VERIFY:
|
|
|
|
for (int i = 0; i < size; i++) {
|
2016-09-18 00:38:01 +00:00
|
|
|
DEBUG_ASSERT_MSG(
|
|
|
|
((u8*)data)[i] == (*ptr)[i],
|
2018-03-27 15:28:42 +00:00
|
|
|
"Savestate verification failure: {} ({#:X}) (at {}) != {} ({:#X}) (at {}).\n",
|
2016-09-18 00:38:01 +00:00
|
|
|
((u8*)data)[i], ((u8*)data)[i], &((u8*)data)[i], (*ptr)[i], (*ptr)[i],
|
|
|
|
&(*ptr)[i]);
|
2014-12-06 01:53:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-09-18 00:38:01 +00:00
|
|
|
default:
|
|
|
|
break; // throw an error?
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
(*ptr) += size;
|
|
|
|
}
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class K, class T>
|
|
|
|
void Do(std::map<K, T*>& x) {
|
|
|
|
if (mode == MODE_READ) {
|
|
|
|
for (auto it = x.begin(), end = x.end(); it != end; ++it) {
|
2014-12-03 18:57:57 +00:00
|
|
|
if (it->second != nullptr)
|
2014-04-01 22:20:08 +00:00
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 00:38:01 +00:00
|
|
|
T* dv = nullptr;
|
2014-04-01 22:20:08 +00:00
|
|
|
DoMap(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class K, class T>
|
|
|
|
void Do(std::map<K, T>& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
T dv = T();
|
|
|
|
DoMap(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class K, class T>
|
|
|
|
void DoMap(std::map<K, T>& x, T& default_val) {
|
2014-04-01 22:20:08 +00:00
|
|
|
unsigned int number = (unsigned int)x.size();
|
|
|
|
Do(number);
|
|
|
|
switch (mode) {
|
2016-09-18 00:38:01 +00:00
|
|
|
case MODE_READ: {
|
|
|
|
x.clear();
|
|
|
|
while (number > 0) {
|
|
|
|
K first = K();
|
|
|
|
Do(first);
|
|
|
|
T second = default_val;
|
|
|
|
Do(second);
|
|
|
|
x[first] = second;
|
|
|
|
--number;
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
2016-09-18 00:38:01 +00:00
|
|
|
} break;
|
2014-04-01 22:20:08 +00:00
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
2016-09-18 00:38:01 +00:00
|
|
|
case MODE_VERIFY: {
|
|
|
|
typename std::map<K, T>::iterator itr = x.begin();
|
|
|
|
while (number > 0) {
|
|
|
|
K first = itr->first;
|
|
|
|
Do(first);
|
|
|
|
Do(itr->second);
|
|
|
|
--number;
|
|
|
|
++itr;
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
2016-09-18 00:38:01 +00:00
|
|
|
} break;
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class K, class T>
|
|
|
|
void Do(std::multimap<K, T*>& x) {
|
|
|
|
if (mode == MODE_READ) {
|
|
|
|
for (auto it = x.begin(), end = x.end(); it != end; ++it) {
|
2014-12-03 18:57:57 +00:00
|
|
|
if (it->second != nullptr)
|
2014-04-01 22:20:08 +00:00
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 00:38:01 +00:00
|
|
|
T* dv = nullptr;
|
2014-04-01 22:20:08 +00:00
|
|
|
DoMultimap(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class K, class T>
|
|
|
|
void Do(std::multimap<K, T>& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
T dv = T();
|
|
|
|
DoMultimap(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class K, class T>
|
|
|
|
void DoMultimap(std::multimap<K, T>& x, T& default_val) {
|
2014-04-01 22:20:08 +00:00
|
|
|
unsigned int number = (unsigned int)x.size();
|
|
|
|
Do(number);
|
|
|
|
switch (mode) {
|
2016-09-18 00:38:01 +00:00
|
|
|
case MODE_READ: {
|
|
|
|
x.clear();
|
|
|
|
while (number > 0) {
|
|
|
|
K first = K();
|
|
|
|
Do(first);
|
|
|
|
T second = default_val;
|
|
|
|
Do(second);
|
|
|
|
x.insert(std::make_pair(first, second));
|
|
|
|
--number;
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
2016-09-18 00:38:01 +00:00
|
|
|
} break;
|
2014-04-01 22:20:08 +00:00
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
2016-09-18 00:38:01 +00:00
|
|
|
case MODE_VERIFY: {
|
|
|
|
typename std::multimap<K, T>::iterator itr = x.begin();
|
|
|
|
while (number > 0) {
|
|
|
|
Do(itr->first);
|
|
|
|
Do(itr->second);
|
|
|
|
--number;
|
|
|
|
++itr;
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
2016-09-18 00:38:01 +00:00
|
|
|
} break;
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store vectors.
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(std::vector<T*>& x) {
|
|
|
|
T* dv = nullptr;
|
2014-04-01 22:20:08 +00:00
|
|
|
DoVector(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(std::vector<T>& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
T dv = T();
|
|
|
|
DoVector(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void DoPOD(std::vector<T>& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
T dv = T();
|
|
|
|
DoVectorPOD(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(std::vector<T>& x, T& default_val) {
|
2014-04-01 22:20:08 +00:00
|
|
|
DoVector(x, default_val);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void DoVector(std::vector<T>& x, T& default_val) {
|
2014-04-01 22:20:08 +00:00
|
|
|
u32 vec_size = (u32)x.size();
|
|
|
|
Do(vec_size);
|
|
|
|
x.resize(vec_size, default_val);
|
|
|
|
if (vec_size > 0)
|
|
|
|
DoArray(&x[0], vec_size);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void DoVectorPOD(std::vector<T>& x, T& default_val) {
|
2014-04-01 22:20:08 +00:00
|
|
|
u32 vec_size = (u32)x.size();
|
|
|
|
Do(vec_size);
|
|
|
|
x.resize(vec_size, default_val);
|
|
|
|
if (vec_size > 0)
|
|
|
|
DoArray(&x[0], vec_size);
|
|
|
|
}
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
// Store deques.
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(std::deque<T*>& x) {
|
|
|
|
T* dv = nullptr;
|
2014-04-01 22:20:08 +00:00
|
|
|
DoDeque(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(std::deque<T>& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
T dv = T();
|
|
|
|
DoDeque(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void DoDeque(std::deque<T>& x, T& default_val) {
|
2014-04-01 22:20:08 +00:00
|
|
|
u32 deq_size = (u32)x.size();
|
|
|
|
Do(deq_size);
|
|
|
|
x.resize(deq_size, default_val);
|
|
|
|
u32 i;
|
2016-09-18 00:38:01 +00:00
|
|
|
for (i = 0; i < deq_size; i++)
|
2014-04-01 22:20:08 +00:00
|
|
|
Do(x[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store STL lists.
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(std::list<T*>& x) {
|
|
|
|
T* dv = nullptr;
|
2014-04-01 22:20:08 +00:00
|
|
|
Do(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(std::list<T>& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
T dv = T();
|
|
|
|
DoList(x, dv);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(std::list<T>& x, T& default_val) {
|
2014-04-01 22:20:08 +00:00
|
|
|
DoList(x, default_val);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void DoList(std::list<T>& x, T& default_val) {
|
2014-04-01 22:20:08 +00:00
|
|
|
u32 list_size = (u32)x.size();
|
|
|
|
Do(list_size);
|
|
|
|
x.resize(list_size, default_val);
|
|
|
|
|
|
|
|
typename std::list<T>::iterator itr, end;
|
|
|
|
for (itr = x.begin(), end = x.end(); itr != end; ++itr)
|
|
|
|
Do(*itr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store STL sets.
|
|
|
|
template <class T>
|
2016-09-18 00:38:01 +00:00
|
|
|
void Do(std::set<T*>& x) {
|
|
|
|
if (mode == MODE_READ) {
|
|
|
|
for (auto it = x.begin(), end = x.end(); it != end; ++it) {
|
2014-12-03 18:57:57 +00:00
|
|
|
if (*it != nullptr)
|
2014-04-01 22:20:08 +00:00
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DoSet(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2016-09-18 00:38:01 +00:00
|
|
|
void Do(std::set<T>& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
DoSet(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
2016-09-18 00:38:01 +00:00
|
|
|
void DoSet(std::set<T>& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
unsigned int number = (unsigned int)x.size();
|
|
|
|
Do(number);
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
switch (mode) {
|
|
|
|
case MODE_READ: {
|
|
|
|
x.clear();
|
|
|
|
while (number-- > 0) {
|
|
|
|
T it = T();
|
|
|
|
Do(it);
|
|
|
|
x.insert(it);
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
2016-09-18 00:38:01 +00:00
|
|
|
} break;
|
2014-04-01 22:20:08 +00:00
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
2016-09-18 00:38:01 +00:00
|
|
|
case MODE_VERIFY: {
|
|
|
|
typename std::set<T>::iterator itr = x.begin();
|
|
|
|
while (number-- > 0)
|
|
|
|
Do(*itr++);
|
|
|
|
} break;
|
2014-04-01 22:20:08 +00:00
|
|
|
|
|
|
|
default:
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Common, "Savestate error: invalid mode {}.", mode);
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store strings.
|
2016-09-18 00:38:01 +00:00
|
|
|
void Do(std::string& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
int stringLen = (int)x.length() + 1;
|
|
|
|
Do(stringLen);
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2014-04-01 22:20:08 +00:00
|
|
|
switch (mode) {
|
2016-09-18 00:38:01 +00:00
|
|
|
case MODE_READ:
|
|
|
|
x = (char*)*ptr;
|
|
|
|
break;
|
|
|
|
case MODE_WRITE:
|
|
|
|
memcpy(*ptr, x.c_str(), stringLen);
|
|
|
|
break;
|
|
|
|
case MODE_MEASURE:
|
|
|
|
break;
|
2014-12-06 01:53:49 +00:00
|
|
|
case MODE_VERIFY:
|
2015-01-21 01:16:47 +00:00
|
|
|
DEBUG_ASSERT_MSG((x == (char*)*ptr),
|
2018-03-27 15:28:42 +00:00
|
|
|
"Savestate verification failure: \"{}\" != \"{}\" (at {}).\n", x,
|
|
|
|
(char*)*ptr, ptr);
|
2014-12-06 01:53:49 +00:00
|
|
|
break;
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
(*ptr) += stringLen;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
void Do(std::wstring& x) {
|
|
|
|
int stringLen = sizeof(wchar_t) * ((int)x.length() + 1);
|
2014-04-01 22:20:08 +00:00
|
|
|
Do(stringLen);
|
|
|
|
|
|
|
|
switch (mode) {
|
2016-09-18 00:38:01 +00:00
|
|
|
case MODE_READ:
|
|
|
|
x = (wchar_t*)*ptr;
|
|
|
|
break;
|
|
|
|
case MODE_WRITE:
|
|
|
|
memcpy(*ptr, x.c_str(), stringLen);
|
|
|
|
break;
|
|
|
|
case MODE_MEASURE:
|
|
|
|
break;
|
2014-12-06 01:53:49 +00:00
|
|
|
case MODE_VERIFY:
|
2015-01-21 01:16:47 +00:00
|
|
|
DEBUG_ASSERT_MSG((x == (wchar_t*)*ptr),
|
2018-03-27 15:28:42 +00:00
|
|
|
"Savestate verification failure: \"{}\" != \"{}\" (at {}).\n", x,
|
|
|
|
(wchar_t*)*ptr, ptr);
|
2014-12-06 01:53:49 +00:00
|
|
|
break;
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
(*ptr) += stringLen;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void DoClass(T& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
x.DoState(*this);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void DoClass(T*& x) {
|
|
|
|
if (mode == MODE_READ) {
|
2014-12-03 18:57:57 +00:00
|
|
|
if (x != nullptr)
|
2014-04-01 22:20:08 +00:00
|
|
|
delete x;
|
|
|
|
x = new T();
|
|
|
|
}
|
|
|
|
x->DoState(*this);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void DoArray(T* x, int count) {
|
2014-04-01 22:20:08 +00:00
|
|
|
DoHelper<T>::DoArray(this, x, count);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(T& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
DoHelper<T>::Do(this, x);
|
|
|
|
}
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void DoPOD(T& x) {
|
2014-04-01 22:20:08 +00:00
|
|
|
DoHelper<T>::Do(this, x);
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T>
|
|
|
|
void DoPointer(T*& x, T* const base) {
|
|
|
|
// pointers can be more than 2^31 apart, but you're using this function wrong if you need
|
|
|
|
// that much range
|
2014-04-01 22:20:08 +00:00
|
|
|
s32 offset = x - base;
|
|
|
|
Do(offset);
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
x = base + offset;
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
template <class T, LinkedListItem<T>* (*TNew)(), void (*TFree)(LinkedListItem<T>*),
|
|
|
|
void (*TDo)(PointerWrap&, T*)>
|
|
|
|
void DoLinkedList(LinkedListItem<T>*& list_start, LinkedListItem<T>** list_end = nullptr) {
|
2014-04-01 22:20:08 +00:00
|
|
|
LinkedListItem<T>* list_cur = list_start;
|
2015-09-12 03:04:19 +00:00
|
|
|
LinkedListItem<T>* prev = nullptr;
|
2014-04-01 22:20:08 +00:00
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
while (true) {
|
2014-04-01 22:20:08 +00:00
|
|
|
u8 shouldExist = (list_cur ? 1 : 0);
|
|
|
|
Do(shouldExist);
|
2016-09-18 00:38:01 +00:00
|
|
|
if (shouldExist == 1) {
|
2014-04-01 22:20:08 +00:00
|
|
|
LinkedListItem<T>* cur = list_cur ? list_cur : TNew();
|
|
|
|
TDo(*this, (T*)cur);
|
2016-09-18 00:38:01 +00:00
|
|
|
if (!list_cur) {
|
|
|
|
if (mode == MODE_READ) {
|
2014-12-03 18:57:57 +00:00
|
|
|
cur->next = nullptr;
|
2014-04-01 22:20:08 +00:00
|
|
|
list_cur = cur;
|
|
|
|
if (prev)
|
|
|
|
prev->next = cur;
|
|
|
|
else
|
|
|
|
list_start = cur;
|
2016-09-18 00:38:01 +00:00
|
|
|
} else {
|
2014-04-01 22:20:08 +00:00
|
|
|
TFree(cur);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 00:38:01 +00:00
|
|
|
} else {
|
|
|
|
if (mode == MODE_READ) {
|
2014-04-01 22:20:08 +00:00
|
|
|
if (prev)
|
2014-12-03 18:57:57 +00:00
|
|
|
prev->next = nullptr;
|
2014-04-01 22:20:08 +00:00
|
|
|
if (list_end)
|
|
|
|
*list_end = prev;
|
2016-09-18 00:38:01 +00:00
|
|
|
if (list_cur) {
|
2014-04-01 22:20:08 +00:00
|
|
|
if (list_start == list_cur)
|
2014-12-03 18:57:57 +00:00
|
|
|
list_start = nullptr;
|
2016-09-18 00:38:01 +00:00
|
|
|
do {
|
2014-04-01 22:20:08 +00:00
|
|
|
LinkedListItem<T>* next = list_cur->next;
|
|
|
|
TFree(list_cur);
|
|
|
|
list_cur = next;
|
2016-09-18 00:38:01 +00:00
|
|
|
} while (list_cur);
|
2014-04-01 22:20:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = list_cur;
|
|
|
|
list_cur = list_cur->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-18 00:38:01 +00:00
|
|
|
void DoMarker(const char* prevName, u32 arbitraryNumber = 0x42) {
|
2014-04-01 22:20:08 +00:00
|
|
|
u32 cookie = arbitraryNumber;
|
|
|
|
Do(cookie);
|
2016-09-18 00:38:01 +00:00
|
|
|
if (mode == PointerWrap::MODE_READ && cookie != arbitraryNumber) {
|
2018-06-29 11:18:07 +00:00
|
|
|
LOG_ERROR(Common,
|
2018-06-29 13:56:12 +00:00
|
|
|
"After \"{}\", found {} ({:#X}) instead of save marker {} ({:#X}). "
|
|
|
|
"Aborting savestate load...",
|
|
|
|
prevName, cookie, cookie, arbitraryNumber, arbitraryNumber);
|
2014-04-01 22:20:08 +00:00
|
|
|
SetError(ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2013-09-20 03:28:05 +00:00
|
|
|
};
|
2013-09-05 00:17:46 +00:00
|
|
|
|
2013-09-20 03:28:05 +00:00
|
|
|
inline PointerWrapSection::~PointerWrapSection() {
|
2014-04-01 22:20:08 +00:00
|
|
|
if (ver_ > 0) {
|
|
|
|
p_.DoMarker(title_);
|
|
|
|
}
|
2013-09-20 03:28:05 +00:00
|
|
|
}
|