From e9ef9b87b575b4746e2090556c192237c95f508a Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Thu, 17 May 2012 20:04:09 +0400 Subject: [PATCH] Add central locations for onUpdate and onStateChange handling in core. --- library/Core.cpp | 35 ++++++++++++++++++++++----------- library/PluginManager.cpp | 2 -- library/include/Core.h | 14 +++++++++++++ library/include/PluginManager.h | 10 ---------- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/library/Core.cpp b/library/Core.cpp index 7f08f402f..1d2a76026 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1017,7 +1017,7 @@ int Core::Update() Lua::Core::Reset(out, "DF code execution"); if (first_update) - plug_mgr->OnStateChange(out, SC_CORE_INITIALIZED); + onStateChange(out, SC_CORE_INITIALIZED); // detect if the game was loaded or unloaded in the meantime void *new_wdata = NULL; @@ -1043,11 +1043,11 @@ int Core::Update() // and if the world is going away, we report the map change first if(had_map) - plug_mgr->OnStateChange(out, SC_MAP_UNLOADED); + onStateChange(out, SC_MAP_UNLOADED); // and if the world is appearing, we report map change after that - plug_mgr->OnStateChange(out, new_wdata ? SC_WORLD_LOADED : SC_WORLD_UNLOADED); + onStateChange(out, new_wdata ? SC_WORLD_LOADED : SC_WORLD_UNLOADED); if(isMapLoaded()) - plug_mgr->OnStateChange(out, SC_MAP_LOADED); + onStateChange(out, SC_MAP_LOADED); } // otherwise just check for map change... else if (new_mapdata != last_local_map_ptr) @@ -1058,7 +1058,7 @@ int Core::Update() if (isMapLoaded() != had_map) { getWorld()->ClearPersistentCache(); - plug_mgr->OnStateChange(out, new_mapdata ? SC_MAP_LOADED : SC_MAP_UNLOADED); + onStateChange(out, new_mapdata ? SC_MAP_LOADED : SC_MAP_UNLOADED); } } @@ -1071,15 +1071,12 @@ int Core::Update() if (screen != top_viewscreen) { top_viewscreen = screen; - plug_mgr->OnStateChange(out, SC_VIEWSCREEN_CHANGED); + onStateChange(out, SC_VIEWSCREEN_CHANGED); } } - // notify all the plugins that a game tick is finished - plug_mgr->OnUpdate(out); - - // process timers in lua - Lua::Core::onUpdate(out); + // Execute per-frame handlers + onUpdate(out); // Release the fake suspend lock { @@ -1116,6 +1113,22 @@ int Core::Update() return 0; }; +void Core::onUpdate(color_ostream &out) +{ + // notify all the plugins that a game tick is finished + plug_mgr->OnUpdate(out); + + // process timers in lua + Lua::Core::onUpdate(out); +} + +void Core::onStateChange(color_ostream &out, state_change_event event) +{ + plug_mgr->OnStateChange(out, event); + + Lua::Core::onStateChange(out, event); +} + // FIXME: needs to terminate the IO threads and properly dismantle all the machinery involved. int Core::Shutdown ( void ) { diff --git a/library/PluginManager.cpp b/library/PluginManager.cpp index 320e8dec8..ae8cc755f 100644 --- a/library/PluginManager.cpp +++ b/library/PluginManager.cpp @@ -609,8 +609,6 @@ void PluginManager::OnStateChange(color_ostream &out, state_change_event event) { all_plugins[i]->on_state_change(out, event); } - - Lua::Core::onStateChange(out, event); } // FIXME: doesn't check name collisions! diff --git a/library/include/Core.h b/library/include/Core.h index fe83715cf..7da633cca 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -68,6 +68,17 @@ namespace DFHack class df_window; } + enum state_change_event + { + SC_WORLD_LOADED = 0, + SC_WORLD_UNLOADED = 1, + SC_MAP_LOADED = 2, + SC_MAP_UNLOADED = 3, + SC_VIEWSCREEN_CHANGED = 4, + SC_CORE_INITIALIZED = 5, + SC_BEGIN_UNLOAD = 6 + }; + // Core is a singleton. Why? Because it is closely tied to SDL calls. It tracks the global state of DF. // There should never be more than one instance // Better than tracking some weird variables all over the place. @@ -162,6 +173,9 @@ namespace DFHack int SDL_Event(SDL::Event* event); bool ncurses_wgetch(int in, int & out); + void onUpdate(color_ostream &out); + void onStateChange(color_ostream &out, state_change_event event); + Core(Core const&); // Don't Implement void operator=(Core const&); // Don't implement diff --git a/library/include/PluginManager.h b/library/include/PluginManager.h index 7f98bf422..02974f36b 100644 --- a/library/include/PluginManager.h +++ b/library/include/PluginManager.h @@ -65,16 +65,6 @@ namespace DFHack // Close a plugin library void ClosePlugin (DFLibrary * plugin); - enum state_change_event - { - SC_WORLD_LOADED = 0, - SC_WORLD_UNLOADED = 1, - SC_MAP_LOADED = 2, - SC_MAP_UNLOADED = 3, - SC_VIEWSCREEN_CHANGED = 4, - SC_CORE_INITIALIZED = 5, - SC_BEGIN_UNLOAD = 6 - }; struct DFHACK_EXPORT CommandReg { const char *name; int (*command)(lua_State*);