-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.cpp
301 lines (275 loc) · 7.71 KB
/
database.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
Copyright (C) 2025 Flyinghead
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/>.
*/
#include "database.h"
#include "common.h"
#include <sqlite3.h>
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <algorithm>
static std::string databasePath = "iwango.db";
void setDatabasePath(const std::string& databasePath) {
if (!databasePath.empty())
::databasePath = databasePath;
}
static void throwSqlError(sqlite3 *db)
{
if (sqlite3_extended_errcode(db) == SQLITE_CONSTRAINT_UNIQUE)
throw AlreadyExistsException();
const char *msg = sqlite3_errmsg(db);
if (msg != nullptr)
throw std::runtime_error(msg);
else
throw std::runtime_error("SQL Error");
}
class Database
{
public:
Database()
{
if (sqlite3_open(databasePath.c_str(), &db) != SQLITE_OK)
{
std::string what = "Can't open database " + databasePath + std::string(": ") + sqlite3_errmsg(db);
sqlite3_close(db);
throw std::runtime_error(what);
}
sqlite3_busy_timeout(db, 1000);
/*
sqlite3_trace_v2(db, SQLITE_TRACE_STMT, [](unsigned t, void *c, void *p, void *x) -> int {
if (x == nullptr)
return 0;
char *stmt = (char *)x;
if ((stmt[0] == '-' && stmt[1] == '-') || p == nullptr)
printf("sql: %s\n", stmt);
else
{
stmt = sqlite3_expanded_sql((sqlite3_stmt *)p);
printf("sql: %s\n", stmt);
sqlite3_free(stmt);
}
return 0;
}, nullptr);
*/
}
Database(const Database&) = delete;
Database& operator=(const Database&) = delete;
~Database() {
sqlite3_close(db);
}
void exec(const std::string& stmt) {
if (sqlite3_exec(db, stmt.c_str(), nullptr, 0, nullptr) != SQLITE_OK)
throwSqlError(db);
}
public:
sqlite3 *db;
friend class Statement;
};
class Statement
{
public:
Statement(Database& database, const char *sql) : db(database.db) {
if (sqlite3_prepare_v2(db, sql, -1, &stmt, 0) != SQLITE_OK)
throwSqlError(db);
}
Statement(const Statement&) = delete;
Statement& operator=(const Statement&) = delete;
~Statement() {
if (stmt != nullptr)
sqlite3_finalize(stmt);
}
void bind(int idx, int v) {
if (sqlite3_bind_int(stmt, idx, v) != SQLITE_OK)
throwSqlError(db);
}
void bind(int idx, const std::string& s) {
if (sqlite3_bind_text(stmt, idx, s.c_str(), s.length(), SQLITE_TRANSIENT) != SQLITE_OK)
throwSqlError(db);
}
void bind(int idx, const uint8_t *data, size_t len) {
if (sqlite3_bind_blob(stmt, idx, data, len, SQLITE_STATIC) != SQLITE_OK)
throwSqlError(db);
}
bool step()
{
int rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW)
return true;
if (rc != SQLITE_DONE && rc != SQLITE_OK)
throwSqlError(db);
return false;
}
int getIntColumn(int idx) {
return sqlite3_column_int(stmt, idx);
}
std::string getStringColumn(int idx) {
return std::string((const char *)sqlite3_column_text(stmt, idx));
}
std::vector<uint8_t> getBlobColumn(int idx)
{
std::vector<uint8_t> blob;
blob.resize(sqlite3_column_bytes(stmt, idx));
if (!blob.empty())
memcpy(blob.data(), sqlite3_column_blob(stmt, idx), blob.size());
return blob;
}
int changedRows() {
return sqlite3_changes(db);
}
private:
sqlite3 *db;
sqlite3_stmt *stmt = nullptr;
};
//
// Gate server
//
bool createHandle(GameId gameId, const std::string& user, int index, const std::string& handle)
{
try {
Database db;
Statement stmt(db, "INSERT INTO USER_HANDLE (USER_NAME, GAME, HANDLE_INDEX, HANDLE) VALUES (?, ?, ?, ?)");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
stmt.bind(3, index);
stmt.bind(4, handle);
stmt.step();
return true;
} catch (const AlreadyExistsException& e) {
throw e;
} catch (const std::runtime_error& e) {
fprintf(stderr, "ERROR: createHandle: %s\n", e.what());
return false;
}
}
bool replaceHandle(GameId gameId, const std::string& user, int index, const std::string& handle)
{
try {
Database db;
Statement stmt(db, "UPDATE USER_HANDLE SET HANDLE = ? WHERE USER_NAME = ? AND GAME = ? AND HANDLE_INDEX = ?");
stmt.bind(1, handle);
stmt.bind(2, user);
stmt.bind(3, (int)gameId);
stmt.bind(4, index);
stmt.step();
return true;
} catch (const AlreadyExistsException& e) {
throw e;
} catch (const std::runtime_error& e) {
fprintf(stderr, "ERROR: replaceHandle: %s\n", e.what());
return false;
}
}
bool deleteHandle(GameId gameId, const std::string& user, int index)
{
try {
Database db;
db.exec("BEGIN TRANSACTION");
{
Statement stmt(db, "DELETE FROM USER_HANDLE WHERE USER_NAME = ? AND GAME = ? AND HANDLE_INDEX = ?");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
stmt.bind(3, index);
stmt.step();
}
for (int i = index + 1; i < 8; i++)
{
Statement stmt(db, "UPDATE USER_HANDLE SET HANDLE_INDEX = HANDLE_INDEX - 1 WHERE USER_NAME = ? AND GAME = ? AND HANDLE_INDEX = ?");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
stmt.bind(3, i);
stmt.step();
if (stmt.changedRows() == 0)
break;
}
db.exec("COMMIT");
return true;
} catch (const std::runtime_error& e) {
fprintf(stderr, "ERROR: deleteHandle: %s\n", e.what());
return false;
}
}
std::vector<std::string> getHandles(GameId gameId, const std::string& user, const std::string& defaultHandle)
{
std::vector<std::string> handles;
try {
Database db;
Statement stmt(db, "SELECT HANDLE FROM USER_HANDLE WHERE USER_NAME = ? AND GAME = ? ORDER BY HANDLE_INDEX");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
while (stmt.step())
handles.push_back(stmt.getStringColumn(0));
if (handles.empty() && !defaultHandle.empty()) {
try {
if (createHandle(gameId, user, 0, defaultHandle))
handles.push_back(defaultHandle);
} catch (const AlreadyExistsException&) {
}
}
} catch (const std::runtime_error& e) {
fprintf(stderr, "ERROR: getHandles: %s\n", e.what());
}
return handles;
}
//
// Lobby server
//
void updateExtraUserMem(GameId gameId, const std::string& user, const uint8_t *data, int offset, int size)
{
try {
Database db;
std::vector<uint8_t> blob;
bool newBlob = false;
Statement stmt(db, "SELECT EXTRAMEM FROM USER_EXTRAMEM WHERE USER_NAME = ? AND GAME = ?");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
if (stmt.step())
blob = stmt.getBlobColumn(0);
else
newBlob = true;
if ((int)blob.size() < offset + size)
blob.resize(offset + size);
memcpy(blob.data() + offset, data, size);
if (newBlob)
{
Statement stmt(db, "INSERT INTO USER_EXTRAMEM (USER_NAME, GAME, EXTRAMEM) VALUES (?, ?, ?)");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
stmt.bind(3, data, size);
stmt.step();
}
else
{
Statement stmt(db, "UPDATE USER_EXTRAMEM SET EXTRAMEM = ? WHERE USER_NAME = ? AND GAME = ?");
stmt.bind(1, data, size);
stmt.bind(2, user);
stmt.bind(3, (int)gameId);
stmt.step();
}
} catch (const std::runtime_error& e) {
fprintf(stderr, "ERROR: updateExtraUserMem: %s\n", e.what());
}
}
std::vector<uint8_t> getExtraUserMem(GameId gameId, const std::string& user)
{
try {
Database db;
Statement stmt(db, "SELECT EXTRAMEM FROM USER_EXTRAMEM WHERE USER_NAME = ? AND GAME = ?");
stmt.bind(1, user);
stmt.bind(2, (int)gameId);
if (stmt.step())
return stmt.getBlobColumn(0);
} catch (const std::runtime_error& e) {
fprintf(stderr, "ERROR: getExtraUserMem: %s\n", e.what());
}
return {};
}