diff options
| author | Alexander Gavrilov | 2012-04-05 19:55:59 +0400 |
|---|---|---|
| committer | Alexander Gavrilov | 2012-04-05 19:55:59 +0400 |
| commit | 9eed9f0d246f44a51266a05e0107ea22fea54e73 (patch) | |
| tree | 72eb99e5ca85311b7a486f8e4036b9dd703ae3d4 /library/LuaTools.cpp | |
| parent | 28a741082f8b0981806b8a63589279627bd8e39e (diff) | |
| download | dfhack-9eed9f0d246f44a51266a05e0107ea22fea54e73.tar.gz dfhack-9eed9f0d246f44a51266a05e0107ea22fea54e73.tar.bz2 dfhack-9eed9f0d246f44a51266a05e0107ea22fea54e73.tar.xz | |
Wrap a few utility functions defined on the c++ side for lua.
Diffstat (limited to 'library/LuaTools.cpp')
| -rw-r--r-- | library/LuaTools.cpp | 48 |
1 files changed, 47 insertions, 1 deletions
diff --git a/library/LuaTools.cpp b/library/LuaTools.cpp index 16c60457..5afd1156 100644 --- a/library/LuaTools.cpp +++ b/library/LuaTools.cpp @@ -39,6 +39,7 @@ distribution. #include "modules/World.h" #include "modules/Gui.h" +#include "modules/Job.h" #include "LuaWrapper.h" #include "LuaTools.h" @@ -46,6 +47,10 @@ distribution. #include "MiscUtils.h" #include "df/job.h" +#include "df/job_item.h" +#include "df/building.h" +#include "df/unit.h" +#include "df/item.h" #include <lua.h> #include <lauxlib.h> @@ -74,6 +79,13 @@ color_ostream *DFHack::Lua::GetOutput(lua_State *L) return rv; } +df::cur_lua_ostream_argument::cur_lua_ostream_argument(lua_State *state) +{ + out = DFHack::Lua::GetOutput(state); + if (!out) + LuaWrapper::field_error(state, UPVAL_METHOD_NAME, "no output stream", "invoke"); +} + static void set_dfhack_output(lua_State *L, color_ostream *p) { lua_pushlightuserdata(L, p); @@ -844,6 +856,39 @@ static void OpenPersistent(lua_State *state) lua_pop(state, 1); } +static void OpenModule(lua_State *state, const char *mname, const LuaWrapper::FunctionReg *reg) +{ + luaL_getsubtable(state, lua_gettop(state), mname); + LuaWrapper::SetFunctionWrappers(state, reg); + lua_pop(state, 1); +} + +#define WRAPM(module, function) { #function, df::wrap_function(&module::function) } +#define WRAP(function) { #function, df::wrap_function(&function) } +#define WRAPN(name, function) { #name, df::wrap_function(&function) } + +static const LuaWrapper::FunctionReg dfhack_gui_module[] = { + WRAPM(Gui, getSelectedWorkshopJob), + WRAPM(Gui, getSelectedJob), + WRAPM(Gui, getSelectedUnit), + WRAPM(Gui, getSelectedItem), + WRAPM(Gui, showAnnouncement), + WRAPM(Gui, showPopupAnnouncement), + { NULL, NULL } +}; + +static bool jobEqual(df::job *job1, df::job *job2) { return *job1 == *job2; } +static bool jobItemEqual(df::job_item *job1, df::job_item *job2) { return *job1 == *job2; } + +static const LuaWrapper::FunctionReg dfhack_job_module[] = { + WRAP(cloneJobStruct), + WRAP(printJobDetails), + WRAP(getJobHolder), + WRAPN(is_equal, jobEqual), + WRAPN(is_item_equal, jobItemEqual), + { NULL, NULL } +}; + lua_State *DFHack::Lua::Open(color_ostream &out, lua_State *state) { if (!state) @@ -872,7 +917,8 @@ lua_State *DFHack::Lua::Open(color_ostream &out, lua_State *state) OpenPersistent(state); - LuaWrapper::AddMethodWrapper(state, 0, -1, "getSelectedJob", df::wrap_function(&Gui::getSelectedJob)); + OpenModule(state, "gui", dfhack_gui_module); + OpenModule(state, "job", dfhack_job_module); lua_setglobal(state, "dfhack"); |
