Dfusion rebuild start (lua script side plugins)

develop
Warmist 2012-10-07 20:45:14 +03:00
parent 923ea3f4b0
commit 49476818c4
8 changed files with 57 additions and 125 deletions

@ -1 +1 @@
Subproject commit a914f3b7558335d53c0ac93f6e7267906a33cd29 Subproject commit 8a78bfa218817765b0a80431e0cf25435ffb2179

@ -1,5 +1,5 @@
include_directories(include) include_directories(include)
include_directories("${dfhack_SOURCE_DIR}/library/depends/tthread")
FILE(GLOB DFUSION_CPPS src/*.c*) FILE(GLOB DFUSION_CPPS src/*.c*)
set( set(
DFUSION_CPPS_ALL DFUSION_CPPS_ALL

@ -6,16 +6,11 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include "tinythread.h"
#include "luamain.h" #include "luamain.h"
#include "lua_Process.h" #include "lua_Process.h"
#include "lua_Hexsearch.h" #include "lua_Hexsearch.h"
#include "lua_Misc.h" #include "lua_Misc.h"
#include "DataDefs.h" #include "DataDefs.h"
#include "LuaTools.h" #include "LuaTools.h"
@ -23,112 +18,51 @@ using std::vector;
using std::string; using std::string;
using namespace DFHack; using namespace DFHack;
static tthread::mutex* mymutex=0;
static tthread::thread* thread_dfusion=0;
uint64_t timeLast=0;
DFHACK_PLUGIN("dfusion")
command_result dfusion (color_ostream &out, std::vector <std::string> &parameters); DFHACK_PLUGIN("dfusion")
command_result dfuse (color_ostream &out, std::vector <std::string> &parameters);
DFhackCExport const char * plugin_name ( void ) static int loadObjectFile(lua_State* L)
{ {
return "dfusion"; std::string path;
path=luaL_checkstring(L,1);
OutFile::File f(path);
lua_newtable(L);
int table_pos=lua_gettop(L);
size_t size=f.GetTextSize();
Lua::Push(L,size);
lua_setfield(L,table_pos,"data_size");
char* buf=new char[size];
f.GetText(buf);
//Lua::PushDFObject(L,DFHack::,buf);
//Lua::Push(L,buf);
lua_pushlightuserdata(L,buf);
lua_setfield(L,table_pos,"data");
OutFile::vSymbol& symbols=f.GetSymbols();
lua_newtable(L);
for(size_t i=0;i<symbols.size();i++)
{
Lua::Push(L,i);
lua_newtable(L);
Lua::Push(L,symbols[i].name);
lua_setfield(L,-2,"name");
Lua::Push(L,symbols[i].pos);
lua_setfield(L,-2,"pos");
lua_settable(L,-3);
}
lua_setfield(L,table_pos,"symbols");
return 1;
} }
DFHACK_PLUGIN_LUA_COMMANDS {
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands) DFHACK_LUA_COMMAND(loadObjectFile),
DFHACK_LUA_END
};
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{ {
lua::state st=lua::glua::Get();
//maybe remake it to run automatically
Lua::Open(out, st);
lua::RegisterProcess(st);
lua::RegisterHexsearch(st);
lua::RegisterMisc(st);
#ifdef LINUX_BUILD
st.push(1);
st.setglobal("LINUX");
#else
st.push(1);
st.setglobal("WINDOWS");
#endif
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));
mymutex=new tthread::mutex;
return CR_OK; return CR_OK;
} }
DFhackCExport command_result plugin_shutdown ( Core * c )
{
// shutdown stuff
if(thread_dfusion)
delete thread_dfusion;
delete mymutex;
return CR_OK;
}
DFhackCExport command_result plugin_onupdate_DISABLED ( Core * c )
{
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");
if(s.is<lua::function>())
{
try{
s.pcall();
}
catch(lua::exception &e)
{
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);
mymutex->unlock();
return CR_OK;
}
void RunDfusion(color_ostream &out, std::vector <std::string> &parameters)
{
mymutex->lock();
lua::state s=lua::glua::Get();
try{
s.loadfile("dfusion/init.lua"); //load script
for(size_t i=0;i<parameters.size();i++)
s.push(parameters[i]);
Lua::SafeCall(out, s, parameters.size(),0);
}
catch(lua::exception &e)
{
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 dfuse(color_ostream &out, std::vector <std::string> &parameters)
{
lua::state s=lua::glua::Get();
s.push(1);
s.setglobal("INIT");
RunDfusion(out,parameters);
return CR_OK;
}
command_result dfusion (color_ostream &out, std::vector <std::string> &parameters)
{
lua::state s=lua::glua::Get();
s.push();
s.setglobal("INIT");
RunDfusion(out,parameters);
return CR_OK;
}

@ -541,8 +541,4 @@ function Allocate(size)
curptr=curptr+size curptr=curptr+size
engine.poked(ptr,curptr) engine.poked(ptr,curptr)
return curptr-size+ptr return curptr-size+ptr
end end
dofile("dfusion/patterns.lua")
dofile("dfusion/patterns2.lua")
dofile("dfusion/itempatterns.lua")
dofile("dfusion/buildingpatterns.lua")

@ -1,7 +1,7 @@
.intel_syntax .intel_syntax
mov eax , [esp+0x1C] mov eax , [esp+0x1C] # loop counter
caste: mark_caste:
movsx ecx, word ptr[eax*2+0xdeadbeef] movsx ecx, word ptr[eax*2+0xdeadbeef]
race: mark_race:
movzx eax,word ptr [eax*2+0xDEADBEEF] movzx eax,word ptr [eax*2+0xDEADBEEF]
ret ret

@ -1,19 +1,21 @@
#include "OutFile.h" #include "OutFile.h"
#include <stdexcept>
using namespace OutFile; using namespace OutFile;
File::File(std::string path) File::File(std::string path)
{ {
//mystream.exceptions ( std::fstream::eofbit | std::fstream::failbit | std::fstream::badbit ); //mystream.exceptions ( std::fstream::eofbit | std::fstream::failbit | std::fstream::badbit );
mystream.open(path.c_str(),std::fstream::binary|std::ios::in|std::ios::out); mystream.open(path.c_str(),std::fstream::binary|std::ios::in|std::ios::out);
mystream.read((char*)&myhead,sizeof(myhead));
for(unsigned i=0;i<myhead.sectioncount;i++)
{
Section x;
mystream.read((char*)&x,sizeof(Section));
sections[x.name]=x;
}
if(mystream) if(mystream)
{ {
mystream.read((char*)&myhead,sizeof(myhead));
for(unsigned i=0;i<myhead.sectioncount;i++)
{
Section x;
mystream.read((char*)&x,sizeof(Section));
sections[x.name]=x;
}
//std::cout<<"Sizeof:"<<sizeof(Section)<<"\n"; //std::cout<<"Sizeof:"<<sizeof(Section)<<"\n";
/*myhead.PrintData(); /*myhead.PrintData();
for(auto it=sections.begin();it!=sections.end();it++) for(auto it=sections.begin();it!=sections.end();it++)
@ -24,7 +26,7 @@ File::File(std::string path)
} }
else else
{ {
std::cout<<"Error opening file!"<<std::endl; throw std::runtime_error("Error opening file!");
} }
} }
Section &File::GetSection(std::string name) Section &File::GetSection(std::string name)