@@ -32,18 +32,31 @@ namespace AlphaUtils {
3232 std::string ret;
3333
3434 #ifdef GEODE_IS_WINDOWS
35- ret = typeid (*obj).name () + 6 ;
35+ ret = typeid (*obj).name ();
36+ constexpr std::string_view classPrefix = " class " ;
37+ constexpr std::string_view structPrefix = " struct " ;
38+
39+ auto removeAll = [](std::string& str, std::string_view prefix) {
40+ size_t pos = 0 ;
41+ while ((pos = str.find (prefix, pos)) != std::string::npos) {
42+ str.erase (pos, prefix.size ());
43+ }
44+ };
45+
46+ removeAll (ret, classPrefix);
47+ removeAll (ret, structPrefix);
3648 #else
3749 int status = 0 ;
38- auto demangle = abi::__cxa_demangle (typeid (*obj).name (), 0 , 0 , &status);
39- if (status == 0 ) {
40- ret = demangle ;
50+ auto demangled = abi::__cxa_demangle (typeid (*obj).name (), nullptr , nullptr , &status);
51+ if (status == 0 && demangled ) {
52+ ret = demangled ;
4153 }
42- free (demangle );
54+ free (demangled );
4355 #endif
4456 if (removeNamespace) {
45- std::vector<std::string> colonSplit = geode::utils::string::split (ret, " ::" );
46- ret = colonSplit[colonSplit.size ()-1 ];
57+ if (auto pos = ret.rfind (" ::" ); pos != std::string::npos) {
58+ ret = ret.substr (pos + 2 );
59+ }
4760 }
4861
4962 return ret;
@@ -112,35 +125,29 @@ namespace AlphaUtils {
112125
113126 // getChildByType but using a string instead for dynamic use.
114127 static inline std::optional<cocos2d::CCNode*> getChildByClassName (cocos2d::CCNode* node, std::string name, int index = 0 ) {
128+ if (!node || node->getChildrenCount () == 0 ) return nullptr ;
129+
115130 size_t indexCounter = 0 ;
131+ const size_t childrenCount = node->getChildrenCount ();
116132
117- if (!node || node->getChildrenCount () == 0 ) return std::nullopt ;
118- // start from end for negative index
119- if (index < 0 ) {
120- index = -index - 1 ;
121- for (size_t i = node->getChildrenCount () - 1 ; i >= 0 ; i--) {
122- cocos2d::CCNode* idxNode = static_cast <cocos2d::CCNode*>(node->getChildren ()->objectAtIndex (i));
123- std::string className = getClassName (idxNode);
124- if (className == name) {
125- if (indexCounter == index) {
126- return idxNode;
127- }
128- ++indexCounter;
129- }
130- if (i == 0 ) break ;
131- }
133+ bool isNegativeIndex = (index < 0 );
134+ if (isNegativeIndex) {
135+ index = -index - 1 ;
132136 }
133- else {
134- for (size_t i = 0 ; i < node-> getChildrenCount ( ); i++) {
135- cocos2d::CCNode* idxNode = static_cast <cocos2d::CCNode*>(node-> getChildren ()-> objectAtIndex (i));
136- std::string className = getClassName (idxNode);
137- if (className == name) {
138- if (indexCounter == index) {
139- return idxNode;
140- }
141- ++indexCounter ;
137+
138+ for (size_t i = (isNegativeIndex ? childrenCount - 1 : 0 );
139+ isNegativeIndex ? i >= 0 : i < childrenCount;
140+ isNegativeIndex ? --i : ++i) {
141+
142+ cocos2d::CCNode* idxNode = static_cast <cocos2d::CCNode*>(node-> getChildren ()-> objectAtIndex (i));
143+ if ( AlphaUtils::Cocos::getClassName (idxNode, true ) == name) {
144+ if (indexCounter == index) {
145+ return idxNode ;
142146 }
147+ ++indexCounter;
143148 }
149+
150+ if (isNegativeIndex && i == 0 ) break ;
144151 }
145152
146153 return std::nullopt ;
@@ -162,7 +169,7 @@ namespace AlphaUtils {
162169 return spr;
163170 }
164171
165- template <typename Layer>
172+ template <typename Layer, typename = std:: enable_if_t <std::is_pointer_v<Layer>> >
166173 static inline std::optional<Layer> getLayer () {
167174
168175 auto scene = cocos2d::CCDirector::sharedDirector ()->getRunningScene ();
0 commit comments