Skip to content

Commit

Permalink
Merge pull request #42 from gucio321/hotkeys
Browse files Browse the repository at this point in the history
`d2s`: extract hotkeys structure from d2s.go; move it into d2shotkeys…
  • Loading branch information
gucio321 committed May 26, 2021
2 parents 108f5bb + c151d7a commit 505a066
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 20 deletions.
33 changes: 13 additions & 20 deletions d2s.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/gucio321/d2d2s/d2scorpse"
"github.com/gucio321/d2d2s/d2sdifficulty"
"github.com/gucio321/d2d2s/d2senums"
"github.com/gucio321/d2d2s/d2shotkeys"
"github.com/gucio321/d2d2s/d2sirongolem"
"github.com/gucio321/d2d2s/d2sitems"
"github.com/gucio321/d2d2s/d2smercenary"
Expand All @@ -29,7 +30,6 @@ import (
const (
saveFileSignature = 0xaa55aa55
characterNameSize = 16
skillHotKeys = 16
unknown6BytesCount = 32
unknown8BytesCount = 144
int32Size = 4
Expand All @@ -38,8 +38,6 @@ const (
checksumPosition = 12
)

type hotkeys map[byte]d2senums.SkillID

// D2S represents a Diablo II character save file structure
type D2S struct {
Version d2senums.Version
Expand All @@ -54,7 +52,7 @@ type D2S struct {
unknown4 uint32
Time uint32
unknown5 uint32
Hotkeys hotkeys
Hotkeys *d2shotkeys.Hotkeys
LeftSkill,
RightSkill,
LeftSkillSwitch,
Expand Down Expand Up @@ -83,7 +81,7 @@ func New() *D2S {
Version: d2senums.VersionLODLatest,
Status: d2sstatus.New(),
Progression: d2sprogression.New(),
Hotkeys: make(hotkeys),
Hotkeys: d2shotkeys.New(),
Difficulty: d2sdifficulty.New(),
Mercenary: d2smercenary.New(),
Quests: d2squests.New(),
Expand Down Expand Up @@ -124,7 +122,14 @@ func Load(data []byte) (*D2S, error) {
result.unknown4 = sr.GetUInt32()
result.Time = sr.GetUInt32()
result.unknown5 = sr.GetUInt32()
result.loadHotkeys(sr)

hd := sr.GetBytes(d2shotkeys.NumHotkeysBytes)

var hotkeysData [d2shotkeys.NumHotkeysBytes]byte

copy(hotkeysData[:], hd[:d2shotkeys.NumHotkeysBytes])

result.Hotkeys.Load(hotkeysData)

lsk := sr.GetUInt32()
result.LeftSkill = d2senums.SkillID(lsk)
Expand Down Expand Up @@ -264,13 +269,6 @@ func (d *D2S) loadHeader(sr *datautils.BitMuncher) error {
return nil
}

func (d *D2S) loadHotkeys(sr *datautils.BitMuncher) {
for i := byte(0); i < skillHotKeys; i++ {
id := sr.GetUInt32()
d.Hotkeys[i] = d2senums.SkillID(id)
}
}

// Encode encodes character save back into a byte slice (WIP)
func (d *D2S) Encode() ([]byte, error) {
sw := d2datautils.CreateStreamWriter()
Expand All @@ -289,7 +287,8 @@ func (d *D2S) Encode() ([]byte, error) {
sw.PushUint32(d.Time)
sw.PushUint32(d.unknown5)

d.encodeHotkeys(sw)
hd := d.Hotkeys.Encode()
sw.PushBytes(hd[:]...)

sw.PushUint32(uint32(d.LeftSkill))
sw.PushUint32(uint32(d.RightSkill))
Expand Down Expand Up @@ -374,12 +373,6 @@ func (d *D2S) encodeHeader(sw *d2datautils.StreamWriter) error {
return nil
}

func (d *D2S) encodeHotkeys(sw *d2datautils.StreamWriter) {
for i := byte(0); i < skillHotKeys; i++ {
sw.PushUint32(uint32(d.Hotkeys[i]))
}
}

// CalculateChecksum calculates a checksum and saves in a byte slice
func CalculateChecksum(data *[]byte) {
var sum uint32
Expand Down
2 changes: 2 additions & 0 deletions d2shotkeys/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package d2shotkeys stores skill hotkeys
package d2shotkeys
48 changes: 48 additions & 0 deletions d2shotkeys/hotkeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package d2shotkeys

import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2datautils"

"github.com/gucio321/d2d2s/d2senums"
)

const (
// NumHotkeysBytes is a len of hotkey's binary form
NumHotkeysBytes = 64

numHotKeys = 16
)

// Hotkeys represents a skill hotkeys data
type Hotkeys map[byte]d2senums.SkillID

// New creates a new Hotkeys
func New() *Hotkeys {
result := &Hotkeys{}
*result = make(Hotkeys)

return result
}

// Load loads hotkeys
func (h *Hotkeys) Load(data [NumHotkeysBytes]byte) {
sr := d2datautils.CreateBitMuncher(data[:], 0)
for i := byte(0); i < numHotKeys; i++ {
id := sr.GetUInt32()
(*h)[i] = d2senums.SkillID(id)
}
}

// Encode encodes hotkeys back into binary form
func (h *Hotkeys) Encode() (result [NumHotkeysBytes]byte) {
sw := d2datautils.CreateStreamWriter()

for i := byte(0); i < numHotKeys; i++ {
sw.PushUint32(uint32((*h)[i]))
}

data := sw.GetBytes()
copy(result[:], data[:NumHotkeysBytes])

return result
}

0 comments on commit 505a066

Please sign in to comment.