This repository has been archived by the owner on Nov 25, 2021. It is now read-only.
/
readview.go
179 lines (148 loc) · 3.87 KB
/
readview.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
/*
Copyright (c) 2017-2018 Andreas T Jonsson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package api
import (
"bytes"
"encoding/gob"
"encoding/json"
"errors"
)
type ReadViewRequest struct {
ViewID int `json:"view_id"`
RLE bool `json:"rle"`
}
type UnitViewData struct {
ID uint64 `json:"unit_id"`
Allegiance Allegiance `json:"allegiance"`
Race UnitRace `json:"race"`
Class UnitClass `json:"class"`
}
type ItemViewData struct {
ID uint64 `json:"item_id"`
Class ItemClass `json:"class"`
}
type ReadViewBase struct {
Flags TileFlag `json:"flags"`
UserFlags UserFlag `json:"usrflags"`
Height uint8 `json:"height"`
BuildingType BuildingType `json:"building_type"`
StructureType StructureType `json:"structure_type"`
StructureMaterial ItemClass `json:"structure_material"`
Building uint64 `json:"building"`
}
type ReadViewData struct {
ReadViewBase
Units []UnitViewData `json:"units"`
Items []ItemViewData `json:"items"`
RLE uint8 `json:"rle"`
}
type ReadViewResponse struct {
W uint16 `json:"w"`
H uint16 `json:"h"`
RLESize uint32 `json:"rle_size"`
Data []ReadViewData `json:"data"`
}
func (rvr *ReadViewResponse) UnmarshalJSON(data []byte) error {
dec := json.NewDecoder(bytes.NewReader(data))
dec.Token() // {
dec.Token() // w
if err := dec.Decode(&rvr.W); err != nil {
return err
}
dec.Token() // h
if err := dec.Decode(&rvr.H); err != nil {
return err
}
dec.Token() // rle_size
if err := dec.Decode(&rvr.RLESize); err != nil {
return err
}
dec.Token() // data
idx := 0
// Fastpath if there is no compression
if rvr.RLESize == 0 {
if err := dec.Decode(&rvr.Data); err != nil {
return err
}
goto end
}
rvr.Data = make([]ReadViewData, int(rvr.W)*int(rvr.H))
dec.Token() // {
for i := 0; i < int(rvr.RLESize); i++ {
rvd := &rvr.Data[idx]
idx++
if err := dec.Decode(rvd); err != nil {
return err
}
for j := 0; j < int(rvd.RLE); j++ {
rvr.Data[idx] = *rvd
idx++
}
}
dec.Token() // }
end: // Verify the last token
if tok, err := dec.Token(); err != nil {
return err
} else {
if delim, ok := tok.(json.Delim); !ok || delim != '}' {
return errors.New("invalid delimiter")
}
return nil
}
}
func (rvr *ReadViewResponse) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
enc.Encode(rvr.W)
enc.Encode(rvr.H)
enc.Encode(rvr.RLESize)
if rvr.RLESize == 0 {
enc.Encode(rvr.Data)
} else {
for i := 0; i < int(rvr.RLESize); i++ {
enc.Encode(rvr.Data[i])
}
}
return buf.Bytes(), nil
}
func (rvr *ReadViewResponse) UnmarshalBinary(data []byte) error {
dec := gob.NewDecoder(bytes.NewReader(data))
if err := dec.Decode(&rvr.W); err != nil {
return err
}
if err := dec.Decode(&rvr.H); err != nil {
return err
}
if err := dec.Decode(&rvr.RLESize); err != nil {
return err
}
// Fastpath if there is no compression
if rvr.RLESize == 0 {
return dec.Decode(&rvr.Data)
}
rvr.Data = make([]ReadViewData, int(rvr.W)*int(rvr.H))
idx := 0
for i := 0; i < int(rvr.RLESize); i++ {
rvd := &rvr.Data[idx]
idx++
if err := dec.Decode(rvd); err != nil {
return err
}
for j := 0; j < int(rvd.RLE); j++ {
rvr.Data[idx] = *rvd
idx++
}
}
return nil
}