Rewrote DB to use ORM, added all remaining features to LoginServer and refactored it

This commit is contained in:
kamilprzyb 2020-09-02 00:37:09 +02:00
parent 4f6c77be4f
commit 11801c1f89
18 changed files with 14108 additions and 23570 deletions

16
CMakeSettings.json Normal file
View File

@ -0,0 +1,16 @@
{
// Zobacz https://go.microsoft.com/fwlink/?linkid=834763, aby uzyskać więcej informacji o tym pliku.
"configurations": [
{
"name": "x64-Debug",
"generator": "Ninja",
"configurationType": "Debug",
"inheritEnvironments": [ "msvc_x64_x64" ],
"buildRoot": "${projectDir}\\out\\build\\${name}",
"installRoot": "${projectDir}\\out\\install\\${name}",
"cmakeCommandArgs": "",
"buildCommandArgs": "",
"ctestCommandArgs": ""
}
]
}

View File

@ -24,11 +24,8 @@ CSRC=\
src/contrib/bcrypt/crypt_blowfish.c\ src/contrib/bcrypt/crypt_blowfish.c\
src/contrib/bcrypt/crypt_gensalt.c\ src/contrib/bcrypt/crypt_gensalt.c\
src/contrib/bcrypt/wrapper.c\ src/contrib/bcrypt/wrapper.c\
src/contrib/sqlite/sqlite3.c\
CXXSRC=\ CXXSRC=\
src/contrib/sqlite/sqlite3pp.cpp\
src/contrib/sqlite/sqlite3ppext.cpp\
src/ChatManager.cpp\ src/ChatManager.cpp\
src/CombatManager.cpp\ src/CombatManager.cpp\
src/CNLoginServer.cpp\ src/CNLoginServer.cpp\
@ -50,18 +47,15 @@ CXXSRC=\
# headers (for timestamp purposes) # headers (for timestamp purposes)
CHDR=\ CHDR=\
src/contrib/sqlite/sqlite_orm.h\
src/contrib/bcrypt/bcrypt.h\ src/contrib/bcrypt/bcrypt.h\
src/contrib/bcrypt/crypt_blowfish.h\ src/contrib/bcrypt/crypt_blowfish.h\
src/contrib/bcrypt/crypt_gensalt.h\ src/contrib/bcrypt/crypt_gensalt.h\
src/contrib/bcrypt/ow-crypt.h\ src/contrib/bcrypt/ow-crypt.h\
src/contrib/bcrypt/winbcrypt.h\ src/contrib/bcrypt/winbcrypt.h\
src/contrib/sqlite/sqlite3.h\
src/contrib/sqlite/sqlite3ext.h\
CXXHDR=\ CXXHDR=\
src/contrib/bcrypt/BCrypt.hpp\ src/contrib/bcrypt/BCrypt.hpp\
src/contrib/sqlite/sqlite3pp.h\
src/contrib/sqlite/sqlite3ppext.h\
src/contrib/INIReader.hpp\ src/contrib/INIReader.hpp\
src/contrib/JSON.hpp\ src/contrib/JSON.hpp\
src/ChatManager.hpp\ src/ChatManager.hpp\

View File

