chunk_file.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // Copyright (C) 2003 Dolphin Project.
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, version 2.0 or later versions.
  5. // This program is distributed in the hope that it will be useful,
  6. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. // GNU General Public License 2.0 for more details.
  9. // A copy of the GPL 2.0 should have been included with the program.
  10. // If not, see http://www.gnu.org/licenses/
  11. // Official SVN repository and contact information can be found at
  12. // http://code.google.com/p/dolphin-emu/
  13. #pragma once
  14. // Extremely simple serialization framework.
  15. // (mis)-features:
  16. // + Super fast
  17. // + Very simple
  18. // + Same code is used for serialization and deserializaition (in most cases)
  19. // - Zero backwards/forwards compatibility
  20. // - Serialization code for anything complex has to be manually written.
  21. #include <map>
  22. #include <vector>
  23. #include <deque>
  24. #include <string>
  25. #include <list>
  26. #include <set>
  27. #include <type_traits>
  28. #include "common/common.h"
  29. #include "common/file_util.h"
  30. template <class T>
  31. struct LinkedListItem : public T
  32. {
  33. LinkedListItem<T> *next;
  34. };
  35. class PointerWrap;
  36. class PointerWrapSection
  37. {
  38. public:
  39. PointerWrapSection(PointerWrap &p, int ver, const char *title) : p_(p), ver_(ver), title_(title) {
  40. }
  41. ~PointerWrapSection();
  42. bool operator == (const int &v) const { return ver_ == v; }
  43. bool operator != (const int &v) const { return ver_ != v; }
  44. bool operator <= (const int &v) const { return ver_ <= v; }
  45. bool operator >= (const int &v) const { return ver_ >= v; }
  46. bool operator < (const int &v) const { return ver_ < v; }
  47. bool operator > (const int &v) const { return ver_ > v; }
  48. operator bool() const {
  49. return ver_ > 0;
  50. }
  51. private:
  52. PointerWrap &p_;
  53. int ver_;
  54. const char *title_;
  55. };
  56. // Wrapper class
  57. class PointerWrap
  58. {
  59. // This makes it a compile error if you forget to define DoState() on non-POD.
  60. // Which also can be a problem, for example struct tm is non-POD on linux, for whatever reason...
  61. #ifdef _MSC_VER
  62. template<typename T, bool isPOD = std::is_pod<T>::value, bool isPointer = std::is_pointer<T>::value>
  63. #else
  64. template<typename T, bool isPOD = __is_pod(T), bool isPointer = std::is_pointer<T>::value>
  65. #endif
  66. struct DoHelper
  67. {
  68. static void DoArray(PointerWrap *p, T *x, int count)
  69. {
  70. for (int i = 0; i < count; ++i)
  71. p->Do(x[i]);
  72. }
  73. static void Do(PointerWrap *p, T &x)
  74. {
  75. p->DoClass(x);
  76. }
  77. };
  78. template<typename T>
  79. struct DoHelper<T, true, false>
  80. {
  81. static void DoArray(PointerWrap *p, T *x, int count)
  82. {
  83. p->DoVoid((void *)x, sizeof(T) * count);
  84. }
  85. static void Do(PointerWrap *p, T &x)
  86. {
  87. p->DoVoid((void *)&x, sizeof(x));
  88. }
  89. };
  90. public:
  91. enum Mode {
  92. MODE_READ = 1, // load
  93. MODE_WRITE, // save
  94. MODE_MEASURE, // calculate size
  95. MODE_VERIFY, // compare
  96. };
  97. enum Error {
  98. ERROR_NONE = 0,
  99. ERROR_WARNING = 1,
  100. ERROR_FAILURE = 2,
  101. };
  102. u8 **ptr;
  103. Mode mode;
  104. Error error;
  105. public:
  106. PointerWrap(u8 **ptr_, Mode mode_) : ptr(ptr_), mode(mode_), error(ERROR_NONE) {}
  107. PointerWrap(unsigned char **ptr_, int mode_) : ptr((u8**)ptr_), mode((Mode)mode_), error(ERROR_NONE) {}
  108. PointerWrapSection Section(const char *title, int ver) {
  109. return Section(title, ver, ver);
  110. }
  111. // The returned object can be compared against the version that was loaded.
  112. // This can be used to support versions as old as minVer.
  113. // Version = 0 means the section was not found.
  114. PointerWrapSection Section(const char *title, int minVer, int ver) {
  115. char marker[16] = {0};
  116. int foundVersion = ver;
  117. strncpy(marker, title, sizeof(marker));
  118. if (!ExpectVoid(marker, sizeof(marker)))
  119. {
  120. // Might be before we added name markers for safety.
  121. if (foundVersion == 1 && ExpectVoid(&foundVersion, sizeof(foundVersion)))
  122. DoMarker(title);
  123. // Wasn't found, but maybe we can still load the state.
  124. else
  125. foundVersion = 0;
  126. }
  127. else
  128. Do(foundVersion);
  129. if (error == ERROR_FAILURE || foundVersion < minVer || foundVersion > ver) {
  130. WARN_LOG(COMMON, "Savestate failure: wrong version %d found for %s", foundVersion, title);
  131. SetError(ERROR_FAILURE);
  132. return PointerWrapSection(*this, -1, title);
  133. }
  134. return PointerWrapSection(*this, foundVersion, title);
  135. }
  136. void SetMode(Mode mode_) {mode = mode_;}
  137. Mode GetMode() const {return mode;}
  138. u8 **GetPPtr() {return ptr;}
  139. void SetError(Error error_)
  140. {
  141. if (error < error_)
  142. error = error_;
  143. if (error > ERROR_WARNING)
  144. mode = PointerWrap::MODE_MEASURE;
  145. }
  146. bool ExpectVoid(void *data, int size)
  147. {
  148. switch (mode) {
  149. case MODE_READ: if (memcmp(data, *ptr, size) != 0) return false; break;
  150. case MODE_WRITE: memcpy(*ptr, data, size); break;
  151. case MODE_MEASURE: break; // MODE_MEASURE - don't need to do anything
  152. 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;
  153. default: break; // throw an error?
  154. }
  155. (*ptr) += size;
  156. return true;
  157. }
  158. void DoVoid(void *data, int size)
  159. {
  160. switch (mode) {
  161. case MODE_READ: memcpy(data, *ptr, size); break;
  162. case MODE_WRITE: memcpy(*ptr, data, size); break;
  163. case MODE_MEASURE: break; // MODE_MEASURE - don't need to do anything
  164. 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;
  165. default: break; // throw an error?
  166. }
  167. (*ptr) += size;
  168. }
  169. template<class K, class T>
  170. void Do(std::map<K, T *> &x)
  171. {
  172. if (mode == MODE_READ)
  173. {
  174. for (auto it = x.begin(), end = x.end(); it != end; ++it)
  175. {
  176. if (it->second != nullptr)
  177. delete it->second;
  178. }
  179. }
  180. T *dv = nullptr;
  181. DoMap(x, dv);
  182. }
  183. template<class K, class T>
  184. void Do(std::map<K, T> &x)
  185. {
  186. T dv = T();
  187. DoMap(x, dv);
  188. }
  189. template<class K, class T>
  190. void DoMap(std::map<K, T> &x, T &default_val)
  191. {
  192. unsigned int number = (unsigned int)x.size();
  193. Do(number);
  194. switch (mode) {
  195. case MODE_READ:
  196. {
  197. x.clear();
  198. while (number > 0)
  199. {
  200. K first = K();
  201. Do(first);
  202. T second = default_val;
  203. Do(second);
  204. x[first] = second;
  205. --number;
  206. }
  207. }
  208. break;
  209. case MODE_WRITE:
  210. case MODE_MEASURE:
  211. case MODE_VERIFY:
  212. {
  213. typename std::map<K, T>::iterator itr = x.begin();
  214. while (number > 0)
  215. {
  216. K first = itr->first;
  217. Do(first);
  218. Do(itr->second);
  219. --number;
  220. ++itr;
  221. }
  222. }
  223. break;
  224. }
  225. }
  226. template<class K, class T>
  227. void Do(std::multimap<K, T *> &x)
  228. {
  229. if (mode == MODE_READ)
  230. {
  231. for (auto it = x.begin(), end = x.end(); it != end; ++it)
  232. {
  233. if (it->second != nullptr)
  234. delete it->second;
  235. }
  236. }
  237. T *dv = nullptr;
  238. DoMultimap(x, dv);
  239. }
  240. template<class K, class T>
  241. void Do(std::multimap<K, T> &x)
  242. {
  243. T dv = T();
  244. DoMultimap(x, dv);
  245. }
  246. template<class K, class T>
  247. void DoMultimap(std::multimap<K, T> &x, T &default_val)
  248. {
  249. unsigned int number = (unsigned int)x.size();
  250. Do(number);
  251. switch (mode) {
  252. case MODE_READ:
  253. {
  254. x.clear();
  255. while (number > 0)
  256. {
  257. K first = K();
  258. Do(first);
  259. T second = default_val;
  260. Do(second);
  261. x.insert(std::make_pair(first, second));
  262. --number;
  263. }
  264. }
  265. break;
  266. case MODE_WRITE:
  267. case MODE_MEASURE:
  268. case MODE_VERIFY:
  269. {
  270. typename std::multimap<K, T>::iterator itr = x.begin();
  271. while (number > 0)
  272. {
  273. Do(itr->first);
  274. Do(itr->second);
  275. --number;
  276. ++itr;
  277. }
  278. }
  279. break;
  280. }
  281. }
  282. // Store vectors.
  283. template<class T>
  284. void Do(std::vector<T *> &x)
  285. {
  286. T *dv = nullptr;
  287. DoVector(x, dv);
  288. }
  289. template<class T>
  290. void Do(std::vector<T> &x)
  291. {
  292. T dv = T();
  293. DoVector(x, dv);
  294. }
  295. template<class T>
  296. void DoPOD(std::vector<T> &x)
  297. {
  298. T dv = T();
  299. DoVectorPOD(x, dv);
  300. }
  301. template<class T>
  302. void Do(std::vector<T> &x, T &default_val)
  303. {
  304. DoVector(x, default_val);
  305. }
  306. template<class T>
  307. void DoVector(std::vector<T> &x, T &default_val)
  308. {
  309. u32 vec_size = (u32)x.size();
  310. Do(vec_size);
  311. x.resize(vec_size, default_val);
  312. if (vec_size > 0)
  313. DoArray(&x[0], vec_size);
  314. }
  315. template<class T>
  316. void DoVectorPOD(std::vector<T> &x, T &default_val)
  317. {
  318. u32 vec_size = (u32)x.size();
  319. Do(vec_size);
  320. x.resize(vec_size, default_val);
  321. if (vec_size > 0)
  322. DoArray(&x[0], vec_size);
  323. }
  324. // Store deques.
  325. template<class T>
  326. void Do(std::deque<T *> &x)
  327. {
  328. T *dv = nullptr;
  329. DoDeque(x, dv);
  330. }
  331. template<class T>
  332. void Do(std::deque<T> &x)
  333. {
  334. T dv = T();
  335. DoDeque(x, dv);
  336. }
  337. template<class T>
  338. void DoDeque(std::deque<T> &x, T &default_val)
  339. {
  340. u32 deq_size = (u32)x.size();
  341. Do(deq_size);
  342. x.resize(deq_size, default_val);
  343. u32 i;
  344. for(i = 0; i < deq_size; i++)
  345. Do(x[i]);
  346. }
  347. // Store STL lists.
  348. template<class T>
  349. void Do(std::list<T *> &x)
  350. {
  351. T *dv = nullptr;
  352. Do(x, dv);
  353. }
  354. template<class T>
  355. void Do(std::list<T> &x)
  356. {
  357. T dv = T();
  358. DoList(x, dv);
  359. }
  360. template<class T>
  361. void Do(std::list<T> &x, T &default_val)
  362. {
  363. DoList(x, default_val);
  364. }
  365. template<class T>
  366. void DoList(std::list<T> &x, T &default_val)
  367. {
  368. u32 list_size = (u32)x.size();
  369. Do(list_size);
  370. x.resize(list_size, default_val);
  371. typename std::list<T>::iterator itr, end;
  372. for (itr = x.begin(), end = x.end(); itr != end; ++itr)
  373. Do(*itr);
  374. }
  375. // Store STL sets.
  376. template <class T>
  377. void Do(std::set<T *> &x)
  378. {
  379. if (mode == MODE_READ)
  380. {
  381. for (auto it = x.begin(), end = x.end(); it != end; ++it)
  382. {
  383. if (*it != nullptr)
  384. delete *it;
  385. }
  386. }
  387. DoSet(x);
  388. }
  389. template <class T>
  390. void Do(std::set<T> &x)
  391. {
  392. DoSet(x);
  393. }
  394. template <class T>
  395. void DoSet(std::set<T> &x)
  396. {
  397. unsigned int number = (unsigned int)x.size();
  398. Do(number);
  399. switch (mode)
  400. {
  401. case MODE_READ:
  402. {
  403. x.clear();
  404. while (number-- > 0)
  405. {
  406. T it = T();
  407. Do(it);
  408. x.insert(it);
  409. }
  410. }
  411. break;
  412. case MODE_WRITE:
  413. case MODE_MEASURE:
  414. case MODE_VERIFY:
  415. {
  416. typename std::set<T>::iterator itr = x.begin();
  417. while (number-- > 0)
  418. Do(*itr++);
  419. }
  420. break;
  421. default:
  422. ERROR_LOG(COMMON, "Savestate error: invalid mode %d.", mode);
  423. }
  424. }
  425. // Store strings.
  426. void Do(std::string &x)
  427. {
  428. int stringLen = (int)x.length() + 1;
  429. Do(stringLen);
  430. switch (mode) {
  431. case MODE_READ: x = (char*)*ptr; break;
  432. case MODE_WRITE: memcpy(*ptr, x.c_str(), stringLen); break;
  433. case MODE_MEASURE: break;
  434. 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;
  435. }
  436. (*ptr) += stringLen;
  437. }
  438. void Do(std::wstring &x)
  439. {
  440. int stringLen = sizeof(wchar_t)*((int)x.length() + 1);
  441. Do(stringLen);
  442. switch (mode) {
  443. case MODE_READ: x = (wchar_t*)*ptr; break;
  444. case MODE_WRITE: memcpy(*ptr, x.c_str(), stringLen); break;
  445. case MODE_MEASURE: break;
  446. 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;
  447. }
  448. (*ptr) += stringLen;
  449. }
  450. template<class T>
  451. void DoClass(T &x) {
  452. x.DoState(*this);
  453. }
  454. template<class T>
  455. void DoClass(T *&x) {
  456. if (mode == MODE_READ)
  457. {
  458. if (x != nullptr)
  459. delete x;
  460. x = new T();
  461. }
  462. x->DoState(*this);
  463. }
  464. template<class T>
  465. void DoArray(T *x, int count) {
  466. DoHelper<T>::DoArray(this, x, count);
  467. }
  468. template<class T>
  469. void Do(T &x) {
  470. DoHelper<T>::Do(this, x);
  471. }
  472. template<class T>
  473. void DoPOD(T &x) {
  474. DoHelper<T>::Do(this, x);
  475. }
  476. template<class T>
  477. void DoPointer(T* &x, T*const base) {
  478. // pointers can be more than 2^31 apart, but you're using this function wrong if you need that much range
  479. s32 offset = x - base;
  480. Do(offset);
  481. if (mode == MODE_READ)
  482. x = base + offset;
  483. }
  484. template<class T, LinkedListItem<T>* (*TNew)(), void (*TFree)(LinkedListItem<T>*), void (*TDo)(PointerWrap&, T*)>
  485. void DoLinkedList(LinkedListItem<T>*& list_start, LinkedListItem<T>** list_end=0)
  486. {
  487. LinkedListItem<T>* list_cur = list_start;
  488. LinkedListItem<T>* prev = 0;
  489. while (true)
  490. {
  491. u8 shouldExist = (list_cur ? 1 : 0);
  492. Do(shouldExist);
  493. if (shouldExist == 1)
  494. {
  495. LinkedListItem<T>* cur = list_cur ? list_cur : TNew();
  496. TDo(*this, (T*)cur);
  497. if (!list_cur)
  498. {
  499. if (mode == MODE_READ)
  500. {
  501. cur->next = nullptr;
  502. list_cur = cur;
  503. if (prev)
  504. prev->next = cur;
  505. else
  506. list_start = cur;
  507. }
  508. else
  509. {
  510. TFree(cur);
  511. continue;
  512. }
  513. }
  514. }
  515. else
  516. {
  517. if (mode == MODE_READ)
  518. {
  519. if (prev)
  520. prev->next = nullptr;
  521. if (list_end)
  522. *list_end = prev;
  523. if (list_cur)
  524. {
  525. if (list_start == list_cur)
  526. list_start = nullptr;
  527. do
  528. {
  529. LinkedListItem<T>* next = list_cur->next;
  530. TFree(list_cur);
  531. list_cur = next;
  532. }
  533. while (list_cur);
  534. }
  535. }
  536. break;
  537. }
  538. prev = list_cur;
  539. list_cur = list_cur->next;
  540. }
  541. }
  542. void DoMarker(const char* prevName, u32 arbitraryNumber=0x42)
  543. {
  544. u32 cookie = arbitraryNumber;
  545. Do(cookie);
  546. if(mode == PointerWrap::MODE_READ && cookie != arbitraryNumber)
  547. {
  548. PanicAlertT("Error: After \"%s\", found %d (0x%X) instead of save marker %d (0x%X). Aborting savestate load...", prevName, cookie, cookie, arbitraryNumber, arbitraryNumber);
  549. SetError(ERROR_FAILURE);
  550. }
  551. }
  552. };
  553. inline PointerWrapSection::~PointerWrapSection() {
  554. if (ver_ > 0) {
  555. p_.DoMarker(title_);
  556. }
  557. }