From ec9c0c3d95a199544f90fe646ba3a4708cb1a889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrkan=20Kaymak?= Date: Mon, 26 Oct 2020 16:13:08 +0300 Subject: [PATCH] fixes #767, implemented deepCopy for the dcc_animation and dc6_animation (#852) --- d2common/d2fileformats/d2dc6/dc6.go | 15 +++++++++++++++ d2common/d2fileformats/d2dcc/dcc.go | 21 ++++++++++++++++++--- d2core/d2asset/animation.go | 8 ++++++++ d2core/d2asset/dc6_animation.go | 8 ++++++-- d2core/d2asset/dcc_animation.go | 8 ++++++-- 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/d2common/d2fileformats/d2dc6/dc6.go b/d2common/d2fileformats/d2dc6/dc6.go index 237c4d06..f5a04749 100644 --- a/d2common/d2fileformats/d2dc6/dc6.go +++ b/d2common/d2fileformats/d2dc6/dc6.go @@ -124,3 +124,18 @@ func scanlineType(b int) scanlineState { return runOfOpaquePixels } + +// Clone creates a copy of the DC6 +func (d *DC6) Clone() *DC6 { + clone := *d + copy(clone.Termination, d.Termination) + copy(clone.FramePointers, d.FramePointers) + clone.Frames = make([]*DC6Frame, len(d.Frames)) + + for i := range d.Frames { + cloneFrame := *d.Frames[i] + clone.Frames = append(clone.Frames, &cloneFrame) + } + + return &clone +} diff --git a/d2common/d2fileformats/d2dcc/dcc.go b/d2common/d2fileformats/d2dcc/dcc.go index 36b524d6..ffd59e41 100644 --- a/d2common/d2fileformats/d2dcc/dcc.go +++ b/d2common/d2fileformats/d2dcc/dcc.go @@ -57,7 +57,22 @@ func Load(fileData []byte) (*DCC, error) { } // decodeDirection decodes and returns the given direction -func (dcc *DCC) decodeDirection(direction int) *DCCDirection { - return CreateDCCDirection(d2datautils.CreateBitMuncher(dcc.fileData, - dcc.directionOffsets[direction]*directionOffsetMultiplier), dcc) +func (d *DCC) decodeDirection(direction int) *DCCDirection { + return CreateDCCDirection(d2datautils.CreateBitMuncher(d.fileData, + d.directionOffsets[direction]*directionOffsetMultiplier), d) +} + +// Clone creates a copy of the DCC +func (d *DCC) Clone() *DCC { + clone := *d + copy(clone.directionOffsets, d.directionOffsets) + copy(clone.fileData, d.fileData) + clone.Directions = make([]*DCCDirection, len(d.Directions)) + + for i := range d.Directions { + cloneDirection := *d.Directions[i] + clone.Directions = append(clone.Directions, &cloneDirection) + } + + return &clone } diff --git a/d2core/d2asset/animation.go b/d2core/d2asset/animation.go index 2efc92c7..8f34507b 100644 --- a/d2core/d2asset/animation.go +++ b/d2core/d2asset/animation.go @@ -396,3 +396,11 @@ func (a *Animation) SetEffect(e d2enum.DrawEffect) { func (a *Animation) SetShadow(shadow bool) { a.hasShadow = shadow } + +// Clone creates a copy of the Animation +func (a *Animation) Clone() Animation { + clone := *a + copy(clone.directions, a.directions) + + return clone +} diff --git a/d2core/d2asset/dc6_animation.go b/d2core/d2asset/dc6_animation.go index bd793172..6db48ecf 100644 --- a/d2core/d2asset/dc6_animation.go +++ b/d2core/d2asset/dc6_animation.go @@ -188,6 +188,10 @@ func (a *DC6Animation) createFrameSurface(directionIndex, frameIndex int) (d2int // Clone creates a copy of the animation func (a *DC6Animation) Clone() d2interface.Animation { - animation := *a - return &animation + clone := &DC6Animation{} + clone.Animation = a.Animation.Clone() + clone.dc6 = a.dc6.Clone() + clone.palette = a.palette + + return clone } diff --git a/d2core/d2asset/dcc_animation.go b/d2core/d2asset/dcc_animation.go index 0b91943b..686932dc 100644 --- a/d2core/d2asset/dcc_animation.go +++ b/d2core/d2asset/dcc_animation.go @@ -72,8 +72,12 @@ func (a *DCCAnimation) init() error { // Clone creates a copy of the animation func (a *DCCAnimation) Clone() d2interface.Animation { - animation := *a - return &animation + clone := &DCCAnimation{} + clone.Animation = a.Animation.Clone() + clone.dcc = a.dcc.Clone() + clone.palette = a.palette + + return clone } // SetDirection places the animation in the direction of an animation