From fc02f3fdad489597f2c2bf1e792e5dc49df2605f Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 5 Nov 2017 20:05:48 +0100 Subject: [PATCH] plugin: Allow new filters and transitions to self-register --- source/plugin.cpp | 9 +++++++++ source/plugin.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/source/plugin.cpp b/source/plugin.cpp index 0cf70243..96dfee31 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -32,15 +32,24 @@ Filter::Displacement *filterDisplacement; Filter::Shape *filterShape; Filter::Transform *filterTransform; +std::list> initializerFunctions; +std::list> finalizerFunctions; + MODULE_EXPORT bool obs_module_load(void) { filterDisplacement = new Filter::Displacement(); filterShape = new Filter::Shape(); filterTransform = new Filter::Transform(); filterBlur = new Filter::Blur(); + for (auto func : initializerFunctions) { + func(); + } return true; } MODULE_EXPORT void obs_module_unload(void) { + for (auto func : finalizerFunctions) { + func(); + } delete filterTransform; delete filterShape; delete filterDisplacement; diff --git a/source/plugin.h b/source/plugin.h index b8ed7126..60b885f4 100644 --- a/source/plugin.h +++ b/source/plugin.h @@ -19,6 +19,8 @@ #pragma once #include +#include +#include #pragma warning (push) #pragma warning (disable: 4201) @@ -57,3 +59,30 @@ #define __FUNCTION_NAME__ __func__ #endif #endif + +#ifdef __cplusplus +#define INITIALIZER(f) \ + static void f(void); \ + struct f##_t_ { f##_t_(void) { f(); } }; static f##_t_ f##_; \ + static void f(void) +#elif defined(_MSC_VER) +#pragma section(".CRT$XCU",read) +#define INITIALIZER2_(f,p) \ + static void f(void); \ + __declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \ + __pragma(comment(linker,"/include:" p #f "_")) \ + static void f(void) +#ifdef _WIN64 +#define INITIALIZER(f) INITIALIZER2_(f,"") +#else +#define INITIALIZER(f) INITIALIZER2_(f,"_") +#endif +#else +#define INITIALIZER(f) \ + static void f(void) __attribute__((constructor)); \ + static void f(void) +#endif + +// Initializer & Finalizer +extern std::list> initializerFunctions; +extern std::list> finalizerFunctions;