@ -1,9 +1,12 @@
#include "CNLoginServer.hpp" #include "CNLoginServer.hpp"
#include "CNShared.hpp" #include "CNShared.hpp"
#include "CNStructs.hpp" #include "CNStructs.hpp"
#include "Database.hpp"
#include "PlayerManager.hpp"
#include <regex>
#include "contrib/bcrypt/BCrypt.hpp">
#include "settings.hpp" #include "settings.hpp"
#include "Database.hpp"
std::map<CNSocket*, CNLoginData> CNLoginServer::loginSessions; std::map<CNSocket*, CNLoginData> CNLoginServer::loginSessions;
@ -17,61 +20,85 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
printPacket(data, CL2LS); printPacket(data, CL2LS);
switch (data->type) { switch (data->type) {
case P_CL2LS_REQ_LOGIN: { case P_CL2LS_REQ_LOGIN: {
if (data->size != sizeof(sP_CL2LS_REQ_LOGIN)) if (data->size != sizeof(sP_CL2LS_REQ_LOGIN))
return; // ignore the malformed packet return; // ignore the malformed packet
sP_CL2LS_REQ_LOGIN* login = (sP_CL2LS_REQ_LOGIN*)data->buf; sP_CL2LS_REQ_LOGIN* login = (sP_CL2LS_REQ_LOGIN*)data->buf;
bool success = false; //TODO: implement better way of sending credentials
int errorCode = 0; std::string userLogin = U16toU8(login->szID);
std::string userLogin = U16toU8(login->szID); std::string userPassword = U16toU8(login->szPassword);
std::string userPassword = U16toU8(login->szPassword);
if (!Database::isLoginDataGood(userLogin, userPassword)) { bool success = false;
// failed regex int errorCode = 0;
errorCode = (int)LOGINERRORID::login_error;
} else if (!Database::doesUserExist(U16toU8(login->szID))) {
// if user does not exist in db, add him to and send succ
Database::addAccount(U16toU8(login->szID), U16toU8(login->szPassword));
success = true;
} else if (Database::isPasswordCorrect((U16toU8(login->szID)), U16toU8(login->szPassword))) {
// if user exists, check if password is correct
success = true;
} else {
errorCode = (int)LOGINERRORID::id_and_password_do_not_match;
}
if (success) { //checking regex
int userID = Database::getUserID(userLogin); if (!CNLoginServer::isLoginDataGood(userLogin, userPassword))
int charCount = Database::getUserSlotsNum(userID); {
errorCode = (int)LOGINERRORID::login_error;
}
else
{
std::unique_ptr<Database::Account> findUser = Database::findAccount(userLogin);
//if account not found, create it
if (findUser == nullptr)
{
loginSessions[sock] = CNLoginData();
loginSessions[sock].userID = Database::addAccount(userLogin, userPassword);
loginSessions[sock].slot = 1;
success = true;
}
//if user exists, check if password is correct
else if (CNLoginServer::isPasswordCorrect(findUser->Password, userPassword))
{
//check if account isn't currently in use
if (CNLoginServer::isAccountInUse(findUser->AccountID) ||
PlayerManager::isAccountInUse(findUser->AccountID))
{
errorCode = (int)LOGINERRORID::id_already_in_use;
}
//if not, login success
else
{
loginSessions[sock] = CNLoginData();
loginSessions[sock].userID = findUser->AccountID;
loginSessions[sock].slot = findUser->Selected;
success = true;
}
}
else
{
errorCode = (int)LOGINERRORID::id_and_password_do_not_match;
}
}
INITSTRUCT(sP_LS2CL_REP_LOGIN_SUCC, resp);
// set username in resp packet
memcpy(resp.szID, login->szID, sizeof(char16_t) * 33);
resp.iCharCount = charCount; if (success)
resp.iSlotNum = 1; {
resp.iPaymentFlag = 1; std::vector<Player> characters = Database::getCharacters(loginSessions[sock].userID);
resp.iOpenBetaFlag = 0; int charCount = characters.size();
resp.uiSvrTime = getTime();
// send the resp in with original key INITSTRUCT(sP_LS2CL_REP_LOGIN_SUCC, resp);
sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_SUCC, sizeof(sP_LS2CL_REP_LOGIN_SUCC)); // set username in resp packet
memcpy(resp.szID, login->szID, sizeof(char16_t) * 33);
// update keys resp.iCharCount = charCount;
sock->setEKey(CNSocketEncryption::createNewKey(resp.uiSvrTime, resp.iCharCount + 1, resp.iSlotNum + 1)); resp.iSlotNum = loginSessions[sock].slot;
sock->setFEKey(CNSocketEncryption::createNewKey((uint64_t)(*(uint64_t*)&CNSocketEncryption::defaultKey[0]), login->iClientVerC, 1)); resp.iPaymentFlag = 1;
resp.iOpenBetaFlag = 0;
resp.uiSvrTime = getTime();
loginSessions[sock] = CNLoginData(); // send the resp in with original key
loginSessions[sock].userID = userID; sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_SUCC, sizeof(sP_LS2CL_REP_LOGIN_SUCC));
// now send the characters :) // update keys
sock->setEKey(CNSocketEncryption::createNewKey(resp.uiSvrTime, resp.iCharCount + 1, resp.iSlotNum + 1));
sock->setFEKey(CNSocketEncryption::createNewKey((uint64_t)(*(uint64_t*)&CNSocketEncryption::defaultKey[0]), login->iClientVerC, 1));
if (charCount > 0) { // now send the characters :)
std::vector<Player>::iterator it;
std::list<Player> characters = Database::getCharacters(loginSessions[sock].userID); for (it = characters.begin(); it != characters.end(); it++)
std::list<Player>::iterator it; {
for (it = characters.begin(); it != characters.end(); it++) {
sP_LS2CL_REP_CHAR_INFO charInfo = sP_LS2CL_REP_CHAR_INFO(); sP_LS2CL_REP_CHAR_INFO charInfo = sP_LS2CL_REP_CHAR_INFO();
charInfo.iSlot = (int8_t)it->slot; charInfo.iSlot = (int8_t)it->slot;
@ -85,23 +112,14 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
charInfo.iZ = it->z; charInfo.iZ = it->z;
//save character in session (for char select) //save character in session (for char select)
int64_t UID = charInfo.sPC_Style.iPC_UID; int UID = it->iID;
loginSessions[sock].characters[UID] = Player(); loginSessions[sock].characters[UID] = Player(*it);
loginSessions[sock].characters[UID].level = charInfo.iLevel;
loginSessions[sock].characters[UID].slot = charInfo.iSlot;
loginSessions[sock].characters[UID].FEKey = sock->getFEKey(); loginSessions[sock].characters[UID].FEKey = sock->getFEKey();
loginSessions[sock].characters[UID].x = charInfo.iX;
loginSessions[sock].characters[UID].y = charInfo.iY;
loginSessions[sock].characters[UID].z = charInfo.iZ;
loginSessions[sock].characters[UID].PCStyle = charInfo.sPC_Style;
loginSessions[sock].characters[UID].PCStyle2 = charInfo.sPC_Style2;
loginSessions[sock].characters[UID].IsGM = false;
loginSessions[sock].characters[UID].money = 9001;
//temporary inventory stuff
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
//equip char creation clothes and lightning rifle //equip char creation clothes and lightning rifle
charInfo.aEquip[i] = it->Equip[i]; charInfo.aEquip[i] = it->Equip[i];
loginSessions[sock].characters[UID].Equip[i] = charInfo.aEquip[i];
} }
for (int i = 5; i < AEQUIP_COUNT; i++) { for (int i = 5; i < AEQUIP_COUNT; i++) {
@ -109,15 +127,6 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
charInfo.aEquip[i].iID = 0; charInfo.aEquip[i].iID = 0;
charInfo.aEquip[i].iType = i; charInfo.aEquip[i].iType = i;
charInfo.aEquip[i].iOpt = 0; charInfo.aEquip[i].iOpt = 0;
loginSessions[sock].characters[UID].Equip[i] = charInfo.aEquip[i];
}
for (int i = 0; i < AINVEN_COUNT; i++) {
// setup inventories
loginSessions[sock].characters[UID].Inven[i].iID = 0;
loginSessions[sock].characters[UID].Inven[i].iType = 0;
loginSessions[sock].characters[UID].Inven[i].iOpt = 0;
} }
// set default to the first character // set default to the first character
@ -127,226 +136,228 @@ void CNLoginServer::handlePacket(CNSocket* sock, CNPacketData* data) {
sock->sendPacket((void*)&charInfo, P_LS2CL_REP_CHAR_INFO, sizeof(sP_LS2CL_REP_CHAR_INFO)); sock->sendPacket((void*)&charInfo, P_LS2CL_REP_CHAR_INFO, sizeof(sP_LS2CL_REP_CHAR_INFO));
} }
} }
} else { //Failure
// failed else {
INITSTRUCT(sP_LS2CL_REP_LOGIN_FAIL, resp); INITSTRUCT(sP_LS2CL_REP_LOGIN_FAIL, resp);
memcpy(resp.szID, login->szID, sizeof(char16_t) * 33); memcpy(resp.szID, login->szID, sizeof(char16_t) * 33);
resp.iErrorCode = errorCode; resp.iErrorCode = errorCode;
sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_FAIL, sizeof(sP_LS2CL_REP_LOGIN_FAIL)); sock->sendPacket((void*)&resp, P_LS2CL_REP_LOGIN_FAIL, sizeof(sP_LS2CL_REP_LOGIN_FAIL));
}
break;
} }
break; case P_CL2LS_REP_LIVE_CHECK: {
} // stubbed, the client really doesn't care LOL
case P_CL2LS_REP_LIVE_CHECK: { break;
// stubbed, the client really doesn't care LOL }
break; case P_CL2LS_REQ_CHECK_CHAR_NAME: {
} if (data->size != sizeof(sP_CL2LS_REQ_CHECK_CHAR_NAME))
case P_CL2LS_REQ_CHECK_CHAR_NAME: { return;
if (data->size != sizeof(sP_CL2LS_REQ_CHECK_CHAR_NAME))
return;
// naughty words allowed!!!!!!!! (also for some reason, the client will always show 'Player 0' if you manually type a name. It will show up for other connected players though) // naughty words allowed!!!!!!!! (also for some reason, the client will always show 'Player 0' if you manually type a name. It will show up for other connected players though)
sP_CL2LS_REQ_CHECK_CHAR_NAME* nameCheck = (sP_CL2LS_REQ_CHECK_CHAR_NAME*)data->buf; sP_CL2LS_REQ_CHECK_CHAR_NAME* nameCheck = (sP_CL2LS_REQ_CHECK_CHAR_NAME*)data->buf;
//check if name is occupied //check if name is occupied
if (Database::isNameFree(U16toU8(nameCheck->szFirstName), U16toU8(nameCheck->szLastName))) if (Database::isNameFree(nameCheck))
{ {
// naughty words allowed!!!!!!!! (also for some reason, the client will always show 'Player + ID' if you manually type a name. It will show up for other connected players though)
// naughty words allowed!!!!!!!! (also for some reason, the client will always show 'Player + ID' if you manually type a name. It will show up for other connected players though) INITSTRUCT(sP_LS2CL_REP_CHECK_CHAR_NAME_SUCC, resp);
INITSTRUCT(sP_LS2CL_REP_CHECK_CHAR_NAME_SUCC, resp); DEBUGLOG(
DEBUGLOG(
std::cout << "P_CL2LS_REQ_CHECK_CHAR_NAME:" << std::endl; std::cout << "P_CL2LS_REQ_CHECK_CHAR_NAME:" << std::endl;
std::cout << "\tFirstName: " << U16toU8(nameCheck->szFirstName) << " LastName: " << U16toU8(nameCheck->szLastName) << std::endl; std::cout << "\tFirstName: " << U16toU8(nameCheck->szFirstName) << " LastName: " << U16toU8(nameCheck->szLastName) << std::endl;
) )
memcpy(resp.szFirstName, nameCheck->szFirstName, sizeof(char16_t) * 9); memcpy(resp.szFirstName, nameCheck->szFirstName, sizeof(char16_t) * 9);
memcpy(resp.szLastName, nameCheck->szLastName, sizeof(char16_t) * 17); memcpy(resp.szLastName, nameCheck->szLastName, sizeof(char16_t) * 17);
// fr*ck allowed!!!
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHECK_CHAR_NAME_SUCC, sizeof(sP_LS2CL_REP_CHECK_CHAR_NAME_SUCC));
}
else {
INITSTRUCT(sP_LS2CL_REP_CHECK_CHAR_NAME_FAIL, resp);
resp.iErrorCode = 1;
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHECK_CHAR_NAME_FAIL, sizeof(sP_LS2CL_REP_CHECK_CHAR_NAME_FAIL));
}
break;
// fr*ck allowed!!!
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHECK_CHAR_NAME_SUCC, sizeof(sP_LS2CL_REP_CHECK_CHAR_NAME_SUCC));
} }
else { case P_CL2LS_REQ_SAVE_CHAR_NAME: {
INITSTRUCT(sP_LS2CL_REP_CHECK_CHAR_NAME_FAIL, resp); if (data->size != sizeof(sP_CL2LS_REQ_SAVE_CHAR_NAME))
resp.iErrorCode = 1; return;
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHECK_CHAR_NAME_FAIL, sizeof(sP_LS2CL_REP_CHECK_CHAR_NAME_FAIL));
}
break;
} sP_CL2LS_REQ_SAVE_CHAR_NAME* save = (sP_CL2LS_REQ_SAVE_CHAR_NAME*)data->buf;
case P_CL2LS_REQ_SAVE_CHAR_NAME: { INITSTRUCT(sP_LS2CL_REP_SAVE_CHAR_NAME_SUCC, resp);
if (data->size != sizeof(sP_CL2LS_REQ_SAVE_CHAR_NAME))
return;
sP_CL2LS_REQ_SAVE_CHAR_NAME* save = (sP_CL2LS_REQ_SAVE_CHAR_NAME*)data->buf; DEBUGLOG(
Database::createCharacter(save, loginSessions[sock].userID); std::cout << "P_CL2LS_REQ_SAVE_CHAR_NAME:" << std::endl;
std::cout << "\tSlot: " << (int)save->iSlotNum << std::endl;
INITSTRUCT(sP_LS2CL_REP_SAVE_CHAR_NAME_SUCC, resp); std::cout << "\tName: " << U16toU8(save->szFirstName) << " " << U16toU8(save->szLastName) << std::endl;
)
DEBUGLOG(
std::cout << "P_CL2LS_REQ_SAVE_CHAR_NAME:" << std::endl;
std::cout << "\tSlot: " << (int)save->iSlotNum << std::endl;
std::cout << "\tName: " << U16toU8(save->szFirstName) << " " << U16toU8(save->szLastName) << std::endl;
)
resp.iSlotNum = save->iSlotNum; resp.iSlotNum = save->iSlotNum;
resp.iGender = save->iGender; resp.iGender = save->iGender;
resp.iPC_UID = Database::getCharacterID(loginSessions[sock].userID, save->iSlotNum); resp.iPC_UID = Database::createCharacter(save, loginSessions[sock].userID);
memcpy(resp.szFirstName, save->szFirstName, sizeof(char16_t) * 9); memcpy(resp.szFirstName, save->szFirstName, sizeof(char16_t) * 9);
memcpy(resp.szLastName, save->szLastName, sizeof(char16_t) * 17); memcpy(resp.szLastName, save->szLastName, sizeof(char16_t) * 17);
sock->sendPacket((void*)&resp, P_LS2CL_REP_SAVE_CHAR_NAME_SUCC, sizeof(sP_LS2CL_REP_SAVE_CHAR_NAME_SUCC)); sock->sendPacket((void*)&resp, P_LS2CL_REP_SAVE_CHAR_NAME_SUCC, sizeof(sP_LS2CL_REP_SAVE_CHAR_NAME_SUCC));
break;
}
case P_CL2LS_REQ_CHAR_CREATE: {
if (data->size != sizeof(sP_CL2LS_REQ_CHAR_CREATE))
return;
sP_CL2LS_REQ_CHAR_CREATE* character = (sP_CL2LS_REQ_CHAR_CREATE*)data->buf; Database::updateSelected(loginSessions[sock].userID, save->iSlotNum);
Database::finishCharacter(character); break;
}
case P_CL2LS_REQ_CHAR_CREATE: {
if (data->size != sizeof(sP_CL2LS_REQ_CHAR_CREATE))
return;
INITSTRUCT(sP_LS2CL_REP_CHAR_CREATE_SUCC, resp); sP_CL2LS_REQ_CHAR_CREATE* character = (sP_CL2LS_REQ_CHAR_CREATE*)data->buf;
Database::finishCharacter(character);
DEBUGLOG( DEBUGLOG(
std::cout << "P_CL2LS_REQ_CHAR_CREATE:" << std::endl; std::cout << "P_CL2LS_REQ_CHAR_CREATE:" << std::endl;
std::cout << "\tPC_UID: " << character->PCStyle.iPC_UID << std::endl; std::cout << "\tPC_UID: " << character->PCStyle.iPC_UID << std::endl;
std::cout << "\tNameCheck: " << (int)character->PCStyle.iNameCheck << std::endl; std::cout << "\tNameCheck: " << (int)character->PCStyle.iNameCheck << std::endl;
std::cout << "\tName: " << U16toU8(character->PCStyle.szFirstName) << " " << U16toU8(character->PCStyle.szLastName) << std::endl; std::cout << "\tName: " << U16toU8(character->PCStyle.szFirstName) << " " << U16toU8(character->PCStyle.szLastName) << std::endl;
std::cout << "\tGender: " << (int)character->PCStyle.iGender << std::endl; std::cout << "\tGender: " << (int)character->PCStyle.iGender << std::endl;
std::cout << "\tFace: " << (int)character->PCStyle.iFaceStyle << std::endl; std::cout << "\tFace: " << (int)character->PCStyle.iFaceStyle << std::endl;
std::cout << "\tHair: " << (int)character->PCStyle.iHairStyle << std::endl; std::cout << "\tHair: " << (int)character->PCStyle.iHairStyle << std::endl;
std::cout << "\tHair Color: " << (int)character->PCStyle.iHairColor << std::endl; std::cout << "\tHair Color: " << (int)character->PCStyle.iHairColor << std::endl;
std::cout << "\tSkin Color: " << (int)character->PCStyle.iSkinColor << std::endl; std::cout << "\tSkin Color: " << (int)character->PCStyle.iSkinColor << std::endl;
std::cout << "\tEye Color: " << (int)character->PCStyle.iEyeColor << std::endl; std::cout << "\tEye Color: " << (int)character->PCStyle.iEyeColor << std::endl;
std::cout << "\tHeight: " << (int)character->PCStyle.iHeight << std::endl; std::cout << "\tHeight: " << (int)character->PCStyle.iHeight << std::endl;
std::cout << "\tBody: " << (int)character->PCStyle.iBody << std::endl; std::cout << "\tBody: " << (int)character->PCStyle.iBody << std::endl;
std::cout << "\tClass: " << (int)character->PCStyle.iClass << std::endl; std::cout << "\tClass: " << (int)character->PCStyle.iClass << std::endl;
std::cout << "\tiEquipUBID: " << (int)character->sOn_Item.iEquipUBID << std::endl; std::cout << "\tiEquipUBID: " << (int)character->sOn_Item.iEquipUBID << std::endl;
std::cout << "\tiEquipLBID: " << (int)character->sOn_Item.iEquipLBID << std::endl; std::cout << "\tiEquipLBID: " << (int)character->sOn_Item.iEquipLBID << std::endl;
std::cout << "\tiEquipFootID: " << (int)character->sOn_Item.iEquipFootID << std::endl; std::cout << "\tiEquipFootID: " << (int)character->sOn_Item.iEquipFootID << std::endl;
) )
int64_t UID = character->PCStyle.iPC_UID; Player player =
Database::DbToPlayer(
Database::getDbPlayerById(character->PCStyle.iPC_UID)
);
int64_t UID = player.iID;
character->PCStyle.iNameCheck = 1; INITSTRUCT(sP_LS2CL_REP_CHAR_CREATE_SUCC, resp);
resp.sPC_Style = character->PCStyle; resp.sPC_Style = player.PCStyle;
resp.sPC_Style2.iAppearanceFlag = 1; resp.sPC_Style2 = player.PCStyle2;
resp.sPC_Style2.iTutorialFlag = 0; resp.iLevel = player.level;
resp.sPC_Style2.iPayzoneFlag = 1; resp.sOn_Item = character->sOn_Item;
resp.iLevel = 36;
resp.sOn_Item = character->sOn_Item;
loginSessions[sock].characters[UID] = Player(); //save player in session
loginSessions[sock].characters[UID].level = 36; loginSessions[sock].characters[UID] = Player(player);
loginSessions[sock].characters[UID].FEKey = sock->getFEKey(); loginSessions[sock].characters[UID].FEKey = sock->getFEKey();
loginSessions[sock].characters[UID].PCStyle = character->PCStyle;
loginSessions[sock].characters[UID].PCStyle2.iAppearanceFlag = 1;
loginSessions[sock].characters[UID].PCStyle2.iPayzoneFlag = 1;
loginSessions[sock].characters[UID].PCStyle2.iTutorialFlag = 1;
loginSessions[sock].characters[UID].x = settings::SPAWN_X;
loginSessions[sock].characters[UID].y = settings::SPAWN_Y;
loginSessions[sock].characters[UID].z = settings::SPAWN_Z;
loginSessions[sock].characters[UID].Equip[1].iID = character->sOn_Item.iEquipUBID; // upper body
loginSessions[sock].characters[UID].Equip[1].iType = 1;
loginSessions[sock].characters[UID].Equip[2].iID = character->sOn_Item.iEquipLBID; // lower body
loginSessions[sock].characters[UID].Equip[2].iType = 2;
loginSessions[sock].characters[UID].Equip[3].iID = character->sOn_Item.iEquipFootID; // foot!
loginSessions[sock].characters[UID].Equip[3].iType = 3;
loginSessions[sock].characters[UID].IsGM = false;
loginSessions[sock].characters[UID].Equip[3].iType = 3;
loginSessions[sock].characters[UID].isTrading = false;
loginSessions[sock].characters[UID].isTradeConfirm = false;
loginSessions[sock].characters[UID].IsGM = settings::GM;
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_CREATE_SUCC, sizeof(sP_LS2CL_REP_CHAR_CREATE_SUCC));
Database::updateSelected(loginSessions[sock].userID, player.slot);
break;
}
case P_CL2LS_REQ_CHAR_DELETE: {
if (data->size != sizeof(sP_CL2LS_REQ_CHAR_DELETE))
return;
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_CREATE_SUCC, sizeof(sP_LS2CL_REP_CHAR_CREATE_SUCC)); sP_CL2LS_REQ_CHAR_DELETE* del = (sP_CL2LS_REQ_CHAR_DELETE*)data->buf;
break; int operationResult = Database::deleteCharacter(del->iPC_UID);
}
case P_CL2LS_REQ_CHAR_DELETE: {
if (data->size != sizeof(sP_CL2LS_REQ_CHAR_DELETE))
return;
sP_CL2LS_REQ_CHAR_DELETE* del = (sP_CL2LS_REQ_CHAR_DELETE*)data->buf;
int operationResult = Database::deleteCharacter(del->iPC_UID, loginSessions[sock].userID);
// success
if (operationResult > 0) {
INITSTRUCT(sP_LS2CL_REP_CHAR_DELETE_SUCC, resp); INITSTRUCT(sP_LS2CL_REP_CHAR_DELETE_SUCC, resp);
resp.iSlotNum = operationResult; resp.iSlotNum = operationResult;
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_DELETE_SUCC, sizeof(sP_LS2CL_REP_CHAR_DELETE_SUCC)); sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_DELETE_SUCC, sizeof(sP_LS2CL_REP_CHAR_DELETE_SUCC));
} else {
// failure break;
// client doesnt't care about this packet and softlocks
INITSTRUCT(sP_LS2CL_REP_CHAR_DELETE_FAIL, resp);
resp.iErrorCode = 0;
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_DELETE_FAIL, sizeof(sP_LS2CL_REP_CHAR_DELETE_FAIL));
} }
case P_CL2LS_REQ_CHAR_SELECT: {
if (data->size != sizeof(sP_CL2LS_REQ_CHAR_SELECT))
return;
break; // character selected
} sP_CL2LS_REQ_CHAR_SELECT* chararacter = (sP_CL2LS_REQ_CHAR_SELECT*)data->buf;
case P_CL2LS_REQ_CHAR_SELECT: { INITSTRUCT(sP_LS2CL_REP_CHAR_SELECT_SUCC, resp);
if (data->size != sizeof(sP_CL2LS_REQ_CHAR_SELECT))
return;
// character selected DEBUGLOG(
sP_CL2LS_REQ_CHAR_SELECT* chararacter = (sP_CL2LS_REQ_CHAR_SELECT*)data->buf; std::cout << "P_CL2LS_REQ_CHAR_SELECT:" << std::endl;
INITSTRUCT(sP_LS2CL_REP_CHAR_SELECT_SUCC, resp); std::cout << "\tPC_UID: " << chararacter->iPC_UID << std::endl;
)
DEBUGLOG( loginSessions[sock].selectedChar = chararacter->iPC_UID;
std::cout << "P_CL2LS_REQ_CHAR_SELECT:" << std::endl; Database::updateSelected(loginSessions[sock].userID, loginSessions[sock].characters[chararacter->iPC_UID].slot);
std::cout << "\tPC_UID: " << chararacter->iPC_UID << std::endl; sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_SELECT_SUCC, sizeof(sP_LS2CL_REP_CHAR_SELECT_SUCC));
) break;
}
case P_CL2LS_REQ_SHARD_SELECT: {
if (data->size != sizeof(sP_CL2LS_REQ_SHARD_SELECT))
return;
loginSessions[sock].selectedChar = chararacter->iPC_UID; // tell client to connect to the shard server
sP_CL2LS_REQ_SHARD_SELECT* shard = (sP_CL2LS_REQ_SHARD_SELECT*)data->buf;
INITSTRUCT(sP_LS2CL_REP_SHARD_SELECT_SUCC, resp);
sock->sendPacket((void*)&resp, P_LS2CL_REP_CHAR_SELECT_SUCC, sizeof(sP_LS2CL_REP_CHAR_SELECT_SUCC)); DEBUGLOG(
break; std::cout << "P_CL2LS_REQ_SHARD_SELECT:" << std::endl;
} std::cout << "\tShard: " << (int)shard->ShardNum << std::endl;
case P_CL2LS_REQ_SHARD_SELECT: { )
if (data->size != sizeof(sP_CL2LS_REQ_SHARD_SELECT))
return;
// tell client to connect to the shard server const char* SHARD_IP = settings::SHARDSERVERIP.c_str();
sP_CL2LS_REQ_SHARD_SELECT* shard = (sP_CL2LS_REQ_SHARD_SELECT*)data->buf; resp.iEnterSerialKey = rand();
INITSTRUCT(sP_LS2CL_REP_SHARD_SELECT_SUCC, resp); resp.g_FE_ServerPort = settings::SHARDPORT;
DEBUGLOG( // copy IP to resp (this struct uses ASCII encoding so we don't have to goof around converting encodings)
std::cout << "P_CL2LS_REQ_SHARD_SELECT:" << std::endl; memcpy(resp.g_FE_ServerIP, SHARD_IP, strlen(SHARD_IP));
std::cout << "\tShard: " << (int)shard->ShardNum << std::endl; resp.g_FE_ServerIP[strlen(SHARD_IP)] = '\0';
)
const char* SHARD_IP = settings::SHARDSERVERIP.c_str(); // pass player to CNSharedData
resp.iEnterSerialKey = rand(); CNSharedData::setPlayer(resp.iEnterSerialKey, loginSessions[sock].characters[loginSessions[sock].selectedChar]);
resp.g_FE_ServerPort = settings::SHARDPORT;
// copy IP to resp (this struct uses ASCII encoding so we don't have to goof around converting encodings) sock->sendPacket((void*)&resp, P_LS2CL_REP_SHARD_SELECT_SUCC, sizeof(sP_LS2CL_REP_SHARD_SELECT_SUCC));
memcpy(resp.g_FE_ServerIP, SHARD_IP, strlen(SHARD_IP)); break;
resp.g_FE_ServerIP[strlen(SHARD_IP)] = '\0'; }
case P_CL2LS_REQ_SAVE_CHAR_TUTOR: {
if (data->size != sizeof(sP_CL2LS_REQ_SAVE_CHAR_TUTOR))
return;
sP_CL2LS_REQ_SAVE_CHAR_TUTOR* save = (sP_CL2LS_REQ_SAVE_CHAR_TUTOR*)data->buf;
Database::finishTutorial(save->iPC_UID);
loginSessions[sock].characters[save->iPC_UID].PCStyle2.iTutorialFlag = 1;
loginSessions[sock].characters[save->iPC_UID].Equip[0].iID = 328;
loginSessions[sock].characters[save->iPC_UID].Equip[0].iType = 0;
loginSessions[sock].characters[save->iPC_UID].Equip[0].iOpt = 1;
// pass player to CNSharedData break;
CNSharedData::setPlayer(resp.iEnterSerialKey, loginSessions[sock].characters[loginSessions[sock].selectedChar]); }
case P_CL2LS_REQ_CHANGE_CHAR_NAME: {
sock->sendPacket((void*)&resp, P_LS2CL_REP_SHARD_SELECT_SUCC, sizeof(sP_LS2CL_REP_SHARD_SELECT_SUCC)); if (data->size != sizeof(sP_CL2LS_REQ_CHANGE_CHAR_NAME))
break; return;
} sP_CL2LS_REQ_CHANGE_CHAR_NAME* save = (sP_CL2LS_REQ_CHANGE_CHAR_NAME*)data->buf;
case P_CL2LS_REQ_SAVE_CHAR_TUTOR: { Database::changeName(save);
if (data->size != sizeof(sP_CL2LS_REQ_SAVE_CHAR_TUTOR)) INITSTRUCT(sP_LS2CL_REP_CHANGE_CHAR_NAME_SUCC, resp);
return; resp.iPC_UID = save->iPCUID;
memcpy(resp.szFirstName, save->szFirstName, sizeof(char16_t)*9);
sP_CL2LS_REQ_SAVE_CHAR_TUTOR* save = (sP_CL2LS_REQ_SAVE_CHAR_TUTOR*)data->buf; memcpy(resp.szLastName, save->szLastName, sizeof(char16_t) * 17);
Database::finishTutorial(save->iPC_UID); resp.iSlotNum = save->iSlotNum;
loginSessions[sock].characters[save->iPC_UID].PCStyle2.iTutorialFlag = 1; sock->sendPacket((void*)&resp, P_LS2CL_REP_CHANGE_CHAR_NAME_SUCC, sizeof(sP_LS2CL_REP_CHANGE_CHAR_NAME_SUCC));
loginSessions[sock].characters[save->iPC_UID].Equip[0].iID = 328; break;
loginSessions[sock].characters[save->iPC_UID].Equip[0].iType = 0; }
loginSessions[sock].characters[save->iPC_UID].Equip[0].iOpt = 1; case P_CL2LS_REQ_PC_EXIT_DUPLICATE:{
if (data->size != sizeof(sP_CL2LS_REQ_PC_EXIT_DUPLICATE))
break; return;
} sP_CL2LS_REQ_PC_EXIT_DUPLICATE* exit = (sP_CL2LS_REQ_PC_EXIT_DUPLICATE*)data->buf;
default: auto account = Database::findAccount(U16toU8(exit->szID));
if (settings::VERBOSITY > 0) if (account == nullptr)
std::cerr << "OpenFusion: LOGIN UNIMPLM ERR. PacketType: " << Defines::p2str(CL2LS, data->type) << " (" << data->type << ")" << std::endl; break;
break; int accountId = account->AccountID;
if (!exitDuplicate(accountId))
PlayerManager::exitDuplicate(accountId);
break;
}
default:
if (settings::VERBOSITY)
std::cerr << "OpenFusion: LOGIN UNIMPLM ERR. PacketType: " << Defines::p2str(CL2LS, data->type) << " (" << data->type << ")" << std::endl;
break;
/* Unimplemented CL2LS packets:
P_CL2LS_CHECK_NAME_LIST - unused by the client
P_CL2LS_REQ_SERVER_SELECT
P_CL2LS_REQ_SHARD_LIST_INFO - dev commands, useless as we only run 1 server
*/
} }
} }
@ -357,3 +368,42 @@ void CNLoginServer::newConnection(CNSocket* cns) {
void CNLoginServer::killConnection(CNSocket* cns) { void CNLoginServer::killConnection(CNSocket* cns) {
loginSessions.erase(cns); loginSessions.erase(cns);
} }
#pragma region helperMethods
bool CNLoginServer::isAccountInUse(int accountId) {
std::map<CNSocket*, CNLoginData>::iterator it;
for (it = CNLoginServer::loginSessions.begin(); it != CNLoginServer::loginSessions.end(); it++)
{
if (it->second.userID == accountId)
return true;
}
return false;
}
bool CNLoginServer::exitDuplicate(int accountId)
{
std::map<CNSocket*, CNLoginData>::iterator it;
for (it = CNLoginServer::loginSessions.begin(); it != CNLoginServer::loginSessions.end(); it++)
{
if (it->second.userID == accountId)
{
CNSocket* sock = it->first;
INITSTRUCT(sP_LS2CL_REP_PC_EXIT_DUPLICATE, resp);
resp.iErrorCode = 0;
sock->sendPacket((void*)&resp, P_LS2CL_REP_PC_EXIT_DUPLICATE, sizeof(sP_LS2CL_REP_PC_EXIT_DUPLICATE));
sock->kill();
return true;
}
}
return false;
}
bool CNLoginServer::isLoginDataGood(std::string login, std::string password)
{
std::regex loginRegex("^([A-Za-z\\d_\\-]){5,20}$");
std::regex passwordRegex("^([A-Za-z\\d_\\-@$!%*#?&,.+:;<=>]){8,20}$");
return (std::regex_match(login, loginRegex) && std::regex_match(password, passwordRegex));
}
bool CNLoginServer::isPasswordCorrect(std::string actualPassword, std::string tryPassword)
{
return BCrypt::validatePassword(tryPassword, actualPassword);
}
#pragma endregion helperMethods

View File

@ -9,7 +9,7 @@
struct CNLoginData { struct CNLoginData {
std::map<int64_t, Player> characters; std::map<int64_t, Player> characters;
int64_t selectedChar; int64_t selectedChar;
int userID; int userID; int slot;
}; };
enum class LOGINERRORID { enum class LOGINERRORID {
@ -30,6 +30,11 @@ private:
static void handlePacket(CNSocket* sock, CNPacketData* data); static void handlePacket(CNSocket* sock, CNPacketData* data);
static std::map<CNSocket*, CNLoginData> loginSessions; static std::map<CNSocket*, CNLoginData> loginSessions;
static bool isLoginDataGood(std::string login, std::string password);
static bool isPasswordCorrect(std::string actualPassword, std::string tryPassword);
static bool isAccountInUse(int accountId);
//returns true if success
static bool exitDuplicate(int accountId);
public: public:
CNLoginServer(uint16_t p); CNLoginServer(uint16_t p);

View File

@ -1,424 +1,356 @@
#include "contrib/sqlite/sqlite3pp.h"
#include "contrib/bcrypt/BCrypt.hpp"
#include "CNProtocol.hpp"
#include "Database.hpp" #include "Database.hpp"
#include "contrib/bcrypt/BCrypt.hpp">
#include "CNProtocol.hpp"
#include <string>
#include "contrib/JSON.hpp"
#include "CNStructs.hpp" #include "CNStructs.hpp"
#include "settings.hpp" #include "settings.hpp"
#include "Player.hpp" #include "Player.hpp"
#include <regex> #include "CNStructs.hpp"
#include <fstream> #include "contrib/sqlite/sqlite_orm.h"
#include "contrib/JSON.hpp"
using namespace sqlite_orm;
//TODO: replace this sqlite wrapper with something better, clean up queries, get rid of json # pragma region DatabaseScheme
auto db = make_storage("database.db",
make_table("Accounts",
make_column("AccountID", &Database::Account::AccountID, autoincrement(), primary_key()),
make_column("Login", &Database::Account::Login),
make_column("Password", &Database::Account::Password),
make_column("Selected", &Database::Account::Selected)
),
make_table("Players",
make_column("PlayerID", &Database::DbPlayer::PlayerID, autoincrement(), primary_key()),
make_column("AccountID", &Database::DbPlayer::AccountID),
make_column("Slot", &Database::DbPlayer::slot),
make_column("Firstname", &Database::DbPlayer::FirstName),
make_column("LastName", &Database::DbPlayer::LastName),
make_column("Level", &Database::DbPlayer::Level),
make_column("AppearanceFlag", &Database::DbPlayer::AppearanceFlag),
make_column("TutorialFlag", &Database::DbPlayer::TutorialFlag),
make_column("PayZoneFlag", &Database::DbPlayer::PayZoneFlag),
make_column("XCoordinates", &Database::DbPlayer::x_coordinates),
make_column("YCoordinates", &Database::DbPlayer::y_coordinates),
make_column("ZCoordinates", &Database::DbPlayer::z_coordinates),
make_column("Body", &Database::DbPlayer::Body),
make_column("Class", &Database::DbPlayer::Class),
make_column("EquipFoot", &Database::DbPlayer::EquipFoot),
make_column("EquipLB", &Database::DbPlayer::EquipLB),
make_column("EquipUB", &Database::DbPlayer::EquipUB),
make_column("EquipWeapon1", &Database::DbPlayer::EquipWeapon1),
make_column("EyeColor", &Database::DbPlayer::EyeColor),
make_column("FaceStyle", &Database::DbPlayer::FaceStyle),
make_column("Gender", &Database::DbPlayer::Gender),
make_column("HP", &Database::DbPlayer::HP),
make_column("HairColor", &Database::DbPlayer::HairColor),
make_column("HairStyle", &Database::DbPlayer::HairStyle),
make_column("Height", &Database::DbPlayer::Height),
make_column("NameCheck", &Database::DbPlayer::NameCheck),
make_column("SkinColor", &Database::DbPlayer::SkinColor),
make_column("isGM", &Database::DbPlayer::isGM),
make_column("FusionMatter", &Database::DbPlayer::FusionMatter),
make_column("Taros", &Database::DbPlayer::Taros)
),
make_table("Inventory",
make_column("AccountID", &Database::Inventory::AccountID, primary_key())
)
);
sqlite3pp::database db; # pragma endregion DatabaseScheme
void Database::open() { #pragma region LoginServer
//checking if database file exists
std::ifstream file;
file.open("data.db");
if (file) { void Database::open()
file.close(); {
// if exists, assign it //this parameter means it will try to preserve data during migration
db = sqlite3pp::database("data.db"); bool preserve = true;
DEBUGLOG(std::cout << "[DB] Database in operation" << std::endl;) db.sync_schema(preserve);
} DEBUGLOG(
else { std::cout << "[DB] Database in operation" << std::endl;
// if doesn't, create all the tables )
DEBUGLOG(std::cout << "[DB] Creating new database" << std::endl;)
db = sqlite3pp::database("data.db");
// creates accounts
db.execute("CREATE TABLE Accounts(AccountID INTEGER PRIMARY KEY AUTOINCREMENT, Login TEXT NOT NULL, Password TEXT NOT NULL);");
// creates characters
db.execute("CREATE TABLE Players(PlayerID INTEGER PRIMARY KEY AUTOINCREMENT, AccountID INTEGER NOT NULL, Slot INTEGER NOT NULL, FirstName TEXT NOT NULL, LastName TEXT NOT NULL, CharData TEXT NOT NULL);");
DEBUGLOG(std::cout << "Done" << std::endl;)
}
} }
// verifies that the username & passwords are valid int Database::addAccount(std::string login, std::string password)
bool Database::isLoginDataGood(std::string login, std::string password) { {
std::regex loginRegex("^([A-Za-z\\d_\\-]){5,20}$"); password = BCrypt::generateHash(password);
std::regex passwordRegex("^([A-Za-z\\d_\\-@$!%*#?&,.+:;<=>]){2,20}$"); Account x;
return (std::regex_match(login, loginRegex) && std::regex_match(password, passwordRegex)); x.Login = login;
x.Password = password;
x.Selected = 1;
return db.insert(x);
} }
void Database::addAccount(std::string login, std::string password) { void Database::updateSelected(int accountId, int slot)
// generates prepared statment {
sqlite3pp::command cmd(db, "INSERT INTO Accounts (Login, Password) VALUES (:login, :password)"); Account acc = db.get<Account>(accountId);
acc.Selected = slot;
// generates a hashed password! db.update(acc);
password = BCrypt::generateHash(password);
// binds args to the command
cmd.bind(":login", login, sqlite3pp::nocopy);
cmd.bind(":password", password, sqlite3pp::nocopy);
cmd.execute();
} }
bool Database::doesUserExist(std::string login) { std::unique_ptr<Database::Account> Database::findAccount(std::string login)
// generates prepared statement {
sqlite3pp::query qry(db, "SELECT COUNT(AccountID) FROM Accounts WHERE Login = :login"); //this is awful, I've tried everything to improve it
// binds to the query auto find = db.get_all<Account>(
qry.bind(":login", login, sqlite3pp::nocopy); where(c(&Account::Login) == login), limit(1));
if (find.empty())
// executes return nullptr;
sqlite3pp::query::iterator i = qry.begin(); return
std::unique_ptr<Account>(new Account(find.front()));
// grabs the first result (the count)
int result;
std::tie(result) = (*i).get_columns<int>(0);
// if there is more than 0 results, the user exists :eyes:
return (result > 0);
} }
bool Database::isPasswordCorrect(std::string login, std::string password) { bool Database::isNameFree(sP_CL2LS_REQ_CHECK_CHAR_NAME* nameCheck)
// generates prepared statement {
sqlite3pp::query qry(db, "SELECT Password FROM Accounts WHERE Login = :login"); //TODO: add colate nocase
// binds username to the query std::string First = U16toU8(nameCheck->szFirstName);
qry.bind(":login", login, sqlite3pp::nocopy); std::string Last = U16toU8(nameCheck->szLastName);
return
// executes (db.get_all<DbPlayer>
sqlite3pp::query::iterator i = qry.begin(); (where((c(&DbPlayer::FirstName) == First)
and (c(&DbPlayer::LastName) == Last)))
// grabs the first result .empty());
const char* actual;
std::tie(actual) = (*i).get_columns<const char*>(0);
// validate password hash with provded password
return BCrypt::validatePassword(password, actual);
} }
int Database::getUserID(std::string login) { int Database::createCharacter(sP_CL2LS_REQ_SAVE_CHAR_NAME* save, int AccountID)
// generates prep statement {
sqlite3pp::query qry(db, "SELECT AccountID FROM Accounts WHERE Login = :login"); DbPlayer create;
// binds the username to the login param //save packet data
qry.bind(":login", login, sqlite3pp::nocopy); create.FirstName = U16toU8(save->szFirstName);
create.LastName = U16toU8(save->szLastName);
create.slot = save->iSlotNum;
create.AccountID = AccountID;
//set flags
create.AppearanceFlag = 0;
create.TutorialFlag = 0;
create.PayZoneFlag = 0;
//set namecheck based on setting
if (settings::APPROVEALLNAMES || save->iFNCode)
create.NameCheck = 1;
else
create.NameCheck = 0;
//create default body character
create.Body= 0;
create.Class= 0;
create.EquipFoot= 0;
create.EquipLB= 0;
create.EquipUB= 0;
create.EquipWeapon1= 0;
create.EquipWeapon2= 0;
create.EyeColor= 1;
create.FaceStyle= 1;
create.Gender= 1;
create.HP= 1000;
create.HairColor= 1;
create.HairStyle = 1;
create.Height= 0;
create.Level= 1;
create.SkinColor= 1;
create.isGM = false;
//commented and disabled for now
//if (U16toU8(save->szFirstName) == settings::GMPASS) {
// create.isGM = true;
//}
// executes the query create.FusionMatter= 0;
sqlite3pp::query::iterator i = qry.begin(); create.Taros= 0;
create.x_coordinates = settings::SPAWN_X;
// grabs the first result of the query create.y_coordinates= settings::SPAWN_Y;
int result; create.z_coordinates= settings::SPAWN_Z;
std::tie(result) = (*i).get_columns<int>(0); return db.insert(create);
// returns the result
return result;
} }
int Database::getUserSlotsNum(int AccountId) { void Database::finishCharacter(sP_CL2LS_REQ_CHAR_CREATE* character)
// generates the prepared statement {
sqlite3pp::query qry(db, "SELECT COUNT(PlayerID) FROM Players WHERE AccountID = :ID"); DbPlayer finish = getDbPlayerById(character->PCStyle.iPC_UID);
finish.AppearanceFlag = 1;
// binds the ID to the param finish.Body = character->PCStyle.iBody;
qry.bind(":ID", AccountId); finish.Class = character->PCStyle.iClass;
finish.EquipFoot = character->sOn_Item.iEquipFootID;
// executes finish.EquipLB = character->sOn_Item.iEquipLBID;
sqlite3pp::query::iterator i = qry.begin(); finish.EquipUB = character->sOn_Item.iEquipUBID;
finish.EyeColor = character->PCStyle.iEyeColor;
// grabs the first result finish.FaceStyle = character->PCStyle.iFaceStyle;
int result; finish.Gender = character->PCStyle.iGender;
std::tie(result) = (*i).get_columns<int>(0); finish.HairColor = character->PCStyle.iHairColor;
finish.HairStyle = character->PCStyle.iHairStyle;
// returns the result finish.Height = character->PCStyle.iHeight;
return result; finish.Level = 1;
finish.SkinColor = character->PCStyle.iSkinColor;
db.update(finish);
} }
bool Database::isNameFree(std::string First, std::string Second) { void Database::finishTutorial(int PlayerID)
// generates the prepared statement {
sqlite3pp::query qry(db, "SELECT COUNT(PlayerID) FROM Players WHERE FirstName = :First COLLATE nocase AND LastName = :Second COLLATE nocase"); DbPlayer finish = getDbPlayerById(PlayerID);
finish.TutorialFlag = 1;
// binds the params //equip lightning gun
qry.bind(":First", First, sqlite3pp::nocopy); finish.EquipWeapon1 = 328;
qry.bind(":Second", Second, sqlite3pp::nocopy); db.update(finish);
// executes the query
sqlite3pp::query::iterator i = qry.begin();
// grabs the result
int result;
std::tie(result) = (*i).get_columns<int>(0);
// if no results return, the the username is unused
return (result == 0);
} }
void Database::createCharacter(sP_CL2LS_REQ_SAVE_CHAR_NAME* save, int AccountID) { int Database::deleteCharacter(int characterID)
// generate the command {
sqlite3pp::command cmd(db, "INSERT INTO Players (AccountID, Slot, FirstName, LastName, CharData) VALUES (:AccountID, :Slot, :FirstName, :LastName, :CharData)"); auto find =
db.get_all<DbPlayer>(where(c(&DbPlayer::PlayerID) == characterID));
// generate character data int slot = find.front().slot;
std::string charData = CharacterToJson(save); db.remove<DbPlayer>(find.front().PlayerID);
std::string first = U16toU8(save->szFirstName); return slot;
std::string last = U16toU8(save->szLastName);
// bind to command
cmd.bind(":AccountID", AccountID);
cmd.bind(":Slot", save->iSlotNum);
cmd.bind(":CharData", charData, sqlite3pp::nocopy);
cmd.bind(":FirstName", first, sqlite3pp::nocopy);
cmd.bind(":LastName", last, sqlite3pp::nocopy);
// run
cmd.execute();
} }
void Database::finishCharacter(sP_CL2LS_REQ_CHAR_CREATE* character) { std::vector <Player> Database::getCharacters(int UserID)
sqlite3pp::command cmd(db, "UPDATE Players SET CharData = :data WHERE PlayerID = :id"); {
std::vector<DbPlayer>characters =
// grab the data to add to the command db.get_all<DbPlayer>(where
int id = character->PCStyle.iPC_UID; (c(&DbPlayer::AccountID) == UserID));
std::string charData = CharacterToJson(character); //parsing DbPlayer to Player
std::vector<Player> result = std::vector<Player>();
// bind to the command & execute for (auto &character : characters) {
cmd.bind(":data", charData, sqlite3pp::nocopy); Player toadd = DbToPlayer(character);
cmd.bind(":id", id); result.push_back(
cmd.execute(); toadd
);
}
return result;
} }
void Database::finishTutorial(int PlayerID) { void Database::evaluateCustomName(int characterID, CUSTOMNAME decision) {
sqlite3pp::query qry(db, "SELECT CharData FROM Players WHERE PlayerID = :ID"); DbPlayer player = getDbPlayerById(characterID);
player.NameCheck = (int)decision;
// bind to the query and execute db.update(player);
qry.bind(":ID", PlayerID);
sqlite3pp::query::iterator i = qry.begin();
// grab the player json from the database
std::string json;
std::tie(json) = (*i).get_columns<std::string>(0);
// parse the json into a Player
Player player = JsonToPlayer(json, PlayerID);
// set the tutorial flag & equip lightning gun
player.PCStyle2.iTutorialFlag = 1;
player.Equip[0].iID = 328;
json = PlayerToJson(player);
// update the database
sqlite3pp::command cmd(db, "UPDATE Players SET CharData = :data WHERE PlayerID = :id");
cmd.bind(":data", json, sqlite3pp::nocopy);
cmd.bind(":id", PlayerID);
cmd.execute();
} }
int Database::getCharacterID(int AccountID, int slot) { void Database::changeName(sP_CL2LS_REQ_CHANGE_CHAR_NAME* save) {
// make the query DbPlayer Player = getDbPlayerById(save->iPCUID);
sqlite3pp::query qry(db, "SELECT PlayerID FROM Players WHERE AccountID = :ID AND Slot = :Slot"); Player.FirstName = U16toU8(save->szFirstName);
Player.LastName = U16toU8(save->szLastName);
// bind the params & execute if (settings::APPROVEALLNAMES || save->iFNCode)
qry.bind(":ID", AccountID); Player.NameCheck = 1;
qry.bind(":Slot", slot); else
sqlite3pp::query::iterator i = qry.begin(); Player.NameCheck = 0;
db.update(Player);
// grab the result
int result;
std::tie(result) = (*i).get_columns<int>(0);
return result;
} }
int Database::deleteCharacter(int characterID, int accountID) { Database::DbPlayer Database::playerToDb(Player player)
// checking if requested player exist and is bound to the account {
DbPlayer result;
result.PlayerID = player.iID;
result.AccountID = player.accountId;
result.AppearanceFlag = player.PCStyle2.iAppearanceFlag;
result.Body = player.PCStyle.iBody;
result.Class = player.PCStyle.iClass;
//equipment
result.EyeColor = player.PCStyle.iEyeColor;
result.FaceStyle = player.PCStyle.iFaceStyle;
result.FirstName = U16toU8( player.PCStyle.szFirstName);
//fm
result.Gender = player.PCStyle.iGender;
result.HairColor = player.PCStyle.iHairColor;
result.HairStyle = player.PCStyle.iHairStyle;
result.Height = player.PCStyle.iHeight;
result.HP = player.HP;
result.isGM = player.IsGM;
result.LastName = U16toU8(player.PCStyle.szLastName);
result.Level = player.level;
result.NameCheck = player.PCStyle.iNameCheck;
result.PayZoneFlag = player.PCStyle2.iPayzoneFlag;
result.PlayerID = player.PCStyle.iPC_UID;
result.SkinColor = player.PCStyle.iSkinColor;
result.slot = player.slot;
//taros
result.TutorialFlag = player.PCStyle2.iTutorialFlag;
result.x_coordinates = player.x;
result.y_coordinates = player.y;
result.z_coordinates = player.z;
sqlite3pp::query qry(db, "SELECT COUNT(AccountID) FROM Players WHERE AccountID = :AccID AND PlayerID = :PID"); return result;
qry.bind(":AccID", accountID);
qry.bind(":PID", characterID);
sqlite3pp::query::iterator i = qry.begin();
int result;
std::tie(result) = (*i).get_columns<int>(0);
if (result > 0) {
// get player character slot
sqlite3pp::query qry(db, "SELECT Slot FROM Players WHERE PlayerID = :PID");
// bind & execute
qry.bind(":PID", characterID);
sqlite3pp::query::iterator i = qry.begin();
// grab the slot to return
int slot;
std::tie(slot) = (*i).get_columns<int>(0);
// actually delete the record
sqlite3pp::command cmd(db, "DELETE FROM Players WHERE PlayerID = :ID");
cmd.bind(":ID", characterID);
cmd.execute();
// finally, return the grabbed slot
return slot;
}
return -1;
} }
// TODO: this should really be std::vector, but for the sake of compatibility with PRs, I'll change this after merging Player Database::DbToPlayer(DbPlayer player) {
std::list<Player> Database::getCharacters(int userID) { Player result;
std::list<Player> result = std::list<Player>();
sqlite3pp::query qry(db, "SELECT * FROM Players WHERE AccountID = :ID"); result.accountId = player.AccountID;
result.PCStyle2.iAppearanceFlag = player.AppearanceFlag;
result.PCStyle.iBody = player.Body;
result.PCStyle.iClass = player.Class;
result.PCStyle.iEyeColor = player.EyeColor;
result.PCStyle.iFaceStyle = player.FaceStyle;
U8toU16(player.FirstName, result.PCStyle.szFirstName);
result.PCStyle.iGender = player.Gender;
result.PCStyle.iHairColor = player.HairColor;
result.PCStyle.iHairStyle = player.HairStyle;
result.PCStyle.iHeight = player.Height;
result.HP = player.HP;
result.IsGM = player.isGM;
U8toU16(player.LastName, result.PCStyle.szLastName);
result.level = player.Level;
result.PCStyle.iNameCheck = player.NameCheck;
result.PCStyle2.iPayzoneFlag = player.PayZoneFlag;
result.iID = player.PlayerID;
result.PCStyle.iPC_UID = player.PlayerID;
result.PCStyle.iSkinColor = player.SkinColor;
result.slot = player.slot;
result.PCStyle2.iTutorialFlag = player.TutorialFlag;
result.x = player.x_coordinates;
result.y = player.y_coordinates;
result.z = player.z_coordinates;
qry.bind(":ID", userID); //TODO:: implement all of below
result.SerialKey = 0;
result.money = 0;
result.fusionmatter = 0;
result.activeNano = 0;
result.iPCState = 0;
result.equippedNanos[0] = 1;
result.equippedNanos[1] = 0;
result.equippedNanos[2] = 0;
result.isTrading = false;
result.isTradeConfirm = false;
// for each character owned by the account, result.Nanos[1].iID = 1;
for (sqlite3pp::query::iterator i = qry.begin(); i != qry.end(); ++i) { result.Nanos[1].iSkillID = 1;
// grab the data result.Nanos[1].iStamina = 150;
int ID, AccountID, slot;
char const* charData, * first, * second;
std::tie(ID, AccountID, slot, first, second, charData) =
(*i).get_columns<int, int, int, char const*, char const*, char const*>(0, 1, 2, 3, 4, 5);
// convert to player for (int i = 2; i < 37; i++) {
Player toadd = JsonToPlayer(charData, ID); result.Nanos[i].iID = 0;
toadd.slot = slot; result.Nanos[i].iSkillID = 0;
result.Nanos[i].iStamina = 0;
}
// add it to the results result.Equip[0].iID = player.EquipWeapon1;
result.push_back(toadd); result.Equip[0].iType = 0;
} (player.EquipWeapon1) ? result.Equip[0].iOpt = 1 : result.Equip[0].iOpt = 0;
return result;
result.Equip[1].iID = player.EquipUB;
result.Equip[1].iType = 1;
(player.EquipUB) ? result.Equip[1].iOpt = 1 : result.Equip[1].iOpt = 0;
result.Equip[2].iID = player.EquipLB;
result.Equip[2].iType = 2;
(player.EquipLB) ? result.Equip[2].iOpt = 1 : result.Equip[2].iOpt = 0;
result.Equip[3].iID = player.EquipFoot;
result.Equip[3].iType = 3;
(player.EquipFoot) ? result.Equip[3].iOpt = 1 : result.Equip[3].iOpt = 0;
for (int i = 4; i < AEQUIP_COUNT; i++) {
// empty equips
result.Equip[i].iID = 0;
result.Equip[i].iType = i;
result.Equip[i].iOpt = 0;
}
for (int i = 0; i < AINVEN_COUNT; i++) {
// setup inventories
result.Inven[i].iID = 0;
result.Inven[i].iType = 0;
result.Inven[i].iOpt = 0;
}
return result;
} }
std::string Database::CharacterToJson(sP_CL2LS_REQ_SAVE_CHAR_NAME* save) { Database::DbPlayer Database::getDbPlayerById(int id) {
nlohmann::json json = { return db.get_all<DbPlayer>(where(c(&DbPlayer::PlayerID) == id))
{"Level",36}, .front();
//to check
{"HP",1000},
{"NameCheck", 1},
{"FirstName",U16toU8(save->szFirstName)},
{"LastName",U16toU8(save->szLastName)},
{"Gender",rand() % 2 + 1 },
{"FaceStyle",1},
{"HairStyle",1},
{"HairColor",(rand() % 19) + 1},
{"SkinColor",(rand() % 13) + 1},
{"EyeColor",(rand() % 6) + 1},
{"Height",(rand() % 6)},
{"Body",(rand() % 4)},
{"Class",0},
{"AppearanceFlag",0},
{"PayzoneFlag",1},
{"TutorialFlag",0},
{"EquipUB", 217},
{"EquipLB", 203},
{"EquipFoot", 314},
{"EquipWeapon", 0},
{"x",settings::SPAWN_X},
{"y",settings::SPAWN_Y},
{"z",settings::SPAWN_Z},
{"isGM",false},
};
return json.dump();
} }
std::string Database::PlayerToJson(Player player) { #pragma endregion LoginServer
nlohmann::json json = {
{"Level",player.level},
//to check
{"HP",player.HP},
{"NameCheck", 1},
{"FirstName",U16toU8(player.PCStyle.szFirstName)},
{"LastName",U16toU8(player.PCStyle.szLastName)},
{"Gender",player.PCStyle.iGender},
{"FaceStyle",player.PCStyle.iFaceStyle},
{"HairStyle",player.PCStyle.iHairStyle},
{"HairColor",player.PCStyle.iHairColor},
{"SkinColor",player.PCStyle.iSkinColor},
{"EyeColor",player.PCStyle.iEyeColor},
{"Height",player.PCStyle.iHeight},
{"Body",player.PCStyle.iBody},
{"Class",player.PCStyle.iClass},
{"AppearanceFlag",player.PCStyle2.iAppearanceFlag},
{"PayzoneFlag",player.PCStyle2.iPayzoneFlag},
{"TutorialFlag",player.PCStyle2.iTutorialFlag},
{"EquipUB", player.Equip[1].iID},
{"EquipLB", player.Equip[2].iID},
{"EquipFoot", player.Equip[3].iID},
{"EquipWeapon", player.Equip[0].iID},
{"x",player.x},
{"y",player.y},
{"z",player.z},
{"isGM",false},
};
return json.dump();
}
Player Database::JsonToPlayer(std::string input, int PC_UID) {
std::string err;
const auto json = nlohmann::json::parse(input);
Player player;
player.PCStyle.iPC_UID = (int64_t)PC_UID;
player.level = std::stoi(json["Level"].dump());
player.HP = std::stoi(json["HP"].dump());
player.PCStyle.iNameCheck = std::stoi(json["NameCheck"].dump());
U8toU16(json["FirstName"].get<std::string>(), player.PCStyle.szFirstName);
U8toU16(json["LastName"].get<std::string>(), player.PCStyle.szLastName);
player.PCStyle.iGender = std::stoi(json["Gender"].dump());
player.PCStyle.iFaceStyle = std::stoi(json["FaceStyle"].dump());
player.PCStyle.iHairStyle = std::stoi(json["HairStyle"].dump());
player.PCStyle.iHairColor = std::stoi(json["HairColor"].dump());
player.PCStyle.iSkinColor = std::stoi(json["SkinColor"].dump());
player.PCStyle.iEyeColor = std::stoi(json["EyeColor"].dump());
player.PCStyle.iHeight = std::stoi(json["Height"].dump());
player.PCStyle.iBody = std::stoi(json["Body"].dump());
player.PCStyle.iClass = std::stoi(json["Class"].dump());
player.PCStyle2.iAppearanceFlag = std::stoi(json["AppearanceFlag"].dump());
player.PCStyle2.iPayzoneFlag = std::stoi(json["PayzoneFlag"].dump());
player.PCStyle2.iTutorialFlag = std::stoi(json["TutorialFlag"].dump());
player.Equip[0].iID = std::stoi(json["EquipWeapon"].dump());
if (player.Equip[0].iID != 0)
player.Equip[0].iOpt = 1;
else
player.Equip[0].iOpt = 0;
player.Equip[0].iType = 0;
player.Equip[1].iID = std::stoi(json["EquipUB"].dump());
player.Equip[1].iOpt = 1;
player.Equip[1].iType = 1;
player.Equip[2].iID = std::stoi(json["EquipLB"].dump());
player.Equip[2].iOpt = 1;
player.Equip[2].iType = 2;
player.Equip[3].iID = std::stoi(json["EquipFoot"].dump());
player.Equip[3].iOpt = 1;
player.Equip[3].iType = 3;
player.x = std::stoi(json["x"].dump());
player.y = std::stoi(json["y"].dump());
player.z = std::stoi(json["z"].dump());
return player;
}
std::string Database::CharacterToJson(sP_CL2LS_REQ_CHAR_CREATE* character) {
nlohmann::json json = {
{"Level",36},
//to check
{"HP",1000},
{"NameCheck", 1},
{"FirstName",U16toU8(character->PCStyle.szFirstName)},
{"LastName",U16toU8(character->PCStyle.szLastName)},
{"Gender",character->PCStyle.iGender},
{"FaceStyle",character->PCStyle.iFaceStyle},
{"HairStyle",character->PCStyle.iHairStyle},
{"HairColor",character->PCStyle.iHairColor},
{"SkinColor",character->PCStyle.iSkinColor},
{"EyeColor",character->PCStyle.iEyeColor},
{"Height",character->PCStyle.iHeight},
{"Body",character->PCStyle.iBody},
{"Class",character->PCStyle.iClass},
{"AppearanceFlag",1},
{"PayzoneFlag",1},
{"TutorialFlag",0},
{"EquipUB", character->sOn_Item.iEquipUBID},
{"EquipLB", character->sOn_Item.iEquipLBID},
{"EquipFoot", character->sOn_Item.iEquipFootID},
{"EquipWeapon", 0},
{"x",settings::SPAWN_X},
{"y",settings::SPAWN_Y},
{"z",settings::SPAWN_Z},
{"isGM",false},
};
return json.dump();
}

