插件加载目录结构
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