summaryrefslogtreecommitdiff
path: root/plugins/Dfusion
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-03-31 15:40:54 +0400
committerAlexander Gavrilov2012-03-31 15:40:54 +0400
commit0f41608ed4b808205dd8734bbc52de64f6501c98 (patch)
tree63229bc9c5cf3c37be28b062bae951bcfae8fb33 /plugins/Dfusion
parent9384f0c842f817bf810b34e44bfa3be913e7e3fc (diff)
downloaddfhack-0f41608ed4b808205dd8734bbc52de64f6501c98.tar.gz
dfhack-0f41608ed4b808205dd8734bbc52de64f6501c98.tar.bz2
dfhack-0f41608ed4b808205dd8734bbc52de64f6501c98.tar.xz
Pull console output support and REPL out of dfusion into core lib.
Diffstat (limited to 'plugins/Dfusion')
-rw-r--r--plugins/Dfusion/dfusion.cpp104
-rw-r--r--plugins/Dfusion/include/lua_Console.h2
-rw-r--r--plugins/Dfusion/src/lua_Console.cpp73
3 files changed, 11 insertions, 168 deletions
diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp
index 9a277fb4..0886d2a3 100644
--- a/plugins/Dfusion/dfusion.cpp
+++ b/plugins/Dfusion/dfusion.cpp
@@ -21,6 +21,7 @@
#include "lua_FunctionCall.h"
#include "lua_Offsets.h"
#include "DataDefs.h"
+#include "LuaTools.h"
using std::vector;
using std::string;
@@ -42,11 +43,12 @@ DFhackCExport const char * plugin_name ( void )
return "dfusion";
}
-DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand> &commands)
+DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
{
lua::state st=lua::glua::Get();
+
//maybe remake it to run automaticaly
- DFHack::AttachDFGlobals(st);
+ Lua::Open(out, st);
lua::RegisterConsole(st);
lua::RegisterProcess(st);
@@ -64,11 +66,6 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand>
st.setglobal("WINDOWS");
#endif
- st.getglobal("Console");
- st.getfield("println");
- st.setglobal("print");
- st.pop();
-
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));
@@ -113,87 +110,6 @@ DFhackCExport command_result plugin_onupdate_DISABLED ( Core * c )
mymutex->unlock();
return CR_OK;
}
-
-void InterpreterLoop(color_ostream &out)
-{
-
- DFHack::CommandHistory hist;
- lua::state s=lua::glua::Get();
- string curline;
- out.print("Type quit to exit interactive lua interpreter.\n"
- "Shortcuts:\n"
- " '= foo' => '_1,_2,... = foo'\n"
- " '! foo' => 'print(foo)'\n"
- "Both assign the first result to '_'\n");
- assert(out.is_console());
- Console &con = static_cast<Console&>(out);
- int vcnt = 1;
-
- s.settop(0);
-
- for (;;) {
- con.lineedit("[lua]# ",curline,hist);
-
- if (curline.empty())
- continue;
- if (curline == "quit")
- break;
-
- hist.add(curline);
-
- try
- {
- char pfix = curline[0];
-
- if (pfix == '=' || pfix == '!')
- {
- curline = "return " + curline.substr(1);
-
- s.loadstring(curline);
- s.pcall(0, LUA_MULTRET);
- int numret = s.gettop();
-
- if (numret >= 1)
- {
- s.pushvalue(1);
- s.setglobal("_");
-
- if (pfix == '!')
- {
- s.getglobal("print");
- s.insert(1);
- s.pcall(numret,0);
- numret = 0;
- }
- }
-
- for (int i = 1; i <= numret; i++)
- {
- std::string name = stl_sprintf("_%d", vcnt++);
- s.pushvalue(i);
- s.setglobal(name);
-
- con.print("%s = ", name.c_str());
- s.getglobal("print");
- s.pushvalue(i);
- s.pcall(1,0);
- }
- }
- else
- {
- s.loadstring(curline);
- s.pcall();
- }
- }
- catch(lua::exception &e)
- {
- con.printerr("Error:%s\n",e.what());
- con.printerr("%s\n",lua::DebugDump(lua::glua::Get()).c_str());
- }
-
- s.settop(0);
- }
-}
command_result lua_run_file (color_ostream &out, std::vector <std::string> &parameters)
{
if(parameters.size()==0)
@@ -207,14 +123,14 @@ command_result lua_run (color_ostream &out, std::vector <std::string> &parameter
{
mymutex->lock();
lua::state s=lua::glua::Get();
- lua::SetConsole(s,out);
+
if(parameters.size()>0)
{
try{
s.loadfile(parameters[0]); //load file
for(size_t i=1;i<parameters.size();i++)
s.push(parameters[i]);
- s.pcall(parameters.size()-1,0);// run it
+ Lua::SafeCall(out, s, parameters.size()-1,0);// run it
}
catch(lua::exception &e)
{
@@ -224,7 +140,7 @@ command_result lua_run (color_ostream &out, std::vector <std::string> &parameter
}
else
{
- InterpreterLoop(out);
+ Lua::InterpreterLoop(out, s);
}
s.settop(0);// clean up
mymutex->unlock();
@@ -235,12 +151,10 @@ void RunDfusion(color_ostream &out, std::vector <std::string> &parameters)
mymutex->lock();
lua::state s=lua::glua::Get();
try{
- s.getglobal("err");
- int errpos=s.gettop();
s.loadfile("dfusion/init.lua"); //load script
for(size_t i=0;i<parameters.size();i++)
s.push(parameters[i]);
- s.pcall(parameters.size(),0,errpos);// run it
+ Lua::SafeCall(out, s, parameters.size(),0);
}
catch(lua::exception &e)
{
@@ -253,7 +167,6 @@ void RunDfusion(color_ostream &out, std::vector <std::string> &parameters)
command_result dfuse(color_ostream &out, std::vector <std::string> &parameters)
{
lua::state s=lua::glua::Get();
- lua::SetConsole(s,out);
s.push(1);
s.setglobal("INIT");
RunDfusion(out,parameters);
@@ -262,7 +175,6 @@ command_result dfuse(color_ostream &out, std::vector <std::string> &parameters)
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,parameters);
diff --git a/plugins/Dfusion/include/lua_Console.h b/plugins/Dfusion/include/lua_Console.h
index 227effa9..c6ac5011 100644
--- a/plugins/Dfusion/include/lua_Console.h
+++ b/plugins/Dfusion/include/lua_Console.h
@@ -7,7 +7,7 @@ namespace lua
{
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/src/lua_Console.cpp b/plugins/Dfusion/src/lua_Console.cpp
index 2a8222af..881ba5d2 100644
--- a/plugins/Dfusion/src/lua_Console.cpp
+++ b/plugins/Dfusion/src/lua_Console.cpp
@@ -1,64 +1,12 @@
#include "lua_Console.h"
+#include "LuaTools.h"
#include <sstream>
//TODO error management. Using lua error? or something other?
static DFHack::color_ostream* GetConsolePtr(lua::state &st)
{
- int t=st.gettop();
- st.getglobal("Console");
- st.getfield("__pointer");
- DFHack::color_ostream* c=static_cast<DFHack::color_ostream*>(lua_touserdata(st,-1));
- st.settop(t);
- return c;
-}
-
-static std::string lua_print_fmt(lua_State *L)
-{
- /* Copied from lua source to fully replicate builtin print */
- int n = lua_gettop(L); /* number of arguments */
- lua_getglobal(L, "tostring");
-
- std::stringstream ss;
-
- for (int i=1; i<=n; i++) {
- lua_pushvalue(L, -1); /* function to be called */
- lua_pushvalue(L, i); /* value to print */
- lua_call(L, 1, 1);
- const char *s = lua_tostring(L, -1); /* get result */
- if (s == NULL)
- luaL_error(L, "tostring must return a string to print");
- if (i>1)
- ss << '\t';
- ss << s;
- lua_pop(L, 1); /* pop result */
- }
-
- return ss.str();
-}
-
-static int lua_Console_print(lua_State *S)
-{
- lua::state st(S);
- DFHack::color_ostream* c=GetConsolePtr(st);
- c->print("%s", lua_print_fmt(S).c_str());
- return 0;
-}
-
-static int lua_Console_println(lua_State *S)
-{
- lua::state st(S);
- DFHack::color_ostream* c=GetConsolePtr(st);
- c->print("%s\n", lua_print_fmt(S).c_str());
- return 0;
-}
-
-static int lua_Console_printerr(lua_State *S)
-{
- lua::state st(S);
- DFHack::color_ostream* c=GetConsolePtr(st);
- c->printerr("%s", lua_print_fmt(S).c_str());
- return 0;
+ return DFHack::Lua::GetOutput(st);
}
static int lua_Console_clear(lua_State *S)
@@ -156,9 +104,6 @@ static int lua_Console_lineedit(lua_State *S)
}
const luaL_Reg lua_console_func[]=
{
- {"print",lua_Console_print},
- {"println",lua_Console_println},
- {"printerr",lua_Console_printerr},
{"clear",lua_Console_clear},
{"gotoxy",lua_Console_gotoxy},
{"color",lua_Console_color},
@@ -183,17 +128,3 @@ void lua::RegisterConsole(lua::state &st)
//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);
-} \ No newline at end of file