Skip to content

Commit

Permalink
fix fallback userobject not being properly assigned in certain cases
Browse files Browse the repository at this point in the history
  • Loading branch information
dankmeme01 committed Sep 24, 2024
1 parent 0771502 commit 0161d70
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 33 deletions.
6 changes: 3 additions & 3 deletions mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"geode": "3.1.1",
"version": "v1.6.1",
"geode": "3.7.1",
"version": "v1.6.2",
"gd": {
"win": "2.206",
"android": "2.206",
Expand All @@ -17,4 +17,4 @@
]
},
"repository": "https://github.com/geode-sdk/textureldr"
}
}
91 changes: 61 additions & 30 deletions src/Fallback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

using namespace geode::prelude;

void assignFallbackObj(CCNode* node) {
static constexpr int FALLBACK_TAG = 105871529;

static void assignFallbackObj(CCNode* node) {
if (!node) return;
node->setUserObject("fallback"_spr, CCBool::create(true));
}
Expand All @@ -21,50 +23,79 @@ class $modify(CCSprite) {
}
return sprite;
}

static CCSprite* createWithSpriteFrameName(const char* name) {
auto* sprite = CCSprite::createWithSpriteFrameName(name);
auto* spriteFrame = CCSpriteFrameCache::get()->spriteFrameByName(name);

// we check for tag instead of the frame name because this is significantly better for performance
bool needFallback = !spriteFrame || spriteFrame->getTag() == FALLBACK_TAG;

if (!needFallback) {
return CCSprite::createWithSpriteFrame(spriteFrame);
}

CCSprite* sprite = CCSprite::create("fallback.png"_spr);
if (sprite == nullptr) {
sprite = CCSprite::create("fallback.png"_spr);
if (sprite == nullptr) {
sprite = CCSprite::create("bigFont.png");
}
assignFallbackObj(sprite);
sprite = CCSprite::create("bigFont.png");
}
assignFallbackObj(sprite);
return sprite;
}

bool initWithSpriteFrame(CCSpriteFrame* frame) {
if (frame == nullptr) {
return CCSprite::initWithFile("fallback.png"_spr);
bool result = CCSprite::initWithFile("fallback.png"_spr);
if (result) {
assignFallbackObj(this);
}

return result;
}
return CCSprite::initWithSpriteFrame(frame);
}
};

class $modify(CCSpriteFrameCache) {
cocos2d::CCSpriteFrame* spriteFrameByName(char const* name) {
auto* frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name);
if (frame == nullptr) {
// this is stupid but rob intentionally doesnt load all icons at startup,
// probably to save memory, so do this to not use fallback on icons
static constexpr std::string_view prefixes[] = {
"player_",
"ship_",
"dart_",
"bird_",
"robot_",
"spider_",
"swing_",
"jetpack_",
};
const std::string_view nameStr = name;
for (auto const& prefix : prefixes) {
if (nameStr.find(prefix) != -1) {
return frame;
}
CCSpriteFrame* spriteFrameByName(char const* name) {
auto* frame = CCSpriteFrameCache::spriteFrameByName(name);

if (frame != nullptr) {
return frame;
}

// this is stupid but rob intentionally doesnt load all icons at startup,
// probably to save memory, so do this to not use fallback on icons
static constexpr std::string_view prefixes[] = {
"player_",
"ship_",
"dart_",
"bird_",
"robot_",
"spider_",
"swing_",
"jetpack_",
};
const std::string_view nameStr = name;
for (auto const& prefix : prefixes) {
if (nameStr.find(prefix) != -1) {
return frame;
}
frame = CCSpriteFrame::create("fallback.png"_spr, {ccp(0, 0), ccp(128, 128)});
}
return frame;

// check if the fallback was already added
auto fallbackFrame = CCSpriteFrameCache::spriteFrameByName("fallback.png"_spr);
if (fallbackFrame) {
return fallbackFrame;
}

// create the fallback frame and add to cache
fallbackFrame = CCSpriteFrame::create("fallback.png"_spr, {ccp(0, 0), ccp(128, 128)});

if (fallbackFrame) {
fallbackFrame->setTag(FALLBACK_TAG);
this->addSpriteFrame(fallbackFrame, "fallback.png"_spr);
}

return fallbackFrame;
}
};

0 comments on commit 0161d70

Please sign in to comment.