View File

@ -1,31 +1,91 @@
#pragma once #pragma once
#include "CNStructs.hpp" #include "CNStructs.hpp"
#include "Player.hpp" #include "Player.hpp"
#include <list>
#include <string> #include <string>
#include <vector>
namespace Database { namespace Database {
#pragma region DatabaseStructs
struct Account
{
int AccountID;
std::string Login;
std::string Password;
int Selected;
};
struct Inventory
{
int AccountID;
};
struct DbPlayer
{
int PlayerID;
int AccountID;
short int slot;
std::string FirstName;
std::string LastName;
short int AppearanceFlag;
short int Body;
short int Class;
short int EquipFoot;
short int EquipLB;
short int EquipUB;
short int EquipWeapon1;
short int EquipWeapon2;
short int EyeColor;
short int FaceStyle;
short int Gender;
int HP;
short int HairColor;
short int HairStyle;
short int Height;
short int Level;
short int NameCheck;
short int PayZoneFlag;
short int SkinColor;
bool TutorialFlag;
bool isGM;
int FusionMatter;
int Taros;
int x_coordinates;
int y_coordinates;
int z_coordinates;
};
#pragma endregion DatabaseStructs
//handles migrations
void open(); void open();
//checks regex //returns ID
bool isLoginDataGood(std::string login, std::string password); int addAccount(std::string login, std::string password);
void addAccount(std::string login, std::string password); void updateSelected(int accountId, int playerId);
bool doesUserExist(std::string login); std::unique_ptr<Account> findAccount(std::string login);
bool isPasswordCorrect(std::string login, std::string password); bool isNameFree(sP_CL2LS_REQ_CHECK_CHAR_NAME* nameCheck);
int getUserID(std::string login); //called after chosing name, returns ID
int getUserSlotsNum(int AccountId); int createCharacter(sP_CL2LS_REQ_SAVE_CHAR_NAME* save, int AccountID);
bool isNameFree(std::string First, std::string Second); //called after finishing creation
//after chosing name
void createCharacter(sP_CL2LS_REQ_SAVE_CHAR_NAME* save, int AccountID);
//after finishing creation
void finishCharacter(sP_CL2LS_REQ_CHAR_CREATE* character); void finishCharacter(sP_CL2LS_REQ_CHAR_CREATE* character);
//called after tutorial
void finishTutorial(int PlayerID); void finishTutorial(int PlayerID);
//returns slot nr if success //returns slot number
int deleteCharacter(int characterID, int accountID); int deleteCharacter(int characterID);
int getCharacterID(int AccountID, int slot); std::vector <Player> getCharacters(int userID);
std::list <Player> getCharacters(int userID); //accepting/declining custom name
//some json parsing crap enum class CUSTOMNAME {
std::string CharacterToJson(sP_CL2LS_REQ_SAVE_CHAR_NAME* save); approve = 1,
std::string CharacterToJson(sP_CL2LS_REQ_CHAR_CREATE* character); disapprove = 2
Player JsonToPlayer(std::string input, int PC_UID); };
std::string PlayerToJson(Player player); void evaluateCustomName(int characterID, CUSTOMNAME decision);
void changeName(sP_CL2LS_REQ_CHANGE_CHAR_NAME* save);
//parsing DbPlayer
DbPlayer playerToDb(Player player);
Player DbToPlayer(DbPlayer player);
//getting players
DbPlayer getDbPlayerById(int id);
} }

