diff options
| author | Alexander Gavrilov | 2012-04-15 19:09:25 +0400 |
|---|---|---|
| committer | Alexander Gavrilov | 2012-04-15 19:09:25 +0400 |
| commit | 14709e5d4598e11ddce6f9d2cce734528d842ca5 (patch) | |
| tree | b1370d42d766b329b1ce8fc5431b4e7abf04a4b8 /plugins/burrows.cpp | |
| parent | cb27a1d83916b333d1a0d1b5aa24a7f371e120af (diff) | |
| download | dfhack-14709e5d4598e11ddce6f9d2cce734528d842ca5.tar.gz dfhack-14709e5d4598e11ddce6f9d2cce734528d842ca5.tar.bz2 dfhack-14709e5d4598e11ddce6f9d2cce734528d842ca5.tar.xz | |
Add an official core lua context, and allow plugins to send events to it.
- This context requires core suspend lock and asserts it in a few places.
- Special 'event' objects are introduced. They can be invoked as
functions, in which case they iterate all their fields and call
them as functions. Errors are printed and consumed.
- When a plugin is opened by the core context, events registered in
a special array are linked to it. The system is organized so as to
avoid even trying to pass the event to lua if the module isn't loaded.
Diffstat (limited to 'plugins/burrows.cpp')
| -rw-r--r-- | plugins/burrows.cpp | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/plugins/burrows.cpp b/plugins/burrows.cpp index 74cb57c4..a629e30c 100644 --- a/plugins/burrows.cpp +++ b/plugins/burrows.cpp @@ -5,6 +5,7 @@ #include "Error.h" #include "DataFuncs.h" +#include "LuaTools.h" #include "modules/Gui.h" #include "modules/Job.h" @@ -121,6 +122,8 @@ static int name_burrow_id = -1; static void handle_burrow_rename(color_ostream &out, df::burrow *burrow); +DEFINE_LUA_EVENT_1(onBurrowRename, handle_burrow_rename, df::burrow*); + static void detect_burrow_renames(color_ostream &out) { if (ui->main.mode == ui_sidebar_mode::Burrows && @@ -134,7 +137,7 @@ static void detect_burrow_renames(color_ostream &out) auto burrow = df::burrow::find(name_burrow_id); name_burrow_id = -1; if (burrow) - handle_burrow_rename(out, burrow); + onBurrowRename(out, burrow); } } @@ -151,6 +154,9 @@ static std::map<int,DigJob> diggers; static void handle_dig_complete(color_ostream &out, df::job_type job, df::coord pos, df::tiletype old_tile, df::tiletype new_tile); +DEFINE_LUA_EVENT_4(onDigComplete, handle_dig_complete, + df::job_type, df::coord, df::tiletype, df::tiletype); + static void detect_digging(color_ostream &out) { for (auto it = diggers.begin(); it != diggers.end();) @@ -172,7 +178,7 @@ static void detect_digging(color_ostream &out) if (new_tile != it->second.old_tile) { - handle_dig_complete(out, it->second.job, pos, it->second.old_tile, new_tile); + onDigComplete(out, it->second.job, pos, it->second.old_tile, new_tile); //if (worker && !worker->job.current_job) // worker->counters.think_counter = worker->counters.job_counter = 0; @@ -410,6 +416,17 @@ static void handle_dig_complete(color_ostream &out, df::job_type job, df::coord } } +static void renameBurrow(color_ostream &out, df::burrow *burrow, std::string name) +{ + CHECK_NULL_POINTER(burrow); + + // The event makes this absolutely necessary + CoreSuspender suspend; + + burrow->name = name; + onBurrowRename(out, burrow); +} + static df::burrow *findByName(color_ostream &out, std::string name, bool silent = false) { int id = -1; @@ -552,6 +569,7 @@ static bool setTilesByKeyword(df::burrow *target, std::string name, bool enable) } DFHACK_PLUGIN_LUA_FUNCTIONS { + DFHACK_LUA_FUNCTION(renameBurrow), DFHACK_LUA_FUNCTION(findByName), DFHACK_LUA_FUNCTION(copyUnits), DFHACK_LUA_FUNCTION(copyTiles), @@ -559,6 +577,12 @@ DFHACK_PLUGIN_LUA_FUNCTIONS { DFHACK_LUA_END }; +DFHACK_PLUGIN_LUA_EVENTS { + DFHACK_LUA_EVENT(onBurrowRename), + DFHACK_LUA_EVENT(onDigComplete), + DFHACK_LUA_END +}; + static command_result burrow(color_ostream &out, vector <string> ¶meters) { CoreSuspender suspend; |
