Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Client/core/CVersionUpdater.Util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,8 @@ namespace
m_ArgMap.SetFromString(strSettings);
// If build is 30 days old, default no report logging
m_ArgMap.Get("filter2", strFilter, GetBuildAge() < 30 ? "+all" : "-all");
m_ArgMap.Get("min", iMinSize, DEFAULT_MIN_SIZE);
m_ArgMap.Get("max", iMaxSize, DEFAULT_MAX_SIZE);
m_ArgMap.Get(std::string("min"), iMinSize, DEFAULT_MIN_SIZE);
m_ArgMap.Get(std::string("max"), iMaxSize, DEFAULT_MAX_SIZE);
SaveReportSettings();
}

Expand Down
8 changes: 4 additions & 4 deletions Client/loader/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ void CheckAndShowModelProblems()
CArgMap argMap;
argMap.SetFromString(GetApplicationSetting("diagnostics", "gta-model-fail"));
argMap.Get("reason", strReason);
argMap.Get("id", iModelId);
argMap.Get(std::string("id"), iModelId);
SetApplicationSetting("diagnostics", "gta-model-fail", "");

if (iModelId)
Expand All @@ -1604,9 +1604,9 @@ void CheckAndShowUpgradeProblems()
int iModelId = 0, iUpgradeId, iFrame;
CArgMap argMap;
argMap.SetFromString(GetApplicationSetting("diagnostics", "gta-upgrade-fail"));
argMap.Get("vehid", iModelId);
argMap.Get("upgid", iUpgradeId);
argMap.Get("frame", iFrame);
argMap.Get(std::string("vehid"), iModelId);
argMap.Get(std::string("upgid"), iUpgradeId);
argMap.Get(std::string("frame"), iFrame);
SetApplicationSetting("diagnostics", "gta-upgrade-fail", "");

