Skip to content

Commit 6c44c80

Browse files
kisgkonczgzorbathut
committed
LibGodot: Core - Build Godot Engine as a Library
* Add a new GodotInstance GDCLASS that provides startup and iteration commands to control a Godot instance. * Adds a libgodot_create_godot_instance entry point that creates a new Godot instance and returns a GodotInstance object. * Adds a libgodot_destroy_godot_instance entry point that destroys the Godot instance. Sample Apps: https://github.com/migeran/libgodot_project Developed by [Migeran](https://migeran.com) Sponsors & Acknowledgements: * Initial development sponsored by [Smirk Software](https://www.smirk.gg/) * Rebasing to Godot 4.3 and further development sponsored by [Xibbon Inc.](https://xibbon.com) * The GDExtension registration of the host process & build system changes were based on @Faolan-Rad's LibGodot PR: #72883 * Thanks to Ben Rog-Wilhelm (Zorbathut) for creating a smaller, minimal version for easier review. * Thanks to Ernest Lee (iFire) for his support Co-Authored-By: Gabor Koncz <[email protected]> Co-Authored-By: Ben Rog-Wilhelm <[email protected]>
1 parent c01c7b8 commit 6c44c80

23 files changed

+752
-16
lines changed

SConstruct

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ opts.Add(
264264
True,
265265
)
266266
)
267+
opts.Add(
268+
EnumVariable(
269+
"library_type",
270+
"Build library type",
271+
"executable",
272+
("executable", "static_library", "shared_library"),
273+
)
274+
)
267275

268276
# Thirdparty libraries
269277
opts.Add(BoolVariable("builtin_brotli", "Use the built-in Brotli library", True))
@@ -556,6 +564,13 @@ if not env["deprecated"]:
556564
if env["precision"] == "double":
557565
env.Append(CPPDEFINES=["REAL_T_IS_DOUBLE"])
558566

567+
# Library Support
568+
if env["library_type"] != "executable":
569+
if "library" not in env.get("supported", []):
570+
print_error(f"Library builds unsupported for {env['platform']}")
571+
Exit(255)
572+
env.Append(CPPDEFINES=["LIBGODOT_ENABLED"])
573+
559574
# Default num_jobs to local cpu count if not user specified.
560575
# SCons has a peculiarity where user-specified options won't be overridden
561576
# by SetOption, so we can rely on this to know if we should use our default.

