new plugin system
This commit is contained in:
+18
-18
@@ -1,49 +1,49 @@
|
||||
# Sample Go WASM Plugin (sample)
|
||||
# Sample Go Plugin (sample)
|
||||
|
||||
This sample lives at the repo root and builds a WASI-compatible Go plugin.
|
||||
This sample lives at the repo root and builds a native Go plugin.
|
||||
|
||||
## Files to include in the plugin zip
|
||||
|
||||
At the root of the zip:
|
||||
- sample.wasm
|
||||
- sample-linux-amd64.so / sample-darwin-arm64.dylib / sample-windows-amd64.dll
|
||||
- sample.html
|
||||
- sample.css
|
||||
- sample.js
|
||||
|
||||
## Build the WASM
|
||||
## Build
|
||||
|
||||
Recommended: Go WASI (most compatible with wazero)
|
||||
Use the provided build scripts:
|
||||
|
||||
1) Build the WASM with Go 1.21+:
|
||||
```bash
|
||||
# Linux/macOS
|
||||
./build-plugin.sh
|
||||
|
||||
GOOS=wasip1 GOARCH=wasm go build -o sample.wasm ./wasm
|
||||
# Multiple targets
|
||||
BUILD_TARGETS="linux-amd64 linux-arm64 darwin-arm64" ./build-plugin.sh
|
||||
|
||||
2) Optional TinyGo fallback:
|
||||
|
||||
tinygo build -o sample.wasm -target wasi -scheduler=none -gc=leaking ./wasm
|
||||
# Windows
|
||||
build-plugin.bat
|
||||
```
|
||||
|
||||
## Create the zip
|
||||
|
||||
Create a zip named sample.zip containing:
|
||||
- sample.wasm
|
||||
- sample.html
|
||||
- sample.css
|
||||
- sample.js
|
||||
The build script automatically creates `sample.zip` containing the built
|
||||
binaries and web assets.
|
||||
|
||||
Then place sample.zip in Overlord-Server/plugins.
|
||||
Then place `sample.zip` in `Overlord-Server/plugins`.
|
||||
|
||||
## Open the UI
|
||||
|
||||
Navigate to:
|
||||
- /plugins/sample?clientId=<CLIENT_ID>
|
||||
|
||||
Click "Send event" and the WASM plugin will echo back to the server logs.
|
||||
Click "Send event" and the plugin will echo back to the server logs.
|
||||
|
||||
## OS detection (Windows-only logic)
|
||||
|
||||
The plugin receives host metadata in the `init` payload. The sample plugin shows
|
||||
how to detect the host OS and branch logic (see
|
||||
[plugin-sample-go/wasm/main.go](plugin-sample-go/wasm/main.go)). For example:
|
||||
`plugin-sample-go/native/main.go`). For example:
|
||||
|
||||
- If `host.os == "windows"`, you can safely run Windows-only flows (e.g., WinAPI-backed behavior).
|
||||
- Otherwise, skip or provide a fallback path.
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
//go:build linux || darwin || freebsd
|
||||
|
||||
package main
|
||||
|
||||
/*
|
||||
#include <stdint.h>
|
||||
|
||||
typedef void (*host_callback_t)(uintptr_t ctx,
|
||||
const char* event, int eventLen,
|
||||
const char* payload, int payloadLen);
|
||||
|
||||
static void invoke_host_callback(uintptr_t fn, uintptr_t ctx,
|
||||
const char* ev, int evLen, const char* pl, int plLen) {
|
||||
((host_callback_t)fn)(ctx, ev, evLen, pl, plLen);
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import "unsafe"
|
||||
|
||||
var (
|
||||
hostCallbackFn C.uintptr_t
|
||||
hostCallbackCtx C.uintptr_t
|
||||
)
|
||||
|
||||
func hostSendViaCallback(event string, payload []byte) {
|
||||
fn := hostCallbackFn
|
||||
if fn == 0 {
|
||||
return
|
||||
}
|
||||
evBytes := []byte(event)
|
||||
var evPtr, plPtr unsafe.Pointer
|
||||
if len(evBytes) > 0 {
|
||||
evPtr = unsafe.Pointer(&evBytes[0])
|
||||
}
|
||||
if len(payload) > 0 {
|
||||
plPtr = unsafe.Pointer(&payload[0])
|
||||
}
|
||||
C.invoke_host_callback(fn, hostCallbackCtx,
|
||||
(*C.char)(evPtr), C.int(len(evBytes)),
|
||||
(*C.char)(plPtr), C.int(len(payload)))
|
||||
}
|
||||
|
||||
//export PluginOnLoad
|
||||
func PluginOnLoad(hostInfo *C.char, hostInfoLen C.int, cb C.uintptr_t, ctx C.uintptr_t) C.int {
|
||||
hostCallbackFn = cb
|
||||
hostCallbackCtx = ctx
|
||||
setSend(hostSendViaCallback)
|
||||
|
||||
data := C.GoBytes(unsafe.Pointer(hostInfo), hostInfoLen)
|
||||
if err := handleInit(data); err != nil {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
//export PluginOnEvent
|
||||
func PluginOnEvent(event *C.char, eventLen C.int, payload *C.char, payloadLen C.int) C.int {
|
||||
ev := C.GoStringN(event, eventLen)
|
||||
var pl []byte
|
||||
if payloadLen > 0 {
|
||||
pl = C.GoBytes(unsafe.Pointer(payload), payloadLen)
|
||||
}
|
||||
if err := handleEvent(ev, pl); err != nil {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
//export PluginOnUnload
|
||||
func PluginOnUnload() {
|
||||
handleUnload()
|
||||
}
|
||||
|
||||
func main() {}
|
||||
@@ -0,0 +1,67 @@
|
||||
//go:build windows
|
||||
|
||||
package main
|
||||
|
||||
import "C"
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var callbackPtr uintptr
|
||||
|
||||
func hostSendViaCallback(event string, payload []byte) {
|
||||
cb := callbackPtr
|
||||
if cb == 0 {
|
||||
return
|
||||
}
|
||||
eventBytes := []byte(event)
|
||||
var evPtr, plPtr uintptr
|
||||
evLen := uintptr(len(eventBytes))
|
||||
plLen := uintptr(len(payload))
|
||||
if len(eventBytes) > 0 {
|
||||
evPtr = uintptr(unsafe.Pointer(&eventBytes[0]))
|
||||
}
|
||||
if len(payload) > 0 {
|
||||
plPtr = uintptr(unsafe.Pointer(&payload[0]))
|
||||
}
|
||||
syscall.SyscallN(cb, evPtr, evLen, plPtr, plLen)
|
||||
}
|
||||
|
||||
//export PluginSetCallback
|
||||
func PluginSetCallback(cb C.ulonglong) {
|
||||
callbackPtr = uintptr(cb)
|
||||
setSend(hostSendViaCallback)
|
||||
}
|
||||
|
||||
//export PluginOnLoad
|
||||
func PluginOnLoad(hostInfo *C.char, hostInfoLen C.int, cb C.ulonglong) C.int {
|
||||
callbackPtr = uintptr(cb)
|
||||
setSend(hostSendViaCallback)
|
||||
|
||||
data := C.GoBytes(unsafe.Pointer(hostInfo), hostInfoLen)
|
||||
if err := handleInit(data); err != nil {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
//export PluginOnEvent
|
||||
func PluginOnEvent(event *C.char, eventLen C.int, payload *C.char, payloadLen C.int) C.int {
|
||||
ev := C.GoStringN(event, eventLen)
|
||||
var pl []byte
|
||||
if payloadLen > 0 {
|
||||
pl = C.GoBytes(unsafe.Pointer(payload), payloadLen)
|
||||
}
|
||||
if err := handleEvent(ev, pl); err != nil {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
//export PluginOnUnload
|
||||
func PluginOnUnload() {
|
||||
handleUnload()
|
||||
}
|
||||
|
||||
func main() {}
|
||||
@@ -0,0 +1,3 @@
|
||||
module sample-plugin
|
||||
|
||||
go 1.24
|
||||
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type HostInfo struct {
|
||||
ClientID string `json:"clientId"`
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
var (
|
||||
hostInfo HostInfo
|
||||
sendFn func(event string, payload []byte)
|
||||
mu sync.Mutex
|
||||
)
|
||||
|
||||
func setSend(fn func(event string, payload []byte)) {
|
||||
mu.Lock()
|
||||
sendFn = fn
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
func sendEvent(event string, payload interface{}) {
|
||||
mu.Lock()
|
||||
fn := sendFn
|
||||
mu.Unlock()
|
||||
if fn == nil {
|
||||
return
|
||||
}
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
log.Printf("[sample] marshal error: %v", err)
|
||||
return
|
||||
}
|
||||
fn(event, data)
|
||||
}
|
||||
|
||||
func handleInit(hostJSON []byte) error {
|
||||
if err := json.Unmarshal(hostJSON, &hostInfo); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("[sample] init: clientId=%s os=%s arch=%s", hostInfo.ClientID, hostInfo.OS, hostInfo.Arch)
|
||||
sendEvent("ready", map[string]string{"message": "sample plugin ready"})
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleEvent(event string, payload []byte) error {
|
||||
switch event {
|
||||
case "ui_message":
|
||||
var msg struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
_ = json.Unmarshal(payload, &msg)
|
||||
log.Printf("[sample] got ui_message: %s", msg.Message)
|
||||
sendEvent("echo", map[string]string{"message": msg.Message})
|
||||
|
||||
case "ping":
|
||||
sendEvent("pong", nil)
|
||||
|
||||
default:
|
||||
log.Printf("[sample] unhandled event: %s", event)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func handleUnload() {
|
||||
log.Printf("[sample] unloading")
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/* Code generated by cmd/cgo; DO NOT EDIT. */
|
||||
|
||||
/* package sample-plugin */
|
||||
|
||||
|
||||
#line 1 "cgo-builtin-export-prolog"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#ifndef GO_CGO_EXPORT_PROLOGUE_H
|
||||
#define GO_CGO_EXPORT_PROLOGUE_H
|
||||
|
||||
#ifndef GO_CGO_GOSTRING_TYPEDEF
|
||||
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
|
||||
extern size_t _GoStringLen(_GoString_ s);
|
||||
extern const char *_GoStringPtr(_GoString_ s);
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
/* Start of preamble from import "C" comments. */
|
||||
|
||||
|
||||
|
||||
|
||||
/* End of preamble from import "C" comments. */
|
||||
|
||||
|
||||
/* Start of boilerplate cgo prologue. */
|
||||
#line 1 "cgo-gcc-export-header-prolog"
|
||||
|
||||
#ifndef GO_CGO_PROLOGUE_H
|
||||
#define GO_CGO_PROLOGUE_H
|
||||
|
||||
typedef signed char GoInt8;
|
||||
typedef unsigned char GoUint8;
|
||||
typedef short GoInt16;
|
||||
typedef unsigned short GoUint16;
|
||||
typedef int GoInt32;
|
||||
typedef unsigned int GoUint32;
|
||||
typedef long long GoInt64;
|
||||
typedef unsigned long long GoUint64;
|
||||
typedef GoInt64 GoInt;
|
||||
typedef GoUint64 GoUint;
|
||||
typedef size_t GoUintptr;
|
||||
typedef float GoFloat32;
|
||||
typedef double GoFloat64;
|
||||
#ifdef _MSC_VER
|
||||
#if !defined(__cplusplus) || _MSVC_LANG <= 201402L
|
||||
#include <complex.h>
|
||||
typedef _Fcomplex GoComplex64;
|
||||
typedef _Dcomplex GoComplex128;
|
||||
#else
|
||||
#include <complex>
|
||||
typedef std::complex<float> GoComplex64;
|
||||
typedef std::complex<double> GoComplex128;
|
||||
#endif
|
||||
#else
|
||||
typedef float _Complex GoComplex64;
|
||||
typedef double _Complex GoComplex128;
|
||||
#endif
|
||||
|
||||
/*
|
||||
static assertion to make sure the file is being used on architecture
|
||||
at least with matching size of GoInt.
|
||||
*/
|
||||
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
|
||||
|
||||
#ifndef GO_CGO_GOSTRING_TYPEDEF
|
||||
typedef _GoString_ GoString;
|
||||
#endif
|
||||
typedef void *GoMap;
|
||||
typedef void *GoChan;
|
||||
typedef struct { void *t; void *v; } GoInterface;
|
||||
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
||||
|
||||
#endif
|
||||
|
||||
/* End of boilerplate cgo prologue. */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern __declspec(dllexport) void PluginSetCallback(long long unsigned int cb);
|
||||
extern __declspec(dllexport) int PluginOnLoad(char* hostInfo, int hostInfoLen, long long unsigned int cb);
|
||||
extern __declspec(dllexport) int PluginOnEvent(char* event, int eventLen, char* payload, int payloadLen);
|
||||
extern __declspec(dllexport) void PluginOnUnload(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -10,7 +10,7 @@
|
||||
<main class="container">
|
||||
<header class="header">
|
||||
<h1>Sample Plugin</h1>
|
||||
<p class="sub">Go WASM + UI plugin</p>
|
||||
<p class="sub">Go Native + UI plugin</p>
|
||||
</header>
|
||||
|
||||
<section class="card">
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
module plugin-sample-go/wasm
|
||||
|
||||
go 1.21
|
||||
|
||||
require github.com/vmihailenco/msgpack/v5 v5.4.1
|
||||
@@ -1,76 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
type Incoming struct {
|
||||
Type string `msgpack:"type"`
|
||||
Event string `msgpack:"event,omitempty"`
|
||||
Payload interface{} `msgpack:"payload,omitempty"`
|
||||
}
|
||||
|
||||
type Outgoing struct {
|
||||
Type string `msgpack:"type"`
|
||||
Event string `msgpack:"event,omitempty"`
|
||||
Payload interface{} `msgpack:"payload,omitempty"`
|
||||
}
|
||||
|
||||
type UiMessage struct {
|
||||
Message string `msgpack:"message"`
|
||||
}
|
||||
|
||||
type HostInfo struct {
|
||||
ClientID string `msgpack:"clientId"`
|
||||
OS string `msgpack:"os"`
|
||||
Arch string `msgpack:"arch"`
|
||||
Version string `msgpack:"version"`
|
||||
}
|
||||
|
||||
type InitPayload struct {
|
||||
Host HostInfo `msgpack:"host"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
dec := msgpack.NewDecoder(os.Stdin)
|
||||
enc := msgpack.NewEncoder(os.Stdout)
|
||||
|
||||
for {
|
||||
var msg Incoming
|
||||
if err := dec.Decode(&msg); err != nil {
|
||||
log.Printf("decode error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
switch msg.Type {
|
||||
case "init":
|
||||
var init InitPayload
|
||||
if payloadBytes, err := msgpack.Marshal(msg.Payload); err == nil {
|
||||
_ = msgpack.Unmarshal(payloadBytes, &init)
|
||||
}
|
||||
|
||||
osName := init.Host.OS
|
||||
if osName == "" {
|
||||
osName = "unknown"
|
||||
}
|
||||
|
||||
if osName == "windows" {
|
||||
log.Printf("[sample] windows detected - you can use WinAPI-specific flows")
|
||||
_ = enc.Encode(Outgoing{Type: "event", Event: "ready", Payload: "sample plugin ready (windows detected)"})
|
||||
} else {
|
||||
log.Printf("[sample] non-windows host detected (%s)", osName)
|
||||
_ = enc.Encode(Outgoing{Type: "event", Event: "ready", Payload: "sample plugin ready"})
|
||||
}
|
||||
case "event":
|
||||
if msg.Event == "ui_message" {
|
||||
payloadBytes, _ := msgpack.Marshal(msg.Payload)
|
||||
var ui UiMessage
|
||||
_ = msgpack.Unmarshal(payloadBytes, &ui)
|
||||
_ = enc.Encode(Outgoing{Type: "event", Event: "echo", Payload: ui.Message})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user