core/sourcemod.cpp
author David Anderson <dvander@alliedmods.net>
Tue Feb 06 05:13:24 2007 +0000 (2007-02-06)
changeset 438 e6e11c82d87e
parent 422 5ef0f5610c2a
child 448 25984ddd0ce0
permissions -rw-r--r--
changed how the admin cache is rebuilt
renamed the plugin and fixed compilation errors
     1 /**
     2  * ===============================================================
     3  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     4  * ===============================================================
     5  *
     6  * This file is not open source and may not be copied without explicit
     7  * written permission of AlliedModders LLC.  This file may not be redistributed 
     8  * in whole or significant part.
     9  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    10  *
    11  * Version: $Id$
    12  */
    13 
    14 #include <stdio.h>
    15 #include "sourcemod.h"
    16 #include "sourcemm_api.h"
    17 #include "systems/LibrarySys.h"
    18 #include "vm/sp_vm_engine.h"
    19 #include <sh_string.h>
    20 #include "PluginSys.h"
    21 #include "ShareSys.h"
    22 #include "CLogger.h"
    23 #include "ExtensionSys.h"
    24 #include "AdminCache.h"
    25 #include "sm_stringutil.h"
    26 
    27 SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, false, bool, const char *, const char *, const char *, const char *, bool, bool);
    28 SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
    29 
    30 SourcePawnEngine g_SourcePawn;
    31 SourceModBase g_SourceMod;
    32 
    33 ILibrary *g_pJIT = NULL;
    34 SourceHook::String g_BaseDir;
    35 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
    36 IVirtualMachine *g_pVM;
    37 IdentityToken_t *g_pCoreIdent = NULL;
    38 
    39 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
    40 typedef unsigned int (*GETEXPORTCOUNT)();
    41 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
    42 typedef void (*NOTIFYSHUTDOWN)();
    43 
    44 void ShutdownJIT()
    45 {
    46 	NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
    47 	if (notify)
    48 	{
    49 		notify();
    50 	}
    51 
    52 	g_pJIT->CloseLibrary();
    53 }
    54 
    55 SourceModBase::SourceModBase()
    56 {
    57 	m_IsMapLoading = false;
    58 	m_ExecPluginReload = false;
    59 }
    60 
    61 bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
    62 {
    63 	g_BaseDir.assign(g_SMAPI->GetBaseDir());
    64 	g_LibSys.PathFormat(m_SMBaseDir, sizeof(m_SMBaseDir), "%s/addons/sourcemod", g_BaseDir.c_str());
    65 	g_LibSys.PathFormat(m_SMRelDir, sizeof(m_SMRelDir), "addons/sourcemod");
    66 
    67 	/* Attempt to load the JIT! */
    68 	char file[PLATFORM_MAX_PATH];
    69 	char myerror[255];
    70 	g_SMAPI->PathFormat(file, sizeof(file), "%s/bin/sourcepawn.jit.x86.%s",
    71 		GetSourceModPath(),
    72 		PLATFORM_LIB_EXT
    73 		);
    74 
    75 	g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
    76 	if (!g_pJIT)
    77 	{
    78 		if (error && err_max)
    79 		{
    80 			snprintf(error, err_max, "%s (failed to load bin/sourcepawn.jit.x86.%s)", 
    81 				myerror,
    82 				PLATFORM_LIB_EXT);
    83 		}
    84 		return false;
    85 	}
    86 
    87 	int err;
    88 	GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
    89 	if (!jit_init)
    90 	{
    91 		ShutdownJIT();
    92 		if (error && err_max)
    93 		{
    94 			snprintf(error, err_max, "Failed to find GiveEnginePointer in JIT!");
    95 		}
    96 		return false;
    97 	}
    98 
    99 	if ((err=jit_init(g_pSourcePawn)) != 0)
   100 	{
   101 		ShutdownJIT();
   102 		if (error && err_max)
   103 		{
   104 			snprintf(error, err_max, "GiveEnginePointer returned %d in the JIT", err);
   105 		}
   106 		return false;
   107 	}
   108 
   109 	GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
   110 	GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
   111 	if (!jit_get)
   112 	{
   113 		ShutdownJIT();
   114 		if (error && err_max)
   115 		{
   116 			snprintf(error, err_max, "JIT is missing a necessary export!");
   117 		}
   118 		return false;
   119 	}
   120 
   121 	unsigned int num = jit_getnum();
   122 	if (!num || ((g_pVM=jit_get(0)) == NULL))
   123 	{
   124 		ShutdownJIT();
   125 		if (error && err_max)
   126 		{
   127 			snprintf(error, err_max, "JIT did not export any virtual machines!");
   128 		}
   129 		return false;
   130 	}
   131 
   132 	unsigned int api = g_pVM->GetAPIVersion();
   133 	if (api != SOURCEPAWN_VM_API_VERSION)
   134 	{
   135 		ShutdownJIT();
   136 		if (error && err_max)
   137 		{
   138 			snprintf(error, err_max, "JIT is not a compatible version");
   139 		}
   140 		return false;
   141 	}
   142 
   143 	StartSourceMod(late);
   144 
   145 	return true;
   146 }
   147 
   148 void SourceModBase::StartSourceMod(bool late)
   149 {
   150 	/* First initialize the global hooks we need */
   151 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   152 	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   153 
   154 	/* Notify! */
   155 	SMGlobalClass *pBase = SMGlobalClass::head;
   156 	while (pBase)
   157 	{
   158 		pBase->OnSourceModStartup(false);
   159 		pBase = pBase->m_pGlobalClassNext;
   160 	}
   161 
   162 	/* Make the global core identity */
   163 	g_pCoreIdent = g_ShareSys.CreateCoreIdentity();
   164 
   165 	/* Notify! */
   166 	pBase = SMGlobalClass::head;
   167 	while (pBase)
   168 	{
   169 		pBase->OnSourceModAllInitialized();
   170 		pBase = pBase->m_pGlobalClassNext;
   171 	}
   172 
   173 	/* Add us now... */
   174 	g_ShareSys.AddInterface(NULL, this);
   175 }
   176 
   177 bool SourceModBase::LevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
   178 {
   179 	m_IsMapLoading = true;
   180 	m_ExecPluginReload = true;
   181 
   182 	g_Logger.MapChange(pMapName);
   183 
   184 	DoGlobalPluginLoads();
   185 
   186 	m_IsMapLoading = false;
   187 
   188 	g_Admins.DumpAdminCache(AdminCache_Overrides, true);
   189 	g_Admins.DumpAdminCache(AdminCache_Groups, true);
   190 
   191 	RETURN_META_VALUE(MRES_IGNORED, true);
   192 }
   193 
   194 void SourceModBase::LevelShutdown()
   195 {
   196 	if (m_ExecPluginReload)
   197 	{
   198 		g_PluginSys.ReloadOrUnloadPlugins();
   199 		m_ExecPluginReload = false;
   200 	}
   201 }
   202 
   203 bool SourceModBase::IsMapLoading()
   204 {
   205 	return m_IsMapLoading;
   206 }
   207 
   208 void SourceModBase::DoGlobalPluginLoads()
   209 {
   210 	char config_path[PLATFORM_MAX_PATH];
   211 	char plugins_path[PLATFORM_MAX_PATH];
   212 
   213 	BuildPath(Path_SM, config_path, 
   214 		sizeof(config_path),
   215 		"configs/plugin_settings.cfg");
   216 	BuildPath(Path_SM, plugins_path,
   217 		sizeof(plugins_path),
   218 		"plugins");
   219 
   220 	/* Load any auto extensions */
   221 	g_Extensions.TryAutoload();
   222 
   223 	/* Run the first pass */
   224 	g_PluginSys.LoadAll_FirstPass(config_path, plugins_path);
   225 
   226 	/* Mark any extensions as loaded */
   227 	g_Extensions.MarkAllLoaded();
   228 
   229 	/* No modules yet, it's safe to call this from here */
   230 	g_PluginSys.LoadAll_SecondPass();
   231 
   232 	/* Re-mark any extensions as loaded */
   233 	g_Extensions.MarkAllLoaded();
   234 }
   235 
   236 size_t SourceModBase::BuildPath(PathType type, char *buffer, size_t maxlength, char *format, ...)
   237 {
   238 	char _buffer[PLATFORM_MAX_PATH+1];
   239 	va_list ap;
   240 
   241 	va_start(ap, format);
   242 	vsnprintf(_buffer, PLATFORM_MAX_PATH, format, ap);
   243 	va_end(ap);
   244 
   245 	const char *base = NULL;
   246 	if (type == Path_Game)
   247 	{
   248 		base = GetModPath();
   249 	} else if (type == Path_SM) {
   250 		base = GetSourceModPath();
   251 	} else if (type == Path_SM_Rel) {
   252 		base = m_SMRelDir;
   253 	}
   254 
   255 	if (base)
   256 	{
   257 		return g_LibSys.PathFormat(buffer, maxlength, "%s/%s", base, _buffer);
   258 	} else {
   259 		return g_LibSys.PathFormat(buffer, maxlength, "%s", _buffer);
   260 	}
   261 }
   262 
   263 void SourceModBase::CloseSourceMod()
   264 {
   265 	/* Notify! */
   266 	SMGlobalClass *pBase = SMGlobalClass::head;
   267 	while (pBase)
   268 	{
   269 		pBase->OnSourceModShutdown();
   270 		pBase = pBase->m_pGlobalClassNext;
   271 	}
   272 
   273 	/* Notify! */
   274 	pBase = SMGlobalClass::head;
   275 	while (pBase)
   276 	{
   277 		pBase->OnSourceModAllShutdown();
   278 		pBase = pBase->m_pGlobalClassNext;
   279 	}
   280 
   281 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelInit, gamedll, this, &SourceModBase::LevelInit, false);
   282 	SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, LevelShutdown, gamedll, this, &SourceModBase::LevelShutdown, false);
   283 
   284 	/* Rest In Peace */
   285 	ShutdownJIT();
   286 }
   287 
   288 void SourceModBase::LogMessage(IExtension *pExt, const char *format, ...)
   289 {
   290 	IExtensionInterface *pAPI = pExt->GetAPI();
   291 	const char *tag = pAPI->GetExtensionTag();
   292 	char buffer[2048];
   293 	va_list ap;
   294 
   295 	va_start(ap, format);
   296 	vsnprintf(buffer, sizeof(buffer), format, ap);
   297 	va_end(ap);
   298 
   299 	if (tag)
   300 	{
   301 		g_Logger.LogMessage("[%s] %s", tag, buffer);
   302 	} else {
   303 		g_Logger.LogMessage("%s", buffer);
   304 	}
   305 }
   306 
   307 void SourceModBase::LogError(IExtension *pExt, const char *format, ...)
   308 {
   309 	IExtensionInterface *pAPI = pExt->GetAPI();
   310 	const char *tag = pAPI->GetExtensionTag();
   311 	char buffer[2048];
   312 	va_list ap;
   313 
   314 	va_start(ap, format);
   315 	vsnprintf(buffer, sizeof(buffer), format, ap);
   316 	va_end(ap);
   317 
   318 	if (tag)
   319 	{
   320 		g_Logger.LogError("[%s] %s", tag, buffer);
   321 	} else {
   322 		g_Logger.LogError("%s", buffer);
   323 	}
   324 }
   325 
   326 size_t SourceModBase::FormatString(char *buffer, size_t maxlength, IPluginContext *pContext, const cell_t *params, unsigned int param)
   327 {
   328 	char *fmt;
   329 
   330 	pContext->LocalToString(params[param], &fmt);
   331 
   332 	int lparam = ++param;
   333 
   334 	return atcprintf(buffer, maxlength, fmt, pContext, params, &lparam);
   335 }
   336 
   337 const char *SourceModBase::GetSourceModPath()
   338 {
   339 	return m_SMBaseDir;
   340 }
   341 
   342 const char *SourceModBase::GetModPath()
   343 {
   344 	return g_BaseDir.c_str();
   345 }
   346 
   347 void SourceModBase::SetGlobalTarget(unsigned int index)
   348 {
   349 	m_target = index;
   350 }
   351 
   352 unsigned int SourceModBase::GetGlobalTarget() const
   353 {
   354 	return m_target;
   355 }
   356 
   357 SMGlobalClass *SMGlobalClass::head = NULL;
   358 
   359 SMGlobalClass::SMGlobalClass()
   360 {
   361 	m_pGlobalClassNext = SMGlobalClass::head;
   362 	SMGlobalClass::head = this;
   363 }