Commit d1c4b218 authored by edo9300's avatar edo9300

Added replay recording without writing

parent 595e266a
...@@ -28,7 +28,6 @@ mtrandom DuelClient::rnd; ...@@ -28,7 +28,6 @@ mtrandom DuelClient::rnd;
std::vector<BufferIO::ReplayPacket> DuelClient::replay_stream; std::vector<BufferIO::ReplayPacket> DuelClient::replay_stream;
Replay DuelClient::last_replay; Replay DuelClient::last_replay;
bool DuelClient::old_replay = true;
bool DuelClient::is_refreshing = false; bool DuelClient::is_refreshing = false;
int DuelClient::match_kill = 0; int DuelClient::match_kill = 0;
...@@ -797,8 +796,8 @@ void DuelClient::HandleSTOCPacketLan(char* data, unsigned int len) { ...@@ -797,8 +796,8 @@ void DuelClient::HandleSTOCPacketLan(char* data, unsigned int len) {
ReplayHeader pheader; ReplayHeader pheader;
memcpy(&pheader, prep, sizeof(ReplayHeader)); memcpy(&pheader, prep, sizeof(ReplayHeader));
replay_stream.push_back(BufferIO::ReplayPacket(OLD_REPLAY_MODE, prep, len - 1)); replay_stream.push_back(BufferIO::ReplayPacket(OLD_REPLAY_MODE, prep, len - 1));
if(old_replay && mainGame->saveReplay) { if(mainGame->saveReplay) {
last_replay.BeginRecord(); last_replay.BeginRecord(false);
last_replay.WriteHeader(pheader); last_replay.WriteHeader(pheader);
last_replay.pheader.id = 0x58707279; last_replay.pheader.id = 0x58707279;
last_replay.WriteData(mainGame->dInfo.hostname, 40, false); last_replay.WriteData(mainGame->dInfo.hostname, 40, false);
...@@ -807,7 +806,7 @@ void DuelClient::HandleSTOCPacketLan(char* data, unsigned int len) { ...@@ -807,7 +806,7 @@ void DuelClient::HandleSTOCPacketLan(char* data, unsigned int len) {
last_replay.WriteData(mainGame->dInfo.hostname_tag, 40, false); last_replay.WriteData(mainGame->dInfo.hostname_tag, 40, false);
last_replay.WriteData(mainGame->dInfo.clientname_tag, 40, false); last_replay.WriteData(mainGame->dInfo.clientname_tag, 40, false);
} }
last_replay.WriteInt32(mainGame->dInfo.duel_field | mainGame->dInfo.extraval); last_replay.WriteInt32(mainGame->dInfo.duel_field | mainGame->dInfo.extraval >> 8);
last_replay.WriteStream(replay_stream); last_replay.WriteStream(replay_stream);
last_replay.EndRecord(); last_replay.EndRecord();
} }
...@@ -957,12 +956,37 @@ void DuelClient::HandleSTOCPacketLan(char* data, unsigned int len) { ...@@ -957,12 +956,37 @@ void DuelClient::HandleSTOCPacketLan(char* data, unsigned int len) {
break; break;
} }
case STOC_NEW_REPLAY: { case STOC_NEW_REPLAY: {
char* prep = pdata; mainGame->gMutex.Lock();
memcpy(&last_replay.pheader, prep, sizeof(ReplayHeader)); mainGame->wPhase->setVisible(false);
prep += sizeof(ReplayHeader); if(mainGame->dInfo.player_type < 7)
memcpy(last_replay.comp_data, prep, len - sizeof(ReplayHeader) - 1); mainGame->btnLeaveGame->setVisible(false);
last_replay.comp_size = len - sizeof(ReplayHeader) - 1; mainGame->btnChainIgnore->setVisible(false);
old_replay = false; mainGame->btnChainAlways->setVisible(false);
mainGame->btnChainWhenAvail->setVisible(false);
mainGame->btnCancelOrFinish->setVisible(false);
time_t nowtime = time(NULL);
struct tm *localedtime = localtime(&nowtime);
char timebuf[40];
strftime(timebuf, 40, "%Y-%m-%d %H-%M-%S", localedtime);
size_t size = strlen(timebuf) + 1;
wchar_t timetext[80];
mbstowcs(timetext, timebuf, size);
mainGame->ebRSName->setText(timetext);
mainGame->PopupElement(mainGame->wReplaySave);
mainGame->gMutex.Unlock();
mainGame->replaySignal.Reset();
mainGame->replaySignal.Wait();
if(mainGame->saveReplay || !is_host) {
char* prep = pdata;
Replay new_replay;
memcpy(&new_replay.pheader, prep, sizeof(ReplayHeader));
prep += sizeof(ReplayHeader);
memcpy(new_replay.comp_data, prep, len - sizeof(ReplayHeader) - 1);
new_replay.comp_size = len - sizeof(ReplayHeader) - 1;
if(mainGame->saveReplay)
new_replay.SaveReplay(mainGame->ebRSName->getText());
else new_replay.SaveReplay(L"_LastReplay");
}
break; break;
} }
} }
......
...@@ -48,7 +48,6 @@ public: ...@@ -48,7 +48,6 @@ public:
static void HandleSTOCPacketLan(char* data, unsigned int len); static void HandleSTOCPacketLan(char* data, unsigned int len);
static std::vector<BufferIO::ReplayPacket> replay_stream; static std::vector<BufferIO::ReplayPacket> replay_stream;
static Replay last_replay; static Replay last_replay;
static bool old_replay;
static int ClientAnalyze(char* msg, unsigned int len); static int ClientAnalyze(char* msg, unsigned int len);
static void SetResponseI(int respI); static void SetResponseI(int respI);
static void SetResponseB(void* respB, unsigned char len); static void SetResponseB(void* respB, unsigned char len);
......
...@@ -16,36 +16,25 @@ Replay::~Replay() { ...@@ -16,36 +16,25 @@ Replay::~Replay() {
delete[] replay_data; delete[] replay_data;
delete[] comp_data; delete[] comp_data;
} }
void Replay::BeginRecord() { void Replay::BeginRecord(bool write) {
#ifdef _WIN32 #ifdef _WIN32
if(is_recording) if(is_recording && is_writing)
CloseHandle(recording_fp); CloseHandle(recording_fp);
recording_fp = CreateFileW(L"./replay/_LastReplay.yrp", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL); is_writing = write;
if(recording_fp == INVALID_HANDLE_VALUE) if(is_writing) {
return; recording_fp = CreateFileW(L"./replay/_LastReplay.yrp", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
#else if(recording_fp == INVALID_HANDLE_VALUE)
if(is_recording) return;
fclose(fp); }
fp = fopen("./replay/_LastReplay.yrp", "wb");
if(!fp)
return;
#endif
pdata = replay_data;
is_recording = true;
}
void Replay::BeginRecord2() {
#ifdef _WIN32
if(is_recording)
CloseHandle(recording_fp);
recording_fp = CreateFileW(L"./replay/_LastReplay2.yrp", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
if(recording_fp == INVALID_HANDLE_VALUE)
return;
#else #else
if(is_recording) if(is_recording && is_writing)
fclose(fp); fclose(fp);
fp = fopen("./replay/_LastReplay2.yrp", "wb"); is_writing = write;
if(!fp) if(is_writing) {
return; fp = fopen("./replay/_LastReplay.yrp", "wb");
if(!fp)
return;
}
#endif #endif
pdata = replay_data; pdata = replay_data;
is_recording = true; is_recording = true;
...@@ -62,6 +51,7 @@ void Replay::WriteStream(std::vector<BufferIO::ReplayPacket> stream) { ...@@ -62,6 +51,7 @@ void Replay::WriteStream(std::vector<BufferIO::ReplayPacket> stream) {
} }
void Replay::WriteHeader(ReplayHeader& header) { void Replay::WriteHeader(ReplayHeader& header) {
pheader = header; pheader = header;
if(!is_writing) return;
#ifdef _WIN32 #ifdef _WIN32
DWORD size; DWORD size;
WriteFile(recording_fp, &header, sizeof(header), &size, NULL); WriteFile(recording_fp, &header, sizeof(header), &size, NULL);
...@@ -75,6 +65,7 @@ void Replay::WriteData(const void* data, unsigned int length, bool flush) { ...@@ -75,6 +65,7 @@ void Replay::WriteData(const void* data, unsigned int length, bool flush) {
return; return;
memcpy(pdata, data, length); memcpy(pdata, data, length);
pdata += length; pdata += length;
if(!is_writing) return;
#ifdef _WIN32 #ifdef _WIN32
DWORD size; DWORD size;
WriteFile(recording_fp, data, length, &size, NULL); WriteFile(recording_fp, data, length, &size, NULL);
...@@ -89,6 +80,7 @@ void Replay::WriteInt32(int data, bool flush) { ...@@ -89,6 +80,7 @@ void Replay::WriteInt32(int data, bool flush) {
return; return;
*((int*)(pdata)) = data; *((int*)(pdata)) = data;
pdata += 4; pdata += 4;
if(!is_writing) return;
#ifdef _WIN32 #ifdef _WIN32
DWORD size; DWORD size;
WriteFile(recording_fp, &data, sizeof(int), &size, NULL); WriteFile(recording_fp, &data, sizeof(int), &size, NULL);
...@@ -103,6 +95,7 @@ void Replay::WriteInt16(short data, bool flush) { ...@@ -103,6 +95,7 @@ void Replay::WriteInt16(short data, bool flush) {
return; return;
*((short*)(pdata)) = data; *((short*)(pdata)) = data;
pdata += 2; pdata += 2;
if(!is_writing) return;
#ifdef _WIN32 #ifdef _WIN32
DWORD size; DWORD size;
WriteFile(recording_fp, &data, sizeof(short), &size, NULL); WriteFile(recording_fp, &data, sizeof(short), &size, NULL);
...@@ -117,6 +110,7 @@ void Replay::WriteInt8(char data, bool flush) { ...@@ -117,6 +110,7 @@ void Replay::WriteInt8(char data, bool flush) {
return; return;
*pdata = data; *pdata = data;
pdata++; pdata++;
if(!is_writing) return;
#ifdef _WIN32 #ifdef _WIN32
DWORD size; DWORD size;
WriteFile(recording_fp, &data, sizeof(char), &size, NULL); WriteFile(recording_fp, &data, sizeof(char), &size, NULL);
...@@ -129,6 +123,7 @@ void Replay::WriteInt8(char data, bool flush) { ...@@ -129,6 +123,7 @@ void Replay::WriteInt8(char data, bool flush) {
void Replay::Flush() { void Replay::Flush() {
if(!is_recording) if(!is_recording)
return; return;
if(!is_writing) return;
#ifdef _WIN32 #ifdef _WIN32
#else #else
fflush(fp); fflush(fp);
...@@ -137,6 +132,7 @@ void Replay::Flush() { ...@@ -137,6 +132,7 @@ void Replay::Flush() {
void Replay::EndRecord() { void Replay::EndRecord() {
if(!is_recording) if(!is_recording)
return; return;
if(is_writing)
#ifdef _WIN32 #ifdef _WIN32
CloseHandle(recording_fp); CloseHandle(recording_fp);
#else #else
......
...@@ -26,8 +26,7 @@ class Replay { ...@@ -26,8 +26,7 @@ class Replay {
public: public:
Replay(); Replay();
~Replay(); ~Replay();
void BeginRecord(); void BeginRecord(bool write = true);
void BeginRecord2();
void WriteStream(std::vector<BufferIO::ReplayPacket> stream); void WriteStream(std::vector<BufferIO::ReplayPacket> stream);
void WritePacket(BufferIO::ReplayPacket p); void WritePacket(BufferIO::ReplayPacket p);
void WriteHeader(ReplayHeader& header); void WriteHeader(ReplayHeader& header);
...@@ -61,6 +60,7 @@ public: ...@@ -61,6 +60,7 @@ public:
size_t replay_size; size_t replay_size;
size_t comp_size; size_t comp_size;
bool is_recording; bool is_recording;
bool is_writing;
bool is_replaying; bool is_replaying;
}; };
......
...@@ -400,13 +400,13 @@ void SingleDuel::TPResult(DuelPlayer* dp, unsigned char tp) { ...@@ -400,13 +400,13 @@ void SingleDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
rh.flag = REPLAY_LUA64; rh.flag = REPLAY_LUA64;
time_t seed = time(0); time_t seed = time(0);
rh.seed = seed; rh.seed = seed;
last_replay.BeginRecord(); last_replay.BeginRecord(false);
last_replay.WriteHeader(rh); last_replay.WriteHeader(rh);
rnd.reset(seed); rnd.reset(seed);
last_replay.WriteData(players[0]->name, 40, false); last_replay.WriteData(players[0]->name, 40, false);
last_replay.WriteData(players[1]->name, 40, false); last_replay.WriteData(players[1]->name, 40, false);
//records the replay with the new system //records the replay with the new system
new_replay.BeginRecord2(); new_replay.BeginRecord();
rh.id = 0x58707279; rh.id = 0x58707279;
new_replay.WriteHeader(rh); new_replay.WriteHeader(rh);
new_replay.WriteData(players[0]->name, 40, false); new_replay.WriteData(players[0]->name, 40, false);
......
...@@ -108,10 +108,10 @@ int SingleMode::SinglePlayThread(void* param) { ...@@ -108,10 +108,10 @@ int SingleMode::SinglePlayThread(void* param) {
char engineBuffer[0x1000]; char engineBuffer[0x1000];
is_closing = false; is_closing = false;
is_continuing = true; is_continuing = true;
last_replay.BeginRecord(); last_replay.BeginRecord(false);
last_replay.WriteHeader(rh); last_replay.WriteHeader(rh);
//records the replay with the new system //records the replay with the new system
new_replay.BeginRecord2(); new_replay.BeginRecord();
rh.id = 0x58707279; rh.id = 0x58707279;
new_replay.WriteHeader(rh); new_replay.WriteHeader(rh);
replay_stream.clear(); replay_stream.clear();
......
...@@ -357,7 +357,7 @@ void TagDuel::TPResult(DuelPlayer* dp, unsigned char tp) { ...@@ -357,7 +357,7 @@ void TagDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
rh.flag = REPLAY_TAG + REPLAY_LUA64; rh.flag = REPLAY_TAG + REPLAY_LUA64;
time_t seed = time(0); time_t seed = time(0);
rh.seed = seed; rh.seed = seed;
last_replay.BeginRecord(); last_replay.BeginRecord(false);
last_replay.WriteHeader(rh); last_replay.WriteHeader(rh);
rnd.reset(seed); rnd.reset(seed);
last_replay.WriteData(players[0]->name, 40, false); last_replay.WriteData(players[0]->name, 40, false);
...@@ -365,7 +365,7 @@ void TagDuel::TPResult(DuelPlayer* dp, unsigned char tp) { ...@@ -365,7 +365,7 @@ void TagDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
last_replay.WriteData(players[2]->name, 40, false); last_replay.WriteData(players[2]->name, 40, false);
last_replay.WriteData(players[3]->name, 40, false); last_replay.WriteData(players[3]->name, 40, false);
//records the replay with the new system //records the replay with the new system
new_replay.BeginRecord2(); new_replay.BeginRecord();
rh.id = 0x58707279; rh.id = 0x58707279;
new_replay.WriteHeader(rh); new_replay.WriteHeader(rh);
new_replay.WriteData(players[0]->name, 40, false); new_replay.WriteData(players[0]->name, 40, false);
......
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