Skip to content

Commit

Permalink
Merge pull request #1281 from dsnopek/4.1-cherrypicks-5
Browse files Browse the repository at this point in the history
Cherry-picks for the godot-cpp 4.1 branch - 5th batch
  • Loading branch information
akien-mga authored Oct 24, 2023
2 parents e389f7a + 9d81331 commit 04b3407
Show file tree
Hide file tree
Showing 17 changed files with 247 additions and 119 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ jobs:

- name: 🌐 Web (wasm32)
os: ubuntu-20.04
platform: javascript
artifact-name: godot-cpp-javascript-wasm32-release
artifact-path: bin/libgodot-cpp.javascript.template_release.wasm32.a
platform: web
artifact-name: godot-cpp-web-wasm32-release
artifact-path: bin/libgodot-cpp.web.template_release.wasm32.a
run-tests: false
cache-name: javascript-wasm32
cache-name: web-wasm32

env:
SCONS_CACHE: ${{ github.workspace }}/.scons-cache/
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
sudo apt-get install -qqq build-essential pkg-config
- name: Web dependencies
if: ${{ matrix.platform == 'javascript' }}
if: ${{ matrix.platform == 'web' }}
uses: mymindstorm/setup-emsdk@v12
with:
version: ${{env.EM_VERSION}}
Expand Down
26 changes: 21 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,22 @@ endif()
# Input from user for GDExtension interface header and the API JSON file
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE STRING "")
set(GODOT_CUSTOM_API_FILE "" CACHE STRING "")
set(FLOAT_PRECISION "single" CACHE STRING "")
if ("${FLOAT_PRECISION}" STREQUAL "double")
add_definitions(-DREAL_T_IS_DOUBLE)
endif()

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

set(FLOAT_PRECISION "single" CACHE STRING "")
if ("${FLOAT_PRECISION}" STREQUAL "double")
add_definitions(-DREAL_T_IS_DOUBLE)
endif()

set(GODOT_COMPILE_FLAGS )

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

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

# Disable exception handling. Godot doesn't use exceptions anywhere, and this
# saves around 20% of binary size and very significant build time (GH-80513).
option(GODOT_DISABLE_EXCEPTIONS OFF "Force disabling exception handling code")
if (GODOT_DISABLE_EXCEPTIONS)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D_HAS_EXCEPTIONS=0")
else()
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-exceptions")
endif()
else()
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc")
endif()
endif()

# Generate source from the bindings file
find_package(Python3 3.4 REQUIRED) # pathlib should be present
if(GENERATE_TEMPLATE_GET_NODE)
Expand Down
64 changes: 1 addition & 63 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ def get_file_list(api_filepath, output_dir, headers=False, sources=False):
if sources:
utility_functions_source_path = source_gen_folder / "variant" / "utility_functions.cpp"
files.append(str(utility_functions_source_path.as_posix()))
register_engine_classes_source_path = source_gen_folder / "register_engine_classes.cpp"
files.append(str(register_engine_classes_source_path.as_posix()))

return files

Expand Down Expand Up @@ -1170,10 +1168,6 @@ def generate_engine_classes_bindings(api, output_dir, use_template_get_node):
generate_engine_class_source(class_api, used_classes, fully_used_classes, use_template_get_node)
)

register_engine_classes_filename = Path(output_dir) / "src" / "register_engine_classes.cpp"
with register_engine_classes_filename.open("w+", encoding="utf-8") as source_file:
source_file.write(generate_register_engine_classes_source(api))

for native_struct in api["native_structures"]:
struct_name = native_struct["name"]
snake_struct_name = camel_to_snake(struct_name)
Expand Down Expand Up @@ -1248,7 +1242,7 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
result.append(f"#include <godot_cpp/{get_include_path(included)}>")

if class_name == "EditorPlugin":
result.append("#include <godot_cpp/templates/vector.hpp>")
result.append("#include <godot_cpp/classes/editor_plugin_registration.hpp>")

if len(fully_used_classes) > 0:
result.append("")
Expand Down Expand Up @@ -1400,30 +1394,6 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
result.append("};")
result.append("")

if class_name == "EditorPlugin":
result.append("class EditorPlugins {")
result.append("private:")
result.append("\tstatic Vector<StringName> plugin_classes;")
result.append("")
result.append("public:")
result.append("\tstatic void add_plugin_class(const StringName &p_class_name);")
result.append("\tstatic void remove_plugin_class(const StringName &p_class_name);")
result.append("\tstatic void deinitialize(GDExtensionInitializationLevel p_level);")
result.append("")

result.append("\ttemplate <class T>")
result.append("\tstatic void add_by_type() {")
result.append("\t\tadd_plugin_class(T::get_class_static());")
result.append("\t}")

result.append("\ttemplate <class T>")
result.append("\tstatic void remove_by_type() {")
result.append("\t\tremove_plugin_class(T::get_class_static());")
result.append("\t}")

result.append("};")
result.append("")

result.append("} // namespace godot")
result.append("")

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


def generate_register_engine_classes_source(api):
includes = []
registrations = []

for class_api in api["classes"]:
if class_api["name"] == "ClassDB":
continue

class_name = class_api["name"]
snake_class_name = camel_to_snake(class_name)

includes.append(f"#include <godot_cpp/classes/{snake_class_name}.hpp>")
registrations.append(f"\tClassDB::register_engine_class<{class_name}>();")

result = []
add_header(f"register_engine_classes.cpp", result)

