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