diff --git a/core/AMBuilder b/core/AMBuilder index 0962ada0..dbe05310 100644 --- a/core/AMBuilder +++ b/core/AMBuilder @@ -86,13 +86,11 @@ for i in SM.sdkInfo: 'smn_halflife.cpp', 'smn_console.cpp', 'UserMessages.cpp', - 'Database.cpp', 'MenuManager.cpp', 'smn_core.cpp', 'smn_hudtext.cpp', 'smn_usermsgs.cpp', 'MenuStyle_Base.cpp', - 'smn_database.cpp', 'smn_keyvalues.cpp', 'smn_vector.cpp', 'EventManager.cpp', diff --git a/core/frame_hooks.cpp b/core/frame_hooks.cpp index d0524cba..c270b28d 100644 --- a/core/frame_hooks.cpp +++ b/core/frame_hooks.cpp @@ -30,7 +30,6 @@ */ #include "frame_hooks.h" #include "TimerSys.h" -#include "Database.h" #include "HalfLife2.h" #include "MenuStyle_Valve.h" #include "MenuStyle_Radio.h" @@ -96,7 +95,6 @@ void RunFrameHooks(bool simulating) } /* Frame based hooks */ - g_DBMan.RunFrame(); g_HL2.ProcessFakeCliCmdQueue(); g_HL2.ProcessDelayedKicks(); diff --git a/core/logic/AMBuilder b/core/logic/AMBuilder index c15eb47a..ddfc6e8e 100644 --- a/core/logic/AMBuilder +++ b/core/logic/AMBuilder @@ -60,7 +60,9 @@ files = [ 'NativeOwner.cpp', 'NativeInvoker.cpp', 'ExtensionSys.cpp', - 'DebugReporter.cpp' + 'DebugReporter.cpp', + 'Database.cpp', + 'smn_database.cpp', ] if AMBuild.target['platform'] == 'windows': files.append('thread/WinThreads.cpp') diff --git a/core/Database.cpp b/core/logic/Database.cpp similarity index 87% rename from core/Database.cpp rename to core/logic/Database.cpp index 37bbf6e5..a4f4cb07 100644 --- a/core/Database.cpp +++ b/core/logic/Database.cpp @@ -30,12 +30,12 @@ */ #include "Database.h" -#include "sourcemod.h" -#include "sm_stringutil.h" -#include "Logger.h" +#include "ISourceMod.h" +#include "HandleSys.h" +#include "ExtensionSys.h" +#include "PluginSys.h" #include #include -#include "logic_bridge.h" #define DBPARSE_LEVEL_NONE 0 #define DBPARSE_LEVEL_MAIN 1 @@ -52,22 +52,29 @@ DBManager::DBManager() { } +static void FrameHook(bool simulating) +{ + g_DBMan.RunFrame(); +} + void DBManager::OnSourceModAllInitialized() { HandleAccess sec; - handlesys->InitAccessDefaults(NULL, &sec); + g_HandleSys.InitAccessDefaults(NULL, &sec); sec.access[HandleAccess_Delete] |= HANDLE_RESTRICT_IDENTITY; sec.access[HandleAccess_Clone] |= HANDLE_RESTRICT_IDENTITY; - m_DriverType = handlesys->CreateType("IDriver", this, 0, NULL, &sec, g_pCoreIdent, NULL); - m_DatabaseType = handlesys->CreateType("IDatabase", this, 0, NULL, NULL, g_pCoreIdent, NULL); + m_DriverType = g_HandleSys.CreateType("IDriver", this, 0, NULL, &sec, g_pCoreIdent, NULL); + m_DatabaseType = g_HandleSys.CreateType("IDatabase", this, 0, NULL, NULL, g_pCoreIdent, NULL); - sharesys->AddInterface(NULL, this); + g_ShareSys.AddInterface(NULL, this); - g_SourceMod.BuildPath(Path_SM, m_Filename, sizeof(m_Filename), "configs/databases.cfg"); + g_pSM->BuildPath(Path_SM, m_Filename, sizeof(m_Filename), "configs/databases.cfg"); - scripts->AddPluginsListener(this); + g_PluginSys.AddPluginsListener(this); + + g_pSM->AddGameFrameHook(&FrameHook); } void DBManager::OnSourceModLevelChange(const char *mapName) @@ -82,21 +89,22 @@ void DBManager::OnSourceModLevelChange(const char *mapName) ke::AutoLock lock(&m_ConfigLock); if ((err = textparsers->ParseFile_SMC(m_Filename, this, &states)) != SMCError_Okay) { - g_Logger.LogError("[SM] Detected parse error(s) in file \"%s\"", m_Filename); + smcore.LogError("[SM] Detected parse error(s) in file \"%s\"", m_Filename); if (err != SMCError_Custom) { const char *txt = textparsers->GetSMCErrorString(err); - g_Logger.LogError("[SM] Line %d: %s", states.line, txt); + smcore.LogError("[SM] Line %d: %s", states.line, txt); } } } void DBManager::OnSourceModShutdown() { + g_pSM->RemoveGameFrameHook(&FrameHook); KillWorkerThread(); - scripts->RemovePluginsListener(this); - handlesys->RemoveType(m_DatabaseType, g_pCoreIdent); - handlesys->RemoveType(m_DriverType, g_pCoreIdent); + g_PluginSys.RemovePluginsListener(this); + g_HandleSys.RemoveType(m_DatabaseType, g_pCoreIdent); + g_HandleSys.RemoveType(m_DriverType, g_pCoreIdent); ClearConfigs(); } @@ -118,7 +126,7 @@ void DBManager::OnHandleDestroy(HandleType_t type, void *object) return; } - if (handlesys->TypeCheck(type, m_DatabaseType)) + if (g_HandleSys.TypeCheck(type, m_DatabaseType)) { IDatabase *pdb = (IDatabase *)object; pdb->Close(); @@ -263,7 +271,7 @@ bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool *pdr = NULL; } *pdb = NULL; - UTIL_Format(error, maxlength, "Configuration \"%s\" not found", name); + g_pSM->Format(error, maxlength, "Configuration \"%s\" not found", name); return false; } @@ -300,7 +308,7 @@ bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool } *pdb = NULL; - UTIL_Format(error, maxlength, "Driver \"%s\" not found", dname); + g_pSM->Format(error, maxlength, "Driver \"%s\" not found", dname); return false; } @@ -395,7 +403,7 @@ Handle_t DBManager::CreateHandle(DBHandleType dtype, void *ptr, IdentityToken_t return BAD_HANDLE; } - return handlesys->CreateHandle(type, ptr, pToken, g_pCoreIdent, NULL); + return g_HandleSys.CreateHandle(type, ptr, pToken, g_pCoreIdent, NULL); } HandleError DBManager::ReadHandle(Handle_t hndl, DBHandleType dtype, void **ptr) @@ -413,13 +421,13 @@ HandleError DBManager::ReadHandle(Handle_t hndl, DBHandleType dtype, void **ptr) HandleSecurity sec(NULL, g_pCoreIdent); - return handlesys->ReadHandle(hndl, type, &sec, ptr); + return g_HandleSys.ReadHandle(hndl, type, &sec, ptr); } HandleError DBManager::ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token) { HandleSecurity sec(token, g_pCoreIdent); - return handlesys->FreeHandle(hndl, &sec); + return g_HandleSys.FreeHandle(hndl, &sec); } unsigned int DBManager::GetDriverCount() @@ -476,9 +484,9 @@ IDBDriver *DBManager::FindOrLoadDriver(const char *name) } char filename[PLATFORM_MAX_PATH]; - UTIL_Format(filename, sizeof(filename), "dbi.%s.ext", name); + g_pSM->Format(filename, sizeof(filename), "dbi.%s.ext", name); - IExtension *pExt = extsys->LoadAutoExtension(filename); + IExtension *pExt = g_Extensions.LoadAutoExtension(filename); if (!pExt || !pExt->IsLoaded() || m_drivers.size() <= last_size) { return NULL; @@ -527,7 +535,7 @@ bool DBManager::AddToThreadQueue(IDBThreadOperation *op, PrioQueueLevel prio) { if (!s_OneTimeThreaderErrorMsg) { - g_Logger.LogError("[SM] Unable to create db threader (error unknown)"); + smcore.LogError("[SM] Unable to create db threader (error unknown)"); s_OneTimeThreaderErrorMsg = true; } m_Worker = NULL; @@ -720,6 +728,6 @@ const char *DBManager::GetDefaultDriverName() void DBManager::AddDependency(IExtension *myself, IDBDriver *driver) { - extsys->AddRawDependency(myself, driver->GetIdentity(), driver); + g_Extensions.AddRawDependency(myself, driver->GetIdentity(), driver); } diff --git a/core/Database.h b/core/logic/Database.h similarity index 96% rename from core/Database.h rename to core/logic/Database.h index 43fd37d8..ecd9a417 100644 --- a/core/Database.h +++ b/core/logic/Database.h @@ -33,7 +33,7 @@ #define _INCLUDE_DATABASE_MANAGER_H_ #include -#include "sm_globals.h" +#include "common_logic.h" #include #include #include diff --git a/core/logic/intercom.h b/core/logic/intercom.h index c829e108..25f15dd2 100644 --- a/core/logic/intercom.h +++ b/core/logic/intercom.h @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -97,6 +98,7 @@ namespace SourceMod class IVEngineServer; class IFileSystem; class ConVar; +class KeyValues; class SMGlobalClass; namespace SourceMod @@ -264,6 +266,7 @@ struct sm_core_t void (*DoGlobalPluginLoads)(); bool (*AreConfigsExecuted)(); void (*ExecuteConfigs)(IPluginContext *ctx); + DatabaseInfo (*GetDBInfoFromKeyValues)(KeyValues *); const char *gamesuffix; /* Data */ ServerGlobals *serverGlobals; diff --git a/core/sm_simple_prioqueue.h b/core/logic/sm_simple_prioqueue.h similarity index 100% rename from core/sm_simple_prioqueue.h rename to core/logic/sm_simple_prioqueue.h diff --git a/core/smn_database.cpp b/core/logic/smn_database.cpp similarity index 91% rename from core/smn_database.cpp rename to core/logic/smn_database.cpp index 5f6c3a97..68663d1f 100644 --- a/core/smn_database.cpp +++ b/core/logic/smn_database.cpp @@ -29,12 +29,11 @@ * Version: $Id$ */ -#include "sm_globals.h" +#include "common_logic.h" #include "Database.h" -#include "sm_stringutil.h" -#include "logic_bridge.h" -#include "KeyValues.h" -#include "sourcemod.h" +#include "ExtensionSys.h" +#include "stringutil.h" +#include "ISourceMod.h" HandleType_t hStmtType; @@ -202,7 +201,7 @@ public: m_pQuery = m_pDatabase->DoQuery(m_Query.c_str()); if (!m_pQuery) { - UTIL_Format(error, sizeof(error), "%s", m_pDatabase->GetError()); + g_pSM->Format(error, sizeof(error), "%s", m_pDatabase->GetError()); } m_pDatabase->UnlockFromFullAtomicOperation(); } @@ -235,7 +234,7 @@ public: { m_pQuery = NULL; } else { - UTIL_Format(error, sizeof(error), "Could not alloc handle"); + g_pSM->Format(error, sizeof(error), "Could not alloc handle"); delete c; } } @@ -293,7 +292,7 @@ public: const DatabaseInfo *pInfo = g_DBMan.FindDatabaseConf(dbname); if (!pInfo) { - UTIL_Format(error, sizeof(error), "Could not find database config \"%s\"", dbname); + g_pSM->Format(error, sizeof(error), "Could not find database config \"%s\"", dbname); } else { m_pDatabase = m_pDriver->Connect(pInfo, false, error, sizeof(error)); } @@ -321,7 +320,7 @@ public: == BAD_HANDLE) { m_pDatabase->Close(); - UTIL_Format(error, sizeof(error), "Unable to allocate Handle"); + g_pSM->Format(error, sizeof(error), "Unable to allocate Handle"); } } @@ -369,10 +368,10 @@ static cell_t SQL_Connect(IPluginContext *pContext, const cell_t *params) } /* HACK! Add us to the dependency list */ - IExtension *pExt = extsys->GetExtensionFromIdent(driver->GetIdentity()); + IExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); if (pExt) { - extsys->BindChildPlugin(pExt, scripts->FindPluginByContext(pContext->GetContext())); + g_Extensions.BindChildPlugin(pExt, scripts->FindPluginByContext(pContext->GetContext())); } return hndl; @@ -402,18 +401,18 @@ static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params) } if (!driver) { - UTIL_Format(error, + g_pSM->Format(error, sizeof(error), "Could not find driver \"%s\"", pInfo->driver[0] == '\0' ? g_DBMan.GetDefaultDriverName() : pInfo->driver); } else if (!driver->IsThreadSafe()) { - UTIL_Format(error, + g_pSM->Format(error, sizeof(error), "Driver \"%s\" is not thread safe!", driver->GetIdentifier()); } } else { - UTIL_Format(error, sizeof(error), "Could not find database conf \"%s\"", conf); + g_pSM->Format(error, sizeof(error), "Could not find database conf \"%s\"", conf); } if (!pInfo || !driver) @@ -427,10 +426,10 @@ static cell_t SQL_TConnect(IPluginContext *pContext, const cell_t *params) } /* HACK! Add us to the dependency list */ - IExtension *pExt = extsys->GetExtensionFromIdent(driver->GetIdentity()); + IExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); if (pExt) { - extsys->BindChildPlugin(pExt, scripts->FindPluginByContext(pContext->GetContext())); + g_Extensions.BindChildPlugin(pExt, scripts->FindPluginByContext(pContext->GetContext())); } /* Finally, add to the thread if we can */ @@ -498,10 +497,10 @@ static cell_t SQL_ConnectEx(IPluginContext *pContext, const cell_t *params) } /* HACK! Add us to the dependency list */ - IExtension *pExt = extsys->GetExtensionFromIdent(driver->GetIdentity()); + IExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); if (pExt) { - extsys->BindChildPlugin(pExt, scripts->FindPluginByContext(pContext->GetContext())); + g_Extensions.BindChildPlugin(pExt, scripts->FindPluginByContext(pContext->GetContext())); } return hndl; @@ -1342,7 +1341,7 @@ static cell_t SQL_ConnectCustom(IPluginContext *pContext, const cell_t *params) KeyValues *kv; HandleError err; - kv = g_SourceMod.ReadKeyValuesHandle(params[1], &err, false); + kv = g_pSM->ReadKeyValuesHandle(params[1], &err, false); if (kv == NULL) { return pContext->ThrowNativeError("Invalid KeyValues handle %x (error: %d)", @@ -1350,14 +1349,7 @@ static cell_t SQL_ConnectCustom(IPluginContext *pContext, const cell_t *params) err); } - DatabaseInfo info; - info.database = kv->GetString("database", ""); - info.driver = kv->GetString("driver", "default"); - info.host = kv->GetString("host", ""); - info.maxTimeout = kv->GetInt("timeout", 0); - info.pass = kv->GetString("pass", ""); - info.port = kv->GetInt("port", 0); - info.user = kv->GetString("user", ""); + DatabaseInfo info = smcore.GetDBInfoFromKeyValues(kv); IDBDriver *driver; if (info.driver[0] == '\0' || strcmp(info.driver, "default") == 0) @@ -1373,7 +1365,7 @@ static cell_t SQL_ConnectCustom(IPluginContext *pContext, const cell_t *params) { char buffer[255]; - UTIL_Format(buffer, sizeof(buffer), "Could not find driver \"%s\"", info.driver); + g_pSM->Format(buffer, sizeof(buffer), "Could not find driver \"%s\"", info.driver); pContext->StringToLocalUTF8(params[2], params[3], buffer, NULL); return BAD_HANDLE; @@ -1398,10 +1390,10 @@ static cell_t SQL_ConnectCustom(IPluginContext *pContext, const cell_t *params) } /* HACK! Add us to the dependency list */ - IExtension *pExt = extsys->GetExtensionFromIdent(driver->GetIdentity()); + IExtension *pExt = g_Extensions.GetExtensionFromIdent(driver->GetIdentity()); if (pExt) { - extsys->BindChildPlugin(pExt, scripts->FindPluginByContext(pContext->GetContext())); + g_Extensions.BindChildPlugin(pExt, scripts->FindPluginByContext(pContext->GetContext())); } return hndl; diff --git a/core/logic_bridge.cpp b/core/logic_bridge.cpp index 3fd372f0..802568bb 100644 --- a/core/logic_bridge.cpp +++ b/core/logic_bridge.cpp @@ -46,6 +46,7 @@ #include "AdminCache.h" #include "HalfLife2.h" #include "CoreConfig.h" +#include "IDBDriver.h" #if SOURCE_ENGINE == SE_DOTA #include "convar_sm_dota.h" #elif SOURCE_ENGINE >= SE_ALIENSWARM @@ -261,6 +262,19 @@ static bool is_map_running() return g_SourceMod.IsMapRunning(); } +static DatabaseInfo keyvalues_to_dbinfo(KeyValues *kv) +{ + DatabaseInfo info; + info.database = kv->GetString("database", ""); + info.driver = kv->GetString("driver", "default"); + info.host = kv->GetString("host", ""); + info.maxTimeout = kv->GetInt("timeout", 0); + info.pass = kv->GetString("pass", ""); + info.port = kv->GetInt("port", 0); + info.user = kv->GetString("user", ""); + return info; +} + int read_cmd_argc(const CCommand &args) { return args.ArgC(); @@ -394,6 +408,7 @@ static sm_core_t core_bridge = do_global_plugin_loads, SM_AreConfigsExecuted, SM_ExecuteForPlugin, + keyvalues_to_dbinfo, GAMEFIX, &serverGlobals }; diff --git a/core/sm_queue.h b/public/sm_queue.h similarity index 100% rename from core/sm_queue.h rename to public/sm_queue.h