Petr Mrázek 2011-08-09 03:35:08 +02:00
commit edb2a14c51
7 changed files with 490 additions and 19 deletions

@ -15,7 +15,8 @@
#include "lua_Console.h"
#include "lua_Process.h"
#include "lua_Hexsearch.h"
#include "lua_Misc.h"
#include "lua_Misc.h"
#include "lua_VersionInfo.h"
#include "functioncall.h"
using std::vector;
@ -40,25 +41,26 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand>
lua::RegisterConsole(lua::glua::Get(),&c->con);
lua::RegisterProcess(lua::glua::Get(),c->p);
lua::RegisterHexsearch(lua::glua::Get());
lua::RegisterMisc(lua::glua::Get());
lua::RegisterMisc(lua::glua::Get());
lua::RegisterVersionInfo(lua::glua::Get());
commands.push_back(PluginCommand("dfusion","Init dfusion system.",dfusion));
commands.push_back(PluginCommand("lua", "Run interactive interpreter.\
\n Options: <filename> = run <filename> instead",lua_run));
mymutex=new tthread::mutex;
return CR_OK;
}
DFhackCExport command_result plugin_shutdown ( Core * c )
{
// shutdown stuff
delete mymutex;
return CR_OK;
}
DFhackCExport command_result plugin_onupdate ( Core * c )
{
{
uint64_t time2 = GetTimeMs64();
uint64_t delta = time2-timeLast;
if(delta<100)
@ -91,7 +93,7 @@ void InterpreterLoop(Core* c)
string curline;
con.print("Type quit to exit interactive mode\n");
con.lineedit(">>",curline);
while (curline!="quit") {
con.history_add(curline);
try
@ -140,7 +142,7 @@ DFhackCExport command_result dfusion (Core * c, vector <string> & parameters)
Console &con=c->con;
mymutex->lock();
lua::state s=lua::glua::Get();
try{
s.loadfile("dfusion/init.lua"); //load script
s.pcall(0,0);// run it

@ -2,9 +2,36 @@
#define LUA_VERSIONINFO_H
#include <dfhack/Core.h>
#include <dfhack/VersionInfo.h>
#include "luamain.h"
namespace lua
{
class OffsetGroup
{
int tblid;
DFHack::OffsetGroup *p;
public:
int GetTableId(){return tblid;};
//OffsetGroup(lua_State *L,DFHack::OffsetGroup *p); //constructor from c++
OffsetGroup(lua_State *L,int id); //constructor from lua
int getOffset(lua_State *L);
int getAddress(lua_State *L);
int getHexValue(lua_State *L);
int getString(lua_State *L);
int getGroup(lua_State *L);
int getSafeOffset(lua_State *L);
int getSafeAddress(lua_State *L);
int PrintOffsets(lua_State *L);
int getName(lua_State *L);
int getFullName(lua_State *L);
int getParent(lua_State *L);
DEF_LUNE(OffsetGroup);
};
void RegisterVersionInfo(lua::state &st);
}
#endif

@ -28,7 +28,11 @@
#define lua_Integer_long 1
#define lua_Integer_int 1
#include <lua.hpp>
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#include <string>
#include <vector>
#include <new>

@ -3,21 +3,23 @@ STD_STRING=0
DWORD=1
WORD=2
BYTE=3
function GetTextRegion()
ranges__=ranges__ or 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
--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
--flgs=""
--if(v["read"])then flgs=flgs..'r' end
--if(v["write"])then flgs=flgs..'w' end
--if(v["execute"]) then flgs=flgs..'e' end
--if num>=100 then
--print(string.format("%d %x->%x %s %s",k,v["start"],v["end"],v.name or "",flgs))
--end
local pos=string.find(v.name,".text") or string.find(v.name,"libs/Dwarf_Fortress")
if(pos~=nil) and v["execute"] then
return v;
end
end
@ -472,4 +474,4 @@ function Allocate(size)
end
dofile("dfusion/patterns.lua")
dofile("dfusion/patterns2.lua")
dofile("dfusion/itempatterns.lua")
dofile("dfusion/itempatterns.lua")

@ -1,3 +1,6 @@
function print(msg)
Console.print(msg.."\n")
end
function err(msg) --make local maybe...
print(msg)
print(debug.traceback())

@ -1,7 +1,12 @@
function f_dwarves()
pos_=offsets.findall(0,0x24,0x14,0x07,0,0,0) --search pattern
for _,v in pairs(pos_) do
print(string.format("Possible hit:%x",v))
end
pos=offsets.find(0,0x24,0x14,0x07,0,0,0,0xeb,0x08,0x8d) --search pattern
print(string.format("Start dwarf position:%x",pos))
if pos~=0 then
return pos+2-offsets.base();
else
@ -317,4 +322,4 @@ function f_materials()
end
return 0
end
offsets.new("Materials",f_materials)
offsets.new("Materials",f_materials)

@ -1 +1,429 @@
#include "lua_VersionInfo.h"
namespace lua
{
OffsetGroup::OffsetGroup(lua_State *L,int id):tblid(id)
{
p=static_cast<DFHack::OffsetGroup*>(lua_touserdata(L,1));
}
int OffsetGroup::getOffset(lua_State *L)
{
lua::state st(L);
int32_t ret=p->getOffset(st.as<std::string>(2));
st.push(ret);
return 1;
}
int OffsetGroup::getAddress(lua_State *L)
{
lua::state st(L);
uint32_t ret=p->getAddress(st.as<std::string>(2));
st.push(ret);
return 1;
}
int OffsetGroup::getHexValue(lua_State *L)
{
lua::state st(L);
uint32_t ret=p->getHexValue(st.as<std::string>(2));
st.push(ret);
return 1;
}
int OffsetGroup::getString(lua_State *L)
{
lua::state st(L);
std::string ret=p->getString(st.as<std::string>(2));
st.push(ret);
return 1;
}
int OffsetGroup::getGroup(lua_State *L)
{
lua::state st(L);
DFHack::OffsetGroup* t= p->getGroup(st.as<std::string>(2));
st.getglobal("OffsetGroup");
st.getfield("new");
st.pushlightuserdata(t);
st.pcall(1,1);
return 1;
}
int OffsetGroup::getSafeOffset(lua_State *L)
{
lua::state st(L);
int32_t out;
bool ret=p->getSafeOffset(st.as<std::string>(2),out);
st.push(ret);
st.push(out);
return 2;
}
int OffsetGroup::getSafeAddress(lua_State *L)
{
lua::state st(L);
uint32_t out;
bool ret=p->getSafeAddress(st.as<std::string>(2),out);
st.push(ret);
st.push(out);
return 2;
}
int OffsetGroup::PrintOffsets(lua_State *L)
{
lua::state st(L);
std::string output;
output=p->PrintOffsets(st.as<int>(2));
st.push(output);
return 1;
}
int OffsetGroup::getName(lua_State *L)
{
lua::state st(L);
std::string ret=p->getName();
st.push(ret);
return 1;
}
int OffsetGroup::getFullName(lua_State *L)
{
lua::state st(L);
std::string ret=p->getFullName();
st.push(ret);
return 1;
}
int OffsetGroup::getParent(lua_State *L)
{
lua::state st(L);
DFHack::OffsetGroup* t= p->getParent();
st.getglobal("OffsetGroup");
st.getfield("new");
st.pushlightuserdata(t);
st.pcall(1,1);
return 1;
}
}
IMP_LUNE(lua::OffsetGroup,OffsetGroup);
LUNE_METHODS_START(lua::OffsetGroup)
method(lua::OffsetGroup,getOffset),
method(lua::OffsetGroup,getAddress),
method(lua::OffsetGroup,getHexValue),
method(lua::OffsetGroup,getString),
method(lua::OffsetGroup,getGroup),
method(lua::OffsetGroup,getSafeOffset),
method(lua::OffsetGroup,getSafeAddress),
method(lua::OffsetGroup,PrintOffsets),
method(lua::OffsetGroup,getName),
method(lua::OffsetGroup,getFullName),
method(lua::OffsetGroup,getParent),
LUNE_METHODS_END();
// VersionInfo Stuff
static int __lua_getMD5(lua_State *S)
{
lua::state st(S);
std::string output;
bool ret=DFHack::Core::getInstance().vinfo->getMD5(output);
st.push(ret);
st.push(output);
return 2;
}
static int __lua_getPE(lua_State *S)
{
lua::state st(S);
uint32_t output;
bool ret=DFHack::Core::getInstance().vinfo->getPE(output);
st.push(ret);
st.push(output);
return 2;
}
static int __lua_PrintOffsets(lua_State *S)
{
lua::state st(S);
std::string output;
//if(st.is<lua::nil>(1))
output=DFHack::Core::getInstance().vinfo->PrintOffsets();
//else
// output=DFHack::Core::getInstance().vinfo->PrintOffsets(st.as<int>(1)); //TODO add when virtual methods are ok
st.push(output);
return 1;
}
static int __lua_getMood(lua_State *S)
{
lua::state st(S);
std::string output=DFHack::Core::getInstance().vinfo->getMood(st.as<uint32_t>(1));
st.push(output);
return 1;
}
/*static int __lua_getString(lua_State *S)
{
lua::state st(S);
std::string output=DFHack::Core::getInstance().vinfo->getString(st.as<std::string>(1));
st.push(output);
return 1;
}*/
static int __lua_getProfession(lua_State *S)
{
lua::state st(S);
std::string output=DFHack::Core::getInstance().vinfo->getProfession(st.as<uint32_t>(1));
st.push(output);
return 1;
}
static int __lua_getJob(lua_State *S)
{
lua::state st(S);
std::string output=DFHack::Core::getInstance().vinfo->getJob(st.as<uint32_t>(1));
st.push(output);
return 1;
}
static int __lua_getSkill(lua_State *S)
{
lua::state st(S);
std::string output=DFHack::Core::getInstance().vinfo->getSkill(st.as<uint32_t>(1));
st.push(output);
return 1;
}
static int __lua_getTrait(lua_State *S)
{
lua::state st(S);
std::string output=DFHack::Core::getInstance().vinfo->getTrait(st.as<uint32_t>(1),st.as<uint32_t>(2));
st.push(output);
return 1;
}
static int __lua_getTraitName(lua_State *S)
{
lua::state st(S);
std::string output=DFHack::Core::getInstance().vinfo->getTraitName(st.as<uint32_t>(1));
st.push(output);
return 1;
}
static int __lua_getLabor(lua_State *S)
{
lua::state st(S);
std::string output=DFHack::Core::getInstance().vinfo->getLabor(st.as<uint32_t>(1));
st.push(output);
return 1;
}
static int __lua_getAllTraits(lua_State *S)
{
lua::state st(S);
std::vector< std::vector<std::string> > output=DFHack::Core::getInstance().vinfo->getAllTraits();
st.newtable();
for(size_t i=0;i<output.size();i++)
{
st.push(i+1);
st.newtable();
for(size_t j=0;j<output[i].size();j++)
{
st.push(j+1);
st.push(output[i][j]);
st.settable();
}
st.settable();
}
return 1;
}
static int __lua_getAllLabours(lua_State *S)
{
lua::state st(S);
std::map<uint32_t, std::string> output=DFHack::Core::getInstance().vinfo->getAllLabours();
st.newtable();
for(std::map<uint32_t, std::string>::iterator it=output.begin();it!=output.end();it++)
{
st.push(it->first);
st.push(it->second);
st.settable();
}
return 1;
}
static int __lua_getLevelInfo(lua_State *S)
{
lua::state st(S);
DFHack::t_level output=DFHack::Core::getInstance().vinfo->getLevelInfo(st.as<uint32_t>(1));
st.newtable();
st.push(output.level);
st.setfield("level");
st.push(output.name);
st.setfield("name");
st.push(output.xpNxtLvl);
st.setfield("xpNxtLvl");
return 1;
}
static int __lua_getVersion(lua_State *S)
{
lua::state st(S);
std::string output=DFHack::Core::getInstance().vinfo->getVersion();
st.push(output);
return 1;
}
static int __lua_getOS(lua_State *S)
{
lua::state st(S);
DFHack::OSType output=DFHack::Core::getInstance().vinfo->getOS();
st.push(output);
return 1;
}
static int __lua_resolveObjectToClassID(lua_State *S)
{
lua::state st(S);
int32_t ret;
bool output=DFHack::Core::getInstance().vinfo->resolveObjectToClassID(st.as<uint32_t>(1),ret);
st.push(output);
st.push(ret);
return 2;
}
static int __lua_resolveClassnameToClassID(lua_State *S)
{
lua::state st(S);
int32_t ret;
bool output=DFHack::Core::getInstance().vinfo->resolveClassnameToClassID(st.as<std::string>(1),ret);
st.push(output);
st.push(ret);
return 2;
}
static int __lua_resolveClassnameToVPtr(lua_State *S)
{
lua::state st(S);
void * ret;
bool output=DFHack::Core::getInstance().vinfo->resolveClassnameToVPtr(st.as<std::string>(1),ret);
st.push(output);
st.pushlightuserdata(ret);
return 2;
}
static int __lua_resolveClassIDToClassname(lua_State *S)
{
lua::state st(S);
std::string ret;
bool output=DFHack::Core::getInstance().vinfo->resolveClassIDToClassname(st.as<int32_t>(1),ret);
st.push(output);
st.push(ret);
return 2;
}
// OFFSET BASE STUFF (for version info)
static int __lua_getOffset(lua_State *S)
{
lua::state st(S);
int32_t ret=DFHack::Core::getInstance().vinfo->getOffset(st.as<std::string>(1));
st.push(ret);
return 1;
}
static int __lua_getAddress(lua_State *S)
{
lua::state st(S);
uint32_t ret=DFHack::Core::getInstance().vinfo->getAddress(st.as<std::string>(1));
st.push(ret);
return 1;
}
static int __lua_getHexValue(lua_State *S)
{
lua::state st(S);
uint32_t ret=DFHack::Core::getInstance().vinfo->getHexValue(st.as<std::string>(1));
st.push(ret);
return 1;
}
/*static int __lua_getString(lua_State *S) //from offsetbase
{
lua::state st(S);
std::string ret=DFHack::Core::getInstance().vinfo->getString(st.as<std::string>(1));
st.push(ret);
return 1;
}*/
static int __lua_getGroup(lua_State *S)
{
lua::state st(S);
DFHack::OffsetGroup* t= DFHack::Core::getInstance().vinfo->getGroup(st.as<std::string>(2));
st.getglobal("OffsetGroup");
st.getfield("new");
st.pushlightuserdata(t);
st.pcall(1,1);
return 1;
}
static int __lua_getParent(lua_State *S)
{
lua::state st(S);
DFHack::OffsetGroup* t= DFHack::Core::getInstance().vinfo->getParent();
st.getglobal("OffsetGroup");
st.getfield("new");
st.pushlightuserdata(t);
st.pcall(1,1);
return 1;
}
static int __lua_getSafeOffset(lua_State *S)
{
lua::state st(S);
int32_t out;
bool ret=DFHack::Core::getInstance().vinfo->getSafeOffset(st.as<std::string>(1),out);
st.push(ret);
st.push(out);
return 2;
}
static int __lua_getSafeAddress(lua_State *S)
{
lua::state st(S);
uint32_t out;
bool ret=DFHack::Core::getInstance().vinfo->getSafeAddress(st.as<std::string>(1),out);
st.push(ret);
st.push(out);
return 2;
}
//std::string PrintOffsets(int indentation); //overload up there^^^
static int __lua_getName(lua_State *S)
{
lua::state st(S);
std::string ret=DFHack::Core::getInstance().vinfo->getName();
st.push(ret);
return 1;
}
static int __lua_getFullName(lua_State *S)
{
lua::state st(S);
std::string ret=DFHack::Core::getInstance().vinfo->getFullName();
st.push(ret);
return 1;
}
#define ___m(x) {#x,__lua_##x}
const luaL_Reg lua_vinfo_func[]=
{
___m(getMD5),
___m(getPE),
___m(PrintOffsets),
___m(getMood),
// ___m(getString),
___m(getProfession),
___m(getJob),
___m(getSkill),
___m(getTrait),
___m(getTraitName),
___m(getLabor),
___m(getAllTraits),
___m(getAllLabours),
___m(getLevelInfo),
___m(getVersion),
___m(getOS),
___m(resolveObjectToClassID),
___m(resolveClassnameToClassID),
___m(resolveClassnameToVPtr),
___m(resolveClassIDToClassname),
___m(getGroup),
___m(getParent),
___m(getOffset),
___m(getAddress),
___m(getHexValue),
//___m(getString),
___m(getSafeOffset),
___m(getSafeAddress),
___m(getName),
___m(getFullName),
{NULL,NULL}
};
#undef ___m
void lua::RegisterVersionInfo(lua::state &st)
{
st.getglobal("VersionInfo");
if(st.is<lua::nil>())
{
st.pop();
st.newtable();
}
lua::RegFunctionsLocal(st, lua_vinfo_func);
st.setglobal("VersionInfo");
Lune<lua::OffsetGroup>::Register(st);
}