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
41 changes: 31 additions & 10 deletions gframe/bufferio.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,52 @@
#define BUFFERIO_H

#include <cstdint>
#include <cstring>
#include <cwchar>
#include "../ocgcore/buffer.h"

class BufferIO {
public:
static int ReadInt32(unsigned char*& p) {
return buffer_read<int32_t>(p);
template<typename T>
static T Read(unsigned char*& p) {
T ret{};
std::memcpy(&ret, p, sizeof(T));
p += sizeof(T);
return ret;
}
Comment thread
salix5 marked this conversation as resolved.
template<typename T>
static void Write(unsigned char*& p, T value) {
std::memcpy(p, &value, sizeof(T));
p += sizeof(T);
}

// for compatibility
[[deprecated]]
static int32_t ReadInt32(unsigned char*& p) {
return Read<int32_t>(p);
}
[[deprecated]]
static short ReadInt16(unsigned char*& p) {
return buffer_read<int16_t>(p);
return Read<int16_t>(p);
}
[[deprecated]]
static char ReadInt8(unsigned char*& p) {
return buffer_read<char>(p);
return Read<char>(p);
}
[[deprecated]]
static unsigned char ReadUInt8(unsigned char*& p) {
return buffer_read<unsigned char>(p);
return Read<unsigned char>(p);
}
static void WriteInt32(unsigned char*& p, int val) {
buffer_write<int32_t>(p, val);
[[deprecated]]
static void WriteInt32(unsigned char*& p, int32_t val) {
Write<int32_t>(p, val);
}
[[deprecated]]
static void WriteInt16(unsigned char*& p, short val) {
buffer_write<int16_t>(p, val);
Write<int16_t>(p, val);
}
[[deprecated]]
static void WriteInt8(unsigned char*& p, char val) {
buffer_write<char>(p, val);
Write<char>(p, val);
}
/**
* @brief Copy a C-style string to another C-style string.
Expand Down
68 changes: 34 additions & 34 deletions gframe/client_card.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ void ClientCard::SetCode(unsigned int x) {
code = x;
}
void ClientCard::UpdateInfo(unsigned char* buf) {
int flag = BufferIO::ReadInt32(buf);
int flag = BufferIO::Read<int32_t>(buf);
if (flag == 0) {
ClearData();
return;
}
if(flag & QUERY_CODE) {
int pdata = BufferIO::ReadInt32(buf);
int pdata = BufferIO::Read<int32_t>(buf);
if (!pdata)
ClearData();
if((location == LOCATION_HAND) && ((unsigned int)pdata != code)) {
Expand All @@ -55,45 +55,45 @@ void ClientCard::UpdateInfo(unsigned char* buf) {
code = pdata;
}
if(flag & QUERY_POSITION) {
int pdata = (BufferIO::ReadInt32(buf) >> 24) & 0xff;
int pdata = (BufferIO::Read<int32_t>(buf) >> 24) & 0xff;
if((location & (LOCATION_EXTRA | LOCATION_REMOVED)) && pdata != position) {
position = pdata;
mainGame->dField.MoveCard(this, 1);
} else
position = pdata;
}
if(flag & QUERY_ALIAS)
alias = BufferIO::ReadInt32(buf);
alias = BufferIO::Read<int32_t>(buf);
if(flag & QUERY_TYPE)
type = BufferIO::ReadInt32(buf);
type = BufferIO::Read<int32_t>(buf);
if(flag & QUERY_LEVEL) {
int pdata = BufferIO::ReadInt32(buf);
int pdata = BufferIO::Read<int32_t>(buf);
if(level != (unsigned int)pdata) {
level = pdata;
myswprintf(lvstring, L"L%d", level);
}
}
if(flag & QUERY_RANK) {
int pdata = BufferIO::ReadInt32(buf);
int pdata = BufferIO::Read<int32_t>(buf);
if(pdata && rank != (unsigned int)pdata) {
rank = pdata;
myswprintf(lvstring, L"R%d", rank);
}
}
if(flag & QUERY_ATTRIBUTE)
attribute = BufferIO::ReadInt32(buf);
attribute = BufferIO::Read<int32_t>(buf);
if(flag & QUERY_RACE)
race = BufferIO::ReadInt32(buf);
race = BufferIO::Read<int32_t>(buf);
if(flag & QUERY_ATTACK) {
attack = BufferIO::ReadInt32(buf);
attack = BufferIO::Read<int32_t>(buf);
if(attack < 0) {
atkstring[0] = '?';
atkstring[1] = 0;
} else
myswprintf(atkstring, L"%d", attack);
}
if(flag & QUERY_DEFENSE) {
defense = BufferIO::ReadInt32(buf);
defense = BufferIO::Read<int32_t>(buf);
if(type & TYPE_LINK) {
defstring[0] = '-';
defstring[1] = 0;
Expand All @@ -104,31 +104,31 @@ void ClientCard::UpdateInfo(unsigned char* buf) {
myswprintf(defstring, L"%d", defense);
}
if(flag & QUERY_BASE_ATTACK)
base_attack = BufferIO::ReadInt32(buf);
base_attack = BufferIO::Read<int32_t>(buf);
if(flag & QUERY_BASE_DEFENSE)
base_defense = BufferIO::ReadInt32(buf);
base_defense = BufferIO::Read<int32_t>(buf);
if(flag & QUERY_REASON)
reason = BufferIO::ReadInt32(buf);
reason = BufferIO::Read<int32_t>(buf);
if(flag & QUERY_REASON_CARD)
buf += 4;
if(flag & QUERY_EQUIP_CARD) {
int c = BufferIO::ReadUInt8(buf);
unsigned int l = BufferIO::ReadUInt8(buf);
int s = BufferIO::ReadUInt8(buf);
BufferIO::ReadUInt8(buf);
int c = BufferIO::Read<uint8_t>(buf);
unsigned int l = BufferIO::Read<uint8_t>(buf);
int s = BufferIO::Read<uint8_t>(buf);
BufferIO::Read<uint8_t>(buf);
ClientCard* ecard = mainGame->dField.GetCard(mainGame->LocalPlayer(c), l, s);
if (ecard) {
equipTarget = ecard;
ecard->equipped.insert(this);
}
}
if(flag & QUERY_TARGET_CARD) {
int count = BufferIO::ReadInt32(buf);
int count = BufferIO::Read<int32_t>(buf);
for(int i = 0; i < count; ++i) {
int c = BufferIO::ReadUInt8(buf);
unsigned int l = BufferIO::ReadUInt8(buf);
int s = BufferIO::ReadUInt8(buf);
BufferIO::ReadUInt8(buf);
int c = BufferIO::Read<uint8_t>(buf);
unsigned int l = BufferIO::Read<uint8_t>(buf);
int s = BufferIO::Read<uint8_t>(buf);
BufferIO::Read<uint8_t>(buf);
ClientCard* tcard = mainGame->dField.GetCard(mainGame->LocalPlayer(c), l, s);
if (tcard) {
cardTarget.insert(tcard);
Expand All @@ -137,38 +137,38 @@ void ClientCard::UpdateInfo(unsigned char* buf) {
}
}
if(flag & QUERY_OVERLAY_CARD) {
int count = BufferIO::ReadInt32(buf);
int count = BufferIO::Read<int32_t>(buf);
for(int i = 0; i < count; ++i) {
overlayed[i]->SetCode(BufferIO::ReadInt32(buf));
overlayed[i]->SetCode(BufferIO::Read<int32_t>(buf));
}
}
if(flag & QUERY_COUNTERS) {
int count = BufferIO::ReadInt32(buf);
int count = BufferIO::Read<int32_t>(buf);
for(int i = 0; i < count; ++i) {
int ctype = BufferIO::ReadInt16(buf);
int ccount = BufferIO::ReadInt16(buf);
int ctype = BufferIO::Read<uint16_t>(buf);
int ccount = BufferIO::Read<uint16_t>(buf);
counters[ctype] = ccount;
}
}
if(flag & QUERY_OWNER)
owner = BufferIO::ReadInt32(buf);
owner = BufferIO::Read<int32_t>(buf);
if(flag & QUERY_STATUS)
status = BufferIO::ReadInt32(buf);
status = BufferIO::Read<int32_t>(buf);
if(flag & QUERY_LSCALE) {
lscale = BufferIO::ReadInt32(buf);
lscale = BufferIO::Read<int32_t>(buf);
myswprintf(lscstring, L"%d", lscale);
}
if(flag & QUERY_RSCALE) {
rscale = BufferIO::ReadInt32(buf);
rscale = BufferIO::Read<int32_t>(buf);
myswprintf(rscstring, L"%d", rscale);
}
if(flag & QUERY_LINK) {
int pdata = BufferIO::ReadInt32(buf);
int pdata = BufferIO::Read<int32_t>(buf);
if (link != (unsigned int)pdata) {
link = pdata;
}
myswprintf(linkstring, L"L\x2012%d", link);
pdata = BufferIO::ReadInt32(buf);
pdata = BufferIO::Read<int32_t>(buf);
if (link_marker != (unsigned int)pdata) {
link_marker = pdata;
}
Expand Down
4 changes: 2 additions & 2 deletions gframe/client_field.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ ClientCard* ClientField::RemoveCard(int controler, int location, int sequence) {
}
void ClientField::UpdateCard(int controler, int location, int sequence, unsigned char* data) {
ClientCard* pcard = GetCard(controler, location, sequence);
int len = BufferIO::ReadInt32(data);
int len = BufferIO::Read<int32_t>(data);
if (pcard && len > LEN_HEADER)
pcard->UpdateInfo(data);
}
Expand Down Expand Up @@ -350,7 +350,7 @@ void ClientField::UpdateFieldCard(int controler, int location, unsigned char* da
return;
int len;
for(auto cit = lst->begin(); cit != lst->end(); ++cit) {
len = BufferIO::ReadInt32(data);
len = BufferIO::Read<int32_t>(data);
if(len > LEN_HEADER)
(*cit)->UpdateInfo(data);
data += len - 4;
Expand Down
10 changes: 5 additions & 5 deletions gframe/deck_con.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,14 +698,14 @@ bool DeckBuilder::OnEvent(const irr::SEvent& event) {
mainGame->ClearCardInfo();
unsigned char deckbuf[1024];
auto pdeck = deckbuf;
BufferIO::WriteInt32(pdeck, deckManager.current_deck.main.size() + deckManager.current_deck.extra.size());
BufferIO::WriteInt32(pdeck, deckManager.current_deck.side.size());
BufferIO::Write<int32_t>(pdeck, deckManager.current_deck.main.size() + deckManager.current_deck.extra.size());
BufferIO::Write<int32_t>(pdeck, deckManager.current_deck.side.size());
for(size_t i = 0; i < deckManager.current_deck.main.size(); ++i)
BufferIO::WriteInt32(pdeck, deckManager.current_deck.main[i]->first);
BufferIO::Write<int32_t>(pdeck, deckManager.current_deck.main[i]->first);
for(size_t i = 0; i < deckManager.current_deck.extra.size(); ++i)
BufferIO::WriteInt32(pdeck, deckManager.current_deck.extra[i]->first);
BufferIO::Write<int32_t>(pdeck, deckManager.current_deck.extra[i]->first);
for(size_t i = 0; i < deckManager.current_deck.side.size(); ++i)
BufferIO::WriteInt32(pdeck, deckManager.current_deck.side[i]->first);
BufferIO::Write<int32_t>(pdeck, deckManager.current_deck.side[i]->first);
DuelClient::SendBufferToServer(CTOS_UPDATE_DECK, deckbuf, pdeck - deckbuf);
break;
}
Expand Down
Loading