Skip to content

Commit 0e97e2b

Browse files
committed
Use complete Godot version for preprocessing conditionals
1 parent 9ae37ac commit 0e97e2b

14 files changed

Lines changed: 73 additions & 71 deletions

File tree

binding_generator.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -376,23 +376,23 @@ def generate_gdextension_interface_loader_header(data):
376376
versions = sorted(data.keys())
377377
for version in versions:
378378
(major, minor) = version.split(".")
379-
result.append(f"// Godot 4.{minor} or newer.")
380-
result.append(f"#if GODOT_VERSION_MINOR >= {minor}")
379+
result.append(f"// Godot {major}.{minor} or newer.")
380+
result.append(f"#if GODOT_VERSION >= 0x{major.zfill(2)}{minor.zfill(2)}00")
381381

382382
for func in data[version]:
383383
name = func["name"]
384384
fn = gdextension_interface_type_name(name)
385385

386386
if "deprecated" in func:
387387
(deprecated_major, deprecated_minor) = func["deprecated"]["since"].split(".")
388-
result.append(f"#if !defined(DISABLE_DEPRECATED) || GODOT_VERSION_MINOR < {deprecated_minor}")
388+
result.append(f"#if !defined(DISABLE_DEPRECATED) || GODOT_VERSION < 0x{deprecated_major.zfill(2)}{deprecated_minor.zfill(2)}00")
389389

390390
result.append(f'extern "C" {fn} {name};')
391391

392392
if "deprecated" in func:
393393
result.append("#endif")
394394

395-
result.append(f"#endif // GODOT_VERSION_MINOR >= {minor}")
395+
result.append(f"#endif // GODOT_VERSION >= 0x{major.zfill(2)}{minor.zfill(2)}00")
396396
result.append("")
397397

398398
result.append("} // namespace gdextension_interface")
@@ -426,23 +426,23 @@ def generate_gdextension_interface_loader_source(data):
426426

427427
for version in versions:
428428
(major, minor) = version.split(".")
429-
result.append(f"// Godot 4.{minor} or newer.")
430-
result.append(f"#if GODOT_VERSION_MINOR >= {minor}")
429+
result.append(f"// Godot {major}.{minor} or newer.")
430+
result.append(f"#if GODOT_VERSION >= 0x{major.zfill(2)}{minor.zfill(2)}00")
431431

432432
for func in data[version]:
433433
name = func["name"]
434434
fn = gdextension_interface_type_name(name)
435435

436436
if "deprecated" in func:
437437
(deprecated_major, deprecated_minor) = func["deprecated"]["since"].split(".")
438-
result.append(f"#if !defined(DISABLE_DEPRECATED) || GODOT_VERSION_MINOR < {deprecated_minor}")
438+
result.append(f"#if !defined(DISABLE_DEPRECATED) || GODOT_VERSION < 0x{deprecated_major.zfill(2)}{deprecated_minor.zfill(2)}00")
439439

440440
result.append(f"{fn} {name} = nullptr;")
441441

442442
if "deprecated" in func:
443443
result.append("#endif")
444444

445-
result.append(f"#endif // GODOT_VERSION_MINOR >= {minor}")
445+
result.append(f"#endif // GODOT_VERSION >= 0x{major.zfill(2)}{minor.zfill(2)}00")
446446
result.append("")
447447

448448
result.append("} // namespace gdextension_interface")
@@ -454,8 +454,8 @@ def generate_gdextension_interface_loader_source(data):
454454

455455
for version in versions:
456456
(major, minor) = version.split(".")
457-
result.append(f"\t// Godot 4.{minor} or newer.")
458-
result.append(f"#if GODOT_VERSION_MINOR >= {minor}")
457+
result.append(f"\t// Godot {major}.{minor} or newer.")
458+
result.append(f"#if GODOT_VERSION >= 0x{major.zfill(2)}{minor.zfill(2)}00")
459459

460460
for func in data[version]:
461461
name = func["name"]
@@ -466,14 +466,14 @@ def generate_gdextension_interface_loader_source(data):
466466

467467
if "deprecated" in func:
468468
(deprecated_major, deprecated_minor) = func["deprecated"]["since"].split(".")
469-
result.append(f"#if !defined(DISABLE_DEPRECATED) || GODOT_VERSION_MINOR < {deprecated_minor}")
469+
result.append(f"#if !defined(DISABLE_DEPRECATED) || GODOT_VERSION < 0x{deprecated_major.zfill(2)}{deprecated_minor.zfill(2)}00")
470470

471471
result.append(f"\tLOAD_PROC_ADDRESS({name}, {fn});")
472472

