From b4092f62e6bcbd5c2246d589d75523610d3a1616 Mon Sep 17 00:00:00 2001 From: expwnent Date: Sat, 23 Mar 2013 22:38:33 -0400 Subject: [PATCH] New module: Once. Intended to help prevent debug error message spam. --- library/CMakeLists.txt | 2 ++ library/include/modules/Once.h | 11 +++++++++++ library/modules/Once.cpp | 16 ++++++++++++++++ plugins/devel/CMakeLists.txt | 1 + plugins/devel/onceExample.cpp | 35 ++++++++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+) create mode 100644 library/include/modules/Once.h create mode 100644 library/modules/Once.cpp create mode 100644 plugins/devel/onceExample.cpp diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 64dafd542..b0474ec9c 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -125,6 +125,7 @@ include/modules/Translation.h include/modules/Vermin.h include/modules/World.h include/modules/Graphic.h +include/modules/Once.h ) SET( MODULE_SOURCES @@ -147,6 +148,7 @@ modules/Vermin.cpp modules/World.cpp modules/Graphic.cpp modules/Windows.cpp +modules/Once.cpp ) IF(WIN32) diff --git a/library/include/modules/Once.h b/library/include/modules/Once.h new file mode 100644 index 000000000..7bfd819ec --- /dev/null +++ b/library/include/modules/Once.h @@ -0,0 +1,11 @@ +#pragma once +#include "Export.h" +#include + +namespace DFHack { + namespace Once { + DFHACK_EXPORT bool alreadyDone(std::string); + DFHACK_EXPORT bool doOnce(std::string); + } +} + diff --git a/library/modules/Once.cpp b/library/modules/Once.cpp new file mode 100644 index 000000000..0ef8b22c2 --- /dev/null +++ b/library/modules/Once.cpp @@ -0,0 +1,16 @@ + +#include "modules/Once.h" +#include + +using namespace std; + +static unordered_set thingsDone; + +bool DFHack::Once::alreadyDone(string bob) { + return thingsDone.find(bob) != thingsDone.end(); +} + +bool DFHack::Once::doOnce(string bob) { + return thingsDone.insert(bob).second; +} + diff --git a/plugins/devel/CMakeLists.txt b/plugins/devel/CMakeLists.txt index 89e129d35..e20e20ce8 100644 --- a/plugins/devel/CMakeLists.txt +++ b/plugins/devel/CMakeLists.txt @@ -21,6 +21,7 @@ DFHACK_PLUGIN(vshook vshook.cpp) DFHACK_PLUGIN(autolabor2 autolabor2.cpp) DFHACK_PLUGIN(eventExample eventExample.cpp) DFHACK_PLUGIN(printArgs printArgs.cpp) +DFHACK_PLUGIN(onceExample onceExample.cpp) IF(UNIX) DFHACK_PLUGIN(ref-index ref-index.cpp) ENDIF() diff --git a/plugins/devel/onceExample.cpp b/plugins/devel/onceExample.cpp new file mode 100644 index 000000000..75cec0841 --- /dev/null +++ b/plugins/devel/onceExample.cpp @@ -0,0 +1,35 @@ + +#include "Core.h" +#include "Console.h" +#include "DataDefs.h" +#include "Export.h" +#include "PluginManager.h" + +#include "modules/Once.h" + +using namespace DFHack; +using namespace df::enums; + +command_result onceExample (color_ostream &out, std::vector & parameters); + +DFHACK_PLUGIN("onceExample"); + +DFhackCExport command_result plugin_init ( color_ostream &out, std::vector &commands) +{ + commands.push_back(PluginCommand( + "onceExample", "Test the doOnce command.", + onceExample, false, + " This command tests the doOnce command..\n" + )); + return CR_OK; +} + +command_result onceExample (color_ostream &out, std::vector & parameters) +{ + out.print("Already done = %d.\n", DFHack::Once::alreadyDone("onceExample_1")); + if ( DFHack::Once::doOnce("onceExample_1") ) { + out.print("Printing this message once!\n"); + } + return CR_OK; +} +