View File

@ -7,6 +7,7 @@
#include "CNStructs.hpp" #include "CNStructs.hpp"
struct Player { struct Player {
int accountId;
int64_t SerialKey; int64_t SerialKey;
int32_t iID; int32_t iID;
uint64_t FEKey; uint64_t FEKey;

View File

@ -686,4 +686,28 @@ WarpLocation PlayerManager::getRespawnPoint(Player *plr) {
return best; return best;
} }
bool PlayerManager::isAccountInUse(int accountId) {
std::map<CNSocket*, PlayerView>::iterator it;
for (it = PlayerManager::players.begin(); it != PlayerManager::players.end(); it++)
{
if (it->second.plr->accountId == accountId)
return true;
}
return false;
}
void PlayerManager::exitDuplicate(int accountId) {
std::map<CNSocket*, PlayerView>::iterator it;
for (it = PlayerManager::players.begin(); it != PlayerManager::players.end(); it++)
{
if (it->second.plr->accountId == accountId)
{
CNSocket* sock = it->first;
INITSTRUCT(sP_FE2CL_REP_PC_EXIT_DUPLICATE, resp);
resp.iErrorCode = 0;
sock->sendPacket((void*)&resp, P_FE2CL_REP_PC_EXIT_DUPLICATE, sizeof(sP_FE2CL_REP_PC_EXIT_DUPLICATE));
sock->kill();
}
}
}
#pragma endregion #pragma endregion

View File

@ -52,4 +52,7 @@ namespace PlayerManager {
Player *getPlayer(CNSocket* key); Player *getPlayer(CNSocket* key);
WarpLocation getRespawnPoint(Player *plr); WarpLocation getRespawnPoint(Player *plr);
bool isAccountInUse(int accountId);
void exitDuplicate(int accountId);
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,659 +0,0 @@
/*
** 2006 June 7
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This header file defines the SQLite interface for use by
** shared libraries that want to be imported as extensions into
** an SQLite instance. Shared libraries that intend to be loaded
** as extensions by SQLite should #include this file instead of
** sqlite3.h.
*/
#ifndef SQLITE3EXT_H
#define SQLITE3EXT_H
#include "sqlite3.h"
/*
** The following structure holds pointers to all of the SQLite API
** routines.
**
** WARNING: In order to maintain backwards compatibility, add new
** interfaces to the end of this structure only. If you insert new
** interfaces in the middle of this structure, then older different
** versions of SQLite will not be able to load each other's shared
** libraries!
*/
struct sqlite3_api_routines {
void * (*aggregate_context)(sqlite3_context*,int nBytes);
int (*aggregate_count)(sqlite3_context*);
int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
int (*bind_double)(sqlite3_stmt*,int,double);
int (*bind_int)(sqlite3_stmt*,int,int);
int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
int (*bind_null)(sqlite3_stmt*,int);
int (*bind_parameter_count)(sqlite3_stmt*);
int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
const char * (*bind_parameter_name)(sqlite3_stmt*,int);
int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
int (*busy_timeout)(sqlite3*,int ms);
int (*changes)(sqlite3*);
int (*close)(sqlite3*);
int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
int eTextRep,const char*));
int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
int eTextRep,const void*));
const void * (*column_blob)(sqlite3_stmt*,int iCol);
int (*column_bytes)(sqlite3_stmt*,int iCol);
int (*column_bytes16)(sqlite3_stmt*,int iCol);
int (*column_count)(sqlite3_stmt*pStmt);
const char * (*column_database_name)(sqlite3_stmt*,int);
const void * (*column_database_name16)(sqlite3_stmt*,int);
const char * (*column_decltype)(sqlite3_stmt*,int i);
const void * (*column_decltype16)(sqlite3_stmt*,int);
double (*column_double)(sqlite3_stmt*,int iCol);
int (*column_int)(sqlite3_stmt*,int iCol);
sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
const char * (*column_name)(sqlite3_stmt*,int);
const void * (*column_name16)(sqlite3_stmt*,int);
const char * (*column_origin_name)(sqlite3_stmt*,int);
const void * (*column_origin_name16)(sqlite3_stmt*,int);
const char * (*column_table_name)(sqlite3_stmt*,int);
const void * (*column_table_name16)(sqlite3_stmt*,int);
const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
const void * (*column_text16)(sqlite3_stmt*,int iCol);
int (*column_type)(sqlite3_stmt*,int iCol);
sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
int (*complete)(const char*sql);
int (*complete16)(const void*sql);
int (*create_collation)(sqlite3*,const char*,int,void*,
int(*)(void*,int,const void*,int,const void*));
int (*create_collation16)(sqlite3*,const void*,int,void*,
int(*)(void*,int,const void*,int,const void*));
int (*create_function)(sqlite3*,const char*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*));
int (*create_function16)(sqlite3*,const void*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*));
int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
int (*data_count)(sqlite3_stmt*pStmt);
sqlite3 * (*db_handle)(sqlite3_stmt*);
int (*declare_vtab)(sqlite3*,const char*);
int (*enable_shared_cache)(int);
int (*errcode)(sqlite3*db);
const char * (*errmsg)(sqlite3*);
const void * (*errmsg16)(sqlite3*);
int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
int (*expired)(sqlite3_stmt*);
int (*finalize)(sqlite3_stmt*pStmt);
void (*free)(void*);
void (*free_table)(char**result);
int (*get_autocommit)(sqlite3*);
void * (*get_auxdata)(sqlite3_context*,int);
int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
int (*global_recover)(void);
void (*interruptx)(sqlite3*);
sqlite_int64 (*last_insert_rowid)(sqlite3*);
const char * (*libversion)(void);
int (*libversion_number)(void);
void *(*malloc)(int);
char * (*mprintf)(const char*,...);
int (*open)(const char*,sqlite3**);
int (*open16)(const void*,sqlite3**);
int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
void *(*realloc)(void*,int);
int (*reset)(sqlite3_stmt*pStmt);
void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_double)(sqlite3_context*,double);
void (*result_error)(sqlite3_context*,const char*,int);
void (*result_error16)(sqlite3_context*,const void*,int);
void (*result_int)(sqlite3_context*,int);
void (*result_int64)(sqlite3_context*,sqlite_int64);
void (*result_null)(sqlite3_context*);
void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
void (*result_value)(sqlite3_context*,sqlite3_value*);
void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
const char*,const char*),void*);
void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
char * (*xsnprintf)(int,char*,const char*,...);
int (*step)(sqlite3_stmt*);
int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
char const**,char const**,int*,int*,int*);
void (*thread_cleanup)(void);
int (*total_changes)(sqlite3*);
void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
sqlite_int64),void*);
void * (*user_data)(sqlite3_context*);
const void * (*value_blob)(sqlite3_value*);
int (*value_bytes)(sqlite3_value*);
int (*value_bytes16)(sqlite3_value*);
double (*value_double)(sqlite3_value*);
int (*value_int)(sqlite3_value*);
sqlite_int64 (*value_int64)(sqlite3_value*);
int (*value_numeric_type)(sqlite3_value*);
const unsigned char * (*value_text)(sqlite3_value*);
const void * (*value_text16)(sqlite3_value*);
const void * (*value_text16be)(sqlite3_value*);
const void * (*value_text16le)(sqlite3_value*);
int (*value_type)(sqlite3_value*);
char *(*vmprintf)(const char*,va_list);
/* Added ??? */
int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
/* Added by 3.3.13 */
int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
int (*clear_bindings)(sqlite3_stmt*);
/* Added by 3.4.1 */
int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
void (*xDestroy)(void *));
/* Added by 3.5.0 */
int (*bind_zeroblob)(sqlite3_stmt*,int,int);
int (*blob_bytes)(sqlite3_blob*);
int (*blob_close)(sqlite3_blob*);
int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
int,sqlite3_blob**);
int (*blob_read)(sqlite3_blob*,void*,int,int);
int (*blob_write)(sqlite3_blob*,const void*,int,int);
int (*create_collation_v2)(sqlite3*,const char*,int,void*,
int(*)(void*,int,const void*,int,const void*),
void(*)(void*));
int (*file_control)(sqlite3*,const char*,int,void*);
sqlite3_int64 (*memory_highwater)(int);
sqlite3_int64 (*memory_used)(void);
sqlite3_mutex *(*mutex_alloc)(int);
void (*mutex_enter)(sqlite3_mutex*);
void (*mutex_free)(sqlite3_mutex*);
void (*mutex_leave)(sqlite3_mutex*);
int (*mutex_try)(sqlite3_mutex*);
int (*open_v2)(const char*,sqlite3**,int,const char*);
int (*release_memory)(int);
void (*result_error_nomem)(sqlite3_context*);
void (*result_error_toobig)(sqlite3_context*);
int (*sleep)(int);
void (*soft_heap_limit)(int);
sqlite3_vfs *(*vfs_find)(const char*);
int (*vfs_register)(sqlite3_vfs*,int);
int (*vfs_unregister)(sqlite3_vfs*);
int (*xthreadsafe)(void);
void (*result_zeroblob)(sqlite3_context*,int);
void (*result_error_code)(sqlite3_context*,int);
int (*test_control)(int, ...);
void (*randomness)(int,void*);
sqlite3 *(*context_db_handle)(sqlite3_context*);
int (*extended_result_codes)(sqlite3*,int);
int (*limit)(sqlite3*,int,int);
sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
const char *(*sql)(sqlite3_stmt*);
int (*status)(int,int*,int*,int);
int (*backup_finish)(sqlite3_backup*);
sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
int (*backup_pagecount)(sqlite3_backup*);
int (*backup_remaining)(sqlite3_backup*);
int (*backup_step)(sqlite3_backup*,int);
const char *(*compileoption_get)(int);
int (*compileoption_used)(const char*);
int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*),
void(*xDestroy)(void*));
int (*db_config)(sqlite3*,int,...);
sqlite3_mutex *(*db_mutex)(sqlite3*);
int (*db_status)(sqlite3*,int,int*,int*,int);
int (*extended_errcode)(sqlite3*);
void (*log)(int,const char*,...);
sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
const char *(*sourceid)(void);
int (*stmt_status)(sqlite3_stmt*,int,int);
int (*strnicmp)(const char*,const char*,int);
int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
int (*wal_autocheckpoint)(sqlite3*,int);
int (*wal_checkpoint)(sqlite3*,const char*);
void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
int (*vtab_config)(sqlite3*,int op,...);
int (*vtab_on_conflict)(sqlite3*);
/* Version 3.7.16 and later */
int (*close_v2)(sqlite3*);
const char *(*db_filename)(sqlite3*,const char*);
int (*db_readonly)(sqlite3*,const char*);
int (*db_release_memory)(sqlite3*);
const char *(*errstr)(int);
int (*stmt_busy)(sqlite3_stmt*);
int (*stmt_readonly)(sqlite3_stmt*);
int (*stricmp)(const char*,const char*);
int (*uri_boolean)(const char*,const char*,int);
sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
const char *(*uri_parameter)(const char*,const char*);
char *(*xvsnprintf)(int,char*,const char*,va_list);
int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
/* Version 3.8.7 and later */
int (*auto_extension)(void(*)(void));
int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
void(*)(void*));
int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
void(*)(void*),unsigned char);
int (*cancel_auto_extension)(void(*)(void));
int (*load_extension)(sqlite3*,const char*,const char*,char**);
void *(*malloc64)(sqlite3_uint64);
sqlite3_uint64 (*msize)(void*);
void *(*realloc64)(void*,sqlite3_uint64);
void (*reset_auto_extension)(void);
void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
void(*)(void*));
void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
void(*)(void*), unsigned char);
int (*strglob)(const char*,const char*);
/* Version 3.8.11 and later */
sqlite3_value *(*value_dup)(const sqlite3_value*);
void (*value_free)(sqlite3_value*);
int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
/* Version 3.9.0 and later */
unsigned int (*value_subtype)(sqlite3_value*);
void (*result_subtype)(sqlite3_context*,unsigned int);
/* Version 3.10.0 and later */
int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
int (*strlike)(const char*,const char*,unsigned int);
int (*db_cacheflush)(sqlite3*);
/* Version 3.12.0 and later */
int (*system_errno)(sqlite3*);
/* Version 3.14.0 and later */
int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
char *(*expanded_sql)(sqlite3_stmt*);
/* Version 3.18.0 and later */
void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
/* Version 3.20.0 and later */
int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
sqlite3_stmt**,const char**);
int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
sqlite3_stmt**,const void**);
int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
void *(*value_pointer)(sqlite3_value*,const char*);
int (*vtab_nochange)(sqlite3_context*);
int (*value_nochange)(sqlite3_value*);
const char *(*vtab_collation)(sqlite3_index_info*,int);
/* Version 3.24.0 and later */
int (*keyword_count)(void);
int (*keyword_name)(int,const char**,int*);
int (*keyword_check)(const char*,int);
sqlite3_str *(*str_new)(sqlite3*);
char *(*str_finish)(sqlite3_str*);
void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
void (*str_append)(sqlite3_str*, const char *zIn, int N);
void (*str_appendall)(sqlite3_str*, const char *zIn);
void (*str_appendchar)(sqlite3_str*, int N, char C);
void (*str_reset)(sqlite3_str*);
int (*str_errcode)(sqlite3_str*);
int (*str_length)(sqlite3_str*);
char *(*str_value)(sqlite3_str*);
/* Version 3.25.0 and later */
int (*create_window_function)(sqlite3*,const char*,int,int,void*,
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*),
void (*xValue)(sqlite3_context*),
void (*xInv)(sqlite3_context*,int,sqlite3_value**),
void(*xDestroy)(void*));
/* Version 3.26.0 and later */
const char *(*normalized_sql)(sqlite3_stmt*);
/* Version 3.28.0 and later */
int (*stmt_isexplain)(sqlite3_stmt*);
int (*value_frombind)(sqlite3_value*);
/* Version 3.30.0 and later */
int (*drop_modules)(sqlite3*,const char**);
/* Version 3.31.0 and later */
sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
const char *(*uri_key)(const char*,int);
const char *(*filename_database)(const char*);
const char *(*filename_journal)(const char*);
const char *(*filename_wal)(const char*);
/* Version 3.32.0 and later */
char *(*create_filename)(const char*,const char*,const char*,
int,const char**);
void (*free_filename)(char*);
sqlite3_file *(*database_file_object)(const char*);
};
/*
** This is the function signature used for all extension entry points. It
** is also defined in the file "loadext.c".
*/
typedef int (*sqlite3_loadext_entry)(
sqlite3 *db, /* Handle to the database. */
char **pzErrMsg, /* Used to set error string on failure. */
const sqlite3_api_routines *pThunk /* Extension API function pointers. */
);
/*
** The following macros redefine the API routines so that they are
** redirected through the global sqlite3_api structure.
**
** This header file is also used by the loadext.c source file
** (part of the main SQLite library - not an extension) so that
** it can get access to the sqlite3_api_routines structure
** definition. But the main library does not want to redefine
** the API. So the redefinition macros are only valid if the
** SQLITE_CORE macros is undefined.
*/
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
#define sqlite3_aggregate_context sqlite3_api->aggregate_context
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_aggregate_count sqlite3_api->aggregate_count
#endif
#define sqlite3_bind_blob sqlite3_api->bind_blob
#define sqlite3_bind_double sqlite3_api->bind_double
#define sqlite3_bind_int sqlite3_api->bind_int
#define sqlite3_bind_int64 sqlite3_api->bind_int64
#define sqlite3_bind_null sqlite3_api->bind_null
#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
#define sqlite3_bind_text sqlite3_api->bind_text
#define sqlite3_bind_text16 sqlite3_api->bind_text16
#define sqlite3_bind_value sqlite3_api->bind_value
#define sqlite3_busy_handler sqlite3_api->busy_handler
#define sqlite3_busy_timeout sqlite3_api->busy_timeout
#define sqlite3_changes sqlite3_api->changes
#define sqlite3_close sqlite3_api->close
#define sqlite3_collation_needed sqlite3_api->collation_needed
#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
#define sqlite3_column_blob sqlite3_api->column_blob
#define sqlite3_column_bytes sqlite3_api->column_bytes
#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
#define sqlite3_column_count sqlite3_api->column_count
#define sqlite3_column_database_name sqlite3_api->column_database_name
#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
#define sqlite3_column_decltype sqlite3_api->column_decltype
#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
#define sqlite3_column_double sqlite3_api->column_double
#define sqlite3_column_int sqlite3_api->column_int
#define sqlite3_column_int64 sqlite3_api->column_int64
#define sqlite3_column_name sqlite3_api->column_name
#define sqlite3_column_name16 sqlite3_api->column_name16
#define sqlite3_column_origin_name sqlite3_api->column_origin_name
#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
#define sqlite3_column_table_name sqlite3_api->column_table_name
#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
#define sqlite3_column_text sqlite3_api->column_text
#define sqlite3_column_text16 sqlite3_api->column_text16
#define sqlite3_column_type sqlite3_api->column_type
#define sqlite3_column_value sqlite3_api->column_value
#define sqlite3_commit_hook sqlite3_api->commit_hook
#define sqlite3_complete sqlite3_api->complete
#define sqlite3_complete16 sqlite3_api->complete16
#define sqlite3_create_collation sqlite3_api->create_collation
#define sqlite3_create_collation16 sqlite3_api->create_collation16
#define sqlite3_create_function sqlite3_api->create_function
#define sqlite3_create_function16 sqlite3_api->create_function16
#define sqlite3_create_module sqlite3_api->create_module
#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
#define sqlite3_data_count sqlite3_api->data_count
#define sqlite3_db_handle sqlite3_api->db_handle
#define sqlite3_declare_vtab sqlite3_api->declare_vtab
#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
#define sqlite3_errcode sqlite3_api->errcode
#define sqlite3_errmsg sqlite3_api->errmsg
#define sqlite3_errmsg16 sqlite3_api->errmsg16
#define sqlite3_exec sqlite3_api->exec
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_expired sqlite3_api->expired
#endif
#define sqlite3_finalize sqlite3_api->finalize
#define sqlite3_free sqlite3_api->free
#define sqlite3_free_table sqlite3_api->free_table
#define sqlite3_get_autocommit sqlite3_api->get_autocommit
#define sqlite3_get_auxdata sqlite3_api->get_auxdata
#define sqlite3_get_table sqlite3_api->get_table
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_global_recover sqlite3_api->global_recover
#endif
#define sqlite3_interrupt sqlite3_api->interruptx
#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
#define sqlite3_libversion sqlite3_api->libversion
#define sqlite3_libversion_number sqlite3_api->libversion_number
#define sqlite3_malloc sqlite3_api->malloc
#define sqlite3_mprintf sqlite3_api->mprintf
#define sqlite3_open sqlite3_api->open
#define sqlite3_open16 sqlite3_api->open16
#define sqlite3_prepare sqlite3_api->prepare
#define sqlite3_prepare16 sqlite3_api->prepare16
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
#define sqlite3_profile sqlite3_api->profile
#define sqlite3_progress_handler sqlite3_api->progress_handler
#define sqlite3_realloc sqlite3_api->realloc
#define sqlite3_reset sqlite3_api->reset
#define sqlite3_result_blob sqlite3_api->result_blob
#define sqlite3_result_double sqlite3_api->result_double
#define sqlite3_result_error sqlite3_api->result_error
#define sqlite3_result_error16 sqlite3_api->result_error16
#define sqlite3_result_int sqlite3_api->result_int
#define sqlite3_result_int64 sqlite3_api->result_int64
#define sqlite3_result_null sqlite3_api->result_null
#define sqlite3_result_text sqlite3_api->result_text
#define sqlite3_result_text16 sqlite3_api->result_text16
#define sqlite3_result_text16be sqlite3_api->result_text16be
#define sqlite3_result_text16le sqlite3_api->result_text16le
#define sqlite3_result_value sqlite3_api->result_value
#define sqlite3_rollback_hook sqlite3_api->rollback_hook
#define sqlite3_set_authorizer sqlite3_api->set_authorizer
#define sqlite3_set_auxdata sqlite3_api->set_auxdata
#define sqlite3_snprintf sqlite3_api->xsnprintf
#define sqlite3_step sqlite3_api->step
#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
#define sqlite3_total_changes sqlite3_api->total_changes
#define sqlite3_trace sqlite3_api->trace
#ifndef SQLITE_OMIT_DEPRECATED
#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
#endif
#define sqlite3_update_hook sqlite3_api->update_hook
#define sqlite3_user_data sqlite3_api->user_data
#define sqlite3_value_blob sqlite3_api->value_blob
#define sqlite3_value_bytes sqlite3_api->value_bytes
#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
#define sqlite3_value_double sqlite3_api->value_double
#define sqlite3_value_int sqlite3_api->value_int
#define sqlite3_value_int64 sqlite3_api->value_int64
#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
#define sqlite3_value_text sqlite3_api->value_text
#define sqlite3_value_text16 sqlite3_api->value_text16
#define sqlite3_value_text16be sqlite3_api->value_text16be
#define sqlite3_value_text16le sqlite3_api->value_text16le
#define sqlite3_value_type sqlite3_api->value_type
#define sqlite3_vmprintf sqlite3_api->vmprintf
#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
#define sqlite3_overload_function sqlite3_api->overload_function
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
#define sqlite3_clear_bindings sqlite3_api->clear_bindings
#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
#define sqlite3_blob_bytes sqlite3_api->blob_bytes
#define sqlite3_blob_close sqlite3_api->blob_close
#define sqlite3_blob_open sqlite3_api->blob_open
#define sqlite3_blob_read sqlite3_api->blob_read
#define sqlite3_blob_write sqlite3_api->blob_write
#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
#define sqlite3_file_control sqlite3_api->file_control
#define sqlite3_memory_highwater sqlite3_api->memory_highwater
#define sqlite3_memory_used sqlite3_api->memory_used
#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
#define sqlite3_mutex_enter sqlite3_api->mutex_enter
#define sqlite3_mutex_free sqlite3_api->mutex_free
#define sqlite3_mutex_leave sqlite3_api->mutex_leave
#define sqlite3_mutex_try sqlite3_api->mutex_try
#define sqlite3_open_v2 sqlite3_api->open_v2
#define sqlite3_release_memory sqlite3_api->release_memory
#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
#define sqlite3_sleep sqlite3_api->sleep
#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
#define sqlite3_vfs_find sqlite3_api->vfs_find
#define sqlite3_vfs_register sqlite3_api->vfs_register
#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
#define sqlite3_threadsafe sqlite3_api->xthreadsafe
#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
#define sqlite3_result_error_code sqlite3_api->result_error_code
#define sqlite3_test_control sqlite3_api->test_control
#define sqlite3_randomness sqlite3_api->randomness
#define sqlite3_context_db_handle sqlite3_api->context_db_handle
#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
#define sqlite3_limit sqlite3_api->limit
#define sqlite3_next_stmt sqlite3_api->next_stmt
#define sqlite3_sql sqlite3_api->sql
#define sqlite3_status sqlite3_api->status
#define sqlite3_backup_finish sqlite3_api->backup_finish
#define sqlite3_backup_init sqlite3_api->backup_init
#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
#define sqlite3_backup_remaining sqlite3_api->backup_remaining
#define sqlite3_backup_step sqlite3_api->backup_step
#define sqlite3_compileoption_get sqlite3_api->compileoption_get
#define sqlite3_compileoption_used sqlite3_api->compileoption_used
#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
#define sqlite3_db_config sqlite3_api->db_config
#define sqlite3_db_mutex sqlite3_api->db_mutex
#define sqlite3_db_status sqlite3_api->db_status
#define sqlite3_extended_errcode sqlite3_api->extended_errcode
#define sqlite3_log sqlite3_api->log
#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
#define sqlite3_sourceid sqlite3_api->sourceid
#define sqlite3_stmt_status sqlite3_api->stmt_status
#define sqlite3_strnicmp sqlite3_api->strnicmp
#define sqlite3_unlock_notify sqlite3_api->unlock_notify
#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
#define sqlite3_wal_hook sqlite3_api->wal_hook
#define sqlite3_blob_reopen sqlite3_api->blob_reopen
#define sqlite3_vtab_config sqlite3_api->vtab_config
#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
/* Version 3.7.16 and later */
#define sqlite3_close_v2 sqlite3_api->close_v2
#define sqlite3_db_filename sqlite3_api->db_filename
#define sqlite3_db_readonly sqlite3_api->db_readonly
#define sqlite3_db_release_memory sqlite3_api->db_release_memory
#define sqlite3_errstr sqlite3_api->errstr
#define sqlite3_stmt_busy sqlite3_api->stmt_busy
#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
#define sqlite3_stricmp sqlite3_api->stricmp
#define sqlite3_uri_boolean sqlite3_api->uri_boolean
#define sqlite3_uri_int64 sqlite3_api->uri_int64
#define sqlite3_uri_parameter sqlite3_api->uri_parameter
#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
/* Version 3.8.7 and later */
#define sqlite3_auto_extension sqlite3_api->auto_extension
#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
#define sqlite3_bind_text64 sqlite3_api->bind_text64
#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
#define sqlite3_load_extension sqlite3_api->load_extension
#define sqlite3_malloc64 sqlite3_api->malloc64
#define sqlite3_msize sqlite3_api->msize
#define sqlite3_realloc64 sqlite3_api->realloc64
#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
#define sqlite3_result_blob64 sqlite3_api->result_blob64
#define sqlite3_result_text64 sqlite3_api->result_text64
#define sqlite3_strglob sqlite3_api->strglob
/* Version 3.8.11 and later */
#define sqlite3_value_dup sqlite3_api->value_dup
#define sqlite3_value_free sqlite3_api->value_free
#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
/* Version 3.9.0 and later */
#define sqlite3_value_subtype sqlite3_api->value_subtype
#define sqlite3_result_subtype sqlite3_api->result_subtype
/* Version 3.10.0 and later */
#define sqlite3_status64 sqlite3_api->status64
#define sqlite3_strlike sqlite3_api->strlike
#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
/* Version 3.12.0 and later */
#define sqlite3_system_errno sqlite3_api->system_errno
/* Version 3.14.0 and later */
#define sqlite3_trace_v2 sqlite3_api->trace_v2
#define sqlite3_expanded_sql sqlite3_api->expanded_sql
/* Version 3.18.0 and later */
#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
/* Version 3.20.0 and later */
#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
#define sqlite3_bind_pointer sqlite3_api->bind_pointer
#define sqlite3_result_pointer sqlite3_api->result_pointer
#define sqlite3_value_pointer sqlite3_api->value_pointer
/* Version 3.22.0 and later */
#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
#define sqlite3_value_nochange sqlite3_api->value_nochange
#define sqlite3_vtab_collation sqlite3_api->vtab_collation
/* Version 3.24.0 and later */
#define sqlite3_keyword_count sqlite3_api->keyword_count
#define sqlite3_keyword_name sqlite3_api->keyword_name
#define sqlite3_keyword_check sqlite3_api->keyword_check
#define sqlite3_str_new sqlite3_api->str_new
#define sqlite3_str_finish sqlite3_api->str_finish
#define sqlite3_str_appendf sqlite3_api->str_appendf
#define sqlite3_str_vappendf sqlite3_api->str_vappendf
#define sqlite3_str_append sqlite3_api->str_append
#define sqlite3_str_appendall sqlite3_api->str_appendall
#define sqlite3_str_appendchar sqlite3_api->str_appendchar
#define sqlite3_str_reset sqlite3_api->str_reset
#define sqlite3_str_errcode sqlite3_api->str_errcode
#define sqlite3_str_length sqlite3_api->str_length
#define sqlite3_str_value sqlite3_api->str_value
/* Version 3.25.0 and later */
#define sqlite3_create_window_function sqlite3_api->create_window_function
/* Version 3.26.0 and later */
#define sqlite3_normalized_sql sqlite3_api->normalized_sql
/* Version 3.28.0 and later */
#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
#define sqlite3_value_frombind sqlite3_api->value_frombind
/* Version 3.30.0 and later */
#define sqlite3_drop_modules sqlite3_api->drop_modules
/* Version 3.31.0 and later */
#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
#define sqlite3_uri_key sqlite3_api->uri_key
#define sqlite3_filename_database sqlite3_api->filename_database
#define sqlite3_filename_journal sqlite3_api->filename_journal
#define sqlite3_filename_wal sqlite3_api->filename_wal
/* Version 3.32.0 and later */
#define sqlite3_create_filename sqlite3_api->create_filename
#define sqlite3_free_filename sqlite3_api->free_filename
#define sqlite3_database_file_object sqlite3_api->database_file_object
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
/* This case when the file really is being compiled as a loadable
** extension */
# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
# define SQLITE_EXTENSION_INIT3 \
extern const sqlite3_api_routines *sqlite3_api;
#else
/* This case when the file is being statically linked into the
** application */
# define SQLITE_EXTENSION_INIT1 /*no-op*/
# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
# define SQLITE_EXTENSION_INIT3 /*no-op*/
#endif
#endif /* SQLITE3EXT_H */

