forked from qcdong2016/PlistDumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
107 lines (89 loc) · 2.25 KB
/
json.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
package main
import (
"encoding/json"
"errors"
"image"
)
var (
ErrNotSupportJsonType = errors.New("not support json type")
ErrNotSupportFileType = errors.New("not support file type")
)
type JsonSize struct {
W int `json:"w"`
H int `json:"h"`
}
type JsonRect struct {
W int `json:"w"`
H int `json:"h"`
X int `json:"x"`
Y int `json:"y"`
}
type JsonMetaData struct {
Image string `json:"image"`
Version string `json:"version"`
}
type JsonVersion struct {
Meta *JsonMetaData `json:"meta"`
Frames interface{} `json:"frames"`
}
type JsonFrameHashV1 struct {
Frames map[string]*JsonFrameV1 `json:"frames"`
}
type JsonFrameArrayV1 struct {
Frames []*JsonFrameV1 `json:"frames"`
}
type JsonFrameV1 struct {
Frame *JsonRect `json:"frame"`
Rotated bool `json:"rotated"`
Trimmed bool `json:"trimmed"`
SpriteSourceSize *JsonRect `json:"spriteSourceSize"`
SourceSize *JsonSize `json:"sourceSize"`
Filename string `json:"filename"`
}
func dumpJson(c *DumpContext) error {
version := JsonVersion{}
err := json.Unmarshal(c.FileContent, &version)
if err != nil {
return err
}
if version.Meta == nil {
return ErrNotSupportJsonType
}
if version.Meta.Version != "1.0" {
return errors.New("unknow version:[" + version.Meta.Version + "]")
}
part := c.AppendPart()
part.ImageFile = version.Meta.Image
frames := map[string]*JsonFrameV1{}
switch version.Frames.(type) {
case map[string]interface{}:
jsonData := JsonFrameHashV1{}
err = json.Unmarshal(c.FileContent, &jsonData)
if err != nil {
return err
}
frames = jsonData.Frames
case []interface{}:
jsonData := JsonFrameArrayV1{}
err = json.Unmarshal(c.FileContent, &jsonData)
if err != nil {
return err
}
for _, v := range jsonData.Frames {
frames[v.Filename] = v
}
default:
return errors.New("unknow version:[" + version.Meta.Version + "]")
}
for k, v := range frames {
f := v.Frame
s := v.SourceSize
part.Frames[k] = &Frame{
Rect: image.Rect(f.X, f.Y, f.X+f.W, f.Y+f.H),
OriginalSize: image.Point{s.W, s.H},
Rotated: ifelse(v.Rotated, 90, 0),
Offset: image.Point{-v.SpriteSourceSize.X / 2, -v.SpriteSourceSize.Y / 2}, //plist offset in center, json in left-top
}
}
return nil
}