script

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 7, 2024 License: AGPL-3.0 Imports: 28 Imported by: 0

README

Script

We use goja as our javascript engine.
Goja support full ES5, but it DOES NOT support full ES6 yet.
Please read their Features and Known incompatibilities to see what you can use and what you cannot

Setup

Lazy setup

npm init glp@latest "<plugin-name>"

Note: Typescript will be automaticly configured.

Manually setup

You may want to configure your project manually, then you have to do the commands below

  1. Install go-liter-plugin:

    npm install go-liter-plugin
    
  2. Add the following element to your tsconfig.json

    {
       "compilerOptions": {
          "typeRoots": [
             "node_modules/@types",
             "node_modules/go-liter-plugin"
          ]
       }
    }
    

Typescript

Check files under ./types/lib/ for more information

Events

There is a list of events on each object below

$

Event Name Description Consequences after called cancel()
unload The unload event for the plugin N/A
handshake Client sent the handshake packet Abort client to connect to the target server, but allows the scripts to handle packet event on the connection.
serve The server connection is success Abort to use native directly copy, but allows the scripts to handle packet event on the connection.

Conn

Event Name Description Consequences after called cancel()
packet Received a minecraft packet Prevent this packet to be forward
error An error occurred on the connection N/A
close Connection closed N/A
before_close The other connection of the pair closed This connection will not be closed.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrScriptInvalid         = errors.New("Invalid script")
	ErrCircularImport        = errors.New("Circular import")
	ErrIsDir                 = errors.New("Target file is a directory")
	ErrExtModuleNotSupported = errors.New("Extension modules are not supported")
)

Functions

func ProxyStorage

func ProxyStorage(s Storage, vm *goja.Runtime) goja.Proxy

func Version added in v0.3.2

func Version() semver.Version

Version returns the version of liter plugin engine This is the version of "go-liter-plugin" when building the package

func WrapPacketBuilder

func WrapPacketBuilder(p *liter.PacketBuilder, conn *liter.Conn, vm *goja.Runtime, loop *eventloop.EventLoop) (o *goja.Object)

Types

type DependMap added in v0.3.2

type DependMap map[string]semver.ComparatorSet

func Dependencies added in v0.3.2

func Dependencies() DependMap

type Event

type Event struct {
	Name       string
	Data       map[string]any
	Cancelable bool
	// contains filtered or unexported fields
}

func NewEvent

func NewEvent(name string, data map[string]any) *Event

NewEvent will create a cancelable event

func (*Event) Cancel

func (e *Event) Cancel()

func (*Event) Cancelled

func (e *Event) Cancelled() bool

type EventEmitter

type EventEmitter struct {
	// contains filtered or unexported fields
}

func NewEventEmitter

func NewEventEmitter(vm *goja.Runtime, loop *eventloop.EventLoop) (e *EventEmitter)

func (*EventEmitter) EmitAsync

func (e *EventEmitter) EmitAsync(event *Event) (done <-chan bool)

func (*EventEmitter) ExportTo

func (e *EventEmitter) ExportTo(o *goja.Object)

func (*EventEmitter) OffAsync

func (e *EventEmitter) OffAsync(name string, listener goja.Callable)

func (*EventEmitter) OnAsync

func (e *EventEmitter) OnAsync(name string, listener goja.Callable)

type Exportsable

type Exportsable interface {
	Exports() *goja.Object
}

type JsonStorage

type JsonStorage struct {
	// contains filtered or unexported fields
}

TODO JsonStorage is a local storage, it's thready-safe

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func NewManager

func NewManager() (m *Manager)

func (*Manager) Emit

func (m *Manager) Emit(event *Event) (done <-chan bool)

func (*Manager) List

func (m *Manager) List() (scripts []*Script)

List will return all active plugins

func (*Manager) Load

func (m *Manager) Load(path string) (script *Script, err error)

Load will call LoadWithContext with context.Background()

func (*Manager) LoadFromDir

func (m *Manager) LoadFromDir(path string) (scripts []*Script, err error)

LoadFromDir will load all plugins under the path which have the ext `.zip`, and return all successfully loaded plugins with a possible error If the target path is not exists, LoadFromDir will do nothing and return without error If there are errors during load any plugin, the errors will be wrapped use `errors.Join`, and other plugins will continue to be load.

func (*Manager) LoadWithContext

func (m *Manager) LoadWithContext(ctx context.Context, path string) (script *Script, err error)

LoadWithContext will load a plugin packet use the given filepath. The first capture group will be the script's ID. The second capture group is the script's version If the script's ID is already loaded, then an error will be returned