View File

@ -1,620 +0,0 @@
// sqlite3pp.cpp
//
// The MIT License
//
// Copyright (c) 2015 Wongoo Lee (iwongu at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <cstring>
#include <memory>
#include "sqlite3pp.h"
namespace sqlite3pp
{
null_type ignore;
namespace
{
int busy_handler_impl(void* p, int cnt)
{
auto h = static_cast<database::busy_handler*>(p);
return (*h)(cnt);
}
int commit_hook_impl(void* p)
{
auto h = static_cast<database::commit_handler*>(p);
return (*h)();
}
void rollback_hook_impl(void* p)
{
auto h = static_cast<database::rollback_handler*>(p);
(*h)();
}
void update_hook_impl(void* p, int opcode, char const* dbname, char const* tablename, long long int rowid)
{
auto h = static_cast<database::update_handler*>(p);
(*h)(opcode, dbname, tablename, rowid);
}
int authorizer_impl(void* p, int evcode, char const* p1, char const* p2, char const* dbname, char const* tvname)
{
auto h = static_cast<database::authorize_handler*>(p);
return (*h)(evcode, p1, p2, dbname, tvname);
}
} // namespace
database::database(char const* dbname, int flags, char const* vfs) : db_(nullptr), borrowing_(false)
{
if (dbname) {
auto rc = connect(dbname, flags, vfs);
if (rc != SQLITE_OK)
throw database_error("can't connect database");
}
}
database::database(sqlite3* pdb) : db_(pdb), borrowing_(true)
{
}
database::database(database&& db) : db_(std::move(db.db_)),
borrowing_(std::move(db.borrowing_)),
bh_(std::move(db.bh_)),
ch_(std::move(db.ch_)),
rh_(std::move(db.rh_)),
uh_(std::move(db.uh_)),
ah_(std::move(db.ah_))
{
db.db_ = nullptr;
}
database& database::operator=(database&& db)
{
db_ = std::move(db.db_);
db.db_ = nullptr;
borrowing_ = std::move(db.borrowing_);
bh_ = std::move(db.bh_);
ch_ = std::move(db.ch_);
rh_ = std::move(db.rh_);
uh_ = std::move(db.uh_);
ah_ = std::move(db.ah_);
return *this;
}
database::~database()
{
if (!borrowing_) {
disconnect();
}
}
int database::connect(char const* dbname, int flags, char const* vfs)
{
if (!borrowing_) {
disconnect();
}
return sqlite3_open_v2(dbname, &db_, flags, vfs);
}
int database::disconnect()
{
auto rc = SQLITE_OK;
if (db_) {
rc = sqlite3_close(db_);
if (rc == SQLITE_OK) {
db_ = nullptr;
}
}
return rc;
}
int database::attach(char const* dbname, char const* name)
{
return executef("ATTACH '%q' AS '%q'", dbname, name);
}
int database::detach(char const* name)
{
return executef("DETACH '%q'", name);
}
int database::backup(database& destdb, backup_handler h)
{
return backup("main", destdb, "main", h);
}
int database::backup(char const* dbname, database& destdb, char const* destdbname, backup_handler h, int step_page)
{
sqlite3_backup* bkup = sqlite3_backup_init(destdb.db_, destdbname, db_, dbname);
if (!bkup) {
return error_code();
}
auto rc = SQLITE_OK;
do {
rc = sqlite3_backup_step(bkup, step_page);
if (h) {
h(sqlite3_backup_remaining(bkup), sqlite3_backup_pagecount(bkup), rc);
}
} while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
sqlite3_backup_finish(bkup);
return rc;
}
void database::set_busy_handler(busy_handler h)
{
bh_ = h;
sqlite3_busy_handler(db_, bh_ ? busy_handler_impl : 0, &bh_);
}
void database::set_commit_handler(commit_handler h)
{
ch_ = h;
sqlite3_commit_hook(db_, ch_ ? commit_hook_impl : 0, &ch_);
}
void database::set_rollback_handler(rollback_handler h)
{
rh_ = h;
sqlite3_rollback_hook(db_, rh_ ? rollback_hook_impl : 0, &rh_);
}
void database::set_update_handler(update_handler h)
{
uh_ = h;
sqlite3_update_hook(db_, uh_ ? update_hook_impl : 0, &uh_);
}
void database::set_authorize_handler(authorize_handler h)
{
ah_ = h;
sqlite3_set_authorizer(db_, ah_ ? authorizer_impl : 0, &ah_);
}
long long int database::last_insert_rowid() const
{
return sqlite3_last_insert_rowid(db_);
}
int database::enable_foreign_keys(bool enable)
{
return sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_FKEY, enable ? 1 : 0, nullptr);
}
int database::enable_triggers(bool enable)
{
return sqlite3_db_config(db_, SQLITE_DBCONFIG_ENABLE_TRIGGER, enable ? 1 : 0, nullptr);
}
int database::enable_extended_result_codes(bool enable)
{
return sqlite3_extended_result_codes(db_, enable ? 1 : 0);
}
int database::changes() const
{
return sqlite3_changes(db_);
}
int database::error_code() const
{
return sqlite3_errcode(db_);
}
int database::extended_error_code() const
{
return sqlite3_extended_errcode(db_);
}
char const* database::error_msg() const
{
return sqlite3_errmsg(db_);
}
int database::execute(char const* sql)
{
return sqlite3_exec(db_, sql, 0, 0, 0);
}
int database::executef(char const* sql, ...)
{
va_list ap;
va_start(ap, sql);
std::shared_ptr<char> msql(sqlite3_vmprintf(sql, ap), sqlite3_free);
va_end(ap);
return execute(msql.get());
}
int database::set_busy_timeout(int ms)
{
return sqlite3_busy_timeout(db_, ms);
}
statement::statement(database& db, char const* stmt) : db_(db), stmt_(0), tail_(0)
{
if (stmt) {
auto rc = prepare(stmt);
if (rc != SQLITE_OK)
throw database_error(db_);
}
}
statement::~statement()
{
// finish() can return error. If you want to check the error, call
// finish() explicitly before this object is destructed.
finish();
}
int statement::prepare(char const* stmt)
{
auto rc = finish();
if (rc != SQLITE_OK)
return rc;
return prepare_impl(stmt);
}
int statement::prepare_impl(char const* stmt)
{
return sqlite3_prepare_v2(db_.db_, stmt, std::strlen(stmt), &stmt_, &tail_);
}
int statement::finish()
{
auto rc = SQLITE_OK;
if (stmt_) {
rc = finish_impl(stmt_);
stmt_ = nullptr;
}
tail_ = nullptr;
return rc;
}
int statement::finish_impl(sqlite3_stmt* stmt)
{
return sqlite3_finalize(stmt);
}
int statement::step()
{
return sqlite3_step(stmt_);
}
int statement::reset()
{
return sqlite3_reset(stmt_);
}
int statement::bind(int idx, int value)
{
return sqlite3_bind_int(stmt_, idx, value);
}
int statement::bind(int idx, double value)
{
return sqlite3_bind_double(stmt_, idx, value);
}
int statement::bind(int idx, long long int value)
{
return sqlite3_bind_int64(stmt_, idx, value);
}
int statement::bind(int idx, char const* value, copy_semantic fcopy)
{
return sqlite3_bind_text(stmt_, idx, value, std::strlen(value), fcopy == copy ? SQLITE_TRANSIENT : SQLITE_STATIC );
}
int statement::bind(int idx, void const* value, int n, copy_semantic fcopy)
{
return sqlite3_bind_blob(stmt_, idx, value, n, fcopy == copy ? SQLITE_TRANSIENT : SQLITE_STATIC );
}
int statement::bind(int idx, std::string const& value, copy_semantic fcopy)
{
return sqlite3_bind_text(stmt_, idx, value.c_str(), value.size(), fcopy == copy ? SQLITE_TRANSIENT : SQLITE_STATIC );
}
int statement::bind(int idx)
{
return sqlite3_bind_null(stmt_, idx);
}
int statement::bind(int idx, null_type)
{
return bind(idx);
}
int statement::bind(char const* name, int value)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value);
}
int statement::bind(char const* name, double value)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value);
}
int statement::bind(char const* name, long long int value)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value);
}
int statement::bind(char const* name, char const* value, copy_semantic fcopy)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value, fcopy);
}
int statement::bind(char const* name, void const* value, int n, copy_semantic fcopy)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value, n, fcopy);
}
int statement::bind(char const* name, std::string const& value, copy_semantic fcopy)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx, value, fcopy);
}
int statement::bind(char const* name)
{
auto idx = sqlite3_bind_parameter_index(stmt_, name);
return bind(idx);
}
int statement::bind(char const* name, null_type)
{
return bind(name);
}
command::bindstream::bindstream(command& cmd, int idx) : cmd_(cmd), idx_(idx)
{
}
command::command(database& db, char const* stmt) : statement(db, stmt)
{
}
command::bindstream command::binder(int idx)
{
return bindstream(*this, idx);
}
int command::execute()
{
auto rc = step();
if (rc == SQLITE_DONE) rc = SQLITE_OK;
return rc;
}
int command::execute_all()
{
auto rc = execute();
if (rc != SQLITE_OK) return rc;
char const* sql = tail_;
while (std::strlen(sql) > 0) { // sqlite3_complete() is broken.
sqlite3_stmt* old_stmt = stmt_;
if ((rc = prepare_impl(sql)) != SQLITE_OK) return rc;
if ((rc = sqlite3_transfer_bindings(old_stmt, stmt_)) != SQLITE_OK) return rc;
finish_impl(old_stmt);
if ((rc = execute()) != SQLITE_OK) return rc;
sql = tail_;
}
return rc;
}
query::rows::getstream::getstream(rows* rws, int idx) : rws_(rws), idx_(idx)
{
}
query::rows::rows(sqlite3_stmt* stmt) : stmt_(stmt)
{
}
int query::rows::data_count() const
{
return sqlite3_data_count(stmt_);
}
int query::rows::column_type(int idx) const
{
return sqlite3_column_type(stmt_, idx);
}
int query::rows::column_bytes(int idx) const
{
return sqlite3_column_bytes(stmt_, idx);
}
int query::rows::get(int idx, int) const
{
return sqlite3_column_int(stmt_, idx);
}
double query::rows::get(int idx, double) const
{
return sqlite3_column_double(stmt_, idx);
}
long long int query::rows::get(int idx, long long int) const
{
return sqlite3_column_int64(stmt_, idx);
}
char const* query::rows::get(int idx, char const*) const
{
return reinterpret_cast<char const*>(sqlite3_column_text(stmt_, idx));
}
std::string query::rows::get(int idx, std::string) const
{
return get(idx, (char const*)0);
}
void const* query::rows::get(int idx, void const*) const
{
return sqlite3_column_blob(stmt_, idx);
}
null_type query::rows::get(int /*idx*/, null_type) const
{
return ignore;
}
query::rows::getstream query::rows::getter(int idx)
{
return getstream(this, idx);
}
query::query_iterator::query_iterator() : cmd_(0)
{
rc_ = SQLITE_DONE;
}
query::query_iterator::query_iterator(query* cmd) : cmd_(cmd)
{
rc_ = cmd_->step();
if (rc_ != SQLITE_ROW && rc_ != SQLITE_DONE)
throw database_error(cmd_->db_);
}
bool query::query_iterator::operator==(query::query_iterator const& other) const
{
return rc_ == other.rc_;
}
bool query::query_iterator::operator!=(query::query_iterator const& other) const
{
return rc_ != other.rc_;
}
query::query_iterator& query::query_iterator::operator++()
{
rc_ = cmd_->step();
if (rc_ != SQLITE_ROW && rc_ != SQLITE_DONE)
throw database_error(cmd_->db_);
return *this;
}
query::query_iterator::value_type query::query_iterator::operator*() const
{
return rows(cmd_->stmt_);
}
query::query(database& db, char const* stmt) : statement(db, stmt)
{
}
int query::column_count() const
{
return sqlite3_column_count(stmt_);
}
char const* query::column_name(int idx) const
{
return sqlite3_column_name(stmt_, idx);
}
char const* query::column_decltype(int idx) const
{
return sqlite3_column_decltype(stmt_, idx);
}
query::iterator query::begin()
{
return query_iterator(this);
}
query::iterator query::end()
{
return query_iterator();
}
transaction::transaction(database& db, bool fcommit, bool freserve) : db_(&db), fcommit_(fcommit)
{
int rc = db_->execute(freserve ? "BEGIN IMMEDIATE" : "BEGIN");
if (rc != SQLITE_OK)
throw database_error(*db_);
}
transaction::~transaction()
{
if (db_) {
// execute() can return error. If you want to check the error,
// call commit() or rollback() explicitly before this object is
// destructed.
db_->execute(fcommit_ ? "COMMIT" : "ROLLBACK");
}
}
int transaction::commit()
{
auto db = db_;
db_ = nullptr;
int rc = db->execute("COMMIT");
return rc;
}
int transaction::rollback()
{
auto db = db_;
db_ = nullptr;
int rc = db->execute("ROLLBACK");
return rc;
}
database_error::database_error(char const* msg) : std::runtime_error(msg)
{
}
database_error::database_error(database& db) : std::runtime_error(sqlite3_errmsg(db.db_))
{
}
} // namespace sqlite3pp

