Commit 5c8af1b7 authored by jan.koester's avatar jan.koester
Browse files

loader finished

parent ab034a9e
Loading
Loading
Loading
Loading
+0 −1
Changes for src/blogi.cpp: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -65,7 +65,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "blogi.h"

#include "webedit/render.h"
#include "webedit/edit.h"

#define cookieid "2d1b0348-1491-11f0-8c81-3cecefce9ac2"
+133 −0
Changes for src/webedit/plugin.cpp: 133 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <iostream>
#include <filesystem>

#include <httppp/exception.h>

#include "plugin.h"

blogi::webedit::EditPluginLoad::EditPluginLoad(const std::string& path){
    try {
        if (std::filesystem::is_directory(path)) {
            for (const auto& entry : std::filesystem::directory_iterator(path)) {
                 std::unique_ptr<PluginData> ldplg = std::make_unique<PluginData>();
#ifdef Windows
                if (entry.path().extension() == ".dll") {
                    try {
                        std::string ppath = entry.path().string();
                        ldplg->pldata = LoadLibraryA(ppath.c_str());

                        if (ldplg->pldata == nullptr) {
                            DWORD lastError = GetLastError();
                            throw std::runtime_error("Cannot load library. Error code: " + std::to_string(lastError));
                        }

                        create_t *create_plugin = (create_t*)GetProcAddress(ldplg->pldata, "create");

                        if (create_plugin == nullptr) {
                            DWORD lastError = GetLastError();
                            FreeLibrary(ldplg->pldata);
                            throw std::runtime_error("Cannot load symbol 'create'. Error code: " + std::to_string(lastError));
                        }

                        destroy_t* destroy_plugin = (destroy_t*)GetProcAddress(ldplg->pldata, "destroy");
                        if (destroy_plugin == nullptr) {
                            DWORD lastError = GetLastError();
                            FreeLibrary(ldplg->pldata);
                            throw std::runtime_error("Cannot load symbol 'destroy'. Error code: " + std::to_string(lastError));
                        }

#else // Linux/Unix
                if (entry.path().extension() == ".so") {
                    try{


                        ldplg->pldata = dlopen(entry.path().c_str(), RTLD_LAZY);

                        const char * dlsym_error = nullptr;

                        if (!ldplg->pldata) { // FIX: Check ldplg->pldata, not !ldplg->pldata && dlsym_error
                            dlsym_error = dlerror();
                            libhttppp::HTTPException err;
                            err[libhttppp::HTTPException::Error] << "Cannot load library: " << (dlsym_error ? dlsym_error : "Unknown error");
                            throw err;
                        }

                        // Get the address of the 'create' function
                        create_t* create_plugin= (create_t*) dlsym(ldplg->pldata, "create");

                        if (create_plugin == nullptr) {
                            dlsym_error = dlerror();
                            libhttppp::HTTPException err;
                            // The custom deleter will call dlclose when ldplg goes out of scope/is destroyed
                            err[libhttppp::HTTPException::Error] << "Cannot load symbol create: " << (dlsym_error ? dlsym_error : "Unknown error") << '\n';
                            throw err;
                        }
                        destroy_t* destroy_plugin = (destroy_t*) dlsym(ldplg->pldata, "destroy");
#endif
                        EditPlugin *plg=create_plugin();
                        ldplg->ins=std::unique_ptr<EditPlugin, EditPluginDeleter>(plg, EditPluginDeleter(destroy_plugin,ldplg->pldata));

                        if(_lastPlugin){
                            _lastPlugin->next=std::move(ldplg);
                            _lastPlugin = _lastPlugin->next.get();
                        }else{
                            _firstPlugin=std::move(ldplg);
                            _lastPlugin = _firstPlugin.get();
                        }
                    }
                    catch (const std::exception& e) {
                        std::cerr << "Error processing file " << entry.path().filename().string() << ": " << e.what() << std::endl;
                    }catch(libhttppp::HTTPException &e){
                         std::cerr << "Error processing file " << e.what() << ": " << e.what() << std::endl;
                    }
                }
            }
        }
    }catch (const std::exception& e) {
        std::cerr << "Error loading plugins directory: " << e.what() << std::endl;
    }
}

blogi::webedit::EditPluginLoad::~EditPluginLoad(){
}

const blogi::webedit::EditPluginLoad::PluginData* blogi::webedit::EditPluginLoad::EditPluginLoad::getFirstPlugin() const{
    return _firstPlugin.get();
}

const blogi::webedit::EditPluginLoad::PluginData *blogi::webedit::EditPluginLoad::PluginData::getNextPlg() const{
    return next.get();
}

blogi::webedit::EditPlugin *blogi::webedit::EditPluginLoad::PluginData::getInstace() const{
    return ins.get();
}

+153 −0
Changes for src/webedit/plugin.h: 153 added lines, 0 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2025, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/

#include <string>
#include <memory>

#include <uuidp.h>
#include <json-c/json.h>


#ifdef Windows
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#else
#include <unistd.h>
#include <dlfcn.h>
#endif

#pragma once

#ifndef EXPORT
  #ifdef Windows
  #define EXPORT extern "C" __declspec(dllexport)
  #else
  #define EXPORT extern "C" __attribute__((visibility("default")))
  #endif
#endif

namespace blogi {
    namespace webedit {
        class EditPlugin;

        typedef EditPlugin* create_t();
        typedef void destroy_t(EditPlugin*);

        struct EditPluginDeleter {
            private:
                destroy_t* destroyer = nullptr;
                void* pldata = nullptr;
            public:
                EditPluginDeleter() = default;
                EditPluginDeleter(destroy_t* fn, void* handle) : destroyer(fn), pldata(handle) {};
                EditPluginDeleter(const EditPluginDeleter&) = default;
                EditPluginDeleter& operator=(const EditPluginDeleter&) = default;

                void operator()(EditPlugin* p) const {
                    if (p && destroyer) {
                        destroyer(p);
                    }

                    if (pldata) {
        #ifdef  Windows
                        FreeLibrary(static_cast<HMODULE>(pldata));
        #else
                        dlclose(pldata);
        #endif
                    }
                };
            };

        class Elements {
        public:
            const uuid::uuid &getElementId();
            const uuid::uuid &getChildElement();
            const uuid::uuid &nextElement();
        protected:
            uuid::uuid                             Type;
            std::unique_ptr<Elements> nextel;
            std::unique_ptr<Elements> child;
            friend class EditPlugin;
        };

        class EditPlugin {
        public:
            EditPlugin()=default;
            virtual ~EditPlugin()=default;

            virtual const std::string &getName()=0;
            virtual const std::string &getAuthor()=0;
            virtual const std::string &getVersion()=0;
            virtual const uuid::uuid &getTypeId()=0;

            virtual void JsonApi(json_object *request,json_object *response)=0;

            virtual bool haveChilds()=0;

            virtual void setChildElement(const Elements &el);
            virtual void unsetChildElement(const Elements &el);

            Elements &getRootElement();
        private:
            std::unique_ptr<Elements> RootElement;
        };

        class EditPluginLoad {
        public:
            EditPluginLoad(const std::string &path);
            ~EditPluginLoad();

            class PluginData{
            private:
    #ifdef Windows
                HMODULE pldata;
    #else
                void* pldata;
    #endif // Windows
                std::unique_ptr<EditPlugin,EditPluginDeleter> ins=nullptr;
                std::unique_ptr<PluginData> next = nullptr;
            public:
                PluginData();
                virtual ~PluginData();
                EditPlugin  *getInstace() const;
                const PluginData  *getNextPlg() const;
                friend class EditPluginLoad;
            };

            const PluginData* getFirstPlugin() const;

        private:
#ifdef Windows
            HMODULE pldata;
#else
            void* pldata;
#endif // Windows
            std::unique_ptr<PluginData>  _firstPlugin = nullptr;
            PluginData                              *_lastPlugin = nullptr;
        };
    };
};
+1 −1

File changed.

Contains only whitespace changes.