func (*Manager) SetLogger

func (m *Manager) SetLogger(loger logger.Logger)

func (*Manager) Unload

func (m *Manager) Unload(id string) (script *Script)

Unload will unload plugin by ID and return the unloaded plugin instance During unloading, the unload event will be emitted

func (*Manager) UnloadAll

func (m *Manager) UnloadAll() (scripts []*Script)

UnloadAll will unload all plugins and return them During unloading, the unload event will be emitted

func (*Manager) WrapConn

func (m *Manager) WrapConn(conn *liter.Conn) (wrapped *WrappedConn)

type MemoryStorage

type MemoryStorage struct {
	// contains filtered or unexported fields
}

MemoryStorage is designed for single thread use, so it's not thread-safe. It should only be called inside the js loop with same Runtime instance.

func NewMemoryStorage

func NewMemoryStorage(vm *goja.Runtime) (s *MemoryStorage)

func (*MemoryStorage) Clear

func (s *MemoryStorage) Clear()

func (*MemoryStorage) Exports

func (s *MemoryStorage) Exports() *goja.Object

func (*MemoryStorage) GetItem

func (s *MemoryStorage) GetItem(key string) (value goja.Value)

func (*MemoryStorage) Keys

func (s *MemoryStorage) Keys() (keys []string)

func (*MemoryStorage) Len

func (s *MemoryStorage) Len() int

func (*MemoryStorage) RemoveItem

func (s *MemoryStorage) RemoveItem(key string)

func (*MemoryStorage) SetItem

func (s *MemoryStorage) SetItem(key string, value goja.Value)

type ModuleNotFoundErr added in v0.3.2

type ModuleNotFoundErr struct {
	Module string
}

func (*ModuleNotFoundErr) Error added in v0.3.2

func (e *ModuleNotFoundErr) Error() string

type PluginIdLoadedError added in v0.3.0

type PluginIdLoadedError struct {
	Id string
}

func (*PluginIdLoadedError) Error added in v0.3.0

func (e *PluginIdLoadedError) Error() string

type PluginLoadError added in v0.3.0

type PluginLoadError struct {
	Path   string
	Origin error
}

func (*PluginLoadError) Error added in v0.3.0

func (e *PluginLoadError) Error() string

func (*PluginLoadError) Unwrap added in v0.3.0

func (e *PluginLoadError) Unwrap() error

type Script

type Script struct {
	ScriptMeta
	// contains filtered or unexported fields
}

func (*Script) Emit

func (s *Script) Emit(event *Event) (done <-chan bool)

func (*Script) Exports

func (s *Script) Exports() *goja.Object

Exports return the module.exports from index.js

func (*Script) Logger

func (s *Script) Logger() logger.Logger

func (*Script) Off

func (s *Script) Off(name string, listener goja.Callable)

func (*Script) On

func (s *Script) On(name string, listener goja.Callable)

type ScriptMeta added in v0.3.0

type ScriptMeta struct {
	Id           string    `json:"name"`
	Version      string    `json:"version"`
	Description  string    `json:"description"`
	Dependencies DependMap `json:"dependencies"`
}

type Storage

type Storage interface {
	Len() int
	Keys() (keys []string)
	GetItem(key string) (value goja.Value)
	SetItem(key string, value goja.Value)
	RemoveItem(key string)
	Clear()
}

type WrappedConn

type WrappedConn struct {
	*liter.Conn

	OnClose func()
	// contains filtered or unexported fields
}

func WrapConn

func WrapConn(conn *liter.Conn, vm *goja.Runtime, loop *eventloop.EventLoop) (c *WrappedConn)

func (*WrappedConn) Close added in v0.2.6

func (c *WrappedConn) Close() (err error)

func (*WrappedConn) Closed

func (c *WrappedConn) Closed() bool

Closed return whether the connection is or not closed manually

func (*WrappedConn) Emit

func (c *WrappedConn) Emit(event *Event) (done <-chan bool)

func (*WrappedConn) Exports

func (c *WrappedConn) Exports() *goja.Object

func (*WrappedConn) Recv

func (c *WrappedConn) Recv() (w *WrappedPacketReader, err error)

type WrappedPacketReader

type WrappedPacketReader struct {
	*liter.PacketReader
	// contains filtered or unexported fields
}

func WrapPacketReader

func WrapPacketReader(p *liter.PacketReader, vm *goja.Runtime) *WrappedPacketReader

func (*WrappedPacketReader) Exports

func (p *WrappedPacketReader) Exports() *goja.Object

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL