Skip to content

Commit 04b3407

Browse files
authored
Merge pull request #1281 from dsnopek/4.1-cherrypicks-5
Cherry-picks for the godot-cpp 4.1 branch - 5th batch
2 parents e389f7a + 9d81331 commit 04b3407

17 files changed

+247
-119
lines changed

.github/workflows/ci.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ jobs:
8080

8181
- name: 🌐 Web (wasm32)
8282
os: ubuntu-20.04
83-
platform: javascript
84-
artifact-name: godot-cpp-javascript-wasm32-release
85-
artifact-path: bin/libgodot-cpp.javascript.template_release.wasm32.a
83+
platform: web
84+
artifact-name: godot-cpp-web-wasm32-release
85+
artifact-path: bin/libgodot-cpp.web.template_release.wasm32.a
8686
run-tests: false
87-
cache-name: javascript-wasm32
87+
cache-name: web-wasm32
8888

8989
env:
9090
SCONS_CACHE: ${{ github.workspace }}/.scons-cache/
@@ -115,7 +115,7 @@ jobs:
115115
sudo apt-get install -qqq build-essential pkg-config
116116
117117
- name: Web dependencies
118-
if: ${{ matrix.platform == 'javascript' }}
118+
if: ${{ matrix.platform == 'web' }}
119119
uses: mymindstorm/setup-emsdk@v12
120120
with:
121121
version: ${{env.EM_VERSION}}

CMakeLists.txt

+21-5
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,22 @@ endif()
7272
# Input from user for GDExtension interface header and the API JSON file
7373
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE STRING "")
7474
set(GODOT_CUSTOM_API_FILE "" CACHE STRING "")
75-
set(FLOAT_PRECISION "single" CACHE STRING "")
76-
if ("${FLOAT_PRECISION}" STREQUAL "double")
77-
add_definitions(-DREAL_T_IS_DOUBLE)
78-
endif()
7975

8076
set(GODOT_GDEXTENSION_API_FILE "${GODOT_GDEXTENSION_DIR}/extension_api.json")
8177
if (NOT "${GODOT_CUSTOM_API_FILE}" STREQUAL "") # User-defined override.
8278
set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}")
8379
endif()
8480

81+
set(FLOAT_PRECISION "single" CACHE STRING "")
82+
if ("${FLOAT_PRECISION}" STREQUAL "double")
83+
add_definitions(-DREAL_T_IS_DOUBLE)
84+
endif()
85+
8586
set(GODOT_COMPILE_FLAGS )
8687

8788
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
8889
# using Visual Studio C++
89-
set(GODOT_COMPILE_FLAGS "/EHsc /utf-8") # /GF /MP
90+
set(GODOT_COMPILE_FLAGS "/utf-8") # /GF /MP
9091

9192
if(CMAKE_BUILD_TYPE MATCHES Debug)
9293
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MDd") # /Od /RTC1 /Zi
@@ -107,6 +108,21 @@ else() # GCC/Clang
107108
endif(CMAKE_BUILD_TYPE MATCHES Debug)
108109
endif()
109110

111+
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
112+
# saves around 20% of binary size and very significant build time (GH-80513).
113+
option(GODOT_DISABLE_EXCEPTIONS OFF "Force disabling exception handling code")
114+
if (GODOT_DISABLE_EXCEPTIONS)
115+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
116+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D_HAS_EXCEPTIONS=0")
117+
else()
118+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-exceptions")
119+
endif()
120+
else()
121+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
122+
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc")
123+
endif()
124+
endif()
125+
110126
# Generate source from the bindings file
111127
find_package(Python3 3.4 REQUIRED) # pathlib should be present
112128
if(GENERATE_TEMPLATE_GET_NODE)

binding_generator.py

+1-63
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False):
130130
if sources:
131131
utility_functions_source_path = source_gen_folder / "variant" / "utility_functions.cpp"
132132
files.append(str(utility_functions_source_path.as_posix()))
133-
register_engine_classes_source_path = source_gen_folder / "register_engine_classes.cpp"
134-
files.append(str(register_engine_classes_source_path.as_posix()))
135133

136134
return files
137135

@@ -1170,10 +1168,6 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
11701168
generate_engine_class_source(class_api, used_classes, fully_used_classes, use_template_get_node)
11711169
)
11721170

1173-
register_engine_classes_filename = Path(output_dir) / "src" / "register_engine_classes.cpp"
1174-
with register_engine_classes_filename.open("w+", encoding="utf-8") as source_file:
1175-
source_file.write(generate_register_engine_classes_source(api))
1176-
11771171
for native_struct in api["native_structures"]:
11781172
struct_name = native_struct["name"]
11791173
snake_struct_name = camel_to_snake(struct_name)
@@ -1248,7 +1242,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
12481242
result.append(f"#include <godot_cpp/{get_include_path(included)}>")
12491243

12501244
if class_name == "EditorPlugin":
1251-
result.append("#include <godot_cpp/templates/vector.hpp>")
1245+
result.append("#include <godot_cpp/classes/editor_plugin_registration.hpp>")
12521246

12531247
if len(fully_used_classes) > 0:
12541248
result.append("")
@@ -1400,30 +1394,6 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
14001394
result.append("};")
14011395
result.append("")
14021396

1403-
if class_name == "EditorPlugin":
1404-
result.append("class EditorPlugins {")
1405-
result.append("private:")
1406-
result.append("\tstatic Vector<StringName> plugin_classes;")
1407-
result.append("")
1408-
result.append("public:")
1409-
result.append("\tstatic void add_plugin_class(const StringName &p_class_name);")
1410-
result.append("\tstatic void remove_plugin_class(const StringName &p_class_name);")
1411-
result.append("\tstatic void deinitialize(GDExtensionInitializationLevel p_level);")
1412-
result.append("")
1413-
1414-
result.append("\ttemplate <class T>")
1415-
result.append("\tstatic void add_by_type() {")
1416-
result.append("\t\tadd_plugin_class(T::get_class_static());")
1417-
result.append("\t}")
1418-
1419-
result.append("\ttemplate <class T>")
1420-
result.append("\tstatic void remove_by_type() {")
1421-
result.append("\t\tremove_plugin_class(T::get_class_static());")
1422-
result.append("\t}")
1423-
1424-
result.append("};")
1425-
result.append("")
1426-
14271397
result.append("} // namespace godot")
14281398
result.append("")
14291399

@@ -1648,38 +1618,6 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
16481618
return "\n".join(result)
16491619

16501620

