Skip to content

Fix spine callback issues#18672

Merged
dumganhar merged 6 commits intococos:v3.8.7from
bofeng-song:387_spine_trackListener
Apr 28, 2025
Merged

Fix spine callback issues#18672
dumganhar merged 6 commits intococos:v3.8.7from
bofeng-song:387_spine_trackListener

Conversation

@bofeng-song
Copy link
Copy Markdown
Contributor

@bofeng-song bofeng-song commented Apr 27, 2025

  1. Callbacks set via setTrackEndListener are not triggered.
  2. The setEndListener callback interface cannot retrieve the animation name through the trackEntry parameter.

Re: #
#18610
cocos/cocos-test-projects#920
cocos/cocos-engine-external#488

Changelog


Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

1. Callbacks set via setTrackEndListener are not triggered.
2. The setEndListener callback interface cannot retrieve the animation name through the trackEntry parameter.
@bofeng-song bofeng-song requested a review from dumganhar April 27, 2025 03:53
for (int i = 0; i < vecTrackEvents.size(); i++) {
auto& info = vecTrackEvents[i];
onTrackEntryEvent(info.entry, info.eventType, info.event);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Optimize the code by checking whether the original vector is emtpy to avoid creating an empty vector and copying the empty vector.

    if (animationEvents.size() > 0) {
        // Cache animation events then call back to JS.
        auto vecAnimationEvents = animationEvents;
        animationEvents.clear();
        for (int i = 0; i < vecAnimationEvents.size(); i++) {
            auto& info = vecAnimationEvents[i];
            onAnimationStateEvent(info.entry, info.eventType, info.event);
        }
    }

    if (trackEvents.size() > 0) {
        // Cache track events then call back to JS.
        auto vecTrackEvents = trackEvents;
        trackEvents.clear();
        for (int i = 0; i < vecTrackEvents.size(); i++) {
            auto& info = vecTrackEvents[i];
            onTrackEntryEvent(info.entry, info.eventType, info.event);
        }
    }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please notice that spine::Vector doesn't provide a operator= implementation, see
https://github.com/cocos/cocos-engine/blob/v3.8.7/native/cocos/editor-support/spine/3.8/spine/Vector.h#L231

So the copy assignment will use a default one which only assigns the buffer pointer and size. If so, we need to fix that for spine::Vector.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please notice that spine::Vector doesn't provide a operator= implementation

It seems to be fine that the assignment auto vecAnimationEvents = animationEvents will trigger the spine::Vector copy constructor.

https://github.com/cocos/cocos-engine/blob/v3.8.7/native/cocos/editor-support/spine/3.8/spine/Vector.h#L49

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wrote a test case that could reproduce the issue I said, but yes, it's not relevant to this PR.

    {
        spine::Vector<int> arr;
        arr.add(1);
        arr.add(2);
        arr.add(3);
        
        spine::Vector<int> arr2;
        arr2 = arr; // --> call the default assignment operator
        
        arr2[0] = 100;
        
        printf("arr --------\n");
        for (size_t i = 0; i < arr.size(); ++i) {
            printf("%d, ", arr[i]);
        }
        printf("\n");
        
        printf("arr2 --------\n");
        for (size_t i = 0; i < arr2.size(); ++i) {
            printf("%d, ", arr2[i]);
        }
        printf("\n");
        printf("\n");
    }

The output is wrong:

arr --------
100, 2, 3, 
arr2 --------
100, 2, 3, 

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I sent a PR to spine runtime official repo ( EsotericSoftware/spine-runtimes#2828 ) to fix the copy assignment issue.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Currently, the assignment type of 'operator =' is not used by spine's module

* In the official implementation of Spine's AnimationState class, the animationCallback is invoked after the trackEntryCallback.
* After the AnimationState completes the EventType_Dispose callback, the TrackEntry will be reclaimed, so this event must be dispatched immediately.
*/
skeletonAnimation->dispatchEvents();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could this cause other issues if something is done in the dispose callback?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

8f12cbe6-f0c2-45d9-9c17-1f68f1267569
I Think that EventType_End disposeTrackEntry as same as EventType_Dispose.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

EventType_end event is cached by cacheAnimationEvent, while EventType_Dispose is dispatched it will dispatch EventType_end firstly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Could this cause other issues if something is done in the dispose callback?

trackEntry will be enter _trackEntryPool only after EventType_dispose is dispatched. So, it should not cause other issues.

@dumganhar
Copy link
Copy Markdown
Contributor

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 28, 2025

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1005779 bytes 1005779 bytes ✅ 0 bytes
2D All (legacy pipeline) 2669466 bytes 2669443 bytes 👍 -23 bytes
2D All (new pipeline) 2757170 bytes 2757147 bytes 👍 -23 bytes
(2D + 3D) All 10013054 bytes 10013031 bytes 👍 -23 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16929528 bytes 16921061 bytes 👍 -8467 bytes

Interface Check Report

This pull request does not change any public interfaces !

@bofeng-song bofeng-song reopened this Apr 28, 2025
@dumganhar dumganhar merged commit 7ead7e4 into cocos:v3.8.7 Apr 28, 2025
40 of 67 checks passed
@bofeng-song bofeng-song deleted the 387_spine_trackListener branch May 19, 2025 09:18
AILHC pushed a commit to AILHC/cocos-engine that referenced this pull request Jun 11, 2025
* Fix spine issues:
1. Callbacks set via setTrackEndListener are not triggered.
2. The setEndListener callback interface cannot retrieve the animation name through the trackEntry parameter.

(cherry picked from commit 7ead7e4)

# Conflicts:
#	native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp   resolved by theirs version
#	native/external-config.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants