mirror of
https://github.com/geode-sdk/geode.git
synced 2025-04-01 07:40:18 -04:00
some code cleanup on InstallTicket + a safe sprite creation utility in
cocos.hpp (that i'll probably remove in the next commit)
This commit is contained in:
parent
cc45578f0d
commit
f7fabdbd38
3 changed files with 64 additions and 49 deletions
loader
|
@ -12,15 +12,15 @@ namespace geode::cocos {
|
|||
* @returns Child at index cast to the given type,
|
||||
* or nullptr if index exceeds bounds
|
||||
*/
|
||||
template<class T = cocos2d::CCNode*>
|
||||
static T getChild(cocos2d::CCNode* x, int i) {
|
||||
template<class T = cocos2d::CCNode>
|
||||
static T* getChild(cocos2d::CCNode* x, int i) {
|
||||
// start from end for negative index
|
||||
if (i < 0) i = x->getChildrenCount() + i;
|
||||
// check if backwards index is out of bounds
|
||||
if (i < 0) return nullptr;
|
||||
// check if forwards index is out of bounds
|
||||
if (static_cast<int>(x->getChildrenCount()) <= i) return nullptr;
|
||||
return reinterpret_cast<T>(x->getChildren()->objectAtIndex(i));
|
||||
return reinterpret_cast<T*>(x->getChildren()->objectAtIndex(i));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -28,12 +28,12 @@ namespace geode::cocos {
|
|||
* @returns Child at index cast to the given type,
|
||||
* or nullptr if index exceeds bounds
|
||||
*/
|
||||
template<class Type = cocos2d::CCNode*>
|
||||
static Type getChildOfType(cocos2d::CCNode* node, size_t index) {
|
||||
auto indexCounter = static_cast<size_t>(0);
|
||||
template<class Type = cocos2d::CCNode>
|
||||
static Type* getChildOfType(cocos2d::CCNode* node, size_t index) {
|
||||
size_t indexCounter = 0;
|
||||
|
||||
for (size_t i = 0; i < node->getChildrenCount(); ++i) {
|
||||
auto obj = cast::typeinfo_cast<Type>(
|
||||
auto obj = cast::typeinfo_cast<Type*>(
|
||||
node->getChildren()->objectAtIndex(i)
|
||||
);
|
||||
if (obj != nullptr) {
|
||||
|
@ -47,6 +47,34 @@ namespace geode::cocos {
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a node, or create a default one if it's
|
||||
* nullptr. Syntactic sugar function
|
||||
*/
|
||||
template<class T, class... Args>
|
||||
static T* nodeOrDefault(T* node, Args... args) {
|
||||
return node ? node : T::create(args...);
|
||||
}
|
||||
|
||||
template<class T = cocos2d::CCNode>
|
||||
struct SafeCreate final {
|
||||
T* result;
|
||||
SafeCreate<T>& with(T* node) {
|
||||
result = node;
|
||||
return *this;
|
||||
}
|
||||
template<class O = T, class... Args>
|
||||
T* orMakeUsing(O*(*func)(Args...), Args... args) {
|
||||
if (result) return result;
|
||||
return func(args...);
|
||||
}
|
||||
template<class O = T, class... Args>
|
||||
T* orMake(Args... args) {
|
||||
if (result) return result;
|
||||
return O::create(args...);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Get bounds for a set of nodes. Based on content
|
||||
* size
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#include <Geode/Geode.hpp>
|
||||
#include "../ui/internal/list/ModListLayer.hpp"
|
||||
#include <Geode/utils/WackyGeodeMacros.hpp>
|
||||
#include <Geode/ui/BasedButtonSprite.hpp>
|
||||
#include <Index.hpp>
|
||||
#include <Geode/ui/Notification.hpp>
|
||||
#include <Index.hpp>
|
||||
#include "../ui/internal/list/ModListLayer.hpp"
|
||||
|
||||
USE_GEODE_NAMESPACE();
|
||||
|
||||
|
@ -134,44 +134,25 @@ class $modify(CustomMenuLayer, MenuLayer) {
|
|||
if (!MenuLayer::init())
|
||||
return false;
|
||||
|
||||
CCMenu* bottomMenu = nullptr;
|
||||
auto bottomMenu = nodeOrDefault(getChildOfType<CCMenu>(this, 1));
|
||||
|
||||
size_t indexCounter = 0;
|
||||
for (size_t i = 0; i < this->getChildren()->count(); i++) {
|
||||
auto obj = typeinfo_cast<CCMenu*>(this->getChildren()->objectAtIndex(i));
|
||||
if (obj != nullptr) {
|
||||
++indexCounter;
|
||||
if (indexCounter == 2) {
|
||||
bottomMenu = obj;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if (!GameManager::sharedState()->m_clickedGarage) {
|
||||
// bottomMenu = getChild<CCMenu*>(this, 4);
|
||||
// }
|
||||
// else {
|
||||
// bottomMenu = getChild<CCMenu*>(this, 3);
|
||||
// }
|
||||
|
||||
auto chest = getChild<>(bottomMenu, -1);
|
||||
auto chest = getChild(bottomMenu, -1);
|
||||
if (chest) {
|
||||
chest->retain();
|
||||
chest->removeFromParent();
|
||||
}
|
||||
|
||||
auto y = getChild<>(bottomMenu, 0)->getPositionY();
|
||||
auto y = getChild(bottomMenu, 0)->getPositionY();
|
||||
|
||||
g_geodeButton = SafeCreate<CCSprite>()
|
||||
.with(CircleButtonSprite::createWithSpriteFrameName(
|
||||
"geode-logo-outline-gold.png"_spr,
|
||||
1.0f,
|
||||
CircleBaseColor::Green,
|
||||
CircleBaseSize::Medium2
|
||||
))
|
||||
.orMake<ButtonSprite>("!!");
|
||||
|
||||
g_geodeButton = CircleButtonSprite::createWithSpriteFrameName(
|
||||
"geode-logo-outline-gold.png"_spr,
|
||||
1.0f,
|
||||
CircleBaseColor::Green,
|
||||
CircleBaseSize::Medium2
|
||||
);
|
||||
if (!g_geodeButton) {
|
||||
g_geodeButton = ButtonSprite::create("!!");
|
||||
}
|
||||
addUpdateIcon();
|
||||
auto btn = CCMenuItemSpriteExtra::create(
|
||||
g_geodeButton.data(), this, menu_selector(CustomMenuLayer::onGeode)
|
||||
|
|
|
@ -52,13 +52,14 @@ void InstallTicket::install(std::string const& id) {
|
|||
tempFile,
|
||||
[this, tempFile](double now, double total) -> int {
|
||||
// check if cancelled
|
||||
m_cancelMutex.lock();
|
||||
std::lock_guard cancelLock(m_cancelMutex);
|
||||
if (m_cancelling) {
|
||||
try { ghc::filesystem::remove(tempFile); } catch(...) {}
|
||||
m_cancelMutex.unlock();
|
||||
return 1;
|
||||
}
|
||||
m_cancelMutex.unlock();
|
||||
|
||||
// no need to scope the lock guard more as this
|
||||
// function is going to exit right after anyway
|
||||
|
||||
this->postProgress(
|
||||
UpdateStatus::Progress,
|
||||
|
@ -77,13 +78,15 @@ void InstallTicket::install(std::string const& id) {
|
|||
}
|
||||
|
||||
// check if cancelled
|
||||
m_cancelMutex.lock();
|
||||
if (m_cancelling) {
|
||||
ghc::filesystem::remove(tempFile);
|
||||
m_cancelMutex.unlock();
|
||||
return;
|
||||
{
|
||||
std::lock_guard cancelLock(m_cancelMutex);
|
||||
if (m_cancelling) {
|
||||
ghc::filesystem::remove(tempFile);
|
||||
return;
|
||||
}
|
||||
// scope ends here since we don't need to
|
||||
// access m_cancelling anymore
|
||||
}
|
||||
m_cancelMutex.unlock();
|
||||
|
||||
// check for 404
|
||||
auto notFound = file_utils::readString(tempFile);
|
||||
|
@ -164,6 +167,9 @@ void InstallTicket::install(std::string const& id) {
|
|||
}
|
||||
|
||||
void InstallTicket::cancel() {
|
||||
// really no point in using std::lock_guard here
|
||||
// since just plain locking and unlocking the mutex
|
||||
// will do the job just fine with the same legibility
|
||||
m_cancelMutex.lock();
|
||||
m_cancelling = true;
|
||||
m_cancelMutex.unlock();
|
||||
|
|
Loading…
Add table
Reference in a new issue