Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 41 additions & 4 deletions gframe/replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,38 @@ 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, SIZE_RETURN_VALUE);
if(!response_length)
return 0;
const bool extended = response_length > UINT8_MAX;
const size_t prefix_length = extended ? sizeof(uint8_t) + sizeof(uint16_t) : sizeof(uint8_t);
const size_t total_length = prefix_length + response_length;
if(total_length > MAX_REPLAY_SIZE - replay_size)
return 0;
if(extended) {
Write<uint8_t>(0, false);
Write<uint16_t>(static_cast<uint16_t>(response_length), false);
} else {
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,12 +221,17 @@ 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{};
if (!ReadData(&len, sizeof len))
uint8_t len{};
if(!ReadData(&len, sizeof len))
return false;
if (!ReadData(resp, len))
std::memset(resp, 0, SIZE_RETURN_VALUE);
if(len)
return ReadData(resp, len);
uint16_t extended_length{};
if(!ReadData(&extended_length, sizeof extended_length))
return false;
return true;
const size_t response_length = std::min<size_t>(extended_length, SIZE_RETURN_VALUE);
return ReadData(resp, response_length);
Comment thread
purerosefallen marked this conversation as resolved.
Outdated
}
bool Replay::ReadName(wchar_t* data) {
uint16_t buffer[20]{};
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 @@ -579,6 +579,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 @@ -1414,8 +1418,7 @@ void SingleDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int
if (len > SIZE_RETURN_VALUE)
len = SIZE_RETURN_VALUE;
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 @@ -1425,6 +1428,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{};
bool match_mode{ false };
int match_kill{ 0 };
unsigned char duel_count{ 0 };
Expand Down
3 changes: 1 addition & 2 deletions gframe/single_mode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,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.WriteResponse(resp, len);
set_responseb(pduel, resp);
}
int SingleMode::SinglePlayThread() {
Expand Down
8 changes: 6 additions & 2 deletions gframe/tag_duel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,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 @@ -1521,8 +1525,7 @@ void TagDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int len
if (len > SIZE_RETURN_VALUE)
len = SIZE_RETURN_VALUE;
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 @@ -1533,6 +1536,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{};
unsigned char turn_count;
short time_limit[2];
short time_elapsed;
Expand Down
Loading