-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathsave.go
303 lines (265 loc) · 7.8 KB
/
save.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
package factorio
import (
"archive/zip"
"encoding/binary"
"fmt"
"io"
"os"
)
type archiveFile struct {
io.ReadCloser
archive io.Closer
}
func (af *archiveFile) Close() error {
if af.ReadCloser != nil {
if err := af.ReadCloser.Close(); err != nil {
return err
}
}
if af.archive != nil {
if err := af.archive.Close(); err != nil {
return err
}
}
return nil
}
// openNames contains a list of all. Will stop searching at the first occurance
func OpenArchiveFile(path string, openNames ...string) (r io.ReadCloser, err error) {
archive, err := zip.OpenReader(path)
if err != nil {
return nil, err
}
f := &archiveFile{archive: archive}
for _, file := range archive.File {
name := file.FileInfo().Name()
for _, openName := range openNames {
if name == openName {
f.ReadCloser, err = file.Open()
if err != nil {
archive.Close()
return nil, err
}
return f, nil
}
}
}
archive.Close()
return nil, os.ErrNotExist
}
type SaveHeader struct {
FactorioVersion Version `json:"factorio_version"`
Campaign string `json:"campaign"`
Name string `json:"name"`
BaseMod string `json:"base_mod"`
Difficulty uint8 `json:"difficulty"`
Finished bool `json:"finished"`
PlayerWon bool `json:"player_won"`
NextLevel string `json:"next_level"`
CanContinue bool `json:"can_continue"`
FinishedButContinuing bool `json:"finished_but_continuing"`
SavingReplay bool `json:"saving_replay"`
AllowNonAdminDebugOptions bool `json:"allow_non_admin_debug_options"`
LoadedFrom Version `json:"loaded_from"`
LoadedFromBuild uint16 `json:"loaded_from_build"`
AllowedCommands uint8 `json:"allowed_commands"`
Stats map[byte][]map[uint16]uint32 `json:"stats,omitempty"`
Mods []Mod `json:"mods"`
}
type Mod struct {
Name string `json:"name"`
Version Version `json:"version"`
CRC uint32 `json:"crc"`
}
// Mostly based on https://forums.factorio.com/viewtopic.php?f=5&t=8568&p=277892&hilit=level.dat+python#p277892
func (h *SaveHeader) ReadFrom(r io.Reader) (err error) {
var scratch [8]byte
var fv version64
_, err = r.Read(scratch[:8])
if err != nil {
return err
}
if err := fv.UnmarshalBinary(scratch[:8]); err != nil {
return fmt.Errorf("read FactorioVersion: %v", err)
}
h.FactorioVersion = Version(fv)
atLeast016 := !h.FactorioVersion.Less(Version{0, 16, 0, 0})
if !h.FactorioVersion.Less(Version{0, 17, 0, 0}) {
//FIXME correct naming
_, err = r.Read(scratch[:1])
if err != nil {
return fmt.Errorf("read first random 0.17 byte: %v", err)
}
}
// campaign, example: "transport-belt-madness"
h.Campaign, err = readString(r, Version(h.FactorioVersion), false)
if err != nil {
return fmt.Errorf("read Campaign: %v", err)
}
// level name, example: "level-01"
h.Name, err = readString(r, Version(h.FactorioVersion), false)
if err != nil {
return fmt.Errorf("read Name: %v", err)
}
// name of the base mod, example: "base"
h.BaseMod, err = readString(r, Version(h.FactorioVersion), false)
if err != nil {
return fmt.Errorf("read BaseMod: %v", err)
}
// Read Difficulty
_, err = r.Read(scratch[:1])
if err != nil {
return fmt.Errorf("read Difficulty: %v", err)
}
h.Difficulty = scratch[0]
// Read finished as bool
_, err = r.Read(scratch[:1])
if err != nil {
return fmt.Errorf("read Finished: %v", err)
}
h.Finished = scratch[0] != 0
// Read playerWon as bool
_, err = r.Read(scratch[:1])
if err != nil {
return fmt.Errorf("read PlayerWon: %v", err)
}
h.PlayerWon = scratch[0] != 0
// Read NextLevel string (normally empty)
h.NextLevel, err = readString(r, Version(h.FactorioVersion), false)
if err != nil {
return fmt.Errorf("read NextLevel: %v", err)
}
if !h.FactorioVersion.Less(Version{0, 12, 0, 0}) {
_, err = r.Read(scratch[:1])
if err != nil {
return fmt.Errorf("read CanContinue: %v", err)
}
h.CanContinue = scratch[0] != 0
_, err = r.Read(scratch[:1])
if err != nil {
return fmt.Errorf("read FinishedButContinuing: %v", err)
}
h.FinishedButContinuing = scratch[0] != 0
}
_, err = r.Read(scratch[:1])
if err != nil {
return fmt.Errorf("read SavingReplay: %v", err)
}
h.SavingReplay = scratch[0] != 0
if atLeast016 {
_, err = r.Read(scratch[:1])
if err != nil {
return fmt.Errorf("read AllowNonAdminDebugOptions: %v", err)
}
h.AllowNonAdminDebugOptions = scratch[0] != 0
}
var loadedFrom version48
err = loadedFrom.ReadFrom(r, Version(h.FactorioVersion))
if err != nil {
return fmt.Errorf("read LoadedFrom: %v", err)
}
h.LoadedFrom = Version(loadedFrom)
_, err = r.Read(scratch[:2])
if err != nil {
return fmt.Errorf("read LoadedFromBuild: %v", err)
}
h.LoadedFromBuild = binary.LittleEndian.Uint16(scratch[:2])
_, err = r.Read(scratch[:1])
if err != nil {
return fmt.Errorf("read AllowedCommands: %v", err)
}
h.AllowedCommands = scratch[0]
if h.FactorioVersion.Less(Version{0, 13, 0, 87}) {
if h.AllowedCommands == 0 {
h.AllowedCommands = 2
} else {
h.AllowedCommands = 1
}
}
if h.FactorioVersion.Less(Version{0, 13, 0, 42}) {
h.Stats, err = h.readStats(r)
if err != nil {
return fmt.Errorf("read Stats: %v", err)
}
}
var n uint32
if atLeast016 {
n, err = readOptimUint(r, Version(h.FactorioVersion), 32)
if err != nil {
return fmt.Errorf("read num mods: %v", err)
}
} else {
_, err = r.Read(scratch[:4])
if err != nil {
return fmt.Errorf("read num mods: %v", err)
}
n = binary.LittleEndian.Uint32(scratch[:4])
}
for i := uint32(0); i < n; i++ {
var m Mod
if err = (&m).ReadFrom(r, Version(h.FactorioVersion)); err != nil {
return fmt.Errorf("read mod: %v", err)
}
h.Mods = append(h.Mods, m)
}
return nil
}
func (h SaveHeader) readStats(r io.Reader) (stats map[byte][]map[uint16]uint32, err error) {
var scratch [4]byte
stats = make(map[byte][]map[uint16]uint32)
_, err = r.Read(scratch[:4])
if err != nil {
return nil, err
}
n := binary.LittleEndian.Uint32(scratch[:4])
for i := uint32(0); i < n; i++ {
_, err := r.Read(scratch[:1])
if err != nil {
return nil, fmt.Errorf("read stat %d force id: %v", i, err)
}
id := scratch[1]
for j := 0; j < 3; j++ {
st := make(map[uint16]uint32)
_, err = r.Read(scratch[:4])
if err != nil {
return nil, fmt.Errorf("read stat %d (id %d) length: %v", i, id, err)
}
length := binary.LittleEndian.Uint32(scratch[:4])
for k := uint32(0); k < length; k++ {
_, err = r.Read(scratch[:2])
if err != nil {
return nil, fmt.Errorf("read stat %d (id %d; index %d) key: %v", i, id, k, err)
}
key := binary.LittleEndian.Uint16(scratch[:2])
_, err = r.Read(scratch[:4])
if err != nil {
return nil, fmt.Errorf("read stat %d (id %d; index %d) val: %v", i, id, k, err)
}
val := binary.LittleEndian.Uint32(scratch[:4])
st[key] = val
}
stats[id] = append(stats[id], st)
}
}
return stats, nil
}
func (m *Mod) ReadFrom(r io.Reader, game Version) (err error) {
m.Name, err = readString(r, game, true)
if err != nil {
return fmt.Errorf("read Name: %v", err)
}
var version version48
err = version.ReadFrom(r, game)
if err != nil {
return err
}
m.Version = Version(version)
var scratch [4]byte
if game.Greater(Version{0, 15, 0, 91}) {
_, err = r.Read(scratch[:4])
if err != nil {
return err
}
m.CRC = binary.LittleEndian.Uint32(scratch[:4])
}
return nil
}