Skip to content

Commit d01d0b6

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 b7c5fca commit d01d0b6

22 files changed

+759
-14
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))
@@ -348,6 +356,13 @@ if not env["platform"]:
348356
if env["platform"]:
349357
print(f"Automatically detected platform: {env['platform']}")
350358

359+
# Library Support
360+
if env["library_type"] != "executable":
361+
if env["platform"] not in ["linuxbsd", "macos", "windows"]:
362+
print_error(f"Library builds not yet supported for {env['platform']}")
363+
Exit(255)
364+
env.Append(CPPDEFINES=["LIBGODOT_ENABLED"])
365+
351366
# Deprecated aliases kept for compatibility.
352367
if env["platform"] in compatibility_platform_aliases:
353368
alias = env["platform"]

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+
#include "gdextension.h"
33+
34+
Error GDExtensionFunctionLoader::open_library(const String &p_path) {
35+
ERR_FAIL_COND_V_MSG(!p_path.begins_with("libgodot://"), ERR_FILE_NOT_FOUND, "Function based GDExtensions should have a path starting with libgodot://");
36+
ERR_FAIL_COND_V_MSG(!initialization_function, ERR_DOES_NOT_EXIST, "Initialization function is required for function based GDExtensions.");
37+
38+
library_path = p_path;
39+
40+
return OK;
41+
}
42+
43+
Error GDExtensionFunctionLoader::initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) {
44+
ERR_FAIL_COND_V_MSG(!initialization_function, ERR_DOES_NOT_EXIST, "Initialization function is required for function based GDExtensions.");
45+
GDExtensionBool ret = initialization_function(p_get_proc_address, p_extension.ptr(), r_initialization);
46+
47+
if (ret) {
48+
return OK;
49+
} else {
50+
ERR_PRINT("GDExtension initialization function for '" + library_path + "' returned an error.");
51+
return FAILED;
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
private:
43+
String library_path;
44+
GDExtensionInitializationFunction initialization_function = nullptr;
45+
46+
public:
47+
Error open_library(const String &p_path) override;
48+
Error initialize(GDExtensionInterfaceGetProcAddress p_get_proc_address, const Ref<GDExtension> &p_extension, GDExtensionInitialization *r_initialization) override;
49+
void close_library() override;
50+
bool is_library_open() const override;
51+
bool has_library_changed() const override;
52+
bool library_exists() const override;
53+
54+
void set_initialization_function(GDExtensionInitializationFunction initialization_function);
55+
};

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

0 commit comments

Comments
 (0)