2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:09:55 +00:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
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.
|
|
|
|
|
2014-09-20 11:32:04 +00:00
|
|
|
#include <array>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <cstddef>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <deque>
|
|
|
|
#include <list>
|
2008-12-08 04:46:09 +00:00
|
|
|
#include <map>
|
2013-08-26 00:06:58 +00:00
|
|
|
#include <set>
|
2008-12-08 04:46:09 +00:00
|
|
|
#include <string>
|
2013-04-09 23:57:39 +00:00
|
|
|
#include <type_traits>
|
2014-02-20 03:11:52 +00:00
|
|
|
#include <utility>
|
2014-02-17 10:18:15 +00:00
|
|
|
#include <vector>
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2014-09-08 01:06:58 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2014-08-24 23:38:33 +00:00
|
|
|
#include "Common/Flag.h"
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2013-08-26 00:06:58 +00:00
|
|
|
// ewww
|
|
|
|
#if _LIBCPP_VERSION
|
2014-03-28 03:42:52 +00:00
|
|
|
#define IsTriviallyCopyable(T) std::is_trivially_copyable<typename std::remove_volatile<T>::type>::value
|
2013-08-26 00:06:58 +00:00
|
|
|
#elif __GNUC__
|
|
|
|
#define IsTriviallyCopyable(T) std::has_trivial_copy_constructor<T>::value
|
|
|
|
#elif _MSC_VER >= 1800
|
|
|
|
// work around bug
|
|
|
|
#define IsTriviallyCopyable(T) (std::is_trivially_copyable<T>::value || std::is_pod<T>::value)
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
#define IsTriviallyCopyable(T) std::has_trivial_copy<T>::value
|
|
|
|
#else
|
|
|
|
#error No version of is_trivially_copyable
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
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);
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
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-20 01:51:12 +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-10-29 05:09:01 +00:00
|
|
|
for (auto& elem : x)
|
2009-11-24 17:10:38 +00:00
|
|
|
{
|
2013-10-29 05:09:01 +00:00
|
|
|
Do(elem.first);
|
|
|
|
Do(elem.second);
|
2009-11-24 17:10:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-08-26 00:06:58 +00:00
|
|
|
template <typename V>
|
|
|
|
void Do(std::set<V>& x)
|
|
|
|
{
|
|
|
|
u32 count = (u32)x.size();
|
|
|
|
Do(count);
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MODE_READ:
|
|
|
|
for (x.clear(); count != 0; --count)
|
|
|
|
{
|
|
|
|
V value;
|
|
|
|
Do(value);
|
|
|
|
x.insert(value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
|
|
|
case MODE_VERIFY:
|
2014-02-12 15:00:34 +00:00
|
|
|
for (V& val : x)
|
2013-08-26 00:06:58 +00:00
|
|
|
{
|
2014-02-12 15:00:34 +00:00
|
|
|
Do(val);
|
2013-08-26 00:06:58 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2013-03-20 01:51:12 +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-20 01:51:12 +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-20 01:51:12 +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-08-26 00:06:58 +00:00
|
|
|
template <typename T, typename U>
|
|
|
|
void Do(std::pair<T, U>& x)
|
|
|
|
{
|
|
|
|
Do(x.first);
|
|
|
|
Do(x.second);
|
|
|
|
}
|
|
|
|
|
2014-09-20 11:32:04 +00:00
|
|
|
template <typename T, std::size_t N>
|
2015-02-15 19:43:31 +00:00
|
|
|
void DoArray(std::array<T, N>& x)
|
2014-09-20 11:32:04 +00:00
|
|
|
{
|
|
|
|
DoArray(x.data(), (u32)x.size());
|
|
|
|
}
|
|
|
|
|
2013-03-20 01:51:12 +00:00
|
|
|
template <typename T>
|
2013-03-02 09:28:44 +00:00
|
|
|
void DoArray(T* x, u32 count)
|
|
|
|
{
|
2014-08-27 23:50:52 +00:00
|
|
|
static_assert(IsTriviallyCopyable(T), "Only sane for trivially copyable types");
|
|
|
|
DoVoid(x, count * sizeof(T));
|
2013-03-20 01:51:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-24 23:38:33 +00:00
|
|
|
void Do(Common::Flag& flag)
|
|
|
|
{
|
|
|
|
bool s = flag.IsSet();
|
|
|
|
Do(s);
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
flag.Set(s);
|
|
|
|
}
|
|
|
|
|
2015-03-31 08:09:33 +00:00
|
|
|
template<typename T>
|
|
|
|
void Do(std::atomic<T>& atomic)
|
|
|
|
{
|
|
|
|
T temp = atomic.load();
|
|
|
|
Do(temp);
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
atomic.store(temp);
|
|
|
|
}
|
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
template <typename T>
|
|
|
|
void Do(T& x)
|
|
|
|
{
|
2013-08-26 00:06:58 +00:00
|
|
|
static_assert(IsTriviallyCopyable(T), "Only sane for trivially copyable types");
|
2014-08-27 23:50:52 +00:00
|
|
|
// Note:
|
|
|
|
// Usually we can just use x = **ptr, etc. However, this doesn't work
|
|
|
|
// for unions containing BitFields (long story, stupid language rules)
|
|
|
|
// or arrays. This will get optimized anyway.
|
2013-03-02 09:28:44 +00:00
|
|
|
DoVoid((void*)&x, sizeof(x));
|
2008-12-08 04:46:09 +00:00
|
|
|
}
|
2013-08-26 00:06:58 +00:00
|
|
|
|
2013-04-09 23:57:39 +00:00
|
|
|
template <typename T>
|
|
|
|
void DoPOD(T& x)
|
|
|
|
{
|
|
|
|
DoVoid((void*)&x, sizeof(x));
|
|
|
|
}
|
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
|
2013-11-13 09:03:46 +00:00
|
|
|
ptrdiff_t offset = x - base;
|
2012-01-04 08:42:22 +00:00
|
|
|
Do(offset);
|
|
|
|
if (mode == MODE_READ)
|
2013-11-13 09:03:46 +00:00
|
|
|
{
|
2012-01-04 08:42:22 +00:00
|
|
|
x = base + offset;
|
2013-11-13 09:03:46 +00:00
|
|
|
}
|
2012-01-04 08:42:22 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2014-03-09 20:14:26 +00:00
|
|
|
LinkedListItem<T>* prev = nullptr;
|
2011-12-19 07:36:54 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-03-09 20:14:26 +00:00
|
|
|
cur->next = nullptr;
|
2011-12-19 07:36:54 +00:00
|
|
|
list_cur = cur;
|
|
|
|
if (prev)
|
|
|
|
prev->next = cur;
|
|
|
|
else
|
|
|
|
list_start = cur;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TFree(cur);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
{
|
|
|
|
if (prev)
|
2014-03-09 20:14:26 +00:00
|
|
|
prev->next = nullptr;
|
2011-12-19 07:36:54 +00:00
|
|
|
if (list_end)
|
|
|
|
*list_end = prev;
|
|
|
|
if (list_cur)
|
|
|
|
{
|
|
|
|
if (list_start == list_cur)
|
2014-03-09 20:14:26 +00:00
|
|
|
list_start = nullptr;
|
2011-12-19 07:36:54 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
LinkedListItem<T>* next = list_cur->next;
|
|
|
|
TFree(list_cur);
|
|
|
|
list_cur = next;
|
2015-02-15 19:43:31 +00:00
|
|
|
} while (list_cur);
|
2011-12-19 07:36:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2014-03-12 19:33:41 +00:00
|
|
|
void DoMarker(const std::string& prevName, u32 arbitraryNumber = 0x42)
|
2011-12-18 00:49:24 +00:00
|
|
|
{
|
|
|
|
u32 cookie = arbitraryNumber;
|
|
|
|
Do(cookie);
|
2013-03-20 01:51:12 +00:00
|
|
|
|
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...",
|
2014-03-12 19:33:41 +00:00
|
|
|
prevName.c_str(), cookie, cookie, arbitraryNumber, arbitraryNumber);
|
2011-12-18 00:49:24 +00:00
|
|
|
mode = PointerWrap::MODE_MEASURE;
|
|
|
|
}
|
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
private:
|
2014-07-06 03:03:43 +00:00
|
|
|
template <typename T>
|
|
|
|
void DoContainer(T& x)
|
|
|
|
{
|
2014-07-06 07:05:27 +00:00
|
|
|
u32 size = (u32)x.size();
|
2014-07-06 03:03:43 +00:00
|
|
|
Do(size);
|
|
|
|
x.resize(size);
|
|
|
|
|
|
|
|
for (auto& elem : x)
|
|
|
|
Do(elem);
|
|
|
|
}
|
|
|
|
|
2014-08-27 23:50:52 +00:00
|
|
|
__forceinline
|
|
|
|
void DoVoid(void *data, u32 size)
|
2013-03-02 09:28:44 +00:00
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MODE_READ:
|
2014-08-27 23:50:52 +00:00
|
|
|
memcpy(data, *ptr, size);
|
2013-03-02 09:28:44 +00:00
|
|
|
break;
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
case MODE_WRITE:
|
2014-08-27 23:50:52 +00:00
|
|
|
memcpy(*ptr, data, size);
|
2013-03-02 09:28:44 +00:00
|
|
|
break;
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
case MODE_MEASURE:
|
|
|
|
break;
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2013-03-02 09:28:44 +00:00
|
|
|
case MODE_VERIFY:
|
2014-08-27 23:50:52 +00:00
|
|
|
_dbg_assert_msg_(COMMON, !memcmp(data, *ptr, size),
|
|
|
|
"Savestate verification failure: buf %p != %p (size %u).\n",
|
|
|
|
data, *ptr, size);
|
2013-03-02 09:28:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2014-08-27 23:50:52 +00:00
|
|
|
*ptr += size;
|
2013-03-02 09:28:44 +00:00
|
|
|
}
|
2008-12-08 04:46:09 +00:00
|
|
|
};
|
|
|
|
|
2014-11-11 02:41:17 +00:00
|
|
|
// NOTE: this class is only used in DolphinWX/ISOFile.cpp for caching loaded
|
|
|
|
// ISO data. It will be removed when DolphinWX is, so please don't use it.
|
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
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
INFO_LOG(COMMON, "ChunkReader: Loading %s", _rFilename.c_str());
|
2009-02-28 01:26:56 +00:00
|
|
|
|
2011-03-01 03:06:14 +00:00
|
|
|
if (!File::Exists(_rFilename))
|
2009-02-28 01:26:56 +00:00
|
|
|
return false;
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
// 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)
|
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
ERROR_LOG(COMMON, "ChunkReader: File too small");
|
2009-02-28 01:26:56 +00:00
|
|
|
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)
|
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
ERROR_LOG(COMMON, "ChunkReader: Can't open file for reading");
|
2009-02-28 01:26:56 +00:00
|
|
|
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))
|
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
ERROR_LOG(COMMON, "ChunkReader: Bad header size");
|
2009-02-28 01:26:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
// Check revision
|
2011-03-11 10:21:46 +00:00
|
|
|
if (header.Revision != _Revision)
|
|
|
|
{
|
2015-02-15 19:43:31 +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;
|
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
// 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)
|
|
|
|
{
|
2015-02-15 19:43:31 +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;
|
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
// 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
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
ERROR_LOG(COMMON, "ChunkReader: Error reading file");
|
2009-02-28 01:26:56 +00:00
|
|
|
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);
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2015-02-15 19:43:31 +00:00
|
|
|
INFO_LOG(COMMON, "ChunkReader: Done loading %s", _rFilename.c_str());
|
2009-02-28 01:26:56 +00:00
|
|
|
return true;
|
2008-12-08 04:46:09 +00:00
|
|
|
}
|
2013-03-20 01:51:12 +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
|
|
|
{
|
2015-02-15 19:43:31 +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)
|
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
ERROR_LOG(COMMON, "ChunkReader: Error opening file for write");
|
2009-02-28 01:26:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
// Get data
|
2014-03-09 20:14:26 +00:00
|
|
|
u8 *ptr = nullptr;
|
2009-02-28 01:26:56 +00:00
|
|
|
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);
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
// Create header
|
|
|
|
SChunkHeader header;
|
|
|
|
header.Revision = _Revision;
|
2013-03-02 09:28:44 +00:00
|
|
|
header.ExpectedSize = (u32)sz;
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2009-02-28 01:26:56 +00:00
|
|
|
// Write to file
|
2011-03-11 10:21:46 +00:00
|
|
|
if (!pFile.WriteArray(&header, 1))
|
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
ERROR_LOG(COMMON, "ChunkReader: Failed writing header");
|
2009-02-28 01:26:56 +00:00
|
|
|
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
|
|
|
{
|
2015-02-15 19:43:31 +00:00
|
|
|
ERROR_LOG(COMMON, "ChunkReader: Failed writing data");
|
2009-02-28 01:26:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-20 01:51:12 +00:00
|
|
|
|
2015-02-15 19:43:31 +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
|
|
|
}
|
2013-03-20 01:51:12 +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
|
|
|
};
|
|
|
|
};
|