if (iModelId)
Expand Down
12 changes: 6 additions & 6 deletions Server/dbconmy/CDatabaseConnectionMySql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ CDatabaseConnectionMySql::CDatabaseConnectionMySql(CDatabaseType* pManager, cons
// Parse options string
CArgMap optionsMap("=", ";");
optionsMap.SetFromString(strOptions);
optionsMap.Get("autoreconnect", m_bAutomaticReconnect, 1);
optionsMap.Get("batch", m_bAutomaticTransactionsEnabled, 1);
optionsMap.Get("multi_statements", m_bMultipleStatements, 0);
optionsMap.Get("use_ssl", m_bUseSSL, 0);
optionsMap.Get("get_server_public_key", getServerPublicKey, 1);
optionsMap.Get("autoreconnect"s, m_bAutomaticReconnect, 1);
optionsMap.Get("batch"s, m_bAutomaticTransactionsEnabled, 1);
optionsMap.Get("multi_statements"s, m_bMultipleStatements, 0);
optionsMap.Get("use_ssl"s, m_bUseSSL, 0);
optionsMap.Get("get_server_public_key"s, getServerPublicKey, 1);

SString strHostname;
SString strDatabaseName;
Expand All @@ -103,7 +103,7 @@ CDatabaseConnectionMySql::CDatabaseConnectionMySql(CDatabaseType* pManager, cons
argMap.SetFromString(strHost);
argMap.Get("dbname", strDatabaseName, "");
argMap.Get("host", strHostname, "localhost");
argMap.Get("port", iPort, 0);
argMap.Get("port"s, iPort, 0);
argMap.Get("unix_socket", strUnixSocket, "");
argMap.Get("charset", strCharset, "");

Expand Down
190 changes: 103 additions & 87 deletions Server/mods/deathmatch/logic/luadefs/CLuaDatabaseDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
#include "CPerfStatManager.h"
#include "lua/CLuaCallback.h"
#include "Utils.h"
#include <lua/CLuaShared.h>

void CLuaDatabaseDefs::LoadFunctions()
{
constexpr static const std::pair<const char*, lua_CFunction> functions[]{
{"dbConnect", DbConnect},
{"dbConnect", ArgumentParser<DbConnect>},
{"dbExec", DbExec},
{"dbQuery", DbQuery},
{"dbFree", DbFree},
Expand Down Expand Up @@ -225,106 +226,121 @@ void CLuaDatabaseDefs::DbFreeCallback(CDbJobData* pJobData, void* pContext)
}
}

int CLuaDatabaseDefs::DbConnect(lua_State* luaVM)
std::variant<CDatabaseConnectionElement*, bool> CLuaDatabaseDefs::DbConnect(lua_State* luaVM, std::string type, std::string host,
std::optional<std::string> username, std::optional<std::string> password,
std::optional<std::string> options, std::optional<CLuaFunctionRef> callback)
{
// element dbConnect ( string type, string host, string username, string password, string options )
SString strType;
SString strHost;
SString strUsername;
SString strPassword;
SString strOptions;
if (!username.has_value())
username = "";
if (!password.has_value())
password = "";
if (!options.has_value())
options = "";

CScriptArgReader argStream(luaVM);
argStream.ReadString(strType);
argStream.ReadString(strHost);
argStream.ReadString(strUsername, "");
argStream.ReadString(strPassword, "");
argStream.ReadString(strOptions, "");
CResource* resource = &lua_getownerresource(luaVM);

if (!argStream.HasErrors())
if (type == "sqlite" && !host.empty())
{
CResource* pThisResource = m_pLuaManager->GetVirtualMachineResource(luaVM);
if (pThisResource)
// If path starts with :/ then use global database directory
if (!host.rfind(":/", 0))
{
// If type is sqlite, and has a host, try to resolve path
if (strType == "sqlite" && !strHost.empty())
host = host.substr(1);
if (!IsValidFilePath(host.c_str()))
{
// If path starts with :/ then use global database directory
if (strHost.BeginsWith(":/"))
{
strHost = strHost.SubStr(1);
if (!IsValidFilePath(strHost))
{
argStream.SetCustomError(SString("host path %s not valid", *strHost));
}
else
{
strHost = PathJoin(g_pGame->GetConfig()->GetGlobalDatabasesPath(), strHost);
}
}
else
{
std::string strAbsPath;

// Parse path
CResource* pPathResource = pThisResource;
if (CResourceManager::ParseResourcePathInput(strHost, pPathResource, &strAbsPath))
{
strHost = strAbsPath;
CheckCanModifyOtherResource(argStream, pThisResource, pPathResource);
}
else
{
argStream.SetCustomError(SString("host path %s not found", *strHost));
}
}
SString err("host path %s not valid", host.c_str());
throw LuaFunctionError(err.c_str());
}

if (!argStream.HasErrors())
{
if (strType == "mysql")
pThisResource->SetUsingDbConnectMysql(true);

// Add logging options
bool bLoggingEnabled;
SString strLogTag;
SString strQueueName;
// Set default values if required
GetOption<CDbOptionsMap>(strOptions, "log", bLoggingEnabled, 1);
GetOption<CDbOptionsMap>(strOptions, "tag", strLogTag, "script");
GetOption<CDbOptionsMap>(strOptions, "queue", strQueueName, (strType == "mysql") ? strHost : DB_SQLITE_QUEUE_NAME_DEFAULT);
SetOption<CDbOptionsMap>(strOptions, "log", bLoggingEnabled);
SetOption<CDbOptionsMap>(strOptions, "tag", strLogTag);
SetOption<CDbOptionsMap>(strOptions, "queue", strQueueName);
// Do connect
SConnectionHandle connection = g_pGame->GetDatabaseManager()->Connect(strType, strHost, strUsername, strPassword, strOptions);
if (connection == INVALID_DB_HANDLE)
{
argStream.SetCustomError(g_pGame->GetDatabaseManager()->GetLastErrorMessage());
}
else
{
// Use an element to wrap the connection for auto disconnected when the resource stops
// Don't set a parent because the element should not be accessible from other resources
CDatabaseConnectionElement* pElement = new CDatabaseConnectionElement(NULL, connection);
CElementGroup* pGroup = pThisResource->GetElementGroup();
if (pGroup)
{
pGroup->Add(pElement);
}
host = PathJoin(g_pGame->GetConfig()->GetGlobalDatabasesPath(), host);
}
else
{
std::string absPath;

lua_pushelement(luaVM, pElement);
return 1;
}
// Parse path
CResource* pathResource = resource;
if (CResourceManager::ParseResourcePathInput(host, pathResource, &absPath))
{
host = absPath;
auto [status, err] = CheckCanModifyOtherResource(resource, pathResource);
if (!status)
throw LuaFunctionError(err.c_str());
}
SString err("host path %s not found", host.c_str());
throw LuaFunctionError(err.c_str());
}
}

if (type == "mysql")
resource->SetUsingDbConnectMysql(true);

// Add logging options
bool loggingEnabled;
std::string logTag;
std::string queueName;
// Set default values if required
GetOption<CDbOptionsMap>(*options, "log", loggingEnabled, 1);
GetOption<CDbOptionsMap>(*options, "tag", logTag, "script");
GetOption<CDbOptionsMap>(*options, "queue", queueName, (type == "mysql") ? host.c_str() : DB_SQLITE_QUEUE_NAME_DEFAULT);
SetOption<CDbOptionsMap>(*options, "log", loggingEnabled);
SetOption<CDbOptionsMap>(*options, "tag", logTag);
SetOption<CDbOptionsMap>(*options, "queue", queueName);

const auto CreateConnection = [](CResource* resource, const SConnectionHandle& handle)
-> CDatabaseConnectionElement*
{
// Use an element to wrap the connection for auto disconnected when the resource stops
// Don't set a parent because the element should not be accessible from other resources
auto* element = new CDatabaseConnectionElement(nullptr, handle);
CElementGroup* group = resource->GetElementGroup();
if (group)
group->Add(element);
return element;
};

if (argStream.HasErrors())
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
if (callback.has_value())
{
const auto taskFunc = [type = type, host, username = username.value(), password = password.value(), options = options.value()]
{
return g_pGame->GetDatabaseManager()->Connect(
type,
host,
username,
password,
options
);
};
const auto readyFunc = [CreateConnection = CreateConnection, resource = resource, luaFunctionRef = callback.value()](const SConnectionHandle& handle)
{
CLuaMain* luaMain = m_pLuaManager->GetVirtualMachine(luaFunctionRef.GetLuaVM());
if (!luaMain)
return;

lua_pushboolean(luaVM, false);
return 1;
CLuaArguments arguments;

if (handle == INVALID_DB_HANDLE)
{
auto lastError = g_pGame->GetDatabaseManager()->GetLastErrorMessage();
m_pScriptDebugging->LogCustom(luaMain->GetVM(), lastError.c_str());
arguments.PushBoolean(false);
}

arguments.PushElement(CreateConnection(resource, handle));
arguments.Call(luaMain, luaFunctionRef);
};

CLuaShared::GetAsyncTaskScheduler()->PushTask(taskFunc, readyFunc);
return true;
}

// Do connect
SConnectionHandle connection = g_pGame->GetDatabaseManager()->Connect(
type, host, *username, *password, *options
);
if (connection == INVALID_DB_HANDLE)
throw LuaFunctionError(g_pGame->GetDatabaseManager()->GetLastErrorMessage().c_str());

return CreateConnection(resource, connection);
}

// This method has an OOP counterpart - don't forget to update the OOP code too!
Expand Down
6 changes: 4 additions & 2 deletions Server/mods/deathmatch/logic/luadefs/CLuaDatabaseDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class CLuaDatabaseDefs : public CLuaDefs
static void DbFreeCallback(CDbJobData* pJobData, void* pContext);
static void DbExecCallback(CDbJobData* pJobData, void* pContext);

LUA_DECLARE(DbConnect);
static std::variant<CDatabaseConnectionElement*, bool> DbConnect(lua_State* luaVM, std::string type, std::string host, std::optional<std::string> username,
std::optional<std::string> password, std::optional<std::string> options,
std::optional<CLuaFunctionRef> callback);
LUA_DECLARE(DbExec);
LUA_DECLARE_OOP(DbQuery);
LUA_DECLARE(DbFree);
Expand All @@ -36,4 +38,4 @@ class CLuaDatabaseDefs : public CLuaDefs
LUA_DECLARE(ExecuteSQLSelect);
LUA_DECLARE(ExecuteSQLUpdate);
LUA_DECLARE(ExecuteSQLQuery);
};
};
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/utils/CMasterServerAnnouncer.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class CMasterServer : public CRefCountable
{
CArgMap argMap;
argMap.SetFromString(result.pData);
SString strOkMessage = argMap.Get("ok_message");
SString strOkMessage = argMap.Get("ok_message"s);

// Log successful initial announcement
if (result.iErrorCode == 200)
Expand Down
Loading