2009-07-28 15:32:10 -06:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-07 21:46:09 -07: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.
|
|
|
|
|
|
|
|
// 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/
|
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
#ifndef _POINTERWRAP_H_
|
|
|
|
#define _POINTERWRAP_H_
|
2008-12-07 21:46:09 -07: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.
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2013-03-02 02:28:44 -07:00
|
|
|
#include <list>
|
2011-05-27 13:55:07 -06:00
|
|
|
#include <deque>
|
2008-12-07 21:46:09 -07:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "Common.h"
|
2009-02-27 18:26:56 -07:00
|
|
|
#include "FileUtil.h"
|
2008-12-07 21:46:09 -07:00
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct LinkedListItem : public T
|
|
|
|
{
|
|
|
|
LinkedListItem<T> *next;
|
|
|
|
};
|
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Wrapper class
|
2008-12-07 21:46:09 -07:00
|
|
|
class PointerWrap
|
|
|
|
{
|
|
|
|
public:
|
2013-03-02 02:28:44 -07:00
|
|
|
enum Mode
|
|
|
|
{
|
2010-04-17 15:02:03 -06:00
|
|
|
MODE_READ = 1, // load
|
|
|
|
MODE_WRITE, // save
|
|
|
|
MODE_MEASURE, // calculate size
|
|
|
|
MODE_VERIFY, // compare
|
2008-12-07 21:46:09 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
u8 **ptr;
|
|
|
|
Mode mode;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PointerWrap(u8 **ptr_, Mode mode_) : ptr(ptr_), mode(mode_) {}
|
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
void SetMode(Mode mode_) { mode = mode_; }
|
|
|
|
Mode GetMode() const { return mode; }
|
|
|
|
u8** GetPPtr() { return ptr; }
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename K, class V>
|
|
|
|
void Do(std::map<K, V>& x)
|
2008-12-07 21:46:09 -07:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
u32 count = (u32)x.size();
|
|
|
|
Do(count);
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
{
|
2009-11-24 10:10:38 -07: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;
|
2013-03-02 02:28:44 -07:00
|
|
|
|
2009-11-24 10:10:38 -07:00
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
2010-04-17 15:02:03 -06:00
|
|
|
case MODE_VERIFY:
|
2013-03-02 02:28:44 -07:00
|
|
|
for (auto itr = x.begin(); itr != x.end(); ++itr)
|
2009-11-24 10:10:38 -07:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
Do(itr->first);
|
|
|
|
Do(itr->second);
|
2009-11-24 10:10:38 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-02 02:28:44 -07:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DoContainer(T& x)
|
|
|
|
{
|
|
|
|
u32 size = (u32)x.size();
|
|
|
|
Do(size);
|
|
|
|
x.resize(size);
|
|
|
|
|
|
|
|
for (auto itr = x.begin(); itr != x.end(); ++itr)
|
|
|
|
Do(*itr);
|
|
|
|
}
|
2009-11-24 10:10:38 -07: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
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
DoContainer(x);
|
2008-12-07 21:46:09 -07:00
|
|
|
}
|
2009-02-27 18:26:56 -07: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
|
|
|
}
|
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::deque<T>& x)
|
2008-12-07 21:46:09 -07:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
DoContainer(x);
|
2008-12-07 21:46:09 -07: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
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
DoContainer(x);
|
2011-12-18 23:01:46 -07:00
|
|
|
}
|
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void DoArray(T* x, u32 count)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i != count; ++i)
|
|
|
|
Do(x[i]);
|
2008-12-07 21:46:09 -07:00
|
|
|
}
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(T& x)
|
|
|
|
{
|
|
|
|
// TODO: Bad, Do(some_non_POD) will compile and fail at runtime
|
|
|
|
// type_traits are not fully supported everywhere yet
|
|
|
|
|
|
|
|
DoVoid((void*)&x, sizeof(x));
|
2008-12-07 21:46:09 -07:00
|
|
|
}
|
2012-01-04 01:42:22 -07: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
|
|
|
|
s32 offset = x - base;
|
|
|
|
Do(offset);
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
x = base + offset;
|
|
|
|
}
|
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
// Let's pretend std::list doesn't exist!
|
|
|
|
template <class T, LinkedListItem<T>* (*TNew)(), void (*TFree)(LinkedListItem<T>*), void (*TDo)(PointerWrap&, T*)>
|
2011-12-19 00:36:54 -07:00
|
|
|
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
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2008-12-07 21:46:09 -07:00
|
|
|
}
|
2011-12-17 17:49:24 -07:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
void DoMarker(const char* prevName, u32 arbitraryNumber = 0x42)
|
2011-12-17 17:49:24 -07:00
|
|
|
{
|
|
|
|
u32 cookie = arbitraryNumber;
|
|
|
|
Do(cookie);
|
2013-03-02 02:28:44 -07:00
|
|
|
|
|
|
|
if (mode == PointerWrap::MODE_READ && cookie != arbitraryNumber)
|
2011-12-17 17:49:24 -07:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
PanicAlertT("Error: After \"%s\", found %d (0x%X) instead of save marker %d (0x%X). Aborting savestate load...",
|
|
|
|
prevName, cookie, cookie, arbitraryNumber, arbitraryNumber);
|
2011-12-17 17:49:24 -07:00
|
|
|
mode = PointerWrap::MODE_MEASURE;
|
|
|
|
}
|
|
|
|
}
|
2013-03-02 02:28:44 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
__forceinline void DoByte(u8& x)
|
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MODE_READ:
|
|
|
|
x = **ptr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MODE_WRITE:
|
|
|
|
**ptr = x;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MODE_MEASURE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MODE_VERIFY:
|
|
|
|
_dbg_assert_msg_(COMMON, (x == *ptr),
|
|
|
|
"Savestate verification failure: %d (0x%X) (at %p) != %d (0x%X) (at %p).\n",
|
|
|
|
x, x, &x, *ptr, *ptr, &*ptr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
++(*ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoVoid(void *data, u32 size)
|
|
|
|
{
|
|
|
|
for(u32 i = 0; i != size; ++i)
|
|
|
|
DoByte(reinterpret_cast<u8*>(data)[i]);
|
|
|
|
}
|
2008-12-07 21:46:09 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class CChunkFileReader
|
|
|
|
{
|
|
|
|
public:
|
2009-02-27 18:26:56 -07:00
|
|
|
// Load file template
|
2008-12-07 21:46:09 -07:00
|
|
|
template<class T>
|
2013-03-02 02:28:44 -07:00
|
|
|
static bool Load(const std::string& _rFilename, u32 _Revision, T& _class)
|
2008-12-07 21:46:09 -07:00
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
INFO_LOG(COMMON, "ChunkReader: Loading %s" , _rFilename.c_str());
|
|
|
|
|
2011-02-28 20:06:14 -07:00
|
|
|
if (!File::Exists(_rFilename))
|
2009-02-27 18:26:56 -07:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check file size
|
2011-03-11 03:21:46 -07:00
|
|
|
const u64 fileSize = File::GetSize(_rFilename);
|
|
|
|
static const u64 headerSize = sizeof(SChunkHeader);
|
|
|
|
if (fileSize < headerSize)
|
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON,"ChunkReader: File too small");
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile pFile(_rFilename, "rb");
|
|
|
|
if (!pFile)
|
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON,"ChunkReader: Can't open file for reading");
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// read the header
|
|
|
|
SChunkHeader header;
|
2011-03-11 03:21:46 -07:00
|
|
|
if (!pFile.ReadArray(&header, 1))
|
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON,"ChunkReader: Bad header size");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check revision
|
2011-03-11 03:21:46 -07:00
|
|
|
if (header.Revision != _Revision)
|
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON,"ChunkReader: Wrong file revision, got %d expected %d",
|
2011-03-11 03:21:46 -07:00
|
|
|
header.Revision, _Revision);
|
2009-02-27 18:26:56 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get size
|
2013-03-02 02:28:44 -07:00
|
|
|
const u32 sz = (u32)(fileSize - headerSize);
|
2011-03-11 03:21:46 -07:00
|
|
|
if (header.ExpectedSize != sz)
|
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON,"ChunkReader: Bad file size, got %d expected %d",
|
2011-03-11 03:21:46 -07:00
|
|
|
sz, header.ExpectedSize);
|
2009-02-27 18:26:56 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the state
|
2013-03-02 02:28:44 -07:00
|
|
|
std::vector<u8> buffer(sz);
|
|
|
|
if (!pFile.ReadArray(&buffer[0], sz))
|
2011-03-11 03:21:46 -07:00
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON,"ChunkReader: Error reading file");
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
u8* ptr = &buffer[0];
|
2009-02-27 18:26:56 -07:00
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_READ);
|
|
|
|
_class.DoState(p);
|
|
|
|
|
|
|
|
INFO_LOG(COMMON, "ChunkReader: Done loading %s" , _rFilename.c_str());
|
|
|
|
return true;
|
2008-12-07 21:46:09 -07:00
|
|
|
}
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
// Save file template
|
2008-12-07 21:46:09 -07:00
|
|
|
template<class T>
|
2013-03-02 02:28:44 -07:00
|
|
|
static bool Save(const std::string& _rFilename, u32 _Revision, T& _class)
|
2008-12-07 21:46:09 -07:00
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
INFO_LOG(COMMON, "ChunkReader: Writing %s" , _rFilename.c_str());
|
2011-03-11 03:21:46 -07:00
|
|
|
File::IOFile pFile(_rFilename, "wb");
|
|
|
|
if (!pFile)
|
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON,"ChunkReader: Error opening file for write");
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Get data
|
|
|
|
u8 *ptr = 0;
|
|
|
|
PointerWrap p(&ptr, PointerWrap::MODE_MEASURE);
|
|
|
|
_class.DoState(p);
|
2011-03-22 01:27:23 -06:00
|
|
|
size_t const sz = (size_t)ptr;
|
|
|
|
std::vector<u8> buffer(sz);
|
|
|
|
ptr = &buffer[0];
|
2009-02-27 18:26:56 -07:00
|
|
|
p.SetMode(PointerWrap::MODE_WRITE);
|
|
|
|
_class.DoState(p);
|
|
|
|
|
|
|
|
// Create header
|
|
|
|
SChunkHeader header;
|
|
|
|
header.Revision = _Revision;
|
2013-03-02 02:28:44 -07:00
|
|
|
header.ExpectedSize = (u32)sz;
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
// Write to file
|
2011-03-11 03:21:46 -07:00
|
|
|
if (!pFile.WriteArray(&header, 1))
|
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON,"ChunkReader: Failed writing header");
|
|
|
|
return false;
|
2008-12-07 21:46:09 -07:00
|
|
|
}
|
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
if (!pFile.WriteArray(&buffer[0], sz))
|
2011-03-11 03:21:46 -07:00
|
|
|
{
|
2009-02-27 18:26:56 -07:00
|
|
|
ERROR_LOG(COMMON,"ChunkReader: Failed writing data");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
INFO_LOG(COMMON,"ChunkReader: Done writing %s", _rFilename.c_str());
|
2009-02-27 18:26:56 -07:00
|
|
|
return true;
|
2008-12-07 21:46:09 -07:00
|
|
|
}
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2008-12-07 21:46:09 -07:00
|
|
|
private:
|
|
|
|
struct SChunkHeader
|
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
u32 Revision;
|
|
|
|
u32 ExpectedSize;
|
2008-12-07 21:46:09 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-03-28 02:57:34 -06:00
|
|
|
#endif // _POINTERWRAP_H_
|