Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion gframe/replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ void Replay::WriteData(const void* data, size_t length, bool flush) {
void Replay::WriteInt32(int32_t data, bool flush) {
Write<int32_t>(data, flush);
}
size_t Replay::WriteResponse(const void* data, size_t length) {
if(!is_recording || !data)
return 0;
const size_t response_length = std::min<size_t>(length, UINT8_MAX);
if(!response_length)
return 0;
const size_t total_length = sizeof(uint8_t) + response_length;
if(total_length > MAX_REPLAY_SIZE - replay_size)
return 0;
Write<uint8_t>(static_cast<uint8_t>(response_length), false);
WriteData(data, response_length);
return total_length;
}
bool Replay::RemoveData(size_t length) {
if(!is_recording)
return false;
if(length > replay_size)
return false;
replay_size -= length;
if(fp) {
std::fflush(fp);
std::fseek(fp, -static_cast<long>(length), SEEK_CUR);
}
return true;
}
void Replay::Flush() {
if(!is_recording)
return;
Expand Down Expand Up @@ -189,7 +214,7 @@ bool Replay::RenameReplay(const wchar_t* oldname, const wchar_t* newname) {
return FileSystem::Rename(old_path, new_path);
}
bool Replay::ReadNextResponse(unsigned char resp[]) {
unsigned char len{};
uint8_t len{};
if (!ReadData(&len, sizeof len))
return false;
if (!ReadData(resp, len))
Expand Down
2 changes: 2 additions & 0 deletions gframe/replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Replay {
WriteData(&data, sizeof(T), flush);
}
void WriteInt32(int32_t data, bool flush = true);
size_t WriteResponse(const void* data, size_t length);
bool RemoveData(size_t length);
void Flush();
void EndRecord();
bool SaveReplay(const wchar_t* base_name);
Expand Down
8 changes: 6 additions & 2 deletions gframe/single_duel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
unsigned char engType = BufferIO::Read<uint8_t>(pbuf);
switch (engType) {
case MSG_RETRY: {
if(last_replay_response_size) {
last_replay.RemoveData(last_replay_response_size);
last_replay_response_size = 0;
}
WaitforResponse(last_response);
NetServer::SendBufferToPlayer(players[last_response], STOC_GAME_MSG, offset, pbuf - offset);
return 1;
Expand Down Expand Up @@ -1416,8 +1420,7 @@ void SingleDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int
if (len > UINT8_MAX)
len = UINT8_MAX;
std::memcpy(resb, pdata, len);
last_replay.Write<uint8_t>(len);
last_replay.WriteData(resb, len);
last_replay_response_size = last_replay.WriteResponse(resb, len);
set_responseb(pduel, resb);
players[dp->type]->state = 0xff;
if(host_info.time_limit) {
Expand All @@ -1427,6 +1430,7 @@ void SingleDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int
time_elapsed = 0;
}
Process();
last_replay_response_size = 0;
}
void SingleDuel::EndDuel() {
if(!pduel)
Expand Down
1 change: 1 addition & 0 deletions gframe/single_duel.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class SingleDuel: public DuelMode {
unsigned char last_response{ 0 };
std::set<DuelPlayer*> observers;
Replay last_replay;
size_t last_replay_response_size{ 0 };
bool match_mode{ false };
int match_kill{ 0 };
unsigned char duel_count{ 0 };
Expand Down
9 changes: 7 additions & 2 deletions gframe/single_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ intptr_t SingleMode::pduel = 0;
bool SingleMode::is_closing = false;
bool SingleMode::is_continuing = false;
Replay SingleMode::last_replay;
size_t SingleMode::last_replay_response_size = 0;

bool SingleMode::StartPlay() {
std::thread(SinglePlayThread).detach();
Expand All @@ -25,8 +26,7 @@ void SingleMode::StopPlay(bool is_exiting) {
void SingleMode::SetResponse(unsigned char* resp, unsigned int len) {
if(!pduel)
return;
last_replay.Write<uint8_t>(len);
last_replay.WriteData(resp, len);
last_replay_response_size = last_replay.WriteResponse(resp, len);
set_responseb(pduel, resp);
}
int SingleMode::SinglePlayThread() {
Expand Down Expand Up @@ -112,6 +112,7 @@ int SingleMode::SinglePlayThread() {
if (len > 0)
is_continuing = SinglePlayAnalyze(engineBuffer.data(), len);
last_replay.BeginRecord();
last_replay_response_size = 0;
last_replay.WriteHeader(rh);
uint16_t host_name[20]{};
BufferIO::CopyCharArray(mainGame->dInfo.hostname, host_name);
Expand Down Expand Up @@ -191,6 +192,10 @@ bool SingleMode::SinglePlayAnalyze(unsigned char* msg, unsigned int len) {
mainGame->dInfo.curMsg = BufferIO::Read<uint8_t>(pbuf);
switch (mainGame->dInfo.curMsg) {
case MSG_RETRY: {
if(last_replay_response_size) {
last_replay.RemoveData(last_replay_response_size);
last_replay_response_size = 0;
}
if(!DuelClient::ClientAnalyze(offset, pbuf - offset)) {
mainGame->singleSignal.Reset();
mainGame->singleSignal.Wait();
Expand Down
1 change: 1 addition & 0 deletions gframe/single_mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class SingleMode {

protected:
static Replay last_replay;
static size_t last_replay_response_size;
};

}
Expand Down
8 changes: 6 additions & 2 deletions gframe/tag_duel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ int TagDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
unsigned char engType = BufferIO::Read<uint8_t>(pbuf);
switch (engType) {
case MSG_RETRY: {
if(last_replay_response_size) {
last_replay.RemoveData(last_replay_response_size);
last_replay_response_size = 0;
}
WaitforResponse(last_response);
NetServer::SendBufferToPlayer(cur_player[last_response], STOC_GAME_MSG, offset, pbuf - offset);
return 1;
Expand Down Expand Up @@ -1522,8 +1526,7 @@ void TagDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int len
if (len > UINT8_MAX)
len = UINT8_MAX;
std::memcpy(resb, pdata, len);
last_replay.Write<uint8_t>(len);
last_replay.WriteData(resb, len);
last_replay_response_size = last_replay.WriteResponse(resb, len);
set_responseb(pduel, resb);
players[dp->type]->state = 0xff;
if(host_info.time_limit) {
Expand All @@ -1534,6 +1537,7 @@ void TagDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int len
time_elapsed = 0;
}
Process();
last_replay_response_size = 0;
}
void TagDuel::EndDuel() {
if(!pduel)
Expand Down
1 change: 1 addition & 0 deletions gframe/tag_duel.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class TagDuel: public DuelMode {
unsigned char hand_result[2];
unsigned char last_response;
Replay last_replay;
size_t last_replay_response_size{ 0 };
unsigned char turn_count;
short time_limit[2];
short time_elapsed;
Expand Down
Loading