From e8788d88722f1b42e2141bfae2abbf62c91c2ab9 Mon Sep 17 00:00:00 2001 From: Warmist Date: Sun, 4 Mar 2012 14:45:15 +0200 Subject: [PATCH 1/4] Lots of work done with xml parsing. --- plugins/Dfusion/dfusion.cpp | 2 + plugins/Dfusion/luafiles/common.lua | 22 ++++- plugins/Dfusion/luafiles/init.lua | 4 +- plugins/Dfusion/luafiles/patterns.lua | 4 +- plugins/Dfusion/luafiles/xml_struct.lua | 83 +++++++++++++---- plugins/Dfusion/luafiles/xml_types.lua | 117 +++++++++++++++++++++--- plugins/Dfusion/src/lua_Offsets.cpp | 114 ++++++++++++++--------- 7 files changed, 269 insertions(+), 77 deletions(-) diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp index 29492d801..b06a7d278 100644 --- a/plugins/Dfusion/dfusion.cpp +++ b/plugins/Dfusion/dfusion.cpp @@ -19,6 +19,7 @@ #include "lua_VersionInfo.h" #include "functioncall.h" #include "lua_FunctionCall.h" +#include "lua_Offsets.h" using std::vector; using std::string; @@ -48,6 +49,7 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector lua::RegisterMisc(st); lua::RegisterVersionInfo(st); lua::RegisterFunctionCall(st); + lua::RegisterEngine(st); #ifdef LINUX_BUILD st.push(1); diff --git a/plugins/Dfusion/luafiles/common.lua b/plugins/Dfusion/luafiles/common.lua index 98043f463..4548acecf 100644 --- a/plugins/Dfusion/luafiles/common.lua +++ b/plugins/Dfusion/luafiles/common.lua @@ -3,6 +3,9 @@ STD_STRING=0 DWORD=1 WORD=2 BYTE=3 +QWORD=4 +DOUBLE=5 +FLOAT=6 function printd(...) if DEBUG then print(...) @@ -94,6 +97,7 @@ function SetExecute(pos) end -- engine bindings engine=engine or {} +--[=[ use default peek/pokes for now engine.peekd=Process.readDWord engine.poked=Process.writeDWord engine.peekb=Process.readByte @@ -106,7 +110,7 @@ engine.peekstr=Process.readCString --engine.pokestr=Process.readCString engine.peekarb=Process.read engine.pokearb=Process.write - +--]=] function engine.peek(offset,rtype) if type(rtype)=="table" then @@ -117,13 +121,19 @@ function engine.peek(offset,rtype) end end if rtype==STD_STRING then - return engine.peekstr(offset) + return engine.peekstr2(offset) elseif rtype==DWORD then return engine.peekd(offset) elseif rtype==WORD then return engine.peekw(offset) elseif rtype==BYTE then return engine.peekb(offset) + elseif rtype==QWORD then + return engine.peekq(offset) + elseif rtype==FLOAT then + return engine.peekfloat(offset) + elseif rtype==DOUBLE then + return engine.peekdouble(offset) else error("Invalid peek type") return @@ -138,13 +148,19 @@ function engine.poke(offset,rtype,val) end end if rtype==STD_STRING then - return engine.pokestr(offset,val) + return engine.pokestr2(offset,val) elseif rtype==DWORD then return engine.poked(offset,val) elseif rtype==WORD then return engine.pokew(offset,val) elseif rtype==BYTE then return engine.pokeb(offset,val) + elseif rtype==QWORD then + return engine.pokeq(offset,val) + elseif rtype==FLOAT then + return engine.pokefloat(offset,val) + elseif rtype==DOUBLE then + return engine.pokedouble(offset,val) else error("Invalid poke type:"..tostring(rtype)) return diff --git a/plugins/Dfusion/luafiles/init.lua b/plugins/Dfusion/luafiles/init.lua index 5adb9abe3..4e6ebc6cb 100644 --- a/plugins/Dfusion/luafiles/init.lua +++ b/plugins/Dfusion/luafiles/init.lua @@ -60,12 +60,12 @@ table.insert(plugins,{"onfunction","run lua on some df function"}) loadall(plugins) dofile_silent("dfusion/initcustom.lua") -print("Locating saves...") +--[=[print("Locating saves...") local str=engine.peekstr(0x1447A40+offsets.base()) print("Current region:"..str) str="data/save/"..str.."/dfusion/init.lua" dofile_silent(str) - +--]=] if not INIT then mainmenu(plugins) end diff --git a/plugins/Dfusion/luafiles/patterns.lua b/plugins/Dfusion/luafiles/patterns.lua index c9f0e8c37..34aa0c274 100644 --- a/plugins/Dfusion/luafiles/patterns.lua +++ b/plugins/Dfusion/luafiles/patterns.lua @@ -170,11 +170,11 @@ end ]]-- ptr_Creature={} -local posoff=VersionInfo.getGroup("Creatures"):getGroup("creature"):getOffset("position") +local posoff=0 --VersionInfo.getGroup("Creatures"):getGroup("creature"):getOffset("position") ptr_Creature.x={off=posoff,rtype=WORD} --ok ptr_Creature.y={off=posoff+2,rtype=WORD} --ok ptr_Creature.z={off=posoff+4,rtype=WORD} --ok -ptr_Creature.flags={off=VersionInfo.getGroup("Creatures"):getGroup("creature"):getOffset("flags1"),rtype=ptt_dfflag.new(10)} +ptr_Creature.flags={off=0,rtype=ptt_dfflag.new(10)} ptr_Creature.name={off=0,rtype=ptt_dfstring} ptr_Creature.ID={off=252,rtype=DWORD} --ok i guess ptr_Creature.followID={off=592,rtype=DWORD} --ok diff --git a/plugins/Dfusion/luafiles/xml_struct.lua b/plugins/Dfusion/luafiles/xml_struct.lua index e0f519b50..0a6b54138 100644 --- a/plugins/Dfusion/luafiles/xml_struct.lua +++ b/plugins/Dfusion/luafiles/xml_struct.lua @@ -24,47 +24,96 @@ types.building=sometype ]=] function parseTree(t) - - for k,v in ipairs(t) do if v.xarg~=nil and v.xarg["type-name"]~=nil and v.label=="ld:global-type" then local name=v.xarg["type-name"]; - print("Parsing:"..name) - for kk,vv in pairs(v.xarg) do - print("\t"..kk.." "..tostring(vv)) + if(types[name]==nil) then + --print("Parsing:"..name) + --for kk,vv in pairs(v.xarg) do + -- print("\t"..kk.." "..tostring(vv)) + --end + types[name]=makeType(v) + --print("found "..name.." or type:"..v.xarg.meta or v.xarg.base) end - types[name]=makeType(v) - - - print("found "..name.." or type:"..v.xarg.meta or v.xarg.base) end end end +function parseTreeGlobals(t) + local glob={} + print("Parsing global-objects") + for k,v in ipairs(t) do + if v.xarg~=nil and v.label=="ld:global-object" then + local name=v.xarg["name"]; + --print("Parsing:"..name) + local subitem=v[1] + if subitem==nil then + error("Global-object subitem is nil:"..name) + end + local ret=makeType(subitem) + if ret==nil then + error("Make global returned nil!") + end + glob[name]=ret + end + end + --print("Printing globals:") + --for k,v in pairs(glob) do + -- print(k) + --end + return glob +end function findAndParse(tname) for k,v in ipairs(main_tree) do - local name=v.xarg["type-name"]; + local name=v.xarg["type-name"]; if v.xarg~=nil and v.xarg["type-name"]~=nil and v.label=="ld:global-type" then + if(name ==tname) then - print("Parsing:"..name) - for kk,vv in pairs(v.xarg) do - print("\t"..kk.." "..tostring(vv)) - end + --print("Parsing:"..name) + --for kk,vv in pairs(v.xarg) do + -- print("\t"..kk.." "..tostring(vv)) + --end types[name]=makeType(v) end - --print("found "..name.." or type:"..v.xarg.meta or v.xarg.base) end end end - - +df={} +df.types=rawget(df,"types") or {} --temporary measure for debug +local df_meta={} +function df_meta:__index(key) + local addr=VersionInfo.getAddress(key) + local vartype=rawget(df,"types")[key]; + if addr==nil then + error("No such global address exist") + elseif vartype==nil then + error("No such global type exist") + else + return type_read(vartype,addr) + end +end +function df_meta:__newindex(key,val) + local addr=VersionInfo.getAddress(key) + local vartype=rawget(df,"types")[key]; + if addr==nil then + error("No such global address exist") + elseif vartype==nil then + error("No such global type exist") + else + return type_write(vartype,addr,val) + end +end +setmetatable(df,df_meta) -------------------------------- types=types or {} dofile("dfusion/patterns/xml_angavrilov.lua") +-- [=[ main_tree=parseXmlFile("dfusion/patterns/supplementary.xml")[1] parseTree(main_tree) main_tree=parseXmlFile("dfusion/patterns/codegen.out.xml")[1] parseTree(main_tree) +rawset(df,"types",parseTreeGlobals(main_tree)) +--]=] --[=[labels={} for k,v in ipairs(t) do labels[v.label]=labels[v.label] or {meta={}} diff --git a/plugins/Dfusion/luafiles/xml_types.lua b/plugins/Dfusion/luafiles/xml_types.lua index 02e49fe9e..b96e77b47 100644 --- a/plugins/Dfusion/luafiles/xml_types.lua +++ b/plugins/Dfusion/luafiles/xml_types.lua @@ -1,15 +1,16 @@ function type_read(valtype,address) - if type(valtype)~= "table" then - return engine.peek(valtype,address) + if valtype.issimple then + print("Simple read:"..tostring(valtype.ctype)) + return engine.peek(address,valtype.ctype) else return valtype:makewrap(address) end end function type_write(valtype,address,val) - if type(valtype)~= "table" then - engine.poke(valtype,address,val) + if altype.issimple then + engine.poke(address,valtype.ctype,val) else - engine.poke(DWORD,address,rawget(val,"ptr")) + engine.poke(address,DWORD,rawget(val,"ptr")) end end function first_of_type(node,labelname) @@ -65,6 +66,9 @@ xtypes["static-array"]=sarr simpletypes={} + +simpletypes["s-float"]={FLOAT,4} +simpletypes.int64_t={QWORD,8} simpletypes.uint32_t={DWORD,4} simpletypes.uint16_t={WORD,2} simpletypes.uint8_t={BYTE,1} @@ -78,6 +82,7 @@ function getSimpleType(typename) local o={} o.ctype=simpletypes[typename][1] o.size=simpletypes[typename][2] + o.issimple=true return o end local type_enum={} @@ -130,7 +135,7 @@ function type_class.new(node) local t_name="" local name=v.xarg.name or v.xarg["anon-name"] or ("unk_"..k) - print("\t"..k.." "..name.."->"..v.xarg.meta.." ttype:"..v.label) + --print("\t"..k.." "..name.."->"..v.xarg.meta.." ttype:"..v.label) local ttype=makeType(v) --for k,v in pairs(ttype) do @@ -206,11 +211,49 @@ xtypes["pointer"]=type_class --stl-vector (beginptr,endptr,allocptr) --df-flagarray (ptr,size) xtypes.containers={} +local dfarr={} +dfarr.__index=dfarr +function dfarr.new(node) + local o={} + setmetatable(o,dfarr) + o.size=8 + return o +end +function dfarr:makewrap(address) + local o={} + o.mtype=self + o.ptr=address + setmetatable(o,self.wrap) + return o +end +dfarr.wrap={} +function dfarr.wrap:__index(key) + local num=tonumber(key) + local mtype=rawget(self,"mtype") + local size=type_read(rawget(self,"ptr")+4,DWORD) + error("TODO make __index for dfarray") + if num~=nil and num +#include //TODO make a seperate module with peeks/pokes and page permisions (linux/windows spec) //TODO maybe remove alltogether- use DFHack::Process instead? -unsigned char peekb(size_t offset) -{ - return *((unsigned char*)(offset)); -} -unsigned short peekw(size_t offset) +template +T engine_peek(size_t offset) { - return *((unsigned short*)(offset)); + return *(reinterpret_cast(offset)); } -unsigned peekd(size_t offset) +template +void engine_poke(size_t offset,T val) { - return *((unsigned*)(offset)); + *(reinterpret_cast(offset))=val; } + void peekarb(size_t offset, void *mem,size_t size) { memcpy(mem,(void*)offset,size); @@ -22,18 +22,6 @@ void peekstr(size_t offset, char* buf, size_t maxsize) { strncpy(buf,(char*)offset,maxsize); } -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); @@ -42,31 +30,42 @@ void pokestr(size_t offset, char* buf, size_t maxsize) { strncpy((char*)offset,buf,maxsize); } -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))); + st.push(engine_peek(st.as(1))); + return 1; +} +static int lua_peekw(lua_State *L) +{ + lua::state st(L); + st.push(engine_peek(st.as(1))); return 1; } static int lua_peekd(lua_State *L) { lua::state st(L); - st.push(peekd(st.as(1))); + st.push(engine_peek(st.as(1))); return 1; } -static int lua_peekw(lua_State *L) +static int lua_peekq(lua_State *L) +{ + lua::state st(L); + st.push(engine_peek(st.as(1))); + return 1; +} +static int lua_peekfloat(lua_State *L) +{ + lua::state st(L); + st.push(engine_peek(st.as(1))); + return 1; +} +static int lua_peekdouble(lua_State *L) { lua::state st(L); - st.push(peekw(st.as(1))); + st.push(engine_peek(st.as(1))); return 1; } static int lua_peekarb(lua_State *L) @@ -88,38 +87,53 @@ static int lua_peekstr(lua_State *L) delete [] buf; return 1; } -/*static int lua_peekarb(lua_State *L) +static int lua_peekstr2(lua_State *L) { lua::state st(L); - st.push(peekarb(st.as(1))); + st.push(engine_peek(st.as(1))); return 1; -}*/ +} static int lua_pokeb(lua_State *L) { lua::state st(L); - pokeb(st.as(1),st.as(2)); + engine_poke(st.as(1),st.as(2)); + return 0; +} +static int lua_pokew(lua_State *L) +{ + lua::state st(L); + engine_poke(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)); + engine_poke(st.as(1),st.as(2)); return 0; } -static int lua_pokew(lua_State *L) +static int lua_pokeq(lua_State *L) { lua::state st(L); - pokew(st.as(1),st.as(2)); + engine_poke(st.as(1),st.as(2)); + return 0; +} +static int lua_pokefloat(lua_State *L) +{ + lua::state st(L); + engine_poke(st.as(1),st.as(2)); + return 0; +} +static int lua_pokedouble(lua_State *L) +{ + lua::state st(L); + engine_poke(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; } @@ -130,18 +144,36 @@ static int lua_pokestr(lua_State *L) pokestr(st.as(1),(char*)trg.c_str(),trg.size()); return 0; } +static int lua_pokestr2(lua_State *L) +{ + lua::state st(L); + std::string trg=st.as(2); + engine_poke(st.as(1),trg); + return 0; +} const luaL_Reg lua_engine_func[]= { {"peekb",lua_peekb}, {"peekw",lua_peekw}, {"peekd",lua_peekd}, + {"peekq",lua_peekq}, + {"peekfloat",lua_peekfloat}, + {"peekdouble",lua_peekdouble}, + {"peekarb",lua_peekarb}, {"peekstr",lua_peekstr}, + {"peekstr2",lua_peekstr2}, + {"pokeb",lua_pokeb}, {"pokew",lua_pokew}, {"poked",lua_poked}, + {"pokeq",lua_pokeq}, + {"pokefloat",lua_pokefloat}, + {"pokedouble",lua_pokedouble}, + {"pokearb",lua_pokearb}, {"pokestr",lua_pokestr}, + {"pokestr2",lua_pokestr2}, {NULL,NULL} }; From 35ca21426b835387ef6d6ab91a21c67f061dcc88 Mon Sep 17 00:00:00 2001 From: Warmist Date: Sun, 11 Mar 2012 13:42:59 +0200 Subject: [PATCH 2/4] Df reading starting to work. Lots of testing required, some types don't have accessors yet, etc bugs --- plugins/Dfusion/CMakeLists.txt | 1 + .../luafiles/patterns/supplementary.xml | 7 + plugins/Dfusion/luafiles/xml_struct.lua | 27 +++- plugins/Dfusion/luafiles/xml_types.lua | 145 +++++++++++++++--- 4 files changed, 156 insertions(+), 24 deletions(-) create mode 100644 plugins/Dfusion/luafiles/patterns/supplementary.xml diff --git a/plugins/Dfusion/CMakeLists.txt b/plugins/Dfusion/CMakeLists.txt index 8ac2e79e9..c7f71c71c 100644 --- a/plugins/Dfusion/CMakeLists.txt +++ b/plugins/Dfusion/CMakeLists.txt @@ -13,3 +13,4 @@ DFHACK_PLUGIN(dfusion ${DFUSION_CPPS_ALL} ${DFUSION_HS} LINK_LIBRARIES lua) # installs into DF root install(DIRECTORY luafiles/ DESTINATION dfusion) +install(FILES ../../library/include/df/codegen.out.xml DESTINATION dfusion/patterns/) \ No newline at end of file diff --git a/plugins/Dfusion/luafiles/patterns/supplementary.xml b/plugins/Dfusion/luafiles/patterns/supplementary.xml new file mode 100644 index 000000000..e341a1368 --- /dev/null +++ b/plugins/Dfusion/luafiles/patterns/supplementary.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/plugins/Dfusion/luafiles/xml_struct.lua b/plugins/Dfusion/luafiles/xml_struct.lua index 0a6b54138..7baa5c48a 100644 --- a/plugins/Dfusion/luafiles/xml_struct.lua +++ b/plugins/Dfusion/luafiles/xml_struct.lua @@ -84,7 +84,7 @@ local df_meta={} function df_meta:__index(key) local addr=VersionInfo.getAddress(key) local vartype=rawget(df,"types")[key]; - if addr==nil then + if addr==0 then error("No such global address exist") elseif vartype==nil then error("No such global type exist") @@ -95,7 +95,7 @@ end function df_meta:__newindex(key,val) local addr=VersionInfo.getAddress(key) local vartype=rawget(df,"types")[key]; - if addr==nil then + if addr==0 then error("No such global address exist") elseif vartype==nil then error("No such global type exist") @@ -129,3 +129,26 @@ for k,v in pairs(labels) do end end end--]=] +function addressOf(var) + local addr=rawget(var,"ptr") + return addr +end +function printGlobals() + print("Globals:") + for k,v in pairs(rawget(df,"types")) do + print(k) + end +end +function printFields(object) + local tbl + if getmetatable(object)==xtypes["struct-type"].wrap then + tbl=rawget(object,"mtype") + elseif getmetatable(object)==xtypes["struct-type"] then + tbl=object + else + error("Not an class_type or a class_object") + end + for k,v in pairs(tbl.types) do + print(k) + end +end \ No newline at end of file diff --git a/plugins/Dfusion/luafiles/xml_types.lua b/plugins/Dfusion/luafiles/xml_types.lua index b96e77b47..e1c870f24 100644 --- a/plugins/Dfusion/luafiles/xml_types.lua +++ b/plugins/Dfusion/luafiles/xml_types.lua @@ -1,13 +1,13 @@ function type_read(valtype,address) if valtype.issimple then - print("Simple read:"..tostring(valtype.ctype)) + --print("Simple read:"..tostring(valtype.ctype)) return engine.peek(address,valtype.ctype) else return valtype:makewrap(address) end end function type_write(valtype,address,val) - if altype.issimple then + if valtype.issimple then engine.poke(address,valtype.ctype,val) else engine.poke(address,DWORD,rawget(val,"ptr")) @@ -20,6 +20,15 @@ function first_of_type(node,labelname) end end end +function padAddress(curoff,sizetoadd) --return new offset to place things + --windows -> sizeof(x)==alignof(x) + if sizetoadd>8 then sizetoadd=8 end + if(math.mod(curoff,sizetoadd)==0) then + return curoff + else + return curoff+(sizetoadd-math.mod(curoff,sizetoadd)) + end +end xtypes={} -- list of all types prototypes (e.g. enum-type -> announcement_type) -- type must have new and makewrap (that makes a wrapper around ptr) local sarr={} @@ -27,13 +36,13 @@ sarr.__index=sarr function sarr.new(node) local o={} setmetatable(o,sarr) - print("Making array.") + --print("Making array.") o.count=tonumber(node.xarg.count) - print("got cound:"..o.count) + --print("got count:"..o.count) o.ctype=makeType(first_of_type(node,"ld:item")) o.size=o.count*o.ctype.size - print("got subtypesize:"..o.ctype.size) + --print("got subtypesize:"..o.ctype.size) return o end function sarr:makewrap(address) @@ -45,6 +54,9 @@ function sarr:makewrap(address) end sarr.wrap={} function sarr.wrap:__index(key) + if key=="size" then + return rawget(self,"mtype").count + end local num=tonumber(key) local mtype=rawget(self,"mtype") if num~=nil and num Date: Sun, 11 Mar 2012 14:33:08 +0200 Subject: [PATCH 3/4] Updated to new dfhack standards. --- plugins/Dfusion/dfusion.cpp | 87 ++++++++++++++----------- plugins/Dfusion/include/lua_Console.h | 4 +- plugins/Dfusion/luafiles/init.lua | 2 + plugins/Dfusion/src/lua_Console.cpp | 93 +++++++++++++++++++-------- 4 files changed, 119 insertions(+), 67 deletions(-) diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp index b06a7d278..84f66138c 100644 --- a/plugins/Dfusion/dfusion.cpp +++ b/plugins/Dfusion/dfusion.cpp @@ -31,8 +31,10 @@ uint64_t timeLast=0; DFHACK_PLUGIN("dfusion") -command_result dfusion (Core * c, vector & parameters); -command_result lua_run (Core * c, vector & parameters); +command_result dfusion (color_ostream &out, std::vector ¶meters); +command_result dfuse (color_ostream &out, std::vector ¶meters); +command_result lua_run (color_ostream &out, std::vector ¶meters); +command_result lua_run_file (color_ostream &out, std::vector ¶meters); DFhackCExport const char * plugin_name ( void ) { @@ -43,7 +45,7 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector { lua::state st=lua::glua::Get(); //maybe remake it to run automaticaly - lua::RegisterConsole(st,&c->con); + lua::RegisterConsole(st); lua::RegisterProcess(st,c->p); lua::RegisterHexsearch(st); lua::RegisterMisc(st); @@ -59,9 +61,10 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector st.setglobal("WINDOWS"); #endif - commands.push_back(PluginCommand("dfusion","Init dfusion system. Use 'dfusion init' to run dfusion in init mode (not interactive).",dfusion)); - commands.push_back(PluginCommand("lua", "Run interactive interpreter. Use 'lua ' to run instead.",lua_run)); - + commands.push_back(PluginCommand("dfusion","Run dfusion system (interactive i.e. can input further commands).",dfusion,true)); + commands.push_back(PluginCommand("dfuse","Init dfusion system (not interactive).",dfuse,false)); + commands.push_back(PluginCommand("lua", "Run interactive interpreter. Use 'lua ' to run instead.",lua_run,true)); + commands.push_back(PluginCommand("runlua", "Run non-interactive interpreter. Use 'runlua ' to run .",lua_run_file,false)); mymutex=new tthread::mutex; return CR_OK; } @@ -93,9 +96,9 @@ DFhackCExport command_result plugin_onupdate_DISABLED ( Core * c ) } catch(lua::exception &e) { - c->con.printerr("Error OnTick:%s\n",e.what()); - c->con.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); - c->con.msleep(1000); + c->getConsole().printerr("Error OnTick:%s\n",e.what()); + c->getConsole().printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); + c->getConsole().msleep(1000); } } s.settop(0); @@ -103,13 +106,15 @@ DFhackCExport command_result plugin_onupdate_DISABLED ( Core * c ) return CR_OK; } -void InterpreterLoop(Core* c) +void InterpreterLoop(color_ostream &out) { - Console &con=c->con; + DFHack::CommandHistory hist; lua::state s=lua::glua::Get(); string curline; - con.print("Type quit to exit interactive mode\n"); + out.print("Type quit to exit interactive mode\n"); + assert(out.is_console()); + Console &con = static_cast(out); con.lineedit(">>",curline,hist); while (curline!="quit") { @@ -122,18 +127,27 @@ void InterpreterLoop(Core* c) catch(lua::exception &e) { con.printerr("Error:%s\n",e.what()); - c->con.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); + con.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); s.settop(0); } con.lineedit(">>",curline,hist); } s.settop(0); } -command_result lua_run (Core * c, vector & parameters) +command_result lua_run_file (color_ostream &out, std::vector ¶meters) +{ + if(parameters.size()==0) + { + out.printerr("runlua without file to run!"); + return CR_FAILURE; + } + return lua_run(out,parameters); +} +command_result lua_run (color_ostream &out, std::vector ¶meters) { - Console &con=c->con; mymutex->lock(); lua::state s=lua::glua::Get(); + lua::SetConsole(s,out); if(parameters.size()>0) { try{ @@ -142,23 +156,21 @@ command_result lua_run (Core * c, vector & parameters) } catch(lua::exception &e) { - con.printerr("Error:%s\n",e.what()); - c->con.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); + out.printerr("Error:%s\n",e.what()); + out.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); } } else { - InterpreterLoop(c); + InterpreterLoop(out); } s.settop(0);// clean up mymutex->unlock(); return CR_OK; } -void RunDfusion(void *p) +void RunDfusion(color_ostream &out) { - Console &con=static_cast(p)->con; mymutex->lock(); - lua::state s=lua::glua::Get(); try{ s.getglobal("err"); @@ -169,26 +181,27 @@ void RunDfusion(void *p) } catch(lua::exception &e) { - con.printerr("Error:%s\n",e.what()); - con.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); + out.printerr("Error:%s\n",e.what()); + out.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); } s.settop(0);// clean up mymutex->unlock(); } -command_result dfusion (Core * c, vector & parameters) +command_result dfuse(color_ostream &out, std::vector ¶meters) { - if(parameters[0]=="init") - { - lua::state s=lua::glua::Get(); - s.push(1); - s.setglobal("INIT"); - } - else - { - lua::state s=lua::glua::Get(); - s.push(); - s.setglobal("INIT"); - } - RunDfusion(c); + lua::state s=lua::glua::Get(); + lua::SetConsole(s,out); + s.push(1); + s.setglobal("INIT"); + RunDfusion(out); + return CR_OK; +} +command_result dfusion (color_ostream &out, std::vector ¶meters) +{ + lua::state s=lua::glua::Get(); + lua::SetConsole(s,out); + s.push(); + s.setglobal("INIT"); + RunDfusion(out); return CR_OK; } diff --git a/plugins/Dfusion/include/lua_Console.h b/plugins/Dfusion/include/lua_Console.h index 2ced33645..227effa9f 100644 --- a/plugins/Dfusion/include/lua_Console.h +++ b/plugins/Dfusion/include/lua_Console.h @@ -6,8 +6,8 @@ namespace lua { -void RegisterConsole(lua::state &st, DFHack::Console *c); - +void RegisterConsole(lua::state &st); +void SetConsole(lua::state &st,DFHack::color_ostream& stream); } #endif \ No newline at end of file diff --git a/plugins/Dfusion/luafiles/init.lua b/plugins/Dfusion/luafiles/init.lua index 4e6ebc6cb..ef2d4d603 100644 --- a/plugins/Dfusion/luafiles/init.lua +++ b/plugins/Dfusion/luafiles/init.lua @@ -44,6 +44,8 @@ function mainmenu(t1) end dofile("dfusion/common.lua") dofile("dfusion/utils.lua") +types=nil +dofile("dfusion/xml_struct.lua") unlockDF() plugins={} table.insert(plugins,{"simple_embark","A simple embark dwarf count editor"}) diff --git a/plugins/Dfusion/src/lua_Console.cpp b/plugins/Dfusion/src/lua_Console.cpp index 2e548b0fa..4f0d62f12 100644 --- a/plugins/Dfusion/src/lua_Console.cpp +++ b/plugins/Dfusion/src/lua_Console.cpp @@ -1,11 +1,11 @@ #include "lua_Console.h" //TODO error management. Using lua error? or something other? -static DFHack::Console* GetConsolePtr(lua::state &st) +static DFHack::color_ostream* GetConsolePtr(lua::state &st) { int t=st.gettop(); st.getglobal("Console"); st.getfield("__pointer"); - DFHack::Console* c=static_cast(lua_touserdata(st,-1)); + DFHack::color_ostream* c=static_cast(lua_touserdata(st,-1)); st.settop(t); return c; } @@ -13,7 +13,7 @@ static int lua_Console_print(lua_State *S) { lua::state st(S); int t=st.gettop(); - DFHack::Console* c=GetConsolePtr(st); + DFHack::color_ostream* c=GetConsolePtr(st); c->print("%s",st.as(t).c_str()); return 0; } @@ -22,7 +22,7 @@ static int lua_Console_printerr(lua_State *S) { lua::state st(S); int t=st.gettop(); - DFHack::Console* c=GetConsolePtr(st); + DFHack::color_ostream* c=GetConsolePtr(st); c->printerr("%s",st.as(t).c_str()); return 0; } @@ -30,69 +30,95 @@ static int lua_Console_printerr(lua_State *S) static int lua_Console_clear(lua_State *S) { lua::state st(S); - DFHack::Console* c=GetConsolePtr(st); + DFHack::color_ostream* c=GetConsolePtr(st); c->clear(); return 0; } static int lua_Console_gotoxy(lua_State *S) { lua::state st(S); - DFHack::Console* c=GetConsolePtr(st); - c->gotoxy(st.as(1,1),st.as(1,2)); + DFHack::color_ostream* c=GetConsolePtr(st); + if(c->is_console()) + { + DFHack::Console* con=static_cast(c); + con->gotoxy(st.as(1,1),st.as(1,2)); + } return 0; } static int lua_Console_color(lua_State *S) { lua::state st(S); - DFHack::Console* c=GetConsolePtr(st); + DFHack::color_ostream* c=GetConsolePtr(st); c->color( static_cast(st.as(-1,1)) ); return 0; } static int lua_Console_reset_color(lua_State *S) { lua::state st(S); - DFHack::Console* c=GetConsolePtr(st); + DFHack::color_ostream* c=GetConsolePtr(st); c->reset_color(); return 0; } static int lua_Console_cursor(lua_State *S) { lua::state st(S); - DFHack::Console* c=GetConsolePtr(st); - c->cursor(st.as(1)); + DFHack::color_ostream* c=GetConsolePtr(st); + if(c->is_console()) + { + DFHack::Console* con=static_cast(c); + con->cursor(st.as(1)); + } return 0; } static int lua_Console_msleep(lua_State *S) { lua::state st(S); - DFHack::Console* c=GetConsolePtr(st); - c->msleep(st.as(1)); + DFHack::color_ostream* c=GetConsolePtr(st); + if(c->is_console()) + { + DFHack::Console* con=static_cast(c); + con->msleep(st.as(1)); + } return 0; } static int lua_Console_get_columns(lua_State *S) { lua::state st(S); - DFHack::Console* c=GetConsolePtr(st); - st.push(c->get_columns()); + DFHack::color_ostream* c=GetConsolePtr(st); + if(c->is_console()) + { + DFHack::Console* con=static_cast(c); + st.push(con->get_columns()); + } return 1; } static int lua_Console_get_rows(lua_State *S) { lua::state st(S); - DFHack::Console* c=GetConsolePtr(st); - st.push(c->get_rows()); + DFHack::color_ostream* c=GetConsolePtr(st); + if(c->is_console()) + { + DFHack::Console* con=static_cast(c); + st.push(con->get_rows()); + } return 1; } static int lua_Console_lineedit(lua_State *S) { lua::state st(S); - DFHack::Console* c=GetConsolePtr(st); - string ret; - DFHack::CommandHistory hist; - int i=c->lineedit(st.as(1),ret,hist); - st.push(ret); - st.push(i); - return 2;// dunno if len is needed... + DFHack::color_ostream* c=GetConsolePtr(st); + if(c->is_console()) + { + DFHack::Console* con=static_cast(c); + string ret; + DFHack::CommandHistory hist; + int i=con->lineedit(st.as(1),ret,hist); + st.push(ret); + st.push(i); + return 2;// dunno if len is needed... + } + else + return 0; } const luaL_Reg lua_console_func[]= { @@ -109,7 +135,7 @@ const luaL_Reg lua_console_func[]= {"lineedit",lua_Console_lineedit}, {NULL,NULL} }; -void lua::RegisterConsole(lua::state &st, DFHack::Console *c) +void lua::RegisterConsole(lua::state &st) { st.getglobal("Console"); if(st.is()) @@ -118,10 +144,21 @@ void lua::RegisterConsole(lua::state &st, DFHack::Console *c) st.newtable(); } - st.pushlightuserdata(c); - st.setfield("__pointer"); - lua::RegFunctionsLocal(st, lua_console_func); //TODO add color consts st.setglobal("Console"); } +void lua::SetConsole(lua::state &st,DFHack::color_ostream& stream) +{ + int top=st.gettop(); + st.getglobal("Console"); + if(st.is()) + { + st.pop(); + st.newtable(); + } + + st.pushlightuserdata(&stream); + st.setfield("__pointer"); + st.settop(top); +} \ No newline at end of file From 081aee13631881efe8083ad28335a708d35defda Mon Sep 17 00:00:00 2001 From: Warmist Date: Sun, 11 Mar 2012 14:59:32 +0200 Subject: [PATCH 4/4] Fixes for DfHack::Process foolery. --- plugins/Dfusion/dfusion.cpp | 2 +- plugins/Dfusion/include/lua_Process.h | 2 +- plugins/Dfusion/luafiles/common.lua | 6 +++--- plugins/Dfusion/luafiles/xml_struct.lua | 2 +- plugins/Dfusion/src/lua_Process.cpp | 12 ++---------- 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp index 84f66138c..120e3fce6 100644 --- a/plugins/Dfusion/dfusion.cpp +++ b/plugins/Dfusion/dfusion.cpp @@ -46,7 +46,7 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector lua::state st=lua::glua::Get(); //maybe remake it to run automaticaly lua::RegisterConsole(st); - lua::RegisterProcess(st,c->p); + lua::RegisterProcess(st); lua::RegisterHexsearch(st); lua::RegisterMisc(st); lua::RegisterVersionInfo(st); diff --git a/plugins/Dfusion/include/lua_Process.h b/plugins/Dfusion/include/lua_Process.h index e6639cfc8..3429c9518 100644 --- a/plugins/Dfusion/include/lua_Process.h +++ b/plugins/Dfusion/include/lua_Process.h @@ -8,6 +8,6 @@ namespace lua { -void RegisterProcess(lua::state &st,DFHack::Process *p); +void RegisterProcess(lua::state &st); } #endif \ No newline at end of file diff --git a/plugins/Dfusion/luafiles/common.lua b/plugins/Dfusion/luafiles/common.lua index 4548acecf..2b706b7f7 100644 --- a/plugins/Dfusion/luafiles/common.lua +++ b/plugins/Dfusion/luafiles/common.lua @@ -19,9 +19,9 @@ function GetTextRegion() ranges__=Process.getMemRanges() --print("Ranges:"..#ranges__) 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 + for k2,v2 in pairs(v) do + --print(string.format("%d %s->%s",k,tostring(k2),tostring(v2))) + end --local num --flgs="" --if(v["read"])then flgs=flgs..'r' end diff --git a/plugins/Dfusion/luafiles/xml_struct.lua b/plugins/Dfusion/luafiles/xml_struct.lua index 7baa5c48a..5c2cb9b26 100644 --- a/plugins/Dfusion/luafiles/xml_struct.lua +++ b/plugins/Dfusion/luafiles/xml_struct.lua @@ -40,7 +40,7 @@ function parseTree(t) end function parseTreeGlobals(t) local glob={} - print("Parsing global-objects") + --print("Parsing global-objects") for k,v in ipairs(t) do if v.xarg~=nil and v.label=="ld:global-object" then local name=v.xarg["name"]; diff --git a/plugins/Dfusion/src/lua_Process.cpp b/plugins/Dfusion/src/lua_Process.cpp index 6d2b3d877..19bf698d1 100644 --- a/plugins/Dfusion/src/lua_Process.cpp +++ b/plugins/Dfusion/src/lua_Process.cpp @@ -2,12 +2,7 @@ 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; + return DFHack::Core::getInstance().p; } static int lua_Process_readDWord(lua_State *S) @@ -275,7 +270,7 @@ const luaL_Reg lua_process_func[]= {NULL,NULL} }; #undef PROC_FUNC -void lua::RegisterProcess(lua::state &st,DFHack::Process *p) +void lua::RegisterProcess(lua::state &st) { st.getglobal("Process"); if(st.is()) @@ -284,9 +279,6 @@ void lua::RegisterProcess(lua::state &st,DFHack::Process *p) st.newtable(); } - st.pushlightuserdata(p); - st.setfield("__pointer"); - lua::RegFunctionsLocal(st, lua_process_func); st.setglobal("Process");