1
0
Fork 0

Added support for passing user data with expression functions

This commit is contained in:
justin 2016-07-09 15:56:39 -06:00
parent 4e4378811c
commit c3f03b6675
6 changed files with 59 additions and 6 deletions

View File

@ -105,6 +105,10 @@ bool _plugin_registerexprfunction(int pluginHandle, const char* name, int argc,
{
return pluginexprfuncregister(pluginHandle, name, argc, cbFunction);
}
bool _plugin_registerexprfunctionuserdata(int pluginHandle, const char* name, int argc, CBPLUGINEXPRFUNCTIONWITHUSERDATA cbFunction, void* usrdata)
{
return pluginexprfuncregister(pluginHandle, name, argc, cbFunction, usrdata);
}
bool _plugin_unregisterexprfunction(int pluginHandle, const char* name)
{

View File

@ -209,6 +209,7 @@ typedef void (*CBPLUGIN)(CBTYPE cbType, void* callbackInfo);
typedef bool (*CBPLUGINCOMMAND)(int argc, char** argv);
typedef void (*CBPLUGINSCRIPT)();
typedef duint(*CBPLUGINEXPRFUNCTION)(int argc, const duint* argv);
typedef duint(*CBPLUGINEXPRFUNCTIONWITHUSERDATA)(int argc, const duint* argv, void* usrdata);
//exports
#ifdef __cplusplus
@ -233,6 +234,7 @@ PLUG_IMPEXP void _plugin_menuentryseticon(int pluginHandle, int hEntry, const IC
PLUG_IMPEXP void _plugin_startscript(CBPLUGINSCRIPT cbScript);
PLUG_IMPEXP bool _plugin_waituntilpaused();
PLUG_IMPEXP bool _plugin_registerexprfunction(int pluginHandle, const char* name, int argc, CBPLUGINEXPRFUNCTION cbFunction);
PLUG_IMPEXP bool _plugin_registerexprfunctionuserdata(int pluginHandle, const char* name, int argc, CBPLUGINEXPRFUNCTIONWITHUSERDATA cbFunction, void* usrdata);
PLUG_IMPEXP bool _plugin_unregisterexprfunction(int pluginHandle, const char* name);
#ifdef __cplusplus

View File

@ -49,6 +49,34 @@ void ExpressionFunctions::Init()
RegisterEasy("mod.entry", ModEntryFromAddr);
}
duint ExpressionFunctions::Function::Invoke(int argc, const duint* argv) const
{
if(cbFunction)
{
return cbFunction(argc, argv);
}
else if(cbFunctionWithUserData)
{
return cbFunctionWithUserData(argc, argv, userdata);
}
return 0;
}
bool ExpressionFunctions::Register(const String & name, int argc, CBEXPRESSIONFUNCTIONWITHUSERDATA cbFunction, void* user)
{
if(!isValidName(name))
return false;
EXCLUSIVE_ACQUIRE(LockExpressionFunctions);
if(mFunctions.count(name))
return false;
Function f;
f.name = name;
f.argc = argc;
f.cbFunctionWithUserData = cbFunction;
f.userdata = user;
mFunctions[name] = f;
return true;
}
bool ExpressionFunctions::Register(const String & name, int argc, CBEXPRESSIONFUNCTION cbFunction)
{
if(!isValidName(name))
@ -83,7 +111,7 @@ bool ExpressionFunctions::Call(const String & name, const std::vector<duint> & a
const auto & f = found->second;
if(f.argc != int(argv.size()))
return false;
result = f.cbFunction(f.argc, argv.data());
result = f.Invoke(f.argc, argv.data());
return true;
}

View File

@ -5,10 +5,12 @@
class ExpressionFunctions
{
public:
using CBEXPRESSIONFUNCTION = std::function<duint(int argc, const duint* argv)>;
using CBEXPRESSIONFUNCTION = std::function<duint(int argc, const duint* argv)>;
using CBEXPRESSIONFUNCTIONWITHUSERDATA = std::function<duint(int argc, const duint* argv, void* user)>;
static void Init();
static bool Register(const String & name, int argc, CBEXPRESSIONFUNCTION cbFunction);
static bool Register(const String & name, int argc, CBEXPRESSIONFUNCTIONWITHUSERDATA cbFunction, void* user);
static bool Unregister(const String & name);
static bool Call(const String & name, const std::vector<duint> & argv, duint & result);
static bool GetArgc(const String & name, int & argc);
@ -16,9 +18,13 @@ public:
private:
struct Function
{
CBEXPRESSIONFUNCTION cbFunction;
CBEXPRESSIONFUNCTIONWITHUSERDATA cbFunctionWithUserData;
String name;
int argc;
CBEXPRESSIONFUNCTION cbFunction;
void* userdata;
duint Invoke(int argc, const duint* argv) const;
};
static bool isValidName(const String & name);

View File

@ -693,17 +693,29 @@ void pluginmenuentryseticon(int pluginHandle, int hEntry, const ICONDATA* icon)
}
}
bool pluginexprfuncregister(int pluginHandle, const char* name, int argc, CBPLUGINEXPRFUNCTION cbFunction)
static void pluginexprfuncregister(int pluginHandle, const char* name)
{
PLUG_EXPRFUNCTION plugExprfunction;
plugExprfunction.pluginHandle = pluginHandle;
strcpy_s(plugExprfunction.name, name);
if(!ExpressionFunctions::Register(name, argc, cbFunction))
return false;
EXCLUSIVE_ACQUIRE(LockPluginExprfunctionList);
pluginExprfunctionList.push_back(plugExprfunction);
EXCLUSIVE_RELEASE();
dprintf("[PLUGIN] expression function \"%s\" registered!\n", name);
}
bool pluginexprfuncregister(int pluginHandle, const char* name, int argc, CBPLUGINEXPRFUNCTIONWITHUSERDATA cbFunction, void* user)
{
if(!ExpressionFunctions::Register(name, argc, cbFunction, user))
return false;
pluginexprfuncregister(pluginHandle, name);
return true;
}
bool pluginexprfuncregister(int pluginHandle, const char* name, int argc, CBPLUGINEXPRFUNCTION cbFunction)
{
if(!ExpressionFunctions::Register(name, argc, cbFunction))
return false;
pluginexprfuncregister(pluginHandle, name);
return true;
}

View File

@ -67,6 +67,7 @@ bool pluginwineventglobal(MSG* message);
void pluginmenuseticon(int hMenu, const ICONDATA* icon);
void pluginmenuentryseticon(int pluginHandle, int hEntry, const ICONDATA* icon);
bool pluginexprfuncregister(int pluginHandle, const char* name, int argc, CBPLUGINEXPRFUNCTION cbFunction);
bool pluginexprfuncregister(int pluginHandle, const char* name, int argc, CBPLUGINEXPRFUNCTIONWITHUSERDATA cbFunction, void* user);
bool pluginexprfuncunregister(int pluginHandle, const char* name);
#endif // _PLUGIN_LOADER_H