2019-04-16 06:07:15 +00:00
|
|
|
package scripting
|
|
|
|
|
|
|
|
import (
|
2019-05-07 05:57:32 +00:00
|
|
|
"errors"
|
2022-01-17 04:09:27 +00:00
|
|
|
"fmt"
|
2020-01-03 01:58:22 +00:00
|
|
|
"sync"
|
2019-05-07 05:57:32 +00:00
|
|
|
|
2021-06-20 05:14:41 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/keybind"
|
2022-01-19 05:24:36 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
2022-01-17 04:09:27 +00:00
|
|
|
"github.com/dop251/goja"
|
2019-04-16 06:07:15 +00:00
|
|
|
)
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
// Event name constants.
|
|
|
|
const (
|
|
|
|
CollideEvent = "OnCollide" // another doodad collides with us
|
|
|
|
EnterEvent = "OnEnter" // a doodad is fully inside us
|
|
|
|
LeaveEvent = "OnLeave" // a doodad no longer collides with us
|
2021-01-03 23:19:21 +00:00
|
|
|
UseEvent = "OnUse" // player pressed the Use key while touching us
|
2019-04-19 01:15:05 +00:00
|
|
|
|
|
|
|
// Controllable (player character) doodad events
|
|
|
|
KeypressEvent = "OnKeypress" // i.e. arrow keys
|
|
|
|
)
|
|
|
|
|
2019-05-07 05:57:32 +00:00
|
|
|
// Event return errors.
|
|
|
|
var (
|
|
|
|
ErrReturnFalse = errors.New("JS callback function returned false")
|
|
|
|
)
|
|
|
|
|
2019-04-16 06:07:15 +00:00
|
|
|
// Events API for Doodad scripts.
|
|
|
|
type Events struct {
|
2022-01-17 04:09:27 +00:00
|
|
|
runtime *goja.Runtime
|
|
|
|
registry map[string][]goja.Value
|
2020-01-03 01:58:22 +00:00
|
|
|
lock sync.RWMutex
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEvents initializes the Events API.
|
2022-01-17 04:09:27 +00:00
|
|
|
func NewEvents(runtime *goja.Runtime) *Events {
|
2019-04-16 06:07:15 +00:00
|
|
|
return &Events{
|
2022-01-17 04:09:27 +00:00
|
|
|
runtime: runtime,
|
|
|
|
registry: map[string][]goja.Value{},
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnCollide fires when another actor collides with yours.
|
2022-01-17 04:09:27 +00:00
|
|
|
func (e *Events) OnCollide(call goja.FunctionCall) goja.Value {
|
2019-04-19 01:15:05 +00:00
|
|
|
return e.register(CollideEvent, call.Argument(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunCollide invokes the OnCollide handler function.
|
2019-05-07 05:57:32 +00:00
|
|
|
func (e *Events) RunCollide(v interface{}) error {
|
|
|
|
return e.run(CollideEvent, v)
|
|
|
|
}
|
|
|
|
|
2021-01-03 23:19:21 +00:00
|
|
|
// OnUse fires when another actor collides with yours.
|
2022-01-17 04:09:27 +00:00
|
|
|
func (e *Events) OnUse(call goja.FunctionCall) goja.Value {
|
2021-01-03 23:19:21 +00:00
|
|
|
return e.register(UseEvent, call.Argument(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunUse invokes the OnUse handler function.
|
|
|
|
func (e *Events) RunUse(v interface{}) error {
|
|
|
|
return e.run(UseEvent, v)
|
|
|
|
}
|
|
|
|
|
2019-05-07 05:57:32 +00:00
|
|
|
// OnLeave fires when another actor stops colliding with yours.
|
2022-01-17 04:09:27 +00:00
|
|
|
func (e *Events) OnLeave(call goja.FunctionCall) goja.Value {
|
2019-05-07 05:57:32 +00:00
|
|
|
return e.register(LeaveEvent, call.Argument(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunLeave invokes the OnLeave handler function.
|
|
|
|
func (e *Events) RunLeave(v interface{}) error {
|
|
|
|
return e.run(LeaveEvent, v)
|
2019-04-19 01:15:05 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 21:03:20 +00:00
|
|
|
// OnKeypress fires when another actor collides with yours.
|
2022-01-17 04:09:27 +00:00
|
|
|
func (e *Events) OnKeypress(call goja.FunctionCall) goja.Value {
|
2019-05-05 21:03:20 +00:00
|
|
|
return e.register(KeypressEvent, call.Argument(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunKeypress invokes the OnCollide handler function.
|
2021-06-20 05:14:41 +00:00
|
|
|
func (e *Events) RunKeypress(ev keybind.State) error {
|
2022-01-17 04:09:27 +00:00
|
|
|
return e.run(KeypressEvent, e.runtime.ToValue(ev))
|
2019-05-05 21:03:20 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
// register a named event.
|
2022-01-17 04:09:27 +00:00
|
|
|
func (e *Events) register(name string, callback goja.Value) goja.Value {
|
2020-01-03 01:58:22 +00:00
|
|
|
e.lock.Lock()
|
|
|
|
defer e.lock.Unlock()
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
if _, ok := e.registry[name]; !ok {
|
2022-01-17 04:09:27 +00:00
|
|
|
e.registry[name] = []goja.Value{}
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
e.registry[name] = append(e.registry[name], callback)
|
2022-01-17 04:09:27 +00:00
|
|
|
return goja.Undefined()
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
// Run an event handler. Returns an error only if there was a JavaScript error
|
|
|
|
// inside the function. If there are no event handlers, just returns nil.
|
|
|
|
func (e *Events) run(name string, args ...interface{}) error {
|
2020-01-03 01:58:22 +00:00
|
|
|
e.lock.RLock()
|
|
|
|
defer e.lock.RUnlock()
|
|
|
|
|
2022-01-19 05:24:36 +00:00
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
// TODO EXCEPTIONS: I once saw a "runtime error: index out of range [-1]"
|
|
|
|
// from an OnCollide handler between azu-white and thief that was crashing
|
|
|
|
// the app, report this upstream nicely to the user.
|
|
|
|
log.Error("PANIC: JS %s handler: %s", name, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
if _, ok := e.registry[name]; !ok {
|
2019-04-16 06:07:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-17 04:09:27 +00:00
|
|
|
var params = make([]goja.Value, len(args))
|
|
|
|
for i, v := range args {
|
|
|
|
params[i] = e.runtime.ToValue(v)
|
|
|
|
}
|
|
|
|
|
2019-04-19 01:15:05 +00:00
|
|
|
for _, callback := range e.registry[name] {
|
2022-01-17 04:09:27 +00:00
|
|
|
function, ok := goja.AssertFunction(callback)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("failed to callback %s: %s", name, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
value, err := function(goja.Undefined(), params...)
|
2019-04-16 06:07:15 +00:00
|
|
|
if err != nil {
|
2022-01-19 05:24:36 +00:00
|
|
|
// TODO EXCEPTIONS: this err is useful like
|
|
|
|
// `ReferenceError: playerSpeed is not defined at <eval>:173:9(93)`
|
|
|
|
// but wherever we're returning the err to isn't handling it!
|
|
|
|
log.Error("Scripting error on %s: %s", name, err)
|
2019-04-16 06:07:15 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-05-07 05:57:32 +00:00
|
|
|
|
|
|
|
// If the event handler returned a boolean false, stop all other
|
|
|
|
// callbacks and return the boolean.
|
2022-01-17 04:09:27 +00:00
|
|
|
if b, ok := value.Export().(bool); ok && !b {
|
|
|
|
return ErrReturnFalse
|
2019-05-07 05:57:32 +00:00
|
|
|
}
|
2019-04-16 06:07:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|