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