From ee999ccbdf8c68175195b604ef5988ddda7b4956 Mon Sep 17 00:00:00 2001 From: Pauli Date: Sat, 9 Jun 2018 12:02:42 +0300 Subject: [PATCH 01/32] Implement runtime debug print filtering The runtime debug print filtering support dynamic debug print selection. Tis patch only implements basic core support for filtering. The commands to change the runtime filtering settings will be added in a following patch. But even with only this one can change filtering settings by editing memory using a debugger. It can even be automated by using gdb break point commands. --- library/CMakeLists.txt | 4 + library/Debug.cpp | 186 ++++++++ library/include/Debug.h | 369 ++++++++++++++++ library/include/DebugManager.h | 111 +++++ library/include/Signal.hpp | 782 +++++++++++++++++++++++++++++++++ 5 files changed, 1452 insertions(+) create mode 100644 library/Debug.cpp create mode 100644 library/include/Debug.h create mode 100644 library/include/DebugManager.h create mode 100644 library/include/Signal.hpp diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 390efdcfe..416e42c41 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -28,6 +28,8 @@ include/Core.h include/ColorText.h include/DataDefs.h include/DataIdentity.h +include/Debug.h +include/DebugManager.h include/VTableInterpose.h include/LuaWrapper.h include/LuaTools.h @@ -38,6 +40,7 @@ include/MiscUtils.h include/Module.h include/Pragma.h include/MemAccess.h +include/Signal.hpp include/TileTypes.h include/Types.h include/VersionInfo.h @@ -55,6 +58,7 @@ SET(MAIN_SOURCES Core.cpp ColorText.cpp DataDefs.cpp +Debug.cpp Error.cpp VTableInterpose.cpp LuaWrapper.cpp diff --git a/library/Debug.cpp b/library/Debug.cpp new file mode 100644 index 000000000..aa0cf8cdf --- /dev/null +++ b/library/Debug.cpp @@ -0,0 +1,186 @@ +/** +Copyright © 2018 Pauli + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must + not claim that you wrote the original software. If you use this + software in a product, an acknowledgment in the product + documentation would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and + must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source + distribution. + */ +#define _POSIX_C_SOURCE 200809L +#include "Core.h" + +#include "Debug.h" +#include "DebugManager.h" + +#include +#include +#include + +#ifdef _MSC_VER +static tm* localtime_r(const time_t* time, tm* result) +{ + localtime_s(result, time); + return result; +} +#endif + +namespace DFHack { +DBG_DECLARE(core,debug); + +void DebugManager::registerCategory(DebugCategory& cat) +{ + DEBUG(debug) << "register DebugCategory '" << cat.category() + << "' from '" << cat.plugin() + << "' allowed " << cat.allowed() << std::endl; + std::lock_guard guard(access_mutex_); + push_back(&cat); + categorySignal(CAT_ADD, cat); +} + +void DebugManager::unregisterCategory(DebugCategory& cat) +{ + DEBUG(debug) << "unregister DebugCategory '" << cat.category() + << "' from '" << cat.plugin() + << "' allowed " << cat.allowed() << std::endl; + std::lock_guard guard(access_mutex_); + auto iter = std::find(begin(), end(), &cat); + std::swap(*iter, back()); + pop_back(); + categorySignal(CAT_REMOVE, 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); +} + +void DebugRegisterBase::unregister(DebugCategory* cat) +{ + DebugManager::getInstance().unregisterCategory(*cat); +} + +static color_value selectColor(const DebugCategory::level msgLevel) +{ + switch(msgLevel) { + case DebugCategory::LTRACE: + return COLOR_GREY; + case DebugCategory::LDEBUG: + return COLOR_LIGHTBLUE; + case DebugCategory::LINFO: + return COLOR_CYAN; + case DebugCategory::LWARNING: + return COLOR_YELLOW; + case DebugCategory::LERROR: + return COLOR_LIGHTRED; + } + return COLOR_WHITE; +} + +#if __GNUC__ +// Allow gcc to optimize tls access. It also makes sure initialized is done as +// early as possible. The early initialization helps to give threads same ids as +// gdb shows. +#define EXEC_ATTR __attribute__((tls_model("initial-exec"))) +#else +#define EXEC_ATTR +#endif + +namespace { +static std::atomic nextId{0}; +static EXEC_ATTR thread_local uint32_t thread_id{nextId.fetch_add(1)+1}; +} + +DebugCategory::ostream_proxy_prefix::ostream_proxy_prefix( + const DebugCategory& cat, + color_ostream& target, + const DebugCategory::level msgLevel) : + color_ostream_proxy(target) +{ + color(selectColor(msgLevel)); + auto now = std::chrono::system_clock::now(); + tm local{}; + //! \todo c++ 2020 will have std::chrono::to_stream(fmt, system_clock::now()) + //! but none implements it yet. + std::time_t now_c = std::chrono::system_clock::to_time_t(now); + auto ms = std::chrono::duration_cast(now.time_since_epoch()) % 1000; + // Output time in format %02H:%02M:%02S.%03ms +#if __GNUC__ < 5 + // Fallback for gcc 4 + char buffer[32]; + size_t sz = strftime(buffer, sizeof(buffer)/sizeof(buffer[0]), + "%T.", localtime_r(&now_c, &local)); + *this << (sz > 0 ? buffer : "HH:MM:SS.") +#else + *this << std::put_time(localtime_r(&now_c, &local),"%T.") +#endif + << std::setfill('0') << std::setw(3) << ms.count() + // Thread id is allocated in the thread creation order to a thread_local + // variable + << ":t" << thread_id + // Output plugin and category names to make it easier to locate where + // the message is coming. It would be easy replaces these with __FILE__ + // and __LINE__ passed from the macro if that would be preferred prefix. + << ':' << cat.plugin() << ':' << cat.category() << ": "; +} + + +DebugCategory::level DebugCategory::allowed() const noexcept +{ + return allowed_.load(std::memory_order_relaxed); +} + +void DebugCategory::allowed(DebugCategory::level value) noexcept +{ + level old = allowed_.exchange(value, std::memory_order_relaxed); + if (old == value) + return; + TRACE(debug) << "modify DebugCategory '" << category() + << "' from '" << plugin() + << "' allowed " << value << std::endl; + auto& manager = DebugManager::getInstance(); + manager.categorySignal(DebugManager::CAT_MODIFIED, *this); +} + +DebugCategory::cstring_ref DebugCategory::category() const noexcept +{ + return category_; +} + +DebugCategory::cstring_ref DebugCategory::plugin() const noexcept +{ + return plugin_; +} + +#if __cplusplus < 201703L && __cpp_lib_atomic_is_always_lock_free < 201603 +//! C++17 has std::atomic::is_always_lock_free for static_assert. Older +//! standards only provide runtime checks if an atomic type is lock free +struct failIfEnumAtomicIsNotLockFree { + failIfEnumAtomicIsNotLockFree() { + std::atomic test; + if (test.is_lock_free()) + return; + std::cerr << __FILE__ << ':' << __LINE__ + << ": error: std::atomic should be lock free. Your compiler reports the atomic requires runtime locks. Either you are using a very old CPU or we need to change code to use integer atomic type." << std::endl; + std::abort(); + } +} failIfEnumAtomicIsNotLockFree; +#endif + +} diff --git a/library/include/Debug.h b/library/include/Debug.h new file mode 100644 index 000000000..976172c8d --- /dev/null +++ b/library/include/Debug.h @@ -0,0 +1,369 @@ +/** +Copyright © 2018 Pauli + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must + not claim that you wrote the original software. If you use this + software in a product, an acknowledgment in the product + documentation would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and + must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source + distribution. + */ + +#pragma once + +#include "ColorText.h" + +#include +#include "Core.h" + +namespace DFHack { + +/*! \file Debug.h + * Light weight wrappers to runtime debug output filtering. Idea is to add as + * little as possible code compared to debug output without filtering. The + * effect is archived using #TRACE, #DEBUG, #INFO, #WARN and #ERR macros. They + * "return" color_ostream object or reference that can be then used normally for + * either printf or stream style debug output. + * + * Internally macros do inline filtering check which allows compiler to have a + * fast path without debug output only checking unlikely condition. But if + * output is enabled then runtime code will jump to debug printing function + * calls. The macro setup code will also print standardized leading part of + * debug string including time stamp, plugin name and debug category name. + * + * \code{.cpp} + * #include "Debug.h" + * DBG_DECLARE(myplugin,init); + * + * DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) + * { + * command_result rv = CR_OK; + * DEBUG(init, out).print("initializing\n") + * if ((rv = initWork()) != CR_OK) { + * ERR(init, out) << "initWork failed with " + * << rv << " error code" << std::endl; + * return rv; + * } + * return rv + * } + * \endcode + * + * The debug print filtering levels can be changed using debugger. Following + * gdb example would automatically setup core/init and core/render to trace + * level when SDL_init is called. + * + * \code{.unparsed} + * break SDL_init + * commands + * silent + * p DFHack::debug::core::debug_init.allowed_ = 0 + * p DFHack::debug::core::debug_render.allowed_ = 0 + * c + * end + * \endcode + * + */ + +#ifndef __has_cpp_attribute +#define __has_cpp_attribute(x) 0 +#endif + +/*! + * \defgroup debug_branch_prediction Branch prediction helper macros + * Helper macro tells compiler that debug output branch is unlikely and should + * be optimized to cold section of the function. + * \{ + */ +#if __cplusplus >= 202000L || __has_cpp_attribute(likely) +// c++20 will have standard branch prediction hint attributes +#define likely(x) (x) [[likely]] +#define unlikely(x) (x) [[unlikely]] +#elif defined(__GNUC__) +// gcc has builtin functions that give hints to the branch prediction +#define likely(x) (__builtin_expect(!!(x), 1)) +#define unlikely(x) (__builtin_expect(!!(x), 0)) +#else +#define likely(x) (x) +#define unlikely(x) (x) +#endif +//! \} + +#ifdef NDEBUG +//! Reduce minimum compiled in debug levels if NDEBUG is defined +#define DBG_FILTER DFHack::DebugCategory::LINFO +#else +//! Set default compiled in debug levels to include all prints +#define DBG_FILTER DFHack::DebugCategory::LTRACE +#endif + +/*! + * DebugCategory is used to enable and disable debug messages in runtime. + * Declaration and definition are handled by #DBG_DECLARE and #DBG_DEFINE + * macros. Runtime filtering support is handled by #TRACE, #DEBUG, #INFO, #WARN + * and #ERR macros. + */ +class DFHACK_EXPORT DebugCategory final { +public: + //! type helper to maybe make it easier to convert to std::string_view when + //! c++17 can be required. + using cstring = const char*; + using cstring_ref = const char*; + /*! + * Debug level enum for message filtering + */ + enum level { + LTRACE = 0, + LDEBUG = 1, + LINFO = 2, + LWARNING = 3, + LERROR = 4, + }; + + /*! + * \param plugin the name of plugin the category belongs to + * \param category the name of category + * \param defaultLevel optional default filtering level for the category + */ + constexpr DebugCategory(cstring_ref plugin, + cstring_ref category, + level defaultLevel = LWARNING) noexcept : + plugin_{plugin}, + category_{category}, + allowed_{defaultLevel} + {} + + + DebugCategory(const DebugCategory&) = delete; + DebugCategory(DebugCategory&&) = delete; + DebugCategory& operator=(DebugCategory) = delete; + DebugCategory& operator=(DebugCategory&&) = delete; + + /*! + * Used by debug macros to check if message should be printed. + * + * It is defined in the header to allow compiler inline it and make disabled + * state a fast path without function calls. + * + * \param msgLevel the debug message level the following print belongs to + * \return boolean with true indicating that message should be printed + */ + bool isEnabled(const level msgLevel) const noexcept { + const uint32_t intLevel = static_cast(msgLevel); + // Compile time filtering to allow compiling out debug checks prints + // from binary. + return static_cast(DBG_FILTER) <= intLevel && + // Runtime filtering for debug messages + static_cast(allowed_.load(std::memory_order_relaxed)) <= intLevel; + } + + struct DFHACK_EXPORT ostream_proxy_prefix : public color_ostream_proxy { + ostream_proxy_prefix(const DebugCategory& cat, + color_ostream& target, + DebugCategory::level level); + ~ostream_proxy_prefix() + { + flush(); + } + }; + + /*! + * Fetch a steam 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. + * + * \param msgLevel Specifies the level which next debug message belongs + * \return color_ostream_proxy that can be used to print the message + * \sa DFHack::Core::getConsole() + */ + ostream_proxy_prefix getStream(const level msgLevel) const + { + return {*this,Core::getInstance().getConsole(),msgLevel}; + } + /*! + * Add standard message components to existing output stream object to begin + * a new message line to an shared buffered object. + * + * \param msgLevel Specifies the level which next debug message belongs + * \param target An output stream object where a debug output is printed + * \return color_ostream reference that was passed as second parameter + */ + ostream_proxy_prefix getStream(const level msgLevel, color_ostream& target) const + { + return {*this,target,msgLevel}; + } + + /*! + * \brief Allow management code to set a new filtering level + * Caller must have locked DebugManager::access_mutex_. + */ + void allowed(level value) noexcept; + //! Query current filtering level + level allowed() const noexcept; + //! Query plugin name + cstring_ref plugin() const noexcept; + //! Query category name + cstring_ref category() const noexcept; +private: + + cstring plugin_; + cstring category_; + std::atomic allowed_; +#if __cplusplus >= 201703L || __cpp_lib_atomic_is_always_lock_free >= 201603 + static_assert(std::atomic::is_always_lock_free, + "std::atomic should be lock free. You are using a very old CPU or code needs to use std::atomic"); +#endif +}; + +/** + * Handle actual registering wrong template parameter generated pointer + * calculation. + */ +class DFHACK_EXPORT DebugRegisterBase { +protected: + DebugRegisterBase(DebugCategory* category); + void unregister(DebugCategory* category); +}; + +/** + * Register DebugCategory to DebugManager + */ +template +class DebugRegister final : public DebugRegisterBase { +public: + DebugRegister() : + DebugRegisterBase{category} + {} + ~DebugRegister() { + unregister(category); + } +}; + +#define DBG_NAME(category) debug_ ## category + + +/*! + * Declares a debug category. There must be only a declaration per category. + * Declaration should be in same plugin where it is used. If same category name + * is used in core and multiple plugins they all are changed with same command + * unless user specifies explicitly plugin name. + * + * Must be used in one translation unit only. + * + * \param plugin the name of plugin where debug category is used + * \param category the name of category + * \param level the initial DebugCategory::level filtering level. + */ +#define DBG_DECLARE(plugin,category, ...) \ + namespace debug { namespace plugin { \ + DebugCategory DBG_NAME(category){#plugin,#category,__VA_ARGS__}; \ + DebugRegister<&DBG_NAME(category)> register_ ## category; \ + } } \ + using debug::plugin::DBG_NAME(category) + +/*! + * Can be used to access a shared DBG_DECLARE category. But may not be used from + * static initializer because translation unit order is undefined. + * + * Can be used in shared headers to gain access to one definition from + * DBG_DECLARE. + * \param plugin The plugin name that must match DBG_DECLARE + * \param category The category name that must matnch DBG_DECLARE + */ +#define DBG_EXTERN(plugin,category) \ + namespace debug { namespace plugin { \ + extern DFHack::DebugCategory DBG_NAME(category); \ + } } \ + using debug::plugin::DBG_NAME(category) + +#define DBG_PRINT(category,pred,level,...) \ + if pred(!DFHack::DBG_NAME(category).isEnabled(level)) \ + ; /* nop fast path when debug category is disabled */ \ + else /* else to allow macro use in if-else branches */ \ + DFHack::DBG_NAME(category).getStream(level, ## __VA_ARGS__) \ +/* end of DBG_PRINT */ + +/*! + * Open a line for trace level debug output if enabled + * + * Preferred category for inside loop debug messages or callbacks/methods called + * multiple times per second. Good example would be render or onUpdate methods. + * + * \param category the debug category + * \param optional the optional second parameter is an existing + * color_ostream_proxy object + * \return color_ostream object that can be used for stream output + */ +#define TRACE(category, ...) DBG_PRINT(category, likely, \ + DFHack::DebugCategory::LTRACE, ## __VA_ARGS__) + +/*! + * Open a line for debug level debug output if enabled + * + * Preferred place to use it would be commonly called functions that don't fall + * into trace category. + * + * \param category the debug category + * \param optional the optional second parameter is an existing + * color_ostream_proxy object + * \return color_ostream object that can be used for stream output + */ +#define DEBUG(category, ...) DBG_PRINT(category, likely, \ + DFHack::DebugCategory::LDEBUG, ## __VA_ARGS__) + +/*! + * Open a line for error level debug output if enabled + * + * Important debug messages when some rarely changed state changes. Example + * would be when a debug category filtering level changes. + * + * \param category the debug category + * \param optional the optional second parameter is an existing + * color_ostream_proxy object + * \return color_ostream object that can be used for stream output + */ +#define INFO(category, ...) DBG_PRINT(category, likely, \ + DFHack::DebugCategory::LINFO, ## __VA_ARGS__) + +/*! + * Open a line for warning level debug output if enabled + * + * Warning category is for recoverable errors. This generally signals that + * something unusual happened but there is code handling the error which should + * allow df continue running without issues. + * + * \param category the debug category + * \param optional the optional second parameter is an existing + * color_ostream_proxy object + * \return color_ostream object that can be used for stream output + */ +#define WARN(category, ...) DBG_PRINT(category, unlikely, \ + DFHack::DebugCategory::LWARNING, ## __VA_ARGS__) + +/*! + * Open a line for error level error output if enabled + * + * Errors should be printed only for cases where plugin or dfhack can't recover + * from reported error and it requires manual handling from the user. + * + * \param category the debug category + * \param optional the optional second parameter is an existing + * color_ostream_proxy object + * \return color_ostream object that can be used for stream output + */ +#define ERR(category, ...) DBG_PRINT(category, unlikely, \ + DFHack::DebugCategory::LERROR, ## __VA_ARGS__) + +} diff --git a/library/include/DebugManager.h b/library/include/DebugManager.h new file mode 100644 index 000000000..fb4c635fb --- /dev/null +++ b/library/include/DebugManager.h @@ -0,0 +1,111 @@ +/** + Copyright © 2018 Pauli + + This software is provided 'as-is', without any express or implied + warranty. In no event will the authors be held liable for any + damages arising from the use of this software. + + Permission is granted to anyone to use this software for any + purpose, including commercial applications, and to alter it and + redistribute it freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must + not claim that you wrote the original software. If you use this + software in a product, an acknowledgment in the product + documentation would be appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and + must not be misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. + */ + +#pragma once + +#include "Export.h" +#include "Signal.hpp" + +#include +#include + +namespace DFHack { + +/*! \file DebugManager.h + * Expose an simple interface to runtime debug output filtering. The management + * interface is separated from output interface because output is required in + * many places while management is expected to be required only in a few places. + */ + +class DebugCategory; + +/*! + * \brief Container holding all registered runtime debug categories + * Singleton DebugManager is a minor extension to std::vector that allows signal + * callbacks to be attached from ui code that manages. + * + * To avoid parallel plugin unload causing issues access to DebugManager must be + * protected by mutex. The access mutex will be taken when + * DFHack::DebugCategory::~DebugCategory performs unregister calls to + * DFHack::DebugManager. The mutex will protect from memory disappearing while + * ui code is accessing or changing the runtime state. + * + * Signal emitting happens from a locked contexts. Taking the + * DFHack::DebugManager::access_mutex_ in a signal callback will results to a + * deadlock. + * + * The interface is extremely simple but enough to implement persistent filter + * states and runtime configuration code in a plugin. + */ +class DFHACK_EXPORT DebugManager : public std::vector { +public: + friend class DebugRegisterBase; + + //! access_mutex_ protects all readers and writers to DFHack::DebugManager + std::mutex access_mutex_; + + //! Different signals that all will be routed to + //! DebugManager::categorySignal + enum signalType { + CAT_ADD, + CAT_REMOVE, + CAT_MODIFIED, + }; + + //! type to help access signal features like Connection and BlockGuard + using categorySignal_t = Signal; + + /*! + * Signal object where callbacks can be connected. Connecting to a class + * method can use a lambda wrapper to the capture object pointer and correctly + * call required method. + * + * Signal is internally serialized allowing multiple threads call it + * freely. + */ + categorySignal_t categorySignal; + + //! Get the singleton object + static DebugManager& getInstance() { + static DebugManager instance; + return instance; + } + + //! Prevent copies + DebugManager(const DebugManager&) = delete; + //! Prevent copies + DebugManager(DebugManager&&) = delete; + //! Prevent copies + DebugManager& operator=(DebugManager) = delete; + //! Prevent copies + DebugManager& operator=(DebugManager&&) = delete; +protected: + DebugManager() = default; + + //! Helper for automatic category registering and signaling + void registerCategory(DebugCategory &); + //! Helper for automatic category unregistering and signaling + void unregisterCategory(DebugCategory &); +private: +}; +} diff --git a/library/include/Signal.hpp b/library/include/Signal.hpp new file mode 100644 index 000000000..acc8f4a0a --- /dev/null +++ b/library/include/Signal.hpp @@ -0,0 +1,782 @@ +/** +Copyright © 2018 Pauli + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must + not claim that you wrote the original software. If you use this + software in a product, an acknowledgment in the product + documentation would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and + must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source + distribution. + */ + +#pragma once + +#include + +#include +#include +#include +#include +#include + +#ifdef __SSE__ +#include +#endif + +namespace DFHack { + +/*! + * Select inline implementation for Signal members + * This requires careful destruction order where all connection has been + * disconnected before Signal::~Signal() + */ +class signal_inline_tag; +/*! + * Select share_ptr managed implementation for Signal members. + * + * If Connection holding object may be deleted without full serialization + * between disconnect and signal emit the holding object must be managed by + * shared_ptr and derive from ConnectedBase. It will also have to pass the + * std::shared_ptr to connect. + + * It uses two way std::weak_ptr reference to guarantee destruction of either + * object doesn't happen when call is made to them. + * + * It is still possible to get a callback call after manual disconnect from + * outside destructor. But without destruction risk the disconnect race can be + * handled by slot implementation side. + */ +class signal_shared_tag; + +/** + * Used for signal_shared_tag holders that may race with destructor triggered + * disconnect and emit from Signal. + */ +class ConnectedBase { +}; + +template +class Signal; + +namespace details { + +template +struct SignalImpl; + +template +struct selectImpl; + +//! Manage callback states in thread safe manner +template +class CallbackHolderImpl; + +template +struct CallbackHolderBase { + using Callback = std::function; + + CallbackHolderBase(const Callback& cb) : + cb_{cb}, + state_{} + {} + + //! Block the connection + void block() noexcept + { + state_ += blocked; + } + + //! Unblock the connection + void unblock() noexcept + { + state_ -= blocked; + } + + //! Check if connection is deleted + bool erased() const noexcept + { + return state_ & deleted; + } + + //! Check if connection is still active (not blocked or erased) + operator bool() const noexcept + { + return !(state_ & ~inCall); + } + +protected: + //! Immutable callback object + const Callback cb_; + using state_t = unsigned; + //! Single shared state as a bitfield to simplify synchronization + //! between state changes. + std::atomic state_; + static constexpr state_t deleted = 0x1 << (sizeof(state_t)*CHAR_BIT - 1); + static constexpr state_t inCall = deleted >> (sizeof(state_t)*CHAR_BIT/2); + static constexpr state_t blocked = 0x1; + static constexpr state_t blockedMask = inCall - 1; + static constexpr state_t inCallMask = (deleted - 1) ^ blockedMask; +}; + +template +class CallbackHolderImpl : + public CallbackHolderBase { + using parent_t = CallbackHolderBase; +public: + using Callback = typename parent_t::Callback; +private: + using state_t = typename parent_t::state_t; + //! Make sure callback pointed object doesn't disappear under us + //! while we call it. + struct CallGuard { + + //! Prevent copies but allow copy elision + CallGuard(const CallGuard&); + + //! Allow implicit conversion to callback for simply syntax + const Callback& operator*() const noexcept + { + return holder_->cb_; + } + + operator bool() const noexcept + { + return *holder_; + } + + //! Mark call not to be called any more + ~CallGuard() { + holder_->state_ -= parent_t::inCall; + } + private: + //! Reference to the connection + CallbackHolderImpl* holder_; + + //! Mark call to be in process + CallGuard(CallbackHolderImpl* holder) : + holder_{holder} + { + holder_->state_ += parent_t::inCall; + } + //! Only allow construction from the CallbackHolderImpl::prepareCall + friend class CallbackHolderImpl; + }; +public: + //! Construct the callback state for a callback + CallbackHolderImpl(const Callback& cb) : + parent_t{cb} + {} + + /*! + * Data race free disconnection for the connection. It spins until + * no more callers to wait. Spinning should be problem as callbacks + * are expected to be simple and fast to execute. + * + * Must not be called from withing callback! + * + * \todo Maybe use monitor instruction to avoid busy wait and call + * std::thread::yield() if wait is longer than expected. + */ + void erase() noexcept + { + state_t oldstate; + state_t newstate; + /** Spin until no callers to this callback */ +spin: + while ((oldstate = parent_t::state_) & parent_t::inCallMask) { + // pause would be portable to all old processors but there + // isn't portable way to generate it without SSE header. +#ifdef __SSE__ + _mm_pause(); +#endif + } + do { + if (oldstate & parent_t::inCallMask) + goto spin; + newstate = oldstate | parent_t::deleted; + } while(!parent_t::state_.compare_exchange_weak(oldstate, newstate)); + } + + //! Return RAII CallGuard to protect race between callback and + //! disconnect. + CallGuard prepareCall() + { + return {this}; + } +}; + +template +class CallbackHolderImpl : + public CallbackHolderBase { + using parent_t = CallbackHolderBase; +public: + using Callback = typename parent_t::Callback; +private: + using state_t = typename parent_t::state_t; + //! Make sure callback pointed object doesn't disappear under us + //! while we call it. + struct CallGuard { + + //! Prevent copies but allow copy elision + CallGuard(const CallGuard&); + + //! Allow implicit conversion to callback for simply syntax + const Callback& operator*() const noexcept + { + return holder_->cb_; + } + + operator bool() const noexcept + { + // If this is not marked erased then weak_ref->lock succeeded or + // the slot isn't managed by shared_ptr + return *holder_; + } + + private: + //! Reference to the connection + CallbackHolderImpl* holder_; + std::shared_ptr strong_ref_; + + //! Mark call to be in process + CallGuard(CallbackHolderImpl* holder) : + holder_{holder}, + strong_ref_{holder->weak_ref_.lock()} + { + } + //! Only allow construction from the CallbackHolderImpl::prepareCall + friend class CallbackHolderImpl; + }; + + std::weak_ptr weak_ref_; + friend CallGuard; +public: + //! Construct the callback state for an automatically synchronized object + CallbackHolderImpl(const Callback& cb, + std::shared_ptr& ref) : + parent_t{cb}, + weak_ref_{ref} + {} + + //! Construct the callback state for an externally synchronized object + CallbackHolderImpl(const Callback& cb) : + parent_t{cb}, + weak_ref_{} + {} + + /*! + * erase from destructor can't happen while we are in call because + */ + void erase() noexcept + { + parent_t::state_ |= parent_t::deleted; + } + + //! Return RAII CallGuard to protect race between callback and + //! disconnect. + CallGuard prepareCall() + { + return {this}; + } +}; + +template +struct SignalImpl : public selectImpl::parent_t { +protected: + using select_t = selectImpl; + using parent_t = typename select_t::parent_t; +public: + using CallbackHolder = CallbackHolderImpl; + using Callback = typename CallbackHolder::Callback; + + //! The container type used to store callbacks + using CallbackContainer = std::list; + struct BlockGuard; + + //! Simple connection class that is required to disconnect from the + //! signal. + struct Connection { + //! Construct a default Connection object but using it will result + //! to undefined behavior unless proper connection is assigned to it + Connection() = default; + + Connection(Connection&& o) : + iter_{o.iter_}, + signal_{} + { + std::swap(signal_, o.signal_); + } + + Connection& operator=(Connection&& o) + { + disconnect(); + iter_ = o.iter_; + std::swap(signal_, o.signal_); + return *this; + } + + Connection(const Connection&) = delete; + Connection& operator=(const Connection&) = delete; + + //! Disconnect from signal + void disconnect() + { + auto s = select_t::lock(signal_); + if (!s) + return; + + s->disconnect(*this); + } + + ~Connection() + { + disconnect(); + } + private: + //! Block the connection temporary + void block() + { + auto s = select_t::lock(signal_); + if (!s) + return; + iter_->block(); + } + + //! Restore blocked connection + void unblock() + { + auto s = select_t::lock(signal_); + if (!s) + return; + iter_->unblock(); + } + + //! Construct connection object + Connection(const typename CallbackContainer::iterator &iter, + typename select_t::weak_ptr ptr) : + iter_{iter}, + signal_{ptr} + {} + + //! std::list iterator that is used to access the callback and allow + //! removal from the list. + typename CallbackContainer::iterator iter_; + //! Reference to signal object + typename select_t::weak_ptr signal_; + friend SignalImpl; + friend BlockGuard; + }; + + /*! + * BlockGuard allows temporary RAII guard managed blocking of a + * connection object. + */ + struct BlockGuard { + /*! + * Block a connection that belongs to signal + * \param connection The connection that will be temporary blocked + */ + BlockGuard(Connection& connection) : + blocked_{&connection} + { + connection.block(); + } + + /*! + * Unblock the temporary blocked connection + */ + ~BlockGuard() + { + blocked_->unblock(); + } + + //! Prevent copies but allow copy elision + BlockGuard(const BlockGuard&); + private: + Connection* blocked_; + }; + + Connection connect(const Callback& f) + { + std::lock_guard lock(access_); + auto iter = callbacks_.emplace(callbacks_.begin(), f); + return {iter, parent_t::shared_from_this()}; + } + + Connection connect(std::shared_ptr c, const Callback& f) + { + std::lock_guard lock(access_); + auto iter = callbacks_.emplace(callbacks_.begin(), f, c); + return {iter, parent_t::shared_from_this()}; + } + + void disconnect(Connection& connection) { + std::lock_guard lock(access_); + if (recursion_) { + deleted_ = true; + connection.iter_->erase(); + } else { + callbacks_.erase(connection.iter_); + } + select_t::reset(connection.signal_); + } + + template + void operator()(Combiner &combiner, Args&&... arg) + { + std::unique_lock lock(access_); + struct RecursionGuard { + SignalImpl* signal_; + std::unique_lock* lock_; + //! Increment access count to make sure disconnect doesn't erase + RecursionGuard(SignalImpl *signal, std::unique_lock* lock) : + signal_{signal}, + lock_{lock} + { + ++signal_->recursion_; + } + + /*! + * Clean up deleted functions in data race free and exception + * safe manner. + */ + ~RecursionGuard() + { + lock_->lock(); + if (--signal_->recursion_ == 0 && signal_->deleted_) { + for (auto iter = signal_->callbacks_.begin(); iter != signal_->callbacks_.end();) { + if (iter->erased()) + iter = signal_->callbacks_.erase(iter); + else + ++iter; + } + signal_->deleted_ = false; + } + } + + } guard{this, &lock}; + // Call begin in locked context to allow data race free iteration + // even if there is parallel inserts to the begin after unlocking. + auto iter = callbacks_.begin(); + lock.unlock(); + for (; iter != callbacks_.end(); ++iter) { + // Quickly skip blocked calls without memory writes + if (!*iter) + continue; + // Protect connection from deletion while we are about to call + // it. + auto cb = iter->prepareCall(); + if (cb) + combiner(*cb, std::forward(arg)...); + } + } + + void operator()(Args&&... arg) + { + auto combiner = [](const Callback& cb, Args&&... arg2) + { + cb(std::forward(arg2)...); + }; + (*this)(combiner,std::forward(arg)...); + } + + ~SignalImpl() { + // Check that callbacks are empty. If this triggers then signal may + // have to be extended to allow automatic disconnection of active + // connections in the destructor. + if (std::is_same::value) + assert(callbacks_.empty() && "It is very likely that this signal should use signal_shared_tag"); + } + + //! Simplify access to pimpl when it is inline + SignalImpl* operator->() { + return this; + } + SignalImpl& operator*() { + return *this; + } + + SignalImpl() = default; +private: + SignalImpl(const SignalImpl&) : + SignalImpl{} + {} + std::mutex access_; + CallbackContainer callbacks_; + int recursion_; + bool deleted_; + friend Signal; +}; + +template +struct selectImpl { + using impl_t = SignalImpl; + using interface_t = Signal; + using type = impl_t; + using weak_ptr = impl_t*; + struct ptr_from_this { + weak_ptr shared_from_this() + { + return static_cast(this); + } + }; + using parent_t = ptr_from_this; + + selectImpl() = default; + + // Disallow copies for inline version. + selectImpl(const selectImpl&) = delete; + selectImpl(selectImpl&&) = delete; + selectImpl& operator=(const selectImpl&) = delete; + selectImpl& operator=(selectImpl&&) = delete; + + + static type make() { + return {}; + } + + static void reset(weak_ptr& ptr) { + ptr = nullptr; + } + + static weak_ptr lock(weak_ptr& ptr) { + return ptr; + } + + static weak_ptr get(interface_t& signal) { + return &signal.pimpl; + } +}; + +template +struct selectImpl { + using impl_t = SignalImpl; + using interface_t = Signal; + using type = std::shared_ptr; + using weak_ptr = std::weak_ptr; + using parent_t = std::enable_shared_from_this; + + // Allow copies for shared version + + static type make() { + return std::make_shared>(); + } + + static void reset(weak_ptr& ptr) { + ptr.reset(); + } + + static type lock(weak_ptr& ptr) { + return ptr.lock(); + } + + static weak_ptr get(interface_t& signal) { + return signal.pimpl; + } +}; +} + +/*! + * As I couldn't figure out which signal library would be a good. Too bad all + * signal libraries seem to be either heavy with unnecessary features or written + * before C++11/14 have become useable targets. That seems to indicate everyone + * is now building signal system with standard components. + * + * Implementation and interface is build around std::function holding delegates + * to a function pointer or a functor. One can put there example lambda function + * that captures this pointer from connect side. The lambda function then calls + * the slot method of object correctly. + * + * It is fairly simple to change the signal signature to directly call methods + * but internally that std::function becomes more complex. The pointer to + * member function is problematic because multiple inheritance requires + * adjustments to this. The lambda capture approach should be easy to use while + * letting compiler optimize method call in the callee side. + * + * DFHack::Signal::Connection is an connection handle. The handle can be used to + * disconnect and block a callback. Connection destructor will automatically + * disconnect from the signal. + * + * DFHack::Signal::BlockGuard is an automatic blocked callback guard object. It + * prevents any signals from calling the slot as long the BlockGuard object is + * alive. Internally it replaces the callback with an empty callback and stores + * the real callback in a member variable. Destructor then puts back the real + * callback. This allows easily recursive BlockGuard work correctly because only + * the first BlockGuard has the real callback. + * + * signal_inline_tag requires careful destruction order where all connection are + * disconnected before signal destruction. The implementation is specifically + * targeting places like static and singleton variables and widget hierarchies. + * It provides data race free connect, disconnect and emit operations. + * + * signal_shared_tag allows a bit more freedom when destroying the Signal. It + * adds data race safety between Connection, BlockGuard and destructor. If + * multiple callers need access to Signal with potential of destruction of + * original owner then callers can use Signal copy constructor to take a strong + * reference managed by shared_ptr or weak_ptr with Signal::weak_from_this(). + * weak_from_this returns an object that forwards call directly to + * implementation when the shared_ptr is created using Signal::lock + * + * \param RT return type is derived from a single signature template argument + * \param Args Variable argument type list that is derived from a signature + * template argument. + * \param tag The tag type which selects between shared_ptr managed pimpl and + * inline member variables. + */ +template +class Signal : protected details::selectImpl { +public: + //! Type of callable that can be connected to the signal. + using Callback = std::function; + +protected: + using select_t = details::selectImpl; + using CallbackContainer = typename select_t::impl_t::CallbackContainer; +public: + using weak_ptr = typename select_t::weak_ptr; + + /*! + * Simple connection class that is required to disconnect from the + * signal. + * \sa SignalImpl::Connection + */ + using Connection = typename select_t::impl_t::Connection; + /*! + * BlockGuard allows temporary RAII guard managed blocking of a + * connection object. + * \sa SignalImpl::BlockGuard + */ + using BlockGuard = typename select_t::impl_t::BlockGuard; + + /*! + * Connect a callback function to the signal + * + * Safe to call from any context as long as SignalImpl destructor can't be + * called simultaneously from other thread. + * + * \param f callable that will connected to the signal + * \return connection handle that can be used to disconnect it + */ + Connection connect(const Callback& f) + { + return pimpl->connect(f); + } + + /*! + * Thread safe connect variant connection and Connected object destruction + * can't race with emit from different threads. + * + * Safe to call from any context as long as SignalImpl destructor can't be + * called simultaneously from other thread. + */ + Connection connect(std::shared_ptr c, const Callback& f) + { + static_assert(std::is_same::value, + "Race free destruction is only possible with signal_shared_tag"); + return pimpl->connect(c, f); + } + + /*! + * Disconnection a callback from slots + * + * signal_inline_tag: + * This may not be called if the callback has been called in same + * thread. If callback should trigger destruction an object then + * deletion must use deferred. This rule prevents issues if other thread + * are trying to call the callback when disconnecting. + * + * signal_shared_tag: + * disconnect can be freely called from anywhere as long as caller holds a + * strong reference to the Signal. Strong reference can be obtained by using + * Connection::disconnect, Signal copy constructor to have a copy of signal + * or weak_ptr from weak_from_this() passed to Signal::lock(). + * + * \param connection the object returned from DFHack::Signal::connect + */ + void disconnect(Connection& connection) + { + pimpl->disconnect(connection); + } + + /*! + * Call all connected callbacks using passed arguments. + * + * signal_inline_tag: + * Must not call operator() from callbacks. + * Must not disconnect called callback from inside callback. Solution often + * is to set just atomic state variables in callback and do actual + * processing including deletion in update handler or logic vmethod. + * + * signal_shared_tag: + * Safe to call from any context as long as SignalImpl destructor can't be + * called simultaneously from other thread. + * Safe to disconnect any connection from callbacks. + * + * \param combiner that calls callbacks and processes return values + * \param arg arguments list defined by template parameter signature. + */ + template + void operator()(Combiner &combiner, Args&&... arg) + { + (*pimpl)(combiner, std::forward(arg)...); + } + + /*! + * Call all connected callbacks using passed arguments. + * + * signal_inline_tag: + * Must not call operator() from callbacks. + * Must not disconnect called callback from inside callback. Solution often + * is to set just atomic state variables in callback and do actual + * processing including deletion in update handler or logic vmethod. + * + * signal_shared_tag: + * Safe to call from any context as long as SignalImpl destructor can't be + * called simultaneously from other thread. + * Safe to disconnect any connection from callbacks. + * + * \param arg arguments list defined by template parameter signature. + */ + void operator()(Args&&... arg) + { + (*pimpl)(std::forward(arg)...); + } + + /*! + * Helper to lock the weak_ptr + */ + static typename select_t::type lock(weak_ptr& ptr) + { + return select_t::lock(ptr); + } + + /*! + * Helper to create a weak reference to pimpl which can be used to access + * pimpl directly. If the tag is signal_shared_tag then it provides race + * free access to Signal when using Signal::lock and checking returned + * shared_ptr. + */ + weak_ptr weak_from_this() noexcept + { + return select_t::get(*this); + } + + Signal() : + pimpl{select_t::make()} + {} +private: + typename select_t::type pimpl; + friend select_t; +}; +} From 8a3a05de242fdede47d832b4e453d7bec8a734fe Mon Sep 17 00:00:00 2001 From: Pauli Date: Sun, 10 Jun 2018 10:17:23 +0300 Subject: [PATCH 02/32] Allow unloading plugins that use std::regex --- library/CMakeLists.txt | 1 + library/CompilerWorkAround.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 library/CompilerWorkAround.cpp diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 416e42c41..d3bff72df 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -57,6 +57,7 @@ include/wdirent.h SET(MAIN_SOURCES Core.cpp ColorText.cpp +CompilerWorkAround.cpp DataDefs.cpp Debug.cpp Error.cpp diff --git a/library/CompilerWorkAround.cpp b/library/CompilerWorkAround.cpp new file mode 100644 index 000000000..402553bd8 --- /dev/null +++ b/library/CompilerWorkAround.cpp @@ -0,0 +1,32 @@ +#include + +namespace DFHack { +namespace neverCalled { + +/** + * gcc/linstdc++ seems to generate code that links libstdc++ back to first + * shared object using std::regex. To allow plugins unload with std::regex in + * the code we need the std::regex functions inside libdfhack.so. + * + * If your plugin decides to use any overloads that aren't listed here it may + * stay in memory after dlclose. + */ +std::regex stdRegexPluginUnloadWorkaround() +{ + std::regex fake("foo"); + std::string haystack("bar is foo in the world"); + std::regex fake2(std::string("bar")); + if (std::regex_match(haystack, fake)) + std::swap(fake, fake2); + if (std::regex_search(haystack, fake)) + std::swap(fake, fake2); + const char* haystack2 = "foo"; + if (std::regex_match(haystack2, fake)) + std::swap(fake, fake2); + if (std::regex_search(haystack2, fake)) + std::swap(fake, fake2); + return fake; +} + +} +} From 9cfb07f4760f7d99cc95109c06c44be8669bb731 Mon Sep 17 00:00:00 2001 From: Pauli Date: Sun, 10 Jun 2018 10:18:39 +0300 Subject: [PATCH 03/32] Add debug plugin to manage runtime debug filters --- plugins/CMakeLists.txt | 1 + plugins/debug.cpp | 1139 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1140 insertions(+) create mode 100644 plugins/debug.cpp diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index fbf458c4f..a05d680ea 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -107,6 +107,7 @@ if (BUILD_SUPPORTED) DFHACK_PLUGIN(cursecheck cursecheck.cpp) DFHACK_PLUGIN(cxxrandom cxxrandom.cpp LINK_LIBRARIES lua) DFHACK_PLUGIN(deramp deramp.cpp) + DFHACK_PLUGIN(debug debug.cpp LINK_LIBRARIES jsoncpp_lib_static) DFHACK_PLUGIN(dig dig.cpp) DFHACK_PLUGIN(digFlood digFlood.cpp) add_subdirectory(diggingInvaders) diff --git a/plugins/debug.cpp b/plugins/debug.cpp new file mode 100644 index 000000000..9464305dd --- /dev/null +++ b/plugins/debug.cpp @@ -0,0 +1,1139 @@ +/** +Copyright © 2018 Pauli + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any +damages arising from the use of this software. + +Permission is granted to anyone to use this software for any +purpose, including commercial applications, and to alter it and +redistribute it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must + not claim that you wrote the original software. If you use this + software in a product, an acknowledgment in the product + documentation would be appreciated but is not required. + +2. Altered source versions must be plainly marked as such, and + must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any source + distribution. + */ + +#include "Core.h" +#include "PluginManager.h" +#include "DebugManager.h" +#include "Debug.h" +#include "modules/Filesystem.h" + +#include + +#include +#include +#include +#include +#include +#include +#include + +DFHACK_PLUGIN("debug"); + +namespace DFHack { +DBG_DECLARE(debug,filter); +DBG_DECLARE(debug,init); +DBG_DECLARE(debug,command); +DBG_DECLARE(debug,ui); +} + +namespace serialization { + +template +struct nvp : public std::pair { + using parent_t = std::pair; + nvp(const char* name, T& value) : + parent_t{name, &value} + {} +}; + +template +nvp make_nvp(const char* name, T& value) { + return {name, value}; +} + +} + +#define NVP(variable) serialization::make_nvp(#variable, variable) + +namespace Json { +template +typename std::enable_if::value, ET>::type +get(Json::Value& ar, const std::string &key, const ET& default_) +{ + return static_cast(as(ar.get(key, static_cast(default_)))); +} +} + +namespace DFHack { namespace debugPlugin { + +using JsonArchive = Json::Value; + +//! Write a named and type value to Json::Value. enable_if makes sure this is +//! only available for types that Json::Value supports directly. +template::value, int>::type = 0> +JsonArchive& operator<<(JsonArchive& ar, const serialization::nvp& target) +{ + ar[target.first] = *target.second; + return ar; +} + +//! Read a named and typed value from Json::Value +template +JsonArchive& operator>>(JsonArchive& ar, const serialization::nvp& target) +{ + *target.second = Json::get(ar, target.first, T{}); + return ar; +} + +/*! + * Default regex flags optimized for matching speed because objects are stored + * long time in memory. + */ +static constexpr auto defaultRegex = + std::regex::optimize | std::regex::nosubs | std::regex::collate; + +static const char* const commandHelp = + " Manage runtime debug print filters.\n" + "\n" + " debugfilter category [ []]\n" + " List categories matching regular expressions.\n" + " debugfilter filter []\n" + " List active filters or show detailed information for a filter.\n" + " debugfilter set [persistent] [ []]\n" + " Set a filter level to categories matching regular expressions.\n" + " debugfilter unset [ ...]\n" + " Unset filters matching space separated list of ids from 'filter'.\n" + " debugfilter disable [ ...]\n" + " Disable filters matching space separated list of ids from 'filter'.\n" + " debugfilter enable [ ...]\n" + " Enable filters matching space separated list of ids from 'filter'.\n" + " debugfilter help []\n" + " Show detailed help for a command or this help.\n"; +static const char* const commandCategory = + " category [ []]\n" + " List categories with optional filters. Parameters are passed to\n" + " std::regex to limit which once are shown. The first regular\n" + " expression is used to match category and the second is used match\n" + " plugin name.\n"; +static const char* const commandSet = + " set [persistent] [ []]\n" + " Set filtering level for matching categories. 'level' must be one of\n" + " trace, debug, info, warning and error. The 'level' parameter sets\n" + " the lowest message level that will be shown. The command doesn't\n" + " allow filters to disable any error messages.\n" + " Default filter life time is until Dwarf Fortress process exists or\n" + " plugin is unloaded. Passing 'persistent' as second parameter tells\n" + " the plugin to store the filter to dfhack-config. Stored filters\n" + " will be active until always when the plugin is loaded. 'unset'\n" + " command can be used to remove persistent filters.\n" + " Filters are applied FIFO order. The latest filter will override any\n" + " older filter that also matches.\n"; +static const char* const commandFilters = + " filter []\n" + " Show the list of active filters. The first column is 'id' which can\n" + " be used to deactivate filters using 'unset' command.\n" + " Filters are printed in same order as applied - the oldest first.\n"; +static const char* const commandUnset = + " unset [ ...]\n" + " 'unset' takes space separated list of filter ids from 'filter'.\n" + " It will reset any matching category back to the default 'warning'\n" + " level or any other still active matching filter level.\n"; +static const char* const commandDisable = + " disable [ ...]\n" + " 'disable' takes space separated list of filter ids from 'filter'.\n" + " It will reset any matching category back to the default 'warning'\n" + " level or any other still active matching filter level.\n" + " 'disable' will print red filters that were already disabled.\n"; +static const char* const commandEnable = + " enable [ ...]\n" + " 'enable' takes space separated list of filter ids from 'filter'.\n" + " It will reset any matching category back to the default 'warning'\n" + " level or any other still active matching filter level.\n" + " 'enable' will print red filters that were already enabled.\n"; +static const char* const commandHelpDetails = + " help []\n" + " Show help for any of subcommands. Without any parameters it shows\n" + " short help for all subcommands.\n"; + +//! Helper type to hold static dispatch table for subcommands +struct CommandDispatch { + //! Store handler function pointer and help message for commands + struct Command { + using handler_t = command_result(*)(color_ostream&,std::vector&); + Command(handler_t handler, const char* help) : + handler_(handler), + help_(help) + {} + + command_result operator()(color_ostream& out, + std::vector& parameters) const noexcept + { + return handler_(out, parameters); + } + + handler_t handler() const noexcept + { return handler_; } + + const char* help() const noexcept + { return help_; } + private: + handler_t handler_; + const char* help_; + }; + using dispatch_t = const std::map; + //! Name to handler function and help message mapping + static dispatch_t dispatch; +}; + +struct LevelName { + static constexpr auto regex_opt = std::regex::icase | std::regex::optimize | std::regex::nosubs; + LevelName(const std::string& name) : + name_{name}, + match_{name, regex_opt} + {} + + bool match(const std::string& value) const + { + return std::regex_match(value, match_); + } + + operator const std::string&() const noexcept + { + return name_; + } + + const std::string& str() const noexcept + { + return name_; + } + + template + std::string operator+(const T& v) const + { + return name_ + v; + } +private: + std::string name_; + std::regex match_; +}; + +std::string operator+(const std::string& a, const LevelName& b) +{ + return a + static_cast(b); +} + +//! List of DebugCategory::level's in human readable form +static const std::array levelNames{ + LevelName{"Trace"}, + LevelName{"Debug"}, + LevelName{"Info"}, + LevelName{"Warning"}, + LevelName{"Error"}, +}; + +/*! + * Filter applies a runtime filter to matching DFHack::DebugCategory 's. + * Filters are stored in DFHack::debugPlugin::FilterManager which applies + * filters to dynamically added categories. The manager also stores and loads + * persistent Filters from the configuration file. + */ +struct Filter { + explicit Filter(DebugCategory::level level, + const std::string& categoryText, + const std::regex& category, + const std::string& pluginText, + const std::regex& plugin, + bool persistent = true, + bool enabled = true) noexcept; + + explicit Filter(DebugCategory::level level, + const std::string& categoryText, + const std::string& pluginText, + bool persistent = true, + bool enabled = true); + + explicit Filter() = default; + + //! Apply the filter to a category if regex's match + void apply(DFHack::DebugCategory& cat) noexcept; + //! Apply the filter to a category that has been already matched + bool applyAgain(DFHack::DebugCategory& cat) const noexcept; + //! Remove the category from matching count if regex's match + //! \return true if regex's matched + bool remove(const DFHack::DebugCategory& cat) noexcept; + + //! Query if filter is enabled + bool enabled() const noexcept {return enabled_;} + //! Set the enable status of filter + void enabled(bool enable) noexcept; + //! Query if filter is persistent + bool persistent() const noexcept {return persistent_;} + //! Query if the filter level + bool level() const noexcept {return level_;} + //! Query number of matching categories + size_t matches() const noexcept {return matches_;} + //! Add matches count for the initial category filter matching + void addMatch() noexcept {++matches_;} + //! Return the category filter text + const std::string& categoryText() const noexcept {return categoryText_;} + //! Return the plugin filter text + const std::string& pluginText() const noexcept {return pluginText_;} + + //! Load Filter from configuration file. Second parameter would be version + //! number if format changes in future to include more fields. Then new + //! fields would have to be loaded conditionally + void load(JsonArchive& ar, const unsigned int) + { + ar >> NVP(categoryText_) + >> NVP(pluginText_) + >> NVP(enabled_) + >> NVP(level_); + TRACE(filter) << "Loading filter cat: " << categoryText_ + << " plug: " << pluginText_ + << " ena " << enabled_ + << " level: " << level_ + << std::endl; + persistent_ = true; + matches_ = 0; + category_ = std::regex{categoryText_}; + plugin_ = std::regex{pluginText_}; + } + + //! Save the Filter to json configuration file + void save(JsonArchive& ar, const unsigned int) const + { + ar << NVP(categoryText_) + << NVP(pluginText_) + << NVP(enabled_) + << NVP(level_); + } + +private: + std::regex category_; + std::regex plugin_; + DebugCategory::level level_; + size_t matches_; + bool persistent_; + bool enabled_; + std::string categoryText_; + std::string pluginText_; +}; + +Filter::Filter(DebugCategory::level level, + const std::string& categoryText, + const std::regex& category, + const std::string& pluginText, + const std::regex& plugin, + bool persistent, + bool enabled) noexcept : + category_{category}, + plugin_{plugin}, + level_{level}, + matches_{0}, + persistent_{persistent}, + enabled_{enabled}, + categoryText_{categoryText}, + pluginText_{pluginText} +{} + +Filter::Filter(DebugCategory::level level, + const std::string& categoryText, + const std::string& pluginText, + bool persistent, + bool enabled) : + Filter{ + level, + categoryText, + std::regex{categoryText, defaultRegex}, + pluginText, + std::regex{pluginText, defaultRegex}, + persistent, + enabled, + } +{} + +void Filter::enabled(bool enable) noexcept +{ + if (enable == enabled_) + return; + enabled_ = enable; +} + +bool Filter::applyAgain(DebugCategory& cat) const noexcept +{ + if (!enabled_) + return false; + if (!std::regex_search(cat.category(), category_)) + return false; + if (!std::regex_search(cat.category(), plugin_)) + return false; + TRACE(filter) << "apply " << cat.plugin() << ':' << cat.category() << " matches '" + << pluginText() << "' '" << categoryText() << '\'' << std::endl; + cat.allowed(level_); + return true; +} + +void Filter::apply(DebugCategory& cat) noexcept +{ + if (applyAgain(cat)) + ++matches_; +} + +bool Filter::remove(const DebugCategory& cat) noexcept +{ + if (!enabled_) + return false; + if (!std::regex_search(cat.category(), category_)) + return false; + if (!std::regex_search(cat.category(), plugin_)) + return false; + TRACE(filter) << "remove " << cat.plugin() << ':' << cat.category() << " matches '" + << pluginText() << "' '" << categoryText() << std::endl; + --matches_; + return true; +} + +/** + * Storage for enabled and disabled filters. It uses ordered map because common + * case is to iterate filters from oldest to the newest. + * + * Data races: any reader and writer must lock + * DFHack::DebugManager::access_lock_. Sharing DebugManager make sense because + * most of functionality related to filters requires locking DebugManager too. + * Remaining functionality can share same lock because they are rarely called + * functions. + */ +struct FilterManager : public std::map +{ + using parent_t = std::map; + //! Current configuration version implemented by the code + constexpr static Json::UInt configVersion{1}; + //! Path to the configuration file + constexpr static const char* configPath{"dfhack-config/runtime-debug.json"}; + + //! Get reference to the singleton + static FilterManager& getInstance() noexcept + { + static FilterManager instance; + return instance; + } + + //! Add a new filter which gets automatically a new id + template + std::pair emplaceNew( Args&&... args ) { + return emplace(std::piecewise_construct, + std::forward_as_tuple(nextId_++), + std::forward_as_tuple(std::forward(args)...)); + } + + //! Load state from the configuration file + DFHack::command_result loadConfig(DFHack::color_ostream& out) noexcept; + //! Save state to the configuration file + DFHack::command_result saveConfig(DFHack::color_ostream& out) const noexcept; + + //! Connect FilterManager to DFHack::DebugManager::categorySignal + void connectTo(DebugManager::categorySignal_t& signal) noexcept; + //! Temporary block DFHack::DebugManager::categorySignal + DebugManager::categorySignal_t::BlockGuard blockSlot() noexcept; + + ~FilterManager(); + + FilterManager(const FilterManager&) = delete; + FilterManager(FilterManager&&) = delete; + FilterManager& operator=(FilterManager) = delete; + FilterManager& operator=(FilterManager&&) = delete; + + void load(JsonArchive& ar) + { + Json::UInt version = -1; + ar >> serialization::make_nvp("configVersion", version); + if (version > configVersion) { + std::stringstream ss; + ss << "The saved config version (" + << version + << ") is newer than code supported version (" + << configVersion + << ")"; + throw std::runtime_error{ss.str()}; + } + ar >> NVP(nextId_); + JsonArchive& children = ar["filters"]; + for (auto iter = children.begin(); iter != children.end(); ++iter) { + Filter child; + child.load(*iter, version); + std::stringstream ss(iter.name()); + size_t id; + ss >> id; + insert(std::make_pair(id, child)); + } + } + + void save(JsonArchive& ar) const + { + JsonArchive children{Json::objectValue}; + for (const auto& filterPair: *this) { + if (!filterPair.second.persistent()) + continue; + std::stringstream ss; + ss << filterPair.first; + filterPair.second.save(children[ss.str()], configVersion); + } + auto configVersion = FilterManager::configVersion; + ar << NVP(configVersion) + << NVP(nextId_); + ar["filters"] = children; + } + +private: + FilterManager() = default; + //! The next integer to use as id to have each with unique number + Json::UInt64 nextId_; + DebugManager::categorySignal_t::Connection connection_; +}; + +FilterManager::~FilterManager() +{ +} + +command_result FilterManager::loadConfig(DFHack::color_ostream& out) noexcept +{ + nextId_ = 1; + if (!Filesystem::isfile(configPath)) + return CR_OK; + try { + DEBUG(command, out) << "Load config from '" << configPath << "'" << std::endl; + JsonArchive archive; + std::ifstream ifs(configPath); + if (!ifs.good()) + throw std::runtime_error{"Failed to open configuration file for reading"}; + ifs >> archive; + load(archive); + } catch(std::exception& e) { + ERR(command, out) << "Serializing filters from '" << configPath << "' failed: " + << e.what() << std::endl; + return CR_FAILURE; + } + return CR_OK; +} + +command_result FilterManager::saveConfig(DFHack::color_ostream& out) const noexcept +{ + try { + DEBUG(command, out) << "Save config to '" << configPath << "'" << std::endl; + JsonArchive archive; + save(archive); + std::ofstream ofs(configPath); + if (!ofs.good()) + throw std::runtime_error{"Failed to open configuration file for writing"}; + ofs << archive; + } catch(std::exception e) { + ERR(command, out) << "Serializing filters to '" << configPath << "' failed: " + << e.what() << std::endl; + return CR_FAILURE; + } + return CR_OK; +} + +void FilterManager::connectTo(DebugManager::categorySignal_t& signal) noexcept +{ + connection_ = signal.connect( + [this](DebugManager::signalType t, DebugCategory& cat) { + TRACE(filter) << "sig type: " << t << ' ' + << cat.plugin() << ':' + << cat.category() << std::endl; + switch (t) { + case DebugManager::CAT_ADD: + for (auto& filterPair: *this) + filterPair.second.apply(cat); + break; + case DebugManager::CAT_REMOVE: + for (auto& filterPair: *this) + filterPair.second.remove(cat); + break; + case DebugManager::CAT_MODIFIED: + break; + } + }); +} + +DebugManager::categorySignal_t::BlockGuard FilterManager::blockSlot() noexcept +{ + TRACE(filter) << "Temporary disable FilterManager::connection_" << std::endl; + return {connection_}; +} + +//! \brief Helper to parse optional regex string safely +static command_result parseRegexParam(std::regex& target, + color_ostream& out, + std::vector& parameters, + size_t pos) +{ + if (parameters.size() <= pos) + return CR_OK; + try { + std::regex temp{parameters[pos], defaultRegex}; + target = std::move(temp); + } catch(std::regex_error e) { + ERR(command,out) << "Failed to parse regular expression '" + << parameters[pos] << "'\n"; + ERR(command,out) << "Parser message: " << e.what() << std::endl; + return CR_WRONG_USAGE; + } + return CR_OK; +} + +/*! + * "Algorithm" to apply category filters based on optional regex parameters. + * \param out The output stream where errors should be written + * \param paramters The list of parameters for the command + * \param pos The position where first optional regular expression parameter is + * \param header The callback after locking DebugManager and before loop + * \param categoryMatch The callback for each matching category + */ +template +static command_result applyCategoryFilters(color_ostream& out, + std::vector& parameters, + size_t pos, Callable1 header, + Callable2 categoryMatch, + Callable3 listComplete) +{ + std::regex pluginRegex{".", defaultRegex}; + std::regex categoryRegex{".", defaultRegex}; + command_result rv = CR_OK; + + DEBUG(command,out) << "applying category filters '" + << (parameters.size() >= pos + 1 ? parameters[pos] : "") + << "' and plugin filter '" + << (parameters.size() >= pos + 2 ? parameters[pos+1] : "") + << '\'' << std::endl; + // Parse parameters + if ((rv = parseRegexParam(pluginRegex, out, parameters, pos)) != CR_OK) + return rv; + if ((rv = parseRegexParam(categoryRegex, out, parameters, pos+1)) != CR_OK) + return rv; + // Lock the manager to have consistent view of categories + auto& manager = DebugManager::getInstance(); + std::lock_guard lock(manager.access_mutex_); + out << std::left; + auto guard = header(manager, categoryRegex, pluginRegex); + for (auto* category: manager) { + DebugCategory::cstring_ref p = category->plugin(); + DebugCategory::cstring_ref c = category->category(); + // Apply filters to the category and plugin names + if (!std::regex_search(c, categoryRegex)) + continue; + if (!std::regex_search(p, pluginRegex)) + continue; + categoryMatch(*category); + } + out << std::flush << std::right; + out.color(COLOR_RESET); + return listComplete(); +} + +static void printCategoryListHeader(color_ostream& out) +{ + // Output the header. + out.color(COLOR_GREEN); + out << std::setw(12) << "Plugin" + << std::setw(12) << "Category" + << std::setw(18) << "Lowest printed" << '\n'; +} + +static void printCategoryListEntry(color_ostream& out, + unsigned& line, + DebugCategory& cat, + DebugCategory::level old = static_cast(-1)) +{ + if ((line & 31) == 0) + printCategoryListHeader(out); + // Output matching categories. + out.color((line++ & 1) == 0 ? COLOR_CYAN : COLOR_LIGHTCYAN); + const std::string& level = (old != static_cast(-1)) ? + levelNames[static_cast(old)] + "->" + + levelNames[static_cast(cat.allowed())] : + levelNames[static_cast(cat.allowed())].str(); + out << std::setw(12) << cat.plugin() + << std::setw(12) << cat.category() + << std::setw(18) << level << '\n'; +} + +//! Handler for debugfilter category +static command_result listCategories(color_ostream& out, + std::vector& parameters) +{ + unsigned line = 0; + return applyCategoryFilters(out, parameters, 1u, + // After parameter parsing + [](DebugManager&, const std::regex&, const std::regex&) { + return 0; + }, + // Per category + [&out, &line](DebugCategory& cat) { + printCategoryListEntry(out, line, cat); + }, + // After list + []() {return CR_OK;}); +} + +//! Type that prints parameter string in center of output stream field +template> +struct center { + using string = std::basic_string; + center(const string& str) : + str_(str) + {} + + const string& str_; +}; + +/*! + * Helper to construct a center object to print fields centered + * \code{.cpp} + * out << std::setw(20) << centered("centered"_s); + * \endcode + */ +template +center centered(const ST& str) +{ + return {str}; +} + +//! c++14 string conversion literal to std::string +std::string operator "" _s(const char* cstr, size_t len) +{ + return {cstr, len}; +} + +//! Output centered string, the stream must be using std::ios::right +//! \sa DFHack::debugPlugin::centered +template +std::basic_ostream& operator<<(std::basic_ostream& os, const center& toCenter) +{ + std::streamsize w = os.width(); + const auto& str = toCenter.str_; + mbstate_t ps{}; + std::streamsize ccount = 0; + auto iter = str.cbegin(); + // Check multibyte character length. It will break when mbrlen find the '\0' + for (;ccount < w; ++ccount) { + const size_t n = std::distance(iter, str.cend()); + using ss_t = std::make_signed::type; + ss_t bytes = mbrlen(&*iter, n, &ps); + if (bytes <= 0) /* Check for errors and null */ + break; + std::advance(iter, bytes); + } + + if (ccount < w) { + // Center the character when there is less than the width + // fillw = w - count + // cw = w - (fillw)/2 = (2w - w + count)/2 + // Extra one for rounding half results up + std::streamsize cw = (w + ccount + 1)/2; + os << std::setw(cw) << str + << std::setw(w - cw) << ""; + } else { + // Truncate characters to the width of field + os.write(&str[0], std::distance(str.begin(), iter)); + // Reset the field width because we wrote the string with write + os << std::setw(0); + } + return os; +} + +static FilterManager::iterator parseFilterId(color_ostream& out, + const std::string& parameter) +{ + unsigned long id = 0; + try { + id = std::stoul(parameter); + } catch(...) { + } + auto& filMan = FilterManager::getInstance(); + auto iter = filMan.find(id); + if (iter == filMan.end()) { + WARN(command,out) << "The optional parameter (" + << parameter << ") must be an filter id." << std::endl; + } + return iter; +} + +static void printFilterListEntry(color_ostream& out, + unsigned line, + color_value lineColor, + size_t id, + const Filter& filter) +{ + if ((line & 31) == 0) { + out.color(COLOR_GREEN); + out << std::setw(4) << "ID" + << std::setw(8) << "enabled" + << std::setw(8) << "persist" + << std::setw(9) << centered("level"_s) + << ' ' + << std::setw(15) << centered("category"_s) + << ' ' + << std::setw(15) << centered("plugin"_s) + << std::setw(8) << "matches" + << '\n'; + } + out.color(lineColor); + out << std::setw(4) << id + << std::setw(8) << centered(filter.enabled() ? "X"_s:""_s) + << std::setw(8) << centered(filter.persistent() ? "X"_s:""_s) + << std::setw(9) << centered(levelNames[filter.level()].str()) + << ' ' + << std::setw(15) << centered(filter.categoryText()) + << ' ' + << std::setw(15) << centered(filter.pluginText()) + << std::setw(8) << filter.matches() + << '\n'; +} + +//! Handler for debugfilter filter +static command_result listFilters(color_ostream& out, + std::vector& parameters) +{ + if (1u < parameters.size()) { + auto& catMan = DebugManager::getInstance(); + std::lock_guard lock(catMan.access_mutex_); + auto iter = parseFilterId(out, parameters[1]); + if (iter == FilterManager::getInstance().end()) + return CR_WRONG_USAGE; + + auto id = iter->first; + Filter& filter = iter->second; + + out << std::left + << std::setw(10) << "ID:" << id << '\n' + << std::setw(10) << "Enabled:" << (filter.enabled() ? "Yes"_s:"No"_s) << '\n' + << std::setw(10) << "Persist:" << (filter.persistent() ? "Yes"_s:"No"_s) << '\n' + << std::setw(10) << "Level:" << levelNames[filter.level()].str() << '\n' + << std::setw(10) << "category:" << filter.categoryText() << '\n' + << std::setw(10) << "plugin:" << filter.pluginText() << '\n' + << std::setw(10) << "matches:" << filter.matches() << '\n' + << std::right + << std::endl; + return CR_OK; + } + auto& catMan = DebugManager::getInstance(); + { + std::lock_guard lock(catMan.access_mutex_); + auto& filMan = FilterManager::getInstance(); + unsigned line = 0; + for (auto& filterPair: filMan) { + size_t id = filterPair.first; + const Filter& filter = filterPair.second; + + color_value c = (line & 1) == 0 ? COLOR_CYAN : COLOR_LIGHTCYAN; + printFilterListEntry(out,line++,c,id,filter); + } + } + out.color(COLOR_RESET); + out.flush(); + return CR_OK; +} + +static const std::string persistent("persistent"); + +//! Handler for debugfilter set +static command_result setFilter(color_ostream& out, + std::vector& parameters) +{ + bool persist = false; + size_t pos = 1u; + if (pos < parameters.size() && + parameters[pos] == persistent) { + pos++; + persist = true; + } + if (pos >= parameters.size()) { + ERR(command,out).print("set requires at least the level parameter\n"); + return CR_WRONG_USAGE; + } + const std::string& level = parameters[pos]; + auto iter = std::find_if(levelNames.begin(), levelNames.end(), + [&level](const LevelName& v) -> bool { + return v.match(level); + }); + if (iter == levelNames.end()) { + ERR(command,out).print("level ('%s') parameter must be one of " + "trace, debug, info, warning, error.\n", + parameters[pos].c_str()); + return CR_WRONG_USAGE; + } + + DebugCategory::level filterLevel = static_cast( + iter - levelNames.begin()); + + unsigned line = 0; + Filter* newFilter = nullptr; + return applyCategoryFilters(out, parameters, pos + 1, + // After parameters parsing + [¶meters, pos, filterLevel,persist,&newFilter](DebugManager&, const std::regex& catRegex, const std::regex& pluginRegex) + { + auto& filMan = FilterManager::getInstance(); + newFilter = &filMan.emplaceNew(filterLevel, + pos+1 < parameters.size()?parameters[pos+1]:".", + catRegex, + pos+2 < parameters.size()?parameters[pos+2]:".", + pluginRegex, + persist).first->second; + return filMan.blockSlot(); + }, + // Per item + [filterLevel, &line, &out, &newFilter](DebugCategory& cat) { + auto old = cat.allowed(); + cat.allowed(filterLevel); + newFilter->addMatch(); + printCategoryListEntry(out, line, cat, old); + }, + // After list + [&out,&persist]() { + if (persist) + return FilterManager::getInstance().saveConfig(out); + return CR_OK; + }); +} + +template +static command_result applyFilterIds(color_ostream& out, + std::vector& parameters, + const char* name, + HighlightRed hlRed, + ListComplete listComplete) +{ + if (1u >= parameters.size()) { + ERR(command,out) << name << " requires at least a filter id" << std::endl; + return CR_WRONG_USAGE; + } + command_result rv = CR_OK; + { + auto& catMan = DebugManager::getInstance(); + std::lock_guard lock(catMan.access_mutex_); + auto& filMan = FilterManager::getInstance(); + unsigned line = 0; + for (size_t pos = 1; pos < parameters.size(); ++pos) { + const std::string& p = parameters[pos]; + auto iter = parseFilterId(out, p); + if (iter == filMan.end()) + continue; + color_value c = (line & 1) == 0 ? COLOR_CYAN : COLOR_LIGHTCYAN; + if (hlRed(iter)) + c = COLOR_RED; + printFilterListEntry(out,line++,c,iter->first,iter->second); + } + rv = listComplete(); + } + out.color(COLOR_RESET); + out.flush(); + return rv; +} + +//! Handler for debugfilter disable +static command_result disableFilter(color_ostream& out, + std::vector& parameters) +{ + std::set modified; + bool mustSave = false; + return applyFilterIds(out,parameters,"disable", + // Per item + [&modified,&mustSave](FilterManager::iterator& iter) -> bool { + Filter& filter = iter->second; + bool enabled = filter.enabled(); + if (enabled == false) + return true; + auto& catMan = DebugManager::getInstance(); + for (DebugCategory* cat: catMan) { + if (filter.remove(*cat)) + modified.emplace(cat); + } + filter.enabled(false); + mustSave = mustSave || filter.persistent(); + return false; + }, + // After list + [&modified,&mustSave,&out]() { + for (DebugCategory* cat: modified) { + // Reset filtering back to default + cat->allowed(DebugCategory::LWARNING); + auto& filMan = FilterManager::getInstance(); + // Reapply all remaining filters + for (auto& filterPair: filMan) + filterPair.second.applyAgain(*cat); + } + if (mustSave) + return FilterManager::getInstance().saveConfig(out); + return CR_OK; + }); +} + +//! Handler for debugfilter enable +static command_result enableFilter(color_ostream& out, + std::vector& parameters) +{ + std::set modified; + bool mustSave = false; + return applyFilterIds(out,parameters,"enable", + // Per item + [&modified,&mustSave](FilterManager::iterator& iter) -> bool { + Filter& filter = iter->second; + bool enabled = filter.enabled(); + if (enabled == true) + return true; + filter.enabled(true); + auto& catMan = DebugManager::getInstance(); + for (DebugCategory* cat: catMan) { + if (filter.applyAgain(*cat)) { + modified.emplace(cat); + filter.addMatch(); + } + } + mustSave = mustSave || filter.persistent(); + return false; + }, + // After list + [&modified,&mustSave,&out]() { + for (DebugCategory* cat: modified) { + // Reset filtering back to default + cat->allowed(DebugCategory::LWARNING); + auto& filMan = FilterManager::getInstance(); + // Reapply all remaining filters + for (auto& filterPair: filMan) + filterPair.second.applyAgain(*cat); + } + if (mustSave) + return FilterManager::getInstance().saveConfig(out); + return CR_OK; + }); +} + +//! Handler for debugfilter unset +static command_result unsetFilter(color_ostream& out, + std::vector& parameters) +{ + std::set modified; + std::vector toErase; + return applyFilterIds(out,parameters,"unset", + // Per item + [&modified, &toErase](FilterManager::iterator& iter) -> bool { + Filter& filter = iter->second; + if (filter.enabled()) { + auto& catMan = DebugManager::getInstance(); + for (DebugCategory* cat: catMan) { + if (filter.remove(*cat)) + modified.emplace(cat); + } + } + toErase.emplace_back(iter); + return false; + }, + // After list + [&modified,&toErase,&out]() { + auto& filMan = FilterManager::getInstance(); + bool mustSave = false; + for (auto iter: toErase) { + mustSave = mustSave || iter->second.persistent(); + filMan.erase(iter); + } + + for (DebugCategory* cat: modified) { + // Reset filtering back to default + cat->allowed(DebugCategory::LWARNING); + // Reapply all remaining filters + for (auto& filterPair: filMan) + filterPair.second.applyAgain(*cat); + } + if (mustSave) + return FilterManager::getInstance().saveConfig(out); + return CR_OK; + }); +} + +using DFHack::debugPlugin::CommandDispatch; + +static command_result printHelp(color_ostream& out, + std::vector& parameters) +{ + const char* help = commandHelp; + auto iter = CommandDispatch::dispatch.end(); + if (1u < parameters.size()) + iter = CommandDispatch::dispatch.find(parameters[1]); + if (iter != CommandDispatch::dispatch.end()) + help = iter->second.help(); + out << help << std::flush; + return CR_OK; +} + +CommandDispatch::dispatch_t CommandDispatch::dispatch { + {"category", {listCategories,commandCategory}}, + {"filter", {listFilters,commandFilters}}, + {"set", {setFilter,commandSet}}, + {"unset", {unsetFilter,commandUnset}}, + {"enable", {enableFilter,commandEnable}}, + {"disable", {disableFilter,commandDisable}}, + {"help", {printHelp,commandHelpDetails}}, +}; + +//! Dispatch command handling to the subcommand or help +static command_result commandDebugFilter(color_ostream& out, + std::vector& parameters) +{ + DEBUG(command,out).print("debugfilter %s, parameter count %zu\n", + parameters.size() > 0 ? parameters[0].c_str() : "", + parameters.size()); + auto handler = printHelp; + auto iter = CommandDispatch::dispatch.end(); + if (0u < parameters.size()) + iter = CommandDispatch::dispatch.find(parameters[0]); + if (iter != CommandDispatch::dispatch.end()) + handler = iter->second.handler(); + return (handler)(out, parameters); +} + +} } /* namespace debug */ + +DFhackCExport DFHack::command_result plugin_init(DFHack::color_ostream& out, + std::vector& commands) +{ + commands.emplace_back( + "debugfilter", + "Manage runtime debug print filters", + DFHack::debugPlugin::commandDebugFilter, + false, + DFHack::debugPlugin::commandHelp); + auto& filMan = DFHack::debugPlugin::FilterManager::getInstance(); + DFHack::command_result rv = DFHack::CR_OK; + if ((rv = filMan.loadConfig(out)) != DFHack::CR_OK) + return rv; + auto& catMan = DFHack::DebugManager::getInstance(); + std::lock_guard lock(catMan.access_mutex_); + for (auto* cat: catMan) { + for (auto& filterPair: filMan) { + DFHack::debugPlugin::Filter& filter = filterPair.second; + filter.apply(*cat); + } + } + INFO(init,out).print("plugin_init with %zu commands, %zu filters and %zu categories\n", + commands.size(), filMan.size(), catMan.size()); + filMan.connectTo(catMan.categorySignal); + return rv; +} + +DFhackCExport DFHack::command_result plugin_shutdown(DFHack::color_ostream& out) +{ + INFO(init,out).print("plugin_shutdown\n"); + return DFHack::CR_OK; +} From 490a8557766bf3459ff85d8e0cb2ea941f311c24 Mon Sep 17 00:00:00 2001 From: Pauli Date: Mon, 2 Jul 2018 18:48:33 +0300 Subject: [PATCH 04/32] Add a test for signal_shared_tag implementation The test cases check that the signal_shared_tag implementation can be used and destructed safely from multiple threads. --- plugins/CMakeLists.txt | 2 + plugins/devel/CMakeLists.txt | 2 +- plugins/devel/kittens.cpp | 178 +++++++++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+), 1 deletion(-) diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index a05d680ea..07a94112c 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -1,5 +1,7 @@ INCLUDE(Plugins.cmake) +find_package(Threads) + OPTION(BUILD_STONESENSE "Build stonesense (needs a checkout first)." OFF) if(BUILD_STONESENSE) add_subdirectory (stonesense) diff --git a/plugins/devel/CMakeLists.txt b/plugins/devel/CMakeLists.txt index 4fb4b5cf5..879edd5bb 100644 --- a/plugins/devel/CMakeLists.txt +++ b/plugins/devel/CMakeLists.txt @@ -9,7 +9,7 @@ DFHACK_PLUGIN(counters counters.cpp) DFHACK_PLUGIN(dumpmats dumpmats.cpp) DFHACK_PLUGIN(eventExample eventExample.cpp) DFHACK_PLUGIN(frozen frozen.cpp) -DFHACK_PLUGIN(kittens kittens.cpp) +DFHACK_PLUGIN(kittens kittens.cpp LINK_LIBRARIES ${CMAKE_THREAD_LIBS_INIT}) DFHACK_PLUGIN(memview memview.cpp memutils.cpp LINK_LIBRARIES lua) DFHACK_PLUGIN(nestboxes nestboxes.cpp) DFHACK_PLUGIN(notes notes.cpp) diff --git a/plugins/devel/kittens.cpp b/plugins/devel/kittens.cpp index 5de94ced3..26a2655e8 100644 --- a/plugins/devel/kittens.cpp +++ b/plugins/devel/kittens.cpp @@ -1,12 +1,16 @@ #include #include +#include #include +#include #include "Console.h" #include "Core.h" +#include "Debug.h" #include "Export.h" #include "MiscUtils.h" #include "PluginManager.h" +#include "Signal.hpp" #include "modules/Gui.h" #include "modules/Items.h" @@ -25,6 +29,10 @@ DFHACK_PLUGIN_IS_ENABLED(is_enabled); REQUIRE_GLOBAL(ui); REQUIRE_GLOBAL(world); +namespace DFHack { +DBG_DECLARE(kittens,command); +} + std::atomic shutdown_flag{false}; std::atomic final_flag{true}; std::atomic timering{false}; @@ -42,6 +50,7 @@ command_result trackmenu (color_ostream &out, vector & parameters); command_result trackpos (color_ostream &out, vector & parameters); command_result trackstate (color_ostream &out, vector & parameters); command_result colormods (color_ostream &out, vector & parameters); +command_result sharedsignal (color_ostream &out, vector & parameters); DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) { @@ -51,6 +60,7 @@ DFhackCExport command_result plugin_init ( color_ostream &out, std::vector & parameters) return CR_OK; } +struct Connected; +using shared = std::shared_ptr; +using weak = std::weak_ptr; + +static constexpr std::chrono::microseconds delay{1}; + +template +struct ClearMem : public ConnectedBase { + ~ClearMem() + { + memset(reinterpret_cast(this), 0xDE, sizeof(Derived)); + } +}; + +struct Connected : public ClearMem { + using Sig = Signal; + std::array con; + Sig signal; + weak other; + Sig::weak_ptr other_sig; + color_ostream *out; + int id; + uint32_t count; + uint32_t caller; + alignas(64) std::atomic callee; + Connected() = default; + Connected(int id) : + Connected{} + { + this->id = id; + } + void connect(color_ostream& o, shared& b, size_t pos, uint32_t c) + { + out = &o; + count = c*2; + other = b; + other_sig = b->signal.weak_from_this(); + // Externally synchronized object destruction is only safe to this + // connect. + con[pos] = b->signal.connect( + [this](int) { + uint32_t old = callee.fetch_add(1); + assert(old != 0xDEDEDEDE); + std::this_thread::sleep_for(delay); + assert(callee != 0xDEDEDEDE); + }); + // Shared object managed object with possibility of destruction while + // other threads calling emit must pass the shared_ptr to connect. + Connected *bptr = b.get(); + b->con[pos] = signal.connect(b, + [bptr](int) { + uint32_t old = bptr->callee.fetch_add(1); + assert(old != 0xDEDEDEDE); + std::this_thread::sleep_for(delay); + assert(bptr->callee != 0xDEDEDEDE); + }); + } + void reconnect(size_t pos) { + auto b = other.lock(); + if (!b) + return; + // Not required to use Sig::lock because other holds strong reference to + // Signal. But this just shows how weak_ref could be used. + auto sig = Sig::lock(other_sig); + if (!sig) + return; + con[pos] = sig->connect(b, + [this](int) { + uint32_t old = callee.fetch_add(1); + assert(old != 0xDEDEDEDE); + std::this_thread::sleep_for(delay); + assert(callee != 0xDEDEDEDE); + }); + } + void connect(color_ostream& o, shared& a, shared& b,size_t pos, uint32_t c) + { + out = &o; + count = c; + con[pos] = b->signal.connect(a, + [this](int) { + uint32_t old = callee.fetch_add(1); + assert(old != 0xDEDEDEDE); + std::this_thread::sleep_for(delay); + assert(callee != 0xDEDEDEDE); + }); + } + Connected* operator->() noexcept + { + return this; + } + ~Connected() { + INFO(command,*out).print("Connected %d had %d count. " + "It was caller %d times. " + "It was callee %d times.\n", + id, count, caller, callee.load()); + } +}; + +command_result sharedsignal (color_ostream &out, vector & parameters) +{ + using rng_t = std::linear_congruential_engine; + rng_t rng(std::random_device{}()); + size_t count = 10; + if (0 < parameters.size()) { + std::stringstream ss(parameters[0]); + ss >> count; + DEBUG(command, out) << "Parsed " << count + << " from paramters[0] '" << parameters[0] << '\'' << std::endl; + } + + + std::uniform_int_distribution dis(4096,8192); + out << "Running signal_shared_tag destruction test " + << count << " times" << std::endl; + for (size_t nr = 0; nr < count; ++nr) { + std::array t{}; + // Make an object which destruction is protected by std::thread::join() + Connected external{static_cast(t.size())}; + TRACE(command, out) << "begin " << std::endl; + { + int id = 0; + // Make objects that are automatically protected using weak_ptr + // references that are promoted to shared_ptr when Signal is + // accessed. + std::array c = { + std::make_shared(id++), + std::make_shared(id++), + std::make_shared(id++), + std::make_shared(id++), + }; + assert(t.size() == c.size()); + for (unsigned i = 1; i < c.size(); ++i) { + c[0]->connect(out, c[0], c[i], i - 1, dis(rng)); + c[i]->connect(out, c[i], c[0], 0, dis(rng)); + } + external.connect(out, c[1], 1, dis(rng)); + auto thr = [&out](shared c) { + TRACE(command, out) << "Thread " << c->id << " started." << std::endl; + weak ref = c; + for (;c->caller < c->count; ++c->caller) { + c->signal(c->caller); + } + TRACE(command, out) << "Thread " << c->id << " resets shared." << std::endl; + c.reset(); + while((c = ref.lock())) { + ++c->caller; + c->signal(c->caller); + c.reset(); + std::this_thread::sleep_for(delay*25); + } + }; + for (unsigned i = 0; i < c.size(); ++i) { + TRACE(command, out) << "start thread " << i << std::endl; + t[i] = std::thread{thr, c[i]}; + } + } + TRACE(command, out) << "running " << std::endl; + for (;external->caller < external->count; ++external->caller) { + external->signal(external->caller); + external->reconnect(1); + } + TRACE(command, out) << "join " << std::endl; + for (unsigned i = 0; i < t.size(); ++i) + t[i].join(); + } + return CR_OK; +} + command_result kittens (color_ostream &out, vector & parameters) { if (parameters.size() >= 1) From c201cf5b7b3faf44e109484ebdcb84a7a0589346 Mon Sep 17 00:00:00 2001 From: Pauli Date: Wed, 4 Jul 2018 14:54:00 +0300 Subject: [PATCH 05/32] Documentation and Changelog for debug printing and Signal --- docs/Plugins.rst | 96 ++++++++++++++++++++++++++++++++++++++++++++++ docs/changelog.txt | 15 ++++++++ 2 files changed, 111 insertions(+) diff --git a/docs/Plugins.rst b/docs/Plugins.rst index 5f41bafed..bbeeb813a 100644 --- a/docs/Plugins.rst +++ b/docs/Plugins.rst @@ -392,6 +392,102 @@ Otherwise somewhat similar to `gui/quickcmd`. .. image:: images/command-prompt.png +.. _debug: + +debug +===== +Manager DFHack runtime debug prints. Debug prints are grouped by plugin name, +category name and print level. Levels are ``trace``, ``debug``, ``info``, +``warning`` and ``error``. + +The runtime message printing is controlled using filters. Filters set minimum +visible message to all matching categories. Matching uses regular expression +that allows listing multiple alternative matches or partial name matches. +Persistent filters are stored in ``dfhack-config/runtime-debug.json``. + +Oldest filters are applied first. That means a newer filter can override the +older printing level selection. + +Usage: ``debugfilter [subcommand] [parameters...]`` + +Following subcommands are supported. + +Regular expression syntax +------------------------- + +Syntax is C++ version of ECMA-262 grammar (Javascript regular expression). +Deails of differences can be found from +https://en.cppreference.com/w/cpp/regex/ecmascript + +help +---- +Give overall help or a detailed help for a subcommand. + +Usage: ``debugfilter help [subcommand]`` + +category +-------- +List available debug plugin and category names. + +Usage: ``debugfilter category [plugin regex] [category regex]`` + +The list can be filtered using optional regex parameters. If filters aren't +given then the it uses ``"."`` regex which matches any character. The regex +parameters are good way to test regex before passing them to ``set``. + +filter +------ +List active and passive debug print level changes. + +Usage: ``debugfilter filter [id]`` + +Optional ``id`` parameter is the id listed as first column in the filter list. +If id is given then the command shows information for the given filter only in +multi line format that is better format if filter has long regex. + +set +--- +Creates a new debug filter to set category printing levels. + +Usage: ``debugfilter set [level] [plugin regex] [category regex]`` + +Adds a filter that will be deleted when DF process exists or plugin is unloaded. + +Usage: ``debugfilter set persistent [level] [plugin regex] [category regex]`` + +Stores the filter in the configuration file to until ``unset`` is used to remove +it. + +Level is the minimum debug printing level to show in log. + +* ``trace``: Possibly very noisy messages which can be printed many times per second + +* ``debug``: Messages that happen often but they should happen only a couple of times per second + +* ``info``: Important state changes that happen rarely during normal execution + +* ``warining``: Enabled by default. Shows warnings about unexpected events which code managed to handle correctly. + +* ``error``: Enabled by default. Shows errors which code can't handle without user intervention. + +unset +----- +Delete a space separated list of filters + +Usage: ``debugfilter unset [id...]`` + +disable +------- +Disable a space separated list of filters but keep it in the filter list + +Usage: ``debugfilter disable [id...]`` + +enable +------ +Enable a space sperate list of filters + +Usage: ``debugfilter enable [id...]`` + .. _hotkeys: hotkeys diff --git a/docs/changelog.txt b/docs/changelog.txt index 083697b3b..3bf19b338 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -37,6 +37,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ================================================================================ # Future +## New Plugins +- `debug`: manages runtime debug print category filtering + ## Fixes - `fix/dead-units`: fixed script trying to use missing isDiplomat function @@ -56,6 +59,18 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - New functions: - ``Units::isDiplomat(unit)`` - Exposed ``Screen::zoom()`` to C++ (was Lua-only) +- New classes: + - ``Signal`` to C++ only + - ``DebugCategory`` to C++ only (used through new macros) + - ``DebugManager`` to C++ only +- New macros: + - ``DBG_DECLARE`` + - ``DBG_EXTERN`` + - ``TRACE`` + - ``DEBUG`` + - ``INFO`` + - ``WARN`` + - ``ERR`` ## Lua - ``gui.widgets``: ``List:setChoices`` clones ``choices`` for internal table changes From 143b557ad99a12a936f0c4595e149ca7ecedbb2b Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Sun, 5 Aug 2018 17:10:41 +0200 Subject: [PATCH 06/32] Added embark-assistant world match indication --- docs/changelog.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index fd53e4f4c..17ed0c25f 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -40,7 +40,9 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## Fixes - `building-hacks`: fixed error when dealing with custom animation tables - `devel/test-perlin`: fixed Lua error (``math.pow()``) -- `embark-assistant`: fixed crash when entering finder with a 16x16 embark selected, and added 16 to dimension choices +- `embark-assistant`: + - fixed crash when entering finder with a 16x16 embark selected, and added 16 to dimension choices + - added match indicator display on the right ("World") map - `labormanager`: - stopped assigning labors to ineligible dwarves, pets, etc. - stopped assigning invalid labors From 70630cfd923b3c01adcae0b5c3979507f0f18c5e Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Sun, 5 Aug 2018 17:11:26 +0200 Subject: [PATCH 07/32] Added embark-assistant world match indication --- plugins/embark-assistant/embark-assistant.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/embark-assistant/embark-assistant.cpp b/plugins/embark-assistant/embark-assistant.cpp index 728d96f58..cfd44a30b 100644 --- a/plugins/embark-assistant/embark-assistant.cpp +++ b/plugins/embark-assistant/embark-assistant.cpp @@ -143,7 +143,7 @@ DFhackCExport command_result plugin_init (color_ostream &out, std::vector Date: Sun, 5 Aug 2018 17:11:39 +0200 Subject: [PATCH 08/32] Added embark-assistant world match indication --- plugins/embark-assistant/matcher.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/embark-assistant/matcher.cpp b/plugins/embark-assistant/matcher.cpp index 28c72b76f..b2509336b 100644 --- a/plugins/embark-assistant/matcher.cpp +++ b/plugins/embark-assistant/matcher.cpp @@ -1518,11 +1518,11 @@ uint16_t embark_assist::matcher::find(embark_assist::defs::match_iterators *iter preliminary_matches = preliminary_world_match(survey_results, &iterator->finder, match_results); if (preliminary_matches == 0) { - out.printerr("matcher::find: Preliminarily matching world tiles: %i\n", preliminary_matches); + out.printerr("matcher::find: Preliminarily matching World Tiles: %i\n", preliminary_matches); return 0; } else { - out.print("matcher::find: Preliminarily matching world tiles: %i\n", preliminary_matches); + out.print("matcher::find: Preliminarily matching World Tiles: %i\n", preliminary_matches); } while (screen->location.region_pos.x != 0 || screen->location.region_pos.y != 0) { From f7fadaab377d1aab744a3327bc1359c383fdba05 Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Sun, 5 Aug 2018 17:11:47 +0200 Subject: [PATCH 09/32] Added embark-assistant world match indication --- plugins/embark-assistant/overlay.cpp | 62 ++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/plugins/embark-assistant/overlay.cpp b/plugins/embark-assistant/overlay.cpp index 19615cac5..c0f5e0c8c 100644 --- a/plugins/embark-assistant/overlay.cpp +++ b/plugins/embark-assistant/overlay.cpp @@ -48,7 +48,7 @@ namespace embark_assist { std::vector embark_info; - Screen::Pen region_match_grid[16][16]; + Screen::Pen local_match_grid[16][16]; pen_column *world_match_grid = nullptr; uint16_t match_count = 0; @@ -60,26 +60,38 @@ namespace embark_assist { //==================================================================== -/* // Attempt to replicate the DF logic for sizing the right world map. This - // code seems to compute the values correctly, but the author hasn't been - // able to apply them at the same time as DF does to 100%. - // DF seems to round down on 0.5 values. - df::coord2d world_dimension_size(uint16_t available_screen, uint16_t map_size) { + // Logic for sizing the World map to the right. + df::coord2d world_dimension_size(uint16_t available_screen, uint16_t map_size) { uint16_t result; for (uint16_t factor = 1; factor < 17; factor++) { - result = map_size / factor; - if ((map_size - result * factor) * 2 != factor) { - result = (map_size + factor / 2) / factor; - } + result = ceil (double (map_size - 1) / factor); if (result <= available_screen) { - return {result, factor}; + if (factor == 1 && + map_size <= available_screen) { + return{ uint16_t(result + 1), factor }; + } + else if ((map_size == 129 && // Weird exceptions where the last row/column goes unused. + (factor == 6 || + factor == 7)) || + (map_size == 257 && + (factor == 5 || + factor == 11 || + factor == 12 || + factor == 14 || + factor == 15))) { + return{ uint16_t(result - 1), factor }; + } + else + { + return{ result, factor }; + } } } return{16, 16}; // Should never get here. } -*/ + //==================================================================== class ViewscreenOverlay : public df::viewscreen_choose_start_sitest @@ -155,6 +167,7 @@ namespace embark_assist { Screen::Pen pen_lr(' ', COLOR_LIGHTRED); Screen::Pen pen_w(' ', COLOR_WHITE); + Screen::Pen pen_g(' ', COLOR_GREY); Screen::paintString(pen_lr, width - 28, 20, DFHack::Screen::getKeyDisplay(df::interface_key::CUSTOM_I).c_str(), false); Screen::paintString(pen_w, width - 27, 20, ": Embark Assistant Info", false); @@ -166,6 +179,7 @@ namespace embark_assist { Screen::paintString(pen_w, width - 27, 23, ": Quit Embark Assistant", false); Screen::paintString(pen_w, width - 28, 25, "Matching World Tiles:", false); Screen::paintString(empty_pen, width - 6, 25, to_string(state->match_count), false); + Screen::paintString(pen_g, width - 28, 26, "(Those on the Region Map)", false); if (height > 25) { // Mask the vanilla DF find help as it's overridden. Screen::paintString(pen_w, 50, height - 2, " ", false); @@ -221,13 +235,25 @@ namespace embark_assist { for (uint8_t i = 0; i < 16; i++) { for (uint8_t k = 0; k < 16; k++) { - if (state->region_match_grid[i][k].ch) { - Screen::paintTile(state->region_match_grid[i][k], i + 1, k + 2); + if (state->local_match_grid[i][k].ch) { + Screen::paintTile(state->local_match_grid[i][k], i + 1, k + 2); } } } -/* // Stuff for trying to replicate the DF right world map sizing logic. Close, but not there. + uint16_t l_width = width - 30 - (ceil(double_t(width) / 2) - 5) + 1; // Horizontal space available for world map. + uint16_t l_height = height - 8 - 2 + 1; // Vertical space available for world map. + df::coord2d size_factor_x = world_dimension_size(l_width, world->worldgen.worldgen_parms.dim_x); + df::coord2d size_factor_y = world_dimension_size(l_height, world->worldgen.worldgen_parms.dim_y); + + for (uint16_t i = 0; i < world->worldgen.worldgen_parms.dim_x; i++) { + for (uint16_t k = 0; k < world->worldgen.worldgen_parms.dim_y; k++) { + if (state->world_match_grid[i][k].ch) { + Screen::paintTile(state->world_match_grid[i][k], width / 2 - 5 + min(size_factor_x.x - 1, i / size_factor_x.y), 2 + min(size_factor_y.x - 1, k / size_factor_y.y)); + } + } + } + /* // Stuff for trying to replicate the DF right world map sizing logic. Close, but not there. Screen::Pen pen(' ', COLOR_YELLOW); // Boundaries of the top level world map Screen::paintString(pen, width / 2 - 5, 2, "X", false); // Marks UL corner of right world map. Constant @@ -390,11 +416,11 @@ void embark_assist::overlay::set_mid_level_tile_match(embark_assist::defs::mlt_m for (uint8_t i = 0; i < 16; i++) { for (uint8_t k = 0; k < 16; k++) { if (mlt_matches[i][k]) { - state->region_match_grid[i][k] = green_x_pen; + state->local_match_grid[i][k] = green_x_pen; } else { - state->region_match_grid[i][k] = empty_pen; + state->local_match_grid[i][k] = empty_pen; } } } @@ -411,7 +437,7 @@ void embark_assist::overlay::clear_match_results() { for (uint8_t i = 0; i < 16; i++) { for (uint8_t k = 0; k < 16; k++) { - state->region_match_grid[i][k] = empty_pen; + state->local_match_grid[i][k] = empty_pen; } } } From 136eb0f03a074b22fe6a01b14ddd829ebe4d2160 Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Sun, 5 Aug 2018 17:23:03 +0200 Subject: [PATCH 10/32] Added embark-assistant world match indication --- plugins/embark-assistant/help_ui.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/embark-assistant/help_ui.cpp b/plugins/embark-assistant/help_ui.cpp index 7c0763a71..0eae32c6b 100644 --- a/plugins/embark-assistant/help_ui.cpp +++ b/plugins/embark-assistant/help_ui.cpp @@ -171,8 +171,8 @@ namespace embark_assist{ help_text.push_back("A list of all economic minerals present in the embark. Both clays and flux"); help_text.push_back("stones are economic, so they show up here as well."); help_text.push_back("In addition to the above, the Find functionality can also produce blinking"); - help_text.push_back("overlays over the region map and the middle world map to indicate where"); - help_text.push_back("matching embarks are found. The region display marks the top left corner of"); + help_text.push_back("overlays over the Local, Region, and World maps to indicate where"); + help_text.push_back("matching embarks are found. The Local display marks the top left corner of"); help_text.push_back("a matching embark rectangle as a matching tile."); break; @@ -264,7 +264,7 @@ namespace embark_assist{ help_text.push_back(" reaching caverns that have been removed at world gen to fail to be"); help_text.push_back(" generated at all. It's likely this bug also affects magma pools."); help_text.push_back(" This plugin does not address this but scripts can correct it."); - help_text.push_back("Version 0.5 2018-07-13"); + help_text.push_back("Version 0.6 2018-08-05"); break; } From aa6182a1eebfbc132ae0abfe0f1ee6aca9d7715c Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Sun, 5 Aug 2018 17:28:49 +0200 Subject: [PATCH 11/32] Added embark-assistant world match indication --- plugins/embark-assistant/overlay.cpp | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/plugins/embark-assistant/overlay.cpp b/plugins/embark-assistant/overlay.cpp index c0f5e0c8c..bacd0b2d6 100644 --- a/plugins/embark-assistant/overlay.cpp +++ b/plugins/embark-assistant/overlay.cpp @@ -253,23 +253,6 @@ namespace embark_assist { } } } - /* // Stuff for trying to replicate the DF right world map sizing logic. Close, but not there. - Screen::Pen pen(' ', COLOR_YELLOW); - // Boundaries of the top level world map - Screen::paintString(pen, width / 2 - 5, 2, "X", false); // Marks UL corner of right world map. Constant -// Screen::paintString(pen, width - 30, 2, "X", false); // Marks UR corner of right world map area. -// Screen::paintString(pen, width / 2 - 5, height - 8, "X", false); // BL corner of right world map area. -// Screen::paintString(pen, width - 30, height - 8, "X", false); // BR corner of right world map area. - - uint16_t l_width = width - 30 - (width / 2 - 5) + 1; // Horizontal space available for right world map. - uint16_t l_height = height - 8 - 2 + 1; // Vertical space available for right world map. - df::coord2d size_factor_x = world_dimension_size(l_width, world->worldgen.worldgen_parms.dim_x); - df::coord2d size_factor_y = world_dimension_size(l_height, world->worldgen.worldgen_parms.dim_y); - - Screen::paintString(pen, width / 2 - 5 + size_factor_x.x - 1, 2, "X", false); - Screen::paintString(pen, width / 2 - 5, 2 + size_factor_y.x - 1, "X", false); - Screen::paintString(pen, width / 2 - 5 + size_factor_x.x - 1, 2 + size_factor_y.x - 1, "X", false); - */ } if (state->matching) { From c75a4fe8eebd3f8ef6b57f094e4c56c29f70eb9e Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Mon, 6 Aug 2018 11:52:22 +0200 Subject: [PATCH 12/32] Used taleden's world map size algorithm --- plugins/embark-assistant/overlay.cpp | 39 +++++----------------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/plugins/embark-assistant/overlay.cpp b/plugins/embark-assistant/overlay.cpp index bacd0b2d6..63c70996c 100644 --- a/plugins/embark-assistant/overlay.cpp +++ b/plugins/embark-assistant/overlay.cpp @@ -61,35 +61,12 @@ namespace embark_assist { //==================================================================== // Logic for sizing the World map to the right. - df::coord2d world_dimension_size(uint16_t available_screen, uint16_t map_size) { - uint16_t result; + df::coord2d world_dimension_size(uint16_t map_size, uint16_t region_size) { + uint16_t factor = (map_size - 1 + region_size - 1) / region_size; + uint16_t result = (map_size + ((factor - 1) / 2)) / factor; + if (result > region_size) { result = region_size; } - for (uint16_t factor = 1; factor < 17; factor++) { - result = ceil (double (map_size - 1) / factor); - - if (result <= available_screen) { - if (factor == 1 && - map_size <= available_screen) { - return{ uint16_t(result + 1), factor }; - } - else if ((map_size == 129 && // Weird exceptions where the last row/column goes unused. - (factor == 6 || - factor == 7)) || - (map_size == 257 && - (factor == 5 || - factor == 11 || - factor == 12 || - factor == 14 || - factor == 15))) { - return{ uint16_t(result - 1), factor }; - } - else - { - return{ result, factor }; - } - } - } - return{16, 16}; // Should never get here. + return{ result, factor}; } //==================================================================== @@ -241,10 +218,8 @@ namespace embark_assist { } } - uint16_t l_width = width - 30 - (ceil(double_t(width) / 2) - 5) + 1; // Horizontal space available for world map. - uint16_t l_height = height - 8 - 2 + 1; // Vertical space available for world map. - df::coord2d size_factor_x = world_dimension_size(l_width, world->worldgen.worldgen_parms.dim_x); - df::coord2d size_factor_y = world_dimension_size(l_height, world->worldgen.worldgen_parms.dim_y); + df::coord2d size_factor_x = world_dimension_size(world->worldgen.worldgen_parms.dim_x, width / 2 - 24); + df::coord2d size_factor_y = world_dimension_size(world->worldgen.worldgen_parms.dim_y, height - 9); for (uint16_t i = 0; i < world->worldgen.worldgen_parms.dim_x; i++) { for (uint16_t k = 0; k < world->worldgen.worldgen_parms.dim_y; k++) { From 039fb3bc6b5e7e3431ec22bc2ff69722b86ebd27 Mon Sep 17 00:00:00 2001 From: billw2012 Date: Fri, 10 Aug 2018 20:42:34 +0100 Subject: [PATCH 13/32] labormanager: add option to disable the management of a labor. Also switching to case insensitive labor name matching. --- plugins/labormanager/labormanager.cpp | 67 +++++++++++++++++++-------- 1 file changed, 48 insertions(+), 19 deletions(-) diff --git a/plugins/labormanager/labormanager.cpp b/plugins/labormanager/labormanager.cpp index 7e42afd64..55cbc8c0e 100644 --- a/plugins/labormanager/labormanager.cpp +++ b/plugins/labormanager/labormanager.cpp @@ -100,6 +100,11 @@ enum ConfigFlags { CF_ALLOW_HUNTING = 4, }; +// Value of 0 for max dwarfs means uncapped. +const int MAX_DWARFS_NONE = 0; +// Value < 0 for max dwarfs means don't manager the labor. +const int MAX_DWARFS_DISABLE = -1; + // Here go all the command declarations... // mostly to allow having the mandatory stuff on top of the file and commands on the bottom @@ -390,16 +395,18 @@ struct labor_info int idle_dwarfs; int busy_dwarfs; - int priority() { return config.ival(1); } + int priority() const { return config.ival(1); } void set_priority(int priority) { config.ival(1) = priority; } - int maximum_dwarfs() { return config.ival(2); } + bool is_disabled() const { return maximum_dwarfs() == MAX_DWARFS_DISABLE; } + int maximum_dwarfs() const { return config.ival(2); } void set_maximum_dwarfs(int maximum_dwarfs) { config.ival(2) = maximum_dwarfs; } - int time_since_last_assigned() + int time_since_last_assigned() const { return (*df::global::cur_year - config.ival(3)) * 403200 + *df::global::cur_year_tick - config.ival(4); } + void mark_assigned() { config.ival(3) = (*df::global::cur_year); config.ival(4) = (*df::global::cur_year_tick); @@ -412,7 +419,6 @@ enum tools_enum { TOOLS_MAX }; - struct labor_default { int priority; @@ -841,6 +847,8 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector \n" " Set max number of dwarves assigned to a labor.\n" + " labormanager max disable\n" + " Don't attempt to assign any dwarves to a labor.\n" " labormanager max none\n" " Unrestrict the number of dwarves assigned to a labor.\n" " labormanager priority \n" @@ -944,7 +952,7 @@ private: private: void set_labor(dwarf_info_t* dwarf, df::unit_labor labor, bool value) { - if (labor >= 0 && labor <= ENUM_LAST_ITEM(unit_labor)) + if (labor >= 0 && labor <= ENUM_LAST_ITEM(unit_labor) && !labor_infos[labor].is_disabled()) { if (!Units::isValidLabor(dwarf->dwarf, labor)) { @@ -954,7 +962,6 @@ private: ENUM_KEY_STR(unit_labor, labor).c_str()); return; } - bool old = dwarf->dwarf->status.labors[labor]; dwarf->dwarf->status.labors[labor] = value; if (old != value) @@ -1782,9 +1789,13 @@ public: if (l == df::unit_labor::NONE) continue; - if (labor_infos[l].maximum_dwarfs() > 0 && - i->second > labor_infos[l].maximum_dwarfs()) - i->second = labor_infos[l].maximum_dwarfs(); + const int user_specified_max_dwarfs = labor_infos[l].maximum_dwarfs(); + + // Allow values less than 0, they will disable this labor. + if (user_specified_max_dwarfs != MAX_DWARFS_NONE && i->second > user_specified_max_dwarfs) + { + i->second = user_specified_max_dwarfs; + } int priority = labor_infos[l].priority(); @@ -2172,25 +2183,41 @@ void print_labor(df::unit_labor labor, color_ostream &out) out << labor_name << ": "; for (int i = 0; i < 20 - (int)labor_name.length(); i++) out << ' '; - out << "priority " << labor_infos[labor].priority() - << ", maximum " << labor_infos[labor].maximum_dwarfs() - << ", currently " << labor_infos[labor].active_dwarfs << " dwarfs (" - << labor_infos[labor].busy_dwarfs << " busy, " - << labor_infos[labor].idle_dwarfs << " idle)" + const auto& labor_info = labor_infos[labor]; + if (labor_info.is_disabled()) + { + out << "DISABLED"; + } + else + { + out << "priority " << labor_info.priority(); + + if (labor_info.maximum_dwarfs() == MAX_DWARFS_NONE) + out << ", no maximum"; + else + out << ", maximum " << labor_info.maximum_dwarfs(); + } + + out << ", currently " << labor_info.active_dwarfs << " dwarfs (" + << labor_info.busy_dwarfs << " busy, " + << labor_info.idle_dwarfs << " idle)" << endl; } -df::unit_labor lookup_labor_by_name(std::string& name) +df::unit_labor lookup_labor_by_name(std::string name) { - df::unit_labor labor = df::unit_labor::NONE; + // We should accept incorrect casing, there is no ambiguity. + std::transform(name.begin(), name.end(), name.begin(), ::toupper); FOR_ENUM_ITEMS(unit_labor, test_labor) { if (name == ENUM_KEY_STR(unit_labor, test_labor)) - labor = test_labor; + { + return test_labor; + } } - return labor; + return df::unit_labor::NONE; } DFhackCExport command_result plugin_enable(color_ostream &out, bool enable) @@ -2250,7 +2277,9 @@ command_result labormanager(color_ostream &out, std::vector & para int v; if (parameters[2] == "none") - v = 0; + v = MAX_DWARFS_NONE; + else if (parameters[2] == "disable") + v = MAX_DWARFS_DISABLE; else v = atoi(parameters[2].c_str()); From 76b8f4af0ed59ac486a55e1c9e9acb3ab2875e3a Mon Sep 17 00:00:00 2001 From: billw2012 Date: Fri, 10 Aug 2018 21:25:40 +0100 Subject: [PATCH 14/32] Fixing tab --- plugins/labormanager/labormanager.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/labormanager/labormanager.cpp b/plugins/labormanager/labormanager.cpp index 55cbc8c0e..d7a2f6f44 100644 --- a/plugins/labormanager/labormanager.cpp +++ b/plugins/labormanager/labormanager.cpp @@ -2197,8 +2197,7 @@ void print_labor(df::unit_labor labor, color_ostream &out) else out << ", maximum " << labor_info.maximum_dwarfs(); } - - out << ", currently " << labor_info.active_dwarfs << " dwarfs (" + out << ", currently " << labor_info.active_dwarfs << " dwarfs (" << labor_info.busy_dwarfs << " busy, " << labor_info.idle_dwarfs << " idle)" << endl; From 866379204144f3ac84fa59ad8e4612c9adaef56c Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Sat, 18 Aug 2018 18:01:30 +0200 Subject: [PATCH 15/32] Made cancel state sensitive --- docs/changelog.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index 17ed0c25f..d87131be7 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -43,6 +43,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - `embark-assistant`: - fixed crash when entering finder with a 16x16 embark selected, and added 16 to dimension choices - added match indicator display on the right ("World") map + - changed 'c'ancel to abort find if it's under way and clear results if not, allowing use of partial surveys. - `labormanager`: - stopped assigning labors to ineligible dwarves, pets, etc. - stopped assigning invalid labors From 2ed6469be8e4753c3c9fd8e58e8db0286b616fb8 Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Sat, 18 Aug 2018 18:02:19 +0200 Subject: [PATCH 16/32] Made cancel state sensitive --- plugins/embark-assistant/help_ui.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plugins/embark-assistant/help_ui.cpp b/plugins/embark-assistant/help_ui.cpp index 0eae32c6b..395cbc25f 100644 --- a/plugins/embark-assistant/help_ui.cpp +++ b/plugins/embark-assistant/help_ui.cpp @@ -127,8 +127,8 @@ namespace embark_assist{ help_text.push_back("Main screen control keys used by the Embark Assistant:"); help_text.push_back("i: Info/Help. Brings up this display."); help_text.push_back("f: Brings up the Find Embark screen. See the Find page for more information."); - help_text.push_back("c: Clears the results of a Find operation, and also cancels an operation if"); - help_text.push_back(" one is under way."); + help_text.push_back("c: Clears the results of a Find operation, or cancels an operation if one is"); + help_text.push_back(" under way (at which time a second 'c' clears it)."); help_text.push_back("q: Quits the Embark Assistant and brings you back to the vanilla DF interface."); help_text.push_back(" It can be noted that the Embark Assistant automatically cancels itself"); help_text.push_back(" when DF leaves the embark screen either through Abort Game or by"); @@ -264,7 +264,7 @@ namespace embark_assist{ help_text.push_back(" reaching caverns that have been removed at world gen to fail to be"); help_text.push_back(" generated at all. It's likely this bug also affects magma pools."); help_text.push_back(" This plugin does not address this but scripts can correct it."); - help_text.push_back("Version 0.6 2018-08-05"); + help_text.push_back("Version 0.7 2018-08-18"); break; } From 73e7ffff837d7339deb362098e24827946e38f6a Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Sat, 18 Aug 2018 18:03:00 +0200 Subject: [PATCH 17/32] Made cancel state sensitive --- plugins/embark-assistant/overlay.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/plugins/embark-assistant/overlay.cpp b/plugins/embark-assistant/overlay.cpp index 63c70996c..75e6258a3 100644 --- a/plugins/embark-assistant/overlay.cpp +++ b/plugins/embark-assistant/overlay.cpp @@ -103,9 +103,13 @@ namespace embark_assist { state->embark_update(); } else if (input->count(df::interface_key::CUSTOM_C)) { - state->match_active = false; - state->matching = false; - state->clear_match_callback(); + if (state->matching) { + state->matching = false; + } + else { + state->match_active = false; + state->clear_match_callback(); + } } else if (input->count(df::interface_key::CUSTOM_F)) { if (!state->match_active && !state->matching) { From 239d4a8c466286b76f572cf96243f49d75866f8d Mon Sep 17 00:00:00 2001 From: billw2012 Date: Sun, 26 Aug 2018 13:38:03 +0100 Subject: [PATCH 18/32] Attempt to full exclude disabled labors from all relevant calculations. --- plugins/labormanager/labormanager.cpp | 127 +++++++++++++++----------- 1 file changed, 73 insertions(+), 54 deletions(-) diff --git a/plugins/labormanager/labormanager.cpp b/plugins/labormanager/labormanager.cpp index d7a2f6f44..a3fd7db4a 100644 --- a/plugins/labormanager/labormanager.cpp +++ b/plugins/labormanager/labormanager.cpp @@ -1016,7 +1016,7 @@ private: df::unit_labor labor = labor_mapper->find_job_labor(j); - if (labor != df::unit_labor::NONE) + if (labor != df::unit_labor::NONE && !labor_infos[labor].is_disabled()) { labor_needed[labor]++; if (worker == -1) @@ -1143,7 +1143,7 @@ private: { df::item* item = *i; - if (item->flags.bits.dump) + if (item->flags.bits.dump && !labor_infos[df::unit_labor::HAUL_REFUSE].is_disabled()) labor_needed[df::unit_labor::HAUL_REFUSE]++; if (item->flags.whole & bad_flags.whole) @@ -1443,7 +1443,7 @@ private: FOR_ENUM_ITEMS(unit_labor, labor) { - if (labor == df::unit_labor::NONE) + if (labor == df::unit_labor::NONE || labor_infos[labor].is_disabled()) continue; df::job_skill skill = labor_to_skill[labor]; @@ -1463,7 +1463,7 @@ private: { FOR_ENUM_ITEMS(unit_labor, labor) { - if (labor == unit_labor::NONE) + if (labor == unit_labor::NONE || labor_infos[labor].is_disabled()) continue; if (Units::isValidLabor(dwarf->dwarf, labor)) set_labor(dwarf, labor, false); @@ -1707,67 +1707,79 @@ public: if (l == df::unit_labor::NONE) continue; - int before = labor_needed[l]; - - labor_needed[l] = max(0, labor_needed[l] - labor_in_use[l]); + if (!labor_infos[l].is_disabled()) + { + int before = labor_needed[l]; - if (default_labor_infos[l].tool != TOOL_NONE) - labor_needed[l] = std::min(labor_needed[l], tool_count[default_labor_infos[l].tool] - tool_in_use[default_labor_infos[l].tool]); + labor_needed[l] = max(0, labor_needed[l] - labor_in_use[l]); - if (print_debug && before != labor_needed[l]) - out.print("labor %s reduced from %d to %d\n", ENUM_KEY_STR(unit_labor, l).c_str(), before, labor_needed[l]); + if (default_labor_infos[l].tool != TOOL_NONE) + labor_needed[l] = std::min(labor_needed[l], tool_count[default_labor_infos[l].tool] - tool_in_use[default_labor_infos[l].tool]); + if (print_debug && before != labor_needed[l]) + out.print("labor %s reduced from %d to %d\n", ENUM_KEY_STR(unit_labor, l).c_str(), before, labor_needed[l]); + } + else + { + labor_needed[l] = 0; + } } /* assign food haulers for rotting food items */ - - if (priority_food > 0 && labor_infos[df::unit_labor::HAUL_FOOD].idle_dwarfs > 0) - priority_food = 1; - - if (print_debug) - out.print("priority food count = %d\n", priority_food); - - while (!available_dwarfs.empty() && priority_food > 0) + if (!labor_infos[df::unit_labor::HAUL_FOOD].is_disabled()) { - std::list::iterator bestdwarf = available_dwarfs.begin(); + if (priority_food > 0 && labor_infos[df::unit_labor::HAUL_FOOD].idle_dwarfs > 0) + priority_food = 1; - int best_score = INT_MIN; + if (print_debug) + out.print("priority food count = %d\n", priority_food); - for (std::list::iterator k = available_dwarfs.begin(); k != available_dwarfs.end(); k++) + while (!available_dwarfs.empty() && priority_food > 0) { - dwarf_info_t* d = (*k); + std::list::iterator bestdwarf = available_dwarfs.begin(); + + int best_score = INT_MIN; - if (Units::isValidLabor(d->dwarf, df::unit_labor::HAUL_FOOD)) + for (std::list::iterator k = available_dwarfs.begin(); k != available_dwarfs.end(); k++) { - int score = score_labor(d, df::unit_labor::HAUL_FOOD); + dwarf_info_t* d = (*k); - if (score > best_score) + if (Units::isValidLabor(d->dwarf, df::unit_labor::HAUL_FOOD)) { - bestdwarf = k; - best_score = score; + int score = score_labor(d, df::unit_labor::HAUL_FOOD); + + if (score > best_score) + { + bestdwarf = k; + best_score = score; + } } } - } - if (best_score > INT_MIN) - { - if (print_debug) - out.print("LABORMANAGER: assign \"%s\" labor %s score=%d (priority food)\n", (*bestdwarf)->dwarf->name.first_name.c_str(), ENUM_KEY_STR(unit_labor, df::unit_labor::HAUL_FOOD).c_str(), best_score); - - FOR_ENUM_ITEMS(unit_labor, l) + if (best_score > INT_MIN) { - if (l == df::unit_labor::NONE) - continue; - if (Units::isValidLabor((*bestdwarf)->dwarf, l)) - set_labor(*bestdwarf, l, l == df::unit_labor::HAUL_FOOD); + if (print_debug) + out.print("LABORMANAGER: assign \"%s\" labor %s score=%d (priority food)\n", (*bestdwarf)->dwarf->name.first_name.c_str(), ENUM_KEY_STR(unit_labor, df::unit_labor::HAUL_FOOD).c_str(), best_score); + + FOR_ENUM_ITEMS(unit_labor, l) + { + if (l == df::unit_labor::NONE) + continue; + if (Units::isValidLabor((*bestdwarf)->dwarf, l)) + set_labor(*bestdwarf, l, l == df::unit_labor::HAUL_FOOD); + } + + available_dwarfs.erase(bestdwarf); + priority_food--; } + else + break; - available_dwarfs.erase(bestdwarf); - priority_food--; } - else - break; - + } + else + { + priority_food = 0; } if (print_debug) @@ -1786,12 +1798,11 @@ public: for (auto i = labor_needed.begin(); i != labor_needed.end(); i++) { df::unit_labor l = i->first; - if (l == df::unit_labor::NONE) + if (l == df::unit_labor::NONE || labor_infos[l].is_disabled()) continue; const int user_specified_max_dwarfs = labor_infos[l].maximum_dwarfs(); - // Allow values less than 0, they will disable this labor. if (user_specified_max_dwarfs != MAX_DWARFS_NONE && i->second > user_specified_max_dwarfs) { i->second = user_specified_max_dwarfs; @@ -1951,7 +1962,7 @@ public: FOR_ENUM_ITEMS(unit_labor, l) { - if (l == df::unit_labor::NONE) + if (l == df::unit_labor::NONE || labor_infos[l].is_disabled()) continue; if (l == (*d)->using_labor) continue; @@ -2002,12 +2013,17 @@ public: } /* Also set the canary to remove constructions, because we have no way yet to tell if there are constructions needing removal */ - - set_labor(canary_dwarf, df::unit_labor::REMOVE_CONSTRUCTION, true); + if (!labor_infos[df::unit_labor::REMOVE_CONSTRUCTION].is_disabled()) + { + set_labor(canary_dwarf, df::unit_labor::REMOVE_CONSTRUCTION, true); + } /* Set HAUL_WATER so we can detect ponds that need to be filled ponds. */ - set_labor(canary_dwarf, df::unit_labor::HAUL_WATER, true); + if (!labor_infos[df::unit_labor::HAUL_WATER].is_disabled()) + { + set_labor(canary_dwarf, df::unit_labor::HAUL_WATER, true); + } if (print_debug) out.print("Setting %s as the hauling canary\n", canary_dwarf->dwarf->name.first_name.c_str()); @@ -2027,7 +2043,7 @@ public: { FOR_ENUM_ITEMS(unit_labor, l) { - if (l == df::unit_labor::NONE) + if (l == df::unit_labor::NONE || labor_infos[l].is_disabled()) continue; if (Units::isValidLabor((*d)->dwarf, l)) @@ -2059,13 +2075,16 @@ public: } } - set_labor(*d, df::unit_labor::PULL_LEVER, true); + if (!labor_infos[df::unit_labor::PULL_LEVER].is_disabled()) + { + set_labor(*d, df::unit_labor::PULL_LEVER, true); + } if (any) continue; FOR_ENUM_ITEMS(unit_labor, l) { - if (l == df::unit_labor::NONE) + if (l == df::unit_labor::NONE || labor_infos[l].is_disabled()) continue; if (to_assign[l] > 0 || l == df::unit_labor::CLEAN) @@ -2086,7 +2105,7 @@ public: FOR_ENUM_ITEMS(unit_labor, l) { - if (l == df::unit_labor::NONE) + if (l == df::unit_labor::NONE || labor_infos[l].is_disabled()) continue; tools_enum t = default_labor_infos[l].tool; From fefef2e121698fdb672a911cbffd7d13976fc3d8 Mon Sep 17 00:00:00 2001 From: billw2012 Date: Fri, 31 Aug 2018 20:53:06 +0100 Subject: [PATCH 19/32] Deprioritize dwarves with unmanged labors assigned, some renaming from disabled to unmanaged. --- plugins/labormanager/labormanager.cpp | 62 ++++++++++++++++----------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/plugins/labormanager/labormanager.cpp b/plugins/labormanager/labormanager.cpp index a3fd7db4a..419432870 100644 --- a/plugins/labormanager/labormanager.cpp +++ b/plugins/labormanager/labormanager.cpp @@ -103,7 +103,7 @@ enum ConfigFlags { // Value of 0 for max dwarfs means uncapped. const int MAX_DWARFS_NONE = 0; // Value < 0 for max dwarfs means don't manager the labor. -const int MAX_DWARFS_DISABLE = -1; +const int MAX_DWARFS_UNMANAGED = -1; // Here go all the command declarations... @@ -398,7 +398,7 @@ struct labor_info int priority() const { return config.ival(1); } void set_priority(int priority) { config.ival(1) = priority; } - bool is_disabled() const { return maximum_dwarfs() == MAX_DWARFS_DISABLE; } + bool is_unmanaged() const { return maximum_dwarfs() == MAX_DWARFS_UNMANAGED; } int maximum_dwarfs() const { return config.ival(2); } void set_maximum_dwarfs(int maximum_dwarfs) { config.ival(2) = maximum_dwarfs; } @@ -530,10 +530,12 @@ struct dwarf_info_t bool has_children; bool armed; + int unmanaged_labors_assigned; + df::unit_labor using_labor; dwarf_info_t(df::unit* dw) : dwarf(dw), state(OTHER), - clear_all(false), high_skill(0), has_children(false), armed(false), using_labor(df::unit_labor::NONE) + clear_all(false), high_skill(0), has_children(false), armed(false), using_labor(df::unit_labor::NONE), unmanaged_labors_assigned(0) { for (int e = TOOL_NONE; e < TOOLS_MAX; e++) has_tool[e] = false; @@ -847,8 +849,11 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector \n" " Set max number of dwarves assigned to a labor.\n" + " labormanager max unmanaged\n" " labormanager max disable\n" - " Don't attempt to assign any dwarves to a labor.\n" + " Don't attempt to manage this labor.\n" + " Any dwarves with unmanaged labors assigned will be less\n" + " likely to have managed labors assigned to them.\n" " labormanager max none\n" " Unrestrict the number of dwarves assigned to a labor.\n" " labormanager priority \n" @@ -865,8 +870,8 @@ DFhackCExport command_result plugin_init(color_ostream &out, std::vector = 0 && labor <= ENUM_LAST_ITEM(unit_labor) && !labor_infos[labor].is_disabled()) + if (labor >= 0 && labor <= ENUM_LAST_ITEM(unit_labor) && !labor_infos[labor].is_unmanaged()) { if (!Units::isValidLabor(dwarf->dwarf, labor)) { @@ -1016,7 +1021,7 @@ private: df::unit_labor labor = labor_mapper->find_job_labor(j); - if (labor != df::unit_labor::NONE && !labor_infos[labor].is_disabled()) + if (labor != df::unit_labor::NONE && !labor_infos[labor].is_unmanaged()) { labor_needed[labor]++; if (worker == -1) @@ -1143,7 +1148,7 @@ private: { df::item* item = *i; - if (item->flags.bits.dump && !labor_infos[df::unit_labor::HAUL_REFUSE].is_disabled()) + if (item->flags.bits.dump && !labor_infos[df::unit_labor::HAUL_REFUSE].is_unmanaged()) labor_needed[df::unit_labor::HAUL_REFUSE]++; if (item->flags.whole & bad_flags.whole) @@ -1389,6 +1394,8 @@ private: dwarf->state = state; + dwarf->unmanaged_labors_assigned = 0; + FOR_ENUM_ITEMS(unit_labor, l) { if (l == df::unit_labor::NONE) @@ -1396,6 +1403,8 @@ private: if (dwarf->dwarf->status.labors[l]) if (state == IDLE) labor_infos[l].idle_dwarfs++; + if (labor_infos[l].is_unmanaged()) + dwarf->unmanaged_labors_assigned++; } @@ -1443,7 +1452,7 @@ private: FOR_ENUM_ITEMS(unit_labor, labor) { - if (labor == df::unit_labor::NONE || labor_infos[labor].is_disabled()) + if (labor == df::unit_labor::NONE || labor_infos[labor].is_unmanaged()) continue; df::job_skill skill = labor_to_skill[labor]; @@ -1463,7 +1472,7 @@ private: { FOR_ENUM_ITEMS(unit_labor, labor) { - if (labor == unit_labor::NONE || labor_infos[labor].is_disabled()) + if (labor == unit_labor::NONE || labor_infos[labor].is_unmanaged()) continue; if (Units::isValidLabor(dwarf->dwarf, labor)) set_labor(dwarf, labor, false); @@ -1579,6 +1588,9 @@ private: score -= Units::computeMovementSpeed(d->dwarf); + // significantly disfavor dwarves who have unmanaged labors assigned + score -= 1000 * d->unmanaged_labors_assigned; + return score; } @@ -1707,7 +1719,7 @@ public: if (l == df::unit_labor::NONE) continue; - if (!labor_infos[l].is_disabled()) + if (!labor_infos[l].is_unmanaged()) { int before = labor_needed[l]; @@ -1726,7 +1738,7 @@ public: } /* assign food haulers for rotting food items */ - if (!labor_infos[df::unit_labor::HAUL_FOOD].is_disabled()) + if (!labor_infos[df::unit_labor::HAUL_FOOD].is_unmanaged()) { if (priority_food > 0 && labor_infos[df::unit_labor::HAUL_FOOD].idle_dwarfs > 0) priority_food = 1; @@ -1798,7 +1810,7 @@ public: for (auto i = labor_needed.begin(); i != labor_needed.end(); i++) { df::unit_labor l = i->first; - if (l == df::unit_labor::NONE || labor_infos[l].is_disabled()) + if (l == df::unit_labor::NONE || labor_infos[l].is_unmanaged()) continue; const int user_specified_max_dwarfs = labor_infos[l].maximum_dwarfs(); @@ -1962,7 +1974,7 @@ public: FOR_ENUM_ITEMS(unit_labor, l) { - if (l == df::unit_labor::NONE || labor_infos[l].is_disabled()) + if (l == df::unit_labor::NONE || labor_infos[l].is_unmanaged()) continue; if (l == (*d)->using_labor) continue; @@ -2013,14 +2025,14 @@ public: } /* Also set the canary to remove constructions, because we have no way yet to tell if there are constructions needing removal */ - if (!labor_infos[df::unit_labor::REMOVE_CONSTRUCTION].is_disabled()) + if (!labor_infos[df::unit_labor::REMOVE_CONSTRUCTION].is_unmanaged()) { set_labor(canary_dwarf, df::unit_labor::REMOVE_CONSTRUCTION, true); } /* Set HAUL_WATER so we can detect ponds that need to be filled ponds. */ - if (!labor_infos[df::unit_labor::HAUL_WATER].is_disabled()) + if (!labor_infos[df::unit_labor::HAUL_WATER].is_unmanaged()) { set_labor(canary_dwarf, df::unit_labor::HAUL_WATER, true); } @@ -2043,7 +2055,7 @@ public: { FOR_ENUM_ITEMS(unit_labor, l) { - if (l == df::unit_labor::NONE || labor_infos[l].is_disabled()) + if (l == df::unit_labor::NONE || labor_infos[l].is_unmanaged()) continue; if (Units::isValidLabor((*d)->dwarf, l)) @@ -2075,7 +2087,7 @@ public: } } - if (!labor_infos[df::unit_labor::PULL_LEVER].is_disabled()) + if (!labor_infos[df::unit_labor::PULL_LEVER].is_unmanaged()) { set_labor(*d, df::unit_labor::PULL_LEVER, true); } @@ -2084,7 +2096,7 @@ public: FOR_ENUM_ITEMS(unit_labor, l) { - if (l == df::unit_labor::NONE || labor_infos[l].is_disabled()) + if (l == df::unit_labor::NONE || labor_infos[l].is_unmanaged()) continue; if (to_assign[l] > 0 || l == df::unit_labor::CLEAN) @@ -2105,7 +2117,7 @@ public: FOR_ENUM_ITEMS(unit_labor, l) { - if (l == df::unit_labor::NONE || labor_infos[l].is_disabled()) + if (l == df::unit_labor::NONE || labor_infos[l].is_unmanaged()) continue; tools_enum t = default_labor_infos[l].tool; @@ -2203,9 +2215,9 @@ void print_labor(df::unit_labor labor, color_ostream &out) for (int i = 0; i < 20 - (int)labor_name.length(); i++) out << ' '; const auto& labor_info = labor_infos[labor]; - if (labor_info.is_disabled()) + if (labor_info.is_unmanaged()) { - out << "DISABLED"; + out << "UNMANAGED"; } else { @@ -2296,8 +2308,8 @@ command_result labormanager(color_ostream &out, std::vector & para if (parameters[2] == "none") v = MAX_DWARFS_NONE; - else if (parameters[2] == "disable") - v = MAX_DWARFS_DISABLE; + else if (parameters[2] == "disable" || parameters[2] == "unmanaged") + v = MAX_DWARFS_UNMANAGED; else v = atoi(parameters[2].c_str()); From 32aaa37070d88ed00e25371fc2e764d0c9c50233 Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Tue, 4 Dec 2018 14:54:28 +0100 Subject: [PATCH 20/32] Added coal search to embark-assistant --- docs/changelog.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/changelog.txt b/docs/changelog.txt index d87131be7..24120c2a2 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -44,6 +44,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - fixed crash when entering finder with a 16x16 embark selected, and added 16 to dimension choices - added match indicator display on the right ("World") map - changed 'c'ancel to abort find if it's under way and clear results if not, allowing use of partial surveys. + - Added Coal as a search criterion, as well as a coal indication as current embark selection info. - `labormanager`: - stopped assigning labors to ineligible dwarves, pets, etc. - stopped assigning invalid labors From 5f6376e76e8c993f2df06bac274d1d9bffbc2638 Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Tue, 4 Dec 2018 14:55:16 +0100 Subject: [PATCH 21/32] Added coal search to embark-assistant --- plugins/embark-assistant/defs.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugins/embark-assistant/defs.h b/plugins/embark-assistant/defs.h index 43f2ddc0a..f7a9ed680 100644 --- a/plugins/embark-assistant/defs.h +++ b/plugins/embark-assistant/defs.h @@ -28,6 +28,7 @@ namespace embark_assist { bool clay = false; bool sand = false; bool flux = false; + bool coal = false; int8_t soil_depth; int8_t offset; int16_t elevation; @@ -51,6 +52,7 @@ namespace embark_assist { uint16_t clay_count = 0; uint16_t sand_count = 0; uint16_t flux_count = 0; + uint16_t coal_count = 0; uint8_t min_region_soil = 10; uint8_t max_region_soil = 0; bool waterfall = false; @@ -90,6 +92,7 @@ namespace embark_assist { bool clay_absent = true; bool sand_absent = true; bool flux_absent = true; + bool coal_absent = true; std::vector possible_metals; std::vector possible_economics; std::vector possible_minerals; @@ -113,6 +116,7 @@ namespace embark_assist { bool clay; bool sand; bool flux; + bool coal; std::vector metals; std::vector economics; std::vector minerals; @@ -253,6 +257,7 @@ namespace embark_assist { present_absent_ranges clay; present_absent_ranges sand; present_absent_ranges flux; + present_absent_ranges coal; soil_ranges soil_min; all_present_ranges soil_min_everywhere; soil_ranges soil_max; From efeb0504ccd112eb8fb9d19941560d183aa4d2d0 Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Tue, 4 Dec 2018 14:55:59 +0100 Subject: [PATCH 22/32] Added coal search to embark-assistant --- plugins/embark-assistant/finder_ui.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plugins/embark-assistant/finder_ui.cpp b/plugins/embark-assistant/finder_ui.cpp index 5578150ea..5c7cc9046 100644 --- a/plugins/embark-assistant/finder_ui.cpp +++ b/plugins/embark-assistant/finder_ui.cpp @@ -44,6 +44,7 @@ namespace embark_assist { clay, sand, flux, + coal, soil_min, soil_min_everywhere, soil_max, @@ -508,6 +509,7 @@ namespace embark_assist { case fields::clay: case fields::sand: case fields::flux: + case fields::coal: { embark_assist::defs::present_absent_ranges k = embark_assist::defs::present_absent_ranges::NA; while (true) { @@ -993,6 +995,10 @@ namespace embark_assist { state->finder_list.push_back({ "Flux", static_cast(i) }); break; + case fields::coal: + state->finder_list.push_back({ "Coal", static_cast(i) }); + break; + case fields::soil_min: state->finder_list.push_back({ "Min Soil", static_cast(i) }); break; @@ -1228,6 +1234,11 @@ namespace embark_assist { static_cast(state->ui[static_cast(i)]->current_value); break; + case fields::coal: + finder.coal = + static_cast(state->ui[static_cast(i)]->current_value); + break; + case fields::soil_min: finder.soil_min = static_cast(state->ui[static_cast(i)]->current_value); From 1ef7f0746caa76544ce7a5455c99b46e7db0a471 Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Tue, 4 Dec 2018 14:56:55 +0100 Subject: [PATCH 23/32] Added coal search to embark-assistant --- plugins/embark-assistant/help_ui.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/embark-assistant/help_ui.cpp b/plugins/embark-assistant/help_ui.cpp index 395cbc25f..e92ae67c0 100644 --- a/plugins/embark-assistant/help_ui.cpp +++ b/plugins/embark-assistant/help_ui.cpp @@ -257,14 +257,13 @@ namespace embark_assist{ help_text.push_back("- The geo information is gathered by code which is essentially a"); help_text.push_back(" copy of parts of prospector's code adapted for this plugin."); help_text.push_back("- Clay determination is made by finding the reaction MAKE_CLAY_BRICKS."); - help_text.push_back(" Flux determination is made by finding the reaction PIG_IRON_MAKING."); - help_text.push_back("- Right world map overlay not implemented as author has failed to"); - help_text.push_back(" emulate the sizing logic exactly."); + help_text.push_back("- Flux determination is made by finding the reaction PIG_IRON_MAKING."); + help_text.push_back("- Coal is detected by finding COAL producing reactions on minerals."); help_text.push_back("- There's currently a DF bug (#0010267) that causes adamantine spires"); help_text.push_back(" reaching caverns that have been removed at world gen to fail to be"); help_text.push_back(" generated at all. It's likely this bug also affects magma pools."); help_text.push_back(" This plugin does not address this but scripts can correct it."); - help_text.push_back("Version 0.7 2018-08-18"); + help_text.push_back("Version 0.8 2018-12-04"); break; } From 8f9cbfeafdf46a6bd2ff01e47dc8ffb5446a3b1c Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Tue, 4 Dec 2018 14:57:23 +0100 Subject: [PATCH 24/32] Added coal search to embark-assistant --- plugins/embark-assistant/matcher.cpp | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/plugins/embark-assistant/matcher.cpp b/plugins/embark-assistant/matcher.cpp index b2509336b..7b3d385cb 100644 --- a/plugins/embark-assistant/matcher.cpp +++ b/plugins/embark-assistant/matcher.cpp @@ -46,6 +46,7 @@ namespace embark_assist { bool clay_found = false; bool sand_found = false; bool flux_found = false; + bool coal_found = false; uint8_t max_soil = 0; bool uneven = false; int16_t min_temperature = survey_results->at(x).at(y).min_temperature[mlt->at(start_x).at(start_y).biome_offset]; @@ -174,6 +175,12 @@ namespace embark_assist { flux_found = true; } + // Coal + if (mlt->at(i).at(k).coal) { + if (finder->coal == embark_assist::defs::present_absent_ranges::Absent) return false; + coal_found = true; + } + // Min Soil if (finder->soil_min != embark_assist::defs::soil_ranges::NA && mlt->at(i).at(k).soil_depth < static_cast(finder->soil_min) && @@ -335,6 +342,9 @@ namespace embark_assist { // Flux if (finder->flux == embark_assist::defs::present_absent_ranges::Present && !flux_found) return false; + // Coal + if (finder->coal == embark_assist::defs::present_absent_ranges::Present && !coal_found) return false; + // Min Soil if (finder->soil_min != embark_assist::defs::soil_ranges::NA && finder->soil_min_everywhere == embark_assist::defs::all_present_ranges::Present && @@ -571,6 +581,7 @@ namespace embark_assist { case embark_assist::defs::present_absent_ranges::Present: if (tile->clay_count == 0) return false; break; + case embark_assist::defs::present_absent_ranges::Absent: if (tile->clay_count > 256 - embark_size) return false; break; @@ -584,6 +595,7 @@ namespace embark_assist { case embark_assist::defs::present_absent_ranges::Present: if (tile->sand_count == 0) return false; break; + case embark_assist::defs::present_absent_ranges::Absent: if (tile->sand_count > 256 - embark_size) return false; break; @@ -597,11 +609,26 @@ namespace embark_assist { case embark_assist::defs::present_absent_ranges::Present: if (tile->flux_count == 0) return false; break; + case embark_assist::defs::present_absent_ranges::Absent: if (tile->flux_count > 256 - embark_size) return false; break; } + // Coal + switch (finder->coal) { + case embark_assist::defs::present_absent_ranges::NA: + break; // No restriction + + case embark_assist::defs::present_absent_ranges::Present: + if (tile->coal_count == 0) return false; + break; + + case embark_assist::defs::present_absent_ranges::Absent: + if (tile->coal_count > 256 - embark_size) return false; + break; + } + // Soil Min switch (finder->soil_min) { case embark_assist::defs::soil_ranges::NA: @@ -1027,6 +1054,7 @@ namespace embark_assist { case embark_assist::defs::present_absent_ranges::Present: if (tile->clay_count == 0) return false; break; + case embark_assist::defs::present_absent_ranges::Absent: if (tile->clay_count == 256) return false; break; @@ -1040,6 +1068,7 @@ namespace embark_assist { case embark_assist::defs::present_absent_ranges::Present: if (tile->sand_count == 0) return false; break; + case embark_assist::defs::present_absent_ranges::Absent: if (tile->sand_count == 256) return false; break; @@ -1053,11 +1082,26 @@ namespace embark_assist { case embark_assist::defs::present_absent_ranges::Present: if (tile->flux_count == 0) return false; break; + case embark_assist::defs::present_absent_ranges::Absent: if (tile->flux_count == 256) return false; break; } + // Coal + switch (finder->coal) { + case embark_assist::defs::present_absent_ranges::NA: + break; // No restriction + + case embark_assist::defs::present_absent_ranges::Present: + if (tile->coal_count == 0) return false; + break; + + case embark_assist::defs::present_absent_ranges::Absent: + if (tile->coal_count == 256) return false; + break; + } + // Soil Min switch (finder->soil_min) { case embark_assist::defs::soil_ranges::NA: From 0916d69373df6e02ef00cb11d427698e76d0f202 Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Tue, 4 Dec 2018 14:57:44 +0100 Subject: [PATCH 25/32] Added coal search to embark-assistant --- plugins/embark-assistant/overlay.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/embark-assistant/overlay.cpp b/plugins/embark-assistant/overlay.cpp index 75e6258a3..c25859ccb 100644 --- a/plugins/embark-assistant/overlay.cpp +++ b/plugins/embark-assistant/overlay.cpp @@ -339,6 +339,10 @@ void embark_assist::overlay::set_embark(embark_assist::defs::site_infos *site_in state->embark_info.push_back({ Screen::Pen(' ', COLOR_RED), "Clay" }); } + if (site_info->coal) { + state->embark_info.push_back({ Screen::Pen(' ', COLOR_GREY), "Coal" }); + } + state->embark_info.push_back({ Screen::Pen(' ', COLOR_BROWN), "Soil " + std::to_string(site_info->min_soil) + " - " + std::to_string(site_info->max_soil) }); if (site_info->flat) { From ef57295c028870ae9d4e1946bbdf869a314eb29e Mon Sep 17 00:00:00 2001 From: PatrikLundell Date: Tue, 4 Dec 2018 14:58:10 +0100 Subject: [PATCH 26/32] Added coal search to embark-assistant --- plugins/embark-assistant/survey.cpp | 60 +++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/plugins/embark-assistant/survey.cpp b/plugins/embark-assistant/survey.cpp index 4071d78b3..f19f3cd4a 100644 --- a/plugins/embark-assistant/survey.cpp +++ b/plugins/embark-assistant/survey.cpp @@ -10,6 +10,7 @@ #include "modules/Materials.h" #include "DataDefs.h" +#include "df/builtin_mats.h" #include "df/coord2d.h" #include "df/creature_interaction_effect.h" #include "df/creature_interaction_effect_display_symbolst.h" @@ -34,6 +35,9 @@ #include "df/interaction_target_materialst.h" #include "df/material_common.h" #include "df/reaction.h" +#include "df/reaction_product.h" +#include "df/reaction_product_itemst.h" +#include "df/reaction_product_type.h" #include "df/region_map_entry.h" #include "df/syndrome.h" #include "df/viewscreen.h" @@ -66,6 +70,7 @@ namespace embark_assist { struct states { uint16_t clay_reaction = -1; uint16_t flux_reaction = -1; + std::vector coals; uint16_t x; uint16_t y; uint8_t local_min_x; @@ -104,6 +109,19 @@ namespace embark_assist { out.printerr("The reaction 'PIG_IRON_MAKING' was not found, so flux can't be identified.\n"); } + for (uint16_t i = 0; i < world->raws.inorganics.size(); i++) { + for (uint16_t k = 0; k < world->raws.inorganics[i]->economic_uses.size(); k++) { + for (uint16_t l = 0; l < world->raws.reactions.reactions[world->raws.inorganics[i]->economic_uses[k]]->products.size(); l++) { + df::reaction_product_itemst *product = static_cast(world->raws.reactions.reactions[world->raws.inorganics[i]->economic_uses[k]]->products[l]); + + if (product->mat_type == df::builtin_mats::COAL) { + state->coals.push_back(i); + break; + } + } + } + } + for (uint16_t i = 0; i < world_data->geo_biomes.size(); i++) { geo_summary->at(i).possible_metals.resize(state->max_inorganic); geo_summary->at(i).possible_economics.resize(state->max_inorganic); @@ -154,6 +172,13 @@ namespace embark_assist { } } + for (uint16_t l = 0; l < state->coals.size(); l++) { + if (layer->mat_index == state->coals[l]) { + geo_summary->at(i).coal_absent = false; + break; + } + } + size = (uint16_t)layer->vein_mat.size(); for (uint16_t l = 0; l < size; l++) { @@ -176,6 +201,14 @@ namespace embark_assist { geo_summary->at(i).flux_absent = false; } } + + for (uint16_t m = 0; m < state->coals.size(); m++) { + if (vein== state->coals[m]) { + geo_summary->at(i).coal_absent = false; + break; + } + } + } } @@ -531,6 +564,7 @@ void embark_assist::survey::high_level_world_survey(embark_assist::defs::geo_dat results.clay_count = 0; results.sand_count = 0; results.flux_count = 0; + results.coal_count = 0; results.min_region_soil = 10; results.max_region_soil = 0; results.waterfall = false; @@ -576,6 +610,7 @@ void embark_assist::survey::high_level_world_survey(embark_assist::defs::geo_dat if (!geo_summary->at(geo_index).clay_absent) results.clay_count++; if (!geo_summary->at(geo_index).sand_absent) results.sand_count++; if (!geo_summary->at(geo_index).flux_absent) results.flux_count++; + if (!geo_summary->at(geo_index).coal_absent) results.coal_count++; if (geo_summary->at(geo_index).soil_size < results.min_region_soil) results.min_region_soil = geo_summary->at(geo_index).soil_size; @@ -614,6 +649,8 @@ void embark_assist::survey::high_level_world_survey(embark_assist::defs::geo_dat if (results.clay_count == offset_count) results.clay_count = 256; if (results.sand_count == offset_count) results.sand_count = 256; if (results.flux_count == offset_count) results.flux_count = 256; + if (results.coal_count == offset_count) results.coal_count = 256; + for (uint8_t l = 0; l < 3; l++) { if (results.savagery_count[l] == offset_count) results.savagery_count[l] = 256; if (results.evilness_count[l] == offset_count) results.evilness_count[l] = 256; @@ -776,6 +813,8 @@ void embark_assist::survey::survey_mid_level_tile(embark_assist::defs::geo_data mlt->at(i).at(k).clay = false; mlt->at(i).at(k).sand = false; mlt->at(i).at(k).flux = false; + mlt->at(i).at(k).coal = false; + if (max_soil_depth == 0) { mlt->at(i).at(k).soil_depth = 0; } @@ -869,6 +908,13 @@ void embark_assist::survey::survey_mid_level_tile(embark_assist::defs::geo_data mlt->at(i).at(k).flux = true; } } + + for (uint16_t m = 0; m < state->coals.size(); m++) { + if (layer->mat_index == state->coals [m]) { + mlt->at(i).at(k).coal = true; + break; + } + } } end_check_m = static_cast(layer->vein_mat.size()); @@ -895,6 +941,13 @@ void embark_assist::survey::survey_mid_level_tile(embark_assist::defs::geo_data mlt->at(i).at(k).flux = true; } } + + for (uint16_t n = 0; n < state->coals.size(); n++) { + if (layer->vein_mat [m] == state->coals[n]) { + mlt->at(i).at(k).coal = true; + break; + } + } } } @@ -911,6 +964,7 @@ void embark_assist::survey::survey_mid_level_tile(embark_assist::defs::geo_data survey_results->at(x).at(y).clay_count = 0; survey_results->at(x).at(y).sand_count = 0; survey_results->at(x).at(y).flux_count = 0; + survey_results->at(x).at(y).coal_count = 0; survey_results->at(x).at(y).min_region_soil = 10; survey_results->at(x).at(y).max_region_soil = 0; survey_results->at(x).at(y).savagery_count[0] = 0; @@ -929,6 +983,7 @@ void embark_assist::survey::survey_mid_level_tile(embark_assist::defs::geo_data if (mlt->at(i).at(k).clay) { survey_results->at(x).at(y).clay_count++; } if (mlt->at(i).at(k).sand) { survey_results->at(x).at(y).sand_count++; } if (mlt->at(i).at(k).flux) { survey_results->at(x).at(y).flux_count++; } + if (mlt->at(i).at(k).coal) { survey_results->at(x).at(y).coal_count++; } if (mlt->at(i).at(k).soil_depth < survey_results->at(x).at(y).min_region_soil) { survey_results->at(x).at(y).min_region_soil = mlt->at(i).at(k).soil_depth; @@ -1170,6 +1225,7 @@ void embark_assist::survey::survey_embark(embark_assist::defs::mid_level_tiles * site_info->clay = false; site_info->sand = false; site_info->flux = false; + site_info->coal = false; site_info->metals.clear(); site_info->economics.clear(); site_info->metals.clear(); @@ -1222,6 +1278,10 @@ void embark_assist::survey::survey_embark(embark_assist::defs::mid_level_tiles * site_info->flux = true; } + if (mlt->at(i).at(k).coal) { + site_info->coal = true; + } + for (uint16_t l = 0; l < state->max_inorganic; l++) { metals[l] = metals[l] || mlt->at(i).at(k).metals[l]; economics[l] = economics[l] || mlt->at(i).at(k).economics[l]; From 0ccfc8be35aec38313932f6967bc4cd4806c4b60 Mon Sep 17 00:00:00 2001 From: Lethosor Date: Thu, 27 Dec 2018 16:21:24 -0500 Subject: [PATCH 27/32] Fix a few typos in docs and reorganize a bit --- docs/Plugins.rst | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/docs/Plugins.rst b/docs/Plugins.rst index bbeeb813a..6eb6326bb 100644 --- a/docs/Plugins.rst +++ b/docs/Plugins.rst @@ -396,28 +396,24 @@ Otherwise somewhat similar to `gui/quickcmd`. debug ===== -Manager DFHack runtime debug prints. Debug prints are grouped by plugin name, +Manager for DFHack runtime debug prints. Debug prints are grouped by plugin name, category name and print level. Levels are ``trace``, ``debug``, ``info``, ``warning`` and ``error``. -The runtime message printing is controlled using filters. Filters set minimum -visible message to all matching categories. Matching uses regular expression -that allows listing multiple alternative matches or partial name matches. -Persistent filters are stored in ``dfhack-config/runtime-debug.json``. +The runtime message printing is controlled using filters. Filters set the +visible messages of all matching categories. Matching uses regular expression syntax, +which allows listing multiple alternative matches or partial name matches. +This syntax is a C++ version of the ECMA-262 grammar (Javascript regular expressions). +Details of differences can be found at +https://en.cppreference.com/w/cpp/regex/ecmascript +Persistent filters are stored in ``dfhack-config/runtime-debug.json``. Oldest filters are applied first. That means a newer filter can override the older printing level selection. Usage: ``debugfilter [subcommand] [parameters...]`` -Following subcommands are supported. - -Regular expression syntax -------------------------- - -Syntax is C++ version of ECMA-262 grammar (Javascript regular expression). -Deails of differences can be found from -https://en.cppreference.com/w/cpp/regex/ecmascript +The following subcommands are supported: help ---- @@ -466,7 +462,7 @@ Level is the minimum debug printing level to show in log. * ``info``: Important state changes that happen rarely during normal execution -* ``warining``: Enabled by default. Shows warnings about unexpected events which code managed to handle correctly. +* ``warning``: Enabled by default. Shows warnings about unexpected events which code managed to handle correctly. * ``error``: Enabled by default. Shows errors which code can't handle without user intervention. From 32edeffc3fec6a04ec45b75fe9540e4c4970c576 Mon Sep 17 00:00:00 2001 From: lethosor Date: Thu, 27 Dec 2018 16:31:57 -0500 Subject: [PATCH 28/32] Remove unused find_package(Threads) --- plugins/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 6eb4ae6ff..284922cc1 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -1,7 +1,5 @@ INCLUDE(Plugins.cmake) -find_package(Threads) - OPTION(BUILD_STONESENSE "Build stonesense (needs a checkout first)." OFF) if(BUILD_STONESENSE) add_subdirectory (stonesense) From e74946f62e7a0f7509a4c4a76bc9a3b7ca79cc79 Mon Sep 17 00:00:00 2001 From: lethosor Date: Thu, 27 Dec 2018 17:37:13 -0500 Subject: [PATCH 29/32] Update xml and related changelog entries --- docs/changelog.txt | 37 ++++++++++++++++++++++++++++++++----- library/xml | 2 +- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index 380636198..d6853be31 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -45,12 +45,14 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - `building-hacks`: fixed error when dealing with custom animation tables - `devel/test-perlin`: fixed Lua error (``math.pow()``) - `embark-assistant`: fixed crash when entering finder with a 16x16 embark selected, and added 16 to dimension choices +- `embark-skills`: fixed missing ``skill_points_remaining`` field - `labormanager`: - stopped assigning labors to ineligible dwarves, pets, etc. - stopped assigning invalid labors - added support for crafting jobs that use pearl - `prospector`: (also affected `embark-tools`) - fixed a crash when prospecting an unusable site (ocean, mountains, etc.) with a large default embark size in d_init.txt (e.g. 16x16) - `siege-engine`: fixed a few Lua errors (``math.pow()``, ``unit.relationship_ids``) +- `tweak`: fixed ``hotkey-clear`` ## Misc Improvements - `devel/export-dt-ini`: added viewscreen offsets for DT 40.1.2 @@ -72,14 +74,39 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - ``utils``: new ``OrderedTable`` class ## Structures -- ``musical_form``: named voices field -- ``musical_form_instruments``: named minimum_required and maximum_permitted -- ``dance_form``: named musical_form_id and musical_written_content_id -- ``written_content``: named poetic_form +- Win32: added missing vtables for ``viewscreen_storesst`` and ``squad_order_rescue_hfst`` - ``activity_event_performancest``: renamed poem as written_content_id -- ``incident_sub6_performance``: made performance_event an enum +- ``dance_form``: named musical_form_id and musical_written_content_id - ``incident_sub6_performance.participants``: named performance_event and role_index +- ``incident_sub6_performance``: made performance_event an enum - ``incident_sub6_performance``: named poetic_form_id, musical_form_id, and dance_form_id +- ``musical_form_instruments``: named minimum_required and maximum_permitted +- ``musical_form``: named voices field +- ``poetic_form``: identified many fields and related enum/bitfield types +- ``setup_character_info``: identified ``skill_points_remaining`` (for `embark-skills`) +- ``unit_thought_type``: added new expulsion thoughts from 0.44.12 +- ``viewscreen_layer_militaryst``: identified ``equip.assigned.assigned_items`` +- ``world_data``: added ``mountain_peak_flags`` type, including ``is_volcano`` +- ``written_content``: named poetic_form +- ``unit_action.attack``: identified ``attack_skill`` +- ``unit_action.attack``: added ``lightly_tap`` and ``spar_report`` flags +- ``misc_trait_type``: removed ``LikesOutdoors``, ``Hardened``, ``TimeSinceBreak``, ``OnBreak`` (all unused by DF) +- ``unit_personality``: identified ``stress_drain``, ``stress_boost``, ``likes_outdoors``, ``combat_hardened`` +- ``plant_tree_tile``: gave connection bits more meaningful names (e.g. ``connection_east`` instead of ``thick_branches_1``) +- ``plant_tree_info``: identified ``extent_east``, etc. +- ``ui``: fixed alignment of ``main`` and ``squads`` (fixes `tweak` hotkey-clear and DF-AI) +- ``ui.main``: identified ``fortress_site`` +- ``ui.squads``: identified ``kill_rect_targets_scroll`` +- ``world_site``: identified names and/or types of some fields +- ``world_history``: identified names and/or types of some fields +- ``viewscreen_setupadventurest``: identified some nemesis and personality fields, and ``page.ChooseHistfig`` +- ``unit_storage_status``: newly identified type, stores noble holdings information (used in ``viewscreen_layer_noblelistst``) +- ``viewscreen_layer_noblelistst``: identified ``storage_status`` (see ``unit_storage_status`` type) +- ``viewscreen_layer_arena_creaturest``: identified item- and name-related fields +- ``viewscreen_new_regionst``: identified ``rejection_msg``, ``raw_folder``, ``load_world_params`` +- ``viewscreen_new_regionst``: changed many ``int8_t`` fields to ``bool`` +- ``unit_flags3``: identified ``marked_for_gelding`` +- ``body_part_status``: identified ``gelded`` ## API - New debug features related to `debug` plugin: diff --git a/library/xml b/library/xml index fa6e818b3..44215836d 160000 --- a/library/xml +++ b/library/xml @@ -1 +1 @@ -Subproject commit fa6e818b34235b9fe10abc37d06eba27a932c870 +Subproject commit 44215836d5b57c3722b126aaf481f652385f3a23 From f8dd215012534a8d6527bc01772f2365a4048fdf Mon Sep 17 00:00:00 2001 From: lethosor Date: Thu, 27 Dec 2018 17:57:55 -0500 Subject: [PATCH 30/32] Update scripts and related changelog entries --- docs/changelog.txt | 29 ++++++++++++++++++++++++++++- scripts | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index d6853be31..d99eabb7f 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -41,11 +41,25 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - `debug`: manages runtime debug print category filtering - `nestboxes`: automatically scan for and forbid fertile eggs incubating in a nestbox +## New Scripts +- `devel/query`: searches for field names in DF objects +- `extinguish`: puts out fires +- `tame`: sets tamed/trained status of animals + ## Fixes - `building-hacks`: fixed error when dealing with custom animation tables - `devel/test-perlin`: fixed Lua error (``math.pow()``) - `embark-assistant`: fixed crash when entering finder with a 16x16 embark selected, and added 16 to dimension choices - `embark-skills`: fixed missing ``skill_points_remaining`` field +- `full-heal`: + - stopped wagon resurrection + - fixed a minor issue with post-resurrection hostility +- `gui/companion-order`: + - fixed issues with printing coordinates + - fixed issues with move command + - fixed cheat commands (and removed "Power up", which was broken) +- `gui/gm-editor`: fixed reinterpret cast (``r``) +- `gui/pathable`: fixed error when sidebar is hidden with ``Tab`` - `labormanager`: - stopped assigning labors to ineligible dwarves, pets, etc. - stopped assigning invalid labors @@ -55,12 +69,25 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - `tweak`: fixed ``hotkey-clear`` ## Misc Improvements -- `devel/export-dt-ini`: added viewscreen offsets for DT 40.1.2 +- `armoks-blessing`: improved documentation to list all available arguments +- `devel/export-dt-ini`: + - added viewscreen offsets for DT 40.1.2 + - added item base flags offset + - added needs offsets - `embark-assistant`: - added match indicator display on the right ("World") map - changed 'c'ancel to abort find if it's under way and clear results if not, allowing use of partial surveys. - added Coal as a search criterion, as well as a coal indication as current embark selection info. +- `full-heal`: + - added ``-all``, ``-all_civ`` and ``-all_citizens`` arguments + - added module support + - now removes historical figure death dates and ghost data +- `growcrops`: added ``all`` argument to grow all crops +- `gui/load-screen`: improved documentation - `labormanager`: now takes nature value into account when assigning jobs +- `open-legends`: added warning about risk of save corruption and improved related documentation +- `points`: added support when in ``viewscreen_setupdwarfgamest`` and improved error messages +- `siren`: removed break handling (relevant ``misc_trait_type`` was no longer used - see "Structures" section) ## Internals - Linux/macOS: changed recommended build backend from Make to Ninja (Make builds will be significantly slower now) diff --git a/scripts b/scripts index 7deb13c68..a242a7cd8 160000 --- a/scripts +++ b/scripts @@ -1 +1 @@ -Subproject commit 7deb13c681b011d9a87782b9cdf9eec6e6acf7c1 +Subproject commit a242a7cd81ae7046146f86c3103360449e1d9ca8 From 9fe24e1b3fe01fcbb15e982967464971b3921b85 Mon Sep 17 00:00:00 2001 From: lethosor Date: Thu, 27 Dec 2018 19:39:43 -0500 Subject: [PATCH 31/32] Update changelog and bump version to r2 --- CMakeLists.txt | 2 +- docs/Authors.rst | 2 ++ docs/changelog.txt | 7 ++++++- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e05e5cf2b..689618637 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -168,7 +168,7 @@ endif() # set up versioning. set(DF_VERSION "0.44.12") -set(DFHACK_RELEASE "r1") +set(DFHACK_RELEASE "r2") set(DFHACK_PRERELEASE FALSE) set(DFHACK_VERSION "${DF_VERSION}-${DFHACK_RELEASE}") diff --git a/docs/Authors.rst b/docs/Authors.rst index 7233e558a..ba3a00b35 100644 --- a/docs/Authors.rst +++ b/docs/Authors.rst @@ -23,6 +23,7 @@ Bearskie Bearskie belal jimhester Ben Lubar BenLubar Ben Rosser TC01 +billw2012 billw2012 brndd brndd burneddi Bumber Bumber64 Caldfir caldfir @@ -68,6 +69,7 @@ Kromtec Kromtec Kurik Amudnil Lethosor lethosor Mason11987 Mason11987 +Matt Regul mattregul Matthew Cline Matthew Lindner mlindner Max maxthyme Max^TM diff --git a/docs/changelog.txt b/docs/changelog.txt index d99eabb7f..4db4072f0 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -37,6 +37,8 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ================================================================================ # Future +# 0.44.12-r2 + ## New Plugins - `debug`: manages runtime debug print category filtering - `nestboxes`: automatically scan for and forbid fertile eggs incubating in a nestbox @@ -64,6 +66,8 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - stopped assigning labors to ineligible dwarves, pets, etc. - stopped assigning invalid labors - added support for crafting jobs that use pearl + - fixed issues causing cleaning jobs to not be assigned + - added support for disabling management of specific labors - `prospector`: (also affected `embark-tools`) - fixed a crash when prospecting an unusable site (ocean, mountains, etc.) with a large default embark size in d_init.txt (e.g. 16x16) - `siege-engine`: fixed a few Lua errors (``math.pow()``, ``unit.relationship_ids``) - `tweak`: fixed ``hotkey-clear`` @@ -93,9 +97,10 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - Linux/macOS: changed recommended build backend from Make to Ninja (Make builds will be significantly slower now) - Added a usable unit test framework for basic tests, and a few basic tests - Core: various thread safety and memory management improvements -- Fixed cmake build dependencies for generated header files +- Fixed CMake build dependencies for generated header files - Fixed custom ``CMAKE_CXX_FLAGS`` not being passed to plugins - Changed ``plugins/CMakeLists.custom.txt`` to be ignored by git and created (if needed) at build time instead +- Added ``CMakeSettings.json`` with intellisense support ## Lua - ``utils``: new ``OrderedTable`` class From 315852a2513679ca1afe0d53b34974cefd946f89 Mon Sep 17 00:00:00 2001 From: lethosor Date: Thu, 27 Dec 2018 19:46:36 -0500 Subject: [PATCH 32/32] labormanager: fix -Wreorder warning --- plugins/labormanager/labormanager.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/labormanager/labormanager.cpp b/plugins/labormanager/labormanager.cpp index d779e036a..e416dcf63 100644 --- a/plugins/labormanager/labormanager.cpp +++ b/plugins/labormanager/labormanager.cpp @@ -535,7 +535,8 @@ struct dwarf_info_t df::unit_labor using_labor; dwarf_info_t(df::unit* dw) : dwarf(dw), state(OTHER), - clear_all(false), high_skill(0), has_children(false), armed(false), using_labor(df::unit_labor::NONE), unmanaged_labors_assigned(0) + clear_all(false), high_skill(0), has_children(false), armed(false), + unmanaged_labors_assigned(0), using_labor(df::unit_labor::NONE) { for (int e = TOOL_NONE; e < TOOLS_MAX; e++) has_tool[e] = false; @@ -2233,7 +2234,7 @@ void print_labor(df::unit_labor labor, color_ostream &out) else { out << "priority " << labor_info.priority(); - + if (labor_info.maximum_dwarfs() == MAX_DWARFS_NONE) out << ", no maximum"; else