/
world.go
205 lines (195 loc) · 4.83 KB
/
world.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"os"
"strconv"
"strings"
lua "github.com/anaminus/gopher-lua"
"github.com/anaminus/pflag"
"github.com/anaminus/rbxmk"
"github.com/anaminus/rbxmk/dump"
"github.com/anaminus/rbxmk/dump/dt"
"github.com/anaminus/rbxmk/enums"
"github.com/anaminus/rbxmk/formats"
"github.com/anaminus/rbxmk/library"
)
// ParseLuaValue parses a string into a Lua value. Numbers, bools, and nil are
// parsed into their respective types, and any other value is interpreted as a
// string.
func ParseLuaValue(s string) lua.LValue {
switch s {
case "true":
return lua.LTrue
case "false":
return lua.LFalse
case "nil":
return lua.LNil
}
if number, err := strconv.ParseFloat(s, 64); err == nil {
return lua.LNumber(number)
}
return lua.LString(s)
}
// WorldFlags are common command flags involved in initializing a World.
type WorldFlags struct {
IncludedRoots []string
InsecurePaths bool
Debug bool
Libraries []string
}
func (f *WorldFlags) SetFlags(flags *pflag.FlagSet) {
flags.StringArrayVar(&f.IncludedRoots, "include-root", nil, DocFlag("Flags/world:Flags/include-root"))
flags.StringArrayVar(&f.Libraries, "libraries", nil, DocFlag("Flags/world:Flags/libraries"))
flags.BoolVar(&f.InsecurePaths, "allow-insecure-paths", false, DocFlag("Flags/world:Flags/allow-insecure-paths"))
flags.BoolVar(&f.Debug, "debug", false, DocFlag("Flags/world:Flags/debug"))
}
// WorldOpt are options to InitWorld.
type WorldOpt struct {
WorldFlags
ExcludeRoots bool
ExcludeFormats bool
ExcludeEnums bool
ExcludeLibraries bool
ExcludeProgram bool
Args []string
}
// InitWorld initializes an rbxmk.World with a common structure.
func InitWorld(opt WorldOpt) (world *rbxmk.World, err error) {
world = rbxmk.NewWorld(lua.NewState(lua.Options{
SkipOpenLibs: true,
IncludeGoStackTrace: opt.Debug,
}))
if !opt.ExcludeRoots {
if opt.InsecurePaths {
world.FS.SetSecured(false)
}
if wd, err := os.Getwd(); err == nil {
// Working directory is an accessible root.
world.FS.AddRoot(wd)
}
for _, root := range opt.IncludedRoots {
world.FS.AddRoot(root)
}
}
if !opt.ExcludeFormats {
for _, f := range formats.All() {
world.RegisterFormat(f())
}
}
if !opt.ExcludeEnums {
world.RegisterEnums(enums.All()...)
}
if !opt.ExcludeLibraries {
libraries := library.All()
included := map[string]bool{}
for _, lib := range libraries {
included[lib.Name] = true
}
for _, list := range opt.Libraries {
for _, name := range strings.Split(list, ",") {
name = strings.TrimSpace(name)
if name == "" {
continue
}
include := true
switch name[0] {
case '-':
include = false
name = name[1:]
case '+':
include = true
name = name[1:]
}
if name == "*" {
for lib := range included {
included[lib] = include
}
} else if _, ok := included[name]; ok {
included[name] = include
}
}
}
for _, lib := range libraries {
if !included[lib.Name] {
continue
}
if err := world.Open(lib); err != nil {
return nil, err
}
}
}
if !opt.ExcludeProgram {
if err := world.Open(ProgramLibrary); err != nil {
return nil, err
}
}
for _, arg := range opt.Args {
world.LuaState().Push(ParseLuaValue(arg))
}
return world, nil
}
func dumpTypes(dst dump.TypeDefs, src []func() rbxmk.Reflector) {
for _, t := range src {
r := t()
if _, ok := dst[r.Name]; ok {
continue
}
dst[r.Name] = r.DumpAll()
dumpTypes(dst, r.Types)
}
}
func DumpWorld(world *rbxmk.World) dump.Root {
state := world.State()
root := dump.Root{
Formats: dump.Formats{},
Types: dump.TypeDefs{},
}
for _, format := range world.Formats() {
root.Formats[format.Name] = format.Dump()
}
for _, l := range world.Libraries() {
if l.Dump == nil {
continue
}
lib := l.Dump(state)
if lib.Name == "" {
lib.Name = l.Name
}
if lib.ImportedAs == "" {
lib.ImportedAs = l.ImportedAs
}
if l.Types != nil {
dumpTypes(root.Types, l.Types)
}
root.Libraries = append(root.Libraries, lib)
}
root.Fragments = DocFragments()
root.Description = "Libraries"
return root
}
var ProgramLibrary = rbxmk.Library{
Name: "program",
ImportedAs: "",
Open: func(s rbxmk.State) *lua.LTable {
lib := s.L.CreateTable(0, 1)
lib.RawSetString("_RBXMK_VERSION", lua.LString(VersionString()))
return lib
},
Dump: func(s rbxmk.State) dump.Library {
return dump.Library{
Name: "program",
ImportedAs: "",
Struct: dump.Struct{
Fields: dump.Fields{
"_RBXMK_VERSION": dump.Property{
ValueType: dt.Prim("string"),
ReadOnly: true,
Summary: "Libraries/program:Fields/_RBXMK_VERSION/Summary",
Description: "Libraries/program:Fields/_RBXMK_VERSION/Description",
},
},
Summary: "Libraries/program:Summary",
Description: "Libraries/program:Description",
},
}
},
}