Skip to content

Commit 42a427a

Browse files
authored
use function template BufferIO::Read (#2835)
1 parent 3e2be8d commit 42a427a

13 files changed

Lines changed: 886 additions & 863 deletions

gframe/bufferio.h

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,52 @@
22
#define BUFFERIO_H
33

44
#include <cstdint>
5+
#include <cstring>
56
#include <cwchar>
6-
#include "../ocgcore/buffer.h"
77

88
class BufferIO {
99
public:
10-
static int ReadInt32(unsigned char*& p) {
11-
return buffer_read<int32_t>(p);
10+
template<typename T>
11+
static T Read(unsigned char*& p) {
12+
T ret{};
13+
std::memcpy(&ret, p, sizeof(T));
14+
p += sizeof(T);
15+
return ret;
16+
}
17+
template<typename T>
18+
static void Write(unsigned char*& p, T value) {
19+
std::memcpy(p, &value, sizeof(T));
20+
p += sizeof(T);
21+
}
22+
23+
// for compatibility
24+
[[deprecated]]
25+
static int32_t ReadInt32(unsigned char*& p) {
26+
return Read<int32_t>(p);
1227
}
28+
[[deprecated]]
1329
static short ReadInt16(unsigned char*& p) {
14-
return buffer_read<int16_t>(p);
30+
return Read<int16_t>(p);
1531
}
32+
[[deprecated]]
1633
static char ReadInt8(unsigned char*& p) {
17-
return buffer_read<char>(p);
34+
return Read<char>(p);
1835
}
36+
[[deprecated]]
1937
static unsigned char ReadUInt8(unsigned char*& p) {
20-
return buffer_read<unsigned char>(p);
38+
return Read<unsigned char>(p);
2139
}
22-
static void WriteInt32(unsigned char*& p, int val) {
23-
buffer_write<int32_t>(p, val);
40+
[[deprecated]]
41+
static void WriteInt32(unsigned char*& p, int32_t val) {
42+
Write<int32_t>(p, val);
2443
}
44+
[[deprecated]]
2545
static void WriteInt16(unsigned char*& p, short val) {
26-
buffer_write<int16_t>(p, val);
46+
Write<int16_t>(p, val);
2747
}
48+
[[deprecated]]
2849
static void WriteInt8(unsigned char*& p, char val) {
29-
buffer_write<char>(p, val);
50+
Write<char>(p, val);
3051
}
3152
/**
3253
* @brief Copy a C-style string to another C-style string.

gframe/client_card.cpp

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ void ClientCard::SetCode(unsigned int x) {
3939
code = x;
4040
}
4141
void ClientCard::UpdateInfo(unsigned char* buf) {
42-
int flag = BufferIO::ReadInt32(buf);
42+
int flag = BufferIO::Read<int32_t>(buf);
4343
if (flag == 0) {
4444
ClearData();
4545
return;
4646
}
4747
if(flag & QUERY_CODE) {
48-
int pdata = BufferIO::ReadInt32(buf);
48+
int pdata = BufferIO::Read<int32_t>(buf);
4949
if (!pdata)
5050
ClearData();
5151
if((location == LOCATION_HAND) && ((unsigned int)pdata != code)) {
@@ -55,45 +55,45 @@ void ClientCard::UpdateInfo(unsigned char* buf) {
5555
code = pdata;
5656
}
5757
if(flag & QUERY_POSITION) {
58-
int pdata = (BufferIO::ReadInt32(buf) >> 24) & 0xff;
58+
int pdata = (BufferIO::Read<int32_t>(buf) >> 24) & 0xff;
5959
if((location & (LOCATION_EXTRA | LOCATION_REMOVED)) && pdata != position) {
6060
position = pdata;
6161
mainGame->dField.MoveCard(this, 1);
6262
} else
6363
position = pdata;
6464
}
6565
if(flag & QUERY_ALIAS)
66-
alias = BufferIO::ReadInt32(buf);
66+
alias = BufferIO::Read<int32_t>(buf);
6767
if(flag & QUERY_TYPE)
68-
type = BufferIO::ReadInt32(buf);
68+
type = BufferIO::Read<int32_t>(buf);
6969
if(flag & QUERY_LEVEL) {
70-
int pdata = BufferIO::ReadInt32(buf);
70+
int pdata = BufferIO::Read<int32_t>(buf);
7171
if(level != (unsigned int)pdata) {
7272
level = pdata;
7373
myswprintf(lvstring, L"L%d", level);
7474
}
7575
}
7676
if(flag & QUERY_RANK) {
77-
int pdata = BufferIO::ReadInt32(buf);
77+
int pdata = BufferIO::Read<int32_t>(buf);
7878
if(pdata && rank != (unsigned int)pdata) {
7979
rank = pdata;
8080
myswprintf(lvstring, L"R%d", rank);
8181
}
8282
}
8383
if(flag & QUERY_ATTRIBUTE)
84-
attribute = BufferIO::ReadInt32(buf);
84+
attribute = BufferIO::Read<int32_t>(buf);
8585
if(flag & QUERY_RACE)
86-
race = BufferIO::ReadInt32(buf);
86+
race = BufferIO::Read<int32_t>(buf);
8787
if(flag & QUERY_ATTACK) {
88-
attack = BufferIO::ReadInt32(buf);
88+
attack = BufferIO::Read<int32_t>(buf);
8989
if(attack < 0) {
9090
atkstring[0] = '?';
9191
atkstring[1] = 0;
9292
} else
9393
myswprintf(atkstring, L"%d", attack);
9494
}
9595
if(flag & QUERY_DEFENSE) {
96-
defense = BufferIO::ReadInt32(buf);
96+
defense = BufferIO::Read<int32_t>(buf);
9797
if(type & TYPE_LINK) {
9898
defstring[0] = '-';
9999
defstring[1] = 0;
@@ -104,31 +104,31 @@ void ClientCard::UpdateInfo(unsigned char* buf) {
104104
myswprintf(defstring, L"%d", defense);
105105
}
106106
if(flag & QUERY_BASE_ATTACK)
107-
base_attack = BufferIO::ReadInt32(buf);
107+
base_attack = BufferIO::Read<int32_t>(buf);
108108
if(flag & QUERY_BASE_DEFENSE)
109-
base_defense = BufferIO::ReadInt32(buf);
109+
base_defense = BufferIO::Read<int32_t>(buf);
110110
if(flag & QUERY_REASON)
111-
reason = BufferIO::ReadInt32(buf);
111+
reason = BufferIO::Read<int32_t>(buf);
112112
if(flag & QUERY_REASON_CARD)
113113
buf += 4;
114114
if(flag & QUERY_EQUIP_CARD) {
115-
int c = BufferIO::ReadUInt8(buf);
116-
unsigned int l = BufferIO::ReadUInt8(buf);
117-
int s = BufferIO::ReadUInt8(buf);
118-
BufferIO::ReadUInt8(buf);
115+
int c = BufferIO::Read<uint8_t>(buf);
116+
unsigned int l = BufferIO::Read<uint8_t>(buf);
117+
int s = BufferIO::Read<uint8_t>(buf);
118+
BufferIO::Read<uint8_t>(buf);
119119
ClientCard* ecard = mainGame->dField.GetCard(mainGame->LocalPlayer(c), l, s);
120120
if (ecard) {
121121
equipTarget = ecard;
122122
ecard->equipped.insert(this);
123123
}
124124
}
125125
if(flag & QUERY_TARGET_CARD) {
126-
int count = BufferIO::ReadInt32(buf);
126+
int count = BufferIO::Read<int32_t>(buf);
127127
for(int i = 0; i < count; ++i) {
128-
int c = BufferIO::ReadUInt8(buf);
129-
unsigned int l = BufferIO::ReadUInt8(buf);
130-
int s = BufferIO::ReadUInt8(buf);
131-
BufferIO::ReadUInt8(buf);
128+
int c = BufferIO::Read<uint8_t>(buf);
129+
unsigned int l = BufferIO::Read<uint8_t>(buf);
130+
int s = BufferIO::Read<uint8_t>(buf);
131+
BufferIO::Read<uint8_t>(buf);
132132
ClientCard* tcard = mainGame->dField.GetCard(mainGame->LocalPlayer(c), l, s);
133133
if (tcard) {
134134
cardTarget.insert(tcard);
@@ -137,38 +137,38 @@ void ClientCard::UpdateInfo(unsigned char* buf) {
137137
}
138138
}
139139
if(flag & QUERY_OVERLAY_CARD) {
140-
int count = BufferIO::ReadInt32(buf);
140+
int count = BufferIO::Read<int32_t>(buf);
141141
for(int i = 0; i < count; ++i) {
142-
overlayed[i]->SetCode(BufferIO::ReadInt32(buf));
142+
overlayed[i]->SetCode(BufferIO::Read<int32_t>(buf));
143143
}
144144
}
145145
if(flag & QUERY_COUNTERS) {
146-
int count = BufferIO::ReadInt32(buf);
146+
int count = BufferIO::Read<int32_t>(buf);
147147
for(int i = 0; i < count; ++i) {
148-
int ctype = BufferIO::ReadInt16(buf);
149-
int ccount = BufferIO::ReadInt16(buf);
148+
int ctype = BufferIO::Read<uint16_t>(buf);
149+
int ccount = BufferIO::Read<uint16_t>(buf);
150150
counters[ctype] = ccount;
151151
}
152152
}
153153
if(flag & QUERY_OWNER)
154-
owner = BufferIO::ReadInt32(buf);
154+
owner = BufferIO::Read<int32_t>(buf);
155155
if(flag & QUERY_STATUS)
156-
status = BufferIO::ReadInt32(buf);
156+
status = BufferIO::Read<int32_t>(buf);
157157
if(flag & QUERY_LSCALE) {
158-
lscale = BufferIO::ReadInt32(buf);
158+
lscale = BufferIO::Read<int32_t>(buf);
159159
myswprintf(lscstring, L"%d", lscale);
160160
}
161161
if(flag & QUERY_RSCALE) {
162-
rscale = BufferIO::ReadInt32(buf);
162+
rscale = BufferIO::Read<int32_t>(buf);
163163
myswprintf(rscstring, L"%d", rscale);
164164
}
165165
if(flag & QUERY_LINK) {
166-
int pdata = BufferIO::ReadInt32(buf);
166+
int pdata = BufferIO::Read<int32_t>(buf);
167167
if (link != (unsigned int)pdata) {
168168
link = pdata;
169169
}
170170
myswprintf(linkstring, L"L\x2012%d", link);
171-
pdata = BufferIO::ReadInt32(buf);
171+
pdata = BufferIO::Read<int32_t>(buf);
172172
if (link_marker != (unsigned int)pdata) {
173173
link_marker = pdata;
174174
}

gframe/client_field.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ ClientCard* ClientField::RemoveCard(int controler, int location, int sequence) {
317317
}
318318
void ClientField::UpdateCard(int controler, int location, int sequence, unsigned char* data) {
319319
ClientCard* pcard = GetCard(controler, location, sequence);
320-
int len = BufferIO::ReadInt32(data);
320+
int len = BufferIO::Read<int32_t>(data);
321321
if (pcard && len > LEN_HEADER)
322322
pcard->UpdateInfo(data);
323323
}
@@ -350,7 +350,7 @@ void ClientField::UpdateFieldCard(int controler, int location, unsigned char* da
350350
return;
351351
int len;
352352
for(auto cit = lst->begin(); cit != lst->end(); ++cit) {
353-
len = BufferIO::ReadInt32(data);
353+
len = BufferIO::Read<int32_t>(data);
354354
if(len > LEN_HEADER)
355355
(*cit)->UpdateInfo(data);
356356
data += len - 4;

gframe/deck_con.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -698,14 +698,14 @@ bool DeckBuilder::OnEvent(const irr::SEvent& event) {
698698
mainGame->ClearCardInfo();
699699
unsigned char deckbuf[1024];
700700
auto pdeck = deckbuf;
701-
BufferIO::WriteInt32(pdeck, deckManager.current_deck.main.size() + deckManager.current_deck.extra.size());
702-
BufferIO::WriteInt32(pdeck, deckManager.current_deck.side.size());
701+
BufferIO::Write<int32_t>(pdeck, deckManager.current_deck.main.size() + deckManager.current_deck.extra.size());
702+
BufferIO::Write<int32_t>(pdeck, deckManager.current_deck.side.size());
703703
for(size_t i = 0; i < deckManager.current_deck.main.size(); ++i)
704-
BufferIO::WriteInt32(pdeck, deckManager.current_deck.main[i]->first);
704+
BufferIO::Write<int32_t>(pdeck, deckManager.current_deck.main[i]->first);
705705
for(size_t i = 0; i < deckManager.current_deck.extra.size(); ++i)
706-
BufferIO::WriteInt32(pdeck, deckManager.current_deck.extra[i]->first);
706+
BufferIO::Write<int32_t>(pdeck, deckManager.current_deck.extra[i]->first);
707707
for(size_t i = 0; i < deckManager.current_deck.side.size(); ++i)
708-
BufferIO::WriteInt32(pdeck, deckManager.current_deck.side[i]->first);
708+
BufferIO::Write<int32_t>(pdeck, deckManager.current_deck.side[i]->first);
709709
DuelClient::SendBufferToServer(CTOS_UPDATE_DECK, deckbuf, pdeck - deckbuf);
710710
break;
711711
}

0 commit comments

Comments
 (0)