From 9f6638432f28fa534a9045324516224b985ad0d3 Mon Sep 17 00:00:00 2001 From: expwnent Date: Mon, 10 Jun 2013 00:45:46 -0400 Subject: [PATCH] When a save is loaded/unloaded, the script at raw/onLoad.init or raw/onUnload.init is run. They use the same format as dfhack.init. --- NEWS | 1 + library/Core.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/NEWS b/NEWS index db8ec5a60..039749951 100644 --- a/NEWS +++ b/NEWS @@ -32,6 +32,7 @@ DFHack future - autotrade: Automatically send items in marked stockpiles to trade depot, when trading is possible. - stocks: An improved stocks display screen. Internals: + - Core: there is now a per-save dfhack.init file for when the save is loaded, and another for when it is unloaded - EventManager: fixed job completion detection - Once: easy way to make sure something happens once per run of DF, such as an error message diff --git a/library/Core.cpp b/library/Core.cpp index 2021a8bc5..094829300 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1298,6 +1298,21 @@ void Core::onUpdate(color_ostream &out) Lua::Core::onUpdate(out); } +static void handleLoadAndUnloadScripts(Core* core, color_ostream& out, state_change_event event) { + //TODO: use different separators for windows + std::string rawFolder = "data/save/" + (df::global::world->cur_savegame.save_dir) + "/raw/"; + switch(event) { + case SC_WORLD_LOADED: + core->loadScriptFile(out, rawFolder + "onLoad.init", true); + break; + case SC_WORLD_UNLOADED: + core->loadScriptFile(out, rawFolder + "onUnload.init", true); + break; + default: + break; + } +} + void Core::onStateChange(color_ostream &out, state_change_event event) { EventManager::onStateChange(out, event); @@ -1307,6 +1322,8 @@ void Core::onStateChange(color_ostream &out, state_change_event event) plug_mgr->OnStateChange(out, event); Lua::Core::onStateChange(out, event); + + handleLoadAndUnloadScripts(this, out, event); } // FIXME: needs to terminate the IO threads and properly dismantle all the machinery involved.