core/config/project_settings.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ const PackedStringArray ProjectSettings::get_required_features() {
8383
// Returns the features supported by this build of Godot. Includes all required features.
8484
const PackedStringArray ProjectSettings::_get_supported_features() {
8585
PackedStringArray features = get_required_features();
86+
87+
#ifdef LIBGODOT_ENABLED
88+
features.append("LibGodot");
89+
#endif
90+
8691
#ifdef MODULE_MONO_ENABLED
8792
features.append("C#");
8893
#endif
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**************************************************************************/
2+
/* gdextension_function_loader.cpp */
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+
#include "gdextension_function_loader.h"
32+
33+
#include "gdextension.h"
34+
35+
Error GDExtensionFunctionLoader::open_library(const String &p_path) {
36+
ERR_FAIL_COND_V_MSG(!p_path.begins_with("libgodot://"), ERR_FILE_NOT_FOUND, "Function based GDExtensions should have a path starting with libgodot://");
37+
ERR_FAIL_COND_V_MSG(!initialization_function, ERR_DOES_NOT_EXIST, "Initialization function is required for function based GDExtensions.");
38+
39+
library_path = p_path;
40+
41+
return OK;
42+
}
43+
44+
Error GDExtensionFunctionLoader::initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) {
45+
ERR_FAIL_COND_V_MSG(!initialization_function, ERR_DOES_NOT_EXIST, "Initialization function is required for function based GDExtensions.");
46+
GDExtensionBool ret = initialization_function(p_get_proc_address, p_extension.ptr(), r_initialization);
47+
48+
if (ret) {
49+
return OK;
50+
} else {
51+
ERR_FAIL_V_MSG(FAILED, "GDExtension initialization function for '" + library_path + "' returned an error.");
52+
}
53+
}
54+
55+
void GDExtensionFunctionLoader::close_library() {
56+
initialization_function = nullptr;
57+
library_path.clear();
58+
}
59+
60+
bool GDExtensionFunctionLoader::is_library_open() const {
61+
return !library_path.is_empty();
62+
}
63+
64+
bool GDExtensionFunctionLoader::has_library_changed() const {
65+
return false;
66+
}
67+
68+
bool GDExtensionFunctionLoader::library_exists() const {
69+
return true;
70+
}
71+
72+
void GDExtensionFunctionLoader::set_initialization_function(GDExtensionInitializationFunction p_initialization_function) {
73+
initialization_function = p_initialization_function;
74+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**************************************************************************/
2+
/* gdextension_function_loader.h */
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+
#pragma once
32+
33+
#include "core/extension/gdextension_loader.h"
34+
#include "core/os/shared_object.h"
35+
36+
class GDExtension;
37+
38+
class GDExtensionFunctionLoader : public GDExtensionLoader {
39+
friend class GDExtensionManager;
40+
friend class GDExtension;
41+
42+
String library_path;
43+
GDExtensionInitializationFunction initialization_function = nullptr;
44+
45+
public:
46+
virtual Error open_library(const String &p_path) override;
47+
virtual Error initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) override;
48+
virtual void close_library() override;
49+
virtual bool is_library_open() const override;
50+
virtual bool has_library_changed() const override;
51+
virtual bool library_exists() const override;
52+
53+
void set_initialization_function(GDExtensionInitializationFunction p_initialization_function);
54+
};

core/extension/gdextension_manager.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "gdextension_manager.h"
3232

33+
#include "core/extension/gdextension_function_loader.h"
3334
#include "core/extension/gdextension_library_loader.h"
3435
#include "core/extension/gdextension_special_compat_hashes.h"
3536
#include "core/io/dir_access.h"
@@ -114,7 +115,14 @@ GDExtensionManager::LoadStatus GDExtensionManager::load_extension(const String &
114115

115116
Ref<GDExtensionLibraryLoader> loader;
116117
loader.instantiate();
117-
return GDExtensionManager::get_singleton()->load_extension_with_loader(p_path, loader);
118+
return load_extension_with_loader(p_path, loader);
119+
}
120+
121+
GDExtensionManager::LoadStatus GDExtensionManager::load_extension_from_function(const String &p_path, GDExtensionConstPtr<const GDExtensionInitializationFunction> p_init_func) {
122+
Ref<GDExtensionFunctionLoader> func_loader;
123+
func_loader.instantiate();
124+
func_loader->set_initialization_function((GDExtensionInitializationFunction)*p_init_func.data);
125+
return load_extension_with_loader(p_path, func_loader);
118126
}
119127

120128
GDExtensionManager::LoadStatus GDExtensionManager::load_extension_with_loader(const String &p_path, const Ref<GDExtensionLoader> &p_loader) {
@@ -454,6 +462,7 @@ GDExtensionManager *GDExtensionManager::get_singleton() {
454462

455463
void GDExtensionManager::_bind_methods() {
456464
ClassDB::bind_method(D_METHOD("load_extension", "path"), &GDExtensionManager::load_extension);
465+
ClassDB::bind_method(D_METHOD("load_extension_from_function", "path", "init_func"), &GDExtensionManager::load_extension_from_function);
457466
ClassDB::bind_method(D_METHOD("reload_extension", "path"), &GDExtensionManager::reload_extension);
458467
ClassDB::bind_method(D_METHOD("unload_extension", "path"), &GDExtensionManager::unload_extension);
459468
ClassDB::bind_method(D_METHOD("is_extension_loaded", "path"), &GDExtensionManager::is_extension_loaded);

core/extension/gdextension_manager.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#pragma once
3232

3333
#include "core/extension/gdextension.h"
34+
#include "core/variant/native_ptr.h"
35+
36+
GDVIRTUAL_NATIVE_PTR(GDExtensionInitializationFunction)
3437

3538
class GDExtensionManager : public Object {
3639
GDCLASS(GDExtensionManager, Object);
@@ -66,6 +69,7 @@ class GDExtensionManager : public Object {
6669

6770
public:
6871
LoadStatus load_extension(const String &p_path);
72+
LoadStatus load_extension_from_function(const String &p_path, GDExtensionConstPtr<const GDExtensionInitializationFunction> p_init_func);
6973
LoadStatus load_extension_with_loader(const String &p_path, const Ref<GDExtensionLoader> &p_loader);
7074
LoadStatus reload_extension(const String &p_path);
7175
LoadStatus unload_extension(const String &p_path);

core/extension/godot_instance.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**************************************************************************/
2+
/* godot_instance.cpp */
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+
#include "godot_instance.h"
32+
33+
#include "core/extension/gdextension_manager.h"
34+
#include "core/os/main_loop.h"
35+
#include "main/main.h"
36+
#include "servers/display/display_server.h"
37+
38+
void GodotInstance::_bind_methods() {
39+
ClassDB::bind_method(D_METHOD("start"), &GodotInstance::start);
40+
ClassDB::bind_method(D_METHOD("is_started"), &GodotInstance::is_started);
41+
ClassDB::bind_method(D_METHOD("iteration"), &GodotInstance::iteration);
42+
ClassDB::bind_method(D_METHOD("focus_in"), &GodotInstance::focus_in);
43+
ClassDB::bind_method(D_METHOD("focus_out"), &GodotInstance::focus_out);
44+
ClassDB::bind_method(D_METHOD("pause"), &GodotInstance::pause);
45+
ClassDB::bind_method(D_METHOD("resume"), &GodotInstance::resume);
46+
}
47+
48+
GodotInstance::GodotInstance() {
49+
}
50+
51+
GodotInstance::~GodotInstance() {
52+
}
53+
54+
bool GodotInstance::initialize(GDExtensionInitializationFunction p_init_func) {
55+
print_verbose("Godot Instance initialization");
56+
GDExtensionManager *gdextension_manager = GDExtensionManager::get_singleton();
57+
GDExtensionConstPtr<const GDExtensionInitializationFunction> ptr((const GDExtensionInitializationFunction *)&p_init_func);
58+
GDExtensionManager::LoadStatus status = gdextension_manager->load_extension_from_function("libgodot://main", ptr);
59+
return status == GDExtensionManager::LoadStatus::LOAD_STATUS_OK;
60+
}
61+
62+
bool GodotInstance::start() {
63+
print_verbose("GodotInstance::start()");
64+
Error err = Main::setup2();
65+
if (err != OK) {
66+
return false;
67+
}
68+
started = Main::start() == EXIT_SUCCESS;
69+
if (started) {
70+
OS::get_singleton()->get_main_loop()->initialize();
71+
}
72+
return started;
73+
}
74+
75+
bool GodotInstance::is_started() {
76+
return started;
77+
}
78+
79+
bool GodotInstance::iteration() {
80+
DisplayServer::get_singleton()->process_events();
81+
return Main::iteration();
82+
}
83+
84+
void GodotInstance::stop() {
85+
print_verbose("GodotInstance::stop()");
86+
if (started) {
87+
OS::get_singleton()->get_main_loop()->finalize();
88+
}
89+
started = false;
90+
}
91+
92+
void GodotInstance::focus_out() {
93+
print_verbose("GodotInstance::focus_out()");
94+
if (started) {
95+
if (OS::get_singleton()->get_main_loop()) {
96+
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_OUT);
97+
}
98+
}
99+
}
100+
101+
void GodotInstance::focus_in() {
102+
print_verbose("GodotInstance::focus_in()");
103+
if (started) {
104+
if (OS::get_singleton()->get_main_loop()) {
105+
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_FOCUS_IN);
106+
}
107+
}
108+
}
109+
110+
void GodotInstance::pause() {
111+
print_verbose("GodotInstance::pause()");
112+
if (started) {
113+
if (OS::get_singleton()->get_main_loop()) {
114+
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
115+
}
116+
}
117+
}
118+
119+
void GodotInstance::resume() {
120+
print_verbose("GodotInstance::resume()");
121+
if (started) {
122+
if (OS::get_singleton()->get_main_loop()) {
123+
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)