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 2 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::removeUnusedSpriteSheetFile()
{
std::vector<string> 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.c_str());
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 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 removeUnusedSpriteSheetFile();

/** 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 getSpriteFrameName(SpriteFrame* frame);

/** Record SpriteFrame with plist and frame name, add frame name
* and plist to index
*/
Expand Down
29 changes: 25 additions & 4 deletions extensions/cocostudio/src/cocostudio/ActionTimeline/CSLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ Node* CSLoader::createNode(std::string_view filename)
auto suffix = path.substr(pos + 1, path.length());

CSLoader* load = CSLoader::getInstance();
load->_loadedPlists.clear();

if (suffix == "csb")
{
Expand All @@ -307,6 +308,7 @@ Node* CSLoader::createNode(std::string_view filename, const ccNodeLoadCallback&
auto suffix = path.substr(pos + 1, path.length());

CSLoader* load = CSLoader::getInstance();
load->_loadedPlists.clear();

if (suffix == "csb")
{
Expand Down Expand Up @@ -466,7 +468,11 @@ 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 (!_loadedPlists.contains(plist + png))
{
_loadedPlists.insert(plist + png);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist, png);
}
}

// decode node tree
Expand Down Expand Up @@ -894,6 +900,8 @@ inline void fast_split(_Elem* s, typename std::remove_const<_Elem>::type delim,
Node* CSLoader::createNode(const Data& data, const ccNodeLoadCallback& callback)
{
CSLoader* loader = CSLoader::getInstance();
loader->_loadedPlists.clear();

Node* node = nullptr;
do
{
Expand Down Expand Up @@ -950,7 +958,11 @@ Node* CSLoader::createNode(const Data& data, const ccNodeLoadCallback& callback)
for (int i = 0; i < textureSize; ++i)
{
std::string plist = textures->Get(i)->c_str();
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
if (!loader->_loadedPlists.contains(plist))
{
loader->_loadedPlists.insert(plist);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
}
}

node = loader->nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
Expand Down Expand Up @@ -1074,7 +1086,11 @@ Node* CSLoader::nodeWithFlatBuffersFile(std::string_view fileName, const ccNodeL
for (int i = 0; i < textureSize; ++i)
{
std::string plist = textures->Get(i)->c_str();
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
if (!_loadedPlists.contains(plist))
{
_loadedPlists.insert(plist);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
}
}

Node* node = nodeWithFlatBuffers(csparsebinary->nodeTree(), callback);
Expand Down Expand Up @@ -1438,7 +1454,12 @@ 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 plist = textures->Get(i)->c_str();
if (!_loadedPlists.contains(plist))
{
_loadedPlists.insert(plist);
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(plist);
}
}

auto nodeTree = csparsebinary->nodeTree();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ class CCS_DLL CSLoader
ax::Vector<ax::Node*> _callbackHandlers;

std::string _csBuildID;

std::set<std::string> _loadedPlists;
};

NS_AX_END
Expand Down
Loading