From 9f482bbb85644f6dd23c9ee33aceee0273a9c185 Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 26 May 2021 11:15:45 +0200 Subject: [PATCH] `d2s`: extract hotkeys structure from d2s.go; move it into d2shotkeys (#8) --- d2s.go | 33 ++++++++++++----------------- d2shotkeys/doc.go | 2 ++ d2shotkeys/hotkeys.go | 48 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 d2shotkeys/doc.go create mode 100644 d2shotkeys/hotkeys.go diff --git a/d2s.go b/d2s.go index f6f829f..797b84c 100644 --- a/d2s.go +++ b/d2s.go @@ -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" @@ -28,7 +29,6 @@ import ( const ( saveFileSignature = 0xaa55aa55 characterNameSize = 16 - skillHotKeys = 16 unknown6BytesCount = 32 unknown8BytesCount = 144 int32Size = 4 @@ -37,8 +37,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 @@ -53,7 +51,7 @@ type D2S struct { unknown4 uint32 Time uint32 unknown5 uint32 - Hotkeys hotkeys + Hotkeys *d2shotkeys.Hotkeys LeftSkill, RightSkill, LeftSkillSwitch, @@ -81,7 +79,7 @@ func New() *D2S { result := &D2S{ Version: d2senums.VersionLODLatest, Status: d2sstatus.New(), - Hotkeys: make(hotkeys), + Hotkeys: d2shotkeys.New(), Difficulty: d2sdifficulty.New(), Mercenary: d2smercenary.New(), Quests: d2squests.New(), @@ -130,7 +128,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) @@ -270,13 +275,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() @@ -295,7 +293,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)) @@ -380,12 +379,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 diff --git a/d2shotkeys/doc.go b/d2shotkeys/doc.go new file mode 100644 index 0000000..92a3f88 --- /dev/null +++ b/d2shotkeys/doc.go @@ -0,0 +1,2 @@ +// Package d2shotkeys stores skill hotkeys +package d2shotkeys diff --git a/d2shotkeys/hotkeys.go b/d2shotkeys/hotkeys.go new file mode 100644 index 0000000..ec9601a --- /dev/null +++ b/d2shotkeys/hotkeys.go @@ -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 +}