1
// Copyright (C) 2003 Dolphin Project.
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
20
// Extremely simple serialization framework.
21
// Currently mis-named, a native ChunkFile is something different (a RIFF file)
26
// + Same code is used for serialization and deserializaition (in most cases)
27
// + Sections can be versioned for backwards/forwards compatibility
28
// - Serialization code for anything complex has to be manually written.
31
#include <unordered_map>
35
#if defined(MACGNUSTD)
36
#include <tr1/type_traits>
38
#include <type_traits>
46
#include "../ext/snappy/snappy-c.h"
49
#if defined(MACGNUSTD)
51
using tr1::is_pointer;
56
struct LinkedListItem : public T
58
LinkedListItem<T> *next;
63
class PointerWrapSection
66
PointerWrapSection(PointerWrap &p, int ver, const char *title) : p_(p), ver_(ver), title_(title) {
68
~PointerWrapSection();
70
bool operator == (const int &v) const { return ver_ == v; }
71
bool operator != (const int &v) const { return ver_ != v; }
72
bool operator <= (const int &v) const { return ver_ <= v; }
73
bool operator >= (const int &v) const { return ver_ >= v; }
74
bool operator < (const int &v) const { return ver_ < v; }
75
bool operator > (const int &v) const { return ver_ > v; }
77
operator bool() const {
90
// This makes it a compile error if you forget to define DoState() on non-POD.
91
// Which also can be a problem, for example struct tm is non-POD on linux, for whatever reason...
93
template<typename T, bool isPOD = std::is_pod<T>::value, bool isPointer = std::is_pointer<T>::value>
95
template<typename T, bool isPOD = __is_pod(T), bool isPointer = std::is_pointer<T>::value>
99
static void DoArray(PointerWrap *p, T *x, int count)
101
for (int i = 0; i < count; ++i)
105
static void Do(PointerWrap *p, T &x)
112
struct DoHelper<T, true, false>
114
static void DoArray(PointerWrap *p, T *x, int count)
116
p->DoVoid((void *)x, sizeof(T) * count);
119
static void Do(PointerWrap *p, T &x)
121
p->DoVoid((void *)&x, sizeof(x));
127
MODE_READ = 1, // load
129
MODE_MEASURE, // calculate size
130
MODE_VERIFY, // compare
144
PointerWrap(u8 **ptr_, Mode mode_) : ptr(ptr_), mode(mode_), error(ERROR_NONE) {}
145
PointerWrap(unsigned char **ptr_, int mode_) : ptr((u8**)ptr_), mode((Mode)mode_), error(ERROR_NONE) {}
147
PointerWrapSection Section(const char *title, int ver);
149
// The returned object can be compared against the version that was loaded.
150
// This can be used to support versions as old as minVer.
151
// Version = 0 means the section was not found.
152
PointerWrapSection Section(const char *title, int minVer, int ver);
154
void SetMode(Mode mode_) {mode = mode_;}
155
Mode GetMode() const {return mode;}
156
u8 **GetPPtr() {return ptr;}
157
void SetError(Error error_);
159
// Same as DoVoid, except doesn't advance pointer if it doesn't match on read.
160
bool ExpectVoid(void *data, int size);
161
void DoVoid(void *data, int size);
163
template<class K, class T>
164
void Do(std::map<K, T *> &x)
166
if (mode == MODE_READ)
168
for (auto it = x.begin(), end = x.end(); it != end; ++it)
170
if (it->second != NULL)
178
template<class K, class T>
179
void Do(std::map<K, T> &x)
185
template<class K, class T>
186
void Do(std::unordered_map<K, T *> &x)
188
if (mode == MODE_READ)
190
for (auto it = x.begin(), end = x.end(); it != end; ++it)
192
if (it->second != NULL)
200
template<class K, class T>
201
void Do(std::unordered_map<K, T> &x)
208
void DoMap(M &x, typename M::mapped_type &default_val)
210
unsigned int number = (unsigned int)x.size();
218
typename M::key_type first = typename M::key_type();
220
typename M::mapped_type second = default_val;
231
typename M::iterator itr = x.begin();
234
typename M::key_type first = itr->first;
245
template<class K, class T>
246
void Do(std::multimap<K, T *> &x)
248
if (mode == MODE_READ)
250
for (auto it = x.begin(), end = x.end(); it != end; ++it)
252
if (it->second != NULL)
260
template<class K, class T>
261
void Do(std::multimap<K, T> &x)
267
template<class K, class T>
268
void Do(std::unordered_multimap<K, T *> &x)
270
if (mode == MODE_READ)
272
for (auto it = x.begin(), end = x.end(); it != end; ++it)
274
if (it->second != NULL)
282
template<class K, class T>
283
void Do(std::unordered_multimap<K, T> &x)
290
void DoMultimap(M &x, typename M::mapped_type &default_val)
292
unsigned int number = (unsigned int)x.size();
300
typename M::key_type first = typename M::key_type();
302
typename M::mapped_type second = default_val;
304
x.insert(std::make_pair(first, second));
313
typename M::iterator itr = x.begin();
328
void Do(std::vector<T *> &x)
335
void Do(std::vector<T> &x)
342
void Do(std::vector<T> &x, T &default_val)
344
DoVector(x, default_val);
348
void DoVector(std::vector<T> &x, T &default_val)
350
u32 vec_size = (u32)x.size();
352
x.resize(vec_size, default_val);
354
DoArray(&x[0], vec_size);
359
void Do(std::deque<T *> &x)
366
void Do(std::deque<T> &x)
373
void DoDeque(std::deque<T> &x, T &default_val)
375
u32 deq_size = (u32)x.size();
377
x.resize(deq_size, default_val);
379
for(i = 0; i < deq_size; i++)
385
void Do(std::list<T *> &x)
392
void Do(std::list<T> &x)
399
void Do(std::list<T> &x, T &default_val)
401
DoList(x, default_val);
405
void DoList(std::list<T> &x, T &default_val)
407
u32 list_size = (u32)x.size();
409
x.resize(list_size, default_val);
411
typename std::list<T>::iterator itr, end;
412
for (itr = x.begin(), end = x.end(); itr != end; ++itr)
418
void Do(std::set<T *> &x)
420
if (mode == MODE_READ)
422
for (auto it = x.begin(), end = x.end(); it != end; ++it)
432
void Do(std::set<T> &x)
438
void DoSet(std::set<T> &x)
440
unsigned int number = (unsigned int)x.size();
460
typename std::set<T>::iterator itr = x.begin();
467
ERROR_LOG(COMMON, "Savestate error: invalid mode %d.", mode);
472
void Do(std::string &x);
473
void Do(std::wstring &x);
477
template<typename T, typename F>
478
void Do(swap_struct_t<T, F> &x) {
490
void DoClass(T *&x) {
491
if (mode == MODE_READ)
501
void DoArray(T *x, int count) {
502
DoHelper<T>::DoArray(this, x, count);
507
DoHelper<T>::Do(this, x);
511
void DoPointer(T* &x, T*const base) {
512
// pointers can be more than 2^31 apart, but you're using this function wrong if you need that much range
513
s32 offset = x - base;
515
if (mode == MODE_READ)
519
template<class T, LinkedListItem<T>* (*TNew)(), void (*TFree)(LinkedListItem<T>*), void (*TDo)(PointerWrap&, T*)>
520
void DoLinkedList(LinkedListItem<T>*& list_start, LinkedListItem<T>** list_end=0)
522
LinkedListItem<T>* list_cur = list_start;
523
LinkedListItem<T>* prev = 0;
527
u8 shouldExist = (list_cur ? 1 : 0);
529
if (shouldExist == 1)
531
LinkedListItem<T>* cur = list_cur ? list_cur : TNew();
535
if (mode == MODE_READ)
553
if (shouldExist != 0)
555
WARN_LOG(COMMON, "Savestate failure: incorrect item marker %d", shouldExist);
556
SetError(ERROR_FAILURE);
558
if (mode == MODE_READ)
566
if (list_start == list_cur)
570
LinkedListItem<T>* next = list_cur->next;
580
list_cur = list_cur->next;
584
void DoMarker(const char *prevName, u32 arbitraryNumber = 0x42);
587
class CChunkFileReader
596
// May fail badly if ptr doesn't point to valid data.
598
static Error LoadPtr(u8 *ptr, T &_class)
600
PointerWrap p(&ptr, PointerWrap::MODE_READ);
603
if (p.error != p.ERROR_FAILURE) {
606
return ERROR_BROKEN_STATE;
611
static size_t MeasurePtr(T &_class)
614
PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
619
// Expects ptr to have at least MeasurePtr bytes at ptr.
621
static Error SavePtr(u8 *ptr, T &_class)
623
PointerWrap p(&ptr, PointerWrap::MODE_WRITE);
626
if (p.error != p.ERROR_FAILURE) {
629
return ERROR_BROKEN_STATE;
633
// Load file template
635
static Error Load(const std::string &filename, const char *gitVersion, T& _class, std::string *failureReason)
637
*failureReason = "LoadStateWrongVersion";
641
Error error = LoadFile(filename, gitVersion, ptr, sz, failureReason);
642
if (error == ERROR_NONE) {
643
error = LoadPtr(ptr, _class);
647
INFO_LOG(COMMON, "ChunkReader: Done loading %s", filename.c_str());
648
if (error == ERROR_NONE) {
649
failureReason->clear();
654
// Save file template
656
static Error Save(const std::string &filename, const std::string &title, const char *gitVersion, T& _class)
659
size_t const sz = MeasurePtr(_class);
660
u8 *buffer = new u8[sz];
661
Error error = SavePtr(buffer, _class);
663
// SaveFile takes ownership of buffer
664
if (error == ERROR_NONE)
665
error = SaveFile(filename, title, gitVersion, buffer, sz);
670
static Error Verify(T& _class)
674
// Step 1: Measure the space required.
675
PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
677
size_t const sz = (size_t)ptr;
678
std::vector<u8> buffer(sz);
680
// Step 2: Dump the state.
682
p.SetMode(PointerWrap::MODE_WRITE);
685
// Step 3: Verify the state.
687
p.SetMode(PointerWrap::MODE_VERIFY);
693
static Error GetFileTitle(const std::string &filename, std::string *title);
701
u32 UncompressedSize;
708
REVISION_CURRENT = REVISION_TITLE,
711
static Error LoadFile(const std::string &filename, const char *gitVersion, u8 *&buffer, size_t &sz, std::string *failureReason);
712
static Error SaveFile(const std::string &filename, const std::string &title, const char *gitVersion, u8 *buffer, size_t sz);
713
static Error LoadFileHeader(File::IOFile &pFile, SChunkHeader &header, std::string *title);