1651-
def generate_register_engine_classes_source(api):
1652-
includes = []
1653-
registrations = []
1654-
1655-
for class_api in api["classes"]:
1656-
if class_api["name"] == "ClassDB":
1657-
continue
1658-
1659-
class_name = class_api["name"]
1660-
snake_class_name = camel_to_snake(class_name)
1661-
1662-
includes.append(f"#include <godot_cpp/classes/{snake_class_name}.hpp>")
1663-
registrations.append(f"\tClassDB::register_engine_class<{class_name}>();")
1664-
1665-
result = []
1666-
add_header(f"register_engine_classes.cpp", result)
1667-
1668-
result.append("#include <godot_cpp/godot.hpp>")
1669-
result.append("")
1670-
result = result + includes
1671-
result.append("")
1672-
result.append("namespace godot {")
1673-
result.append("")
1674-
result.append("void GDExtensionBinding::register_engine_classes() {")
1675-
result = result + registrations
1676-
result.append("}")
1677-
result.append("")
1678-
result.append("} // namespace godot ")
1679-
1680-
return "\n".join(result)
1681-
1682-
16831621
def generate_global_constants(api, output_dir):
16841622
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes"
16851623
source_gen_folder = Path(output_dir) / "src" / "classes"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**************************************************************************/
2+
/* editor_plugin_registration.hpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef GODOT_EDITOR_PLUGIN_REGISTRATION_HPP
32+
#define GODOT_EDITOR_PLUGIN_REGISTRATION_HPP
33+
34+
#include <godot_cpp/templates/vector.hpp>
35+
36+
namespace godot {
37+
38+
class EditorPlugin;
39+
class StringName;
40+
41+
class EditorPlugins {
42+
private:
43+
static Vector<StringName> plugin_classes;
44+
45+
public:
46+
static void add_plugin_class(const StringName &p_class_name);
47+
static void remove_plugin_class(const StringName &p_class_name);
48+
static void deinitialize(GDExtensionInitializationLevel p_level);
49+
50+
template <class T>
51+
static void add_by_type() {
52+
add_plugin_class(T::get_class_static());
53+
}
54+
template <class T>
55+
static void remove_by_type() {
56+
remove_plugin_class(T::get_class_static());
57+
}
58+
};
59+
60+
} // namespace godot
61+
62+
#endif // GODOT_EDITOR_PLUGIN_REGISTRATION_HPP

include/godot_cpp/classes/wrapped.hpp

+36
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ class Wrapped {
9595
GodotObject *_owner = nullptr;
9696
};
9797

98+
namespace internal {
99+
100+
typedef void (*EngineClassRegistrationCallback)();
101+
void add_engine_class_registration_callback(EngineClassRegistrationCallback p_callback);
102+
void register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks);
103+
void register_engine_classes();
104+
105+
template <class T>
106+
struct EngineClassRegistration {
107+
EngineClassRegistration() {
108+
add_engine_class_registration_callback(&EngineClassRegistration<T>::callback);
109+
}
110+
111+
static void callback() {
112+
register_engine_class(T::get_class_static(), &T::_gde_binding_callbacks);
113+
}
114+
};
115+
116+
} // namespace internal
117+
98118
} // namespace godot
99119

100120
#define GDCLASS(m_class, m_inherits) \
@@ -150,6 +170,8 @@ protected:
150170
} \
151171
\
152172
public: \
173+
typedef m_class self_type; \
174+
\
153175
static void initialize_class() { \
154176
static bool initialized = false; \
155177
if (initialized) { \
@@ -308,6 +330,7 @@ public:
308330
// Don't use this for your classes, use GDCLASS() instead.
309331
#define GDEXTENSION_CLASS_ALIAS(m_class, m_alias_for, m_inherits) \
310332
private: \
333+
inline static ::godot::internal::EngineClassRegistration<m_class> _gde_engine_class_registration_helper; \
311334
void operator=(const m_class &p_rval) {} \
312335
\
313336
protected: \
@@ -351,6 +374,8 @@ protected:
351374
} \
352375
\
353376
public: \
377+
typedef m_class self_type; \
378+
\
354379
static void initialize_class() {} \
355380
\
356381
static ::godot::StringName &get_class_static() { \
@@ -360,6 +385,17 @@ public:
360385
\
361386
static ::godot::StringName &get_parent_class_static() { \
362387
return m_inherits::get_class_static(); \
388+
} \
389+
\
390+
static GDExtensionObjectPtr create(void *data) { \
391+
return nullptr; \
392+
} \
393+
\
394+
static GDExtensionClassInstancePtr recreate(void *data, GDExtensionObjectPtr obj) { \
395+
return nullptr; \
396+
} \
397+
\
398+
static void free(void *data, GDExtensionClassInstancePtr ptr) { \
363399
} \
364400
\
365401
static void *_gde_binding_create_callback(void *p_token, void *p_instance) { \

include/godot_cpp/core/class_db.hpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,10 @@ class ClassDB {
122122
static void register_class(bool p_virtual = false);
123123
template <class T>
124124
static void register_abstract_class();
125-
template <class T>
126-
static void register_engine_class();
125+
126+
_FORCE_INLINE_ static void _register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
127+
instance_binding_callbacks[p_name] = p_callbacks;
128+
}
127129

128130
template <class N, class M, typename... VarArgs>
129131
static MethodBind *bind_method(N p_method_name, M p_method, VarArgs... p_args);
@@ -171,6 +173,7 @@ class ClassDB {
171173

172174
template <class T, bool is_abstract>
173175
void ClassDB::_register_class(bool p_virtual) {
176+
static_assert(TypesAreSame<typename T::self_type, T>::value, "Class not declared properly, please use GDCLASS.");
174177
instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks;
175178

176179
// Register this class within our plugin
@@ -226,11 +229,6 @@ void ClassDB::register_abstract_class() {
226229
ClassDB::_register_class<T, true>();
227230
}
228231

229-
template <class T>
230-
void ClassDB::register_engine_class() {
231-
instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks;
232-
}
233-
234232
template <class N, class M, typename... VarArgs>
235233
MethodBind *ClassDB::bind_method(N p_method_name, M p_method, VarArgs... p_args) {
236234
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.

include/godot_cpp/godot.hpp

-3
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,6 @@ enum ModuleInitializationLevel {
191191
};
192192

193193
class GDExtensionBinding {
194-
private:
195-
static void register_engine_classes();
196-
197194
public:
198195
using Callback = void (*)(ModuleInitializationLevel p_level);
199196

include/godot_cpp/variant/char_string.hpp

+12
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ class CharStringT {
120120
void copy_from(const T *p_cstr);
121121
};
122122

123+
template <>
124+
const char *CharStringT<char>::get_data() const;
125+
126+
template <>
127+
const char16_t *CharStringT<char16_t>::get_data() const;
128+
129+
template <>
130+
const char32_t *CharStringT<char32_t>::get_data() const;
131+
132+
template <>
133+
const wchar_t *CharStringT<wchar_t>::get_data() const;
134+
123135
typedef CharStringT<char> CharString;
124136
typedef CharStringT<char16_t> Char16String;
125137
typedef CharStringT<char32_t> Char32String;

src/classes/editor_plugin.cpp src/classes/editor_plugin_registration.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**************************************************************************/
2-
/* editor_plugin.cpp */
2+
/* editor_plugin_registration.cpp */
33
/**************************************************************************/
44
/* This file is part of: */
55
/* GODOT ENGINE */
@@ -28,9 +28,9 @@
2828
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2929
/**************************************************************************/
3030

31-
#include <godot_cpp/classes/editor_plugin.hpp>
31+
#include <godot_cpp/classes/editor_plugin_registration.hpp>
3232

33-
#include <godot_cpp/variant/string_name.hpp>
33+
#include <godot_cpp/variant/variant.hpp>
3434

3535
namespace godot {
3636

0 commit comments

Comments
 (0)