3 #include "sourcemm_api.h"
4 #include "systems/LibrarySys.h"
5 #include "vm/sp_vm_engine.h"
8 #include "ForwardSys.h"
10 SourcePawnEngine g_SourcePawn;
11 SourceModBase g_SourceMod;
13 ILibrary *g_pJIT = NULL;
14 SourceHook::String g_BaseDir;
15 ISourcePawnEngine *g_pSourcePawn = &g_SourcePawn;
16 IVirtualMachine *g_pVM;
18 typedef int (*GIVEENGINEPOINTER)(ISourcePawnEngine *);
19 typedef unsigned int (*GETEXPORTCOUNT)();
20 typedef IVirtualMachine *(*GETEXPORT)(unsigned int);
21 typedef void (*NOTIFYSHUTDOWN)();
25 NOTIFYSHUTDOWN notify = (NOTIFYSHUTDOWN)g_pJIT->GetSymbolAddress("NotifyShutdown");
31 g_pJIT->CloseLibrary();
34 bool SourceModBase::InitializeSourceMod(char *error, size_t err_max, bool late)
36 //:TODO: we need a localinfo system!
37 g_BaseDir.assign(g_SMAPI->GetBaseDir());
39 /* Attempt to load the JIT! */
40 char file[PLATFORM_MAX_PATH];
42 g_SMAPI->PathFormat(file, sizeof(file), "%s/addons/sourcemod/bin/sourcepawn.jit.x86.%s",
47 g_pJIT = g_LibSys.OpenLibrary(file, myerror, sizeof(myerror));
52 snprintf(error, err_max, "%s (failed to load bin/sourcepawn.jit.x86.%s)",
60 GIVEENGINEPOINTER jit_init = (GIVEENGINEPOINTER)g_pJIT->GetSymbolAddress("GiveEnginePointer");
66 snprintf(error, err_max, "Failed to find GiveEnginePointer in JIT!");
71 if ((err=jit_init(g_pSourcePawn)) != 0)
76 snprintf(error, err_max, "GiveEnginePointer returned %d in the JIT", err);
81 GETEXPORTCOUNT jit_getnum = (GETEXPORTCOUNT)g_pJIT->GetSymbolAddress("GetExportCount");
82 GETEXPORT jit_get = (GETEXPORT)g_pJIT->GetSymbolAddress("GetExport");
88 snprintf(error, err_max, "JIT is missing a necessary export!");
93 unsigned int num = jit_getnum();
94 if (!num || ((g_pVM=jit_get(0)) == NULL))
99 snprintf(error, err_max, "JIT did not export any virtual machines!");
104 unsigned int api = g_pVM->GetAPIVersion();
105 if (api != SOURCEPAWN_VM_API_VERSION)
108 if (error && err_max)
110 snprintf(error, err_max, "JIT is not a compatible version");
115 g_SMAPI->PathFormat(file, sizeof(file), "%s/addons/sourcemod/plugins/test.smx", g_BaseDir.c_str());
116 IPlugin *pPlugin = g_PluginMngr.LoadPlugin(file, false, PluginType_Global, error, err_max);
117 IPluginFunction *func = pPlugin->GetFunctionByName("Test");
118 IPluginFunction *func2 = pPlugin->GetFunctionByName("Test2");
121 ParamType types[] = {Param_Cell, Param_CellByRef};
122 va_list ap = va_start(ap, late);
123 CForward *fwd = CForward::CreateForward(NULL, ET_Custom, 2, types, ap);
124 fwd->AddFunction(func);
125 fwd->AddFunction(func2);
127 fwd->PushCellByRef(&val, 0);
128 fwd->Execute(&result, NULL);
129 g_PluginMngr.UnloadPlugin(pPlugin);