Peek and pokes with lua bindings

Signed-off-by: Warmist <Warmist@gmail.com>
develop
Warmist 2011-07-27 23:41:56 +03:00
parent d262986740
commit 260bdfac0d
2 changed files with 157 additions and 3 deletions

@ -82,7 +82,29 @@ DFhackCExport command_result plugin_onupdate ( Core * c )
return CR_OK;
}
void InterpreterLoop(Core* c)
{
Console &con=c->con;
lua::state s=lua::glua::Get();
string curline;
con.print("Type quit to exit interactive mode\n");
con.lineedit(">>",curline);
while (curline!="quit") {
try
{
s.loadstring(curline);
s.pcall();
}
catch(lua::exception &e)
{
con.printerr("Error:%s\n",e.what());
s.settop(0);
}
con.lineedit(">>",curline);
}
s.settop(0);
}
DFhackCExport command_result lua_run (Core * c, vector <string> & parameters)
{
Console &con=c->con;
@ -101,7 +123,7 @@ DFhackCExport command_result lua_run (Core * c, vector <string> & parameters)
}
else
{
//TODO interpreter...
InterpreterLoop(c);
}
s.settop(0);// clean up
mymutex->unlock();

@ -12,7 +12,139 @@ unsigned peekd(size_t offset)
{
return *((unsigned*)(offset));
}
void lua::RegisterEngine(lua::state &st)
void peekarb(size_t offset, void *mem,size_t size)
{
memcpy(mem,(void*)offset,size);
}
void peekstr(size_t offset, char* buf, size_t maxsize)
{
strcpy_s(buf,maxsize,(char*)offset);
}
void pokeb(size_t offset,unsigned char val)
{
*((unsigned char*)(offset))=val;
}
void pokew(size_t offset,unsigned short val)
{
*((unsigned short*)(offset))=val;
}
void poked(size_t offset,unsigned val)
{
*((unsigned*)(offset))=val;
}
void pokearb(size_t offset, void *mem,size_t size)
{
memcpy((void*)offset,mem,size);
}
void pokestr(size_t offset, char* buf, size_t maxsize)
{
strcpy_s((char*)offset,maxsize,buf);
}
template <typename T>
T peek(size_t offset) //prob lower performance
{
T tmp;
peekarb(offset,&tmp,sizeof(T));
return tmp;
}
//// lua stuff here
static int lua_peekb(lua_State *L)
{
lua::state st(L);
st.push(peekb(st.as<size_t>(1)));
return 1;
}
static int lua_peekd(lua_State *L)
{
lua::state st(L);
st.push(peekd(st.as<size_t>(1)));
return 1;
}
static int lua_peekw(lua_State *L)
{
lua::state st(L);
st.push(peekw(st.as<size_t>(1)));
return 1;
}
static int lua_peekarb(lua_State *L)
{
lua::state st(L);
size_t size=st.as<size_t>(2);
void *p=st.newuserdata(size);
peekarb(st.as<size_t>(1),p,size);
return 1;
}
static int lua_peekstr(lua_State *L)
{
lua::state st(L);
char *buf;
buf=new char[256];
peekstr(st.as<size_t>(1),buf,256);
std::string tstr(buf);
st.push(tstr);
delete [] buf;
return 1;
}
/*static int lua_peekarb(lua_State *L)
{
lua::state st(L);
st.push(peekarb(st.as<DWORD>(1)));
return 1;
}*/
static int lua_pokeb(lua_State *L)
{
lua::state st(L);
pokeb(st.as<size_t>(1),st.as<size_t>(2));
return 0;
}
static int lua_poked(lua_State *L)
{
lua::state st(L);
poked(st.as<size_t>(1),st.as<size_t>(2));
return 0;
}
static int lua_pokew(lua_State *L)
{
lua::state st(L);
pokew(st.as<size_t>(1),st.as<size_t>(2));
return 0;
}
static int lua_pokearb(lua_State *L)
{
lua::state st(L);
void *p=(void *)lua_touserdata(L, 2);//st.as<lua::userdata>(2);
size_t size=st.as<size_t>(3);
pokearb(st.as<size_t>(1),p,size);
return 0;
}
static int lua_pokestr(lua_State *L)
{
lua::state st(L);
std::string trg=st.as<std::string>(2);
pokestr(st.as<size_t>(1),(char*)trg.c_str(),trg.size());
return 0;
}
const luaL_Reg lua_engine_func[]=
{
{"peekb",lua_peekb},
{"peekw",lua_peekw},
{"peekd",lua_peekd},
{"peekarb",lua_peekarb},
{"peekstr",lua_peekstr},
{"pokeb",lua_pokeb},
{"pokew",lua_pokew},
{"poked",lua_poked},
{"pokearb",lua_pokearb},
{"pokestr",lua_pokestr},
{NULL,NULL}
};
void lua::RegisterEngine(lua::state &st)
{
st.newtable();
lua::RegFunctionsLocal(st,lua_engine_func);
st.setglobal("engine");
}