Updated to new dfhack standards.

develop
Warmist 2012-03-11 14:33:08 +02:00
parent 792e48fb76
commit 237d7b433a
4 changed files with 119 additions and 67 deletions

@ -31,8 +31,10 @@ uint64_t timeLast=0;
DFHACK_PLUGIN("dfusion") DFHACK_PLUGIN("dfusion")
command_result dfusion (Core * c, vector <string> & parameters); command_result dfusion (color_ostream &out, std::vector <std::string> &parameters);
command_result lua_run (Core * c, vector <string> & parameters); command_result dfuse (color_ostream &out, std::vector <std::string> &parameters);
command_result lua_run (color_ostream &out, std::vector <std::string> &parameters);
command_result lua_run_file (color_ostream &out, std::vector <std::string> &parameters);
DFhackCExport const char * plugin_name ( void ) DFhackCExport const char * plugin_name ( void )
{ {
@ -43,7 +45,7 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand>
{ {
lua::state st=lua::glua::Get(); lua::state st=lua::glua::Get();
//maybe remake it to run automaticaly //maybe remake it to run automaticaly
lua::RegisterConsole(st,&c->con); lua::RegisterConsole(st);
lua::RegisterProcess(st,c->p); lua::RegisterProcess(st,c->p);
lua::RegisterHexsearch(st); lua::RegisterHexsearch(st);
lua::RegisterMisc(st); lua::RegisterMisc(st);
@ -59,9 +61,10 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand>
st.setglobal("WINDOWS"); st.setglobal("WINDOWS");
#endif #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("dfusion","Run dfusion system (interactive i.e. can input further commands).",dfusion,true));
commands.push_back(PluginCommand("lua", "Run interactive interpreter. Use 'lua <filename>' to run <filename> instead.",lua_run)); commands.push_back(PluginCommand("dfuse","Init dfusion system (not interactive).",dfuse,false));
commands.push_back(PluginCommand("lua", "Run interactive interpreter. Use 'lua <filename>' to run <filename> instead.",lua_run,true));
commands.push_back(PluginCommand("runlua", "Run non-interactive interpreter. Use 'runlua <filename>' to run <filename>.",lua_run_file,false));
mymutex=new tthread::mutex; mymutex=new tthread::mutex;
return CR_OK; return CR_OK;
} }
@ -93,9 +96,9 @@ DFhackCExport command_result plugin_onupdate_DISABLED ( Core * c )
} }
catch(lua::exception &e) catch(lua::exception &e)
{ {
c->con.printerr("Error OnTick:%s\n",e.what()); c->getConsole().printerr("Error OnTick:%s\n",e.what());
c->con.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); c->getConsole().printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str());
c->con.msleep(1000); c->getConsole().msleep(1000);
} }
} }
s.settop(0); s.settop(0);
@ -103,13 +106,15 @@ DFhackCExport command_result plugin_onupdate_DISABLED ( Core * c )
return CR_OK; return CR_OK;
} }
void InterpreterLoop(Core* c) void InterpreterLoop(color_ostream &out)
{ {
Console &con=c->con;
DFHack::CommandHistory hist; DFHack::CommandHistory hist;
lua::state s=lua::glua::Get(); lua::state s=lua::glua::Get();
string curline; 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<Console&>(out);
con.lineedit(">>",curline,hist); con.lineedit(">>",curline,hist);
while (curline!="quit") { while (curline!="quit") {
@ -122,18 +127,27 @@ void InterpreterLoop(Core* c)
catch(lua::exception &e) catch(lua::exception &e)
{ {
con.printerr("Error:%s\n",e.what()); 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); s.settop(0);
} }
con.lineedit(">>",curline,hist); con.lineedit(">>",curline,hist);
} }
s.settop(0); s.settop(0);
} }
command_result lua_run (Core * c, vector <string> & parameters) command_result lua_run_file (color_ostream &out, std::vector <std::string> &parameters)
{
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 <std::string> &parameters)
{ {
Console &con=c->con;
mymutex->lock(); mymutex->lock();
lua::state s=lua::glua::Get(); lua::state s=lua::glua::Get();
lua::SetConsole(s,out);
if(parameters.size()>0) if(parameters.size()>0)
{ {
try{ try{
@ -142,23 +156,21 @@ command_result lua_run (Core * c, vector <string> & parameters)
} }
catch(lua::exception &e) catch(lua::exception &e)
{ {
con.printerr("Error:%s\n",e.what()); out.printerr("Error:%s\n",e.what());
c->con.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); out.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str());
} }
} }
else else
{ {
InterpreterLoop(c); InterpreterLoop(out);
} }
s.settop(0);// clean up s.settop(0);// clean up
mymutex->unlock(); mymutex->unlock();
return CR_OK; return CR_OK;
} }
void RunDfusion(void *p) void RunDfusion(color_ostream &out)
{ {
Console &con=static_cast<Core*>(p)->con;
mymutex->lock(); mymutex->lock();
lua::state s=lua::glua::Get(); lua::state s=lua::glua::Get();
try{ try{
s.getglobal("err"); s.getglobal("err");
@ -169,26 +181,27 @@ void RunDfusion(void *p)
} }
catch(lua::exception &e) catch(lua::exception &e)
{ {
con.printerr("Error:%s\n",e.what()); out.printerr("Error:%s\n",e.what());
con.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str()); out.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str());
} }
s.settop(0);// clean up s.settop(0);// clean up
mymutex->unlock(); mymutex->unlock();
} }
command_result dfusion (Core * c, vector <string> & parameters) command_result dfuse(color_ostream &out, std::vector <std::string> &parameters)
{
if(parameters[0]=="init")
{ {
lua::state s=lua::glua::Get(); lua::state s=lua::glua::Get();
lua::SetConsole(s,out);
s.push(1); s.push(1);
s.setglobal("INIT"); s.setglobal("INIT");
RunDfusion(out);
return CR_OK;
} }
else command_result dfusion (color_ostream &out, std::vector <std::string> &parameters)
{ {
lua::state s=lua::glua::Get(); lua::state s=lua::glua::Get();
lua::SetConsole(s,out);
s.push(); s.push();
s.setglobal("INIT"); s.setglobal("INIT");
} RunDfusion(out);
RunDfusion(c);
return CR_OK; return CR_OK;
} }

