Skip to content

Commit 5c2d198

Browse files
committed
backwards compatible fix of destructor fix (very ugly)
1 parent add8a57 commit 5c2d198

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@ endif()
169169

170170
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_VISIBILITY_PRESET hidden)
171171
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_20)
172-
target_compile_definitions(${PROJECT_NAME} PUBLIC
173-
GEODE_USE_NEW_DESTRUCTOR_LOCK=1
174-
)
175172

176173
if (WIN32)
177174
if (MSVC)

codegen/src/SourceGen.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,15 @@ auto {class_name}::{function_name}({parameters}){const} -> decltype({function_na
5555
)GEN";
5656

5757
constexpr char const* declare_destructor = R"GEN(
58+
{destructor_hack_begin}
5859
{class_name}::{function_name}({parameters}) {{
5960
// basically we destruct it once by calling the gd function,
6061
// then lock it, so that other gd destructors dont get called
62+
#ifdef GEODE_USE_NEW_DESTRUCTOR_LOCK
6163
if (!geode::DestructorLock::isLocked(this)) {{
64+
#else
65+
if (!cocos2d::CCDestructor::destructorLock().count(this)) {{
66+
#endif
6267
using FunctionType = void(*)({class_name}*{parameter_comma}{parameter_types});
6368
static auto func = wrapFunction({address_inline}, tulip::hook::WrapperMetadata{{
6469
.m_convention = geode::hook::createConvention(tulip::hook::TulipConvention::{convention}),
@@ -69,16 +74,26 @@ auto {class_name}::{function_name}({parameters}){const} -> decltype({function_na
6974
// we need to construct it back so that it uhhh ummm doesnt crash
7075
// while going to the child destructors
7176
auto thing = new (this) {class_name}(geode::CutoffConstructor, sizeof({class_name}));
77+
#ifdef GEODE_USE_NEW_DESTRUCTOR_LOCK
7278
geode::DestructorLock::addLock(this);
79+
#else
80+
cocos2d::CCDestructor::destructorLock().insert({{(void*)this, true}});
81+
#endif
7382
}}
7483
}}
84+
{destructor_hack_end}
7585
)GEN";
7686

7787
constexpr char const* declare_destructor_baseless = R"GEN(
88+
{destructor_hack_begin}
7889
{class_name}::{function_name}({parameters}) {{
7990
// basically we destruct it once by calling the gd function,
8091
// then we release the lock because there are no other destructors after this
92+
#ifdef GEODE_USE_NEW_DESTRUCTOR_LOCK
8193
if (!geode::DestructorLock::isLocked(this)) {{
94+
#else
95+
if (!cocos2d::CCDestructor::destructorLock().count(this)) {{
96+
#endif
8297
using FunctionType = void(*)({class_name}*{parameter_comma}{parameter_types});
8398
static auto func = wrapFunction({address_inline}, tulip::hook::WrapperMetadata{{
8499
.m_convention = geode::hook::createConvention(tulip::hook::TulipConvention::{convention}),
@@ -87,17 +102,27 @@ auto {class_name}::{function_name}({parameters}){const} -> decltype({function_na
87102
reinterpret_cast<FunctionType>(func)(this{parameter_comma}{arguments});
88103
}}
89104
else {{
105+
#ifdef GEODE_USE_NEW_DESTRUCTOR_LOCK
90106
geode::DestructorLock::removeLock(this);
107+
#else
108+
cocos2d::CCDestructor::destructorLock().erase(this);
109+
#endif
91110
}}
92111
}}
112+
{destructor_hack_end}
93113
)GEN";
94114

95115
constexpr char const* declare_constructor = R"GEN(
116+
{destructor_hack_begin}
96117
{class_name}::{function_name}({parameters}) : {class_name}(geode::CutoffConstructor, sizeof({class_name})) {{
97118
// here we construct it as normal as we can, then destruct it
98119
// using the generated functions. this ensures no memory gets leaked
99120
// no crashes :pray:
121+
#ifdef GEODE_USE_NEW_DESTRUCTOR_LOCK
100122
geode::DestructorLock::addLock(this);
123+
#else
124+
cocos2d::CCDestructor::destructorLock().insert({{(void*)this, true}});
125+
#endif
101126
{class_name}::~{unqualified_class_name}();
102127
103128
using FunctionType = void(*)({class_name}*{parameter_comma}{parameter_types});
@@ -107,9 +132,11 @@ auto {class_name}::{function_name}({parameters}){const} -> decltype({function_na
107132
}});
108133
reinterpret_cast<FunctionType>(func)(this{parameter_comma}{arguments});
109134
}}
135+
{destructor_hack_end}
110136
)GEN";
111137

112138
constexpr char const* declare_constructor_begin = R"GEN(
139+
{destructor_hack_begin}
113140
{class_name}::{function_name}({parameters}) {{
114141
using FunctionType = void(*)({class_name}*{parameter_comma}{parameter_types});
115142
static auto func = wrapFunction({address_inline}, tulip::hook::WrapperMetadata{{
@@ -118,6 +145,7 @@ auto {class_name}::{function_name}({parameters}){const} -> decltype({function_na
118145
}});
119146
reinterpret_cast<FunctionType>(func)(this{parameter_comma}{arguments});
120147
}}
148+
{destructor_hack_end}
121149
)GEN";
122150

123151
constexpr char const* declare_unimplemented_error = R"GEN(
@@ -222,13 +250,19 @@ std::string generateBindingSource(Root const& root, bool skipPugixml) {
222250
}
223251
} else if (codegen::getStatus(*fn) != BindStatus::Unbindable || codegen::platformNumber(fn->binds) != -1) {
224252
char const* used_declare_format = nullptr;
253+
char const* destructor_hack_begin = "";
254+
char const* destructor_hack_end = "";
225255

226256
if (codegen::platformNumber(fn->binds) == 0x9999999) {
227257
used_declare_format = format_strings::declare_unimplemented_error;
228258
}
229259
else if (codegen::getStatus(*fn) != BindStatus::NeedsBinding && codegen::getStatus(*fn) != BindStatus::NeedsRebinding) {
230260
continue;
231261
}
262+
if (codegen::getStatus(*fn) == BindStatus::NeedsRebinding && codegen::platform == Platform::Windows) {
263+
destructor_hack_begin = "#ifdef GEODE_USE_NEW_DESTRUCTOR_LOCK";
264+
destructor_hack_end = "#endif\n";
265+
}
232266

233267
if (!used_declare_format) {
234268
switch (fn->prototype.type) {
@@ -264,7 +298,9 @@ std::string generateBindingSource(Root const& root, bool skipPugixml) {
264298
fmt::arg("parameters", codegen::getParameters(fn->prototype)),
265299
fmt::arg("parameter_types", codegen::getParameterTypes(fn->prototype)),
266300
fmt::arg("arguments", codegen::getParameterNames(fn->prototype)),
267-
fmt::arg("parameter_comma", str_if(", ", !fn->prototype.args.empty()))
301+
fmt::arg("parameter_comma", str_if(", ", !fn->prototype.args.empty())),
302+
fmt::arg("destructor_hack_begin", destructor_hack_begin),
303+
fmt::arg("destructor_hack_end", destructor_hack_end)
268304
);
269305
}
270306
}

0 commit comments

Comments
 (0)