View File

@ -1,345 +0,0 @@
// sqlite3pp.h
//
// The MIT License
//
// Copyright (c) 2015 Wongoo Lee (iwongu at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef SQLITE3PP_H
#define SQLITE3PP_H
#define SQLITE3PP_VERSION "1.0.8"
#define SQLITE3PP_VERSION_MAJOR 1
#define SQLITE3PP_VERSION_MINOR 0
#define SQLITE3PP_VERSION_PATCH 8
#include <functional>
#include <iterator>
#include <stdexcept>
#include <string>
#include <tuple>
#ifdef SQLITE3PP_LOADABLE_EXTENSION
#include <sqlite3ext.h>
SQLITE_EXTENSION_INIT1
#else
# include "sqlite3.h"
#endif
namespace sqlite3pp
{
class database;
namespace ext
{
class function;
class aggregate;
database borrow(sqlite3* pdb);
}
template <class T>
struct convert {
using to_int = int;
};
class null_type {};
extern null_type ignore;
class noncopyable
{
protected:
noncopyable() = default;
~noncopyable() = default;
noncopyable(noncopyable&&) = default;
noncopyable& operator=(noncopyable&&) = default;
noncopyable(noncopyable const&) = delete;
noncopyable& operator=(noncopyable const&) = delete;
};
class database : noncopyable
{
friend class statement;
friend class database_error;
friend class ext::function;
friend class ext::aggregate;
friend database ext::borrow(sqlite3* pdb);
public:
using busy_handler = std::function<int (int)>;
using commit_handler = std::function<int ()>;
using rollback_handler = std::function<void ()>;
using update_handler = std::function<void (int, char const*, char const*, long long int)>;
using authorize_handler = std::function<int (int, char const*, char const*, char const*, char const*)>;
using backup_handler = std::function<void (int, int, int)>;
explicit database(char const* dbname = nullptr, int flags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, const char* vfs = nullptr);
database(database&& db);
database& operator=(database&& db);
~database();
int connect(char const* dbname, int flags, const char* vfs = nullptr);
int disconnect();
int attach(char const* dbname, char const* name);
int detach(char const* name);
int backup(database& destdb, backup_handler h = {});
int backup(char const* dbname, database& destdb, char const* destdbname, backup_handler h, int step_page = 5);
long long int last_insert_rowid() const;
int enable_foreign_keys(bool enable = true);
int enable_triggers(bool enable = true);
int enable_extended_result_codes(bool enable = true);
int changes() const;
int error_code() const;
int extended_error_code() const;
char const* error_msg() const;
int execute(char const* sql);
int executef(char const* sql, ...);
int set_busy_timeout(int ms);
void set_busy_handler(busy_handler h);
void set_commit_handler(commit_handler h);
void set_rollback_handler(rollback_handler h);
void set_update_handler(update_handler h);
void set_authorize_handler(authorize_handler h);
private:
database(sqlite3* pdb);
private:
sqlite3* db_;
bool borrowing_;
busy_handler bh_;
commit_handler ch_;
rollback_handler rh_;
update_handler uh_;
authorize_handler ah_;
};
class database_error : public std::runtime_error
{
public:
explicit database_error(char const* msg);
explicit database_error(database& db);
};
enum copy_semantic { copy, nocopy };
class statement : noncopyable
{
public:
int prepare(char const* stmt);
int finish();
int bind(int idx, int value);
int bind(int idx, double value);
int bind(int idx, long long int value);
int bind(int idx, char const* value, copy_semantic fcopy);
int bind(int idx, void const* value, int n, copy_semantic fcopy);
int bind(int idx, std::string const& value, copy_semantic fcopy);
int bind(int idx);
int bind(int idx, null_type);
int bind(char const* name, int value);
int bind(char const* name, double value);
int bind(char const* name, long long int value);
int bind(char const* name, char const* value, copy_semantic fcopy);
int bind(char const* name, void const* value, int n, copy_semantic fcopy);
int bind(char const* name, std::string const& value, copy_semantic fcopy);
int bind(char const* name);
int bind(char const* name, null_type);
int step();
int reset();
protected:
explicit statement(database& db, char const* stmt = nullptr);
~statement();
int prepare_impl(char const* stmt);
int finish_impl(sqlite3_stmt* stmt);
protected:
database& db_;
sqlite3_stmt* stmt_;
char const* tail_;
};
class command : public statement
{
public:
class bindstream
{
public:
bindstream(command& cmd, int idx);
template <class T>
bindstream& operator << (T value) {
auto rc = cmd_.bind(idx_, value);
if (rc != SQLITE_OK) {
throw database_error(cmd_.db_);
}
++idx_;
return *this;
}
bindstream& operator << (char const* value) {
auto rc = cmd_.bind(idx_, value, copy);
if (rc != SQLITE_OK) {
throw database_error(cmd_.db_);
}
++idx_;
return *this;
}
bindstream& operator << (std::string const& value) {
auto rc = cmd_.bind(idx_, value, copy);
if (rc != SQLITE_OK) {
throw database_error(cmd_.db_);
}
++idx_;
return *this;
}
private:
command& cmd_;
int idx_;
};
explicit command(database& db, char const* stmt = nullptr);
bindstream binder(int idx = 1);
int execute();
int execute_all();
};
class query : public statement
{
public:
class rows
{
public:
class getstream
{
public:
getstream(rows* rws, int idx);
template <class T>
getstream& operator >> (T& value) {
value = rws_->get(idx_, T());
++idx_;
return *this;
}
private:
rows* rws_;
int idx_;
};
explicit rows(sqlite3_stmt* stmt);
int data_count() const;
int column_type(int idx) const;
int column_bytes(int idx) const;
template <class T> T get(int idx) const {
return get(idx, T());
}
template <class... Ts>
std::tuple<Ts...> get_columns(typename convert<Ts>::to_int... idxs) const {
return std::make_tuple(get(idxs, Ts())...);
}
getstream getter(int idx = 0);
private:
int get(int idx, int) const;
double get(int idx, double) const;
long long int get(int idx, long long int) const;
char const* get(int idx, char const*) const;
std::string get(int idx, std::string) const;
void const* get(int idx, void const*) const;
null_type get(int idx, null_type) const;
private:
sqlite3_stmt* stmt_;
};
class query_iterator
: public std::iterator<std::input_iterator_tag, rows>
{
public:
query_iterator();
explicit query_iterator(query* cmd);
bool operator==(query_iterator const&) const;
bool operator!=(query_iterator const&) const;
query_iterator& operator++();
value_type operator*() const;
private:
query* cmd_;
int rc_;
};
explicit query(database& db, char const* stmt = nullptr);
int column_count() const;
char const* column_name(int idx) const;
char const* column_decltype(int idx) const;
using iterator = query_iterator;
iterator begin();
iterator end();
};
class transaction : noncopyable
{
public:
explicit transaction(database& db, bool fcommit = false, bool freserve = false);
~transaction();
int commit();
int rollback();
private:
database* db_;
bool fcommit_;
};
} // namespace sqlite3pp
#endif

