initial import of dynamic native code for both the JIT and plugins

note: dependency resolution is not done yet!

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40607
This commit is contained in:
David Anderson
2007-03-12 07:08:05 +00:00
parent 7ab8e2027b
commit 7c06d89b00
14 changed files with 963 additions and 5 deletions
+5 -1
View File
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="8,00"
Version="8.00"
Name="sourcemod_mm"
ProjectGUID="{E39527CD-7CAB-4420-97CC-DA1B93B260BC}"
RootNamespace="sourcemod_mm"
@@ -713,6 +713,10 @@
RelativePath="..\smn_events.cpp"
>
</File>
<File
RelativePath="..\smn_fakenatives.cpp"
>
</File>
<File
RelativePath="..\smn_filesystem.cpp"
>
+430
View File
@@ -0,0 +1,430 @@
/**
* ===============================================================
* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
* ===============================================================
*
* This file is not open source and may not be copied without explicit
* written permission of AlliedModders LLC. This file may not be redistributed
* in whole or significant part.
* For information, see LICENSE.txt or http://www.sourcemod.net/license.php
*
* Version: $Id$
*/
#include <sh_list.h>
#include <sh_string.h>
#include "sm_trie.h"
#include "sm_globals.h"
#include "PluginSys.h"
#include "sourcemod.h"
#include "sm_stringutil.h"
using namespace SourceHook;
static cell_t s_curparams[SP_MAX_EXEC_PARAMS+1];
static FakeNative *s_curnative = NULL;
static IPluginContext *s_curcaller = NULL;
cell_t FakeNativeRouter(IPluginContext *pContext, const cell_t *params, void *pData)
{
FakeNative *native = (FakeNative *)pData;
/* Check if too many parameters were passed */
if (params[0] > SP_MAX_EXEC_PARAMS)
{
return pContext->ThrowNativeError("Called native with too many parameters (%d>%d)", params[9], SP_MAX_EXEC_PARAMS);
}
/* Check if the native is paused */
sp_context_t *pNativeCtx = native->ctx->GetContext();
if ((pNativeCtx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED)
{
return pContext->ThrowNativeError("Plugin owning this native is currently paused.");
}
CPlugin *pCaller = g_PluginSys.GetPluginByCtx(pContext->GetContext());
/* Save any old data on the stack */
FakeNative *pSaveNative = s_curnative;
IPluginContext *pSaveCaller = s_curcaller;
cell_t save_params[SP_MAX_EXEC_PARAMS+1];
if (pSaveNative != NULL)
{
/* Copy all old parameters */
for (cell_t i=0; i<=s_curparams[0]; i++)
{
save_params[i] = s_curparams[i];
}
}
/* Save the current parameters */
s_curnative = native;
s_curcaller = pContext;
for (cell_t i=0; i<=params[0]; i++)
{
s_curparams[i] = params[i];
}
/* Push info and execute. */
cell_t result = 0;
native->call->PushCell(pCaller->GetMyHandle());
native->call->PushCell(params[0]);
if (native->call->Execute(&result) != SP_ERROR_NONE)
{
if (pContext->GetContext()->n_err == SP_ERROR_NONE)
{
pContext->ThrowNativeError("Error encountered while processing a dynamic native");
}
}
/* Restore everything from the stack if necessary */
if (pSaveNative != NULL)
{
s_curnative = pSaveNative;
s_curcaller = pSaveCaller;
for (cell_t i=0; i<=save_params[0]; i++)
{
s_curparams[i] = save_params[i];
}
}
return result;
}
static cell_t CreateNative(IPluginContext *pContext, const cell_t *params)
{
char *name;
pContext->LocalToString(params[1], &name);
IPluginFunction *pFunction = pContext->GetFunctionById(params[2]);
if (!pFunction)
{
return pContext->ThrowNativeError("Function %x is not a valid function", params[2]);
}
if (!g_PluginSys.AddFakeNative(pFunction, name, FakeNativeRouter))
{
return pContext->ThrowNativeError("Fatal error creating dynamic native!");
}
return 1;
}
static cell_t ThrowNativeError(IPluginContext *pContext, const cell_t *params)
{
if (!s_curnative || (s_curnative->ctx != pContext))
{
return pContext->ThrowNativeError("Not called from inside a native function");
}
char buffer[512];
g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
if (pContext->GetContext()->n_err != SP_ERROR_NONE)
{
s_curcaller->ThrowNativeError("Error encountered while processing a dynamic native");
} else {
s_curcaller->ThrowNativeErrorEx(params[1], "%s", buffer);
}
return 0;
}
static cell_t GetNativeStringLength(IPluginContext *pContext, const cell_t *params)
{
if (!s_curnative || (s_curnative->ctx != pContext))
{
return pContext->ThrowNativeError("Not called from inside a native function");
}
cell_t param = params[1];
if (param < 1 || param > s_curparams[0])
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
}
int err;
char *str;
if ((err=s_curcaller->LocalToString(s_curparams[param], &str)) != SP_ERROR_NONE)
{
return err;
}
cell_t *addr;
pContext->LocalToPhysAddr(params[2], &addr);
*addr = (cell_t)strlen(str);
return SP_ERROR_NONE;
}
static cell_t GetNativeString(IPluginContext *pContext, const cell_t *params)
{
if (!s_curnative || (s_curnative->ctx != pContext))
{
return pContext->ThrowNativeError("Not called from inside a native function");
}
cell_t param = params[1];
if (param < 1 || param > s_curparams[0])
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
}
int err;
char *str;
if ((err=s_curcaller->LocalToString(s_curparams[param], &str)) != SP_ERROR_NONE)
{
return err;
}
size_t bytes = 0;
pContext->StringToLocalUTF8(params[2], params[3], str, &bytes);
cell_t *addr;
pContext->LocalToPhysAddr(params[4], &addr);
*addr = (cell_t)bytes;
return SP_ERROR_NONE;
}
static cell_t SetNativeString(IPluginContext *pContext, const cell_t *params)
{
if (!s_curnative || (s_curnative->ctx != pContext))
{
return pContext->ThrowNativeError("Not called from inside a native function");
}
cell_t param = params[1];
if (param < 1 || param > s_curparams[0])
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
}
char *str;
pContext->LocalToString(params[2], &str);
int err;
size_t bytes = 0;
if (params[4])
{
err = s_curcaller->StringToLocalUTF8(s_curparams[param], params[3], str, &bytes);
} else {
err = s_curcaller->StringToLocal(s_curparams[param], params[3], str);
/* Eww */
bytes = strlen(str);
if (bytes >= (size_t)params[3])
{
bytes = params[3] - 1;
}
}
if (err != SP_ERROR_NONE)
{
return err;
}
cell_t *addr;
pContext->LocalToPhysAddr(params[5], &addr);
*addr = (cell_t)bytes;
return SP_ERROR_NONE;
}
static cell_t GetNativeCell(IPluginContext *pContext, const cell_t *params)
{
if (!s_curnative || (s_curnative->ctx != pContext))
{
return pContext->ThrowNativeError("Not called from inside a native function");
}
cell_t param = params[1];
if (param < 1 || param > s_curparams[0])
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
}
return s_curparams[param];
}
static cell_t GetNativeCellRef(IPluginContext *pContext, const cell_t *params)
{
if (!s_curnative || (s_curnative->ctx != pContext))
{
return pContext->ThrowNativeError("Not called from inside a native function");
}
cell_t param = params[1];
if (param < 1 || param > s_curparams[0])
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
}
cell_t *addr;
if (s_curcaller->LocalToPhysAddr(s_curparams[param], &addr) != SP_ERROR_NONE)
{
return s_curcaller->ThrowNativeErrorEx(SP_ERROR_INVALID_ADDRESS, "Invalid address value");
}
return *addr;
}
static cell_t SetNativeCellRef(IPluginContext *pContext, const cell_t *params)
{
if (!s_curnative || (s_curnative->ctx != pContext))
{
return pContext->ThrowNativeError("Not called from inside a native function");
}
cell_t param = params[1];
if (param < 1 || param > s_curparams[0])
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
}
cell_t *addr;
if (s_curcaller->LocalToPhysAddr(s_curparams[param], &addr) != SP_ERROR_NONE)
{
return s_curcaller->ThrowNativeErrorEx(SP_ERROR_INVALID_ADDRESS, "Invalid address value");
}
*addr = params[2];
return 1;
}
static cell_t GetNativeArray(IPluginContext *pContext, const cell_t *params)
{
if (!s_curnative || (s_curnative->ctx != pContext))
{
return pContext->ThrowNativeError("Not called from inside a native function");
}
cell_t param = params[1];
if (param < 1 || param > s_curparams[0])
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
}
int err;
cell_t *addr;
if ((err=s_curcaller->LocalToPhysAddr(s_curparams[param], &addr)) != SP_ERROR_NONE)
{
return err;
}
cell_t *src;
pContext->LocalToPhysAddr(params[2], &src);
memcpy(src, addr, sizeof(cell_t) * params[3]);
return SP_ERROR_NONE;
}
static cell_t SetNativeArray(IPluginContext *pContext, const cell_t *params)
{
if (!s_curnative || (s_curnative->ctx != pContext))
{
return pContext->ThrowNativeError("Not called from inside a native function");
}
cell_t param = params[1];
if (param < 1 || param > s_curparams[0])
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", param);
}
int err;
cell_t *addr;
if ((err=s_curcaller->LocalToPhysAddr(s_curparams[param], &addr)) != SP_ERROR_NONE)
{
return err;
}
cell_t *src;
pContext->LocalToPhysAddr(params[2], &src);
memcpy(addr, src, sizeof(cell_t) * params[3]);
return SP_ERROR_NONE;
}
static cell_t FormatNativeString(IPluginContext *pContext, const cell_t *params)
{
if (!s_curnative || (s_curnative->ctx != pContext))
{
return pContext->ThrowNativeError("Not called from inside a native function");
}
int out_param = params[1];
int fmt_param = params[2];
int var_param = params[3];
/* Validate input */
if (out_param && (out_param < 1 || out_param > s_curparams[0]))
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", out_param);
}
if (fmt_param && (fmt_param < 1 || fmt_param > s_curparams[0]))
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", fmt_param);
}
if (var_param && (var_param < 1 || var_param > s_curparams[0] + 1))
{
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAM, "Invalid parameter number: %d", fmt_param);
}
/* Get buffer information */
int err;
char *output_buffer;
char *format_buffer;
if (out_param)
{
if ((err=s_curcaller->LocalToString(s_curparams[out_param], &output_buffer)) != SP_ERROR_NONE)
{
return err;
}
} else {
pContext->LocalToString(params[6], &output_buffer);
}
if (fmt_param)
{
if ((err=s_curcaller->LocalToString(s_curparams[fmt_param], &format_buffer)) != SP_ERROR_NONE)
{
return err;
}
} else {
pContext->LocalToString(params[7], &format_buffer);
}
/* Get maximum length */
size_t maxlen = (size_t)params[4];
/* Do the format */
size_t written = atcprintf(output_buffer, maxlen, format_buffer, s_curcaller, s_curparams, &var_param);
cell_t *addr;
pContext->LocalToPhysAddr(params[5], &addr);
*addr = (cell_t)written;
return s_curcaller->GetContext()->n_err;
}
//tee hee
REGISTER_NATIVES(nativeNatives)
{
{"CreateNative", CreateNative},
{"GetNativeArray", GetNativeArray},
{"GetNativeCell", GetNativeCell},
{"GetNativeCellRef", GetNativeCellRef},
{"GetNativeString", GetNativeString},
{"GetNativeStringLength", GetNativeStringLength},
{"FormatNativeString", FormatNativeString},
{"ThrowNativeError", ThrowNativeError},
{"SetNativeArray", SetNativeArray},
{"SetNativeCellRef", SetNativeCellRef},
{"SetNativeString", SetNativeString},
{NULL, NULL},
};
+54 -1
View File
@@ -588,6 +588,7 @@ CPluginManager::CPluginManager()
m_LoadLookup = sm_trie_create();
m_AllPluginsLoaded = false;
m_MyIdent = NULL;
m_pNativeLookup = sm_trie_create();
}
CPluginManager::~CPluginManager()
@@ -598,9 +599,9 @@ CPluginManager::~CPluginManager()
* will crash anyway. YAY
*/
sm_trie_destroy(m_LoadLookup);
sm_trie_destroy(m_pNativeLookup);
}
void CPluginManager::LoadAll_FirstPass(const char *config, const char *basedir)
{
/* First read in the database of plugin settings */
@@ -958,6 +959,7 @@ bool CPluginManager::RunSecondPass(CPlugin *pPlugin, char *error, size_t maxleng
/* Bind all extra natives */
g_Extensions.BindAllNativesToPlugin(pPlugin);
AddFakeNativesToPlugin(pPlugin);
/* Find any unbound natives
* Right now, these are not allowed
@@ -1013,6 +1015,32 @@ void CPluginManager::AddCoreNativesToPlugin(CPlugin *pPlugin)
}
}
void CPluginManager::AddFakeNativesToPlugin(CPlugin *pPlugin)
{
IPluginContext *pContext = pPlugin->GetBaseContext();
sp_nativeinfo_t native;
List<FakeNative *>::iterator iter;
FakeNative *pNative;
sp_context_t *ctx;
for (iter = m_Natives.begin(); iter != m_Natives.end(); iter++)
{
pNative = (*iter);
ctx = pNative->ctx->GetContext();
if ((ctx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED)
{
/* Ignore natives in paused plugins */
continue;
}
native.name = pNative->name.c_str();
native.func = pNative->func;
if (pContext->BindNative(&native) == SP_ERROR_NONE)
{
/* :TODO: add to dependency list */
}
}
}
bool CPluginManager::UnloadPlugin(IPlugin *plugin)
{
CPlugin *pPlugin = (CPlugin *)plugin;
@@ -1675,3 +1703,28 @@ void CPluginManager::_SetPauseState(CPlugin *pl, bool paused)
}
}
bool CPluginManager::AddFakeNative(IPluginFunction *pFunction, const char *name, SPVM_FAKENATIVE_FUNC func)
{
if (sm_trie_retrieve(m_pNativeLookup, name, NULL))
{
return false;
}
FakeNative *pNative = new FakeNative;
pNative->func = g_pVM->CreateFakeNative(func, pNative);
if (!pNative->func)
{
delete pNative;
return false;
}
pNative->call = pFunction;
pNative->name.assign(name);
pNative->ctx = pFunction->GetParentContext();
m_Natives.push_back(pNative);
sm_trie_insert(m_pNativeLookup, name,pNative);
return true;
}
+18
View File
@@ -22,6 +22,7 @@
#include <sh_list.h>
#include <sh_stack.h>
#include <sh_vector.h>
#include <sh_string.h>
#include "sm_globals.h"
#include "vm/sp_vm_basecontext.h"
#include "PluginInfoDatabase.h"
@@ -232,6 +233,14 @@ private:
Trie *m_pProps;
};
struct FakeNative
{
IPluginContext *ctx;
IPluginFunction *call;
String name;
SPVM_NATIVE_FUNC func;
};
class CPluginManager :
public IPluginManager,
public SMGlobalClass,
@@ -380,6 +389,10 @@ protected:
{
return m_MyIdent;
}
public:
bool AddFakeNative(IPluginFunction *pFunction, const char *name, SPVM_FAKENATIVE_FUNC func);
private:
void AddFakeNativesToPlugin(CPlugin *pPlugin);
private:
List<IPluginsListener *> m_listeners;
List<CPlugin *> m_plugins;
@@ -389,6 +402,11 @@ private:
Trie *m_LoadLookup;
bool m_AllPluginsLoaded;
IdentityToken_t *m_MyIdent;
/* Dynamic native stuff */
List<FakeNative *> m_Natives;
Trie *m_pNativeLookup;
};
extern CPluginManager g_PluginSys;