Commit cf135ac0 authored by Chen Bill's avatar Chen Bill Committed by GitHub

DataManager: add ReadDB (#2600)

parent 40d77767
...@@ -16,40 +16,20 @@ DataManager::DataManager() : _datas(16384), _strings(16384) { ...@@ -16,40 +16,20 @@ DataManager::DataManager() : _datas(16384), _strings(16384) {
strings_end = _strings.end(); strings_end = _strings.end();
extra_setcode = { {8512558u, {0x8f, 0x54, 0x59, 0x82, 0x13a}}, }; extra_setcode = { {8512558u, {0x8f, 0x54, 0x59, 0x82, 0x13a}}, };
} }
bool DataManager::LoadDB(const wchar_t* wfile) { bool DataManager::ReadDB(sqlite3* pDB) {
char file[256]; sqlite3_stmt* pStmt{};
BufferIO::EncodeUTF8(wfile, file);
#ifdef _WIN32
IReadFile* reader = FileSystem->createAndOpenFile(wfile);
#else
IReadFile* reader = FileSystem->createAndOpenFile(file);
#endif
if(reader == NULL)
return false;
spmemvfs_db_t db;
spmembuffer_t* mem = (spmembuffer_t*)calloc(sizeof(spmembuffer_t), 1);
spmemvfs_env_init();
mem->total = mem->used = reader->getSize();
mem->data = (char*)malloc(mem->total + 1);
reader->read(mem->data, mem->total);
reader->drop();
(mem->data)[mem->total] = '\0';
if(spmemvfs_open_db(&db, file, mem) != SQLITE_OK)
return Error(&db);
sqlite3* pDB = db.handle;
sqlite3_stmt* pStmt;
const char* sql = "select * from datas,texts where datas.id=texts.id"; const char* sql = "select * from datas,texts where datas.id=texts.id";
if(sqlite3_prepare_v2(pDB, sql, -1, &pStmt, 0) != SQLITE_OK) if (sqlite3_prepare_v2(pDB, sql, -1, &pStmt, 0) != SQLITE_OK)
return Error(&db); return Error(pDB);
wchar_t strBuffer[4096]; wchar_t strBuffer[4096];
int step = 0; int step = 0;
do { do {
CardDataC cd; CardDataC cd;
CardString cs; CardString cs;
step = sqlite3_step(pStmt); step = sqlite3_step(pStmt);
if(step == SQLITE_BUSY || step == SQLITE_ERROR || step == SQLITE_MISUSE) if (step == SQLITE_BUSY || step == SQLITE_ERROR || step == SQLITE_MISUSE)
return Error(&db, pStmt); return Error(pDB, pStmt);
else if(step == SQLITE_ROW) { else if (step == SQLITE_ROW) {
cd.code = sqlite3_column_int(pStmt, 0); cd.code = sqlite3_column_int(pStmt, 0);
cd.ot = sqlite3_column_int(pStmt, 1); cd.ot = sqlite3_column_int(pStmt, 1);
cd.alias = sqlite3_column_int(pStmt, 2); cd.alias = sqlite3_column_int(pStmt, 2);
...@@ -69,10 +49,11 @@ bool DataManager::LoadDB(const wchar_t* wfile) { ...@@ -69,10 +49,11 @@ bool DataManager::LoadDB(const wchar_t* wfile) {
cd.type = sqlite3_column_int(pStmt, 4); cd.type = sqlite3_column_int(pStmt, 4);
cd.attack = sqlite3_column_int(pStmt, 5); cd.attack = sqlite3_column_int(pStmt, 5);
cd.defense = sqlite3_column_int(pStmt, 6); cd.defense = sqlite3_column_int(pStmt, 6);
if(cd.type & TYPE_LINK) { if (cd.type & TYPE_LINK) {
cd.link_marker = cd.defense; cd.link_marker = cd.defense;
cd.defense = 0; cd.defense = 0;
} else }
else
cd.link_marker = 0; cd.link_marker = 0;
unsigned int level = sqlite3_column_int(pStmt, 7); unsigned int level = sqlite3_column_int(pStmt, 7);
cd.level = level & 0xff; cd.level = level & 0xff;
...@@ -82,32 +63,57 @@ bool DataManager::LoadDB(const wchar_t* wfile) { ...@@ -82,32 +63,57 @@ bool DataManager::LoadDB(const wchar_t* wfile) {
cd.attribute = sqlite3_column_int(pStmt, 9); cd.attribute = sqlite3_column_int(pStmt, 9);
cd.category = sqlite3_column_int(pStmt, 10); cd.category = sqlite3_column_int(pStmt, 10);
_datas[cd.code] = cd; _datas[cd.code] = cd;
if(const char* text = (const char*)sqlite3_column_text(pStmt, 12)) { if (const char* text = (const char*)sqlite3_column_text(pStmt, 12)) {
BufferIO::DecodeUTF8(text, strBuffer); BufferIO::DecodeUTF8(text, strBuffer);
cs.name = strBuffer; cs.name = strBuffer;
} }
if(const char* text = (const char*)sqlite3_column_text(pStmt, 13)) { if (const char* text = (const char*)sqlite3_column_text(pStmt, 13)) {
BufferIO::DecodeUTF8(text, strBuffer); BufferIO::DecodeUTF8(text, strBuffer);
cs.text = strBuffer; cs.text = strBuffer;
} }
for(int i = 0; i < 16; ++i) { for (int i = 0; i < 16; ++i) {
if(const char* text = (const char*)sqlite3_column_text(pStmt, i + 14)) { if (const char* text = (const char*)sqlite3_column_text(pStmt, i + 14)) {
BufferIO::DecodeUTF8(text, strBuffer); BufferIO::DecodeUTF8(text, strBuffer);
cs.desc[i] = strBuffer; cs.desc[i] = strBuffer;
} }
} }
_strings[cd.code] = cs; _strings[cd.code] = cs;
} }
} while(step != SQLITE_DONE); } while (step != SQLITE_DONE);
sqlite3_finalize(pStmt);
spmemvfs_close_db(&db);
spmemvfs_env_fini();
datas_begin = _datas.begin(); datas_begin = _datas.begin();
datas_end = _datas.end(); datas_end = _datas.end();
strings_begin = _strings.begin(); strings_begin = _strings.begin();
strings_end = _strings.end(); strings_end = _strings.end();
sqlite3_finalize(pStmt);
return true; return true;
} }
bool DataManager::LoadDB(const wchar_t* wfile) {
char file[256];
BufferIO::EncodeUTF8(wfile, file);
#ifdef _WIN32
IReadFile* reader = FileSystem->createAndOpenFile(wfile);
#else
IReadFile* reader = FileSystem->createAndOpenFile(file);
#endif
if(reader == NULL)
return false;
spmemvfs_db_t db;
spmembuffer_t* mem = (spmembuffer_t*)calloc(sizeof(spmembuffer_t), 1);
spmemvfs_env_init();
mem->total = mem->used = reader->getSize();
mem->data = (char*)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) { bool DataManager::LoadStrings(const char* file) {
FILE* fp = fopen(file, "r"); FILE* fp = fopen(file, "r");
if(!fp) if(!fp)
...@@ -167,13 +173,11 @@ void DataManager::ReadStringConfLine(const char* linebuf) { ...@@ -167,13 +173,11 @@ void DataManager::ReadStringConfLine(const char* linebuf) {
_setnameStrings[value] = strBuffer; _setnameStrings[value] = strBuffer;
} }
} }
bool DataManager::Error(spmemvfs_db_t* pDB, sqlite3_stmt* pStmt) { bool DataManager::Error(sqlite3* pDB, sqlite3_stmt* pStmt) {
wchar_t strBuffer[4096]; std::strncpy(errmsg, sqlite3_errmsg(pDB), sizeof errmsg);
BufferIO::DecodeUTF8(sqlite3_errmsg(pDB->handle), strBuffer); BufferIO::NullTerminate(errmsg);
if(pStmt) if(pStmt)
sqlite3_finalize(pStmt); sqlite3_finalize(pStmt);
spmemvfs_close_db(pDB);
spmemvfs_env_fini();
return false; return false;
} }
bool DataManager::GetData(unsigned int code, CardData* pData) const { bool DataManager::GetData(unsigned int code, CardData* pData) const {
......
...@@ -14,11 +14,13 @@ namespace ygo { ...@@ -14,11 +14,13 @@ namespace ygo {
class DataManager { class DataManager {
public: public:
DataManager(); DataManager();
bool ReadDB(sqlite3* pDB);
bool LoadDB(const wchar_t* wfile); bool LoadDB(const wchar_t* wfile);
bool LoadStrings(const char* file); bool LoadStrings(const char* file);
bool LoadStrings(IReadFile* reader); bool LoadStrings(IReadFile* reader);
void ReadStringConfLine(const char* linebuf); void ReadStringConfLine(const char* linebuf);
bool Error(spmemvfs_db_t* pDB, sqlite3_stmt* pStmt = 0); bool Error(sqlite3* pDB, sqlite3_stmt* pStmt = nullptr);
bool GetData(unsigned int code, CardData* pData) const; bool GetData(unsigned int code, CardData* pData) const;
code_pointer GetCodePointer(unsigned int code) const; code_pointer GetCodePointer(unsigned int code) const;
string_pointer GetStringPointer(unsigned int code) const; string_pointer GetStringPointer(unsigned int code) const;
...@@ -47,6 +49,7 @@ public: ...@@ -47,6 +49,7 @@ public:
code_pointer datas_end; code_pointer datas_end;
string_pointer strings_begin; string_pointer strings_begin;
string_pointer strings_end; string_pointer strings_end;
char errmsg[512]{};
wchar_t numStrings[301][4]{}; wchar_t numStrings[301][4]{};
wchar_t numBuffer[6]{}; wchar_t numBuffer[6]{};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment