From 2f9021a3a0911d1cd39108535e02b91f4abb8db3 Mon Sep 17 00:00:00 2001 From: myk002 Date: Tue, 2 Aug 2022 12:56:44 -0700 Subject: [PATCH 1/9] move examples to the examples folder --- docs/Dev-intro.rst | 4 +-- plugins/CMakeLists.txt | 6 ---- plugins/{skeleton => examples}/skeleton.cpp | 0 .../{skeleton => examples}/skeletonShort.cpp | 0 plugins/skeleton/CMakeLists.txt | 36 ------------------- plugins/skeleton/skeleton.h | 1 - 6 files changed, 2 insertions(+), 45 deletions(-) rename plugins/{skeleton => examples}/skeleton.cpp (100%) rename plugins/{skeleton => examples}/skeletonShort.cpp (100%) delete mode 100644 plugins/skeleton/CMakeLists.txt delete mode 100644 plugins/skeleton/skeleton.h diff --git a/docs/Dev-intro.rst b/docs/Dev-intro.rst index 758bf225f..d4167aae3 100644 --- a/docs/Dev-intro.rst +++ b/docs/Dev-intro.rst @@ -22,7 +22,7 @@ Plugins DFHack plugins are written in C++ and located in the ``plugins`` folder. Currently, documentation on how to write plugins is somewhat sparse. There are -templates that you can use to get started in the ``plugins/skeleton`` +templates that you can use to get started in the ``plugins/examples`` folder, and the source code of existing plugins can also be helpful. If you want to compile a plugin that you have just added, you will need to add a @@ -35,7 +35,7 @@ other commands). Plugins can also register handlers to run on every tick, and can interface with the built-in `enable` and `disable` commands. For the full plugin API, see the -skeleton plugins or ``PluginManager.cpp``. +example plugins or ``PluginManager.cpp``. Installed plugins live in the ``hack/plugins`` folder of a DFHack installation, and the `load` family of commands can be used to load a recompiled plugin diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 338db4ab9..6a34117b9 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -185,12 +185,6 @@ if(BUILD_SUPPORTED) # see instructions for adding "external" plugins at the end of this file. endif() -# this is the skeleton plugin. If you want to make your own, make a copy and then change it -option(BUILD_SKELETON "Build the skeleton plugin." OFF) -if(BUILD_SKELETON) - add_subdirectory(skeleton) -endif() - macro(subdirlist result subdir) file(GLOB children ABSOLUTE ${subdir}/ ${subdir}/*/) set(dirlist "") diff --git a/plugins/skeleton/skeleton.cpp b/plugins/examples/skeleton.cpp similarity index 100% rename from plugins/skeleton/skeleton.cpp rename to plugins/examples/skeleton.cpp diff --git a/plugins/skeleton/skeletonShort.cpp b/plugins/examples/skeletonShort.cpp similarity index 100% rename from plugins/skeleton/skeletonShort.cpp rename to plugins/examples/skeletonShort.cpp diff --git a/plugins/skeleton/CMakeLists.txt b/plugins/skeleton/CMakeLists.txt deleted file mode 100644 index cbe5f7ce6..000000000 --- a/plugins/skeleton/CMakeLists.txt +++ /dev/null @@ -1,36 +0,0 @@ -project(skeleton) -# A list of source files -set(PROJECT_SRCS - skeleton.cpp -) -# A list of headers -set(PROJECT_HDRS - skeleton.h -) -set_source_files_properties(${PROJECT_HDRS} PROPERTIES HEADER_FILE_ONLY TRUE) - -# mash them together (headers are marked as headers and nothing will try to compile them) -list(APPEND PROJECT_SRCS ${PROJECT_HDRS}) - -# option to use a thread for no particular reason -option(SKELETON_THREAD "Use threads in the skeleton plugin." ON) -if(UNIX) - if(APPLE) - set(PROJECT_LIBS - # add any extra mac libraries here - ${PROJECT_LIBS} - ) - else() - set(PROJECT_LIBS - # add any extra linux libraries here - ${PROJECT_LIBS} - ) - endif() -else() - set(PROJECT_LIBS - # add any extra windows libraries here - ${PROJECT_LIBS} - ) -endif() -# this makes sure all the stuff is put in proper places and linked to dfhack -dfhack_plugin(skeleton ${PROJECT_SRCS} LINK_LIBRARIES ${PROJECT_LIBS}) diff --git a/plugins/skeleton/skeleton.h b/plugins/skeleton/skeleton.h deleted file mode 100644 index 6f70f09be..000000000 --- a/plugins/skeleton/skeleton.h +++ /dev/null @@ -1 +0,0 @@ -#pragma once From 0bbbacf161aa0f4bd4869d108f56d85f9c5ec8e6 Mon Sep 17 00:00:00 2001 From: myk002 Date: Tue, 2 Aug 2022 18:40:50 -0700 Subject: [PATCH 2/9] extend the docs and examples in skeleton.cpp --- plugins/examples/skeleton.cpp | 221 +++++++++++++++++++++------------- 1 file changed, 134 insertions(+), 87 deletions(-) diff --git a/plugins/examples/skeleton.cpp b/plugins/examples/skeleton.cpp index 7d5936f6d..e1f552d31 100644 --- a/plugins/examples/skeleton.cpp +++ b/plugins/examples/skeleton.cpp @@ -1,65 +1,71 @@ -// This is a generic plugin that does nothing useful apart from acting as an example... of a plugin that does nothing :D +// This is an example plugin that just documents and implements all the plugin +// callbacks and features. You can compile it, load it, run it, and see the +// debug messages get printed to the console. +// +// See the other example plugins in this directory for plugins that are +// configured for specific use cases (but don't come with as many comments as +// this one does). + +#include +#include + +#include "df/world.h" -// some headers required for a plugin. Nothing special, just the basics. #include "Core.h" -#include -#include -#include -#include -// If you need to save data per-world: -//#include "modules/Persistence.h" +#include "Debug.h" +#include "LuaTools.h" +#include "PluginManager.h" -// DF data structure definition headers -#include "DataDefs.h" -//#include "df/world.h" +#include "modules/Persistence.h" +#include "modules/World.h" -// our own, empty header. -#include "skeleton.h" +using std::string; +using std::vector; using namespace DFHack; -using namespace df::enums; -// Expose the plugin name to the DFHack core, as well as metadata like the DFHack version. -// The name string provided must correspond to the filename - +// Expose the plugin name to the DFHack core, as well as metadata like the +// DFHack version that this plugin was compiled with. This macro provides a +// variable for the plugin name as const char * plugin_name. +// The name provided must correspond to the filename -- // skeleton.plug.so, skeleton.plug.dylib, or skeleton.plug.dll in this case DFHACK_PLUGIN("skeleton"); -// The identifier declared with this macro (ie. enabled) can be specified by the user -// and subsequently used to manage the plugin's operations. -// This will also be tracked by `plug`; when true the plugin will be shown as enabled. -DFHACK_PLUGIN_IS_ENABLED(enabled); +// The identifier declared with this macro (i.e. is_enabled) is used to track +// whether the plugin is in an "enabled" state. If you don't need enablement +// for your plugin, you don't need this line. This variable will also be read +// by the `plug` builtin command; when true the plugin will be shown as enabled. +DFHACK_PLUGIN_IS_ENABLED(is_enabled); // Any globals a plugin requires (e.g. world) should be listed here. // For example, this line expands to "using df::global::world" and prevents the -// plugin from being loaded if df::global::world is null (i.e. missing from symbols.xml): -// +// plugin from being loaded if df::global::world is null (i.e. missing from +// symbols.xml). REQUIRE_GLOBAL(world); -// You may want some compile time debugging options -// one easy system just requires you to cache the color_ostream &out into a global debug variable -//#define P_DEBUG 1 -//uint16_t maxTickFreq = 1200; //maybe you want to use some events +// logging levels can be dynamically controlled with the `debugfilter` command. +namespace DFHack { + // for configuration-related logging + DBG_DECLARE(skeleton, status, DebugCategory::LINFO); + // for logging during the periodic scan + DBG_DECLARE(skeleton, cycle, DebugCategory::LINFO); +} -command_result command_callback1(color_ostream &out, std::vector ¶meters); +command_result command_callback1(color_ostream &out, vector ¶meters); +// run when the plugin is loaded DFhackCExport command_result plugin_init(color_ostream &out, std::vector &commands) { - commands.push_back(PluginCommand("skeleton", - "~54 character description of plugin", //to use one line in the ``[DFHack]# ls`` output - command_callback1, - false, - "example usage" - " skeleton