Skip to content

Commit cf7db41

Browse files
committed
Fix crashes while calling setSlotTexture or running at cache mode
1 parent 387af44 commit cf7db41

File tree

3 files changed

+56
-30
lines changed

3 files changed

+56
-30
lines changed

native/cocos/editor-support/spine-creator-support/SkeletonCache.cpp

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@
3030
#include "SkeletonCache.h"
3131
#include "base/memory/Memory.h"
3232
#include "spine-creator-support/AttachmentVertices.h"
33+
#include "SkeletonDataMgr.h"
3334

3435
USING_NS_MW; // NOLINT(google-build-using-namespace)
3536
using namespace cc; // NOLINT(google-build-using-namespace)
3637
using namespace spine;
38+
39+
extern "C" AttachmentVertices *generateAttachmentVertices(Attachment *attachment);
40+
3741
namespace cc {
3842

3943
float SkeletonCache::FrameTime = 1.0F / 60.0F;
@@ -311,6 +315,10 @@ void SkeletonCache::renderAnimationFrame(AnimationData *animationData) {
311315
matm[13] = bone->getWorldY();
312316
}
313317

318+
319+
auto *verticesMap = SkeletonDataMgr::getInstance()->getSkeletonDataInfo(_uuid);
320+
if (!verticesMap) return;
321+
auto &attachmentVerticesMap = *verticesMap;
314322
auto &drawOrder = _skeleton->getDrawOrder();
315323
for (size_t i = 0, n = drawOrder.size(); i < n; ++i) {
316324
slot = drawOrder[i];
@@ -319,20 +327,34 @@ void SkeletonCache::renderAnimationFrame(AnimationData *animationData) {
319327
continue;
320328
}
321329

322-
if (!slot->getAttachment()) {
330+
auto *tmpAttachment = slot->getAttachment();
331+
if (!tmpAttachment) {
323332
_clipper->clipEnd(*slot);
324333
continue;
325334
}
326335
const spine::Color &slotColor = slot->getColor();
327336

337+
auto iterAttachment = attachmentVerticesMap.find(tmpAttachment);
338+
if (iterAttachment != attachmentVerticesMap.end()) {
339+
attachmentVertices = iterAttachment->second;
340+
} else {
341+
attachmentVertices = nullptr;
342+
}
343+
328344
TwoColorTriangles trianglesTwoColor;
329345
spine::Color attachmentColor;
330-
if (slot->getAttachment()->getRTTI().isExactly(RegionAttachment::rtti)) {
331-
auto *attachment = dynamic_cast<RegionAttachment *>(slot->getAttachment());
332-
#if CC_USE_SPINE_3_8
333-
attachmentVertices = static_cast<AttachmentVertices *>(attachment->getRendererObject());
334-
#else
335-
attachmentVertices = static_cast<AttachmentVertices *>(attachment->getRegion()->rendererObject);
346+
if (tmpAttachment->getRTTI().isExactly(RegionAttachment::rtti)) {
347+
auto *attachment = dynamic_cast<RegionAttachment *>(tmpAttachment);
348+
#if CC_USE_SPINE_4_2
349+
if (!attachment->getRegion()) {
350+
attachment->getSequence()->apply(slot, attachment);
351+
if (attachment->getRegion()) {
352+
attachmentVertices = generateAttachmentVertices(attachment);
353+
if (attachmentVertices) {
354+
attachmentVerticesMap[attachment] = attachmentVertices;
355+
}
356+
}
357+
}
336358
#endif
337359

338360
// Early exit if attachment is invisible
@@ -362,12 +384,18 @@ void SkeletonCache::renderAnimationFrame(AnimationData *animationData) {
362384

363385
attachmentColor = attachment->getColor();
364386

365-
} else if (slot->getAttachment()->getRTTI().isExactly(MeshAttachment::rtti)) {
366-
auto *attachment = dynamic_cast<MeshAttachment *>(slot->getAttachment());
367-
#if CC_USE_SPINE_3_8
368-
attachmentVertices = static_cast<AttachmentVertices *>(attachment->getRendererObject());
369-
#else
370-
attachmentVertices = static_cast<AttachmentVertices *>(attachment->getRegion()->rendererObject);
387+
} else if (tmpAttachment->getRTTI().isExactly(MeshAttachment::rtti)) {
388+
auto *attachment = dynamic_cast<MeshAttachment *>(tmpAttachment);
389+
#if CC_USE_SPINE_4_2
390+
if (!attachment->getRegion()) {
391+
attachment->getSequence()->apply(slot, attachment);
392+
if (attachment->getRegion()) {
393+
attachmentVertices = generateAttachmentVertices(attachment);
394+
if (attachmentVertices) {
395+
attachmentVerticesMap[attachment] = attachmentVertices;
396+
}
397+
}
398+
}
371399
#endif
372400

373401
// Early exit if attachment is invisible
@@ -391,8 +419,8 @@ void SkeletonCache::renderAnimationFrame(AnimationData *animationData) {
391419
trianglesTwoColor.indices = reinterpret_cast<uint16_t *>(ib.getCurBuffer());
392420
memcpy(trianglesTwoColor.indices, attachmentVertices->_triangles->indices, ibSize);
393421
attachmentColor = attachment->getColor();
394-
} else if (slot->getAttachment()->getRTTI().isExactly(ClippingAttachment::rtti)) {
395-
auto *clip = dynamic_cast<ClippingAttachment *>(slot->getAttachment());
422+
} else if (tmpAttachment->getRTTI().isExactly(ClippingAttachment::rtti)) {
423+
auto *clip = dynamic_cast<ClippingAttachment *>(tmpAttachment);
396424
_clipper->clipStart(*slot, clip);
397425
continue;
398426
} else {

native/cocos/editor-support/spine-creator-support/SkeletonRenderer.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,6 @@ enum DebugType {
6565
BONES
6666
};
6767

68-
template<typename VertexType, typename UVArrayType>
69-
void loopUVCoords(VertexType* tmp, const UVArrayType& uvs, int count) {
70-
for (int i = 0, ii = 0; i < count; ++i, ii += 2) {
71-
tmp[i].texCoord.u = uvs[ii];
72-
tmp[i].texCoord.v = uvs[ii + 1];
73-
}
74-
}
75-
7668
extern "C" AttachmentVertices *generateAttachmentVertices(Attachment *attachment);
7769
namespace cc {
7870
SkeletonRenderer *SkeletonRenderer::create() {
@@ -1168,12 +1160,15 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D
11681160
if (!attachment) return;
11691161
auto width = tex2d->getWidth();
11701162
auto height = tex2d->getHeight();
1163+
auto *verticesMap = SkeletonDataMgr::getInstance()->getSkeletonDataInfo(_uuid);
1164+
if (!verticesMap) return;
1165+
auto &attachmentVerticesMap = *verticesMap;
1166+
auto *attachmentVertices = attachmentVerticesMap.at(attachment);
11711167

11721168
if (createAttachment) {
11731169
attachment = attachment->copy();
11741170
}
11751171
SlotCacheInfo slotCacheInfo{createAttachment, attachment, nullptr};
1176-
AttachmentVertices *attachmentVertices = nullptr;
11771172
if (attachment->getRTTI().isExactly(spine::RegionAttachment::rtti)) {
11781173
auto region = static_cast<RegionAttachment *>(attachment);
11791174
#if CC_USE_SPINE_3_8
@@ -1185,7 +1180,6 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D
11851180
region->setHeight(height);
11861181
region->setUVs(0, 0, 1.0f, 1.0f, false);
11871182
region->updateOffset();
1188-
attachmentVertices = static_cast<AttachmentVertices *>(region->getRendererObject());
11891183
if (createAttachment) {
11901184
attachmentVertices = attachmentVertices->copy();
11911185
slotCacheInfo.attachmentVertices = attachmentVertices;
@@ -1215,8 +1209,6 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D
12151209
uvs[0] = 1;
12161210
uvs[1] = 1;
12171211
region->updateRegion();
1218-
auto *tmpMap = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(textureRegion->rendererObject);
1219-
attachmentVertices = (*tmpMap)[slot->getAttachment()];
12201212
if (createAttachment) {
12211213
attachmentVertices = attachmentVertices->copy();
12221214
slotCacheInfo.attachmentVertices = attachmentVertices;
@@ -1244,7 +1236,6 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D
12441236
mesh->setRegionRotate(true);
12451237
mesh->setRegionDegrees(0);
12461238
mesh->updateUVs();
1247-
attachmentVertices = static_cast<AttachmentVertices *>(mesh->getRendererObject());
12481239
if (createAttachment) {
12491240
attachmentVertices = attachmentVertices->copy();
12501241
slotCacheInfo.attachmentVertices = attachmentVertices;
@@ -1265,8 +1256,6 @@ void SkeletonRenderer::setSlotTexture(const std::string &slotName, cc::Texture2D
12651256
mesh->setWidth(width);
12661257
mesh->setHeight(height);
12671258
mesh->updateRegion();
1268-
auto *tmpMap = static_cast<spine::HashMap<Attachment *, AttachmentVertices *> *>(mesh->getRegion()->rendererObject);
1269-
attachmentVertices = (*tmpMap)[slot->getAttachment()];
12701259
if (createAttachment) {
12711260
attachmentVertices = attachmentVertices->copy();
12721261
slotCacheInfo.attachmentVertices = attachmentVertices;

native/cocos/editor-support/spine-creator-support/SkeletonRenderer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ class Material;
5050

5151
class AttachmentVertices;
5252

53+
54+
template <typename VertexType, typename UVArrayType>
55+
void loopUVCoords(VertexType *tmp, const UVArrayType &uvs, int count) {
56+
for (int i = 0, ii = 0; i < count; ++i, ii += 2) {
57+
tmp[i].texCoord.u = uvs[ii];
58+
tmp[i].texCoord.v = uvs[ii + 1];
59+
}
60+
}
61+
5362
struct SlotCacheInfo {
5463
bool isOwner{false};
5564
spine::Attachment *attachment{nullptr};

0 commit comments

Comments
 (0)