From 8a15c0b0741568a2b4b0d473b83de227bcfdb705 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Wed, 10 Feb 2021 08:24:53 +0100 Subject: [PATCH 1/3] hotfix: cof encoder: coding weapon class --- d2common/d2fileformats/d2cof/cof.go | 11 +++++++++-- d2common/d2fileformats/d2cof/cof_layer.go | 13 ++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/d2common/d2fileformats/d2cof/cof.go b/d2common/d2fileformats/d2cof/cof.go index 5b119b47..3b47c4a3 100644 --- a/d2common/d2fileformats/d2cof/cof.go +++ b/d2common/d2fileformats/d2cof/cof.go @@ -121,7 +121,6 @@ func (c *COF) Unmarshal(fileData []byte) error { layer.Transparent = b[layerTransparent] > 0 layer.DrawEffect = d2enum.DrawEffect(b[layerDrawEffect]) - layer.weaponClassByte = b[layerWeaponClass:] layer.WeaponClass = d2enum.WeaponClassFromString(strings.TrimSpace(strings.ReplaceAll( string(b[layerWeaponClass:]), badCharacter, ""))) @@ -193,7 +192,15 @@ func (c *COF) Marshal() []byte { sw.PushBytes(byte(c.CofLayers[i].DrawEffect)) - sw.PushBytes(c.CofLayers[i].weaponClassByte...) + s := c.CofLayers[i].WeaponClass.String() + + for j := 0; j < 4; j++ { + if j < len(s) { + sw.PushBytes(s[j]) + } else { + sw.PushBytes(0) + } + } } for _, i := range c.AnimationFrames { diff --git a/d2common/d2fileformats/d2cof/cof_layer.go b/d2common/d2fileformats/d2cof/cof_layer.go index 2658e691..8a09f9c1 100644 --- a/d2common/d2fileformats/d2cof/cof_layer.go +++ b/d2common/d2fileformats/d2cof/cof_layer.go @@ -4,11 +4,10 @@ import "github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum" // CofLayer is a structure that represents a single layer in a COF file. type CofLayer struct { - Type d2enum.CompositeType - Shadow byte - Selectable bool - Transparent bool - DrawEffect d2enum.DrawEffect - WeaponClass d2enum.WeaponClass - weaponClassByte []byte + Type d2enum.CompositeType + Shadow byte + Selectable bool + Transparent bool + DrawEffect d2enum.DrawEffect + WeaponClass d2enum.WeaponClass } From 10103530716f2a0293f990feca3358303327999f Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Wed, 10 Feb 2021 12:35:35 +0100 Subject: [PATCH 2/3] hotfix: d2cof encoder: removed magic number (len of weapon class) --- d2common/d2fileformats/d2cof/cof.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/d2common/d2fileformats/d2cof/cof.go b/d2common/d2fileformats/d2cof/cof.go index 3b47c4a3..fa9b1002 100644 --- a/d2common/d2fileformats/d2cof/cof.go +++ b/d2common/d2fileformats/d2cof/cof.go @@ -30,6 +30,10 @@ const ( layerWeaponClass ) +const ( + layerWeaponClassLength = 4 +) + const ( badCharacter = string(byte(0)) ) @@ -192,11 +196,11 @@ func (c *COF) Marshal() []byte { sw.PushBytes(byte(c.CofLayers[i].DrawEffect)) - s := c.CofLayers[i].WeaponClass.String() + weaponClassString := c.CofLayers[i].WeaponClass.String() - for j := 0; j < 4; j++ { - if j < len(s) { - sw.PushBytes(s[j]) + for letter := 0; letter < layerWeaponClassLength; letter++ { + if letter < len(weaponClassString) { + sw.PushBytes(weaponClassString[letter]) } else { sw.PushBytes(0) } From 7d0eeb0fd3a0bbb6a4cfda0cd05fc3bb9f537b0f Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Wed, 10 Feb 2021 12:59:07 +0100 Subject: [PATCH 3/3] hotfix: d2cof encoder: changed way of pushing weapon class --- d2common/d2fileformats/d2cof/cof.go | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/d2common/d2fileformats/d2cof/cof.go b/d2common/d2fileformats/d2cof/cof.go index fa9b1002..ceb9ab11 100644 --- a/d2common/d2fileformats/d2cof/cof.go +++ b/d2common/d2fileformats/d2cof/cof.go @@ -30,10 +30,6 @@ const ( layerWeaponClass ) -const ( - layerWeaponClassLength = 4 -) - const ( badCharacter = string(byte(0)) ) @@ -196,15 +192,22 @@ func (c *COF) Marshal() []byte { sw.PushBytes(byte(c.CofLayers[i].DrawEffect)) - weaponClassString := c.CofLayers[i].WeaponClass.String() + const ( + maxCodeLength = 3 // we assume item codes to look like 'hax' or 'kit' + terminator = 0 + ) - for letter := 0; letter < layerWeaponClassLength; letter++ { - if letter < len(weaponClassString) { - sw.PushBytes(weaponClassString[letter]) - } else { - sw.PushBytes(0) + weaponCode := c.CofLayers[i].WeaponClass.String() + + for idx, letter := range weaponCode { + if idx > maxCodeLength { + break } + + sw.PushBytes(byte(letter)) } + + sw.PushBytes(terminator) } for _, i := range c.AnimationFrames {