View File

@ -1,200 +0,0 @@
// sqlite3ppext.cpp
//
// The MIT License
//
// Copyright (c) 2015 Wongoo Lee (iwongu at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include <cstring>
#include "sqlite3ppext.h"
namespace sqlite3pp
{
namespace ext
{
namespace
{
void function_impl(sqlite3_context* ctx, int nargs, sqlite3_value** values)
{
auto f = static_cast<function::function_handler*>(sqlite3_user_data(ctx));
context c(ctx, nargs, values);
(*f)(c);
}
void step_impl(sqlite3_context* ctx, int nargs, sqlite3_value** values)
{
auto p = static_cast<std::pair<aggregate::pfunction_base, aggregate::pfunction_base>*>(sqlite3_user_data(ctx));
auto s = static_cast<aggregate::function_handler*>((*p).first.get());
context c(ctx, nargs, values);
((function::function_handler&)*s)(c);
}
void finalize_impl(sqlite3_context* ctx)
{
auto p = static_cast<std::pair<aggregate::pfunction_base, aggregate::pfunction_base>*>(sqlite3_user_data(ctx));
auto f = static_cast<aggregate::function_handler*>((*p).second.get());
context c(ctx);
((function::function_handler&)*f)(c);
}
} // namespace
database borrow(sqlite3* pdb) {
return database(pdb);
}
context::context(sqlite3_context* ctx, int nargs, sqlite3_value** values)
: ctx_(ctx), nargs_(nargs), values_(values)
{
}
int context::args_count() const
{
return nargs_;
}
int context::args_bytes(int idx) const
{
return sqlite3_value_bytes(values_[idx]);
}
int context::args_type(int idx) const
{
return sqlite3_value_type(values_[idx]);
}
int context::get(int idx, int) const
{
return sqlite3_value_int(values_[idx]);
}
double context::get(int idx, double) const
{
return sqlite3_value_double(values_[idx]);
}
long long int context::get(int idx, long long int) const
{
return sqlite3_value_int64(values_[idx]);
}
char const* context::get(int idx, char const*) const
{
return reinterpret_cast<char const*>(sqlite3_value_text(values_[idx]));
}
std::string context::get(int idx, std::string) const
{
return get(idx, (char const*)0);
}
void const* context::get(int idx, void const*) const
{
return sqlite3_value_blob(values_[idx]);
}
void context::result(int value)
{
sqlite3_result_int(ctx_, value);
}
void context::result(double value)
{
sqlite3_result_double(ctx_, value);
}
void context::result(long long int value)
{
sqlite3_result_int64(ctx_, value);
}
void context::result(std::string const& value)
{
result(value.c_str(), false);
}
void context::result(char const* value, bool fcopy)
{
sqlite3_result_text(ctx_, value, std::strlen(value), fcopy ? SQLITE_TRANSIENT : SQLITE_STATIC);
}
void context::result(void const* value, int n, bool fcopy)
{
sqlite3_result_blob(ctx_, value, n, fcopy ? SQLITE_TRANSIENT : SQLITE_STATIC );
}
void context::result()
{
sqlite3_result_null(ctx_);
}
void context::result(null_type)
{
sqlite3_result_null(ctx_);
}
void context::result_copy(int idx)
{
sqlite3_result_value(ctx_, values_[idx]);
}
void context::result_error(char const* msg)
{
sqlite3_result_error(ctx_, msg, std::strlen(msg));
}
void* context::aggregate_data(int size)
{
return sqlite3_aggregate_context(ctx_, size);
}
int context::aggregate_count()
{
return sqlite3_aggregate_count(ctx_);
}
function::function(database& db) : db_(db.db_)
{
}
int function::create(char const* name, function_handler h, int nargs)
{
fh_[name] = pfunction_base(new function_handler(h));
return sqlite3_create_function(db_, name, nargs, SQLITE_UTF8, fh_[name].get(), function_impl, 0, 0);
}
aggregate::aggregate(database& db) : db_(db.db_)
{
}
int aggregate::create(char const* name, function_handler s, function_handler f, int nargs)
{
ah_[name] = std::make_pair(pfunction_base(new function_handler(s)), pfunction_base(new function_handler(f)));
return sqlite3_create_function(db_, name, nargs, SQLITE_UTF8, &ah_[name], 0, step_impl, finalize_impl);
}
} // namespace ext
} // namespace sqlite3pp

