diff options
| author | 2013-09-19 23:28:05 -0400 | |
|---|---|---|
| committer | 2013-09-19 23:28:05 -0400 | |
| commit | a5b31dea56e9a966ec44345c08b1478a2867f4f8 (patch) | |
| tree | 26f0c4b954823abf0c59cbc8a8759b4ce7bf31e8 /src | |
| parent | added a module for loading bootable binaries (diff) | |
| download | yuzu-a5b31dea56e9a966ec44345c08b1478a2867f4f8.tar.gz yuzu-a5b31dea56e9a966ec44345c08b1478a2867f4f8.tar.xz yuzu-a5b31dea56e9a966ec44345c08b1478a2867f4f8.zip | |
updated to chunk_file module from ppsspp
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/src/chunk_file.h | 756 |
1 files changed, 623 insertions, 133 deletions
diff --git a/src/common/src/chunk_file.h b/src/common/src/chunk_file.h index 0c333b094..c480bc8ea 100644 --- a/src/common/src/chunk_file.h +++ b/src/common/src/chunk_file.h | |||
| @@ -1,7 +1,19 @@ | |||
| 1 | // Copyright 2013 Dolphin Emulator Project | 1 | // Copyright (C) 2003 Dolphin Project. |
| 2 | // Licensed under GPLv2 | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | 2 | ||
| 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. | ||
| 6 | |||
| 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. | ||
| 11 | |||
| 12 | // A copy of the GPL 2.0 should have been included with the program. | ||
| 13 | // If not, see http://www.gnu.org/licenses/ | ||
| 14 | |||
| 15 | // Official SVN repository and contact information can be found at | ||
| 16 | // http://code.google.com/p/dolphin-emu/ | ||
| 5 | 17 | ||
| 6 | #ifndef _POINTERWRAP_H_ | 18 | #ifndef _POINTERWRAP_H_ |
| 7 | #define _POINTERWRAP_H_ | 19 | #define _POINTERWRAP_H_ |
| @@ -17,13 +29,39 @@ | |||
| 17 | 29 | ||
| 18 | #include <map> | 30 | #include <map> |
| 19 | #include <vector> | 31 | #include <vector> |
| 20 | #include <list> | ||
| 21 | #include <deque> | 32 | #include <deque> |
| 22 | #include <string> | 33 | #include <string> |
| 34 | #include <list> | ||
| 35 | #include <set> | ||
| 36 | #ifndef __SYMBIAN32__ | ||
| 37 | #if defined(IOS) || defined(MACGNUSTD) | ||
| 38 | #include <tr1/type_traits> | ||
| 39 | #else | ||
| 23 | #include <type_traits> | 40 | #include <type_traits> |
| 41 | #endif | ||
| 42 | #endif | ||
| 24 | 43 | ||
| 25 | #include "common.h" | 44 | #include "common.h" |
| 26 | #include "file_util.h" | 45 | #include "file_util.h" |
| 46 | //#include "../ext/snappy/snappy-c.h" | ||
| 47 | |||
| 48 | #if defined(IOS) || defined(MACGNUSTD) | ||
| 49 | namespace std { | ||
| 50 | using tr1::is_pointer; | ||
| 51 | } | ||
| 52 | #endif | ||
| 53 | #ifdef __SYMBIAN32__ | ||
| 54 | namespace std { | ||
| 55 | template <bool bool_value> | ||
| 56 | struct bool_constant { | ||
| 57 | typedef bool_constant<bool_value> type; | ||
| 58 | static const bool value = bool_value; | ||
| 59 | }; | ||
| 60 | template <bool bool_value> const bool bool_constant<bool_value>::value; | ||
| 61 | template <typename T> struct is_pointer : public bool_constant<false> {}; | ||
| 62 | template <typename T> struct is_pointer<T*> : public bool_constant<true> {}; | ||
| 63 | } | ||
| 64 | #endif | ||
| 27 | 65 | ||
| 28 | template <class T> | 66 | template <class T> |
| 29 | struct LinkedListItem : public T | 67 | struct LinkedListItem : public T |
| @@ -31,118 +69,505 @@ struct LinkedListItem : public T | |||
| 31 | LinkedListItem<T> *next; | 69 | LinkedListItem<T> *next; |
| 32 | }; | 70 | }; |
| 33 | 71 | ||
| 72 | class PointerWrap; | ||
| 73 | |||
| 74 | class PointerWrapSection | ||
| 75 | { | ||
| 76 | public: | ||
| 77 | PointerWrapSection(PointerWrap &p, int ver, const char *title) : p_(p), ver_(ver), title_(title) { | ||
| 78 | } | ||
| 79 | ~PointerWrapSection(); | ||
| 80 | |||
| 81 | bool operator == (const int &v) const { return ver_ == v; } | ||
| 82 | bool operator != (const int &v) const { return ver_ != v; } | ||
| 83 | bool operator <= (const int &v) const { return ver_ <= v; } | ||
| 84 | bool operator >= (const int &v) const { return ver_ >= v; } | ||
| 85 | bool operator < (const int &v) const { return ver_ < v; } | ||
| 86 | bool operator > (const int &v) const { return ver_ > v; } | ||
| 87 | |||
| 88 | operator bool() const { | ||
| 89 | return ver_ > 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | private: | ||
| 93 | PointerWrap &p_; | ||
| 94 | int ver_; | ||
| 95 | const char *title_; | ||
| 96 | }; | ||
| 97 | |||
| 34 | // Wrapper class | 98 | // Wrapper class |
| 35 | class PointerWrap | 99 | class PointerWrap |
| 36 | { | 100 | { |
| 37 | public: | 101 | // This makes it a compile error if you forget to define DoState() on non-POD. |
| 38 | enum Mode | 102 | // Which also can be a problem, for example struct tm is non-POD on linux, for whatever reason... |
| 103 | #ifdef _MSC_VER | ||
| 104 | template<typename T, bool isPOD = std::is_pod<T>::value, bool isPointer = std::is_pointer<T>::value> | ||
| 105 | #else | ||
| 106 | template<typename T, bool isPOD = __is_pod(T), bool isPointer = std::is_pointer<T>::value> | ||
| 107 | #endif | ||
| 108 | struct DoHelper | ||
| 39 | { | 109 | { |
| 110 | static void DoArray(PointerWrap *p, T *x, int count) | ||
| 111 | { | ||
| 112 | for (int i = 0; i < count; ++i) | ||
| 113 | p->Do(x[i]); | ||
| 114 | } | ||
| 115 | |||
| 116 | static void Do(PointerWrap *p, T &x) | ||
| 117 | { | ||
| 118 | p->DoClass(x); | ||
| 119 | } | ||
| 120 | }; | ||
| 121 | |||
| 122 | template<typename T> | ||
| 123 | struct DoHelper<T, true, false> | ||
| 124 | { | ||
| 125 | static void DoArray(PointerWrap *p, T *x, int count) | ||
| 126 | { | ||
| 127 | p->DoVoid((void *)x, sizeof(T) * count); | ||
| 128 | } | ||
| 129 | |||
| 130 | static void Do(PointerWrap *p, T &x) | ||
| 131 | { | ||
| 132 | p->DoVoid((void *)&x, sizeof(x)); | ||
| 133 | } | ||
| 134 | }; | ||
| 135 | |||
| 136 | public: | ||
| 137 | enum Mode { | ||
| 40 | MODE_READ = 1, // load | 138 | MODE_READ = 1, // load |
| 41 | MODE_WRITE, // save | 139 | MODE_WRITE, // save |
| 42 | MODE_MEASURE, // calculate size | 140 | MODE_MEASURE, // calculate size |
| 43 | MODE_VERIFY, // compare | 141 | MODE_VERIFY, // compare |
| 44 | }; | 142 | }; |
| 45 | 143 | ||
| 144 | enum Error { | ||
| 145 | ERROR_NONE = 0, | ||
| 146 | ERROR_WARNING = 1, | ||
| 147 | ERROR_FAILURE = 2, | ||
| 148 | }; | ||
| 149 | |||
| 46 | u8 **ptr; | 150 | u8 **ptr; |
| 47 | Mode mode; | 151 | Mode mode; |
| 152 | Error error; | ||
| 48 | 153 | ||
| 49 | public: | 154 | public: |
| 50 | PointerWrap(u8 **ptr_, Mode mode_) : ptr(ptr_), mode(mode_) {} | 155 | PointerWrap(u8 **ptr_, Mode mode_) : ptr(ptr_), mode(mode_), error(ERROR_NONE) {} |
| 156 | PointerWrap(unsigned char **ptr_, int mode_) : ptr((u8**)ptr_), mode((Mode)mode_), error(ERROR_NONE) {} | ||
| 157 | |||
| 158 | PointerWrapSection Section(const char *title, int ver) { | ||
| 159 | return Section(title, ver, ver); | ||
| 160 | } | ||
| 51 | 161 | ||
| 52 | void SetMode(Mode mode_) { mode = mode_; } | 162 | // The returned object can be compared against the version that was loaded. |
| 53 | Mode GetMode() const { return mode; } | 163 | // This can be used to support versions as old as minVer. |
| 54 | u8** GetPPtr() { return ptr; } | 164 | // Version = 0 means the section was not found. |
| 165 | PointerWrapSection Section(const char *title, int minVer, int ver) { | ||
| 166 | char marker[16] = {0}; | ||
| 167 | int foundVersion = ver; | ||
| 55 | 168 | ||
| 56 | template <typename K, class V> | 169 | strncpy(marker, title, sizeof(marker)); |
| 57 | void Do(std::map<K, V>& x) | 170 | if (!ExpectVoid(marker, sizeof(marker))) |
| 171 | { | ||
| 172 | // Might be before we added name markers for safety. | ||
| 173 | if (foundVersion == 1 && ExpectVoid(&foundVersion, sizeof(foundVersion))) | ||
| 174 | DoMarker(title); | ||
| 175 | // Wasn't found, but maybe we can still load the state. | ||
| 176 | else | ||
| 177 | foundVersion = 0; | ||
| 178 | } | ||
| 179 | else | ||
| 180 | Do(foundVersion); | ||
| 181 | |||
| 182 | if (error == ERROR_FAILURE || foundVersion < minVer || foundVersion > ver) { | ||
| 183 | WARN_LOG(COMMON, "Savestate failure: wrong version %d found for %s", foundVersion, title); | ||
| 184 | SetError(ERROR_FAILURE); | ||
| 185 | return PointerWrapSection(*this, -1, title); | ||
| 186 | } | ||
| 187 | return PointerWrapSection(*this, foundVersion, title); | ||
| 188 | } | ||
| 189 | |||
| 190 | void SetMode(Mode mode_) {mode = mode_;} | ||
| 191 | Mode GetMode() const {return mode;} | ||
| 192 | u8 **GetPPtr() {return ptr;} | ||
| 193 | void SetError(Error error_) | ||
| 58 | { | 194 | { |
| 59 | u32 count = (u32)x.size(); | 195 | if (error < error_) |
| 60 | Do(count); | 196 | error = error_; |
| 197 | if (error > ERROR_WARNING) | ||
| 198 | mode = PointerWrap::MODE_MEASURE; | ||
| 199 | } | ||
| 61 | 200 | ||
| 62 | switch (mode) | 201 | bool ExpectVoid(void *data, int size) |
| 202 | { | ||
| 203 | switch (mode) { | ||
| 204 | case MODE_READ: if (memcmp(data, *ptr, size) != 0) return false; break; | ||
| 205 | case MODE_WRITE: memcpy(*ptr, data, size); break; | ||
| 206 | case MODE_MEASURE: break; // MODE_MEASURE - don't need to do anything | ||
| 207 | case MODE_VERIFY: for(int i = 0; i < size; i++) _dbg_assert_msg_(COMMON, ((u8*)data)[i] == (*ptr)[i], "Savestate verification failure: %d (0x%X) (at %p) != %d (0x%X) (at %p).\n", ((u8*)data)[i], ((u8*)data)[i], &((u8*)data)[i], (*ptr)[i], (*ptr)[i], &(*ptr)[i]); break; | ||
| 208 | default: break; // throw an error? | ||
| 209 | } | ||
| 210 | (*ptr) += size; | ||
| 211 | return true; | ||
| 212 | } | ||
| 213 | |||
| 214 | void DoVoid(void *data, int size) | ||
| 215 | { | ||
| 216 | switch (mode) { | ||
| 217 | case MODE_READ: memcpy(data, *ptr, size); break; | ||
| 218 | case MODE_WRITE: memcpy(*ptr, data, size); break; | ||
| 219 | case MODE_MEASURE: break; // MODE_MEASURE - don't need to do anything | ||
| 220 | case MODE_VERIFY: for(int i = 0; i < size; i++) _dbg_assert_msg_(COMMON, ((u8*)data)[i] == (*ptr)[i], "Savestate verification failure: %d (0x%X) (at %p) != %d (0x%X) (at %p).\n", ((u8*)data)[i], ((u8*)data)[i], &((u8*)data)[i], (*ptr)[i], (*ptr)[i], &(*ptr)[i]); break; | ||
| 221 | default: break; // throw an error? | ||
| 222 | } | ||
| 223 | (*ptr) += size; | ||
| 224 | } | ||
| 225 | |||
| 226 | template<class K, class T> | ||
| 227 | void Do(std::map<K, T *> &x) | ||
| 228 | { | ||
| 229 | if (mode == MODE_READ) | ||
| 63 | { | 230 | { |
| 231 | for (auto it = x.begin(), end = x.end(); it != end; ++it) | ||
| 232 | { | ||
| 233 | if (it->second != NULL) | ||
| 234 | delete it->second; | ||
| 235 | } | ||
| 236 | } | ||
| 237 | T *dv = NULL; | ||
| 238 | DoMap(x, dv); | ||
| 239 | } | ||
| 240 | |||
| 241 | template<class K, class T> | ||
| 242 | void Do(std::map<K, T> &x) | ||
| 243 | { | ||
| 244 | T dv = T(); | ||
| 245 | DoMap(x, dv); | ||
| 246 | } | ||
| 247 | |||
| 248 | template<class K, class T> | ||
| 249 | void DoMap(std::map<K, T> &x, T &default_val) | ||
| 250 | { | ||
| 251 | unsigned int number = (unsigned int)x.size(); | ||
| 252 | Do(number); | ||
| 253 | switch (mode) { | ||
| 64 | case MODE_READ: | 254 | case MODE_READ: |
| 65 | for (x.clear(); count != 0; --count) | ||
| 66 | { | 255 | { |
| 67 | std::pair<K, V> pair; | 256 | x.clear(); |
| 68 | Do(pair.first); | 257 | while (number > 0) |
| 69 | Do(pair.second); | 258 | { |
| 70 | x.insert(pair); | 259 | K first = K(); |
| 260 | Do(first); | ||
| 261 | T second = default_val; | ||
| 262 | Do(second); | ||
| 263 | x[first] = second; | ||
| 264 | --number; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | break; | ||
| 268 | case MODE_WRITE: | ||
| 269 | case MODE_MEASURE: | ||
| 270 | case MODE_VERIFY: | ||
| 271 | { | ||
| 272 | typename std::map<K, T>::iterator itr = x.begin(); | ||
| 273 | while (number > 0) | ||
| 274 | { | ||
| 275 | K first = itr->first; | ||
| 276 | Do(first); | ||
| 277 | Do(itr->second); | ||
| 278 | --number; | ||
| 279 | ++itr; | ||
| 280 | } | ||
| 71 | } | 281 | } |
| 72 | break; | 282 | break; |
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | template<class K, class T> | ||
| 287 | void Do(std::multimap<K, T *> &x) | ||
| 288 | { | ||
| 289 | if (mode == MODE_READ) | ||
| 290 | { | ||
| 291 | for (auto it = x.begin(), end = x.end(); it != end; ++it) | ||
| 292 | { | ||
| 293 | if (it->second != NULL) | ||
| 294 | delete it->second; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | T *dv = NULL; | ||
| 298 | DoMultimap(x, dv); | ||
| 299 | } | ||
| 300 | |||
| 301 | template<class K, class T> | ||
| 302 | void Do(std::multimap<K, T> &x) | ||
| 303 | { | ||
| 304 | T dv = T(); | ||
| 305 | DoMultimap(x, dv); | ||
| 306 | } | ||
| 73 | 307 | ||
| 308 | template<class K, class T> | ||
| 309 | void DoMultimap(std::multimap<K, T> &x, T &default_val) | ||
| 310 | { | ||
| 311 | unsigned int number = (unsigned int)x.size(); | ||
| 312 | Do(number); | ||
| 313 | switch (mode) { | ||
| 314 | case MODE_READ: | ||
| 315 | { | ||
| 316 | x.clear(); | ||
| 317 | while (number > 0) | ||
| 318 | { | ||
| 319 | K first = K(); | ||
| 320 | Do(first); | ||
| 321 | T second = default_val; | ||
| 322 | Do(second); | ||
| 323 | x.insert(std::make_pair(first, second)); | ||
| 324 | --number; | ||
| 325 | } | ||
| 326 | } | ||
| 327 | break; | ||
| 74 | case MODE_WRITE: | 328 | case MODE_WRITE: |
| 75 | case MODE_MEASURE: | 329 | case MODE_MEASURE: |
| 76 | case MODE_VERIFY: | 330 | case MODE_VERIFY: |
| 77 | for (auto itr = x.begin(); itr != x.end(); ++itr) | ||
| 78 | { | 331 | { |
| 79 | Do(itr->first); | 332 | typename std::multimap<K, T>::iterator itr = x.begin(); |
| 80 | Do(itr->second); | 333 | while (number > 0) |
| 334 | { | ||
| 335 | Do(itr->first); | ||
| 336 | Do(itr->second); | ||
| 337 | --number; | ||
| 338 | ++itr; | ||
| 339 | } | ||
| 81 | } | 340 | } |
| 82 | break; | 341 | break; |
| 83 | } | 342 | } |
| 84 | } | 343 | } |
| 85 | 344 | ||
| 86 | template <typename T> | 345 | // Store vectors. |
| 87 | void DoContainer(T& x) | 346 | template<class T> |
| 347 | void Do(std::vector<T *> &x) | ||
| 88 | { | 348 | { |
| 89 | u32 size = (u32)x.size(); | 349 | T *dv = NULL; |
| 90 | Do(size); | 350 | DoVector(x, dv); |
| 91 | x.resize(size); | 351 | } |
| 92 | 352 | ||
| 93 | for (auto itr = x.begin(); itr != x.end(); ++itr) | 353 | template<class T> |
| 94 | Do(*itr); | 354 | void Do(std::vector<T> &x) |
| 355 | { | ||
| 356 | T dv = T(); | ||
| 357 | DoVector(x, dv); | ||
| 95 | } | 358 | } |
| 96 | 359 | ||
| 97 | template <typename T> | 360 | |
| 98 | void Do(std::vector<T>& x) | 361 | template<class T> |
| 362 | void DoPOD(std::vector<T> &x) | ||
| 99 | { | 363 | { |
| 100 | DoContainer(x); | 364 | T dv = T(); |
| 365 | DoVectorPOD(x, dv); | ||
| 101 | } | 366 | } |
| 102 | 367 | ||
| 103 | template <typename T> | 368 | template<class T> |
| 104 | void Do(std::list<T>& x) | 369 | void Do(std::vector<T> &x, T &default_val) |
| 105 | { | 370 | { |
| 106 | DoContainer(x); | 371 | DoVector(x, default_val); |
| 107 | } | 372 | } |
| 108 | 373 | ||
| 109 | template <typename T> | 374 | template<class T> |
| 110 | void Do(std::deque<T>& x) | 375 | void DoVector(std::vector<T> &x, T &default_val) |
| 111 | { | 376 | { |
| 112 | DoContainer(x); | 377 | u32 vec_size = (u32)x.size(); |
| 378 | Do(vec_size); | ||
| 379 | x.resize(vec_size, default_val); | ||
| 380 | if (vec_size > 0) | ||
| 381 | DoArray(&x[0], vec_size); | ||
| 113 | } | 382 | } |
| 114 | 383 | ||
| 115 | template <typename T> | 384 | template<class T> |
| 116 | void Do(std::basic_string<T>& x) | 385 | void DoVectorPOD(std::vector<T> &x, T &default_val) |
| 117 | { | 386 | { |
| 118 | DoContainer(x); | 387 | u32 vec_size = (u32)x.size(); |
| 388 | Do(vec_size); | ||
| 389 | x.resize(vec_size, default_val); | ||
| 390 | if (vec_size > 0) | ||
| 391 | DoArray(&x[0], vec_size); | ||
| 392 | } | ||
| 393 | |||
| 394 | // Store deques. | ||
| 395 | template<class T> | ||
| 396 | void Do(std::deque<T *> &x) | ||
| 397 | { | ||
| 398 | T *dv = NULL; | ||
| 399 | DoDeque(x, dv); | ||
| 400 | } | ||
| 401 | |||
| 402 | template<class T> | ||
| 403 | void Do(std::deque<T> &x) | ||
| 404 | { | ||
| 405 | T dv = T(); | ||
| 406 | DoDeque(x, dv); | ||
| 119 | } | 407 | } |
| 120 | 408 | ||
| 121 | template <typename T> | 409 | template<class T> |
| 122 | void DoArray(T* x, u32 count) | 410 | void DoDeque(std::deque<T> &x, T &default_val) |
| 123 | { | 411 | { |
| 124 | for (u32 i = 0; i != count; ++i) | 412 | u32 deq_size = (u32)x.size(); |
| 413 | Do(deq_size); | ||
| 414 | x.resize(deq_size, default_val); | ||
| 415 | u32 i; | ||
| 416 | for(i = 0; i < deq_size; i++) | ||
| 125 | Do(x[i]); | 417 | Do(x[i]); |
| 126 | } | 418 | } |
| 127 | 419 | ||
| 128 | template <typename T> | 420 | // Store STL lists. |
| 129 | void Do(T& x) | 421 | template<class T> |
| 422 | void Do(std::list<T *> &x) | ||
| 130 | { | 423 | { |
| 131 | // Ideally this would be std::is_trivially_copyable, but not enough support yet | 424 | T *dv = NULL; |
| 132 | static_assert(std::is_pod<T>::value, "Only sane for POD types"); | 425 | Do(x, dv); |
| 133 | |||
| 134 | DoVoid((void*)&x, sizeof(x)); | ||
| 135 | } | 426 | } |
| 136 | 427 | ||
| 137 | template <typename T> | 428 | template<class T> |
| 138 | void DoPOD(T& x) | 429 | void Do(std::list<T> &x) |
| 430 | { | ||
| 431 | T dv = T(); | ||
| 432 | DoList(x, dv); | ||
| 433 | } | ||
| 434 | |||
| 435 | template<class T> | ||
| 436 | void Do(std::list<T> &x, T &default_val) | ||
| 437 | { | ||
| 438 | DoList(x, default_val); | ||
| 439 | } | ||
| 440 | |||
| 441 | template<class T> | ||
| 442 | void DoList(std::list<T> &x, T &default_val) | ||
| 443 | { | ||
| 444 | u32 list_size = (u32)x.size(); | ||
| 445 | Do(list_size); | ||
| 446 | x.resize(list_size, default_val); | ||
| 447 | |||
| 448 | typename std::list<T>::iterator itr, end; | ||
| 449 | for (itr = x.begin(), end = x.end(); itr != end; ++itr) | ||
| 450 | Do(*itr); | ||
| 451 | } | ||
| 452 | |||
| 453 | |||
| 454 | // Store STL sets. | ||
| 455 | template <class T> | ||
| 456 | void Do(std::set<T *> &x) | ||
| 457 | { | ||
| 458 | if (mode == MODE_READ) | ||
| 459 | { | ||
| 460 | for (auto it = x.begin(), end = x.end(); it != end; ++it) | ||
| 461 | { | ||
| 462 | if (*it != NULL) | ||
| 463 | delete *it; | ||
| 464 | } | ||
| 465 | } | ||
| 466 | DoSet(x); | ||
| 467 | } | ||
| 468 | |||
| 469 | template <class T> | ||
| 470 | void Do(std::set<T> &x) | ||
| 471 | { | ||
| 472 | DoSet(x); | ||
| 473 | } | ||
| 474 | |||
| 475 | template <class T> | ||
| 476 | void DoSet(std::set<T> &x) | ||
| 477 | { | ||
| 478 | unsigned int number = (unsigned int)x.size(); | ||
| 479 | Do(number); | ||
| 480 | |||
| 481 | switch (mode) | ||
| 482 | { | ||
| 483 | case MODE_READ: | ||
| 484 | { | ||
| 485 | x.clear(); | ||
| 486 | while (number-- > 0) | ||
| 487 | { | ||
| 488 | T it = T(); | ||
| 489 | Do(it); | ||
| 490 | x.insert(it); | ||
| 491 | } | ||
| 492 | } | ||
| 493 | break; | ||
| 494 | case MODE_WRITE: | ||
| 495 | case MODE_MEASURE: | ||
| 496 | case MODE_VERIFY: | ||
| 497 | { | ||
| 498 | typename std::set<T>::iterator itr = x.begin(); | ||
| 499 | while (number-- > 0) | ||
| 500 | Do(*itr++); | ||
| 501 | } | ||
| 502 | break; | ||
| 503 | |||
| 504 | default: | ||
| 505 | ERROR_LOG(COMMON, "Savestate error: invalid mode %d.", mode); | ||
| 506 | } | ||
| 507 | } | ||
| 508 | |||
| 509 | // Store strings. | ||
| 510 | void Do(std::string &x) | ||
| 139 | { | 511 | { |
| 140 | DoVoid((void*)&x, sizeof(x)); | 512 | int stringLen = (int)x.length() + 1; |
| 513 | Do(stringLen); | ||
| 514 | |||
| 515 | switch (mode) { | ||
| 516 | case MODE_READ: x = (char*)*ptr; break; | ||
| 517 | case MODE_WRITE: memcpy(*ptr, x.c_str(), stringLen); break; | ||
| 518 | case MODE_MEASURE: break; | ||
| 519 | case MODE_VERIFY: _dbg_assert_msg_(COMMON, !strcmp(x.c_str(), (char*)*ptr), "Savestate verification failure: \"%s\" != \"%s\" (at %p).\n", x.c_str(), (char*)*ptr, ptr); break; | ||
| 520 | } | ||
| 521 | (*ptr) += stringLen; | ||
| 141 | } | 522 | } |
| 142 | 523 | ||
| 143 | template <typename T> | 524 | void Do(std::wstring &x) |
| 144 | void DoPointer(T*& x, T* const base) | ||
| 145 | { | 525 | { |
| 526 | int stringLen = sizeof(wchar_t)*((int)x.length() + 1); | ||
| 527 | Do(stringLen); | ||
| 528 | |||
| 529 | switch (mode) { | ||
| 530 | case MODE_READ: x = (wchar_t*)*ptr; break; | ||
| 531 | case MODE_WRITE: memcpy(*ptr, x.c_str(), stringLen); break; | ||
| 532 | case MODE_MEASURE: break; | ||
| 533 | case MODE_VERIFY: _dbg_assert_msg_(COMMON, x == (wchar_t*)*ptr, "Savestate verification failure: \"%ls\" != \"%ls\" (at %p).\n", x.c_str(), (wchar_t*)*ptr, ptr); break; | ||
| 534 | } | ||
| 535 | (*ptr) += stringLen; | ||
| 536 | } | ||
| 537 | |||
| 538 | template<class T> | ||
| 539 | void DoClass(T &x) { | ||
| 540 | x.DoState(*this); | ||
| 541 | } | ||
| 542 | |||
| 543 | template<class T> | ||
| 544 | void DoClass(T *&x) { | ||
| 545 | if (mode == MODE_READ) | ||
| 546 | { | ||
| 547 | if (x != NULL) | ||
| 548 | delete x; | ||
| 549 | x = new T(); | ||
| 550 | } | ||
| 551 | x->DoState(*this); | ||
| 552 | } | ||
| 553 | |||
| 554 | template<class T> | ||
| 555 | void DoArray(T *x, int count) { | ||
| 556 | DoHelper<T>::DoArray(this, x, count); | ||
| 557 | } | ||
| 558 | |||
| 559 | template<class T> | ||
| 560 | void Do(T &x) { | ||
| 561 | DoHelper<T>::Do(this, x); | ||
| 562 | } | ||
| 563 | |||
| 564 | template<class T> | ||
| 565 | void DoPOD(T &x) { | ||
| 566 | DoHelper<T>::Do(this, x); | ||
| 567 | } | ||
| 568 | |||
| 569 | template<class T> | ||
| 570 | void DoPointer(T* &x, T*const base) { | ||
| 146 | // pointers can be more than 2^31 apart, but you're using this function wrong if you need that much range | 571 | // pointers can be more than 2^31 apart, but you're using this function wrong if you need that much range |
| 147 | s32 offset = x - base; | 572 | s32 offset = x - base; |
| 148 | Do(offset); | 573 | Do(offset); |
| @@ -150,8 +575,7 @@ public: | |||
| 150 | x = base + offset; | 575 | x = base + offset; |
| 151 | } | 576 | } |
| 152 | 577 | ||
| 153 | // Let's pretend std::list doesn't exist! | 578 | template<class T, LinkedListItem<T>* (*TNew)(), void (*TFree)(LinkedListItem<T>*), void (*TDo)(PointerWrap&, T*)> |
| 154 | template <class T, LinkedListItem<T>* (*TNew)(), void (*TFree)(LinkedListItem<T>*), void (*TDo)(PointerWrap&, T*)> | ||
| 155 | void DoLinkedList(LinkedListItem<T>*& list_start, LinkedListItem<T>** list_end=0) | 579 | void DoLinkedList(LinkedListItem<T>*& list_start, LinkedListItem<T>** list_end=0) |
| 156 | { | 580 | { |
| 157 | LinkedListItem<T>* list_cur = list_start; | 581 | LinkedListItem<T>* list_cur = list_start; |
| @@ -211,66 +635,48 @@ public: | |||
| 211 | } | 635 | } |
| 212 | } | 636 | } |
| 213 | 637 | ||
| 214 | void DoMarker(const char* prevName, u32 arbitraryNumber = 0x42) | 638 | void DoMarker(const char* prevName, u32 arbitraryNumber=0x42) |
| 215 | { | 639 | { |
| 216 | u32 cookie = arbitraryNumber; | 640 | u32 cookie = arbitraryNumber; |
| 217 | Do(cookie); | 641 | Do(cookie); |
| 218 | 642 | if(mode == PointerWrap::MODE_READ && cookie != arbitraryNumber) | |
| 219 | if (mode == PointerWrap::MODE_READ && cookie != arbitraryNumber) | ||
| 220 | { | 643 | { |
| 221 | PanicAlertT("Error: After \"%s\", found %d (0x%X) instead of save marker %d (0x%X). Aborting savestate load...", | 644 | PanicAlertT("Error: After \"%s\", found %d (0x%X) instead of save marker %d (0x%X). Aborting savestate load...", prevName, cookie, cookie, arbitraryNumber, arbitraryNumber); |
| 222 | prevName, cookie, cookie, arbitraryNumber, arbitraryNumber); | 645 | SetError(ERROR_FAILURE); |
| 223 | mode = PointerWrap::MODE_MEASURE; | ||
| 224 | } | 646 | } |
| 225 | } | 647 | } |
| 648 | }; | ||
| 226 | 649 | ||
| 227 | private: | 650 | inline PointerWrapSection::~PointerWrapSection() { |
| 228 | __forceinline void DoByte(u8& x) | 651 | if (ver_ > 0) { |
| 229 | { | 652 | p_.DoMarker(title_); |
| 230 | switch (mode) | ||
| 231 | { | ||
| 232 | case MODE_READ: | ||
| 233 | x = **ptr; | ||
| 234 | break; | ||
| 235 | |||
| 236 | case MODE_WRITE: | ||
| 237 | **ptr = x; | ||
| 238 | break; | ||
| 239 | |||
| 240 | case MODE_MEASURE: | ||
| 241 | break; | ||
| 242 | |||
| 243 | case MODE_VERIFY: | ||
| 244 | _dbg_assert_msg_(COMMON, (x == **ptr), | ||
| 245 | "Savestate verification failure: %d (0x%X) (at %p) != %d (0x%X) (at %p).\n", | ||
| 246 | x, x, &x, **ptr, **ptr, *ptr); | ||
| 247 | break; | ||
| 248 | |||
| 249 | default: | ||
| 250 | break; | ||
| 251 | } | ||
| 252 | |||
| 253 | ++(*ptr); | ||
| 254 | } | 653 | } |
| 654 | } | ||
| 255 | 655 | ||
| 256 | void DoVoid(void *data, u32 size) | ||
| 257 | { | ||
| 258 | for(u32 i = 0; i != size; ++i) | ||
| 259 | DoByte(reinterpret_cast<u8*>(data)[i]); | ||
| 260 | } | ||
| 261 | }; | ||
| 262 | 656 | ||
| 263 | class CChunkFileReader | 657 | class CChunkFileReader |
| 264 | { | 658 | { |
| 265 | public: | 659 | public: |
| 660 | enum Error { | ||
| 661 | ERROR_NONE, | ||
| 662 | ERROR_BAD_FILE, | ||
| 663 | ERROR_BROKEN_STATE, | ||
| 664 | }; | ||
| 665 | |||
| 266 | // Load file template | 666 | // Load file template |
| 267 | template<class T> | 667 | template<class T> |
| 268 | static bool Load(const std::string& _rFilename, u32 _Revision, T& _class) | 668 | static Error Load(const std::string& _rFilename, int _Revision, const char *_VersionString, T& _class, std::string* _failureReason) |
| 269 | { | 669 | { |
| 270 | INFO_LOG(COMMON, "ChunkReader: Loading %s" , _rFilename.c_str()); | 670 | INFO_LOG(COMMON, "ChunkReader: Loading %s" , _rFilename.c_str()); |
| 271 | 671 | _failureReason->clear(); | |
| 272 | if (!File::Exists(_rFilename)) | 672 | _failureReason->append("LoadStateWrongVersion"); |
| 273 | return false; | 673 | |
| 674 | if (!File::Exists(_rFilename)) { | ||
| 675 | _failureReason->clear(); | ||
| 676 | _failureReason->append("LoadStateDoesntExist"); | ||
| 677 | ERROR_LOG(COMMON, "ChunkReader: File doesn't exist"); | ||
| 678 | return ERROR_BAD_FILE; | ||
| 679 | } | ||
| 274 | 680 | ||
| 275 | // Check file size | 681 | // Check file size |
| 276 | const u64 fileSize = File::GetSize(_rFilename); | 682 | const u64 fileSize = File::GetSize(_rFilename); |
| @@ -278,14 +684,14 @@ public: | |||
| 278 | if (fileSize < headerSize) | 684 | if (fileSize < headerSize) |
| 279 | { | 685 | { |
| 280 | ERROR_LOG(COMMON,"ChunkReader: File too small"); | 686 | ERROR_LOG(COMMON,"ChunkReader: File too small"); |
| 281 | return false; | 687 | return ERROR_BAD_FILE; |
| 282 | } | 688 | } |
| 283 | 689 | ||
| 284 | File::IOFile pFile(_rFilename, "rb"); | 690 | File::IOFile pFile(_rFilename, "rb"); |
| 285 | if (!pFile) | 691 | if (!pFile) |
| 286 | { | 692 | { |
| 287 | ERROR_LOG(COMMON,"ChunkReader: Can't open file for reading"); | 693 | ERROR_LOG(COMMON,"ChunkReader: Can't open file for reading"); |
| 288 | return false; | 694 | return ERROR_BAD_FILE; |
| 289 | } | 695 | } |
| 290 | 696 | ||
| 291 | // read the header | 697 | // read the header |
| @@ -293,91 +699,175 @@ public: | |||
| 293 | if (!pFile.ReadArray(&header, 1)) | 699 | if (!pFile.ReadArray(&header, 1)) |
| 294 | { | 700 | { |
| 295 | ERROR_LOG(COMMON,"ChunkReader: Bad header size"); | 701 | ERROR_LOG(COMMON,"ChunkReader: Bad header size"); |
| 296 | return false; | 702 | return ERROR_BAD_FILE; |
| 297 | } | 703 | } |
| 298 | 704 | ||
| 299 | // Check revision | 705 | // Check revision |
| 300 | if (header.Revision != _Revision) | 706 | if (header.Revision != _Revision) |
| 301 | { | 707 | { |
| 302 | ERROR_LOG(COMMON,"ChunkReader: Wrong file revision, got %d expected %d", | 708 | ERROR_LOG(COMMON,"ChunkReader: Wrong file revision, got %d expected %d", |
| 303 | header.Revision, _Revision); | 709 | header.Revision, _Revision); |
| 304 | return false; | 710 | return ERROR_BAD_FILE; |
| 711 | } | ||
| 712 | |||
| 713 | if (strcmp(header.GitVersion, _VersionString) != 0) | ||
| 714 | { | ||
| 715 | WARN_LOG(COMMON, "This savestate was generated by a different version of PPSSPP, %s. It may not load properly.", | ||
| 716 | header.GitVersion); | ||
| 305 | } | 717 | } |
| 306 | 718 | ||
| 307 | // get size | 719 | // get size |
| 308 | const u32 sz = (u32)(fileSize - headerSize); | 720 | const int sz = (int)(fileSize - headerSize); |
| 309 | if (header.ExpectedSize != sz) | 721 | if (header.ExpectedSize != sz) |
| 310 | { | 722 | { |
| 311 | ERROR_LOG(COMMON,"ChunkReader: Bad file size, got %d expected %d", | 723 | ERROR_LOG(COMMON,"ChunkReader: Bad file size, got %d expected %d", |
| 312 | sz, header.ExpectedSize); | 724 | sz, header.ExpectedSize); |
| 313 | return false; | 725 | return ERROR_BAD_FILE; |
| 314 | } | 726 | } |
| 315 | 727 | ||
| 316 | // read the state | 728 | // read the state |
| 317 | std::vector<u8> buffer(sz); | 729 | u8* buffer = new u8[sz]; |
| 318 | if (!pFile.ReadArray(&buffer[0], sz)) | 730 | if (!pFile.ReadBytes(buffer, sz)) |
| 319 | { | 731 | { |
| 320 | ERROR_LOG(COMMON,"ChunkReader: Error reading file"); | 732 | ERROR_LOG(COMMON,"ChunkReader: Error reading file"); |
| 321 | return false; | 733 | return ERROR_BAD_FILE; |
| 734 | } | ||
| 735 | |||
| 736 | u8 *ptr = buffer; | ||
| 737 | u8 *buf = buffer; | ||
| 738 | if (header.Compress) { | ||
| 739 | u8 *uncomp_buffer = new u8[header.UncompressedSize]; | ||
| 740 | size_t uncomp_size = header.UncompressedSize; | ||
| 741 | snappy_uncompress((const char *)buffer, sz, (char *)uncomp_buffer, &uncomp_size); | ||
| 742 | if ((int)uncomp_size != header.UncompressedSize) { | ||
| 743 | ERROR_LOG(COMMON,"Size mismatch: file: %i calc: %i", (int)header.UncompressedSize, (int)uncomp_size); | ||
| 744 | } | ||
| 745 | ptr = uncomp_buffer; | ||
| 746 | buf = uncomp_buffer; | ||
| 747 | delete [] buffer; | ||
| 322 | } | 748 | } |
| 323 | 749 | ||
| 324 | u8* ptr = &buffer[0]; | ||
| 325 | PointerWrap p(&ptr, PointerWrap::MODE_READ); | 750 | PointerWrap p(&ptr, PointerWrap::MODE_READ); |
| 326 | _class.DoState(p); | 751 | _class.DoState(p); |
| 752 | delete[] buf; | ||
| 327 | 753 | ||
| 328 | INFO_LOG(COMMON, "ChunkReader: Done loading %s" , _rFilename.c_str()); | 754 | INFO_LOG(COMMON, "ChunkReader: Done loading %s" , _rFilename.c_str()); |
| 329 | return true; | 755 | if (p.error != p.ERROR_FAILURE) { |
| 756 | return ERROR_NONE; | ||
| 757 | } else { | ||
| 758 | return ERROR_BROKEN_STATE; | ||
| 759 | } | ||
| 330 | } | 760 | } |
| 331 | 761 | ||
| 332 | // Save file template | 762 | // Save file template |
| 333 | template<class T> | 763 | template<class T> |
| 334 | static bool Save(const std::string& _rFilename, u32 _Revision, T& _class) | 764 | static Error Save(const std::string& _rFilename, int _Revision, const char *_VersionString, T& _class) |
| 335 | { | 765 | { |
| 336 | INFO_LOG(COMMON, "ChunkReader: Writing %s" , _rFilename.c_str()); | 766 | INFO_LOG(COMMON, "ChunkReader: Writing %s" , _rFilename.c_str()); |
| 767 | |||
| 337 | File::IOFile pFile(_rFilename, "wb"); | 768 | File::IOFile pFile(_rFilename, "wb"); |
| 338 | if (!pFile) | 769 | if (!pFile) |
| 339 | { | 770 | { |
| 340 | ERROR_LOG(COMMON,"ChunkReader: Error opening file for write"); | 771 | ERROR_LOG(COMMON,"ChunkReader: Error opening file for write"); |
| 341 | return false; | 772 | return ERROR_BAD_FILE; |
| 342 | } | 773 | } |
| 343 | 774 | ||
| 775 | bool compress = true; | ||
| 776 | |||
| 344 | // Get data | 777 | // Get data |
| 345 | u8 *ptr = 0; | 778 | u8 *ptr = 0; |
| 346 | PointerWrap p(&ptr, PointerWrap::MODE_MEASURE); | 779 | PointerWrap p(&ptr, PointerWrap::MODE_MEASURE); |
| 347 | _class.DoState(p); | 780 | _class.DoState(p); |
| 348 | size_t const sz = (size_t)ptr; | 781 | size_t const sz = (size_t)ptr; |
| 349 | std::vector<u8> buffer(sz); | 782 | |
| 783 | u8 * buffer = new u8[sz]; | ||
| 350 | ptr = &buffer[0]; | 784 | ptr = &buffer[0]; |
| 351 | p.SetMode(PointerWrap::MODE_WRITE); | 785 | p.SetMode(PointerWrap::MODE_WRITE); |
| 352 | _class.DoState(p); | 786 | _class.DoState(p); |
| 353 | 787 | ||
| 354 | // Create header | 788 | // Create header |
| 355 | SChunkHeader header; | 789 | SChunkHeader header; |
| 790 | header.Compress = compress ? 1 : 0; | ||
| 356 | header.Revision = _Revision; | 791 | header.Revision = _Revision; |
| 357 | header.ExpectedSize = (u32)sz; | 792 | header.ExpectedSize = (int)sz; |
| 793 | header.UncompressedSize = (int)sz; | ||
| 794 | strncpy(header.GitVersion, _VersionString, 32); | ||
| 795 | header.GitVersion[31] = '\0'; | ||
| 358 | 796 | ||
| 359 | // Write to file | 797 | // Write to file |
| 360 | if (!pFile.WriteArray(&header, 1)) | 798 | if (compress) { |
| 361 | { | 799 | size_t comp_len = snappy_max_compressed_length(sz); |
| 362 | ERROR_LOG(COMMON,"ChunkReader: Failed writing header"); | 800 | u8 *compressed_buffer = new u8[comp_len]; |
| 363 | return false; | 801 | snappy_compress((const char *)buffer, sz, (char *)compressed_buffer, &comp_len); |
| 802 | delete [] buffer; | ||
| 803 | header.ExpectedSize = (int)comp_len; | ||
| 804 | if (!pFile.WriteArray(&header, 1)) | ||
| 805 | { | ||
| 806 | ERROR_LOG(COMMON,"ChunkReader: Failed writing header"); | ||
| 807 | return ERROR_BAD_FILE; | ||
| 808 | } | ||
| 809 | if (!pFile.WriteBytes(&compressed_buffer[0], comp_len)) { | ||
| 810 | ERROR_LOG(COMMON,"ChunkReader: Failed writing compressed data"); | ||
| 811 | return ERROR_BAD_FILE; | ||
| 812 | } else { | ||
| 813 | INFO_LOG(COMMON, "Savestate: Compressed %i bytes into %i", (int)sz, (int)comp_len); | ||
| 814 | } | ||
| 815 | delete [] compressed_buffer; | ||
| 816 | } else { | ||
| 817 | if (!pFile.WriteArray(&header, 1)) | ||
| 818 | { | ||
| 819 | ERROR_LOG(COMMON,"ChunkReader: Failed writing header"); | ||
| 820 | return ERROR_BAD_FILE; | ||
| 821 | } | ||
| 822 | if (!pFile.WriteBytes(&buffer[0], sz)) | ||
| 823 | { | ||
| 824 | ERROR_LOG(COMMON,"ChunkReader: Failed writing data"); | ||
| 825 | return ERROR_BAD_FILE; | ||
| 826 | } | ||
| 827 | delete [] buffer; | ||
| 364 | } | 828 | } |
| 365 | 829 | ||
| 366 | if (!pFile.WriteArray(&buffer[0], sz)) | 830 | INFO_LOG(COMMON,"ChunkReader: Done writing %s", |
| 367 | { | 831 | _rFilename.c_str()); |
| 368 | ERROR_LOG(COMMON,"ChunkReader: Failed writing data"); | 832 | if (p.error != p.ERROR_FAILURE) { |
| 369 | return false; | 833 | return ERROR_NONE; |
| 834 | } else { | ||
| 835 | return ERROR_BROKEN_STATE; | ||
| 370 | } | 836 | } |
| 837 | } | ||
| 838 | |||
| 839 | template <class T> | ||
| 840 | static Error Verify(T& _class) | ||
| 841 | { | ||
| 842 | u8 *ptr = 0; | ||
| 371 | 843 | ||
| 372 | INFO_LOG(COMMON,"ChunkReader: Done writing %s", _rFilename.c_str()); | 844 | // Step 1: Measure the space required. |
| 373 | return true; | 845 | PointerWrap p(&ptr, PointerWrap::MODE_MEASURE); |
| 846 | _class.DoState(p); | ||
| 847 | size_t const sz = (size_t)ptr; | ||
| 848 | std::vector<u8> buffer(sz); | ||
| 849 | |||
| 850 | // Step 2: Dump the state. | ||
| 851 | ptr = &buffer[0]; | ||
| 852 | p.SetMode(PointerWrap::MODE_WRITE); | ||
| 853 | _class.DoState(p); | ||
| 854 | |||
| 855 | // Step 3: Verify the state. | ||
| 856 | ptr = &buffer[0]; | ||
| 857 | p.SetMode(PointerWrap::MODE_VERIFY); | ||
| 858 | _class.DoState(p); | ||
| 859 | |||
| 860 | return ERROR_NONE; | ||
| 374 | } | 861 | } |
| 375 | 862 | ||
| 376 | private: | 863 | private: |
| 377 | struct SChunkHeader | 864 | struct SChunkHeader |
| 378 | { | 865 | { |
| 379 | u32 Revision; | 866 | int Revision; |
| 380 | u32 ExpectedSize; | 867 | int Compress; |
| 868 | int ExpectedSize; | ||
| 869 | int UncompressedSize; | ||
| 870 | char GitVersion[32]; | ||
| 381 | }; | 871 | }; |
| 382 | }; | 872 | }; |
| 383 | 873 | ||