chunk_file.h 17 KB

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