View File

@ -1,232 +0,0 @@
// sqlite3ppext.h
//
// The MIT License
//
// Copyright (c) 2015 Wongoo Lee (iwongu at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef SQLITE3PPEXT_H
#define SQLITE3PPEXT_H
#include <cstddef>
#include <map>
#include <memory>
#include <tuple>
#include <type_traits>
#include <utility>
#include "sqlite3pp.h"
namespace sqlite3pp
{
namespace
{
template<size_t N>
struct Apply {
template<typename F, typename T, typename... A>
static inline auto apply(F&& f, T&& t, A&&... a)
-> decltype(Apply<N-1>::apply(std::forward<F>(f),
std::forward<T>(t),
std::get<N-1>(std::forward<T>(t)),
std::forward<A>(a)...))
{
return Apply<N-1>::apply(std::forward<F>(f),
std::forward<T>(t),
std::get<N-1>(std::forward<T>(t)),
std::forward<A>(a)...);
}
};
template<>
struct Apply<0> {
template<typename F, typename T, typename... A>
static inline auto apply(F&& f, T&&, A&&... a)
-> decltype(std::forward<F>(f)(std::forward<A>(a)...))
{
return std::forward<F>(f)(std::forward<A>(a)...);
}
};
template<typename F, typename T>
inline auto apply(F&& f, T&& t)
-> decltype(Apply<std::tuple_size<typename std::decay<T>::type>::value>::apply(std::forward<F>(f), std::forward<T>(t)))
{
return Apply<std::tuple_size<typename std::decay<T>::type>::value>::apply(
std::forward<F>(f), std::forward<T>(t));
}
}
namespace ext
{
database borrow(sqlite3* pdb);
class context : noncopyable
{
public:
explicit context(sqlite3_context* ctx, int nargs = 0, sqlite3_value** values = nullptr);
int args_count() const;
int args_bytes(int idx) const;
int args_type(int idx) const;
template <class T> T get(int idx) const {
return get(idx, T());
}
void result(int value);
void result(double value);
void result(long long int value);
void result(std::string const& value);
void result(char const* value, bool fcopy);
void result(void const* value, int n, bool fcopy);
void result();
void result(null_type);
void result_copy(int idx);
void result_error(char const* msg);
void* aggregate_data(int size);
int aggregate_count();
template <class... Ts>
std::tuple<Ts...> to_tuple() {
return to_tuple_impl(0, *this, std::tuple<Ts...>());
}
private:
int get(int idx, int) const;
double get(int idx, double) const;
long long int get(int idx, long long int) const;
char const* get(int idx, char const*) const;
std::string get(int idx, std::string) const;
void const* get(int idx, void const*) const;
template<class H, class... Ts>
static inline std::tuple<H, Ts...> to_tuple_impl(int index, const context& c, std::tuple<H, Ts...>&&)
{
auto h = std::make_tuple(c.context::get<H>(index));
return std::tuple_cat(h, to_tuple_impl(++index, c, std::tuple<Ts...>()));
}
static inline std::tuple<> to_tuple_impl(int /*index*/, const context& /*c*/, std::tuple<>&&)
{
return std::tuple<>();
}
private:
sqlite3_context* ctx_;
int nargs_;
sqlite3_value** values_;
};
namespace
{
template <class R, class... Ps>
void functionx_impl(sqlite3_context* ctx, int nargs, sqlite3_value** values)
{
context c(ctx, nargs, values);
auto f = static_cast<std::function<R (Ps...)>*>(sqlite3_user_data(ctx));
c.result(apply(*f, c.to_tuple<Ps...>()));
}
}
class function : noncopyable
{
public:
using function_handler = std::function<void (context&)>;
using pfunction_base = std::shared_ptr<void>;
explicit function(database& db);
int create(char const* name, function_handler h, int nargs = 0);
template <class F> int create(char const* name, std::function<F> h) {
fh_[name] = std::shared_ptr<void>(new std::function<F>(h));
return create_function_impl<F>()(db_, fh_[name].get(), name);
}
private:
template<class R, class... Ps>
struct create_function_impl;
template<class R, class... Ps>
struct create_function_impl<R (Ps...)>
{
int operator()(sqlite3* db, void* fh, char const* name) {
return sqlite3_create_function(db, name, sizeof...(Ps), SQLITE_UTF8, fh,
functionx_impl<R, Ps...>,
0, 0);
}
};
private:
sqlite3* db_;
std::map<std::string, pfunction_base> fh_;
};
namespace
{
template <class T, class... Ps>
void stepx_impl(sqlite3_context* ctx, int nargs, sqlite3_value** values)
{
context c(ctx, nargs, values);
T* t = static_cast<T*>(c.aggregate_data(sizeof(T)));
if (c.aggregate_count() == 1) new (t) T;
apply([](T* tt, Ps... ps){tt->step(ps...);},
std::tuple_cat(std::make_tuple(t), c.to_tuple<Ps...>()));
}
template <class T>
void finishN_impl(sqlite3_context* ctx)
{
context c(ctx);
T* t = static_cast<T*>(c.aggregate_data(sizeof(T)));
c.result(t->finish());
t->~T();
}
}
class aggregate : noncopyable
{
public:
using function_handler = std::function<void (context&)>;
using pfunction_base = std::shared_ptr<void>;
explicit aggregate(database& db);
int create(char const* name, function_handler s, function_handler f, int nargs = 1);
template <class T, class... Ps>
int create(char const* name) {
return sqlite3_create_function(db_, name, sizeof...(Ps), SQLITE_UTF8, 0, 0, stepx_impl<T, Ps...>, finishN_impl<T>);
}
private:
sqlite3* db_;
std::map<std::string, std::pair<pfunction_base, pfunction_base> > ah_;
};
} // namespace ext
} // namespace sqlite3pp
#endif

File diff suppressed because it is too large Load Diff

View File

@ -7,6 +7,7 @@ int settings::VERBOSITY = 1;
int settings::LOGINPORT = 8001; int settings::LOGINPORT = 8001;
bool settings::LOGINRANDCHARACTERS = false; bool settings::LOGINRANDCHARACTERS = false;
bool settings::APPROVEALLNAMES = true;
int settings::SHARDPORT = 8002; int settings::SHARDPORT = 8002;
std::string settings::SHARDSERVERIP = "127.0.0.1"; std::string settings::SHARDSERVERIP = "127.0.0.1";
@ -35,6 +36,7 @@ void settings::init() {
return; return;
} }
APPROVEALLNAMES = reader.GetInteger("", "acceptallcustomnames", APPROVEALLNAMES);
VERBOSITY = reader.GetInteger("", "verbosity", VERBOSITY); VERBOSITY = reader.GetInteger("", "verbosity", VERBOSITY);
LOGINPORT = reader.GetInteger("login", "port", LOGINPORT); LOGINPORT = reader.GetInteger("login", "port", LOGINPORT);
LOGINRANDCHARACTERS = reader.GetBoolean("login", "randomcharacters", LOGINRANDCHARACTERS); LOGINRANDCHARACTERS = reader.GetBoolean("login", "randomcharacters", LOGINRANDCHARACTERS);

View File

@ -4,6 +4,7 @@ namespace settings {
extern int VERBOSITY; extern int VERBOSITY;
extern int LOGINPORT; extern int LOGINPORT;
extern bool LOGINRANDCHARACTERS; extern bool LOGINRANDCHARACTERS;
extern bool APPROVEALLNAMES;
extern int SHARDPORT; extern int SHARDPORT;
extern std::string SHARDSERVERIP; extern std::string SHARDSERVERIP;
extern int PLAYERDISTANCE; extern int PLAYERDISTANCE;