diff --git a/plugins/Dfusion/dfusion.cpp b/plugins/Dfusion/dfusion.cpp index 4e31add47..33d2503a7 100644 --- a/plugins/Dfusion/dfusion.cpp +++ b/plugins/Dfusion/dfusion.cpp @@ -6,15 +6,14 @@ #include - - -#include +#include "luamain.h" +#include "lua_Console.h" using std::vector; using std::string; using namespace DFHack; -static SDL::Mutex *mymutex=0; +static SDL::Mutex* mymutex=0; DFhackCExport command_result dfusion (Core * c, vector & parameters); @@ -27,7 +26,10 @@ DFhackCExport const char * plugin_name ( void ) DFhackCExport command_result plugin_init ( Core * c, std::vector &commands) { commands.clear(); - commands.push_back(PluginCommand("DFusion","Init dfusion system.",dfusion)); + //maybe remake it to run automaticaly + lua::RegisterConsole(lua::glua::Get(),&c->con); + + commands.push_back(PluginCommand("dfusion","Init dfusion system.",dfusion)); mymutex=SDL_CreateMutex(); return CR_OK; } @@ -41,7 +43,7 @@ DFhackCExport command_result plugin_shutdown ( Core * c ) DFhackCExport command_result plugin_onupdate ( Core * c ) { - /*if(timering == true) + /*if(timering == true) //TODO maybe reuse this to make it run less often. { uint64_t time2 = GetTimeMs64(); uint64_t delta = time2-timeLast; @@ -49,7 +51,7 @@ DFhackCExport command_result plugin_onupdate ( Core * c ) c->con.print("Time delta = %d ms\n", delta); } return CR_OK;*/ - SDL_mutexP(mymutex); //TODO: make lua thread safe (somehow)... + SDL_mutexP(mymutex); lua::state s=lua::glua::Get(); s.getglobal("OnTick"); if(s.is()) @@ -67,16 +69,18 @@ DFhackCExport command_result dfusion (Core * c, vector & parameters) // do stuff Console &con=c->con; SDL_mutexP(mymutex); + lua::state s=lua::glua::Get(); + try{ - lua::glua::Get().loadfile("dfusion/init.lua"); //load script - lua::glua::Get().pcall(0,0);// run it + s.loadfile("dfusion/init.lua"); //load script + s.pcall(0,0);// run it } catch(lua::exception &e) { con.printerr("Error:%s\n",e.what()); } - lua::glua::Get().settop(0);// clean up + s.settop(0);// clean up SDL_mutexV(mymutex); return CR_OK; } diff --git a/plugins/Dfusion/include/lua_Console.h b/plugins/Dfusion/include/lua_Console.h new file mode 100644 index 000000000..a6430dcbf --- /dev/null +++ b/plugins/Dfusion/include/lua_Console.h @@ -0,0 +1,13 @@ +#ifndef LUA_CONSOLE_H +#define LUA_CONSOLE_H +#include +#include "luamain.h" + +namespace lua +{ + +void RegisterConsole(lua::state &st, DFHack::Console *c); + +} + +#endif \ No newline at end of file diff --git a/plugins/Dfusion/include/luamain.h b/plugins/Dfusion/include/luamain.h index ff4a9d138..aee073729 100644 --- a/plugins/Dfusion/include/luamain.h +++ b/plugins/Dfusion/include/luamain.h @@ -1,5 +1,5 @@ -#ifndef LUASTUFF_H -#define LUASTUFF_H +#ifndef LUAMAIN_H +#define LUAMAIN_H #include using std::string; @@ -37,4 +37,4 @@ namespace lua -#endif // LUASTUFF_H +#endif // LUAMAIN_H diff --git a/plugins/Dfusion/src/lua_Console.cpp b/plugins/Dfusion/src/lua_Console.cpp new file mode 100644 index 000000000..f45fa8d56 --- /dev/null +++ b/plugins/Dfusion/src/lua_Console.cpp @@ -0,0 +1,137 @@ +#include "lua_Console.h" +//TODO error management. Using lua error? or something other? +static DFHack::Console* GetConsolePtr(lua::state &st) +{ + int t=st.gettop(); + st.getglobal("Console"); + st.getfield("__pointer"); + DFHack::Console* c=static_cast(lua_touserdata(st,-1)); + st.settop(t); + return c; +} +static int lua_Console_print(lua_State *S) +{ + lua::state st(S); + int t=st.gettop(); + DFHack::Console* c=GetConsolePtr(st); + c->print("%s",st.as(t).c_str()); + return 0; +} + +static int lua_Console_printerr(lua_State *S) +{ + lua::state st(S); + int t=st.gettop(); + DFHack::Console* c=GetConsolePtr(st); + c->printerr("%s",st.as(t).c_str()); + return 0; +} + +static int lua_Console_clear(lua_State *S) +{ + lua::state st(S); + DFHack::Console* 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(1,1),st.as(1,2)); + return 0; +} +static int lua_Console_color(lua_State *S) +{ + lua::state st(S); + DFHack::Console* c=GetConsolePtr(st); + c->color( static_cast(st.as(-1,1)) ); + return 0; +} +static int lua_Console_reset_color(lua_State *S) +{ + lua::state st(S); + DFHack::Console* 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(1)); + return 0; +} +static int lua_Console_msleep(lua_State *S) +{ + lua::state st(S); + DFHack::Console* c=GetConsolePtr(st); + c->msleep(st.as(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()); + 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()); + return 1; +} +static int lua_Console_lineedit(lua_State *S) +{ + lua::state st(S); + DFHack::Console* c=GetConsolePtr(st); + string ret; + int i=c->lineedit(st.as(1),ret); + st.push(ret); + st.push(i); + return 2;// dunno if len is needed... +} +static int lua_Console_history_add(lua_State *S) +{ + lua::state st(S); + DFHack::Console* c=GetConsolePtr(st); + c->history_add(st.as(1)); + return 0; +} +/*static int lua_Console_history_clear(lua_State *S) //TODO someday add this +{ + lua::state st(S); + DFHack::Console* c=GetConsolePtr(st); + c->history_clear(); + return 0; +}*/ +const luaL_Reg lua_console_func[]= +{ + {"print",lua_Console_print}, + {"printerr",lua_Console_printerr}, + {"clear",lua_Console_clear}, + {"gotoxy",lua_Console_gotoxy}, + {"color",lua_Console_color}, + {"reset_color",lua_Console_reset_color}, + {"cursor",lua_Console_cursor}, + {"msleep",lua_Console_msleep}, + {"get_columns",lua_Console_get_columns}, + {"get_rows",lua_Console_get_rows}, + {"lineedit",lua_Console_lineedit}, + {"history_add",lua_Console_history_add}, + //{"history_clear",lua_Console_history_clear}, + {NULL,NULL} +}; +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"); +}