diff --git a/docs/Core.rst b/docs/Core.rst index 5decd668d..62b91c19c 100644 --- a/docs/Core.rst +++ b/docs/Core.rst @@ -53,7 +53,7 @@ double quotes. To include a double quote character, use ``\"``. If the first non-whitespace character is ``:``, the command is parsed in an alternative mode. The non-whitespace characters following the ``:`` are the command name, and the remaining part of the line is used verbatim as -the first argument. This is very useful for the `lua` and `rb` commands. +the first argument. This is very useful for the `lua` command. As an example, the following two command lines are exactly equivalent:: :foo a b "c d" e f @@ -306,6 +306,23 @@ the root DF folder. Note that ``script-paths.txt`` is only read at startup, but the paths can also be modified programmatically at any time through the `Lua API `. +Commandline options +=================== + +In addition to `Using an OS terminal`_ to execute commands on startup, DFHack +also recognizes a single commandline option that can be specified on the +commandline: + +- ``--disable-dfhack``: If this option is passed on the Dwarf Fortress + commandline, then DFHack will be disabled for the session. You will have to + restart Dwarf Fortress without specifying this option in order to use DFHack. + If you are launching Dwarf Fortress from Steam, you can enter the option in + the "Launch Options" text box in the properties for the Dwarf Fortress app. + Note that if you do this, DFHack will be disabled regardless of whether you + run Dwarf Fortress from its own app or DFHack's. You will have to clear the + DF Launch Options in order to use DFHack again. Note that even if DFHack is + disabled, :file:`stdout.txt` and :file:`stderr.txt` will still be redirected + to :file:`stdout.log` and :file:`stderr.log`, respectively. .. _env-vars: diff --git a/docs/changelog.txt b/docs/changelog.txt index fc706cdba..d602ed93a 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -48,6 +48,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## Misc Improvements - Terminal console no longer appears in front of the game window on startup - `gui/design`: Improved performance for drawing shapes +- ``Core``: For debugging purposes, you can now pass ``--disable-dfhack`` on the Dwarf Fortress commandline to disable DFHack for the session. ## Documentation diff --git a/library/Core.cpp b/library/Core.cpp index 9ff6976d5..256493dce 100644 --- a/library/Core.cpp +++ b/library/Core.cpp @@ -1471,16 +1471,8 @@ std::string Core::getHackPath() #endif } -bool Core::Init() -{ - if(started) - return true; - if(errorstate) - return false; - - // Lock the CoreSuspendMutex until the thread exits or call Core::Shutdown - // Core::Update will temporary unlock when there is any commands queued - MainThread::suspend().lock(); +bool Core::InitMainThread() { + Filesystem::init(); // Re-route stdout and stderr again - DF seems to set up stdout and // stderr.txt on Windows as of 0.43.05. Also, log before switching files to @@ -1496,8 +1488,6 @@ bool Core::Init() if (!freopen("stderr.log", "w", stderr)) std::cerr << "Could not redirect stderr to stderr.log" << std::endl; - Filesystem::init(); - std::cerr << "DFHack build: " << Version::git_description() << "\n" << "Starting with working directory: " << Filesystem::getcwd() << std::endl; @@ -1566,6 +1556,20 @@ bool Core::Init() // Init global object pointers df::global::InitGlobals(); + return true; +} + +bool Core::InitSimulationThread() +{ + if(started) + return true; + if(errorstate) + return false; + + // Lock the CoreSuspendMutex until the thread exits or call Core::Shutdown + // Core::Update will temporary unlock when there is any commands queued + MainThread::suspend().lock(); + std::cerr << "Initializing Console.\n"; // init the console. bool is_text_mode = (init && init->display.flag.is_set(init_display_flags::TEXT)); @@ -1965,7 +1969,7 @@ int Core::Update() if(!started) { // Initialize the core - Init(); + InitSimulationThread(); if(errorstate) return -1; } diff --git a/library/Hooks.cpp b/library/Hooks.cpp index c241e4875..1976d60d7 100644 --- a/library/Hooks.cpp +++ b/library/Hooks.cpp @@ -1,31 +1,49 @@ #include "Core.h" #include "Export.h" +#include "df/gamest.h" + +static bool disabled = false; + // called from the main thread before the simulation thread is started // and the main event loop is initiated DFhackCExport void dfhooks_init() { - // TODO: initialize things we need to do while still in the main thread + if (!DFHack::Core::getInstance().InitMainThread() || !df::global::game) + return; + const std::string & cmdline = df::global::game->command_line.original; + if (cmdline.find("--disable-dfhack") != std::string::npos) { + fprintf(stdout, "dfhack: --disable-dfhack specified on commandline; disabling\n"); + disabled = true; + } } // called from the main thread after the main event loops exits DFhackCExport void dfhooks_shutdown() { + if (disabled) + return; DFHack::Core::getInstance().Shutdown(); } // called from the simulation thread in the main event loop DFhackCExport void dfhooks_update() { + if (disabled) + return; DFHack::Core::getInstance().Update(); } // called from the simulation thread just before adding the macro // recording/playback overlay DFhackCExport void dfhooks_prerender() { + if (disabled) + return; // TODO: render overlay widgets that are not attached to a viewscreen } // called from the main thread for each SDL event. if true is returned, then // the event has been consumed and further processing shouldn't happen DFhackCExport bool dfhooks_sdl_event(SDL::Event* event) { + if (disabled) + return false; return DFHack::Core::getInstance().DFH_SDL_Event(event); } @@ -34,5 +52,7 @@ DFhackCExport bool dfhooks_sdl_event(SDL::Event* event) { // if true is returned, then the event has been consumed and further processing // shouldn't happen DFhackCExport bool dfhooks_ncurses_key(int key) { + if (disabled) + return false; return DFHack::Core::getInstance().DFH_ncurses_key(key); } diff --git a/library/include/Core.h b/library/include/Core.h index 386769fcb..696be4ead 100644 --- a/library/include/Core.h +++ b/library/include/Core.h @@ -191,7 +191,8 @@ namespace DFHack struct Private; std::unique_ptr d; - bool Init(); + bool InitMainThread(); + bool InitSimulationThread(); int Update (void); int Shutdown (void); bool DFH_SDL_Event(SDL::Event* event);