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