@ -6,8 +6,8 @@
namespace lua 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 #endif

@ -44,6 +44,8 @@ function mainmenu(t1)
end end
dofile("dfusion/common.lua") dofile("dfusion/common.lua")
dofile("dfusion/utils.lua") dofile("dfusion/utils.lua")
types=nil
dofile("dfusion/xml_struct.lua")
unlockDF() unlockDF()
plugins={} plugins={}
table.insert(plugins,{"simple_embark","A simple embark dwarf count editor"}) table.insert(plugins,{"simple_embark","A simple embark dwarf count editor"})

@ -1,11 +1,11 @@
#include "lua_Console.h" #include "lua_Console.h"
//TODO error management. Using lua error? or something other? //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(); int t=st.gettop();
st.getglobal("Console"); st.getglobal("Console");
st.getfield("__pointer"); st.getfield("__pointer");
DFHack::Console* c=static_cast<DFHack::Console*>(lua_touserdata(st,-1)); DFHack::color_ostream* c=static_cast<DFHack::color_ostream*>(lua_touserdata(st,-1));
st.settop(t); st.settop(t);
return c; return c;
} }
@ -13,7 +13,7 @@ static int lua_Console_print(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
int t=st.gettop(); int t=st.gettop();
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
c->print("%s",st.as<string>(t).c_str()); c->print("%s",st.as<string>(t).c_str());
return 0; return 0;
} }
@ -22,7 +22,7 @@ static int lua_Console_printerr(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
int t=st.gettop(); int t=st.gettop();
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
c->printerr("%s",st.as<string>(t).c_str()); c->printerr("%s",st.as<string>(t).c_str());
return 0; return 0;
} }
@ -30,70 +30,96 @@ static int lua_Console_printerr(lua_State *S)
static int lua_Console_clear(lua_State *S) static int lua_Console_clear(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
c->clear(); c->clear();
return 0; return 0;
} }
static int lua_Console_gotoxy(lua_State *S) static int lua_Console_gotoxy(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
c->gotoxy(st.as<int>(1,1),st.as<int>(1,2)); if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
con->gotoxy(st.as<int>(1,1),st.as<int>(1,2));
}
return 0; return 0;
} }
static int lua_Console_color(lua_State *S) static int lua_Console_color(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
c->color( static_cast<DFHack::Console::color_value>(st.as<int>(-1,1)) ); c->color( static_cast<DFHack::Console::color_value>(st.as<int>(-1,1)) );
return 0; return 0;
} }
static int lua_Console_reset_color(lua_State *S) static int lua_Console_reset_color(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
c->reset_color(); c->reset_color();
return 0; return 0;
} }
static int lua_Console_cursor(lua_State *S) static int lua_Console_cursor(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
c->cursor(st.as<bool>(1)); if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
con->cursor(st.as<bool>(1));
}
return 0; return 0;
} }
static int lua_Console_msleep(lua_State *S) static int lua_Console_msleep(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
c->msleep(st.as<unsigned>(1)); if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
con->msleep(st.as<unsigned>(1));
}
return 0; return 0;
} }
static int lua_Console_get_columns(lua_State *S) static int lua_Console_get_columns(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
st.push(c->get_columns()); if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
st.push(con->get_columns());
}
return 1; return 1;
} }
static int lua_Console_get_rows(lua_State *S) static int lua_Console_get_rows(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
st.push(c->get_rows()); if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
st.push(con->get_rows());
}
return 1; return 1;
} }
static int lua_Console_lineedit(lua_State *S) static int lua_Console_lineedit(lua_State *S)
{ {
lua::state st(S); lua::state st(S);
DFHack::Console* c=GetConsolePtr(st); DFHack::color_ostream* c=GetConsolePtr(st);
if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
string ret; string ret;
DFHack::CommandHistory hist; DFHack::CommandHistory hist;
int i=c->lineedit(st.as<string>(1),ret,hist); int i=con->lineedit(st.as<string>(1),ret,hist);
st.push(ret); st.push(ret);
st.push(i); st.push(i);
return 2;// dunno if len is needed... return 2;// dunno if len is needed...
} }
else
return 0;
}
const luaL_Reg lua_console_func[]= const luaL_Reg lua_console_func[]=
{ {
{"print",lua_Console_print}, {"print",lua_Console_print},
@ -109,7 +135,7 @@ const luaL_Reg lua_console_func[]=
{"lineedit",lua_Console_lineedit}, {"lineedit",lua_Console_lineedit},
{NULL,NULL} {NULL,NULL}
}; };
void lua::RegisterConsole(lua::state &st, DFHack::Console *c) void lua::RegisterConsole(lua::state &st)
{ {
st.getglobal("Console"); st.getglobal("Console");
if(st.is<lua::nil>()) if(st.is<lua::nil>())
@ -118,10 +144,21 @@ void lua::RegisterConsole(lua::state &st, DFHack::Console *c)
st.newtable(); st.newtable();
} }
st.pushlightuserdata(c);
st.setfield("__pointer");
lua::RegFunctionsLocal(st, lua_console_func); lua::RegFunctionsLocal(st, lua_console_func);
//TODO add color consts //TODO add color consts
st.setglobal("Console"); st.setglobal("Console");
} }
void lua::SetConsole(lua::state &st,DFHack::color_ostream& stream)
{
int top=st.gettop();
st.getglobal("Console");
if(st.is<lua::nil>())
{
st.pop();
st.newtable();
}
st.pushlightuserdata(&stream);
st.setfield("__pointer");
st.settop(top);
}