Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spine::Animation & spine::Timeline can not release normally #17653

Open
wants to merge 9 commits into
base: v3.8.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cocos/spine/lib/spine-core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
duration: number;
name: string;
timelines: Array<Timeline>;
apply(skeleton: Skeleton, lastTime: number, time: number, loop: boolean, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 50 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 166. Maximum allowed is 150
hasTimeline(id: number): boolean;
}
interface Timeline {
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 54 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
getPropertyId(): number;
}
enum MixBlend {
Expand Down Expand Up @@ -94,7 +94,7 @@
getCurveType(frameIndex: number): number;
setCurve(frameIndex: number, cx1: number, cy1: number, cx2: number, cy2: number): void;
getCurvePercent(frameIndex: number, percent: number): number;
abstract apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 97 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 160. Maximum allowed is 150
}
class RotateTimeline extends CurveTimeline {
static ENTRIES: number;
Expand All @@ -106,7 +106,7 @@
constructor(frameCount: number);
getPropertyId(): number;
setFrame(frameIndex: number, time: number, degrees: number): void;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 109 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class TranslateTimeline extends CurveTimeline {
static readonly ENTRIES: number;
Expand All @@ -114,17 +114,17 @@
constructor(frameCount: number);
getPropertyId(): number;
setFrame(frameIndex: number, time: number, x: number, y: number): void;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 117 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class ScaleTimeline extends TranslateTimeline {
constructor(frameCount: number);
getPropertyId(): number;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 122 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class ShearTimeline extends TranslateTimeline {
constructor(frameCount: number);
getPropertyId(): number;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 127 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class ColorTimeline extends CurveTimeline {
static ENTRIES: number;
Expand All @@ -135,7 +135,7 @@
getSlotIndex(): number;
setSlotIndex(inValue: number): void;
setFrame(frameIndex: number, time: number, r: number, g: number, b: number, a: number): void;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 138 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class TwoColorTimeline extends CurveTimeline {
static readonly ENTRIES: number;
Expand All @@ -146,7 +146,7 @@
getSlotIndex(): number;
setSlotIndex(inValue: number): void;
setFrame(frameIndex: number, time: number, r: number, g: number, b: number, a: number, r2: number, g2: number, b2: number): void;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 149 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class AttachmentTimeline implements Timeline {
slotIndex: number;
Expand All @@ -159,7 +159,7 @@
setSlotIndex(inValue: number): void;
getAttachmentNames(): Array<string>;
setFrame(frameIndex: number, time: number, attachmentName: string): void;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 162 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class DeformTimeline extends CurveTimeline {
slotIndex: number;
Expand All @@ -168,7 +168,9 @@
frameVertices: Array<ArrayLike<number>>;
constructor(frameCount: number);
getPropertyId(): number;
setFrame(frameIndex: number, time: number, vertices: ArrayLike<number>): void;
setSlotIndex(index: number);
setAttachment(attachment: Attachment);
setFrame(frameIndex: number, time: number, vertices: []): void;
apply(skeleton: Skeleton, lastTime: number, time: number, firedEvents: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;
}
class EventTimeline implements Timeline {
Expand Down Expand Up @@ -781,7 +783,7 @@
MipMapNearestNearest = 9984,
MipMapLinearNearest = 9985,
MipMapNearestLinear = 9986,
MipMapLinearLinear = 9987

Check failure on line 786 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Duplicate enum member value 9987
}
enum TextureWrap {
MirroredRepeat = 33648,
Expand Down
21 changes: 17 additions & 4 deletions cocos/spine/lib/spine-define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ import spine from './spine-core.js';
import { js } from '../../core';

function resizeArray (array, newSize): Array<any> {
if (!array) return new Array(newSize);
if (newSize === array.length) return array;
if (newSize < array.length) return array.slice(0, newSize);
else return new Array(newSize);
if (!array) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return new Array(newSize);
}
if (newSize === array.length) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return array;
}
if (newSize < array.length) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return array.slice(0, newSize);
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return new Array(newSize);
}
}

function overrideDefineArrayProp (prototype: any, getPropVector: any, name: string): void {
Expand All @@ -44,6 +55,7 @@ function overrideDefineArrayProp (prototype: any, getPropVector: any, name: stri
array = resizeArray(array, count);
for (let i = 0; i < count; i++) array[i] = vectors.get(i);
this[_name] = array;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return array;
},
});
Expand Down Expand Up @@ -82,6 +94,7 @@ function overrideDefineArrayFunction (prototype: any, getPropVector: any, name:
array = resizeArray(array, count);
for (let i = 0; i < count; i++) array[i] = vectors.get(i);
this[_name] = array;
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return array;
},
});
Expand Down
4 changes: 4 additions & 0 deletions native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2389,6 +2389,10 @@ if(USE_MIDDLEWARE)
cocos/editor-support/spine-creator-support/SkeletonDataMgr.h
NO_WERROR NO_UBUILD cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp
cocos/editor-support/spine-creator-support/SkeletonRenderer.h
NO_WERROR cocos/editor-support/spine-creator-support/SpineAnimation.cpp
cocos/editor-support/spine-creator-support/SpineAnimation.h
NO_WERROR cocos/editor-support/spine-creator-support/SpineAnimationState.cpp
cocos/editor-support/spine-creator-support/SpineAnimationState.h
NO_WERROR cocos/editor-support/spine-creator-support/spine-cocos2dx.cpp
cocos/editor-support/spine-creator-support/spine-cocos2dx.h
NO_WERROR cocos/editor-support/spine-creator-support/VertexEffectDelegate.cpp
Expand Down
3 changes: 3 additions & 0 deletions native/cocos/bindings/manual/jsb_conversions_spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,9 @@ bool sevalue_to_native(const se::Value &from, cc::IBArray *to, se::Object * /*ct

// NOLINTNEXTLINE(readability-identifier-naming)
bool sevalue_to_native(const se::Value &val, spine::String *obj, se::Object * /*unused*/) {
if (!val.isString()) {
return false;
}
*obj = val.toString().data();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*****************************************************************************/

#include "spine-creator-support/SkeletonAnimation.h"
#include "SpineAnimationState.h"

#include <algorithm>
#include "base/DeferredReleasePool.h"
#include "base/Log.h"
Expand Down Expand Up @@ -99,7 +101,7 @@ void SkeletonAnimation::initialize() {
super::initialize();

_ownsAnimationStateData = true;
_state = new (__FILE__, __LINE__) AnimationState(new (__FILE__, __LINE__) AnimationStateData(_skeleton->getData()));
_state = new (__FILE__, __LINE__) SpineAnimationState(new (__FILE__, __LINE__) AnimationStateData(_skeleton->getData()));
_state->setRendererObject(this);
_state->setListener(animationCallback);
}
Expand Down Expand Up @@ -140,7 +142,7 @@ void SkeletonAnimation::setAnimationStateData(AnimationStateData *stateData) {
}

_ownsAnimationStateData = false;
_state = new (__FILE__, __LINE__) AnimationState(stateData);
_state = new (__FILE__, __LINE__) SpineAnimationState(stateData);
_state->setRendererObject(this);
_state->setListener(animationCallback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ SkeletonCache *SkeletonCacheMgr::buildSkeletonCache(const std::string &uuid) {
void SkeletonCacheMgr::removeSkeletonCache(const std::string &uuid) {
auto it = _caches.find(uuid);
if (it != _caches.end()) {
auto *item = it->second;
delete item;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

item->release();

_caches.erase(it);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include "SpineAnimation.h"

using namespace spine;

spine::Vector<Timeline*>& convertAndUseVector(const std::vector<std::shared_ptr<Timeline>>& stdVec, spine::Vector<Timeline*>& _spineVecTimelines) {
for (const auto& element : stdVec) {
_spineVecTimelines.add(element.get());
}

return _spineVecTimelines;
}

SpineAnimation::SpineAnimation(const String& name, std::vector<std::shared_ptr<Timeline>> timelines, float duration) : Animation(name, convertAndUseVector(timelines, _spineVecTimelines), duration) {
_vecTimelines = timelines;
}

SpineAnimation::~SpineAnimation() {
_vecTimelines.clear();
_spineVecTimelines.clear();
auto& timelines = getTimelines();
timelines.clear();
}
20 changes: 20 additions & 0 deletions native/cocos/editor-support/spine-creator-support/SpineAnimation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <spine/Animation.h>
#include <vector>
#include <memory>

namespace spine {

class SpineAnimation : public Animation {
public:
SpineAnimation(const String &name, std::vector<std::shared_ptr<Timeline>> timelines, float duration);

~SpineAnimation();

private:
std::vector<std::shared_ptr<Timeline>> _vecTimelines;
spine::Vector<Timeline*> _spineVecTimelines;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why need to use two different containers at the same time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spine::Animation's constructor requires spine::Vector<Timeline*>, we need to change from std::vector to spine::Vector

};

} // namespace spine
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

#include "SpineAnimationState.h"

using namespace spine;

SpineAnimationState::SpineAnimationState(AnimationStateData* data) : AnimationState(data) {

}

SpineAnimationState::~SpineAnimationState() {
_vecAnimations.clear();
}

TrackEntry* SpineAnimationState::addAnimation(size_t trackIndex, std::shared_ptr<Animation> animation, bool loop, float delay) {
_vecAnimations.push_back(animation);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use emplace_back instead.

return AnimationState::addAnimation(trackIndex, animation.get(), loop, delay);
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a new file we created, it's not a file in spine runtime , so I think we need to use our license.

*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/

#pragma once

#include <spine/AnimationState.h>
#include "spine/Animation.h"
#include<vector>
#include<memory>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a space before <


namespace spine {


class SP_API SpineAnimationState : public AnimationState {

public:
explicit SpineAnimationState(AnimationStateData* data);

~SpineAnimationState();

TrackEntry* addAnimation(size_t trackIndex, std::shared_ptr<Animation> animation, bool loop, float delay);

private:
std::vector<std::shared_ptr<Animation>> _vecAnimations;
};
} // namespace spine

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "spine-creator-support/SkeletonCacheMgr.h"
#include "spine-creator-support/SkeletonDataMgr.h"
#include "spine-creator-support/SkeletonRenderer.h"
#include "spine-creator-support/SpineAnimation.h"
#include "spine-creator-support/SpineAnimationState.h"
#include "spine/spine.h"

namespace spine {
Expand Down
18 changes: 18 additions & 0 deletions native/cocos/editor-support/spine-wasm/SpineAnimationState.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#include "SpineAnimationState.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing licenese.


using namespace spine;

SpineAnimationState::SpineAnimationState(AnimationStateData* data) : AnimationState(data) {
}

SpineAnimationState::~SpineAnimationState() {
_mapAnimations.clear();
}

TrackEntry* SpineAnimationState::addAnimation(size_t trackIndex, Animation* animation, bool loop, float delay) {
if (_mapAnimations.count(animation) == 0) {
_mapAnimations[animation] = std::shared_ptr<Animation>(animation);
}
return AnimationState::addAnimation(trackIndex, animation, loop, delay);
}
21 changes: 21 additions & 0 deletions native/cocos/editor-support/spine-wasm/SpineAnimationState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing licenese.


#include <spine/AnimationState.h>
#include <map>
#include <memory>
#include "spine/Animation.h"

namespace spine {

class SP_API SpineAnimationState : public AnimationState {
public:
explicit SpineAnimationState(AnimationStateData* data);

~SpineAnimationState();

TrackEntry* addAnimation(size_t trackIndex, Animation* animation, bool loop, float delay);

private:
std::map<Animation*, std::shared_ptr<Animation>> _mapAnimations;
};
} // namespace spine
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "spine-mesh-data.h"
#include "spine-wasm.h"
#include "util-function.h"
#include "../spine-creator-support/SpineAnimationState.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use '../' for include path is not a good way.


SlotMesh globalMesh(nullptr, nullptr, 0, 0);

Expand Down Expand Up @@ -57,7 +58,7 @@ Skeleton *SpineSkeletonInstance::initSkeleton(SkeletonData *data) {
_skeletonData = data;
_skeleton = new Skeleton(_skeletonData);
_animStateData = new AnimationStateData(_skeletonData);
_animState = new AnimationState(_animStateData);
_animState = new SpineAnimationState(_animStateData);
_clipper = new SkeletonClipping();
_skeleton->setToSetupPose();
_skeleton->updateWorldTransform();
Expand Down
Loading
Loading