-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
151 lines (133 loc) · 3.36 KB
/
main.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
// Command codegen generates a file called spec.go with
// specifications for each available environment.
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"text/template"
"github.com/unixpickle/essentials"
)
func main() {
spec, err := ioutil.ReadFile("games/spec.json")
if err != nil {
essentials.Die(err)
}
var specObj []map[string]interface{}
if err := json.Unmarshal(spec, &specObj); err != nil {
essentials.Die(err)
}
out, err := os.Create("spec.go")
if err != nil {
essentials.Die(err)
}
defer out.Close()
if err := writeSpec(out, specObj); err != nil {
out.Close()
os.Remove("spec.go")
essentials.Die(err)
}
}
func writeSpec(f *os.File, specObj []map[string]interface{}) error {
t := template.New("template")
t, err := t.Parse(TemplateSource)
if err != nil {
return err
}
if err := inheritVariantFields(specObj); err != nil {
return err
}
if err := formatOptionsField(specObj); err != nil {
return err
}
return t.Execute(f, specObj)
}
func inheritVariantFields(spec []map[string]interface{}) error {
byName := map[string]map[string]interface{}{}
for _, item := range spec {
name, ok := item["name"].(string)
if !ok {
return errors.New("missing or invalid name attribute")
}
byName[name] = item
}
for _, item := range spec {
if parent, ok := item["variant_of"].(string); ok {
ref, ok := byName[parent]
if !ok {
return fmt.Errorf("make variant of %s: spec not found", parent)
}
for key, val := range ref {
if _, ok := item[key]; !ok {
item[key] = val
}
}
}
}
return nil
}
func formatOptionsField(spec []map[string]interface{}) error {
for _, item := range spec {
opts, ok := item["options"]
if !ok {
opts = map[string]interface{}{}
}
data, err := json.Marshal(opts)
if err != nil {
return essentials.AddCtx("marshal options", err)
}
item["options"] = strconv.Quote(string(data))
}
return nil
}
var TemplateSource = `// This file was auto-generated.
package muniverse
// An EnvSpec contains meta-data about an environment.
type EnvSpec struct {
Name string ` + "`bson:\"Name\"`" + `
BaseURL string ` + "`bson:\"BaseURL\"`" + `
Width int ` + "`bson:\"Width\"`" + `
Height int ` + "`bson:\"Height\"`" + `
AllCanvas bool ` + "`bson:\"AllCanvas\"`" + `
Options string ` + "`bson:\"Options\"`" + `
KeyWhitelist []string ` + "`bson:\"KeyWhitelist,omitempty\"`" + `
MouseType string ` + "`bson:\"MouseType\"`" + `
MouseRequired bool ` + "`bson:\"MouseRequired\"`" + `
VariantOf string ` + "`bson:\"VariantOf\"`" + `
}
var EnvSpecs = []*EnvSpec{ {{- range .}}
{
Name: "{{.name}}",
BaseURL: "{{.base}}",
Width: {{.width}},
Height: {{.height}},
AllCanvas: {{if .all_canvas}}true{{else}}false{{end}},
Options: {{.options}},
{{- $length := len .key_whitelist -}}
{{if gt $length 0}}
KeyWhitelist: []string{
{{- range .key_whitelist}}
"{{.}}", {{- end}}
}, {{- end}}
MouseType: "{{.mouseType}}",
MouseRequired: {{if .mouseRequired}}true{{else}}false{{end}},
{{- if .variant_of}}
VariantOf: "{{.variant_of}}",
{{- end}}
},
{{end -}} }
// SpecForName finds the first entry in EnvSpecs with the
// given name.
// If no entry is found, nil is returned.
func SpecForName(name string) *EnvSpec {
for _, s := range EnvSpecs {
if s.Name == name {
return s
}
}
return nil
}
`