-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmkassets.go
229 lines (198 loc) · 4.97 KB
/
mkassets.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//+build ignore
// Copyright 2015 Garrett D'Amore
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"compress/gzip"
"encoding/gob"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"gopkg.in/yaml.v2"
)
var pkg = "main"
// This program builds sprite data as go declarations, loading the data
// from YAML.
func die(format string, v ...interface{}) {
fmt.Fprintf(os.Stderr, format, v...)
fmt.Fprintln(os.Stderr, "")
os.Exit(1)
}
func validateSprite(iname string, data *SpriteData) {
if data.Name == "" {
die("%s: Missing name", iname)
}
for fri, frame := range data.Frames {
if len(frame.Data) != data.Height {
die("Frame %d bad lines (%d != %d)",
fri, len(frame.Data), data.Height)
}
for lno, line := range frame.Data {
if len(line) != data.Width {
die("%s: Frame %d, line %d wrong len "+
"(%d != %d)", iname, fri, lno,
len(line), data.Width)
}
for i := range line {
if line[i] == ' ' {
continue
}
ss := string(line[i])
if _, ok := data.Glyphs[ss]; !ok {
die("%s: Frame %d: line %d: "+
"unknown glyph",
iname, fri, i)
}
}
}
}
}
func writeSpriteGo(w io.Writer, data *SpriteData) {
buf := new(bytes.Buffer)
zbuf, e := gzip.NewWriterLevel(buf, gzip.BestCompression)
if e != nil {
die("Cannot gzip gob: %v", e)
}
enc := gob.NewEncoder(zbuf)
if e := enc.Encode(data); e != nil {
die("Cannot encode gob: %v", e)
}
zbuf.Close()
fmt.Fprintf(w, "// Sprite data file\n")
fmt.Fprintf(w, "// Generated automatically, do not edit!\n")
fmt.Fprintf(w, "package %s\n\n", pkg)
fmt.Fprintf(w, "func init() {\n")
fmt.Fprintf(w, "\tRegisterSpriteGobZ([]byte{\n")
x := buf.Bytes()
for i, b := range x {
if i%16 == 0 {
fmt.Fprint(w, "\t\t")
} else {
fmt.Fprint(w, " ")
}
fmt.Fprintf(w, "0x%02x,", b)
if i%16 == 15 {
fmt.Fprint(w, "\n")
}
}
if len(x)%16 != 0 {
fmt.Fprint(w, "\n")
}
fmt.Fprint(w, "\t})\n}\n")
}
func validateLevel(iname string, level *LevelData) {
if level.Name == "" {
die("Missing level label")
}
if level.Properties.PropInt("width", 0) < 1 ||
level.Properties.PropInt("height", 0) < 1 {
die("Impossible level dimensions")
}
}
func writeLevelGo(w io.Writer, level *LevelData) {
buf := new(bytes.Buffer)
zbuf, e := gzip.NewWriterLevel(buf, gzip.BestCompression)
if e != nil {
die("Cannot gzip gob: %v", e)
}
enc := gob.NewEncoder(zbuf)
if e := enc.Encode(level); e != nil {
die("Cannot encode gob: %v", e)
}
zbuf.Close()
fmt.Fprintf(w, "// Level data file\n")
fmt.Fprintf(w, "// Generated automatically, do not edit!\n")
fmt.Fprintf(w, "package %s\n\n", pkg)
fmt.Fprintf(w, "func init() {\n")
fmt.Fprintf(w, "\tRegisterLevelGobZ([]byte{\n")
x := buf.Bytes()
for i, b := range x {
if i%16 == 0 {
fmt.Fprint(w, "\t\t")
} else {
fmt.Fprint(w, " ")
}
fmt.Fprintf(w, "0x%02x,", b)
if i%16 == 15 {
fmt.Fprint(w, "\n")
}
}
if len(x)%16 != 0 {
fmt.Fprint(w, "\n")
}
fmt.Fprint(w, "\t})\n}\n")
}
func main() {
var inf *os.File
var e error
var asset string = "sprite"
var oname string = ""
var format string = "go"
flag.StringVar(&asset, "type", asset, "asset type [sprite|level]")
flag.StringVar(&pkg, "pkg", pkg, "package name")
flag.StringVar(&oname, "out", "", "output file name")
flag.StringVar(&format, "format", "go", "output format [go|gob]")
flag.Parse()
for _, arg := range flag.Args() {
data := &SpriteData{}
level := &LevelData{}
iname := arg
if inf, e = os.Open(iname); e != nil {
die("%s: open: %v", iname, e)
}
nname := oname
if nname == "" {
nname = iname + ".go"
if strings.HasSuffix(iname, ".yml") {
nname = iname[:len(iname)-4] + ".go"
} else if strings.HasSuffix(iname, ".yaml") {
nname = iname[:len(iname)-5] + ".go"
}
}
all, e := ioutil.ReadAll(inf)
if e != nil {
die("%s; Cannot load: %v", iname, e)
}
buf := new(bytes.Buffer)
switch asset {
case "sprite":
if err := yaml.Unmarshal(all, &data); err != nil {
die("%s: YAML error: %v", iname, err)
}
validateSprite(iname, data)
writeSpriteGo(buf, data)
case "level":
if err := yaml.Unmarshal(all, &level); err != nil {
die("%s: YAML error: %v", iname, err)
}
validateLevel(iname, level)
writeLevelGo(buf, level)
default:
panic("unknown asset class")
}
allb := buf.Bytes()
if nname != "" && nname != "-" {
e := ioutil.WriteFile(nname, allb, 0644)
if e != nil {
die("Cannot create output %s: %v", nname, e)
}
} else {
os.Stdout.Write(allb)
}
}
}