Commit 24e5ca92 authored by mercury233's avatar mercury233

update loadDB

parent ae3da991
...@@ -9,20 +9,33 @@ byte DataManager::scriptBuffer[0x20000]; ...@@ -9,20 +9,33 @@ byte DataManager::scriptBuffer[0x20000];
DataManager dataManager; DataManager dataManager;
bool DataManager::LoadDB(const char* file) { bool DataManager::LoadDB(const char* file) {
sqlite3* pDB; wchar_t wfile[256];
if(sqlite3_open_v2(file, &pDB, SQLITE_OPEN_READONLY, 0) != SQLITE_OK) BufferIO::DecodeUTF8(file, wfile);
return Error(pDB); IReadFile* reader = FileSystem->createAndOpenFile(wfile);
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; 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(pDB); return Error(&db);
CardDataC cd; CardDataC cd;
CardString cs; CardString cs;
int step = 0; int step = 0;
do { do {
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(pDB, pStmt); return Error(&db, 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);
...@@ -62,7 +75,8 @@ bool DataManager::LoadDB(const char* file) { ...@@ -62,7 +75,8 @@ bool DataManager::LoadDB(const char* file) {
} }
} while(step != SQLITE_DONE); } while(step != SQLITE_DONE);
sqlite3_finalize(pStmt); sqlite3_finalize(pStmt);
sqlite3_close(pDB); spmemvfs_close_db(&db);
spmemvfs_env_fini();
return true; return true;
} }
bool DataManager::LoadStrings(const char* file) { bool DataManager::LoadStrings(const char* file) {
...@@ -99,11 +113,12 @@ bool DataManager::LoadStrings(const char* file) { ...@@ -99,11 +113,12 @@ bool DataManager::LoadStrings(const char* file) {
myswprintf(numStrings[i], L"%d", i); myswprintf(numStrings[i], L"%d", i);
return true; return true;
} }
bool DataManager::Error(sqlite3* pDB, sqlite3_stmt* pStmt) { bool DataManager::Error(spmemvfs_db_t* pDB, sqlite3_stmt* pStmt) {
BufferIO::DecodeUTF8(sqlite3_errmsg(pDB), strBuffer); BufferIO::DecodeUTF8(sqlite3_errmsg(pDB->handle), strBuffer);
if(pStmt) if(pStmt)
sqlite3_finalize(pStmt); sqlite3_finalize(pStmt);
sqlite3_close(pDB); spmemvfs_close_db(pDB);
spmemvfs_env_fini();
return false; return false;
} }
bool DataManager::GetData(int code, CardData* pData) { bool DataManager::GetData(int code, CardData* pData) {
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "config.h" #include "config.h"
#include "sqlite3.h" #include "sqlite3.h"
#include "spmemvfs.h"
#include "client_card.h" #include "client_card.h"
#include <unordered_map> #include <unordered_map>
...@@ -13,7 +14,7 @@ public: ...@@ -13,7 +14,7 @@ public:
DataManager(): _datas(8192), _strings(8192) {} DataManager(): _datas(8192), _strings(8192) {}
bool LoadDB(const char* file); bool LoadDB(const char* file);
bool LoadStrings(const char* file); bool LoadStrings(const char* file);
bool Error(sqlite3* pDB, sqlite3_stmt* pStmt = 0); bool Error(spmemvfs_db_t* pDB, sqlite3_stmt* pStmt = 0);
bool GetData(int code, CardData* pData); bool GetData(int code, CardData* pData);
code_pointer GetCodePointer(int code); code_pointer GetCodePointer(int code);
bool GetString(int code, CardString* pStr); bool GetString(int code, CardString* pStr);
...@@ -54,7 +55,7 @@ public: ...@@ -54,7 +55,7 @@ public:
static int CardReader(int, void*); static int CardReader(int, void*);
static byte* ScriptReaderEx(const char* script_name, int* slen); static byte* ScriptReaderEx(const char* script_name, int* slen);
static byte* ScriptReader(const char* script_name, int* slen); static byte* ScriptReader(const char* script_name, int* slen);
IFileSystem* FileSystem;
}; };
extern DataManager dataManager; extern DataManager dataManager;
......
...@@ -56,6 +56,7 @@ bool Game::Initialize() { ...@@ -56,6 +56,7 @@ bool Game::Initialize() {
ErrorLog("Failed to load textures!"); ErrorLog("Failed to load textures!");
return false; return false;
} }
dataManager.FileSystem = device->getFileSystem();
LoadExpansionDB(); LoadExpansionDB();
if(!dataManager.LoadDB("cards.cdb")) { if(!dataManager.LoadDB("cards.cdb")) {
ErrorLog("Failed to load card database (cards.cdb)!"); ErrorLog("Failed to load card database (cards.cdb)!");
......
This diff is collapsed.
/*
* BSD 2-Clause License
*
* Copyright 2009 Stephen Liu
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __spmemvfs_h__
#define __spmemvfs_h__
#ifdef __cplusplus
extern "C" {
#endif
#include "sqlite3.h"
#define SPMEMVFS_NAME "spmemvfs"
typedef struct spmembuffer_t {
char * data;
int used;
int total;
} spmembuffer_t;
typedef struct spmemvfs_db_t {
sqlite3 * handle;
spmembuffer_t * mem;
} spmemvfs_db_t;
int spmemvfs_env_init();
void spmemvfs_env_fini();
int spmemvfs_open_db( spmemvfs_db_t * db, const char * path, spmembuffer_t * mem );
int spmemvfs_close_db( spmemvfs_db_t * db );
#ifdef __cplusplus
}
#endif
#endif
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