Member
POC Class to Manage Lua Calls and Return Values
I've been a little bit bored today so I decided to finish up an old project I've found on my harddisk.
Here's the Code, it isn't a complete solution for executing lua functions but you can use it as a starting point if you've no Idea how to retrive lua return values:
CLua_Manager.h:
PHP Code:
#ifndef _H_CLUA_MANAGER_
#define _H_CLUA_MANAGER_
#include <vector>
#include <boost/format.hpp>
#include "Lua/lua.h"
#include "WoWDefines.h"
typedef void (_cdecl *LUA_DOSTRING)(std::string, std::string, void*);
typedef void (_cdecl *LUA_REGISTER)(std::string, void*);
LUA_DOSTRING lua_DoString = reinterpret_cast<LUA_DOSTRING>(gpLuaDoString);
LUA_REGISTER lua_Register = reinterpret_cast<LUA_REGISTER>(gpLuaRegister);
int MyCallback (lua_State *L);
class CLua_Manager{
private:
CLua_Manager();
~CLua_Manager();
static CLua_Manager* singleton;
public:
static CLua_Manager* Get(){if(!singleton){singleton = new CLua_Manager();lua_Register("MyCallback",(void*)MyCallback);return singleton;}return singleton;}
static void Destroy(){delete singleton; singleton = 0;}
std::vector<std::string> vResults;
void DoString(std::string& s);
std::string GetResult(int n);
};
#endif
CLua_Manager.cc
PHP Code:
#include "CLua_Manager.h"
int MyCallback (lua_State *L)
{
//clear the vector if its not empty
if ( !CLua_Manager::Get()->vResults.empty() )
CLua_Manager::Get()->vResults.clear();
//get count of returns on stack
int n = lua_gettop(L);
//loop to retreive these
for (int i = 1;i<=n; i++)
{
//using lua_tostring to get the result
const char * pszResult = lua_tostring(L, i);
//add result to vector
CLua_Manager::Get()->vResults.push_back( pszResult );
}
return 0;
}
std::string CLua_Manager::GetResult(int n){
try{
return vResults[n];
}
catch(std::string e){
return e;
}
}
void CLua_Manager::DoString(std::string& s){
std::string Command = (boost::format("MyCallback(%1%)") % s).str();
lua_DoString(Command.c_str(), Command.c_str(), 0);
}
as you can see I don't provide the offsets for the two wow functions I'm using, but I guess you can figure them out yourself.
I also leave it up to you to bypass the IsFunctionInRange check.
The Code is quick and dirty but I guess it's enough to make the concept understandable.
Last edited by Xarg0; 01-21-2010 at 01:31 PM.
I hacked 127.0.0.1
These ads disappear when you log in.