-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkage.h
More file actions
186 lines (167 loc) · 4.31 KB
/
kage.h
File metadata and controls
186 lines (167 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Kage game server.
Copyright 2019 Shuouma <dreamcast-talk.com>
Copyright 2025 Flyinghead <flyinghead.github@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include <string.h>
#include <stdexcept>
#include <arpa/inet.h>
extern std::string DataDir;
void dumpData(const uint8_t *data, size_t len);
enum class Game
{
None = -1,
Bomberman = 0,
Outtrigger = 1,
PropellerA = 2,
};
static inline uint16_t read16(const uint8_t *p, unsigned offset) {
return ntohs(*(const uint16_t *)&p[offset]);
}
static inline uint32_t read32(const uint8_t *p, unsigned offset) {
return ntohl(*(const uint32_t *)&p[offset]);
}
static inline void write16(uint8_t *p, unsigned offset, uint16_t v) {
*(uint16_t *)&p[offset] = htons(v);
}
static inline void write32(uint8_t *p, unsigned offset, uint32_t v) {
*(uint32_t *)&p[offset] = htonl(v);
}
class Packet
{
public:
enum Command : uint8_t {
REQ_BOOTSTRAP_LOGIN = 0x2c,
REQ_NOP = 0,
REQ_LOBBY_LOGIN = 1,
REQ_LOBBY_LOGOUT = 2,
REQ_CREATE_ROOM = 4,
REQ_JOIN_LOBBY_ROOM = 6,
REQ_LEAVE_LOBBY_ROOM = 7,
REQ_CHG_ROOM_STATUS = 8,
REG_QRY_ROOM_ATTR = 9,
REQ_QRY_USERS = 0xa,
REQ_QRY_ROOMS = 0xb,
REQ_CHG_USER_PROP = 0xc,
REQ_CHG_USER_STATUS = 0xd,
REQ_QRY_LOBBIES = 0xe,
REQ_CHAT = 0xf,
REQ_DM_CHAT = 0x10,
REQ_GAME_DATA = 0x11,
REQ_AUDIO_START = 0x12,
REQ_AUDIO_STOP = 0x13,
REQ_PING = 0x14,
RSP_TAG_CMD = 0x10,
RSP_OK = 0x28,
RSP_FAILED = 0x27,
RSP_LOGIN_SUCCESS2 = 0x29,
RSP_LOGIN_SUCCESS = 0x2d,
};
enum {
// from some debug code:
FLAG_RELAY = 0x400, // T-CH text chat
FLAG_CONTINUE = 0x800, // MULT
FLAG_LOBBY = 0x1000, // LB, GR
FLAG_UNKNOWN = 0x2000, // SNG single?
FLAG_ACK = 0x4000, // ACK
FLAG_RUDP = 0x8000, // RUDP
};
uint8_t data[0x800] {};
uint16_t size = 0x10;
uint16_t startOffset = 0;
uint16_t flags = FLAG_UNKNOWN;
Command type = REQ_NOP;
void reset()
{
startOffset = 0;
size = 0x10;
this->type = REQ_NOP;
memset(data, 0, sizeof(data));
flags = FLAG_UNKNOWN;
}
void init(Command type)
{
if (!empty()) {
finalize();
append(type);
}
else {
reset();
this->type = type;
}
}
void respOK(Command type) {
init(RSP_OK);
writeData((uint32_t)type);
}
void respFailed(Command type) {
init(RSP_FAILED);
writeData((uint32_t)type);
}
void writeData(uint32_t v) {
write32(data, size, v);
size += sizeof(v);
}
void writeData(uint16_t v) {
write16(data, size, v);
size += sizeof(v);
}
void writeData(uint8_t v) {
data[size] = v;
size += sizeof(v);
}
void writeData(const uint8_t *data, int size) {
memcpy(&this->data[this->size], data, size);
this->size += size;
}
void writeData(const char *str, int size)
{
strncpy((char *)&this->data[this->size], str, size);
int l = std::min<int>(size, strlen(str));
size -= l;
this->size += l;
memset(&this->data[this->size], 0, size);
this->size += size;
}
void ack(uint32_t seq) {
flags |= Packet::FLAG_ACK;
write32(data, startOffset + 0xc, seq);
}
size_t finalize()
{
const uint16_t chunkSize = size - startOffset;
if (chunkSize > 0x3ff)
throw std::runtime_error("Packet too big");
write16(data, startOffset, flags | chunkSize);
data[startOffset + 3] = type;
memcpy(&data[size], &KageToken, sizeof(KageToken));
return size + sizeof(KageToken);
}
void append(Command type)
{
if (startOffset == 0)
write16(data, 0, read16(data, 0) | FLAG_CONTINUE);
startOffset = size;
// reset kage token to 0
memset(&data[size], 0, sizeof(KageToken));
size += 0x10;
this->type = type;
flags = FLAG_UNKNOWN;
}
bool empty() const {
return size == 0x10 && flags == FLAG_UNKNOWN && type == REQ_NOP && startOffset == 0;
}
static constexpr uint32_t KageToken = 0x106647BA;
};