From d262986740e344a4ebd2a343be8bbeaf7d538928 Mon Sep 17 00:00:00 2001 From: Warmist Date: Wed, 27 Jul 2011 23:10:20 +0300 Subject: [PATCH 1/8] Added simple readme, fixed sdl_threads and moved to tinythreads (mutex for lua engine). Other small fixes Signed-off-by: Warmist --- plugins/Dfusion/CMakeLists.txt | 1 + plugins/Dfusion/dfusion.cpp | 22 +++++++++++++--------- plugins/Dfusion/include/lua_Offsets.h | 13 +++++++++++++ plugins/Dfusion/readme.txt | 19 +++++++++++++++++++ plugins/Dfusion/src/lua_Offsets.cpp | 18 ++++++++++++++++++ 5 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 plugins/Dfusion/include/lua_Offsets.h create mode 100644 plugins/Dfusion/readme.txt create mode 100644 plugins/Dfusion/src/lua_Offsets.cpp diff --git a/plugins/Dfusion/CMakeLists.txt b/plugins/Dfusion/CMakeLists.txt index 74527acb6..e58069e69 100644 --- a/plugins/Dfusion/CMakeLists.txt +++ b/plugins/Dfusion/CMakeLists.txt @@ -2,6 +2,7 @@ find_package(Lua51 QUIET) if(LUA51_FOUND) include_directories(${LUA_INCLUDE_DIR} include) + include_directories("${dfhack_SOURCE_DIR}/library/depends/tthread") FILE(GLOB DFUSION_CPPS src/*.c*) set( DFUSION_CPPS_ALL diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp index 0cd6883fa..28977c51d 100644 --- a/plugins/Dfusion/dfusion.cpp +++ b/plugins/Dfusion/dfusion.cpp @@ -6,6 +6,10 @@ #include #include + +#include "tinythread.h" + + #include "luamain.h" #include "lua_Console.h" #include "functioncall.h" @@ -14,7 +18,7 @@ using std::vector; using std::string; using namespace DFHack; -static SDL::Mutex* mymutex=0; +static tthread::mutex* mymutex=0; DFhackCExport command_result dfusion (Core * c, vector & parameters); DFhackCExport command_result lua_run (Core * c, vector & parameters); @@ -37,7 +41,7 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector commands.push_back(PluginCommand("lua", "Run interactive interpreter.\ \n Options: = run instead",lua_run)); - mymutex=SDL_CreateMutex(); + mymutex=new tthread::mutex; return CR_OK; } @@ -45,6 +49,7 @@ DFhackCExport command_result plugin_shutdown ( Core * c ) { // shutdown stuff + delete mymutex; return CR_OK; } @@ -58,7 +63,7 @@ DFhackCExport command_result plugin_onupdate ( Core * c ) c->con.print("Time delta = %d ms\n", delta); } return CR_OK;*/ - SDL_mutexP(mymutex); + mymutex->lock(); lua::state s=lua::glua::Get(); s.getglobal("OnTick"); if(s.is()) @@ -73,7 +78,7 @@ DFhackCExport command_result plugin_onupdate ( Core * c ) } } s.settop(0); - SDL_mutexV(mymutex); + mymutex->unlock(); return CR_OK; } @@ -81,7 +86,7 @@ DFhackCExport command_result plugin_onupdate ( Core * c ) DFhackCExport command_result lua_run (Core * c, vector & parameters) { Console &con=c->con; - SDL_mutexP(mymutex); + mymutex->lock(); lua::state s=lua::glua::Get(); if(parameters.size()>0) { @@ -99,15 +104,14 @@ DFhackCExport command_result lua_run (Core * c, vector & parameters) //TODO interpreter... } s.settop(0);// clean up - SDL_mutexV(mymutex); + mymutex->unlock(); return CR_OK; } DFhackCExport command_result dfusion (Core * c, vector & parameters) { Console &con=c->con; - con.print("%x\n",c->p->getBase()); - SDL_mutexP(mymutex); + mymutex->lock(); lua::state s=lua::glua::Get(); try{ @@ -119,6 +123,6 @@ DFhackCExport command_result dfusion (Core * c, vector & parameters) con.printerr("Error:%s\n",e.what()); } s.settop(0);// clean up - SDL_mutexV(mymutex); + mymutex->unlock(); return CR_OK; } diff --git a/plugins/Dfusion/include/lua_Offsets.h b/plugins/Dfusion/include/lua_Offsets.h new file mode 100644 index 000000000..8d6a94b95 --- /dev/null +++ b/plugins/Dfusion/include/lua_Offsets.h @@ -0,0 +1,13 @@ +#ifndef LUA_OFFSETS_H +#define LUA_OFFSETS_H +#include "luamain.h" + +namespace lua +{ + +void RegisterEngine(lua::state &st); + +} + + +#endif \ No newline at end of file diff --git a/plugins/Dfusion/readme.txt b/plugins/Dfusion/readme.txt new file mode 100644 index 000000000..6ddbeff59 --- /dev/null +++ b/plugins/Dfusion/readme.txt @@ -0,0 +1,19 @@ +Dfusion command runs file \dfusion\init.lua +Other than std lua commands supported: + * Console. + print(string) + printerr(string) + clear() + gotoxy(x,y) + color(int) //TODO add id's, use numbers upto 16 for now + reset_color() + cursor(true/false) + msleep(int) + get_columns() + get_rows() + lineedit(string) //text input through console + history_add(string) // adds string to console history + +Functions searched: + OnTick() + If defined is called each DFHack tick. \ No newline at end of file diff --git a/plugins/Dfusion/src/lua_Offsets.cpp b/plugins/Dfusion/src/lua_Offsets.cpp new file mode 100644 index 000000000..86768d051 --- /dev/null +++ b/plugins/Dfusion/src/lua_Offsets.cpp @@ -0,0 +1,18 @@ +#include "lua_Offsets.h" +//TODO make a seperate module with peeks/pokes and page permisions (linux/windows spec) +unsigned char peekb(size_t offset) +{ + return *((unsigned char*)(offset)); +} +unsigned short peekw(size_t offset) +{ + return *((unsigned short*)(offset)); +} +unsigned peekd(size_t offset) +{ + return *((unsigned*)(offset)); +} +void lua::RegisterEngine(lua::state &st) +{ + +} \ No newline at end of file From 260bdfac0d9c0f416fe6c493e314a6e8d1fdc2bb Mon Sep 17 00:00:00 2001 From: Warmist Date: Wed, 27 Jul 2011 23:41:56 +0300 Subject: [PATCH 2/8] Peek and pokes with lua bindings Signed-off-by: Warmist --- plugins/Dfusion/dfusion.cpp | 26 +++++- plugins/Dfusion/src/lua_Offsets.cpp | 134 +++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 3 deletions(-) diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp index 28977c51d..0324716ef 100644 --- a/plugins/Dfusion/dfusion.cpp +++ b/plugins/Dfusion/dfusion.cpp @@ -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 & parameters) { Console &con=c->con; @@ -101,7 +123,7 @@ DFhackCExport command_result lua_run (Core * c, vector & parameters) } else { - //TODO interpreter... + InterpreterLoop(c); } s.settop(0);// clean up mymutex->unlock(); diff --git a/plugins/Dfusion/src/lua_Offsets.cpp b/plugins/Dfusion/src/lua_Offsets.cpp index 86768d051..459920ae4 100644 --- a/plugins/Dfusion/src/lua_Offsets.cpp +++ b/plugins/Dfusion/src/lua_Offsets.cpp @@ -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 +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(1))); + return 1; +} +static int lua_peekd(lua_State *L) +{ + lua::state st(L); + st.push(peekd(st.as(1))); + return 1; +} +static int lua_peekw(lua_State *L) +{ + lua::state st(L); + st.push(peekw(st.as(1))); + return 1; +} +static int lua_peekarb(lua_State *L) +{ + lua::state st(L); + size_t size=st.as(2); + void *p=st.newuserdata(size); + peekarb(st.as(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(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(1))); + return 1; +}*/ +static int lua_pokeb(lua_State *L) +{ + lua::state st(L); + pokeb(st.as(1),st.as(2)); + return 0; +} +static int lua_poked(lua_State *L) +{ + lua::state st(L); + poked(st.as(1),st.as(2)); + return 0; +} +static int lua_pokew(lua_State *L) +{ + lua::state st(L); + pokew(st.as(1),st.as(2)); + return 0; +} +static int lua_pokearb(lua_State *L) +{ + lua::state st(L); + + + void *p=(void *)lua_touserdata(L, 2);//st.as(2); + size_t size=st.as(3); + + pokearb(st.as(1),p,size); + return 0; +} +static int lua_pokestr(lua_State *L) +{ + lua::state st(L); + std::string trg=st.as(2); + pokestr(st.as(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"); } \ No newline at end of file From f1d32ef766f94346eda069d9e781ccb230248ece Mon Sep 17 00:00:00 2001 From: Warmist Date: Wed, 27 Jul 2011 23:41:56 +0300 Subject: [PATCH 3/8] Peek and pokes with lua bindings. Also interpreter implemented Signed-off-by: Warmist --- plugins/Dfusion/dfusion.cpp | 26 +++++- plugins/Dfusion/src/lua_Offsets.cpp | 134 +++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 3 deletions(-) diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp index 28977c51d..0324716ef 100644 --- a/plugins/Dfusion/dfusion.cpp +++ b/plugins/Dfusion/dfusion.cpp @@ -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 & parameters) { Console &con=c->con; @@ -101,7 +123,7 @@ DFhackCExport command_result lua_run (Core * c, vector & parameters) } else { - //TODO interpreter... + InterpreterLoop(c); } s.settop(0);// clean up mymutex->unlock(); diff --git a/plugins/Dfusion/src/lua_Offsets.cpp b/plugins/Dfusion/src/lua_Offsets.cpp index 86768d051..459920ae4 100644 --- a/plugins/Dfusion/src/lua_Offsets.cpp +++ b/plugins/Dfusion/src/lua_Offsets.cpp @@ -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 +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(1))); + return 1; +} +static int lua_peekd(lua_State *L) +{ + lua::state st(L); + st.push(peekd(st.as(1))); + return 1; +} +static int lua_peekw(lua_State *L) +{ + lua::state st(L); + st.push(peekw(st.as(1))); + return 1; +} +static int lua_peekarb(lua_State *L) +{ + lua::state st(L); + size_t size=st.as(2); + void *p=st.newuserdata(size); + peekarb(st.as(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(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(1))); + return 1; +}*/ +static int lua_pokeb(lua_State *L) +{ + lua::state st(L); + pokeb(st.as(1),st.as(2)); + return 0; +} +static int lua_poked(lua_State *L) +{ + lua::state st(L); + poked(st.as(1),st.as(2)); + return 0; +} +static int lua_pokew(lua_State *L) +{ + lua::state st(L); + pokew(st.as(1),st.as(2)); + return 0; +} +static int lua_pokearb(lua_State *L) +{ + lua::state st(L); + + + void *p=(void *)lua_touserdata(L, 2);//st.as(2); + size_t size=st.as(3); + + pokearb(st.as(1),p,size); + return 0; +} +static int lua_pokestr(lua_State *L) +{ + lua::state st(L); + std::string trg=st.as(2); + pokestr(st.as(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"); } \ No newline at end of file From bc23cc9eca8a0f6053f0ef7a6087dfbb7a764df8 Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 28 Jul 2011 01:00:12 +0300 Subject: [PATCH 4/8] Made Process::setPermisions functions, to set memory page's permisions --- library/Process-linux.cpp | 13 +++++++++++++ library/Process-windows.cpp | 15 +++++++++++++++ library/include/dfhack/Process.h | 3 +++ 3 files changed, 31 insertions(+) diff --git a/library/Process-linux.cpp b/library/Process-linux.cpp index 23d8baf12..937bd155b 100644 --- a/library/Process-linux.cpp +++ b/library/Process-linux.cpp @@ -25,6 +25,7 @@ distribution. #include "Internal.h" #include #include +#include #include #include @@ -176,4 +177,16 @@ string Process::getPath() int Process::getPID() { return getpid(); +} + +bool Process::setPermisions(const t_memrange & range,const t_memrange &trgrange) +{ + int result; + int protect=0; + if(trgrange.read)protect|=PROT_READ; + if(trgrange.write)protect|=PROT_WRITE; + if(trgrange.execute)protect|=PROT_EXECUTE; + result=mprotect((void *)range.start, range.end-range.start,protect); + + return result==0; } \ No newline at end of file diff --git a/library/Process-windows.cpp b/library/Process-windows.cpp index 2edfc6325..88b77891e 100644 --- a/library/Process-windows.cpp +++ b/library/Process-windows.cpp @@ -356,3 +356,18 @@ string Process::getPath() string out(String); return(out.substr(0,out.find_last_of("\\"))); } + +bool Process::setPermisions(const t_memrange & range,const t_memrange &trgrange) +{ + DWORD newprotect=0; + if(trgrange.read && !trgrange.write && !trgrange.execute)newprotect=PAGE_READONLY; + if(trgrange.read && trgrange.write && !trgrange.execute)newprotect=PAGE_READWRITE; + if(!trgrange.read && !trgrange.write && trgrange.execute)newprotect=PAGE_EXECUTE; + if(trgrange.read && !trgrange.write && trgrange.execute)newprotect=PAGE_EXECUTE_READ; + if(trgrange.read && trgrange.write && trgrange.execute)newprotect=PAGE_EXECUTE_READWRITE; + DWORD oldprotect=0; + bool result; + result=VirtualProtect((LPVOID)range.start,range.end-range.start,newprotect,&oldprotect); + + return result; +} diff --git a/library/include/dfhack/Process.h b/library/include/dfhack/Process.h index d2ca7e355..df681c475 100644 --- a/library/include/dfhack/Process.h +++ b/library/include/dfhack/Process.h @@ -269,6 +269,9 @@ namespace DFHack int getPID(); /// get the DF Process FilePath std::string getPath(); + + /// modify permisions of memory range + bool setPermisions(const t_memrange & range,const t_memrange &trgrange); private: VersionInfo * my_descriptor; PlatformSpecific *d; From e30975f8f387fe4197ba7fa9e692072cf5392905 Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 28 Jul 2011 01:28:50 +0300 Subject: [PATCH 5/8] Functioncall minilib quickfix for unix. Signed-off-by: Warmist --- plugins/Dfusion/src/functioncall.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/Dfusion/src/functioncall.cpp b/plugins/Dfusion/src/functioncall.cpp index db78603a7..f95a644d8 100644 --- a/plugins/Dfusion/src/functioncall.cpp +++ b/plugins/Dfusion/src/functioncall.cpp @@ -73,7 +73,10 @@ int FunctionCaller::CallF(size_t count,callconv conv,void* f,const vector & int FunctionCaller::CallFunction(size_t func_ptr,callconv conv,const vector &arguments) { //nasty nasty code... - +#ifdef LINUX_BUILD //quick fix + if(conv==THIS_CALL) + conv=STD_CALL +#endif void* f= reinterpret_cast(func_ptr+base_); size_t count=arguments.size(); if(count==0) From 0799cde3afac2ec481b3e2fe686d2bce09fb21a7 Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 28 Jul 2011 03:11:33 +0300 Subject: [PATCH 6/8] Small fixes and lua port of Process Signed-off-by: Warmist --- plugins/Dfusion/dfusion.cpp | 6 +- plugins/Dfusion/include/lua_Process.h | 13 ++ plugins/Dfusion/src/lua_Console.cpp | 7 +- plugins/Dfusion/src/lua_Offsets.cpp | 8 +- plugins/Dfusion/src/lua_Process.cpp | 293 ++++++++++++++++++++++++++ 5 files changed, 321 insertions(+), 6 deletions(-) create mode 100644 plugins/Dfusion/include/lua_Process.h create mode 100644 plugins/Dfusion/src/lua_Process.cpp 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 From 6c2a822494795b0f26bfbb50951a89aab8ace132 Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 28 Jul 2011 04:08:57 +0300 Subject: [PATCH 7/8] small bugfix (lua_Process.cpp) and onTick rate slowed Signed-off-by: Warmist --- plugins/Dfusion/dfusion.cpp | 18 +++++++++--------- plugins/Dfusion/src/lua_Process.cpp | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp index 86cc20505..4b6a33710 100644 --- a/plugins/Dfusion/dfusion.cpp +++ b/plugins/Dfusion/dfusion.cpp @@ -3,6 +3,7 @@ #include #include #include +#include "dfhack/extra/stopwatch.h" #include #include @@ -20,6 +21,7 @@ using std::string; using namespace DFHack; static tthread::mutex* mymutex=0; +uint64_t timeLast=0; DFhackCExport command_result dfusion (Core * c, vector & parameters); DFhackCExport command_result lua_run (Core * c, vector & parameters); @@ -52,15 +54,12 @@ DFhackCExport command_result plugin_shutdown ( Core * c ) } DFhackCExport command_result plugin_onupdate ( Core * c ) -{ - /*if(timering == true) //TODO maybe reuse this to make it run less often. - { - uint64_t time2 = GetTimeMs64(); - uint64_t delta = time2-timeLast; - timeLast = time2; - c->con.print("Time delta = %d ms\n", delta); - } - return CR_OK;*/ +{ + uint64_t time2 = GetTimeMs64(); + uint64_t delta = time2-timeLast; + if(delta<100) + return CR_OK; + timeLast = time2; mymutex->lock(); lua::state s=lua::glua::Get(); s.getglobal("OnTick"); @@ -89,6 +88,7 @@ void InterpreterLoop(Core* c) con.lineedit(">>",curline); while (curline!="quit") { + con.history_add(curline); try { s.loadstring(curline); diff --git a/plugins/Dfusion/src/lua_Process.cpp b/plugins/Dfusion/src/lua_Process.cpp index 4ca82afdf..ea96f8ac8 100644 --- a/plugins/Dfusion/src/lua_Process.cpp +++ b/plugins/Dfusion/src/lua_Process.cpp @@ -229,7 +229,7 @@ static int lua_Process_setPermisions(lua_State *S) range.start=st.as(); st.pop(); st.getfield("end",1); - range.start=st.as(); + range.end=st.as(); st.pop(); st.getfield("read",2); From 5f729ffc0f9bc07b45d7de3dccf9ca60fbb726ea Mon Sep 17 00:00:00 2001 From: Warmist Date: Thu, 28 Jul 2011 04:22:15 +0300 Subject: [PATCH 8/8] Added script files and cmake install script for them --- plugins/Dfusion/CMakeLists.txt | 1 + plugins/Dfusion/luafiles/common.lua | 61 +++++++++++++++++++ plugins/Dfusion/luafiles/init.lua | 33 ++++++++++ plugins/Dfusion/luafiles/offsets.txt | 15 +++++ .../Dfusion/luafiles/simple_embark/plugin.lua | 15 +++++ 5 files changed, 125 insertions(+) create mode 100644 plugins/Dfusion/luafiles/common.lua create mode 100644 plugins/Dfusion/luafiles/init.lua create mode 100644 plugins/Dfusion/luafiles/offsets.txt create mode 100644 plugins/Dfusion/luafiles/simple_embark/plugin.lua diff --git a/plugins/Dfusion/CMakeLists.txt b/plugins/Dfusion/CMakeLists.txt index e58069e69..0607406d4 100644 --- a/plugins/Dfusion/CMakeLists.txt +++ b/plugins/Dfusion/CMakeLists.txt @@ -11,6 +11,7 @@ if(LUA51_FOUND) ) DFHACK_PLUGIN(dfusion ${DFUSION_CPPS_ALL}) target_link_libraries(dfusion ${LUA_LIBRARIES}) + install(DIRECTORY luafiles/ DESTINATION ${DFHACK_DATA_DESTINATION}/dfusion) else(LUA51_FOUND) MESSAGE(STATUS "Required libraries (lua51) not found - dfusion plugin can't be built.") endif(LUA51_FOUND) diff --git a/plugins/Dfusion/luafiles/common.lua b/plugins/Dfusion/luafiles/common.lua new file mode 100644 index 000000000..aeca0a820 --- /dev/null +++ b/plugins/Dfusion/luafiles/common.lua @@ -0,0 +1,61 @@ +offsets=offsets or {} +offsets._toff={} +offsets.get = function (name) + return offsets._toff[name] +end +offsets.getEx = function (name) + return offsets._toff[name]+Process.getBase() +end +offsets.load = function () + local f=io.open("dfusion/offsets.txt") + local line=f:read() + while line~=nil do + --print(line) + local sppliter=string.find(line,":") + offsets._toff[string.sub(line,1,sppliter-2)]=tonumber(string.sub(line,sppliter+2)) + line=f:read() + end +end +offsets.load() +function unlockDF() + local ranges=Process.getMemRanges() + for k,v in pairs(ranges) do + --for k2,v2 in pairs(v) do + -- print(string.format("%d %s->%s",k,tostring(k2),tostring(v2))) + --end + --local num + --num=0 + --if(v["read"])then num=num+1 end + --if(v["write"])then num=num+10 end + --if(v["execute"]) then num=num+100 end + --print(string.format("%d %x->%x %s %d",k,v["start"],v["end"],v.name,num)) + local pos=string.find(v.name,".text") + if pos~=nil then + v["write"]=true + Process.setPermisions(v,v) + end + end +end +function lockDF() + local ranges=Process.getMemRanges() + for k,v in pairs(ranges) do + --for k2,v2 in pairs(v) do + -- print(string.format("%d %s->%s",k,tostring(k2),tostring(v2))) + --end + --local num + --num=0 + --if(v["read"])then num=num+1 end + --if(v["write"])then num=num+10 end + --if(v["execute"]) then num=num+100 end + --print(string.format("%d %x->%x %s %d",k,v["start"],v["end"],v.name,num)) + local pos=string.find(v.name,".text") + if pos~=nil then + v["write"]=false + Process.setPermisions(v,v) + end + end +end +-- engine bindings +engine=engine or {} +engine.peekd=Process.readDWord +engine.poked=Process.writeDWord \ No newline at end of file diff --git a/plugins/Dfusion/luafiles/init.lua b/plugins/Dfusion/luafiles/init.lua new file mode 100644 index 000000000..4056da046 --- /dev/null +++ b/plugins/Dfusion/luafiles/init.lua @@ -0,0 +1,33 @@ +function err(msg) --make local maybe... + print(msg) + print(debug.traceback()) +end +function dofile(filename) --safer dofile, with traceback (very usefull) + f,perr=loadfile(filename) + if f~=nil then + return xpcall(f,err) + else + print(perr) + end +end +dofile("dfusion/common.lua") + +print("Unlocking Df .text section...") +unlockDF() +print("Done unlock") +lockDF() +dofile("dfusion/simple_embark/plugin.lua") +print("hello world") +Console.print("Hello world in console!\n") +--name=Console.lineedit("Enter name:") +--Console.print("Your name is:"..name) + +function OnTick() -- floods the console + r=Console.get_rows() + c=Console.get_columns() + Console.clear() + Console.gotoxy(math.random(1,r),math.random(1,2)) + Console.color(math.random(0,15)) + Console.print("*") +end +OnTick=nil \ No newline at end of file diff --git a/plugins/Dfusion/luafiles/offsets.txt b/plugins/Dfusion/luafiles/offsets.txt new file mode 100644 index 000000000..a87bda07a --- /dev/null +++ b/plugins/Dfusion/luafiles/offsets.txt @@ -0,0 +1,15 @@ +AdvCreatureVec : 0x12c44ac +CreatureGloss : 0x1308040 +CreaturePtr : 0xaf2430 +CreatureVec : 0x12c44ac +CurrentRace : 0x10f0c28 +Items : 0x12c4550 +Legends : 0x12c451c +Materials : 0x1307f50 +PlayerLegend : 0x145bfec +SiteData : 0x1307778 +StartDwarfs : 0x518332 +WordVec : 0x1308254 +WorldData : 0x1306148 +Xpointer : 0x7347f0 +vtableLegends : 0x6e7594 diff --git a/plugins/Dfusion/luafiles/simple_embark/plugin.lua b/plugins/Dfusion/luafiles/simple_embark/plugin.lua new file mode 100644 index 000000000..133cffb51 --- /dev/null +++ b/plugins/Dfusion/luafiles/simple_embark/plugin.lua @@ -0,0 +1,15 @@ +function simple_embark(num) +stoff=offsets.getEx('StartDwarfs') +print("Starting dwarves found:"..engine.peekd(stoff)) +engine.poked(stoff,num) +end +if not(FILE) then +print("Type in new ammount:") + repeat + ans=tonumber(io.read()) + if ans==nil or not(ans<=15000 and ans>0) then + print("incorrect choice") + end + until ans~=nil and (ans<=15000 and ans>0) + simple_embark(ans) +end \ No newline at end of file