C++插件管理系统

插件加载目录结构

execute
    plug.exe
    plugify.dll
    plugify.pconfig
    res
        cpp-lang-module.pmodule
        example_plugin.pplugin
        bin
            cpp-lang-module.dll
            example_plugin.dll

 plugify.pconfig

{
    "baseDir": "res",
    "logSeverity": "debug",
    "repositories": [
    ],
    "preferOwnSymbols": false
}

example_plugin.pplugin

{
    "fileVersion": 1,
    "version": 1,
    "versionName": "1.0",
    "friendlyName": "PluginCPP",
    "description": "An example of a plugin. This can be used as a starting point when creating your own plugin.",
    "createdBy": "untrustedmodders",
    "createdByURL": "https://github.com/untrustedmodders/",
    "docsURL": "https://github.com/orgs/untrustedmodders/README.md",
    "downloadURL": "https://github.com/orgs/untrustedmodders/example-repo.zip",
    "updateURL": "https://github.com/untrustedmodders/plugify/issues",
    "entryPoint": "bin/example_plugin",
    "supportedPlatforms": [],
    "languageModule": {
        "name": "cpp"
    },
    "dependencies": [],
    "exportedMethods": [
        {
            "name": "MakePrint",
            "funcName": "MakePrint",
            "paramTypes": [
                {
                    "type": "int32",
                    "name": "count"
                },
                {
                    "type": "string",
                    "name": "message"
                }
            ],
            "retType": {
                "type": "void"
            }
        }
    ]
}

 cpp-lang-module.pmodule

{
    "fileVersion": 1,
    "version": 0,
    "versionName": "v0",
    "friendlyName": "Cpp language module",
    "language": "cpp",
    "description": "Adds support for C++ plugins",
    "createdBy": "untrustedmodders",
    "createdByURL": "https://github.com/untrustedmodders/",
    "docsURL": "https://github.com/untrustedmodders/cpp-lang-module/blob/main/README.md",
    "downloadURL": "https://github.com/untrustedmodders/cpp-lang-module/releases/download/v0/cpp-lang-module.zip",
    "updateURL": "https://untrustedmodders.github.io/cpp-lang-module/cpp-lang-module.json",
    "supportedPlatforms": [],
    "forceLoad": false
}

 插件定义
#include <plugify/cpp_plugin.h>
#include <plugin_export.h>
#include <iostream>

class ExamplePlugin : public plugify::IPluginEntry {
public:
	void OnPluginStart() override {
		std::cout << "Example Start!" << std::endl;
	}

	void OnPluginEnd() override {
		std::cout << "Example End!" << std::endl;
	}

	void MakePrint(int count, const std::string& message) {
		for (int i = 0; i < count; ++i) {
			std::cout << message << std::endl;
		}
	}
} g_examplePlugin;

EXPOSE_PLUGIN(PLUGIN_API, &g_examplePlugin)

extern "C"
PLUGIN_API void MakePrint(int count, const std::string& message) {
	g_examplePlugin.MakePrint(count, message);
}
 模块定义

#include <plugify/assembly.h>
#include <plugify/module.h>
#include <plugify/plugin.h>
#include <plugify/plugify_provider.h>
#include <plugify/language_module.h>
#include <plugify/cpp_plugin.h>

#include <module_export.h>
#include <unordered_map>
#include <map>
#include <array>

class CppLanguageModule final : public plugify::ILanguageModule {
public:
	CppLanguageModule() = default;

	// ILanguageModule
	plugify::InitResult Initialize(std::weak_ptr<plugify::IPlugifyProvider> provider, plugify::ModuleRef module) override;
	void Shutdown() override;
	void OnMethodExport(plugify::PluginRef plugin) override;
	plugify::LoadResult OnPluginLoad(plugify::PluginRef plugin) override;
	void OnPluginStart(plugify::PluginRef plugin) override;
	void OnPluginEnd(plugify::PluginRef plugin) override;
	bool IsDebugBuild() override;

	const std::shared_ptr<plugify::IPlugifyProvider>& GetProvider() { return _provider; }
	plugify::MemAddr GetNativeMethod(std::string_view methodName) const;
	void GetNativeMethod(std::string_view methodName, plugify::MemAddr* addressDest);

private:
	std::shared_ptr<plugify::IPlugifyProvider> _provider;
	
	std::map<plugify::UniqueId, AssemblyHolder> _assemblyMap;
	std::unordered_map<std::string, plugify::MemAddr, string_hash, std::equal_to<>> _nativesMap;
	
	std::vector<plugify::MemAddr*> _addresses;

	static std::array<void*, 15> _pluginApi;
};

// ILanguageModule
InitResult CppLanguageModule::Initialize(std::weak_ptr<IPlugifyProvider> provider, ModuleRef /*module*/) {
	if (!(_provider = provider.lock())) {
		return ErrorData{ "Provider not exposed" };
	}

	_provider->Log("[CPPLM]  Inited!", Severity::Debug);

	return InitResultData{};
}
 测试
plug.exe
plugify init
[+] Info: Plugify Init!
[+] Info: Version: 1.0.0.0
[+] Info: Git: [v35-1-g08987de]:(v35) - Fix cmake typo in test on main at 'Sun Sep 1 09:21:31 2024'
[+] Info: Compiled on: Windows-10.0.19045 from: Ninja with: 'MSVC'
[~] Debug: Loading local packages
[~] Debug: Loading remote packages
[#] Error: Packages manifest from 'https://github.com/untrustedmodders/plugify/issues' has JSON parsing error: 8:1: expected_brace
   <!DOCTYPE html>
   ^
[~] Debug: PackageManager loaded in 788.826ms
[~] Debug: [CPPLM] Inited!
Example Start!
[~] Debug: PluginManager loaded in 10.951ms
 参考

GitHub - untrustedmodders/cpp-lang-module: C++ Language Module


 

创作不易,小小的支持一下吧!

  • 31
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码力码力我爱你

创作不易,小小的支持一下吧!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值