2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// 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
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// 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-10-07 04:26:08 +00:00
|
|
|
#pragma once
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-03-07 19:10:18 +00:00
|
|
|
#include "ppsspp_config.h"
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Extremely simple serialization framework.
|
2014-06-22 15:02:04 +00:00
|
|
|
// Currently mis-named, a native ChunkFile is something different (a RIFF file)
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// (mis)-features:
|
|
|
|
// + Super fast
|
|
|
|
// + Very simple
|
|
|
|
// + Same code is used for serialization and deserializaition (in most cases)
|
2013-10-07 04:26:08 +00:00
|
|
|
// + Sections can be versioned for backwards/forwards compatibility
|
2012-11-01 15:19:01 +00:00
|
|
|
// - Serialization code for anything complex has to be manually written.
|
|
|
|
|
2017-11-05 18:04:22 +00:00
|
|
|
#include <cstdlib>
|
2012-11-01 15:19:01 +00:00
|
|
|
#include <map>
|
2014-11-03 16:24:53 +00:00
|
|
|
#include <unordered_map>
|
2012-11-01 15:19:01 +00:00
|
|
|
#include <deque>
|
2012-12-27 05:07:29 +00:00
|
|
|
#include <list>
|
2012-12-27 06:45:19 +00:00
|
|
|
#include <set>
|
2013-02-04 08:52:56 +00:00
|
|
|
#include <type_traits>
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
#include "Common.h"
|
2017-08-31 15:13:18 +00:00
|
|
|
#include "Swap.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include "FileUtil.h"
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct LinkedListItem : public T
|
|
|
|
{
|
|
|
|
LinkedListItem<T> *next;
|
|
|
|
};
|
|
|
|
|
2013-09-15 03:19:58 +00:00
|
|
|
class PointerWrap;
|
|
|
|
|
|
|
|
class PointerWrapSection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PointerWrapSection(PointerWrap &p, int ver, const char *title) : p_(p), ver_(ver), title_(title) {
|
|
|
|
}
|
|
|
|
~PointerWrapSection();
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
operator bool() const {
|
|
|
|
return ver_ > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
PointerWrap &p_;
|
|
|
|
int ver_;
|
|
|
|
const char *title_;
|
|
|
|
};
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Wrapper class
|
|
|
|
class PointerWrap
|
|
|
|
{
|
2020-08-02 15:11:09 +00:00
|
|
|
private:
|
2013-02-04 09:03:04 +00:00
|
|
|
// This makes it a compile error if you forget to define DoState() on non-POD.
|
2013-06-24 08:00:10 +00:00
|
|
|
// Which also can be a problem, for example struct tm is non-POD on linux, for whatever reason...
|
2013-02-04 19:19:00 +00:00
|
|
|
#ifdef _MSC_VER
|
2013-02-04 09:03:04 +00:00
|
|
|
template<typename T, bool isPOD = std::is_pod<T>::value, bool isPointer = std::is_pointer<T>::value>
|
2013-02-04 19:19:00 +00:00
|
|
|
#else
|
|
|
|
template<typename T, bool isPOD = __is_pod(T), bool isPointer = std::is_pointer<T>::value>
|
|
|
|
#endif
|
2013-02-04 09:03:04 +00:00
|
|
|
struct DoHelper
|
|
|
|
{
|
|
|
|
static void DoArray(PointerWrap *p, T *x, int count)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < count; ++i)
|
2013-03-21 06:37:18 +00:00
|
|
|
p->Do(x[i]);
|
2013-02-04 09:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void Do(PointerWrap *p, T &x)
|
|
|
|
{
|
|
|
|
p->DoClass(x);
|
|
|
|
}
|
|
|
|
};
|
2013-02-04 08:52:56 +00:00
|
|
|
|
2013-02-04 09:03:04 +00:00
|
|
|
template<typename T>
|
|
|
|
struct DoHelper<T, true, false>
|
2013-02-04 08:52:56 +00:00
|
|
|
{
|
|
|
|
static void DoArray(PointerWrap *p, T *x, int count)
|
|
|
|
{
|
|
|
|
p->DoVoid((void *)x, sizeof(T) * count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Do(PointerWrap *p, T &x)
|
|
|
|
{
|
|
|
|
p->DoVoid((void *)&x, sizeof(x));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-02 15:11:09 +00:00
|
|
|
const char *firstBadSectionTitle_ = nullptr;
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
|
|
|
enum Mode {
|
|
|
|
MODE_READ = 1, // load
|
|
|
|
MODE_WRITE, // save
|
|
|
|
MODE_MEASURE, // calculate size
|
|
|
|
MODE_VERIFY, // compare
|
|
|
|
};
|
|
|
|
|
2013-04-13 08:39:17 +00:00
|
|
|
enum Error {
|
|
|
|
ERROR_NONE = 0,
|
|
|
|
ERROR_WARNING = 1,
|
|
|
|
ERROR_FAILURE = 2,
|
|
|
|
};
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
u8 **ptr;
|
|
|
|
Mode mode;
|
2020-08-02 15:11:09 +00:00
|
|
|
Error error = ERROR_NONE;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
public:
|
2020-08-02 15:11:09 +00:00
|
|
|
PointerWrap(u8 **ptr_, Mode mode_) : ptr(ptr_), mode(mode_) {}
|
|
|
|
PointerWrap(unsigned char **ptr_, int mode_) : ptr((u8**)ptr_), mode((Mode)mode_) {}
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-10-07 04:26:08 +00:00
|
|
|
PointerWrapSection Section(const char *title, int ver);
|
2013-09-15 03:19:58 +00:00
|
|
|
|
|
|
|
// The returned object can be compared against the version that was loaded.
|
|
|
|
// This can be used to support versions as old as minVer.
|
2013-09-15 14:58:52 +00:00
|
|
|
// Version = 0 means the section was not found.
|
2013-10-07 04:26:08 +00:00
|
|
|
PointerWrapSection Section(const char *title, int minVer, int ver);
|
2013-09-15 03:19:58 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
void SetMode(Mode mode_) {mode = mode_;}
|
|
|
|
Mode GetMode() const {return mode;}
|
|
|
|
u8 **GetPPtr() {return ptr;}
|
2013-10-07 04:26:08 +00:00
|
|
|
void SetError(Error error_);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2020-08-02 15:11:09 +00:00
|
|
|
const char *GetBadSectionTitle() const {
|
|
|
|
return firstBadSectionTitle_;
|
|
|
|
}
|
|
|
|
|
2013-10-07 04:26:08 +00:00
|
|
|
// Same as DoVoid, except doesn't advance pointer if it doesn't match on read.
|
|
|
|
bool ExpectVoid(void *data, int size);
|
|
|
|
void DoVoid(void *data, int size);
|
2013-02-04 19:19:00 +00:00
|
|
|
|
2013-02-04 16:24:32 +00:00
|
|
|
template<class K, class T>
|
|
|
|
void Do(std::map<K, T *> &x)
|
2013-02-04 02:09:14 +00:00
|
|
|
{
|
2013-02-04 16:24:32 +00:00
|
|
|
if (mode == MODE_READ)
|
|
|
|
{
|
|
|
|
for (auto it = x.begin(), end = x.end(); it != end; ++it)
|
|
|
|
{
|
|
|
|
if (it->second != NULL)
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
T *dv = NULL;
|
|
|
|
DoMap(x, dv);
|
2013-02-04 02:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class T>
|
|
|
|
void Do(std::map<K, T> &x)
|
2013-02-04 16:24:32 +00:00
|
|
|
{
|
2013-07-08 01:24:53 +00:00
|
|
|
T dv = T();
|
2013-02-04 16:24:32 +00:00
|
|
|
DoMap(x, dv);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class T>
|
2014-11-03 16:24:53 +00:00
|
|
|
void Do(std::unordered_map<K, T *> &x)
|
|
|
|
{
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
{
|
|
|
|
for (auto it = x.begin(), end = x.end(); it != end; ++it)
|
|
|
|
{
|
|
|
|
if (it->second != NULL)
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
T *dv = NULL;
|
|
|
|
DoMap(x, dv);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class T>
|
|
|
|
void Do(std::unordered_map<K, T> &x)
|
|
|
|
{
|
|
|
|
T dv = T();
|
|
|
|
DoMap(x, dv);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class M>
|
|
|
|
void DoMap(M &x, typename M::mapped_type &default_val)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
unsigned int number = (unsigned int)x.size();
|
|
|
|
Do(number);
|
|
|
|
switch (mode) {
|
|
|
|
case MODE_READ:
|
|
|
|
{
|
|
|
|
x.clear();
|
|
|
|
while (number > 0)
|
|
|
|
{
|
2014-11-03 16:24:53 +00:00
|
|
|
typename M::key_type first = typename M::key_type();
|
2012-11-01 15:19:01 +00:00
|
|
|
Do(first);
|
2014-11-03 16:24:53 +00:00
|
|
|
typename M::mapped_type second = default_val;
|
2012-11-01 15:19:01 +00:00
|
|
|
Do(second);
|
|
|
|
x[first] = second;
|
|
|
|
--number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
|
|
|
case MODE_VERIFY:
|
|
|
|
{
|
2014-11-03 16:24:53 +00:00
|
|
|
typename M::iterator itr = x.begin();
|
2012-11-01 15:19:01 +00:00
|
|
|
while (number > 0)
|
|
|
|
{
|
2014-11-03 16:24:53 +00:00
|
|
|
typename M::key_type first = itr->first;
|
2013-07-05 07:00:41 +00:00
|
|
|
Do(first);
|
2012-11-01 15:19:01 +00:00
|
|
|
Do(itr->second);
|
|
|
|
--number;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 16:24:32 +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)
|
|
|
|
{
|
|
|
|
if (it->second != NULL)
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
T *dv = NULL;
|
|
|
|
DoMultimap(x, dv);
|
|
|
|
}
|
|
|
|
|
2012-12-27 22:21:39 +00:00
|
|
|
template<class K, class T>
|
|
|
|
void Do(std::multimap<K, T> &x)
|
2013-02-04 16:24:32 +00:00
|
|
|
{
|
2013-07-08 01:24:53 +00:00
|
|
|
T dv = T();
|
2013-02-04 16:24:32 +00:00
|
|
|
DoMultimap(x, dv);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class T>
|
2014-11-03 16:24:53 +00:00
|
|
|
void Do(std::unordered_multimap<K, T *> &x)
|
|
|
|
{
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
{
|
|
|
|
for (auto it = x.begin(), end = x.end(); it != end; ++it)
|
|
|
|
{
|
|
|
|
if (it->second != NULL)
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
T *dv = NULL;
|
|
|
|
DoMultimap(x, dv);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class K, class T>
|
|
|
|
void Do(std::unordered_multimap<K, T> &x)
|
|
|
|
{
|
|
|
|
T dv = T();
|
|
|
|
DoMultimap(x, dv);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class M>
|
|
|
|
void DoMultimap(M &x, typename M::mapped_type &default_val)
|
2012-12-27 22:21:39 +00:00
|
|
|
{
|
|
|
|
unsigned int number = (unsigned int)x.size();
|
|
|
|
Do(number);
|
|
|
|
switch (mode) {
|
|
|
|
case MODE_READ:
|
|
|
|
{
|
|
|
|
x.clear();
|
|
|
|
while (number > 0)
|
|
|
|
{
|
2014-11-03 16:24:53 +00:00
|
|
|
typename M::key_type first = typename M::key_type();
|
2012-12-27 22:21:39 +00:00
|
|
|
Do(first);
|
2014-11-03 16:24:53 +00:00
|
|
|
typename M::mapped_type second = default_val;
|
2012-12-27 22:21:39 +00:00
|
|
|
Do(second);
|
2014-11-03 16:41:53 +00:00
|
|
|
x.insert(std::make_pair(first, second));
|
2012-12-27 22:21:39 +00:00
|
|
|
--number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
|
|
|
case MODE_VERIFY:
|
|
|
|
{
|
2014-11-03 16:24:53 +00:00
|
|
|
typename M::iterator itr = x.begin();
|
2012-12-27 22:21:39 +00:00
|
|
|
while (number > 0)
|
|
|
|
{
|
|
|
|
Do(itr->first);
|
|
|
|
Do(itr->second);
|
|
|
|
--number;
|
|
|
|
++itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Store vectors.
|
2013-02-04 16:24:32 +00:00
|
|
|
template<class T>
|
|
|
|
void Do(std::vector<T *> &x)
|
|
|
|
{
|
|
|
|
T *dv = NULL;
|
|
|
|
DoVector(x, dv);
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
template<class T>
|
|
|
|
void Do(std::vector<T> &x)
|
2012-12-28 10:22:39 +00:00
|
|
|
{
|
2013-09-01 08:05:35 +00:00
|
|
|
T dv = T();
|
2013-02-04 16:24:32 +00:00
|
|
|
DoVector(x, dv);
|
2012-12-28 10:22:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void Do(std::vector<T> &x, T &default_val)
|
2013-02-04 16:24:32 +00:00
|
|
|
{
|
|
|
|
DoVector(x, default_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void DoVector(std::vector<T> &x, T &default_val)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
u32 vec_size = (u32)x.size();
|
|
|
|
Do(vec_size);
|
2012-12-28 10:22:39 +00:00
|
|
|
x.resize(vec_size, default_val);
|
2012-12-28 06:14:31 +00:00
|
|
|
if (vec_size > 0)
|
|
|
|
DoArray(&x[0], vec_size);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2013-12-30 09:49:05 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Store deques.
|
2013-02-04 16:24:32 +00:00
|
|
|
template<class T>
|
|
|
|
void Do(std::deque<T *> &x)
|
|
|
|
{
|
|
|
|
T *dv = NULL;
|
|
|
|
DoDeque(x, dv);
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
template<class T>
|
|
|
|
void Do(std::deque<T> &x)
|
2013-02-04 16:24:32 +00:00
|
|
|
{
|
2013-09-01 08:05:35 +00:00
|
|
|
T dv = T();
|
2013-02-04 16:24:32 +00:00
|
|
|
DoDeque(x, dv);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void DoDeque(std::deque<T> &x, T &default_val)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
u32 deq_size = (u32)x.size();
|
|
|
|
Do(deq_size);
|
2013-02-04 16:24:32 +00:00
|
|
|
x.resize(deq_size, default_val);
|
2012-11-01 15:19:01 +00:00
|
|
|
u32 i;
|
|
|
|
for(i = 0; i < deq_size; i++)
|
2013-02-04 16:24:32 +00:00
|
|
|
Do(x[i]);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2012-12-27 05:07:29 +00:00
|
|
|
|
|
|
|
// Store STL lists.
|
2013-02-04 16:24:32 +00:00
|
|
|
template<class T>
|
|
|
|
void Do(std::list<T *> &x)
|
|
|
|
{
|
|
|
|
T *dv = NULL;
|
|
|
|
Do(x, dv);
|
|
|
|
}
|
|
|
|
|
2012-12-27 05:07:29 +00:00
|
|
|
template<class T>
|
|
|
|
void Do(std::list<T> &x)
|
|
|
|
{
|
2013-09-01 08:05:35 +00:00
|
|
|
T dv = T();
|
2013-02-04 16:24:32 +00:00
|
|
|
DoList(x, dv);
|
2012-12-27 05:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void Do(std::list<T> &x, T &default_val)
|
2013-02-04 16:24:32 +00:00
|
|
|
{
|
|
|
|
DoList(x, default_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void DoList(std::list<T> &x, T &default_val)
|
2012-12-27 05:07:29 +00:00
|
|
|
{
|
|
|
|
u32 list_size = (u32)x.size();
|
2012-12-27 06:45:19 +00:00
|
|
|
Do(list_size);
|
2012-12-27 05:07:29 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-12-27 06:45:19 +00:00
|
|
|
// Store STL sets.
|
2013-02-04 16:24:32 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(std::set<T *> &x)
|
|
|
|
{
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
{
|
|
|
|
for (auto it = x.begin(), end = x.end(); it != end; ++it)
|
|
|
|
{
|
|
|
|
if (*it != NULL)
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DoSet(x);
|
|
|
|
}
|
|
|
|
|
2012-12-27 06:45:19 +00:00
|
|
|
template <class T>
|
|
|
|
void Do(std::set<T> &x)
|
2013-02-04 16:24:32 +00:00
|
|
|
{
|
|
|
|
DoSet(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
void DoSet(std::set<T> &x)
|
2012-12-27 06:45:19 +00:00
|
|
|
{
|
|
|
|
unsigned int number = (unsigned int)x.size();
|
|
|
|
Do(number);
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MODE_READ:
|
|
|
|
{
|
|
|
|
x.clear();
|
|
|
|
while (number-- > 0)
|
|
|
|
{
|
2013-09-01 08:05:35 +00:00
|
|
|
T it = T();
|
2012-12-27 06:45:19 +00:00
|
|
|
Do(it);
|
|
|
|
x.insert(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
|
|
|
case MODE_VERIFY:
|
|
|
|
{
|
|
|
|
typename std::set<T>::iterator itr = x.begin();
|
|
|
|
while (number-- > 0)
|
|
|
|
Do(*itr++);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-03-06 12:10:23 +00:00
|
|
|
ERROR_LOG(SAVESTATE, "Savestate error: invalid mode %d.", mode);
|
2012-12-27 06:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Store strings.
|
2013-10-07 04:26:08 +00:00
|
|
|
void Do(std::string &x);
|
2020-03-22 15:44:19 +00:00
|
|
|
void Do(std::wstring &x); // DEPRECATED, do not save wstrings
|
|
|
|
void Do(std::u16string &x);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-10-07 04:42:51 +00:00
|
|
|
void Do(tm &t);
|
|
|
|
|
2014-01-26 22:24:32 +00:00
|
|
|
template<typename T, typename F>
|
|
|
|
void Do(swap_struct_t<T, F> &x) {
|
|
|
|
T v = x.swap();
|
|
|
|
Do(v);
|
|
|
|
x = v;
|
|
|
|
}
|
|
|
|
|
2013-02-04 09:03:04 +00:00
|
|
|
template<class T>
|
|
|
|
void DoClass(T &x) {
|
|
|
|
x.DoState(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
void DoClass(T *&x) {
|
|
|
|
if (mode == MODE_READ)
|
2013-02-04 16:24:32 +00:00
|
|
|
{
|
|
|
|
if (x != NULL)
|
|
|
|
delete x;
|
2013-02-04 09:03:04 +00:00
|
|
|
x = new T();
|
2013-02-04 16:24:32 +00:00
|
|
|
}
|
2013-02-04 09:03:04 +00:00
|
|
|
x->DoState(*this);
|
|
|
|
}
|
|
|
|
|
2013-02-04 08:52:56 +00:00
|
|
|
template<class T>
|
2012-11-01 15:19:01 +00:00
|
|
|
void DoArray(T *x, int count) {
|
2013-02-04 09:39:52 +00:00
|
|
|
DoHelper<T>::DoArray(this, x, count);
|
2013-02-04 08:52:56 +00:00
|
|
|
}
|
2013-02-04 19:19:00 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
template<class T>
|
|
|
|
void Do(T &x) {
|
2013-02-04 08:52:56 +00:00
|
|
|
DoHelper<T>::Do(this, x);
|
2012-11-01 15:19: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
|
|
|
|
s32 offset = x - base;
|
|
|
|
Do(offset);
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
x = base + offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, LinkedListItem<T>* (*TNew)(), void (*TFree)(LinkedListItem<T>*), void (*TDo)(PointerWrap&, T*)>
|
|
|
|
void DoLinkedList(LinkedListItem<T>*& list_start, LinkedListItem<T>** list_end=0)
|
|
|
|
{
|
|
|
|
LinkedListItem<T>* list_cur = list_start;
|
|
|
|
LinkedListItem<T>* prev = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
u8 shouldExist = (list_cur ? 1 : 0);
|
|
|
|
Do(shouldExist);
|
|
|
|
if (shouldExist == 1)
|
|
|
|
{
|
|
|
|
LinkedListItem<T>* cur = list_cur ? list_cur : TNew();
|
|
|
|
TDo(*this, (T*)cur);
|
|
|
|
if (!list_cur)
|
|
|
|
{
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
{
|
|
|
|
cur->next = 0;
|
|
|
|
list_cur = cur;
|
|
|
|
if (prev)
|
|
|
|
prev->next = cur;
|
|
|
|
else
|
|
|
|
list_start = cur;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TFree(cur);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-09 05:18:56 +00:00
|
|
|
if (shouldExist != 0)
|
|
|
|
{
|
2017-03-06 12:10:23 +00:00
|
|
|
WARN_LOG(SAVESTATE, "Savestate failure: incorrect item marker %d", shouldExist);
|
2014-12-09 05:18:56 +00:00
|
|
|
SetError(ERROR_FAILURE);
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
if (mode == MODE_READ)
|
|
|
|
{
|
|
|
|
if (prev)
|
|
|
|
prev->next = 0;
|
|
|
|
if (list_end)
|
|
|
|
*list_end = prev;
|
|
|
|
if (list_cur)
|
|
|
|
{
|
|
|
|
if (list_start == list_cur)
|
|
|
|
list_start = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
LinkedListItem<T>* next = list_cur->next;
|
|
|
|
TFree(list_cur);
|
|
|
|
list_cur = next;
|
|
|
|
}
|
|
|
|
while (list_cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = list_cur;
|
|
|
|
list_cur = list_cur->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-07 04:26:08 +00:00
|
|
|
void DoMarker(const char *prevName, u32 arbitraryNumber = 0x42);
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CChunkFileReader
|
|
|
|
{
|
|
|
|
public:
|
2013-09-15 04:19:10 +00:00
|
|
|
enum Error {
|
|
|
|
ERROR_NONE,
|
|
|
|
ERROR_BAD_FILE,
|
|
|
|
ERROR_BROKEN_STATE,
|
2017-03-07 14:19:20 +00:00
|
|
|
ERROR_BAD_ALLOC,
|
2013-09-15 04:19:10 +00:00
|
|
|
};
|
|
|
|
|
2013-09-29 20:54:39 +00:00
|
|
|
// May fail badly if ptr doesn't point to valid data.
|
|
|
|
template<class T>
|
2020-08-02 15:11:09 +00:00
|
|
|
static Error LoadPtr(u8 *ptr, T &_class, std::string *errorString)
|
2013-09-29 20:54:39 +00:00
|
|
|
{
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_READ);
|
|
|
|
_class.DoState(p);
|
|
|
|
|
|
|
|
if (p.error != p.ERROR_FAILURE) {
|
|
|
|
return ERROR_NONE;
|
|
|
|
} else {
|
2020-08-02 15:11:09 +00:00
|
|
|
*errorString = std::string("Failure at ") + p.GetBadSectionTitle();
|
2013-09-29 20:54:39 +00:00
|
|
|
return ERROR_BROKEN_STATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static size_t MeasurePtr(T &_class)
|
|
|
|
{
|
|
|
|
u8 *ptr = 0;
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
|
|
|
|
_class.DoState(p);
|
|
|
|
return (size_t)ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expects ptr to have at least MeasurePtr bytes at ptr.
|
|
|
|
template<class T>
|
|
|
|
static Error SavePtr(u8 *ptr, T &_class)
|
|
|
|
{
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_WRITE);
|
|
|
|
_class.DoState(p);
|
|
|
|
|
|
|
|
if (p.error != p.ERROR_FAILURE) {
|
|
|
|
return ERROR_NONE;
|
|
|
|
} else {
|
|
|
|
return ERROR_BROKEN_STATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Load file template
|
|
|
|
template<class T>
|
2018-06-15 01:25:52 +00:00
|
|
|
static Error Load(const std::string &filename, std::string *gitVersion, T& _class, std::string *failureReason)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2016-01-23 20:53:03 +00:00
|
|
|
*failureReason = "LoadStateWrongVersion";
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2015-10-04 10:09:05 +00:00
|
|
|
u8 *ptr = nullptr;
|
2013-10-07 04:26:08 +00:00
|
|
|
size_t sz;
|
2016-01-23 20:53:03 +00:00
|
|
|
Error error = LoadFile(filename, gitVersion, ptr, sz, failureReason);
|
2013-10-07 04:26:08 +00:00
|
|
|
if (error == ERROR_NONE) {
|
2016-01-23 20:53:03 +00:00
|
|
|
failureReason->clear();
|
2020-08-02 15:11:09 +00:00
|
|
|
error = LoadPtr(ptr, _class, failureReason);
|
|
|
|
delete [] ptr;
|
|
|
|
INFO_LOG(SAVESTATE, "ChunkReader: Done loading '%s'", filename.c_str());
|
|
|
|
} else {
|
|
|
|
WARN_LOG(SAVESTATE, "ChunkReader: Error found during load of '%s'", filename.c_str());
|
2013-10-07 04:26:08 +00:00
|
|
|
}
|
2013-09-29 20:54:39 +00:00
|
|
|
return error;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2013-09-29 20:54:39 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Save file template
|
|
|
|
template<class T>
|
2016-01-23 20:53:03 +00:00
|
|
|
static Error Save(const std::string &filename, const std::string &title, const char *gitVersion, T& _class)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
|
|
|
// Get data
|
2013-09-29 20:54:39 +00:00
|
|
|
size_t const sz = MeasurePtr(_class);
|
2017-11-05 18:04:22 +00:00
|
|
|
u8 *buffer = (u8 *)malloc(sz);
|
|
|
|
if (!buffer)
|
2017-03-07 14:19:20 +00:00
|
|
|
return ERROR_BAD_ALLOC;
|
2013-09-29 20:54:39 +00:00
|
|
|
Error error = SavePtr(buffer, _class);
|
2013-01-02 21:10:41 +00:00
|
|
|
|
2015-10-04 10:09:05 +00:00
|
|
|
// SaveFile takes ownership of buffer
|
2013-10-07 04:26:08 +00:00
|
|
|
if (error == ERROR_NONE)
|
2016-01-23 20:53:03 +00:00
|
|
|
error = SaveFile(filename, title, gitVersion, buffer, sz);
|
2013-09-29 20:54:39 +00:00
|
|
|
return error;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-27 18:34:22 +00:00
|
|
|
template <class T>
|
2013-09-15 04:19:10 +00:00
|
|
|
static Error Verify(T& _class)
|
2012-12-27 18:34:22 +00:00
|
|
|
{
|
|
|
|
u8 *ptr = 0;
|
|
|
|
|
|
|
|
// Step 1: Measure the space required.
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
|
|
|
|
_class.DoState(p);
|
|
|
|
size_t const sz = (size_t)ptr;
|
|
|
|
std::vector<u8> buffer(sz);
|
|
|
|
|
|
|
|
// Step 2: Dump the state.
|
|
|
|
ptr = &buffer[0];
|
|
|
|
p.SetMode(PointerWrap::MODE_WRITE);
|
|
|
|
_class.DoState(p);
|
|
|
|
|
|
|
|
// Step 3: Verify the state.
|
|
|
|
ptr = &buffer[0];
|
|
|
|
p.SetMode(PointerWrap::MODE_VERIFY);
|
|
|
|
_class.DoState(p);
|
|
|
|
|
2013-09-15 04:19:10 +00:00
|
|
|
return ERROR_NONE;
|
2012-12-27 18:34:22 +00:00
|
|
|
}
|
|
|
|
|
2016-01-23 20:53:03 +00:00
|
|
|
static Error GetFileTitle(const std::string &filename, std::string *title);
|
2013-10-07 04:26:08 +00:00
|
|
|
|
2016-01-23 20:53:03 +00:00
|
|
|
private:
|
2012-11-01 15:19:01 +00:00
|
|
|
struct SChunkHeader
|
|
|
|
{
|
|
|
|
int Revision;
|
|
|
|
int Compress;
|
2013-10-19 21:16:07 +00:00
|
|
|
u32 ExpectedSize;
|
|
|
|
u32 UncompressedSize;
|
2013-07-17 05:33:26 +00:00
|
|
|
char GitVersion[32];
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
2016-01-23 20:53:03 +00:00
|
|
|
|
|
|
|
enum {
|
|
|
|
REVISION_MIN = 4,
|
|
|
|
REVISION_TITLE = 5,
|
|
|
|
REVISION_CURRENT = REVISION_TITLE,
|
|
|
|
};
|
|
|
|
|
2018-06-15 01:25:52 +00:00
|
|
|
static Error LoadFile(const std::string &filename, std::string *gitVersion, u8 *&buffer, size_t &sz, std::string *failureReason);
|
2016-01-23 20:53:03 +00:00
|
|
|
static Error SaveFile(const std::string &filename, const std::string &title, const char *gitVersion, u8 *buffer, size_t sz);
|
|
|
|
static Error LoadFileHeader(File::IOFile &pFile, SChunkHeader &header, std::string *title);
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|