Skip to content

Commit 075ecb4

Browse files
committed
restore vanilla behavior of falling back to hd and nonhd plists if uhd is not found at all
1 parent 48c6519 commit 075ecb4

1 file changed

Lines changed: 61 additions & 44 deletions

File tree

‎src/PlistRewrite.cpp‎

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,62 @@ std::string getPngForQuality(const char *plist, cocos2d::TextureQuality quality)
204204
}
205205
}
206206

207+
bool doAddSpriteFramesWithFile(const char *plist, cocos2d::TextureQuality quality) {
208+
auto fileUtils = CCFileUtils::sharedFileUtils();
209+
std::string plistStr = getPlistForQuality(plist, quality);
210+
211+
bool found = false;
212+
213+
for (const auto &dir : fileUtils->getSearchPaths()) {
214+
std::string texturePath;
215+
std::string fullPath = joinPath(dir, plistStr);
216+
217+
if (!fileUtils->isFileExist(fullPath)) { // does the search path find it
218+
auto pngPath = getPngForQuality(plist, quality);
219+
auto pngFullPath = joinPath(dir, pngPath);
220+
if (!fileUtils->isFileExist(pngFullPath)) {
221+
continue;
222+
} else {
223+
texturePath = pngPath;
224+
// if the plist is not present, we assume we should use the one from the vanilla game
225+
for (const auto &dir : fileUtils->getSearchPaths() | std::views::reverse) {
226+
fullPath = joinPath(dir, plistStr);
227+
if (fileUtils->isFileExist(fullPath)) {
228+
break;
229+
}
230+
}
231+
}
232+
}
233+
234+
auto dict = CCDictionary::createWithContentsOfFileThreadSafe(fullPath.c_str()); // robtop method in creating plist arrays
235+
236+
if (!dict) // failed?
237+
continue;
238+
239+
found = true;
240+
241+
// robtop checks if textureFileName exists... I join the dir together to make sure this search path is used
242+
if (texturePath.empty()) {
243+
if (auto metadata = typeinfo_cast<CCDictionary *>(dict->objectForKey("metadata"))) {
244+
texturePath = joinPath(dir, metadata->valueForKey("textureFileName")->getCString());
245+
}
246+
}
247+
// Doesn't contain metadata for textureFileName
248+
if (texturePath.empty()) {
249+
texturePath = plistStr;
250+
size_t pos = texturePath.find_last_of('.');
251+
if (pos != std::string::npos)
252+
texturePath.erase(pos);
253+
texturePath += ".png";
254+
}
255+
// inlined version of addSpriteFramesWithDictionary as it private also makes it so it only loads the image if needed
256+
addSpriteFramesWithDictionaryOnlyLoadingTextureIfNeeded(dict, texturePath);
257+
dict->release();
258+
}
259+
260+
return found;
261+
}
262+
207263
class $modify(RewrittenSpriteFrames, CCSpriteFrameCache) {
208264
static void onModify(auto &self) {
209265
// "If you’re reimplementing the original function and do not call the original, use Priority::Last with setHookPriorityPre."
@@ -230,51 +286,12 @@ class $modify(RewrittenSpriteFrames, CCSpriteFrameCache) {
230286
if (!m_pLoadedFileNames->insert(plist).second)
231287
return;
232288

233-
std::string plistStr = getPlistForQuality(plist, CCDirector::get()->getLoadedTextureQuality());
234-
235-
for (const auto &dir : fileUtils->getSearchPaths()) {
236-
std::string texturePath;
237-
std::string fullPath = joinPath(dir, plistStr);
238-
239-
if (!fileUtils->isFileExist(fullPath)) { // does the search path find it
240-
auto pngPath = getPngForQuality(plist, CCDirector::get()->getLoadedTextureQuality());
241-
auto pngFullPath = joinPath(dir, pngPath);
242-
if (!fileUtils->isFileExist(pngFullPath)) {
243-
continue;
244-
} else {
245-
texturePath = pngPath;
246-
// if the plist is not present, we assume we should use the one from the vanilla game
247-
for (const auto &dir : fileUtils->getSearchPaths() | std::views::reverse) {
248-
fullPath = joinPath(dir, plistStr);
249-
if (fileUtils->isFileExist(fullPath)) {
250-
break;
251-
}
252-
}
253-
}
254-
}
255-
256-
auto dict = CCDictionary::createWithContentsOfFileThreadSafe(fullPath.c_str()); // robtop method in creating plist arrays
257-
258-
if (!dict) // failed?
259-
continue;
260-
261-
// robtop checks if textureFileName exists... I join the dir together to make sure this search path is used
262-
if (texturePath.empty()) {
263-
if (auto metadata = typeinfo_cast<CCDictionary *>(dict->objectForKey("metadata"))) {
264-
texturePath = joinPath(dir, metadata->valueForKey("textureFileName")->getCString());
265-
}
266-
}
267-
// Doesn't contain metadata for textureFileName
268-
if (texturePath.empty()) {
269-
texturePath = plistStr;
270-
size_t pos = texturePath.find_last_of('.');
271-
if (pos != std::string::npos)
272-
texturePath.erase(pos);
273-
texturePath += ".png";
289+
auto quality = CCDirector::get()->getLoadedTextureQuality();
290+
while (quality >= cocos2d::TextureQuality::kTextureQualityLow) {
291+
if (doAddSpriteFramesWithFile(plist, quality)) {
292+
return;
274293
}
275-
// inlined version of addSpriteFramesWithDictionary as it private also makes it so it only loads the image if needed
276-
addSpriteFramesWithDictionaryOnlyLoadingTextureIfNeeded(dict, texturePath);
277-
dict->release();
294+
quality = static_cast<cocos2d::TextureQuality>(static_cast<int>(quality) - 1);
278295
}
279296
}
280297
};

0 commit comments

Comments
 (0)