Skip to content

Commit 2cc1373

Browse files
committed
Alpha 2
1 parent 48f6237 commit 2cc1373

8 files changed

Lines changed: 232 additions & 83 deletions

File tree

README.md

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,47 @@ Include `ObjectModify.hpp` to access these macros.
2222
```c++
2323
class $nodeModify(MyCustomCreatorLayer, CustomCreatorLayer) {
2424

25-
static int modifyPrio() {
26-
return 10;
27-
}
28-
29-
struct Fields {
30-
int m_number = 0;
31-
};
32-
33-
void modify() {
34-
35-
CCSprite* spr = CCSprite::createWithSpriteFrameName("GJ_playBtn_001.png");
36-
CCMenuItemSpriteExtra* btn = CCMenuItemSpriteExtra::create(spr, this, menu_selector(MyCustomCreatorLayer::onEpicButton));
37-
btn->setID("epic-button"_spr);
38-
39-
if (CCMenu* creatorButtonsMenu = typeinfo_cast<CCMenu*>(getChildByID("cvolton.betterinfo/creator-buttons-menu"))) {
40-
creatorButtonsMenu->addChild(btn);
41-
creatorButtonsMenu->updateLayout();
42-
}
43-
}
44-
45-
void onEpicButton(CCObject* obj) {
46-
log::info("m_number {}", m_fields->m_number);
47-
m_fields->m_number++;
48-
}
25+
static int modifyPrio() {
26+
return 10;
27+
}
28+
29+
struct Fields {
30+
int m_number = 0;
31+
};
32+
33+
void modify() {
34+
35+
CCSprite* spr = CCSprite::createWithSpriteFrameName("GJ_playBtn_001.png");
36+
CCMenuItemSpriteExtra* btn = CCMenuItemSpriteExtra::create(spr, this, menu_selector(MyCustomCreatorLayer::onEpicButton));
37+
btn->setID("epic-button"_spr);
38+
39+
if (CCMenu* creatorButtonsMenu = typeinfo_cast<CCMenu*>(getChildByID("cvolton.betterinfo/creator-buttons-menu"))) {
40+
creatorButtonsMenu->addChild(btn);
41+
creatorButtonsMenu->updateLayout();
42+
}
43+
}
44+
45+
void onEpicButton(CCObject* obj) {
46+
log::info("m_number {}", m_fields->m_number);
47+
m_fields->m_number++;
48+
}
4949
};
5050
```
5151
52+
There's also a useful utility for modifying all classes with a specific Base class. `class $baseModify(SomeClass)`
53+
54+
It works just like above:
55+
56+
```cpp
57+
class $baseModify(MyFLAlertLayer, FLAlertLayer) {
58+
59+
void modify() {
60+
m_noElasticity = true;
61+
m_mainLayer->setScale(0.5f);
62+
}
63+
}
64+
```
65+
5266
## General Utils
5367

5468
Include `Utils.hpp` to access these utils.

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# 1.2.0-beta.2
2+
- Geode Alpha 2 support
3+
- Performance Improvements
4+
- Added $baseModify
5+
16
# 1.2.0-beta.1
27
- Cleanup everything
38
- Added $classModify to use a base class instead of CCNode or CCObject

include/ModifyHandler.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include <queue>
44
#include <Geode/cocos/cocoa/CCObject.h>
55
#include <Geode/utils/cocos.hpp>
6+
#include <vector>
67
#include "ObjectData.hpp"
8+
#include <typeindex>
79

810
#ifdef GEODE_IS_WINDOWS
911
#ifdef ALPHALANEOUS_UTILS_API_EXPORTING
@@ -28,7 +30,26 @@ namespace alpha::utils {
2830
void createObjectData(cocos2d::CCObject* object);
2931
void handleObject(cocos2d::CCObject* object);
3032

33+
template <class Base>
34+
inline bool containsBase(const cocos2d::CCObject* obj) {
35+
auto key = std::type_index(typeid(*obj));
36+
auto base = std::type_index(typeid(Base));
37+
38+
auto it = m_types.find(key);
39+
if (it != m_types.end()) {
40+
auto it2 = it->second.find(base);
41+
if (it2 != it->second.end()) {
42+
return it2->second;
43+
}
44+
}
45+
46+
bool ret = geode::cast::typeinfo_cast<Base*>(obj);
47+
m_types[key][base] = ret;
48+
return ret;
49+
}
50+
3151
protected:
52+
std::unordered_map<std::type_index, std::unordered_map<std::type_index, bool>> m_types;
3253
std::vector<geode::Ref<ObjectData>> m_arena;
3354
std::queue<uint32_t> m_slots;
3455
};

