diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp index 0324716ef..86cc20505 100644 --- a/plugins/Dfusion/dfusion.cpp +++ b/plugins/Dfusion/dfusion.cpp @@ -12,6 +12,7 @@ #include "luamain.h" #include "lua_Console.h" +#include "lua_Process.h" #include "functioncall.h" using std::vector; @@ -23,9 +24,6 @@ static tthread::mutex* mymutex=0; DFhackCExport command_result dfusion (Core * c, vector & parameters); DFhackCExport command_result lua_run (Core * c, vector & parameters); - typedef - int (__thiscall *dfprint)(const char*, char, char,void *) ; - DFhackCExport const char * plugin_name ( void ) { return "dfusion"; @@ -36,7 +34,7 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector commands.clear(); //maybe remake it to run automaticaly 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("lua", "Run interactive interpreter.\ \n Options: = run instead",lua_run)); diff --git a/plugins/Dfusion/include/lua_Process.h b/plugins/Dfusion/include/lua_Process.h new file mode 100644 index 000000000..515aa55af --- /dev/null +++ b/plugins/Dfusion/include/lua_Process.h @@ -0,0 +1,13 @@ +#ifndef LUA_PROCESS_H +#define LUA_PROCESS_H + +#include +#include + +#include "luamain.h" + +namespace lua +{ +void RegisterProcess(lua::state &st,DFHack::Process *p); +} +#endif \ No newline at end of file diff --git a/plugins/Dfusion/src/lua_Console.cpp b/plugins/Dfusion/src/lua_Console.cpp index f45fa8d56..d0b1a7c66 100644 --- a/plugins/Dfusion/src/lua_Console.cpp +++ b/plugins/Dfusion/src/lua_Console.cpp @@ -126,7 +126,12 @@ const luaL_Reg lua_console_func[]= }; void lua::RegisterConsole(lua::state &st, DFHack::Console *c) { - st.newtable(); + st.getglobal("Console"); + if(st.is()) + { + st.pop(); + st.newtable(); + } st.pushlightuserdata(c); st.setfield("__pointer"); diff --git a/plugins/Dfusion/src/lua_Offsets.cpp b/plugins/Dfusion/src/lua_Offsets.cpp index 459920ae4..4ecbf124b 100644 --- a/plugins/Dfusion/src/lua_Offsets.cpp +++ b/plugins/Dfusion/src/lua_Offsets.cpp @@ -142,9 +142,15 @@ const luaL_Reg lua_engine_func[]= {"pokestr",lua_pokestr}, {NULL,NULL} }; + void lua::RegisterEngine(lua::state &st) { - st.newtable(); + st.getglobal("engine"); + if(st.is()) + { + st.pop(); + st.newtable(); + } lua::RegFunctionsLocal(st,lua_engine_func); st.setglobal("engine"); } \ No newline at end of file diff --git a/plugins/Dfusion/src/lua_Process.cpp b/plugins/Dfusion/src/lua_Process.cpp new file mode 100644 index 000000000..4ca82afdf --- /dev/null +++ b/plugins/Dfusion/src/lua_Process.cpp @@ -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(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(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(1),st.as(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(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(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(1),st.as(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(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(1),st.as(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(2); + uint8_t* buf; + + if(!st.is(3)) + buf=(uint8_t*)lua_touserdata(st,3); + else + buf=new uint8_t[len]; + c->read(st.as(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(1),st.as(2),static_cast(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(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(1),st.as(2)); + return 0; +} +static int lua_Process_copySTLString(lua_State *S) +{ + lua::state st(S); + DFHack::Process* c=GetProcessPtr(st); + c->copySTLString(st.as(1),st.as(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(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(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(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 threads; + c->getThreadIDs(threads); + st.newtable(); + for(size_t i=0;i ranges; + c->getMemRanges(ranges); + st.newtable(); + for(size_t i=0;igetBase(); + 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(); + st.pop(); + st.getfield("end",1); + range.start=st.as(); + st.pop(); + + st.getfield("read",2); + trange.read=st.as(); + st.pop(); + st.getfield("write",2); + trange.write=st.as(); + st.pop(); + st.getfield("execute",2); + trange.execute=st.as(); + 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()) + { + st.pop(); + st.newtable(); + } + + st.pushlightuserdata(p); + st.setfield("__pointer"); + + lua::RegFunctionsLocal(st, lua_process_func); + + st.setglobal("Process"); +} \ No newline at end of file