Small fixes and lua port of Process

Signed-off-by: Warmist <Warmist@gmail.com>
develop
Warmist 2011-07-28 03:11:33 +03:00
parent e30975f8f3
commit 0799cde3af
5 changed files with 321 additions and 6 deletions

@ -12,6 +12,7 @@
#include "luamain.h" #include "luamain.h"
#include "lua_Console.h" #include "lua_Console.h"
#include "lua_Process.h"
#include "functioncall.h" #include "functioncall.h"
using std::vector; using std::vector;
@ -23,9 +24,6 @@ static tthread::mutex* mymutex=0;
DFhackCExport command_result dfusion (Core * c, vector <string> & parameters); DFhackCExport command_result dfusion (Core * c, vector <string> & parameters);
DFhackCExport command_result lua_run (Core * c, vector <string> & parameters); DFhackCExport command_result lua_run (Core * c, vector <string> & parameters);
typedef
int (__thiscall *dfprint)(const char*, char, char,void *) ;
DFhackCExport const char * plugin_name ( void ) DFhackCExport const char * plugin_name ( void )
{ {
return "dfusion"; return "dfusion";
@ -36,7 +34,7 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand>
commands.clear(); commands.clear();
//maybe remake it to run automaticaly //maybe remake it to run automaticaly
lua::RegisterConsole(lua::glua::Get(),&c->con); lua::RegisterConsole(lua::glua::Get(),&c->con);
lua::RegisterProcess(lua::glua::Get(),c->p);
commands.push_back(PluginCommand("dfusion","Init dfusion system.",dfusion)); commands.push_back(PluginCommand("dfusion","Init dfusion system.",dfusion));
commands.push_back(PluginCommand("lua", "Run interactive interpreter.\ commands.push_back(PluginCommand("lua", "Run interactive interpreter.\
\n Options: <filename> = run <filename> instead",lua_run)); \n Options: <filename> = run <filename> instead",lua_run));

@ -0,0 +1,13 @@
#ifndef LUA_PROCESS_H
#define LUA_PROCESS_H
#include <dfhack/Core.h>
#include <dfhack/Process.h>
#include "luamain.h"
namespace lua
{
void RegisterProcess(lua::state &st,DFHack::Process *p);
}
#endif

@ -126,7 +126,12 @@ const luaL_Reg lua_console_func[]=
}; };
void lua::RegisterConsole(lua::state &st, DFHack::Console *c) void lua::RegisterConsole(lua::state &st, DFHack::Console *c)
{ {
st.getglobal("Console");
if(st.is<lua::nil>())
{
st.pop();
st.newtable(); st.newtable();
}
st.pushlightuserdata(c); st.pushlightuserdata(c);
st.setfield("__pointer"); st.setfield("__pointer");

@ -142,9 +142,15 @@ const luaL_Reg lua_engine_func[]=
{"pokestr",lua_pokestr}, {"pokestr",lua_pokestr},
{NULL,NULL} {NULL,NULL}
}; };
void lua::RegisterEngine(lua::state &st) void lua::RegisterEngine(lua::state &st)
{ {
st.getglobal("engine");
if(st.is<lua::nil>())
{
st.pop();
st.newtable(); st.newtable();
}
lua::RegFunctionsLocal(st,lua_engine_func); lua::RegFunctionsLocal(st,lua_engine_func);
st.setglobal("engine"); st.setglobal("engine");
} }

