diff options
-rw-r--r-- | facetracknoir/plugin-support.cpp | 69 | ||||
-rw-r--r-- | facetracknoir/plugin-support.h | 2 |
2 files changed, 55 insertions, 16 deletions
diff --git a/facetracknoir/plugin-support.cpp b/facetracknoir/plugin-support.cpp index 2e80c5e4..50b8ad1a 100644 --- a/facetracknoir/plugin-support.cpp +++ b/facetracknoir/plugin-support.cpp @@ -76,19 +76,44 @@ SelectedLibraries::SelectedLibraries(IDynamicLibraryProvider* mainApp) : correct = true; } -DynamicLibrary::DynamicLibrary(const QString& filename) +DynamicLibrary::DynamicLibrary(const QString& filename) : + handle(nullptr), + Dialog(nullptr), + Constructor(nullptr), + Metadata(nullptr) { this->filename = filename; #if defined(_WIN32) QString fullPath = QCoreApplication::applicationDirPath() + "/" + this->filename; handle = new QLibrary(fullPath); - qDebug() << handle->errorString(); + + struct _foo { + static bool die(QLibrary*& l, bool failp) + { + if (failp) + { + qDebug() << "failed" << l->errorString(); + delete l; + l = nullptr; + } + return failp; + } + }; + + if (_foo::die(handle, !handle->load())) + return; + Dialog = (DIALOG_FUNPTR) handle->resolve(MAYBE_STDCALL_UNDERSCORE "GetDialog" CALLING_CONVENTION_SUFFIX_VOID_FUNCTION); - qDebug() << handle->errorString(); + if (_foo::die(handle, !Dialog)) + return; + Constructor = (CTOR_FUNPTR) handle->resolve(MAYBE_STDCALL_UNDERSCORE "GetConstructor" CALLING_CONVENTION_SUFFIX_VOID_FUNCTION); - qDebug() << handle->errorString(); + if (_foo::die(handle, !Constructor)) + return; + Metadata = (METADATA_FUNPTR) handle->resolve(MAYBE_STDCALL_UNDERSCORE "GetMetadata" CALLING_CONVENTION_SUFFIX_VOID_FUNCTION); - qDebug() << handle->errorString(); + if (_foo::die(handle, !Metadata)) + return; #else QByteArray latin1 = QFile::encodeName(filename); handle = dlopen(latin1.constData(), RTLD_NOW | @@ -102,20 +127,34 @@ DynamicLibrary::DynamicLibrary(const QString& filename) ); if (handle) { - fprintf(stderr, "Error, if any: %s\n", dlerror()); - fflush(stderr); + struct _foo { + static bool err(void*& handle) + { + const char* err = dlerror(); + if (err) + { + fprintf(stderr, "Error, ignoring: %s\n", err); + fflush(stderr); + dlclose(handle); + handle = nullptr; + return true; + } + false; + } + }; + if (_foo::err(handle)) + return; Dialog = (DIALOG_FUNPTR) dlsym(handle, "GetDialog"); - fprintf(stderr, "Error, if any: %s\n", dlerror()); - fflush(stderr); + if (_foo::err(handle)) + return; Constructor = (CTOR_FUNPTR) dlsym(handle, "GetConstructor"); - fprintf(stderr, "Error, if any: %s\n", dlerror()); - fflush(stderr); + if (_foo::err(handle)) + return; Metadata = (METADATA_FUNPTR) dlsym(handle, "GetMetadata"); - fprintf(stderr, "Error, if any: %s\n", dlerror()); - fflush(stderr); + if (_foo::err(handle)) + return; } else { - fprintf(stderr, "Error, if any: %s\n", dlerror()); - fflush(stderr); + (void) _foo::err(handle); } #endif } diff --git a/facetracknoir/plugin-support.h b/facetracknoir/plugin-support.h index bb7b3c02..f3270aa6 100644 --- a/facetracknoir/plugin-support.h +++ b/facetracknoir/plugin-support.h @@ -46,7 +46,7 @@ extern "C" typedef void* (CALLING_CONVENTION* DIALOG_FUNPTR)(void); class DynamicLibrary { public: DynamicLibrary(const QString& filename); - virtual ~DynamicLibrary(); + ~DynamicLibrary(); DIALOG_FUNPTR Dialog; CTOR_FUNPTR Constructor; METADATA_FUNPTR Metadata; |