include/ObjectModify.hpp

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
22

3-
#include <functional>
43
#include <Geode/cocos/base_nodes/CCNode.h>
54
#include <Geode/utils/StringMap.hpp>
65
#include <Geode/utils/ZStringView.hpp>
@@ -28,34 +27,58 @@ namespace alpha::utils {
2827

2928
struct ObjectModifyInfo {
3029
int priority;
31-
std::function<void(ModifyCCObject<cocos2d::CCObject>*)> method;
30+
geode::Function<void(ModifyCCObject<cocos2d::CCObject>*)> method;
31+
32+
ObjectModifyInfo(int p, geode::Function<void(ModifyCCObject<cocos2d::CCObject>*)>&& m)
33+
: priority(p), method(std::move(m)) {}
34+
35+
ObjectModifyInfo(ObjectModifyInfo&&) noexcept = default;
36+
ObjectModifyInfo& operator=(ObjectModifyInfo&&) noexcept = default;
37+
38+
ObjectModifyInfo(const ObjectModifyInfo&) = delete;
39+
ObjectModifyInfo& operator=(const ObjectModifyInfo&) = delete;
3240
};
3341

3442
class ALPHA_UTILS_API_DLL ObjectModify {
3543
protected:
3644
geode::utils::StringMap<std::vector<ObjectModifyInfo>> m_objectsToModify;
45+
geode::utils::StringMap<std::vector<ObjectModifyInfo>> m_objectBasesToModify;
3746
private:
47+
ObjectModify() = default;
48+
ObjectModify(const ObjectModify&) = delete;
49+
ObjectModify& operator=(const ObjectModify&) = delete;
50+
ObjectModify(ObjectModify&&) = delete;
51+
ObjectModify& operator=(ObjectModify&&) = delete;
52+
3853
static ObjectModify* get();
39-
void addObjectToModify(geode::ZStringView className, int prio, std::function<void(ModifyCCObject<cocos2d::CCObject>*)> func);
54+
void addObjectToModify(geode::ZStringView className, int prio, geode::Function<void(ModifyCCObject<cocos2d::CCObject>*)> func);
55+
void addObjectToModifyBase(geode::ZStringView className, int prio, geode::Function<void(ModifyCCObject<cocos2d::CCObject>*)> func);
4056
geode::utils::StringMap<std::vector<ObjectModifyInfo>>& getObjectsToModify();
41-
void handleObject(ModifyCCObject<cocos2d::CCObject>* object);
57+
geode::utils::StringMap<std::vector<ObjectModifyInfo>>& getObjectBasesToModify();
4258

4359
template <class, class>
4460
friend class ClassModifyLoad;
61+
62+
template <class, class>
63+
friend class BaseModifyLoad;
64+
4565
friend class ModifyHandler;
4666
};
4767

4868
template <class Derived, class Base>
4969
class ClassModifyLoad {
5070
public:
5171
ClassModifyLoad(geode::ZStringView str, bool useStr) {
52-
std::string name;
53-
if (useStr) name = str;
54-
else name = alpha::utils::cocos::getObjectName<Base>();
55-
56-
ObjectModify::get()->addObjectToModify(name, Derived::modifyPrio(), [](ModifyCCObject<cocos2d::CCObject>* self) {
57-
reinterpret_cast<Derived*>(reinterpret_cast<Base*>(self))->modify();
58-
});
72+
if (useStr) {
73+
ObjectModify::get()->addObjectToModify(str, Derived::modifyPrio(), [](ModifyCCObject<cocos2d::CCObject>* self) {
74+
reinterpret_cast<Derived*>(reinterpret_cast<Base*>(self))->modify();
75+
});
76+
}
77+
else {
78+
ObjectModify::get()->addObjectToModify(alpha::utils::cocos::getObjectName<Base>(), Derived::modifyPrio(), [](ModifyCCObject<cocos2d::CCObject>* self) {
79+
reinterpret_cast<Derived*>(reinterpret_cast<Base*>(self))->modify();
80+
});
81+
}
5982
}
6083
};
6184

@@ -72,6 +95,33 @@ namespace alpha::utils {
7295
static int modifyPrio() { return 0; }
7396
void modify() {}
7497
};
98+
99+
template <class Derived, class Base>
100+
class BaseModifyLoad {
101+
public:
102+
BaseModifyLoad(geode::ZStringView str) {
103+
ObjectModify::get()->addObjectToModifyBase(str, Derived::modifyPrio(), [](ModifyCCObject<cocos2d::CCObject>* self) {
104+
if (ModifyHandler::get()->containsBase<Base>(reinterpret_cast<cocos2d::CCObject*>(self))) {
105+
reinterpret_cast<Derived*>(reinterpret_cast<Base*>(self))->modify();
106+
}
107+
});
108+
}
109+
};
110+
111+
template <class Derived, class Base, geode::utils::string::ConstexprString BaseStr>
112+
struct BaseObjectWrapper : public ModifyCCObject<Base> {
113+
private:
114+
static inline BaseModifyLoad<Derived, Base> s_apply{BaseStr.data()};
115+
static inline auto s_applyRef = &BaseObjectWrapper::s_apply;
116+
117+
public:
118+
using Self = Derived;
119+
120+
ObjectFieldIntermediate<Derived, Base, BaseStr, false> m_fields;
121+
static int modifyPrio() { return 0; }
122+
void modify() {}
123+
};
124+
75125
}
76126

