chunk_file.h 17 KB

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