Skip to content

Commit

Permalink
When loading csb files, prevent repeated loading of plist files (#1844)
Browse files Browse the repository at this point in the history
* When loading csb files, prevent repeated loading of plist files

* Add more feature

* Rename function

* Use isSpriteFramesWithFileLoaded to determine whether the atlas is loaded

* use string_view instead of string

* Update SpriteFrameCache.cpp

* Fix compilation error
  • Loading branch information
tkzcfc authored Apr 23, 2024
1 parent f159250 commit 16cb4b5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
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

0 comments on commit 16cb4b5

Please sign in to comment.