core/sourcemod.cpp
changeset 160 c056af9b4aaf
child 176 c1699993453c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/sourcemod.cpp	Wed Nov 08 07:32:44 2006 +0000
     1.3 @@ -0,0 +1,103 @@
     1.4 +#include <stdio.h>
     1.5 +#include "sourcemod.h"
     1.6 +#include "sourcemm_api.h"
     1.7 +#include "systems/LibrarySys.h"
     1.8 +#include "vm/sp_vm_engine.h"
     1.9 +#include <sh_string.h>
    1.10 +
    1.11 +SourcePawnEngine g_SourcePawn;
    1.12 +SourceModBase g_SourceMod;
    1.13 +
    1.14 +ILibrary *g_pJIT = NULL;
    1.15 +SourceHook::String g_BaseDir;
    1.16 +ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
    1.17 +IVirtualMachine *g_pVM;
    1.18 +
    1.19 +typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
    1.20 +typedef unsigned int (*GETEXPORTCOUNT)();
    1.21 +typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
    1.22 +typedef void (*NOTIFYSHUTDOWN)();
    1.23 +
    1.24 +void ShutdownJIT()
    1.25 +{
    1.26 +	NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
    1.27 +	if (notify)
    1.28 +	{
    1.29 +		notify();
    1.30 +	}
    1.31 +
    1.32 +	g_pJIT->CloseLibrary();
    1.33 +}
    1.34 +
    1.35 +bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
    1.36 +{
    1.37 +	//:TODO: we need a localinfo system!
    1.38 +	g_BaseDir.assign(g_SMAPI->GetBaseDir());
    1.39 +
    1.40 +	/* Attempt to load the JIT! */
    1.41 +	char file[PLATFORM_MAX_PATH];
    1.42 +	char myerror[255];
    1.43 +	g_SMAPI->PathFormat(file, sizeof(file), "%s/addons/sourcemod/bin/sourcepawn.jit.x86.%s",
    1.44 +		g_BaseDir.c_str(),
    1.45 +		PLATFORM_LIB_EXT
    1.46 +		);
    1.47 +
    1.48 +	g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
    1.49 +	if (!g_pJIT)
    1.50 +	{
    1.51 +		if (error && err_max)
    1.52 +		{
    1.53 +			snprintf(error, err_max, "%s (failed to load bin/sourcepawn.jit.x86.%s)", 
    1.54 +				myerror,
    1.55 +				PLATFORM_LIB_EXT);
    1.56 +		}
    1.57 +		return false;
    1.58 +	}
    1.59 +
    1.60 +	int err;
    1.61 +	GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
    1.62 +	if (!jit_init)
    1.63 +	{
    1.64 +		ShutdownJIT();
    1.65 +		if (error && err_max)
    1.66 +		{
    1.67 +			snprintf(error, err_max, "Failed to find GiveEnginePointer in JIT!");
    1.68 +		}
    1.69 +		return false;
    1.70 +	}
    1.71 +
    1.72 +	if ((err=jit_init(g_pSourcePawn)) != 0)
    1.73 +	{
    1.74 +		ShutdownJIT();
    1.75 +		if (error && err_max)
    1.76 +		{
    1.77 +			snprintf(error, err_max, "GiveEnginePointer returned %d in the JIT", err);
    1.78 +		}
    1.79 +		return false;
    1.80 +	}
    1.81 +
    1.82 +	GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
    1.83 +	GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
    1.84 +	if (!jit_get)
    1.85 +	{
    1.86 +		ShutdownJIT();
    1.87 +		if (error && err_max)
    1.88 +		{
    1.89 +			snprintf(error, err_max, "JIT is missing a necessary export!");
    1.90 +		}
    1.91 +		return false;
    1.92 +	}
    1.93 +
    1.94 +	unsigned int num = jit_getnum();
    1.95 +	if (!num || ((g_pVM=jit_get(0)) == NULL))
    1.96 +	{
    1.97 +		ShutdownJIT();
    1.98 +		if (error && err_max)
    1.99 +		{
   1.100 +			snprintf(error, err_max, "JIT did not export any virtual machines!");
   1.101 +		}
   1.102 +		return false;
   1.103 +	}
   1.104 +
   1.105 +	return true;
   1.106 +}