-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.go
309 lines (280 loc) · 9.14 KB
/
data.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"bytes"
"compress/gzip"
"fmt"
"github.com/Tnze/go-mc/nbt"
"io"
"os"
)
const LatestDataVersion = 2586
// >= 1.0 && <= 1.8.9
// classic map format
type MapClassic struct {
Data struct {
Width int `nbt:"width"`
Height int `nbt:"height"`
XCenter int32 `nbt:"xCenter"`
ZCenter int32 `nbt:"zCenter"`
Scale byte `nbt:"scale"`
Dimension byte `nbt:"dimension"`
Colors []byte `nbt:"colors"`
} `nbt:"data"`
}
// >= 1.9 && <= 1.10.2
// add field: trackingPosition (byte)
type Map169 struct {
Data struct {
Width int `nbt:"width"`
Height int `nbt:"height"`
XCenter int32 `nbt:"xCenter"`
ZCenter int32 `nbt:"zCenter"`
Scale byte `nbt:"scale"`
Dimension byte `nbt:"dimension"`
TrackingPosition byte `nbt:"trackingPosition"`
Colors []byte `nbt:"colors"`
} `nbt:"data"`
}
// >= 1.11 && <= 1.12.2
// add field: unlimitedTracking (byte)
type Map819 struct {
Data struct {
Width int `nbt:"width"`
Height int `nbt:"height"`
XCenter int32 `nbt:"xCenter"`
ZCenter int32 `nbt:"zCenter"`
Scale byte `nbt:"scale"`
Dimension byte `nbt:"dimension"`
UnlimitedTracking byte `nbt:"unlimitedTracking"`
TrackingPosition byte `nbt:"trackingPosition"`
Colors []byte `nbt:"colors"`
} `nbt:"data"`
}
type Versioned struct {
DataVersion int32 `nbt:"DataVersion"`
}
// >= 1.13 && <= 1.13
// add field: banners (but we ignore this anyway)
// del field: width
// del field: height
type Map1519 struct {
Data struct {
XCenter int32 `nbt:"xCenter"`
ZCenter int32 `nbt:"zCenter"`
Scale byte `nbt:"scale"`
Dimension byte `nbt:"dimension"`
UnlimitedTracking byte `nbt:"unlimitedTracking"`
TrackingPosition byte `nbt:"trackingPosition"`
Colors []byte `nbt:"colors"`
} `nbt:"data"`
Versioned
}
// >= 1.13.1 && <= 1.13.2
// change dimension type to int
// add field: frames (but we ignore this anyways)
type Map1628 struct {
Data struct {
XCenter int32 `nbt:"xCenter"`
ZCenter int32 `nbt:"zCenter"`
Scale byte `nbt:"scale"`
Dimension int `nbt:"dimension"`
UnlimitedTracking byte `nbt:"unlimitedTracking"`
TrackingPosition byte `nbt:"trackingPosition"`
Colors []byte `nbt:"colors"`
} `nbt:"data"`
Versioned
}
// >= 1.14 && <= 1.14.4
// add field: locked (byte)
type Map1952 struct {
Data struct {
XCenter int32 `nbt:"xCenter"`
ZCenter int32 `nbt:"zCenter"`
Scale byte `nbt:"scale"`
Dimension int `nbt:"dimension"`
UnlimitedTracking byte `nbt:"unlimitedTracking"`
TrackingPosition byte `nbt:"trackingPosition"`
Locked byte `nbt:"locked"`
Colors []byte `nbt:"colors"`
} `nbt:"data"`
Versioned
}
// >= 1.16 && <= 1.16.4
// change dimension type to string (resource location)
type Map2566 struct {
Data struct {
XCenter int32 `nbt:"xCenter"`
ZCenter int32 `nbt:"zCenter"`
Scale byte `nbt:"scale"`
Dimension string `nbt:"dimension"`
UnlimitedTracking byte `nbt:"unlimitedTracking"`
TrackingPosition byte `nbt:"trackingPosition"`
Locked byte `nbt:"locked"`
Colors []byte `nbt:"colors"`
} `nbt:"data"`
Versioned
}
// >= 1.16.5
// add field: UUIDMost (long)
// add field: UUIDLeast (long)
type Map2586 struct {
Data struct {
UuidMost int64 `nbt:"UUIDMost"`
UuidLeast int64 `nbt:"UUIDLeast"`
XCenter int32 `nbt:"xCenter"`
ZCenter int32 `nbt:"zCenter"`
Scale byte `nbt:"scale"`
Dimension string `nbt:"dimension"`
UnlimitedTracking byte `nbt:"unlimitedTracking"`
TrackingPosition byte `nbt:"trackingPosition"`
Locked byte `nbt:"locked"`
Colors []byte `nbt:"colors"`
} `nbt:"data"`
Versioned
}
type Map struct {
Data struct {
Width int `nbt:"-"`
Height int `nbt:"-"`
UuidMost int64 `nbt:"UUIDMost"`
UuidLeast int64 `nbt:"UUIDLeast"`
XCenter int32 `nbt:"xCenter"`
ZCenter int32 `nbt:"zCenter"`
Scale byte `nbt:"scale"`
Dimension string `nbt:"dimension"`
UnlimitedTracking byte `nbt:"unlimitedTracking"`
TrackingPosition byte `nbt:"trackingPosition"`
Locked byte `nbt:"locked"`
Colors []byte `nbt:"colors"`
Banners []byte `nbt:"banners,list"`
Frames []byte `nbt:"frames,list"`
} `nbt:"data"`
Versioned
}
func ReadNbt(b []byte) (m Map, err error) {
var v Versioned
_, err = nbt.NewDecoder(bytes.NewReader(b)).Decode(&v)
if err != nil {
return m, fmt.Errorf("failed to decode nbt data: %w", err)
}
if v.DataVersion <= 1343 {
var map1343 Map819
_, err = nbt.NewDecoder(bytes.NewReader(b)).Decode(&map1343)
m.Data.Width = map1343.Data.Width
m.Data.Height = map1343.Data.Height
m.Data.UuidMost = 0 // TODO
m.Data.UuidLeast = 0 // TODO
m.Data.XCenter = map1343.Data.XCenter
m.Data.ZCenter = map1343.Data.ZCenter
m.Data.Scale = map1343.Data.Scale
m.Data.Dimension = dimById(int(map1343.Data.Dimension))
m.Data.UnlimitedTracking = map1343.Data.UnlimitedTracking
m.Data.TrackingPosition = map1343.Data.TrackingPosition
m.Data.Locked = 0
m.Data.Colors = map1343.Data.Colors
m.DataVersion = LatestDataVersion
} else if v.DataVersion <= 1519 {
var map1519 Map1519
_, err = nbt.NewDecoder(bytes.NewReader(b)).Decode(&map1519)
m.Data.Width = 128
m.Data.Height = 128
m.Data.UuidMost = 0 // TODO
m.Data.UuidLeast = 0 // TODO
m.Data.XCenter = map1519.Data.XCenter
m.Data.ZCenter = map1519.Data.ZCenter
m.Data.Scale = map1519.Data.Scale
m.Data.Dimension = dimById(int(map1519.Data.Dimension))
m.Data.UnlimitedTracking = map1519.Data.UnlimitedTracking
m.Data.TrackingPosition = map1519.Data.TrackingPosition
m.Data.Locked = 0
m.Data.Colors = map1519.Data.Colors
m.DataVersion = LatestDataVersion
} else if v.DataVersion <= 1628 {
var map1628 Map1628
_, err = nbt.NewDecoder(bytes.NewReader(b)).Decode(&map1628)
m.Data.Width = 128
m.Data.Height = 128
m.Data.UuidMost = 0 // TODO
m.Data.UuidLeast = 0 // TODO
m.Data.XCenter = map1628.Data.XCenter
m.Data.ZCenter = map1628.Data.ZCenter
m.Data.Scale = map1628.Data.Scale
m.Data.Dimension = dimById(map1628.Data.Dimension)
m.Data.UnlimitedTracking = map1628.Data.UnlimitedTracking
m.Data.TrackingPosition = map1628.Data.TrackingPosition
m.Data.Locked = 0
m.Data.Colors = map1628.Data.Colors
m.DataVersion = LatestDataVersion
} else if v.DataVersion <= 1952 {
var map1952 Map1952
_, err = nbt.NewDecoder(bytes.NewReader(b)).Decode(&map1952)
m.Data.Width = 128
m.Data.Height = 128
m.Data.UuidMost = 0 // TODO
m.Data.UuidLeast = 0 // TODO
m.Data.XCenter = map1952.Data.XCenter
m.Data.ZCenter = map1952.Data.ZCenter
m.Data.Scale = map1952.Data.Scale
m.Data.Dimension = dimById(map1952.Data.Dimension)
m.Data.UnlimitedTracking = map1952.Data.UnlimitedTracking
m.Data.TrackingPosition = map1952.Data.TrackingPosition
m.Data.Locked = map1952.Data.Locked
m.Data.Colors = map1952.Data.Colors
m.DataVersion = LatestDataVersion
} else if v.DataVersion <= 2566 {
var map2566 Map2566
_, err = nbt.NewDecoder(bytes.NewReader(b)).Decode(&map2566)
m.Data.Width = 128
m.Data.Height = 128
m.Data.UuidMost = 0 // TODO
m.Data.UuidLeast = 0 // TODO
m.Data.XCenter = map2566.Data.XCenter
m.Data.ZCenter = map2566.Data.ZCenter
m.Data.Scale = map2566.Data.Scale
m.Data.Dimension = map2566.Data.Dimension
m.Data.UnlimitedTracking = map2566.Data.UnlimitedTracking
m.Data.TrackingPosition = map2566.Data.TrackingPosition
m.Data.Locked = map2566.Data.Locked
m.Data.Colors = map2566.Data.Colors
m.DataVersion = LatestDataVersion
} else if v.DataVersion <= 2586 {
var map2586 Map2586
_, err = nbt.NewDecoder(bytes.NewReader(b)).Decode(&map2586)
m.Data.Width = 128
m.Data.Height = 128
m.Data.UuidMost = map2586.Data.UuidMost
m.Data.UuidLeast = map2586.Data.UuidLeast
m.Data.XCenter = map2586.Data.XCenter
m.Data.ZCenter = map2586.Data.ZCenter
m.Data.Scale = map2586.Data.Scale
m.Data.Dimension = map2586.Data.Dimension
m.Data.UnlimitedTracking = map2586.Data.UnlimitedTracking
m.Data.TrackingPosition = map2586.Data.TrackingPosition
m.Data.Locked = map2586.Data.Locked
m.Data.Colors = map2586.Data.Colors
m.DataVersion = LatestDataVersion
} else {
err = fmt.Errorf("unsupported data version %v", v.DataVersion)
}
if err != nil {
return m, fmt.Errorf("failed to decode nbt data (data version %v): %w", v.DataVersion, err)
}
return m, nil
}
func LoadMap(path string) (m Map, err error) {
fr, err := os.Open(path)
if err != nil {
return m, fmt.Errorf("failed to open file: %w", err)
}
defer fr.Close()
// map files are always gzipped
gzr, err := gzip.NewReader(fr)
if err != nil {
return m, fmt.Errorf("failed to decompress gzip: %w", err)
}
b, err := io.ReadAll(gzr)
if err != nil {
return m, fmt.Errorf("failed to read data: %w", err)
}
return ReadNbt(b)
}