forked from Fluorohydride/ygopro
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_manager.cpp
More file actions
513 lines (508 loc) · 16.5 KB
/
Copy pathdata_manager.cpp
File metadata and controls
513 lines (508 loc) · 16.5 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#include "data_manager.h"
#include "game.h"
#include "spmemvfs/spmemvfs.h"
namespace ygo {
const wchar_t* DataManager::unknown_string = L"???";
unsigned char DataManager::scriptBuffer[0x100000] = {};
irr::io::IFileSystem* DataManager::FileSystem = nullptr;
DataManager dataManager;
DataManager::DataManager() : _datas(32768), _strings(32768) {
extra_setcode = { {8512558u, {0x8f, 0x54, 0x59, 0x82, 0x13a}}, };
}
bool DataManager::ReadDB(sqlite3* pDB) {
sqlite3_stmt* pStmt = nullptr;
const char* sql = "select * from datas,texts where datas.id=texts.id";
if (sqlite3_prepare_v2(pDB, sql, -1, &pStmt, 0) != SQLITE_OK)
return Error(pDB, pStmt);
wchar_t strBuffer[4096];
int step = 0;
do {
CardDataC cd;
CardString cs;
step = sqlite3_step(pStmt);
if (step == SQLITE_ROW) {
cd.code = sqlite3_column_int(pStmt, 0);
cd.ot = sqlite3_column_int(pStmt, 1);
cd.alias = sqlite3_column_int(pStmt, 2);
uint64_t setcode = static_cast<uint64_t>(sqlite3_column_int64(pStmt, 3));
if (setcode) {
auto it = extra_setcode.find(cd.code);
if (it != extra_setcode.end()) {
int len = it->second.size();
if (len > SIZE_SETCODE)
len = SIZE_SETCODE;
if (len)
std::memcpy(cd.setcode, it->second.data(), len * sizeof(uint16_t));
}
else
cd.set_setcode(setcode);
}
cd.type = static_cast<decltype(cd.type)>(sqlite3_column_int64(pStmt, 4));
cd.attack = sqlite3_column_int(pStmt, 5);
cd.defense = sqlite3_column_int(pStmt, 6);
if (cd.type & TYPE_LINK) {
cd.link_marker = cd.defense;
cd.defense = 0;
}
else
cd.link_marker = 0;
uint32_t level = static_cast<uint32_t>(sqlite3_column_int(pStmt, 7));
cd.level = level & 0xff;
cd.lscale = (level >> 24) & 0xff;
cd.rscale = (level >> 16) & 0xff;
cd.race = static_cast<decltype(cd.race)>(sqlite3_column_int64(pStmt, 8));
cd.attribute = static_cast<decltype(cd.attribute)>(sqlite3_column_int64(pStmt, 9));
cd.category = static_cast<decltype(cd.category)>(sqlite3_column_int64(pStmt, 10));
_datas[cd.code] = cd;
if (const char* text = (const char*)sqlite3_column_text(pStmt, 12)) {
BufferIO::DecodeUTF8(text, strBuffer);
cs.name = strBuffer;
}
if (const char* text = (const char*)sqlite3_column_text(pStmt, 13)) {
BufferIO::DecodeUTF8(text, strBuffer);
cs.text = strBuffer;
}
constexpr int desc_count = sizeof cs.desc / sizeof cs.desc[0];
for (int i = 0; i < desc_count; ++i) {
if (const char* text = (const char*)sqlite3_column_text(pStmt, i + 14)) {
BufferIO::DecodeUTF8(text, strBuffer);
cs.desc[i] = strBuffer;
}
}
_strings[cd.code] = cs;
}
else if (step != SQLITE_DONE)
return Error(pDB, pStmt);
} while (step == SQLITE_ROW);
sqlite3_finalize(pStmt);
return true;
}
bool DataManager::LoadDB(const wchar_t* wfile) {
char file[256];
BufferIO::EncodeUTF8(wfile, file);
#ifdef _WIN32
auto reader = FileSystem->createAndOpenFile(wfile);
#else
auto reader = FileSystem->createAndOpenFile(file);
#endif
if(reader == nullptr)
return false;
spmemvfs_db_t db;
spmembuffer_t* mem = (spmembuffer_t*)std::calloc(sizeof(spmembuffer_t), 1);
spmemvfs_env_init();
mem->total = mem->used = reader->getSize();
mem->data = (char*)std::malloc(mem->total + 1);
reader->read(mem->data, mem->total);
reader->drop();
(mem->data)[mem->total] = '\0';
bool ret{};
if (spmemvfs_open_db(&db, file, mem) != SQLITE_OK)
ret = Error(db.handle);
else
ret = ReadDB(db.handle);
spmemvfs_close_db(&db);
spmemvfs_env_fini();
return ret;
}
bool DataManager::LoadStrings(const char* file) {
FILE* fp = myfopen(file, "r");
if(!fp)
return false;
char linebuf[TEXT_LINE_SIZE]{};
while(std::fgets(linebuf, sizeof linebuf, fp)) {
ReadStringConfLine(linebuf);
}
std::fclose(fp);
return true;
}
bool DataManager::LoadStrings(irr::io::IReadFile* reader) {
char ch{};
std::string linebuf;
while (reader->read(&ch, 1)) {
if (ch == '\0')
break;
linebuf.push_back(ch);
if (ch == '\n' || linebuf.size() >= TEXT_LINE_SIZE - 1) {
ReadStringConfLine(linebuf.data());
linebuf.clear();
}
}
reader->drop();
return true;
}
void DataManager::ReadStringConfLine(const char* linebuf) {
if(linebuf[0] != '!')
return;
char strbuf[TEXT_LINE_SIZE]{};
int value{};
wchar_t strBuffer[4096]{};
if (std::sscanf(linebuf, "!%63s", strbuf) != 1)
return;
if(!std::strcmp(strbuf, "system")) {
if (std::sscanf(&linebuf[7], "%d %240[^\n]", &value, strbuf) != 2)
return;
BufferIO::DecodeUTF8(strbuf, strBuffer);
_sysStrings[value] = strBuffer;
} else if(!std::strcmp(strbuf, "victory")) {
if (std::sscanf(&linebuf[8], "%x %240[^\n]", &value, strbuf) != 2)
return;
BufferIO::DecodeUTF8(strbuf, strBuffer);
_victoryStrings[value] = strBuffer;
} else if(!std::strcmp(strbuf, "counter")) {
if (std::sscanf(&linebuf[8], "%x %240[^\n]", &value, strbuf) != 2)
return;
BufferIO::DecodeUTF8(strbuf, strBuffer);
_counterStrings[value] = strBuffer;
} else if(!std::strcmp(strbuf, "setname")) {
//using tab for comment
if (std::sscanf(&linebuf[8], "%x %240[^\t\n]", &value, strbuf) != 2)
return;
BufferIO::DecodeUTF8(strbuf, strBuffer);
_setnameStrings[value] = strBuffer;
}
}
bool DataManager::Error(sqlite3* pDB, sqlite3_stmt* pStmt) {
std::strncpy(errmsg, sqlite3_errmsg(pDB), sizeof errmsg - 1);
errmsg[sizeof errmsg - 1] = 0;
if(pStmt)
sqlite3_finalize(pStmt);
return false;
}
code_pointer DataManager::GetCodePointer(unsigned int code) const {
return _datas.find(code);
}
string_pointer DataManager::GetStringPointer(unsigned int code) const {
return _strings.find(code);
}
code_pointer DataManager::datas_begin() const {
return _datas.cbegin();
}
code_pointer DataManager::datas_end() const {
return _datas.cend();
}
string_pointer DataManager::strings_begin() const {
return _strings.cbegin();
}
string_pointer DataManager::strings_end() const {
return _strings.cend();
}
bool DataManager::GetData(unsigned int code, CardData* pData) const {
auto cdit = _datas.find(code);
if(cdit == _datas.end())
return false;
if (pData) {
*pData = cdit->second;
}
return true;
}
bool DataManager::GetString(unsigned int code, CardString* pStr) const {
auto csit = _strings.find(code);
if(csit == _strings.end()) {
pStr->name = unknown_string;
pStr->text = unknown_string;
return false;
}
*pStr = csit->second;
return true;
}
const wchar_t* DataManager::GetName(unsigned int code) const {
auto csit = _strings.find(code);
if(csit == _strings.end())
return unknown_string;
if(!csit->second.name.empty())
return csit->second.name.c_str();
return unknown_string;
}
const wchar_t* DataManager::GetText(unsigned int code) const {
auto csit = _strings.find(code);
if(csit == _strings.end())
return unknown_string;
if(!csit->second.text.empty())
return csit->second.text.c_str();
return unknown_string;
}
const wchar_t* DataManager::GetDesc(unsigned int strCode) const {
if (strCode < (MIN_CARD_ID << 4))
return GetSysString(strCode);
unsigned int code = (strCode >> 4) & 0x0fffffff;
unsigned int offset = strCode & 0xf;
auto csit = _strings.find(code);
if(csit == _strings.end())
return unknown_string;
if(!csit->second.desc[offset].empty())
return csit->second.desc[offset].c_str();
return unknown_string;
}
const wchar_t* DataManager::GetSysString(int code) const {
if (code < 0 || code > MAX_STRING_ID)
return unknown_string;
auto csit = _sysStrings.find(code);
if(csit == _sysStrings.end())
return unknown_string;
return csit->second.c_str();
}
const wchar_t* DataManager::GetVictoryString(int code) const {
auto csit = _victoryStrings.find(code);
if(csit == _victoryStrings.end())
return unknown_string;
return csit->second.c_str();
}
const wchar_t* DataManager::GetCounterName(int code) const {
auto csit = _counterStrings.find(code);
if(csit == _counterStrings.end())
return unknown_string;
return csit->second.c_str();
}
const wchar_t* DataManager::GetSetName(int code) const {
auto csit = _setnameStrings.find(code);
if(csit == _setnameStrings.end())
return unknown_string;
return csit->second.c_str();
}
std::vector<unsigned int> DataManager::GetSetCodes(std::wstring setname) const {
std::vector<unsigned int> matchingCodes;
for(auto csit = _setnameStrings.begin(); csit != _setnameStrings.end(); ++csit) {
auto xpos = csit->second.find_first_of(L'|');//setname|another setname or extra info
if(setname.size() < 2) {
if(csit->second.compare(0, xpos, setname) == 0
|| csit->second.compare(xpos + 1, csit->second.length(), setname) == 0)
matchingCodes.push_back(csit->first);
} else {
if(csit->second.substr(0, xpos).find(setname) != std::wstring::npos
|| csit->second.substr(xpos + 1).find(setname) != std::wstring::npos) {
matchingCodes.push_back(csit->first);
}
}
}
return matchingCodes;
}
std::wstring DataManager::GetNumString(int num, bool bracket) const {
if(!bracket)
return std::to_wstring(num);
std::wstring numBuffer{ L"(" };
numBuffer.append(std::to_wstring(num));
numBuffer.push_back(L')');
return numBuffer;
}
const wchar_t* DataManager::FormatLocation(int location, int sequence) const {
if(location == LOCATION_SZONE) {
if(sequence < 5)
return GetSysString(1003);
else if(sequence == 5)
return GetSysString(1008);
else
return GetSysString(1009);
}
int i = 1000;
int string_id = 0;
for (unsigned filter = LOCATION_DECK; filter <= LOCATION_PZONE; filter <<= 1, ++i) {
if (filter == location) {
string_id = i;
break;
}
}
if (string_id)
return GetSysString(string_id);
else
return unknown_string;
}
std::wstring DataManager::FormatAttribute(unsigned int attribute) const {
std::wstring buffer;
for (int i = 0; i < ATTRIBUTES_COUNT; ++i) {
if (attribute & (0x1U << i)) {
if (!buffer.empty())
buffer.push_back(L'|');
buffer.append(GetSysString(1010 + i));
}
}
if (buffer.empty())
return std::wstring(unknown_string);
return buffer;
}
std::wstring DataManager::FormatRace(unsigned int race) const {
std::wstring buffer;
for(int i = 0; i < RACES_COUNT; ++i) {
if(race & (0x1U << i)) {
if (!buffer.empty())
buffer.push_back(L'|');
buffer.append(GetSysString(1020 + i));
}
}
if (buffer.empty())
return std::wstring(unknown_string);
return buffer;
}
std::wstring DataManager::FormatType(unsigned int type) const {
std::wstring buffer;
int i = 1050;
for (unsigned filter = TYPE_MONSTER; filter <= TYPE_LINK; filter <<= 1, ++i) {
if (type & filter) {
if (!buffer.empty())
buffer.push_back(L'|');
buffer.append(GetSysString(i));
}
}
if (buffer.empty())
return std::wstring(unknown_string);
return buffer;
}
std::wstring DataManager::FormatSetName(const uint16_t setcode[]) const {
std::wstring buffer;
for(int i = 0; i < 10; ++i) {
if (!setcode[i])
break;
const wchar_t* setname = GetSetName(setcode[i]);
if (!buffer.empty())
buffer.push_back(L'|');
buffer.append(setname);
}
if (buffer.empty())
return std::wstring(unknown_string);
return buffer;
}
std::wstring DataManager::FormatLinkMarker(unsigned int link_marker) const {
std::wstring buffer;
if (link_marker & LINK_MARKER_TOP_LEFT)
buffer.append(L"[\u2196]");
if (link_marker & LINK_MARKER_TOP)
buffer.append(L"[\u2191]");
if (link_marker & LINK_MARKER_TOP_RIGHT)
buffer.append(L"[\u2197]");
if (link_marker & LINK_MARKER_LEFT)
buffer.append(L"[\u2190]");
if (link_marker & LINK_MARKER_RIGHT)
buffer.append(L"[\u2192]");
if (link_marker & LINK_MARKER_BOTTOM_LEFT)
buffer.append(L"[\u2199]");
if (link_marker & LINK_MARKER_BOTTOM)
buffer.append(L"[\u2193]");
if (link_marker & LINK_MARKER_BOTTOM_RIGHT)
buffer.append(L"[\u2198]");
return buffer;
}
uint32_t DataManager::CardReader(uint32_t code, card_data* pData) {
if (!dataManager.GetData(code, pData))
pData->clear();
return 0;
}
unsigned char* DataManager::ScriptReaderEx(const char* script_path, int* slen) {
// default script name: ./script/c%d.lua
if (std::strncmp(script_path, "./script", 8) != 0) // not a card script file
return ReadScriptFromFile(script_path, slen);
const char* script_name = script_path + 2;
char expansions_path[1024]{};
std::snprintf(expansions_path, sizeof expansions_path, "./expansions/%s", script_name);
if (mainGame->gameConf.prefer_expansion_script) { // debug script with raw file in expansions
if (ReadScriptFromFile(expansions_path, slen))
return scriptBuffer;
if (ReadScriptFromIrrFS(script_name, slen))
return scriptBuffer;
if (ReadScriptFromFile(script_path, slen))
return scriptBuffer;
} else {
if (ReadScriptFromIrrFS(script_name, slen))
return scriptBuffer;
if (ReadScriptFromFile(script_path, slen))
return scriptBuffer;
if (ReadScriptFromFile(expansions_path, slen))
return scriptBuffer;
}
return nullptr;
}
unsigned char* DataManager::ReadScriptFromIrrFS(const char* script_name, int* slen) {
#ifdef _WIN32
wchar_t fname[256]{};
BufferIO::DecodeUTF8(script_name, fname);
auto reader = FileSystem->createAndOpenFile(fname);
#else
auto reader = FileSystem->createAndOpenFile(script_name);
#endif
if (!reader)
return nullptr;
int size = reader->read(scriptBuffer, sizeof scriptBuffer);
reader->drop();
if (size >= (int)sizeof scriptBuffer)
return nullptr;
*slen = size;
return scriptBuffer;
}
unsigned char* DataManager::ReadScriptFromFile(const char* script_name, int* slen) {
FILE* fp = myfopen(script_name, "rb");
if (!fp)
return nullptr;
size_t len = std::fread(scriptBuffer, 1, sizeof scriptBuffer, fp);
std::fclose(fp);
if (len >= sizeof scriptBuffer)
return nullptr;
*slen = (int)len;
return scriptBuffer;
}
bool DataManager::deck_sort_lv(code_pointer p1, code_pointer p2) {
if ((p1->second.type & 0x7) != (p2->second.type & 0x7))
return (p1->second.type & 0x7) < (p2->second.type & 0x7);
if ((p1->second.type & 0x7) == 1) {
int type1 = (p1->second.type & 0x48020c0) ? (p1->second.type & 0x48020c1) : (p1->second.type & 0x31);
int type2 = (p2->second.type & 0x48020c0) ? (p2->second.type & 0x48020c1) : (p2->second.type & 0x31);
if (type1 != type2)
return type1 < type2;
if (p1->second.level != p2->second.level)
return p1->second.level > p2->second.level;
if (p1->second.attack != p2->second.attack)
return p1->second.attack > p2->second.attack;
if (p1->second.defense != p2->second.defense)
return p1->second.defense > p2->second.defense;
return p1->first < p2->first;
}
if ((p1->second.type & 0xfffffff8) != (p2->second.type & 0xfffffff8))
return (p1->second.type & 0xfffffff8) < (p2->second.type & 0xfffffff8);
return p1->first < p2->first;
}
bool DataManager::deck_sort_atk(code_pointer p1, code_pointer p2) {
if ((p1->second.type & 0x7) != (p2->second.type & 0x7))
return (p1->second.type & 0x7) < (p2->second.type & 0x7);
if ((p1->second.type & 0x7) == 1) {
if (p1->second.attack != p2->second.attack)
return p1->second.attack > p2->second.attack;
if (p1->second.defense != p2->second.defense)
return p1->second.defense > p2->second.defense;
if (p1->second.level != p2->second.level)
return p1->second.level > p2->second.level;
int type1 = (p1->second.type & 0x48020c0) ? (p1->second.type & 0x48020c1) : (p1->second.type & 0x31);
int type2 = (p2->second.type & 0x48020c0) ? (p2->second.type & 0x48020c1) : (p2->second.type & 0x31);
if (type1 != type2)
return type1 < type2;
return p1->first < p2->first;
}
if ((p1->second.type & 0xfffffff8) != (p2->second.type & 0xfffffff8))
return (p1->second.type & 0xfffffff8) < (p2->second.type & 0xfffffff8);
return p1->first < p2->first;
}
bool DataManager::deck_sort_def(code_pointer p1, code_pointer p2) {
if ((p1->second.type & 0x7) != (p2->second.type & 0x7))
return (p1->second.type & 0x7) < (p2->second.type & 0x7);
if ((p1->second.type & 0x7) == 1) {
if (p1->second.defense != p2->second.defense)
return p1->second.defense > p2->second.defense;
if (p1->second.attack != p2->second.attack)
return p1->second.attack > p2->second.attack;
if (p1->second.level != p2->second.level)
return p1->second.level > p2->second.level;
int type1 = (p1->second.type & 0x48020c0) ? (p1->second.type & 0x48020c1) : (p1->second.type & 0x31);
int type2 = (p2->second.type & 0x48020c0) ? (p2->second.type & 0x48020c1) : (p2->second.type & 0x31);
if (type1 != type2)
return type1 < type2;
return p1->first < p2->first;
}
if ((p1->second.type & 0xfffffff8) != (p2->second.type & 0xfffffff8))
return (p1->second.type & 0xfffffff8) < (p2->second.type & 0xfffffff8);
return p1->first < p2->first;
}
bool DataManager::deck_sort_name(code_pointer p1, code_pointer p2) {
const wchar_t* name1 = dataManager.GetName(p1->first);
const wchar_t* name2 = dataManager.GetName(p2->first);
int res = std::wcscmp(name1, name2);
if (res != 0)
return res < 0;
return p1->first < p2->first;
}
}