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")
command_result dfusion (Core * c, vector <string> & parameters);
command_result lua_run (Core * c, vector <string> & parameters);
command_result dfusion (color_ostream &out, std::vector <std::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 )
{
@ -43,7 +45,7 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand>
{
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 <PluginCommand>
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 <filename>' to run <filename> 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 <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;
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<Console&>(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 <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();
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 <string> & 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<Core*>(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 <string> & parameters)
command_result dfuse(color_ostream &out, std::vector <std::string> &parameters)
{
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 <std::string> &parameters)
{
lua::state s=lua::glua::Get();
lua::SetConsole(s,out);
s.push();
s.setglobal("INIT");
RunDfusion(out);
return CR_OK;
}

@ -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

@ -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"})

@ -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<DFHack::Console*>(lua_touserdata(st,-1));
DFHack::color_ostream* c=static_cast<DFHack::color_ostream*>(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<string>(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<string>(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<int>(1,1),st.as<int>(1,2));
DFHack::color_ostream* c=GetConsolePtr(st);
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;
}
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<DFHack::Console::color_value>(st.as<int>(-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<bool>(1));
DFHack::color_ostream* c=GetConsolePtr(st);
if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
con->cursor(st.as<bool>(1));
}
return 0;
}
static int lua_Console_msleep(lua_State *S)
{
lua::state st(S);
DFHack::Console* c=GetConsolePtr(st);
c->msleep(st.as<unsigned>(1));
DFHack::color_ostream* c=GetConsolePtr(st);
if(c->is_console())
{
DFHack::Console* con=static_cast<DFHack::Console*>(c);
con->msleep(st.as<unsigned>(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<DFHack::Console*>(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<DFHack::Console*>(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<string>(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<DFHack::Console*>(c);
string ret;
DFHack::CommandHistory hist;
int i=con->lineedit(st.as<string>(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<lua::nil>())
@ -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<lua::nil>())
{
st.pop();
st.newtable();
}
st.pushlightuserdata(&stream);
st.setfield("__pointer");
st.settop(top);
}