/
main.go
341 lines (303 loc) · 7.24 KB
/
main.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2014
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"flag"
"github.com/robmerrell/comandante"
"hash/crc32"
"io"
"io/ioutil"
"log"
"os"
)
// http://www.gamefaqs.com/snes/554041-final-fantasy-iii/faqs/11544 for basics,
// then loads of trial and error.
type (
uint24 struct {
Data [3]uint8
}
CharacterName [27]byte
Time struct {
Hours uint8
Minutes uint8
Seconds uint8
}
Header struct {
Checksum uint32
Padding [12]byte
}
Location struct {
X uint8
Y uint8
}
Body struct {
Characters [16]Character
Parties Party
Gil uint24
Time Time
Steps uint24
SpellMastery SpellMasteries
Allzeroes [332]byte `json:"-"`
Something uint8
Unknown222 [49]byte
Swdtech uint8
BlitzMastery BlitzMastery
LoreMastery LoreMastery
RageMastery RageMastery
DanceMastery DanceMastery
Unknown22 [361]byte
Something2 uint8
Unknown22a [169]byte
CharacterWorldmapLocation Location
AirshipWorldmapLocation Location
Unknown22b [9]byte
Something3 uint8
Unknown22c [51]byte
Something4 [5]byte
Unknown22d [88]byte
Checksum uint16 `json:"-"`
}
Iv struct {
EspersOwned EspersOwned
Unknown [12]uint8
Inventory Inventory
Unknown4 [430]uint8
IvChecksum uint16 `json:"-"`
}
Body2 struct {
Unknown41 [1028]uint8 //`json:"-"`
Names [16]CharacterName
Unknown5 [736]uint8 //`json:"-"`
}
Save struct {
Header `json:"-"`
Body Body
Iv Iv
Body2 Body2
Allzeroes2 [15212]byte `json:"-"`
}
FileHeader struct {
Unknown1 [20]byte
LastSaveSlot uint32
Unknown2 [12]byte
Version uint32
Unknown3 [24]byte //`json:"-"`
Header `json:"-"`
}
String16 [16]byte
HaltData struct {
Header `json:"-"`
Data [25600]byte
}
SaveFile struct {
FF6 String16 //`json:"-"`
Mobile String16 //`json:"-"`
FileHeader FileHeader //`json:"-"`
Saves [5]Save
HaltData HaltData
Unknown [16512]byte //`json:"-"`
}
Character struct {
Id CharId
Portrait Portrait
Unknown [6]uint8
Level uint8
CurrHp uint16
TotHp uint16
CurrMp uint16
TotMp uint16
Experience uint32
Status Status
Command [4]Command
Strength uint8
Speed uint8
Stamina uint8
Magic uint8
Esper Esper
RightHand Equipable
LeftHand Equipable
Helmet Equipable
Body Equipable
Relic1 Equipable
Relic2 Equipable
}
Writer interface {
Write(io.Writer) error
}
)
const (
FileChecksumOffset = 32 + 36 + 4 + 24
HeaderSize = 16
SaveEntrySize = 21008
FirstSaveOffset = 112
)
func csData(v interface{}) []byte {
buf := bytes.NewBuffer(nil)
binary.Write(buf, binary.LittleEndian, v)
data := buf.Bytes()
// Don't include the actual checksum value in the
// calculation of it ;)
off := len(data) - 2
var sum uint16
for _, v := range data[:off] {
sum += uint16(v)
}
binary.LittleEndian.PutUint16(data[off:], sum)
return data
}
func (i *Iv) Encode() []byte {
data := csData(*i)
off := len(data) - 2
curr := binary.LittleEndian.Uint16(data[off:])
if curr != 0 {
// TODO(.): why do I need to do this?
binary.LittleEndian.PutUint16(data[off:], curr+1)
}
return data
}
func (b *Body) Encode() []byte {
return csData(*b)
}
func (s *Save) Encode() []byte {
buf := bytes.NewBuffer(nil)
buf.Write(s.Body.Encode())
buf.Write(s.Iv.Encode())
binary.Write(buf, binary.LittleEndian, s.Body2)
buf.Write(s.Allzeroes2[:])
s.Checksum = crc32.ChecksumIEEE(buf.Bytes())
buf2 := bytes.NewBuffer(nil)
binary.Write(buf2, binary.LittleEndian, s.Header)
buf2.Write(buf.Bytes())
return buf2.Bytes()
}
func (s *SaveFile) Encode() []byte {
s.FileHeader.Checksum = 0
buf := bytes.NewBuffer(nil)
buf.Write(s.Mobile[:])
binary.Write(buf, binary.LittleEndian, s.FileHeader)
for i := range s.Saves {
buf.Write(s.Saves[i].Encode())
}
s.HaltData.Checksum = crc32.ChecksumIEEE(s.HaltData.Data[:])
binary.Write(buf, binary.LittleEndian, s.HaltData)
binary.Write(buf, binary.LittleEndian, s.Unknown)
data := buf.Bytes()
s.FileHeader.Checksum = crc32.ChecksumIEEE(data)
binary.LittleEndian.PutUint32(data[80:], s.FileHeader.Checksum)
buf2 := bytes.NewBuffer(nil)
buf2.Write(s.FF6[:])
buf2.Write(data)
return buf2.Bytes()
}
func (u *uint24) Set(j uint32) {
for i := range u.Data {
u.Data[i] = uint8((j >> (uint(i) * 8)) & 0xff)
}
}
func (u uint24) Uint() uint32 {
var j uint32
for i, v := range u.Data {
j |= uint32(v) << uint32(i*8)
}
return j
}
func (u *uint24) UnmarshalJSON(data []byte) error {
var j uint32
if err := json.Unmarshal(data, &j); err != nil {
return err
}
u.Set(j)
return nil
}
func (u uint24) MarshalJSON() ([]byte, error) {
return json.Marshal(u.Uint())
}
func (n *CharacterName) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
copy(n[:], []byte(s))
return nil
}
func (n CharacterName) MarshalJSON() ([]byte, error) {
bytes := n[:]
i := len(bytes) - 1
for i > 0 && bytes[i] == 0 {
i--
}
return json.Marshal(string(bytes[:i+1]))
}
func (n String16) MarshalJSON() ([]byte, error) {
return json.Marshal(string(n[:]))
}
func (n *String16) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
copy(n[:], []byte(s))
return nil
}
var (
savfile = "filesRockDat0.sav"
jsonfile = "filesRockDat0.json"
)
func tojson() error {
f, err := os.Open(savfile)
if err != nil {
return err
}
defer f.Close()
var s SaveFile
if err := binary.Read(f, binary.LittleEndian, &s); err != nil {
return err
}
d, err := json.MarshalIndent(s, "", "\t")
if err != nil {
return err
}
return ioutil.WriteFile(jsonfile, d, 0644)
}
func fromjson() error {
data, err := ioutil.ReadFile(jsonfile)
if err != nil {
return err
}
var s SaveFile
if err := json.Unmarshal(data, &s); err != nil {
return err
}
_, err = os.Stat(savfile)
if err == nil {
_, err = os.Stat(savfile + ".bak")
if err != nil {
os.Rename(savfile, savfile+".bak")
}
}
return ioutil.WriteFile(savfile, s.Encode(), 0644)
}
func main() {
bin := comandante.New("ff6", "Manage Final Fantasy VI mobile save files")
bin.IncludeHelp()
tj := comandante.NewCommand("tojson", "Convert .sav save file to .json", tojson)
tj.FlagInit = func(fs *flag.FlagSet) {
fs.StringVar(&savfile, "i", savfile, "Input .sav file")
fs.StringVar(&jsonfile, "o", jsonfile, "Output .json file")
}
bin.RegisterCommand(tj)
fj := comandante.NewCommand("fromjson", "Convert .json save file to .sav", fromjson)
fj.FlagInit = func(fs *flag.FlagSet) {
fs.StringVar(&jsonfile, "i", jsonfile, "Input .json file")
fs.StringVar(&savfile, "o", savfile, "Output .sav file")
}
bin.RegisterCommand(fj)
flag.Parse()
if err := bin.Run(); err != nil {
log.Fatalln(err)
}
}