Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/Console-windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ bool Console::init(bool)
// FIXME: looks awfully empty, doesn't it?
bool Console::shutdown(void)
{
assert(inited);
std::lock_guard<std::recursive_mutex> lock{*wlock};
FreeConsole();
inited = false;
Expand Down
20 changes: 18 additions & 2 deletions library/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ namespace DFHack {
return Filesystem::getInstallDir() / "hack" / "data" / "dfhack-config-defaults";
};

Core* Core::active_instance = nullptr;

class MainThread {
public:
//! MainThread::suspend keeps the main DF thread suspended from Core::Init to
Expand Down Expand Up @@ -1062,6 +1064,11 @@ df::viewscreen * Core::getTopViewscreen() {
}

bool Core::InitMainThread() {
assert(active_instance == nullptr);

// set this instance as the active instance
active_instance = this;

// this hook is always called from DF's main (render) thread, so capture this thread id
df_render_thread = std::this_thread::get_id();

Expand Down Expand Up @@ -1462,8 +1469,8 @@ bool Core::InitSimulationThread()
}

Core& Core::getInstance() {
static Core instance;
return instance;
assert(Core::active_instance != nullptr);
return *Core::active_instance;
}

bool Core::isSuspended(void)
Expand Down Expand Up @@ -1893,20 +1900,29 @@ int Core::Shutdown ( void )

if (hotkey_mgr) {
delete hotkey_mgr;
hotkey_mgr = nullptr;
}

if(plug_mgr)
{
delete plug_mgr;
plug_mgr = nullptr;
}

// invalidate all modules
allModules.clear();
Textures::cleanup();
DFSDL::cleanup();

// FIXME console has already been shut down at this point, so getConsole is returning a dead object
DFSteam::cleanup(getConsole());

memset(&(s_mods), 0, sizeof(s_mods));
d.reset();

// clear active instance
Core::active_instance = nullptr;

return -1;
}

Expand Down
3 changes: 0 additions & 3 deletions library/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ void DebugManager::unregisterCategory(DebugCategory& cat)

DebugRegisterBase::DebugRegisterBase(DebugCategory* cat)
{
// Make sure Core lives at least as long any DebugCategory to
// allow debug prints until all Debugcategories has been destructed
Core::getInstance();
DebugManager::getInstance().registerCategory(*cat);
}

Expand Down
21 changes: 15 additions & 6 deletions library/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ static bool disabled = false;

DFhackCExport const int32_t dfhooks_priority = 100;

static std::unique_ptr<DFHack::Core> core_instance;

// called from the main thread before the simulation thread is started
// and the main event loop is initiated
DFhackCExport void dfhooks_init() {
Expand All @@ -16,8 +18,11 @@ DFhackCExport void dfhooks_init() {
return;
}

// construct DFHack core instance
core_instance = std::make_unique<DFHack::Core>();

// we need to init DF globals before we can check the commandline
if (!DFHack::Core::getInstance().InitMainThread() || !df::global::game) {
if (!core_instance->InitMainThread() || !df::global::game) {
// we don't set disabled to true here so symbol generation can work
return;
}
Expand All @@ -26,6 +31,8 @@ DFhackCExport void dfhooks_init() {
if (cmdline.find("--disable-dfhack") != std::string::npos) {
fprintf(stderr, "dfhack: --disable-dfhack specified on commandline; disabling\n");
disabled = true;
core_instance->Shutdown();
core_instance.reset();
return;
}

Expand All @@ -36,14 +43,16 @@ DFhackCExport void dfhooks_init() {
DFhackCExport void dfhooks_shutdown() {
if (disabled)
return;
DFHack::Core::getInstance().Shutdown();
core_instance->Shutdown();
// release DFHack core instance
core_instance.reset();
}

// called from the simulation thread in the main event loop
DFhackCExport void dfhooks_update() {
if (disabled)
return;
DFHack::Core::getInstance().Update();
core_instance->Update();
}

// called from the simulation thread just before adding the macro
Expand All @@ -59,7 +68,7 @@ DFhackCExport void dfhooks_prerender() {
DFhackCExport bool dfhooks_sdl_event(SDL_Event* event) {
if (disabled)
return false;
return DFHack::Core::getInstance().DFH_SDL_Event(event);
return core_instance->DFH_SDL_Event(event);
}

// called from the main thread just after setting mouse state in gps and just
Expand All @@ -68,7 +77,7 @@ DFhackCExport void dfhooks_sdl_loop() {
if (disabled)
return;
// TODO: wire this up to the new SDL-based console once it is merged
DFHack::Core::getInstance().DFH_SDL_Loop();
core_instance->DFH_SDL_Loop();
}

// called from the main thread for each utf-8 char read from the ncurses input
Expand All @@ -78,5 +87,5 @@ DFhackCExport void dfhooks_sdl_loop() {
DFhackCExport bool dfhooks_ncurses_key(int key) {
if (disabled)
return false;
return DFHack::Core::getInstance().DFH_ncurses_key(key);
return core_instance->DFH_ncurses_key(key);
}
12 changes: 9 additions & 3 deletions library/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,11 @@ namespace DFHack
friend void ::dfhooks_sdl_loop();
friend bool ::dfhooks_ncurses_key(int key);
public:
/// Get the single Core instance or make one.
/// Get the current active Core instance. will assert if none exists
/// Use noInstance() to check first if unsure
static Core& getInstance();
static bool noInstance() { return active_instance == nullptr; }

/// check if the activity lock is owned by this thread
bool isSuspended(void);
/// Is everything OK?
Expand Down Expand Up @@ -259,11 +262,14 @@ namespace DFHack
return false;
}

Core();
~Core();

private:
static Core* active_instance;

DFHack::Console con;

Core();
~Core();

struct Private;
std::unique_ptr<Private> d;
Expand Down
9 changes: 8 additions & 1 deletion library/include/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class DFHACK_EXPORT DebugCategory final {
};

/*!
* Fetch a steam object proxy object for output. It also adds standard
* Fetch a stream object proxy object for output. It also adds standard
* message components like time and plugin and category names to the line.
*
* User must make sure that the line is terminated with a line end.
Expand All @@ -194,6 +194,13 @@ class DFHACK_EXPORT DebugCategory final {
*/
ostream_proxy_prefix getStream(const level msgLevel) const
{
// if the core instance is unavailable, use stderr as a fallback
if (Core::noInstance())
{
static color_ostream_wrapper fallback{std::cerr};
return {*this,fallback,msgLevel};
}

return {*this,Core::getInstance().getConsole(),msgLevel};
}
/*!
Expand Down