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

* 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
This commit is contained in:
tkzcfc 2024-04-23 21:21:53 +08:00 committed by GitHub
parent f159250c4e
commit 16cb4b5e9d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 62 additions and 6 deletions

View File

@ -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
@ -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);

View File

@ -199,6 +199,8 @@ public:
*/
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.
@ -246,6 +248,8 @@ public:
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
*/

View File

@ -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
@ -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);
@ -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);
@ -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();