Guest User

Untitled

a guest
Nov 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.02 KB | None | 0 0
  1. #include "replay.h"
  2. #include "../ocgcore/ocgapi.h"
  3. #include "../ocgcore/card.h"
  4. #include <algorithm>
  5. #include "lzma/LzmaLib.h"
  6.  
  7. namespace ygo {
  8.  
  9. Replay::Replay() {
  10.     is_recording = false;
  11.     is_replaying = false;
  12.     replay_data = new unsigned char[0x20000];
  13.     comp_data = new unsigned char[0x2000];
  14. }
  15. Replay::~Replay() {
  16.     delete replay_data;
  17.     delete comp_data;
  18. }
  19. void Replay::BeginRecord() {
  20. #ifdef _WIN32
  21.     if(is_recording)
  22.         CloseHandle(recording_fp);
  23.     recording_fp = CreateFileW(L"./replay/_LastReplay.yrp", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
  24.     if(recording_fp == INVALID_HANDLE_VALUE)
  25.         return;
  26. #else
  27.     if(is_recording)
  28.         fclose(fp);
  29.     fp = fopen("./replay/_LastReplay.yrp", "wb");
  30.     if(!fp)
  31.         return;
  32. #endif
  33.     pdata = replay_data;
  34.     is_recording = true;
  35. }
  36. void Replay::WriteHeader(ReplayHeader& header) {
  37.     pheader = header;
  38. #ifdef _WIN32
  39.     DWORD size;
  40.     WriteFile(recording_fp, &header, sizeof(header), &size, NULL);
  41. #else
  42.     fwrite(&header, sizeof(header), 1, fp);
  43.     fflush(fp);
  44. #endif
  45. }
  46. void Replay::WriteData(const void* data, unsigned int length, bool flush) {
  47.     if(!is_recording)
  48.         return;
  49.     memcpy(pdata, data, length);
  50.     pdata += length;
  51. #ifdef _WIN32
  52.     DWORD size;
  53.     WriteFile(recording_fp, data, length, &size, NULL);
  54. #else
  55.     fwrite(data, length, 1, fp);
  56.     if(flush)
  57.         fflush(fp);
  58. #endif
  59. }
  60. void Replay::WriteInt32(int data, bool flush) {
  61.     if(!is_recording)
  62.         return;
  63.     *((int*)(pdata)) = data;
  64.     pdata += 4;
  65. #ifdef _WIN32
  66.     DWORD size;
  67.     WriteFile(recording_fp, &data, sizeof(int), &size, NULL);
  68. #else
  69.     fwrite(&data, sizeof(int), 1, fp);
  70.     if(flush)
  71.         fflush(fp);
  72. #endif
  73. }
  74. void Replay::WriteInt16(short data, bool flush) {
  75.     if(!is_recording)
  76.         return;
  77.     *((short*)(pdata)) = data;
  78.     pdata += 2;
  79. #ifdef _WIN32
  80.     DWORD size;
  81.     WriteFile(recording_fp, &data, sizeof(short), &size, NULL);
  82. #else
  83.     fwrite(&data, sizeof(short), 1, fp);
  84.     if(flush)
  85.         fflush(fp);
  86. #endif
  87. }
  88. void Replay::WriteInt8(char data, bool flush) {
  89.     if(!is_recording)
  90.         return;
  91.     *pdata = data;
  92.     pdata++;
  93. #ifdef _WIN32
  94.     DWORD size;
  95.     WriteFile(recording_fp, &data, sizeof(char), &size, NULL);
  96. #else
  97.     fwrite(&data, sizeof(char), 1, fp);
  98.     if(flush)
  99.         fflush(fp);
  100. #endif
  101. }
  102. void Replay::Flush() {
  103.     if(!is_recording)
  104.         return;
  105. #ifdef _WIN32
  106. #else
  107.     fflush(fp);
  108. #endif
  109. }
  110. void Replay::EndRecord() {
  111.     if(!is_recording)
  112.         return;
  113. #ifdef _WIN32
  114.     CloseHandle(recording_fp);
  115. #else
  116.     fclose(fp);
  117. #endif
  118.     pheader.datasize = pdata - replay_data;
  119.     pheader.flag |= REPLAY_COMPRESSED;
  120.     size_t propsize = 5;
  121.     comp_size = 0x1000;
  122.     LzmaCompress(comp_data, &comp_size, replay_data, pdata - replay_data, pheader.props, &propsize, 5, 1 << 24, 3, 0, 2, 32, 1);
  123.     is_recording = false;
  124. }
  125. void Replay::SaveReplay(const wchar_t* name) {
  126.     wchar_t fname[64];
  127.     myswprintf(fname, L"./replay/%ls.yrp", name);
  128. #ifdef WIN32
  129.     fp = _wfopen(fname, L"wb");
  130. #else
  131.     char fname2[256];
  132.     BufferIO::EncodeUTF8(fname, fname2);
  133.     fp = fopen(fname2, "wb");
  134. #endif
  135.     if(!fp)
  136.         return;
  137.     fwrite(&pheader, sizeof(pheader), 1, fp);
  138.     fwrite(comp_data, comp_size, 1, fp);
  139.     fclose(fp);
  140. }
  141. bool Replay::OpenReplay(const wchar_t* name) {
  142.     wchar_t fname[256];
  143.     myswprintf(fname, L"./replay/%ls", name);
  144. #ifdef WIN32
  145.     fp = _wfopen(fname, L"rb");
  146. #else
  147.     char fname2[256];
  148.     BufferIO::EncodeUTF8(fname, fname2);
  149.     fp = fopen(fname2, "rb");
  150. #endif
  151.     if(!fp)
  152.         return false;
  153.     fseek(fp, 0, SEEK_END);
  154.     comp_size = ftell(fp) - sizeof(pheader);
  155.     fseek(fp, 0, SEEK_SET);
  156.     fread(&pheader, sizeof(pheader), 1, fp);
  157.     if(pheader.flag & REPLAY_COMPRESSED) {
  158.         fread(comp_data, 0x1000, 1, fp);
  159.         fclose(fp);
  160.         replay_size = pheader.datasize;
  161.         if(LzmaUncompress(replay_data, &replay_size, comp_data, &comp_size, pheader.props, 5) != SZ_OK)
  162.             return false;
  163.     } else {
  164.         fread(replay_data, 0x20000, 1, fp);
  165.         fclose(fp);
  166.         replay_size = comp_size;
  167.     }
  168.     pdata = replay_data;
  169.     is_replaying = true;
  170.     return true;
  171. }
  172. bool Replay::CheckReplay(const wchar_t* name) {
  173.     wchar_t fname[256];
  174.     myswprintf(fname, L"./replay/%ls", name);
  175. #ifdef WIN32
  176.     FILE* rfp = _wfopen(fname, L"rb");
  177. #else
  178.     char fname2[256];
  179.     BufferIO::EncodeUTF8(fname, fname2);
  180.     FILE* rfp = fopen(fname2, "rb");
  181. #endif
  182.     if(!rfp)
  183.         return false;
  184.     ReplayHeader rheader;
  185.     fread(&rheader, sizeof(ReplayHeader), 1, rfp);
  186.     fclose(rfp);
  187.     return rheader.id == 0x31707279 && rheader.version >= 0x12d0;
  188. }
  189. bool Replay::ReadNextResponse(unsigned char resp[64]) {
  190.     if(pdata - replay_data >= replay_size)
  191.         return false;
  192.     int len = *pdata++;
  193.     if(len > 64)
  194.         return false;
  195.     memcpy(resp, pdata, len);
  196.     pdata += len;
  197.     return true;
  198. }
  199. void Replay::ReadData(void* data, unsigned int length) {
  200.     if(!is_replaying)
  201.         return;
  202.     memcpy(data, pdata, length);
  203.     pdata += length;
  204. }
  205. int Replay::ReadInt32() {
  206.     if(!is_replaying)
  207.         return -1;
  208.     int ret = *((int*)pdata);
  209.     pdata += 4;
  210.     return ret;
  211. }
  212. short Replay::ReadInt16() {
  213.     if(!is_replaying)
  214.         return -1;
  215.     short ret = *((short*)pdata);
  216.     pdata += 4;
  217.     return ret;
  218. }
  219. char Replay::ReadInt8() {
  220.     if(!is_replaying)
  221.         return -1;
  222.     return *pdata++;
  223. }
  224.  
  225. }
Add Comment
Please, Sign In to add comment