-
Notifications
You must be signed in to change notification settings - Fork 1
/
d2s.go
390 lines (293 loc) · 9.51 KB
/
d2s.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package d2d2s
import (
"errors"
"fmt"
"log"
"strings"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2datautils"
"github.com/gucio321/d2d2s/d2scorpse"
"github.com/gucio321/d2d2s/d2sdifficulty"
"github.com/gucio321/d2d2s/d2senums"
"github.com/gucio321/d2d2s/d2sirongolem"
"github.com/gucio321/d2d2s/d2sitems"
"github.com/gucio321/d2d2s/d2smercenary"
"github.com/gucio321/d2d2s/d2snpc"
"github.com/gucio321/d2d2s/d2squests"
"github.com/gucio321/d2d2s/d2sstats"
"github.com/gucio321/d2d2s/d2sstatus"
"github.com/gucio321/d2d2s/d2swaypoints"
"github.com/gucio321/d2d2s/datautils"
)
const (
saveFileSignature = 0xaa55aa55
characterNameSize = 16
skillHotKeys = 16
unknown6BytesCount = 32
unknown8BytesCount = 144
skillsHeaderID = "if"
numSkills = 30
int32Size = 4
fileSizePosition = 8
checksumPosition = 12
)
type hotkeys map[byte]d2senums.SkillID
// D2S represents a Diablo II character save file structure
type D2S struct {
Version d2senums.Version
unknown1 uint32
Name string
Status *d2sstatus.Status
Progression byte
unknown2 uint16
Class d2senums.CharacterClass
unknown3 uint16
Level byte
unknown4 uint32
Time uint32
unknown5 uint32
Hotkeys hotkeys
LeftSkill,
RightSkill,
LeftSkillSwitch,
RightSkillSwitch d2senums.SkillID
unknown6 [unknown6BytesCount]byte // probably character apperence in char select menu
Difficulty *d2sdifficulty.Difficulty
MapID uint32
unknown7 uint16
Mercenary *d2smercenary.Mercenary
unknown8 [unknown8BytesCount]byte
Quests *d2squests.Quests
Waypoints *d2swaypoints.Waypoints
NPC *d2snpc.NPC
Stats *d2sstats.Stats
Skills [numSkills]d2senums.SkillID
Items *d2sitems.Items
Corpse *d2scorpse.Corpse
// necromancer only
IronGolem *d2sirongolem.IronGolem
}
// New creates a new D2S structure
func New() *D2S {
result := &D2S{
Status: d2sstatus.New(),
Hotkeys: make(hotkeys),
Difficulty: d2sdifficulty.New(),
Mercenary: d2smercenary.New(),
Quests: d2squests.New(),
Waypoints: d2swaypoints.New(),
NPC: d2snpc.New(),
Stats: d2sstats.New(),
Items: &d2sitems.Items{},
Corpse: d2scorpse.New(),
IronGolem: d2sirongolem.New(),
}
return result
}
// Unmarshal loads d2s file into D2S structure
// nolint:funlen,gocyclo // probably inpossible to reduce, but TODO
func Unmarshal(data []byte) (*D2S, error) {
var err error
result := New()
sr := datautils.CreateBitMuncher(data, 0)
if signature := sr.GetUInt32(); signature != saveFileSignature {
return nil, errors.New("unexpected file signature")
}
v := sr.GetUInt32()
version := d2senums.Version(v)
if version != d2senums.VersionLODLatest {
log.Printf("Warning! wrong version %s. It might be unsupported", version.String())
}
result.Version = version
// file size in bytes ( len(data) )
_ = sr.GetInt32()
// checksum (32-bit checksum)
sum := sr.GetUInt32()
fmt.Println(sum)
result.unknown1 = sr.GetUInt32()
name := sr.GetBytes(characterNameSize)
result.Name = strings.ReplaceAll(string(name), string(rune(0)), "")
status := sr.GetByte()
result.Status.Unmarshal(status)
/*
from: https://user.xmission.com/~trevin/DiabloIIv1.09_File_Format.shtml
Character progression. This number tells (sort of) how many acts you have completed from all
difficulty levels. It appears to be incremented when you kill the final demon in an act -- i.e.,
Andarial, Duriel, Mephisto, and Diablo / Baal. There's a catch to that last one: in an
Expansion game, the value is not incremented after killing Diablo, but is incremented by 2 after killing Baal.
(The reason is unknown.) So it skips the values 4, 9, and 14.
*/
result.Progression = sr.GetByte()
result.unknown2 = sr.GetUInt16()
class := sr.GetByte()
result.Class = d2senums.CharacterClass(class)
result.unknown3 = sr.GetUInt16()
result.Level = sr.GetByte()
result.unknown4 = sr.GetUInt32()
result.Time = sr.GetUInt32()
result.unknown5 = sr.GetUInt32()
for i := byte(0); i < skillHotKeys; i++ {
id := sr.GetUInt32()
result.Hotkeys[i] = d2senums.SkillID(id)
}
lsk := sr.GetUInt32()
result.LeftSkill = d2senums.SkillID(lsk)
rsk := sr.GetUInt32()
result.RightSkill = d2senums.SkillID(rsk)
if result.Status.Expansion {
alsk := sr.GetUInt32()
result.LeftSkillSwitch = d2senums.SkillID(alsk)
arsk := sr.GetUInt32()
result.RightSkillSwitch = d2senums.SkillID(arsk)
}
unknown6 := sr.GetBytes(unknown6BytesCount)
copy(result.unknown6[:], unknown6[:unknown6BytesCount])
result.Difficulty.Load(sr)
result.MapID = sr.GetUInt32()
result.unknown7 = sr.GetUInt16()
result.Mercenary.Died = sr.GetUInt16()
result.Mercenary.ID = sr.GetUInt32()
result.Mercenary.Name = sr.GetUInt16()
mercType := sr.GetUInt16()
result.Mercenary.LoadType(mercType)
result.Mercenary.Experience = sr.GetUInt32()
unknown8 := sr.GetBytes(unknown8BytesCount)
copy(result.unknown8[:], unknown8[:unknown8BytesCount])
qd := sr.GetBytes(d2squests.NumQuestsBytes)
var questsData [d2squests.NumQuestsBytes]byte
copy(questsData[:], qd[:d2squests.NumQuestsBytes])
err = result.Quests.Unmarshal(&questsData)
if err != nil {
return nil, fmt.Errorf("error loading quests: %w", err)
}
wd := sr.GetBytes(d2swaypoints.NumWaypointsBytes)
var waypointsData [d2swaypoints.NumWaypointsBytes]byte
copy(waypointsData[:], wd[:d2swaypoints.NumWaypointsBytes])
if err := result.Waypoints.Load(&waypointsData); err != nil { // nolint:govet // it is ok
return nil, fmt.Errorf("error loading waypoints data: %w", err)
}
nd := sr.GetBytes(d2snpc.NumNPCBytes)
var npcData [d2snpc.NumNPCBytes]byte
copy(npcData[:], nd[:d2snpc.NumNPCBytes])
if err := result.NPC.Load(npcData); err != nil { // nolint:govet // it is og
return nil, fmt.Errorf("error loading npcs data: %w", err)
}
if err := result.Stats.Load(sr); err != nil { // nolint:govet // it is ok
return nil, fmt.Errorf("error loading character stats: %w", err)
}
skillsID := sr.GetBytes(2) // nolint:gomnd // skills header
if string(skillsID) != skillsHeaderID {
return nil, errors.New("unexpected skills section header")
}
for i := 0; i < numSkills; i++ {
id := sr.GetByte()
result.Skills[i] = d2senums.SkillID(id)
}
numItems, err := result.Items.LoadHeader(sr)
if err != nil {
return nil, fmt.Errorf("error loading items header: %w", err)
}
if err := result.Items.LoadList(sr, numItems); err != nil {
return nil, fmt.Errorf("error loading items list: %w", err)
}
// thanks to @nokka <https://github.com/nokka/d2s> for figuring out these fields!
if err := result.Corpse.Load(sr); err != nil {
return nil, fmt.Errorf("error loading corpse: %w", err)
}
if result.Status.Expansion {
if err := result.Mercenary.LoadMercItems(sr); err != nil {
return nil, fmt.Errorf("error loading merc items: %w", err)
}
}
// iron golem for necromancer
if result.Class == d2senums.CharacterClassNecromancer && result.Status.Expansion {
if err := result.IronGolem.Load(sr); err != nil {
return nil, fmt.Errorf("error loading iron golem: %w", err)
}
}
return result, nil
}
// Encode encodes character save back into a byte slice (WIP)
// nolint:funlen // I suppose, it is unable to reduce, but TODO
func (d *D2S) Encode() ([]byte, error) {
sw := d2datautils.CreateStreamWriter()
sw.PushUint32(saveFileSignature)
sw.PushUint32(uint32(d.Version))
// file size, 0 for now
sw.PushUint32(0)
// checksum - 0 for now
sw.PushUint32(0)
name := []byte(d.Name)
if len(name) > characterNameSize {
return nil, errors.New("wrong character name! (len(name) > 16)")
}
sw.PushUint32(d.unknown1)
sw.PushBytes(name...)
for i := 0; i < characterNameSize-len(name); i++ {
sw.PushBytes(0)
}
sw.PushBytes(d.Status.Encode())
sw.PushBytes(d.Progression)
sw.PushUint16(d.unknown2)
sw.PushBytes(byte(d.Class))
sw.PushUint16(d.unknown3)
sw.PushBytes(d.Level)
sw.PushUint32(d.unknown4)
sw.PushUint32(d.Time)
sw.PushUint32(d.unknown5)
for i := byte(0); i < skillHotKeys; i++ {
sw.PushUint32(uint32(d.Hotkeys[i]))
}
sw.PushUint32(uint32(d.LeftSkill))
sw.PushUint32(uint32(d.RightSkill))
if d.Status.Expansion {
sw.PushUint32(uint32(d.LeftSkillSwitch))
sw.PushUint32(uint32(d.RightSkillSwitch))
}
sw.PushBytes(d.unknown6[:]...)
d.Difficulty.Encode(sw)
sw.PushUint32(d.MapID)
sw.PushUint16(d.unknown7)
// merc
sw.PushUint16(d.Mercenary.Died)
sw.PushUint32(d.Mercenary.ID)
sw.PushUint16(d.Mercenary.Name)
sw.PushUint16(d.Mercenary.EncodeType())
sw.PushUint32(d.Mercenary.Experience)
sw.PushBytes(d.unknown8[:]...)
qd := d.Quests.Encode()
sw.PushBytes(qd[:]...)
wd := d.Waypoints.Encode()
sw.PushBytes(wd[:]...)
nd := d.NPC.Encode()
sw.PushBytes(nd[:]...)
sw.PushBytes(d.Stats.Encode()...)
// skills section
sw.PushBytes([]byte(skillsHeaderID)...)
for i := 0; i < numSkills; i++ {
sw.PushBytes(byte(d.Skills[i]))
}
sw.PushBytes(d.Items.Encode()...)
if err := d.Corpse.Encode(sw); err != nil {
return nil, fmt.Errorf("error encoding corpse: %w", err)
}
d.Mercenary.EncodeItems(sw)
if d.Class == d2senums.CharacterClassNecromancer && d.Status.Expansion {
d.IronGolem.Encode(sw)
}
// we need to write file size and checksum here:
data := sw.GetBytes()
fileSize := uint32(len(data))
for i := 0; i < int32Size; i++ {
data[fileSizePosition+i] = byte(fileSize >> i * 8) // nolint:gomnd // byte size
}
// checksum here - TODO
var sum uint32
for i := range data {
sum <<= 1
sum += uint32(data[i])
}
for i := 0; i < int32Size; i++ {
data[checksumPosition+i] = byte(sum >> i * 8) // nolint:gomnd // byte size
}
return data, nil
}