summaryrefslogtreecommitdiff
path: root/main.go
blob: abde00f4b2b6b4976c09d8553386e2f18ca7afc4 (plain)
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
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"math"
	"os"
)

type Root struct {
	Path        string
	Name        string
	NName       string
	ImageWidth  float64
	ImageHeight float64
	TileWidth   float64
	TileHeight  float64
	Margin      float64
	Columns     float64
	TileCount   float64
	Tiles       []Tile
}

type Tile struct {
	Id          float64
	Type        string
	Probability float64
	Frames      []Frame
}

type Frame struct {
	Duration float64
	TileId   float64
}

func main() {
	out := "#include \"tsj2c.h\"\n"
	names := []string{}
	for i := 1; i < len(os.Args); i++ {
		result, err := process(os.Args[i])
		if err != nil {
			panic(err)
		}
		out += fmt.Sprint(result)
		names = append(names, result.Name)
	}
	out += "const Tsj2cSet *tsj2c_sets[]={"
	for i := 0; i < len(names); i++ {
		out += "&" + names[i] + ","
	}
	out += "0};\n"
	fmt.Print(out)
}

func process(path string) (Root, error) {
	content, err := ioutil.ReadFile(path)
	if err != nil {
		panic(err)
	}

	var data map[string]interface{}
	err = json.Unmarshal(content, &data)
	if err != nil {
		panic(err)
	}

	var root = Root{
		Path:        path,
		Name:        "",
		NName:       data["name"].(string),
		ImageWidth:  data["imagewidth"].(float64),
		ImageHeight: data["imageheight"].(float64),
		TileWidth:   data["tilewidth"].(float64),
		TileHeight:  data["tileheight"].(float64),
		Margin:      data["margin"].(float64),
		Columns:     data["columns"].(float64),
		TileCount:   data["tilecount"].(float64),
		Tiles:       []Tile{},
	}
	for i := 0; i < len(root.Path); i++ {
		chr := root.Path[i]
		if (chr >= '0' && chr <= '9') || (chr >= 'A' && chr <= 'Z') ||
			(chr >= 'a' && chr <= 'z') {
			root.Name += string(chr)
		} else {
			root.Name += "_"
		}
	}

	tiles := data["tiles"].([]interface{})
	for i := 0; i < len(tiles); i++ {
		root.Tiles = append(root.Tiles, NewTile(tiles[i].(map[string]interface{})))
	}
	return root, nil
}

func (this Root) String() string {
	out := "static const Tsj2cSet " + this.Name + "={"
	out += "\"" + this.NName + "\","
	out += "\"" + this.Path + "\","
	out += fmt.Sprint(this.ImageWidth) + ","
	out += fmt.Sprint(this.ImageHeight) + ","
	out += fmt.Sprint(this.TileWidth) + ","
	out += fmt.Sprint(this.TileHeight) + ","
	out += fmt.Sprint(this.Margin) + ","
	out += fmt.Sprint(this.Columns) + ","
	out += fmt.Sprint(this.TileCount) + ","
	out += fmt.Sprint(len(this.Tiles)) + ","

	out += "(const Tsj2cTile[]){"
	for i := 0; i < len(this.Tiles); i++ {
		out += this.Tiles[i].String() + ","
	}
	out += "}"

	out += "};\n"
	return out
}

func (this Tile) String() string {
	out := "{"
	out += fmt.Sprint(this.Id) + ","
	if this.Type != "" {
		out += "\"" + this.Type + "\","
	} else {
		out += "0,"
	}
	if math.IsNaN(this.Probability) {
		out += "1,"
	} else {
		out += fmt.Sprint(this.Probability) + ","
	}

	out += fmt.Sprint(len(this.Frames)) + ","
	if len(this.Frames) > 0 {
		out += "(const Tsj2cFrame[]){"
		for i := 0; i < len(this.Frames); i++ {
			if i > 0 {
				out += ","
			}
			out += this.Frames[i].String()
		}
		out += "}"
	} else {
		out += "0"
	}

	out += "}"
	return out
}

func (this Frame) String() string {
	out := "{"
	out += fmt.Sprint(this.Duration) + ","
	out += fmt.Sprint(this.TileId)
	out += "}"
	return out
}

func NewTile(jtile map[string]interface{}) Tile {
	tile := Tile{
		Id:          jtile["id"].(float64),
		Type:        "",
		Probability: math.NaN(),
		Frames:      []Frame{},
	}
	v, ok := jtile["type"]
	if ok {
		tile.Type = v.(string)
	}
	v, ok = jtile["probability"]
	if ok {
		tile.Probability = v.(float64)
	}
	v, ok = jtile["animation"]
	if ok {
		frames := v.([]interface{})
		for i := 0; i < len(frames); i++ {
			frame := frames[i].(map[string]interface{})
			tile.Frames = append(tile.Frames, NewFrame(frame))
		}
	}
	return tile
}

func NewFrame(src map[string]interface{}) Frame {
	return Frame{
		Duration: src["duration"].(float64),
		TileId:   src["tileid"].(float64),
	}
}