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

When loading csb files, prevent repeated loading of plist files #1844

Merged
merged 7 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions core/2d/SpriteFrameCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,33 @@ void SpriteFrameCache::removeUnusedSpriteFrames()
}
}

void SpriteFrameCache::removeUnusedSpriteSheets()
{
std::vector<std::string_view> willRemoveSpriteSheetFileNames;
for (auto&& it : _spriteSheets)
{
bool isUsed = false;
for (auto&& frame : it.second->frames)
{
auto spriteFrame = getSpriteFrameByName(frame);
if (spriteFrame && spriteFrame->getReferenceCount() > 1)
{
isUsed = true;
break;
}
}

if (!isUsed)
willRemoveSpriteSheetFileNames.push_back(it.first);
}

for (auto& spriteSheetFileName : willRemoveSpriteSheetFileNames)
{
AXLOG("axmol: SpriteFrameCache: removing unused sprite sheet file : %s", spriteSheetFileName.data());
removeSpriteSheet(spriteSheetFileName);
}
}

void SpriteFrameCache::removeSpriteFrameByName(std::string_view name)
{
// explicit nil handling
Expand Down Expand Up @@ -376,6 +403,18 @@ SpriteFrame* SpriteFrameCache::findFrame(std::string_view frame)
return _spriteFrames.at(frame);
}

std::string_view SpriteFrameCache::getSpriteFrameName(SpriteFrame* frame)
{
for (auto& it : _spriteFrames)
{
if (it.second == frame)
{
return it.first;
}
}
return "";
}

void SpriteFrameCache::addSpriteFrameCapInset(SpriteFrame* spriteFrame, const Rect& capInsets, Texture2D* texture)
{
texture->addSpriteFrameCapInset(spriteFrame, capInsets);
Expand Down
4 changes: 4 additions & 0 deletions core/2d/SpriteFrameCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ class AX_DLL SpriteFrameCache
*/
void removeUnusedSpriteFrames();

void removeUnusedSpriteSheets();

/** Deletes an sprite frame from the sprite frame cache.
*
* @param name The name of the sprite frame that needs to removed.
Expand Down Expand Up @@ -246,6 +248,8 @@ class AX_DLL SpriteFrameCache

SpriteFrame* findFrame(std::string_view frame);

std::string_view getSpriteFrameName(SpriteFrame* frame);

/** Record SpriteFrame with plist and frame name, add frame name
* and plist to index
*/
Expand Down
25 changes: 19 additions & 6 deletions extensions/cocostudio/src/cocostudio/ActionTimeline/CSLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,10 @@ Node* CSLoader::loadNodeWithContent(std::string_view content)
std::string png = DICTOOL->getStringValueFromArray_json(doc, TEXTURES_PNG, i);
plist = _jsonPath + plist;
png = _jsonPath + png;
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist, png);
if (!SpriteFrameCache::getInstance()->isSpriteFramesWithFileLoaded(plist))
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist, png);
}
}

// decode node tree
Expand Down Expand Up @@ -949,8 +952,11 @@ Node* CSLoader::createNode(const Data& data, const ccNodeLoadCallback& callback)
AXLOG("textureSize = %d", textureSize);
for (int i = 0; i < textureSize; ++i)
{
std::string plist = textures->Get(i)->c_str();
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
std::string_view plist = textures->Get(i)->c_str();
if (!SpriteFrameCache::getInstance()->isSpriteFramesWithFileLoaded(plist))
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
}
}

node = loader->nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
Expand Down Expand Up @@ -1073,8 +1079,11 @@ Node* CSLoader::nodeWithFlatBuffersFile(std::string_view fileName, const ccNodeL
int textureSize = textures->size();
for (int i = 0; i < textureSize; ++i)
{
std::string plist = textures->Get(i)->c_str();
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
std::string_view plist = textures->Get(i)->c_str();
if (!SpriteFrameCache::getInstance()->isSpriteFramesWithFileLoaded(plist))
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
}
}

Node* node = nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
Expand Down Expand Up @@ -1438,7 +1447,11 @@ Node* CSLoader::createNodeWithFlatBuffersForSimulator(std::string_view filename)
// AXLOG("textureSize = %d", textureSize);
for (int i = 0; i < textureSize; ++i)
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(textures->Get(i)->c_str());
std::string_view plist = textures->Get(i)->c_str();
if (!SpriteFrameCache::getInstance()->isSpriteFramesWithFileLoaded(plist))
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
}
}

auto nodeTree = csparsebinary->nodeTree();
Expand Down
Loading