473473
if "deprecated" in func:
474474
result.append("#endif")
475475

476-
result.append(f"#endif // GODOT_VERSION_MINOR >= {minor}")
476+
result.append(f"#endif // GODOT_VERSION >= 0x{major.zfill(2)}{minor.zfill(2)}00")
477477
result.append("")
478478

479479
result.append("\treturn true;")
@@ -1138,7 +1138,7 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
11381138
if class_name == "Dictionary":
11391139
result.append("\tconst Variant &operator[](const Variant &p_key) const;")
11401140
result.append("\tVariant &operator[](const Variant &p_key);")
1141-
result.append("#if GODOT_VERSION_MINOR >= 4")
1141+
result.append("#if GODOT_VERSION >= 0x040400")
11421142
result.append(
11431143
"\tvoid set_typed(uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script);"
11441144
)
@@ -2332,6 +2332,8 @@ def generate_version_header(api, output_dir):
23322332

23332333
header.append("")
23342334

2335+
header.append("#define GODOT_VERSION 0x10000 * GODOT_VERSION_MAJOR + 0x100 * GODOT_VERSION_MINOR + GODOT_VERSION_PATCH")
2336+
23352337
with header_file_path.open("w+", encoding="utf-8") as header_file:
23362338
header_file.write("\n".join(header))
23372339

