Skip to content

Commit 79256c8

Browse files
committed
Fixes
1 parent 276a825 commit 79256c8

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.1.8
2+
- Quick fixes you'll never see as an end user
3+
- Code cleanup
4+
15
# 1.1.7
26
- Fix a Cocos2d-x 2.2.3 bug where for some godforsaken reason, autorelease can be called on a nullptr in CCDictionary::createWithContentsOfFile. Thank you cocos devs :)
37

include/Utils.h

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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();

mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"id": "alphalaneous.alphas_geode_utils",
1010
"name": "Alpha's Geode Utils",
11-
"version": "v1.1.7",
11+
"version": "v1.1.8",
1212
"developer": "Alphalaneous",
1313
"description": "Miscellaneous utilities for Geode Modding",
1414
"api": {

0 commit comments

Comments
 (0)