77127
#define ALPHA_MODIFY(baseStr, derived, baseType, useStr) \
@@ -97,4 +147,18 @@ namespace alpha::utils {
97147
GEODE_INVOKE(GEODE_CONCAT(MODIFYNODE, GEODE_NUMBER_OF_ARGS(__VA_ARGS__)), __VA_ARGS__)
98148

99149
#define $classModify(...) \
100-
GEODE_INVOKE(GEODE_CONCAT(MODIFYCLASS, GEODE_NUMBER_OF_ARGS(__VA_ARGS__)), __VA_ARGS__)
150+
GEODE_INVOKE(GEODE_CONCAT(MODIFYCLASS, GEODE_NUMBER_OF_ARGS(__VA_ARGS__)), __VA_ARGS__)
151+
152+
153+
#define ALPHA_MODIFY_BASE(baseStr, derived, baseType) \
154+
GEODE_CONCAT(derived, Dummy); \
155+
struct derived : alpha::utils::BaseObjectWrapper<derived, baseType, #baseStr>
156+
157+
#define ALPHA_MODIFY_BASE_AUTO(baseStr, baseType) \
158+
ALPHA_MODIFY_BASE(baseStr, GEODE_CONCAT(hook, __LINE__), baseType)
159+
160+
#define MODIFYBASE1(base) ALPHA_MODIFY_BASE_AUTO(base, base)
161+
#define MODIFYBASE2(derived, base) ALPHA_MODIFY_BASE(base, derived, base)
162+
163+
#define $baseModify(...) \
164+
GEODE_INVOKE(GEODE_CONCAT(MODIFYBASE, GEODE_NUMBER_OF_ARGS(__VA_ARGS__)), __VA_ARGS__)

include/Utils.hpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,21 @@ namespace alpha::utils {
4040
}
4141

4242
template <class T>
43-
static inline std::string_view getObjectName() {
43+
static inline geode::ZStringView getObjectName() {
4444
#ifdef GEODE_IS_WINDOWS
45-
static const std::string name = []() {
46-
std::string_view tname = typeid(T).name();
47-
if (tname.starts_with("class ")) {
48-
tname.remove_prefix(6);
49-
} else if (tname.starts_with("struct ")) {
50-
tname.remove_prefix(7);
45+
static const std::string name = [] {
46+
const char* name = typeid(T).name();
47+
if (std::strncmp(name, "class ", 6) == 0) {
48+
name += 6;
49+
} else if (std::strncmp(name, "struct ", 7) == 0) {
50+
name += 7;
5151
}
52-
return std::string(tname);
52+
53+
return name;
5354
}();
5455
return name;
5556
#else
56-
static const std::string name = []() {
57+
static const std::string name = [] {
5758
int status = 0;
5859
char* demangled = abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status);
5960
std::string ret;

mod.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"geode": "5.0.0-alpha.1",
2+
"geode": "5.0.0-alpha.2",
33
"gd": {
44
"win": "2.2081",
55
"android": "2.2081",
@@ -8,7 +8,7 @@
88
},
99
"id": "alphalaneous.alphas_geode_utils",
1010
"name": "Alpha's Geode Utils",
11-
"version": "v1.2.0-beta.1",
11+
"version": "v1.2.0-beta.2",
1212
"developer": "Alphalaneous",
1313
"description": "Miscellaneous utilities for Geode Modding",
1414
"api": {

0 commit comments

Comments
 (0)