include/godot_cpp/core/class_db.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,21 @@ class ClassDB {
111111
static void _register_class(bool p_virtual = false, bool p_exposed = true, bool p_runtime = false);
112112

113113
template <typename T>
114-
#if GODOT_VERSION_MINOR >= 4
114+
#if GODOT_VERSION >= 0x040400
115115
static GDExtensionObjectPtr _create_instance_func(void *data, GDExtensionBool p_notify_postinitialize) {
116116
#else
117117
static GDExtensionObjectPtr _create_instance_func(void *data) {
118-
#endif // GODOT_VERSION_MINOR >= 4
118+
#endif // GODOT_VERSION >= 0x040400
119119
if constexpr (!std::is_abstract_v<T>) {
120120
Wrapped::_set_construct_info<T>();
121-
#if GODOT_VERSION_MINOR >= 4
121+
#if GODOT_VERSION >= 0x040400
122122
T *new_object = new ("", "") T;
123123
if (p_notify_postinitialize) {
124124
new_object->_postinitialize();
125125
}
126126
#else
127127
T *new_object = memnew(T);
128-
#endif // GODOT_VERSION_MINOR >= 4
128+
#endif // GODOT_VERSION >= 0x040400
129129
return new_object->_owner;
130130
} else {
131131
return nullptr;
@@ -203,11 +203,11 @@ class ClassDB {
203203

204204
static MethodBind *get_method(const StringName &p_class, const StringName &p_method);
205205

206-
#if GODOT_VERSION_MINOR >= 4
206+
#if GODOT_VERSION >= 0x040400
207207
static GDExtensionClassCallVirtual get_virtual_func(void *p_userdata, GDExtensionConstStringNamePtr p_name, uint32_t p_hash);
208208
#else
209209
static GDExtensionClassCallVirtual get_virtual_func(void *p_userdata, GDExtensionConstStringNamePtr p_name);
210-
#endif // GODOT_VERSION_MINOR >= 4
210+
#endif // GODOT_VERSION >= 0x040400
211211

212212
static const GDExtensionInstanceBindingCallbacks *get_instance_binding_callbacks(const StringName &p_class);
213213

@@ -255,9 +255,9 @@ void ClassDB::_register_class(bool p_virtual, bool p_exposed, bool p_runtime) {
255255
class_register_order.push_back(cl.name);
256256

257257
// Register this class with Godot
258-
#if GODOT_VERSION_MINOR >= 5
258+
#if GODOT_VERSION >= 0x040500
259259
GDExtensionClassCreationInfo5 class_info = {
260-
#elif GODOT_VERSION_MINOR >= 4
260+
#elif GODOT_VERSION >= 0x040400
261261
GDExtensionClassCreationInfo4 class_info = {
262262
#else
263263
GDExtensionClassCreationInfo3 class_info = {
@@ -266,9 +266,9 @@ void ClassDB::_register_class(bool p_virtual, bool p_exposed, bool p_runtime) {
266266
is_abstract, // GDExtensionBool is_abstract;
267267
p_exposed, // GDExtensionBool is_exposed;
268268
p_runtime, // GDExtensionBool is_runtime;
269-
#if GODOT_VERSION_MINOR >= 4
269+
#if GODOT_VERSION >= 0x040400
270270
nullptr, // GDExtensionConstStringPtr icon_path;
271-
#endif // GODOT_VERSION_MINOR >= 4
271+
#endif // GODOT_VERSION >= 0x040400
272272
T::set_bind, // GDExtensionClassSet set_func;
273273
T::get_bind, // GDExtensionClassGet get_func;
274274
T::has_get_property_list() ? T::get_property_list_bind : nullptr, // GDExtensionClassGetPropertyList get_property_list_func;
@@ -286,15 +286,15 @@ void ClassDB::_register_class(bool p_virtual, bool p_exposed, bool p_runtime) {
286286
&ClassDB::get_virtual_func, // GDExtensionClassGetVirtual get_virtual_func;
287287
nullptr, // GDExtensionClassGetVirtualCallData get_virtual_call_data_func;
288288
nullptr, // GDExtensionClassCallVirtualWithData call_virtual_func;
289-
#if GODOT_VERSION_MINOR <= 3
289+
#if GODOT_VERSION <= 0x040300
290290
nullptr, // GDExtensionClassGetRID get_rid;
291-
#endif // GODOT_VERSION_MINOR <= 3
291+
#endif // GODOT_VERSION <= 0x040300
292292
(void *)&T::get_class_static(), // void *class_userdata;
293293
};
294294

295-
#if GODOT_VERSION_MINOR >= 5
295+
#if GODOT_VERSION >= 0x040500
296296
::godot::gdextension_interface::classdb_register_extension_class5(::godot::gdextension_interface::library, cl.name._native_ptr(), cl.parent_name._native_ptr(), &class_info);
297-
#elif GODOT_VERSION_MINOR >= 4
297+
#elif GODOT_VERSION >= 0x040400
298298
::godot::gdextension_interface::classdb_register_extension_class4(::godot::gdextension_interface::library, cl.name._native_ptr(), cl.parent_name._native_ptr(), &class_info);
299299
#else
300300
::godot::gdextension_interface::classdb_register_extension_class3(::godot::gdextension_interface::library, cl.name._native_ptr(), cl.parent_name._native_ptr(), &class_info);

include/godot_cpp/godot.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extern "C" GDExtensionInterfaceGetProcAddress get_proc_address;
4040
extern "C" GDExtensionClassLibraryPtr library;
4141
extern "C" void *token;
4242

43-
#if GODOT_VERSION_MINOR >= 5
43+
#if GODOT_VERSION >= 0x040500
4444
extern "C" GDExtensionGodotVersion2 godot_version;
4545
#else
4646
extern "C" GDExtensionGodotVersion godot_version;
@@ -73,7 +73,7 @@ class GDExtensionBinding {
7373
GDExtensionInitializationLevel minimum_initialization_level = GDEXTENSION_INITIALIZATION_CORE;
7474
Callback init_callback = nullptr;
7575
Callback terminate_callback = nullptr;
76-
#if GODOT_VERSION_MINOR >= 5
76+
#if GODOT_VERSION >= 0x040500
7777
GDExtensionMainLoopCallbacks main_loop_callbacks = {};
7878

7979
inline bool has_main_loop_callbacks() const {
@@ -114,7 +114,7 @@ class GDExtensionBinding {
114114
void register_terminator(Callback p_init) const;
115115
void set_minimum_library_initialization_level(ModuleInitializationLevel p_level) const;
116116

117-
#if GODOT_VERSION_MINOR >= 5
117+
#if GODOT_VERSION >= 0x040500
118118
// Register a callback that is called after all initialization levels when Godot is fully initialized.
119119
void register_startup_callback(GDExtensionMainLoopStartupCallback p_callback) const;
120120
// Register a callback that is called for every process frame. This will run after all `_process()` methods on Node, and before `ScriptServer::frame()`.

include/godot_cpp/variant/typed_dictionary.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
#include <godot_cpp/core/version.hpp>
3434

35-
#if GODOT_VERSION_MINOR >= 4
35+
#if GODOT_VERSION >= 0x040400
3636

3737
#include <godot_cpp/core/type_info.hpp>
3838
#include <godot_cpp/templates/pair.hpp>
@@ -470,4 +470,4 @@ MAKE_TYPED_DICTIONARY_INFO(IPAddress, Variant::STRING)
470470

471471
#else
472472
#error "TypedDictionary is only supported when targeting Godot 4.4+"
473-
#endif // GODOT_VERSION_MINOR >= 4
473+
#endif // GODOT_VERSION >= 0x040400

include/godot_cpp/variant/variant_internal.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
#include <godot_cpp/core/version.hpp>
3434

35-
#if GODOT_VERSION_MINOR >= 4
35+
#if GODOT_VERSION >= 0x040400
3636

3737
#include <gdextension_interface.h>
3838
#include <godot_cpp/variant/variant.hpp>
@@ -509,4 +509,4 @@ struct VariantDefaultInitializer {
509509

510510
} // namespace godot
511511

512-
#endif // GODOT_VERSION_MINOR >= 4
512+
#endif // GODOT_VERSION >= 0x040400

src/classes/wrapped.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void Wrapped::_postinitialize() {
6060
Wrapped::_constructing_mutex.unlock();
6161
#endif
6262

63-
#if GODOT_VERSION_MINOR >= 4
63+
#if GODOT_VERSION >= 0x040400
6464
Object *obj = dynamic_cast<Object *>(this);
6565
if (obj) {
6666
obj->notification(Object::NOTIFICATION_POSTINITIALIZE);
@@ -70,7 +70,7 @@ void Wrapped::_postinitialize() {
7070
if (_is_extension_class()) {
7171
_notificationv(Object::NOTIFICATION_POSTINITIALIZE);
7272
}
73-
#endif // GODOT_VERSION_MINOR >= 4
73+
#endif // GODOT_VERSION >= 0x040400
7474
}
7575

7676
Wrapped::Wrapped(const StringName &p_godot_class) {
@@ -81,7 +81,7 @@ Wrapped::Wrapped(const StringName &p_godot_class) {
8181
} else
8282
#endif
8383
{
84-
#if GODOT_VERSION_MINOR >= 4
84+
#if GODOT_VERSION >= 0x040400
8585
_owner = ::godot::gdextension_interface::classdb_construct_object2(reinterpret_cast<GDExtensionConstStringNamePtr>(p_godot_class._native_ptr()));
8686
#else
8787
_owner = ::godot::gdextension_interface::classdb_construct_object(reinterpret_cast<GDExtensionConstStringNamePtr>(p_godot_class._native_ptr()));

src/core/class_db.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ void ClassDB::bind_integer_constant(const StringName &p_class_name, const String
284284
::godot::gdextension_interface::classdb_register_extension_class_integer_constant(::godot::gdextension_interface::library, p_class_name._native_ptr(), p_enum_name._native_ptr(), p_constant_name._native_ptr(), p_constant_value, p_is_bitfield);
285285
}
286286

287-
#if GODOT_VERSION_MINOR >= 4
287+
#if GODOT_VERSION >= 0x040400
288288
GDExtensionClassCallVirtual ClassDB::get_virtual_func(void *p_userdata, GDExtensionConstStringNamePtr p_name, uint32_t p_hash) {
289289
#else
290290
GDExtensionClassCallVirtual ClassDB::get_virtual_func(void *p_userdata, GDExtensionConstStringNamePtr p_name) {
291-
#endif // GODOT_VERSION_MINOR >= 4
291+
#endif // GODOT_VERSION >= 0x040400
292292
// This is called by Godot the first time it calls a virtual function, and it caches the result, per object instance.
293293
// Because of this, it can happen from different threads at once.
294294
// It should be ok not using any mutex as long as we only READ data.
@@ -304,11 +304,11 @@ GDExtensionClassCallVirtual ClassDB::get_virtual_func(void *p_userdata, GDExtens
304304
while (type != nullptr) {
305305
AHashMap<StringName, ClassInfo::VirtualMethod>::ConstIterator method_it = type->virtual_methods.find(*name);
306306

307-
#if GODOT_VERSION_MINOR >= 4
307+
#if GODOT_VERSION >= 0x040400
308308
if (method_it != type->virtual_methods.end() && method_it->value.hash == p_hash) {
309309
#else
310310
if (method_it != type->virtual_methods.end()) {
311-
#endif // GODOT_VERSION_MINOR >= 4
311+
#endif // GODOT_VERSION >= 0x040400
312312
return method_it->value.func;
313313
}
314314

src/core/memory.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
namespace godot {
3636

3737
void *Memory::alloc_static(size_t p_bytes, bool p_pad_align) {
38-
#if GODOT_VERSION_MINOR >= 6
38+
#if GODOT_VERSION >= 0x040600
3939
return ::godot::gdextension_interface::mem_alloc2(p_bytes, p_pad_align);
4040
#else
4141
void *mem = ::godot::gdextension_interface::mem_alloc(p_bytes + (p_pad_align ? DATA_OFFSET : 0));
@@ -58,7 +58,7 @@ void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
5858
return nullptr;
5959
}
6060

61-
#if GODOT_VERSION_MINOR >= 6
61+
#if GODOT_VERSION >= 0x040600
6262
return ::godot::gdextension_interface::mem_realloc2(p_memory, p_bytes, p_pad_align);
6363
#else
6464
uint8_t *mem = (uint8_t *)p_memory;
@@ -74,7 +74,7 @@ void *Memory::realloc_static(void *p_memory, size_t p_bytes, bool p_pad_align) {
7474
}
7575

7676
void Memory::free_static(void *p_ptr, bool p_pad_align) {
77-
#if GODOT_VERSION_MINOR >= 6
77+
#if GODOT_VERSION >= 0x040600
7878
::godot::gdextension_interface::mem_free2(p_ptr, p_pad_align);
7979
#else
8080
uint8_t *mem = (uint8_t *)p_ptr;

src/godot.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ GDExtensionInterfaceGetProcAddress get_proc_address = nullptr;
5050
GDExtensionClassLibraryPtr library = nullptr;
5151
void *token = nullptr;
5252

53-
#if GODOT_VERSION_MINOR >= 5
53+
#if GODOT_VERSION >= 0x040500
5454
GDExtensionGodotVersion2 godot_version = {};
5555
#else
5656
GDExtensionGodotVersion godot_version = {};
@@ -146,7 +146,7 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
146146
::godot::gdextension_interface::library = p_library;
147147
::godot::gdextension_interface::token = p_library;
148148

149-
#if GODOT_VERSION_MINOR >= 5
149+
#if GODOT_VERSION >= 0x040500
150150
LOAD_PROC_ADDRESS(get_godot_version2, GDExtensionInterfaceGetGodotVersion2);
151151
::godot::gdextension_interface::get_godot_version2(&::godot::gdextension_interface::godot_version);
152152
#else
@@ -213,14 +213,14 @@ void GDExtensionBinding::initialize_level(void *p_userdata, GDExtensionInitializ
213213
}
214214
level_initialized[p_level]++;
215215

216-
#if GODOT_VERSION_MINOR >= 5
216+
#if GODOT_VERSION >= 0x040500
217217
if ((ModuleInitializationLevel)p_level == MODULE_INITIALIZATION_LEVEL_CORE && init_data && init_data->has_main_loop_callbacks()) {
218218
::godot::gdextension_interface::register_main_loop_callbacks(::godot::gdextension_interface::library, &init_data->main_loop_callbacks);
219219
}
220220
#endif
221221

222222
if ((ModuleInitializationLevel)p_level == MODULE_INITIALIZATION_LEVEL_EDITOR) {
223-
#if GODOT_VERSION_MINOR >= 5
223+
#if GODOT_VERSION >= 0x040500
224224
::godot::gdextension_interface::editor_register_get_classes_used_callback(::godot::gdextension_interface::library, &ClassDB::_editor_get_classes_used_callback);
225225
#endif
226226

@@ -291,7 +291,7 @@ void GDExtensionBinding::InitObject::set_minimum_library_initialization_level(Mo
291291
init_data->minimum_initialization_level = static_cast<GDExtensionInitializationLevel>(p_level);
292292
}
293293

294-
#if GODOT_VERSION_MINOR >= 5
294+
#if GODOT_VERSION >= 0x040500
295295
void GDExtensionBinding::InitObject::register_startup_callback(GDExtensionMainLoopStartupCallback p_callback) const {
296296
init_data->main_loop_callbacks.startup_func = p_callback;
297297
}

src/variant/packed_arrays.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,12 +250,12 @@ Variant &Dictionary::operator[](const Variant &p_key) {
250250
return *var;
251251
}
252252

253-
#if GODOT_VERSION_MINOR >= 4
253+
#if GODOT_VERSION >= 0x040400
254254
void Dictionary::set_typed(uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script) {
255255
// p_key_type/p_value_type are not Variant::Type so that header doesn't depend on <variant.hpp>.
256256
::godot::gdextension_interface::dictionary_set_typed((GDExtensionTypePtr *)this, (GDExtensionVariantType)p_key_type, (GDExtensionConstStringNamePtr)&p_key_class_name, (GDExtensionConstVariantPtr)&p_key_script,
257257
(GDExtensionVariantType)p_value_type, (GDExtensionConstStringNamePtr)&p_value_class_name, (GDExtensionConstVariantPtr)&p_value_script);
258258
}
259-
#endif // GODOT_VERSION_MINOR >= 4
259+
#endif // GODOT_VERSION >= 0x040400
260260

261261
} // namespace godot

0 commit comments

Comments
 (0)