result.append("#include <godot_cpp/godot.hpp>")
result.append("")
result = result + includes
result.append("")
result.append("namespace godot {")
result.append("")
result.append("void GDExtensionBinding::register_engine_classes() {")
result = result + registrations
result.append("}")
result.append("")
result.append("} // namespace godot ")

return "\n".join(result)


def generate_global_constants(api, output_dir):
include_gen_folder = Path(output_dir) / "include" / "godot_cpp" / "classes"
source_gen_folder = Path(output_dir) / "src" / "classes"
Expand Down
62 changes: 62 additions & 0 deletions include/godot_cpp/classes/editor_plugin_registration.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**************************************************************************/
/* editor_plugin_registration.hpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef GODOT_EDITOR_PLUGIN_REGISTRATION_HPP
#define GODOT_EDITOR_PLUGIN_REGISTRATION_HPP

#include <godot_cpp/templates/vector.hpp>

namespace godot {

class EditorPlugin;
class StringName;

class EditorPlugins {
private:
static Vector<StringName> plugin_classes;

public:
static void add_plugin_class(const StringName &p_class_name);
static void remove_plugin_class(const StringName &p_class_name);
static void deinitialize(GDExtensionInitializationLevel p_level);

template <class T>
static void add_by_type() {
add_plugin_class(T::get_class_static());
}
template <class T>
static void remove_by_type() {
remove_plugin_class(T::get_class_static());
}
};

} // namespace godot

#endif // GODOT_EDITOR_PLUGIN_REGISTRATION_HPP
36 changes: 36 additions & 0 deletions include/godot_cpp/classes/wrapped.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ class Wrapped {
GodotObject *_owner = nullptr;
};

namespace internal {

typedef void (*EngineClassRegistrationCallback)();
void add_engine_class_registration_callback(EngineClassRegistrationCallback p_callback);
void register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks);
void register_engine_classes();

template <class T>
struct EngineClassRegistration {
EngineClassRegistration() {
add_engine_class_registration_callback(&EngineClassRegistration<T>::callback);
}

static void callback() {
register_engine_class(T::get_class_static(), &T::_gde_binding_callbacks);
}
};

} // namespace internal

} // namespace godot

#define GDCLASS(m_class, m_inherits) \
Expand Down Expand Up @@ -150,6 +170,8 @@ protected:
} \
\
public: \
typedef m_class self_type; \
\
static void initialize_class() { \
static bool initialized = false; \
if (initialized) { \
Expand Down Expand Up @@ -308,6 +330,7 @@ public:
// Don't use this for your classes, use GDCLASS() instead.
#define GDEXTENSION_CLASS_ALIAS(m_class, m_alias_for, m_inherits) \
private: \
inline static ::godot::internal::EngineClassRegistration<m_class> _gde_engine_class_registration_helper; \
void operator=(const m_class &p_rval) {} \
\
protected: \
Expand Down Expand Up @@ -351,6 +374,8 @@ protected:
} \
\
public: \
typedef m_class self_type; \
\
static void initialize_class() {} \
\
static ::godot::StringName &get_class_static() { \
Expand All @@ -360,6 +385,17 @@ public:
\
static ::godot::StringName &get_parent_class_static() { \
return m_inherits::get_class_static(); \
} \
\
static GDExtensionObjectPtr create(void *data) { \
return nullptr; \
} \
\
static GDExtensionClassInstancePtr recreate(void *data, GDExtensionObjectPtr obj) { \
return nullptr; \
} \
\
static void free(void *data, GDExtensionClassInstancePtr ptr) { \
} \
\
static void *_gde_binding_create_callback(void *p_token, void *p_instance) { \
Expand Down
12 changes: 5 additions & 7 deletions include/godot_cpp/core/class_db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ class ClassDB {
static void register_class(bool p_virtual = false);
template <class T>
static void register_abstract_class();
template <class T>
static void register_engine_class();

_FORCE_INLINE_ static void _register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks) {
instance_binding_callbacks[p_name] = p_callbacks;
}

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

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

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

template <class T>
void ClassDB::register_engine_class() {
instance_binding_callbacks[T::get_class_static()] = &T::_gde_binding_callbacks;
}

template <class N, class M, typename... VarArgs>
MethodBind *ClassDB::bind_method(N p_method_name, M p_method, VarArgs... p_args) {
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
Expand Down
3 changes: 0 additions & 3 deletions include/godot_cpp/godot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,6 @@ enum ModuleInitializationLevel {
};

class GDExtensionBinding {
private:
static void register_engine_classes();

public:
using Callback = void (*)(ModuleInitializationLevel p_level);

Expand Down
12 changes: 12 additions & 0 deletions include/godot_cpp/variant/char_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ class CharStringT {
void copy_from(const T *p_cstr);
};

template <>
const char *CharStringT<char>::get_data() const;

template <>
const char16_t *CharStringT<char16_t>::get_data() const;

template <>
const char32_t *CharStringT<char32_t>::get_data() const;

template <>
const wchar_t *CharStringT<wchar_t>::get_data() const;

typedef CharStringT<char> CharString;
typedef CharStringT<char16_t> Char16String;
typedef CharStringT<char32_t> Char32String;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**************************************************************************/
/* editor_plugin.cpp */
/* editor_plugin_registration.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
Expand Down Expand Up @@ -28,9 +28,9 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include <godot_cpp/classes/editor_plugin.hpp>
#include <godot_cpp/classes/editor_plugin_registration.hpp>

#include <godot_cpp/variant/string_name.hpp>
#include <godot_cpp/variant/variant.hpp>

namespace godot {

Expand Down
Loading

0 comments on commit 04b3407

Please sign in to comment.