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