Commit fb6814a8 authored by Chen Bill's avatar Chen Bill Committed by GitHub

Refactor replay code for cross-platform compatibility (#2877)

parent 4e002a72
#include "config.h"
#include "replay.h" #include "replay.h"
#include "myfilesystem.h" #include "myfilesystem.h"
#include "lzma/LzmaLib.h" #include "lzma/LzmaLib.h"
...@@ -15,31 +16,18 @@ Replay::~Replay() { ...@@ -15,31 +16,18 @@ Replay::~Replay() {
void Replay::BeginRecord() { void Replay::BeginRecord() {
if(!FileSystem::IsDirExists(L"./replay") && !FileSystem::MakeDir(L"./replay")) if(!FileSystem::IsDirExists(L"./replay") && !FileSystem::MakeDir(L"./replay"))
return; return;
#ifdef _WIN32
if(is_recording)
CloseHandle(recording_fp);
recording_fp = CreateFileW(L"./replay/_LastReplay.yrp", GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, nullptr);
if(recording_fp == INVALID_HANDLE_VALUE)
return;
#else
if(is_recording) if(is_recording)
std::fclose(fp); std::fclose(fp);
fp = myfopen("./replay/_LastReplay.yrp", "wb"); fp = myfopen("./replay/_LastReplay.yrp", "wb");
if(!fp) if(!fp)
return; return;
#endif
Reset(); Reset();
is_recording = true; is_recording = true;
} }
void Replay::WriteHeader(ExtendedReplayHeader& header) { void Replay::WriteHeader(ExtendedReplayHeader& header) {
pheader = header; pheader = header;
#ifdef _WIN32 std::fwrite(&header, sizeof header, 1, fp);
DWORD size;
WriteFile(recording_fp, &header, sizeof(header), &size, nullptr);
#else
std::fwrite(&header, sizeof(header), 1, fp);
std::fflush(fp); std::fflush(fp);
#endif
} }
void Replay::WriteData(const void* data, size_t length, bool flush) { void Replay::WriteData(const void* data, size_t length, bool flush) {
if(!is_recording) if(!is_recording)
...@@ -48,14 +36,9 @@ void Replay::WriteData(const void* data, size_t length, bool flush) { ...@@ -48,14 +36,9 @@ void Replay::WriteData(const void* data, size_t length, bool flush) {
return; return;
std::memcpy(replay_data + replay_size, data, length); std::memcpy(replay_data + replay_size, data, length);
replay_size += length; replay_size += length;
#ifdef _WIN32
DWORD size;
WriteFile(recording_fp, data, length, &size, nullptr);
#else
std::fwrite(data, length, 1, fp); std::fwrite(data, length, 1, fp);
if(flush) if(flush)
std::fflush(fp); std::fflush(fp);
#endif
} }
void Replay::WriteInt32(int32_t data, bool flush) { void Replay::WriteInt32(int32_t data, bool flush) {
Write<int32_t>(data, flush); Write<int32_t>(data, flush);
...@@ -63,19 +46,12 @@ void Replay::WriteInt32(int32_t data, bool flush) { ...@@ -63,19 +46,12 @@ void Replay::WriteInt32(int32_t data, bool flush) {
void Replay::Flush() { void Replay::Flush() {
if(!is_recording) if(!is_recording)
return; return;
#ifdef _WIN32
#else
std::fflush(fp); std::fflush(fp);
#endif
} }
void Replay::EndRecord() { void Replay::EndRecord() {
if(!is_recording) if(!is_recording)
return; return;
#ifdef _WIN32
CloseHandle(recording_fp);
#else
std::fclose(fp); std::fclose(fp);
#endif
pheader.base.datasize = replay_size; pheader.base.datasize = replay_size;
pheader.base.flag |= REPLAY_COMPRESSED; pheader.base.flag |= REPLAY_COMPRESSED;
size_t propsize = 5; size_t propsize = 5;
......
#ifndef REPLAY_H #ifndef REPLAY_H
#define REPLAY_H #define REPLAY_H
#include "config.h" #include <cstdio>
#include <vector>
#include <string>
#include "../ocgcore/ocgapi.h"
#include "deck_manager.h" #include "deck_manager.h"
namespace ygo { namespace ygo {
...@@ -95,10 +98,6 @@ public: ...@@ -95,10 +98,6 @@ public:
bool IsReplaying() const; bool IsReplaying() const;
FILE* fp{ nullptr }; FILE* fp{ nullptr };
#ifdef _WIN32
HANDLE recording_fp{ nullptr };
#endif
ExtendedReplayHeader pheader; ExtendedReplayHeader pheader;
unsigned char* comp_data; unsigned char* comp_data;
size_t comp_size{}; size_t comp_size{};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment