1
0
Fork 0

DBG: expose registration of expression functions to plugins

This commit is contained in:
mrexodia 2016-07-07 13:40:34 +02:00
parent 2a8e7e9026
commit e5c2b6ae7f
No known key found for this signature in database
GPG Key ID: D72F9A4FAA0073B4
5 changed files with 89 additions and 4 deletions

View File

@ -99,4 +99,14 @@ PLUG_IMPEXP bool _plugin_waituntilpaused()
while(DbgIsDebugging() && dbgisrunning()) //wait until the debugger paused
Sleep(1);
return DbgIsDebugging();
}
}
bool _plugin_registerexprfunction(int pluginHandle, const char* name, int argc, CBPLUGINEXPRFUNCTION cbFunction)
{
return pluginexprfuncregister(pluginHandle, name, argc, cbFunction);
}
bool _plugin_unregisterexprfunction(int pluginHandle, const char* name)
{
return pluginexprfuncunregister(pluginHandle, name);
}

View File

@ -206,8 +206,9 @@ typedef enum
//typedefs
typedef void (*CBPLUGIN)(CBTYPE cbType, void* callbackInfo);
typedef bool (*CBPLUGINCOMMAND)(int, char**);
typedef bool (*CBPLUGINCOMMAND)(int argc, char** argv);
typedef void (*CBPLUGINSCRIPT)();
typedef duint(*CBPLUGINEXPRFUNCTION)(int argc, const duint* argv);
//exports
#ifdef __cplusplus
@ -231,6 +232,8 @@ PLUG_IMPEXP void _plugin_menuseticon(int hMenu, const ICONDATA* icon);
PLUG_IMPEXP void _plugin_menuentryseticon(int pluginHandle, int hEntry, const ICONDATA* icon);
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_unregisterexprfunction(int pluginHandle, const char* name);
#ifdef __cplusplus
}

View File

@ -8,6 +8,7 @@
#include "console.h"
#include "debugger.h"
#include "threading.h"
#include "expressionfunctions.h"
/**
\brief List of plugins.
@ -29,11 +30,17 @@ static std::vector<PLUG_CALLBACK> pluginCallbackList;
*/
static std::vector<PLUG_COMMAND> pluginCommandList;
/**
\brief List of plugin menus.
*/
static std::vector<PLUG_MENU> pluginMenuList;
/**
\brief List of plugin exprfunctions.
*/
static std::vector<PLUG_EXPRFUNCTION> pluginExprfunctionList;
/**
\brief Loads plugins from a specified directory.
\param pluginDir The directory to load plugins from.
@ -303,6 +310,29 @@ static void plugincmdunregisterall(int pluginHandle)
}
}
/**
\brief Unregister all plugin expression functions.
\param pluginHandle Handle of the plugin to remove the commands from.
*/
static void pluginexprfuncunregisterall(int pluginHandle)
{
SHARED_ACQUIRE(LockPluginExprfunctionList);
auto commandList = pluginExprfunctionList; //copy for thread-safety reasons
SHARED_RELEASE();
auto i = commandList.begin();
while(i != commandList.end())
{
auto currentExprfunction = *i;
if(currentExprfunction.pluginHandle == pluginHandle)
{
i = commandList.erase(i);
ExpressionFunctions::Unregister(currentExprfunction.name);
}
else
++i;
}
}
/**
\brief Unloads all plugins.
*/
@ -316,6 +346,7 @@ void pluginunload()
if(stop)
stop();
plugincmdunregisterall(currentPlugin.initStruct.pluginHandle);
pluginexprfuncunregisterall(currentPlugin.initStruct.pluginHandle);
}
}
{
@ -660,4 +691,37 @@ void pluginmenuentryseticon(int pluginHandle, int hEntry, const ICONDATA* icon)
break;
}
}
}
}
bool pluginexprfuncregister(int pluginHandle, const char* name, int argc, CBPLUGINEXPRFUNCTION cbFunction)
{
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);
return true;
}
bool pluginexprfuncunregister(int pluginHandle, const char* name)
{
EXCLUSIVE_ACQUIRE(LockPluginExprfunctionList);
for(auto it = pluginExprfunctionList.begin(); it != pluginExprfunctionList.end(); ++it)
{
const auto & currentExprfunction = *it;
if(currentExprfunction.pluginHandle == pluginHandle && !strcmp(currentExprfunction.name, name))
{
pluginExprfunctionList.erase(it);
EXCLUSIVE_RELEASE();
if(!ExpressionFunctions::Unregister(name))
return false;
dprintf("[PLUGIN] command \"%s\" unregistered!\n", name);
return true;
}
}
return false;
}

View File

@ -43,6 +43,12 @@ struct PLUG_COMMAND
char command[deflen];
};
struct PLUG_EXPRFUNCTION
{
int pluginHandle;
char name[deflen];
};
//plugin management functions
void pluginload(const char* pluginDir);
void pluginunload();
@ -60,6 +66,7 @@ bool pluginwinevent(MSG* message, long* result);
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 pluginexprfuncunregister(int pluginHandle, const char* name);
#endif // _PLUGIN_LOADER_H

View File

@ -53,6 +53,7 @@ enum SectionLock
LockPluginCallbackList,
LockPluginCommandList,
LockPluginMenuList,
LockPluginExprfunctionList,
LockSehCache,
LockMnemonicHelp,
LockTraceRecord,