@ -0,0 +1,293 @@
#include "lua_Process.h"
static DFHack::Process* GetProcessPtr(lua::state &st)
{
int t=st.gettop();
st.getglobal("Process");
st.getfield("__pointer");
DFHack::Process* c=static_cast<DFHack::Process*>(lua_touserdata(st,-1));
st.settop(t);
return c;
}
static int lua_Process_readDWord(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
uint32_t ret=c->readDWord(st.as<uint32_t>(1));
st.push(ret);
return 1;
}
static int lua_Process_writeDWord(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
c->writeDWord(st.as<uint32_t>(1),st.as<uint32_t>(2));
return 0;
}
static int lua_Process_readFloat(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
float ret=c->readFloat(st.as<uint32_t>(1));
st.push(ret);
return 1;
}
static int lua_Process_readWord(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
uint16_t ret=c->readWord(st.as<uint32_t>(1));
st.push(ret);
return 1;
}
static int lua_Process_writeWord(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
c->writeWord(st.as<uint32_t>(1),st.as<uint16_t>(2));
return 0;
}
static int lua_Process_readByte(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
uint8_t ret=c->readByte(st.as<uint32_t>(1));
st.push(ret);
return 1;
}
static int lua_Process_writeByte(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
c->writeByte(st.as<uint32_t>(1),st.as<uint8_t>(2));
return 0;
}
static int lua_Process_read(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
size_t len=st.as<uint32_t>(2);
uint8_t* buf;
if(!st.is<lua::nil>(3))
buf=(uint8_t*)lua_touserdata(st,3);
else
buf=new uint8_t[len];
c->read(st.as<uint32_t>(1),len,buf);
st.pushlightuserdata(buf);
return 1;
}
static int lua_Process_write(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
c-> write(st.as<uint32_t>(1),st.as<uint32_t>(2),static_cast<uint8_t*>(lua_touserdata(st,3)));
return 0;
}
static int lua_Process_readSTLString (lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
std::string r=c->readSTLString(st.as<uint32_t>(1));
st.push(r);
return 1;
}
static int lua_Process_writeSTLString(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
c->writeSTLString(st.as<uint32_t>(1),st.as<std::string>(2));
return 0;
}
static int lua_Process_copySTLString(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
c->copySTLString(st.as<uint32_t>(1),st.as<uint32_t>(2));
return 0;
}
static int lua_Process_doReadClassName(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
std::string r=c->doReadClassName((void*)st.as<size_t>(1));
st.push(r);
return 1;
}
static int lua_Process_readClassName(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
std::string r=c->readClassName((void*)st.as<size_t>(1));
st.push(r);
return 1;
}
static int lua_Process_readCString (lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
std::string r=c->readCString(st.as<uint32_t>(1));
st.push(r);
return 1;
}
static int lua_Process_isSuspended(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
st.push(c->isSuspended());
return 1;
}
static int lua_Process_isIdentified(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
st.push(c->isIdentified());
return 1;
}
static int lua_Process_getThreadIDs(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
std::vector<uint32_t> threads;
c->getThreadIDs(threads);
st.newtable();
for(size_t i=0;i<threads.size();i++)
{
st.push(i);
st.push(threads[i]);
st.settable();
}
return 1;
}
static int lua_Process_getMemRanges(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
std::vector<DFHack::t_memrange> ranges;
c->getMemRanges(ranges);
st.newtable();
for(size_t i=0;i<ranges.size();i++)
{
st.push(i);
st.newtable();
st.push(ranges[i].start); // WARNING!! lua has only 32bit numbers, possible loss of data!!
st.setfield("start");
st.push(ranges[i].end);
st.setfield("end");
st.push(std::string(ranges[i].name));
st.setfield("name");
st.push(ranges[i].read);
st.setfield("read");
st.push(ranges[i].write);
st.setfield("write");
st.push(ranges[i].execute);
st.setfield("execute");
st.push(ranges[i].shared);
st.setfield("shared");
st.push(ranges[i].valid);
st.setfield("valid");
st.settable();
}
return 1;
}
static int lua_Process_getBase(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
uint32_t base=c->getBase();
st.push(base);
return 1;
}
/*static int lua_Process_getPID(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
int ret=c->getPID();
st.push(ret);
return 1;
}*/
static int lua_Process_getPath(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
std::string ret=c->getPath();
st.push(ret);
return 1;
}
static int lua_Process_setPermisions(lua_State *S)
{
lua::state st(S);
DFHack::Process* c=GetProcessPtr(st);
DFHack::t_memrange range,trange;
st.getfield("start",1);
range.start=st.as<uint64_t>();
st.pop();
st.getfield("end",1);
range.start=st.as<uint64_t>();
st.pop();
st.getfield("read",2);
trange.read=st.as<bool>();
st.pop();
st.getfield("write",2);
trange.write=st.as<bool>();
st.pop();
st.getfield("execute",2);
trange.execute=st.as<bool>();
st.pop();
c->setPermisions(range,trange);
return 0;
}
#define PROC_FUNC(name) {#name,lua_Process_ ## name}
const luaL_Reg lua_process_func[]=
{
PROC_FUNC(readDWord),
PROC_FUNC(writeDWord),
PROC_FUNC(readFloat),
PROC_FUNC(readWord),
PROC_FUNC(writeWord),
PROC_FUNC(readByte),
PROC_FUNC(writeByte),
PROC_FUNC(read),
PROC_FUNC(write),
PROC_FUNC(readSTLString),
PROC_FUNC(writeSTLString),
PROC_FUNC(copySTLString),
PROC_FUNC(doReadClassName),
PROC_FUNC(readClassName),
PROC_FUNC(readCString ),
PROC_FUNC(isSuspended),
PROC_FUNC(isIdentified),
PROC_FUNC(getThreadIDs),
PROC_FUNC(getMemRanges),
PROC_FUNC(getBase),
//PROC_FUNC(getPID), //not implemented
PROC_FUNC(getPath),
PROC_FUNC(setPermisions),
{NULL,NULL}
};
#undef PROC_FUNC
void lua::RegisterProcess(lua::state &st,DFHack::Process *p)
{
st.getglobal("Process");
if(st.is<lua::nil>())
{
st.pop();
st.newtable();
}
st.pushlightuserdata(p);
st.setfield("__pointer");
lua::RegFunctionsLocal(st, lua_process_func);
st.setglobal("Process");
}