-
Notifications
You must be signed in to change notification settings - Fork 20
/
scripting.go
152 lines (130 loc) · 2.9 KB
/
scripting.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package scripting
import (
"encoding/json"
"reflect"
"strconv"
"github.com/df-mc/dragonfly/server/world"
"github.com/dop251/goja"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sirupsen/logrus"
_ "embed"
)
//go:embed enums.js
var enums_js string
type VM struct {
vm *goja.Runtime
CB struct {
OnEntityAdd func(entity any, metadata *goja.Object) (ignore bool)
OnChunkAdd func(pos world.ChunkPos) (ignore bool)
OnEntityDataUpdate func(entity any, metadata *goja.Object)
}
}
func New() *VM {
v := &VM{
vm: goja.New(),
}
console := v.vm.NewObject()
console.Set("log", func(val goja.Value) {
if val.SameAs(goja.Undefined()) {
logrus.Println("undefined")
return
}
if val.ExportType().Kind() == reflect.String {
logrus.Println(val.String())
return
}
obj := val.ToObject(v.vm)
data, err := json.MarshalIndent(obj, "", " ")
if err != nil {
panic(err)
}
logrus.Println(string(data))
})
v.vm.GlobalObject().Set("console", console)
_, err := v.vm.RunString(enums_js)
if err != nil {
panic(err)
}
return v
}
func (v *VM) tryResolveCB(name string, fun any) {
val := v.vm.Get(name)
if val == nil {
return
}
err := v.vm.ExportTo(val, fun)
if err != nil {
logrus.Error(err)
}
}
func (v *VM) Load(script string) error {
_, err := v.vm.RunScript("script.js", script)
if err != nil {
return err
}
v.tryResolveCB("OnEntityAdd", &v.CB.OnEntityAdd)
v.tryResolveCB("OnChunkAdd", &v.CB.OnChunkAdd)
v.tryResolveCB("OnEntityDataUpdate", &v.CB.OnEntityDataUpdate)
return nil
}
type entityDataObject struct {
d protocol.EntityMetadata
r *goja.Runtime
}
func (m entityDataObject) Get(key string) goja.Value {
if key == "Flag" {
f := m.d.Flag
return m.r.ToValue(&f)
}
if key == "SetFlag" {
f := m.d.SetFlag
return m.r.ToValue(&f)
}
k, err := strconv.Atoi(key)
if err != nil {
return nil
}
d, ok := m.d[uint32(k)]
if !ok {
return nil
}
return m.r.ToValue(d)
}
func (m entityDataObject) Set(key string, val goja.Value) bool {
k, err := strconv.Atoi(key)
if err != nil {
return false
}
m.d[uint32(k)] = val.Export()
return true
}
func (m entityDataObject) Has(key string) bool {
k, err := strconv.Atoi(key)
if err != nil {
return false
}
_, ok := m.d[uint32(k)]
return ok
}
func (m entityDataObject) Delete(key string) bool {
k, err := strconv.Atoi(key)
if err != nil {
return false
}
delete(m.d, uint32(k))
return true
}
func (m entityDataObject) Keys() (keys []string) {
for k := range m.d {
keys = append(keys, strconv.Itoa(int(k)))
}
return
}
func (v *VM) OnEntityAdd(entity any, metadata protocol.EntityMetadata) bool {
data := v.vm.NewDynamicObject(entityDataObject{metadata, v.vm})
return v.CB.OnEntityAdd(entity, data)
}
func (v *VM) OnEntityDataUpdate(entity any, metadata protocol.EntityMetadata) {
data := v.vm.NewDynamicObject(entityDataObject{metadata, v.vm})
v.CB.OnEntityDataUpdate(entity, data)
}