@preeternal/react-native-godot 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/Package.swift +106 -0
- package/README.md +631 -2
- package/android/build.gradle +289 -0
- package/android/consumer-rules.pro +19 -0
- package/android/fix-prefab.gradle +54 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/CMakeLists.txt +74 -0
- package/android/src/main/cpp/GodotModule.cpp +718 -0
- package/android/src/main/cpp/OnLoad.cpp +66 -0
- package/android/src/main/cpp/libgodot_android.h +79 -0
- package/android/src/main/cpp/libgodot_jni.cpp +585 -0
- package/android/src/main/cpp/libgodot_jni.h +130 -0
- package/android/src/main/cpp/native_godot_module_jni.cpp +86 -0
- package/android/src/main/cpp/native_godot_module_jni.h +58 -0
- package/android/src/main/java/com/preeternal/reactnativegodot/NativeGodotModuleModule.kt +83 -0
- package/android/src/main/java/com/preeternal/reactnativegodot/NativeGodotModulePackage.kt +41 -0
- package/android/src/main/java/com/preeternal/reactnativegodot/NativeLibraryLoader.kt +5 -0
- package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotView.kt +81 -0
- package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotViewManager.kt +34 -0
- package/android/src/main/java/com/preeternal/reactnativegodot/RTNLibGodot.kt +350 -0
- package/android/src/main/res/layout/downloading_expansion.xml +165 -0
- package/android/src/main/res/layout/godot_app_layout.xml +5 -0
- package/android/src/main/res/layout/snackbar.xml +27 -0
- package/android/src/main/res/layout/status_bar_ongoing_event_progress_bar.xml +108 -0
- package/android/src/main/res/mipmap/icon.png +0 -0
- package/android/src/main/res/mipmap/icon_background.png +0 -0
- package/android/src/main/res/mipmap/icon_foreground.png +0 -0
- package/android/src/main/res/mipmap/icon_monochrome.png +0 -0
- package/android/src/main/res/mipmap-anydpi-v26/icon.xml +8 -0
- package/android/src/main/res/mipmap-anydpi-v26/themed_icon.xml +11 -0
- package/android/src/main/res/mipmap-hdpi/icon.png +0 -0
- package/android/src/main/res/mipmap-hdpi/icon_background.png +0 -0
- package/android/src/main/res/mipmap-hdpi/icon_foreground.png +0 -0
- package/android/src/main/res/mipmap-hdpi/icon_monochrome.png +0 -0
- package/android/src/main/res/mipmap-mdpi/icon.png +0 -0
- package/android/src/main/res/mipmap-mdpi/icon_background.png +0 -0
- package/android/src/main/res/mipmap-mdpi/icon_foreground.png +0 -0
- package/android/src/main/res/mipmap-mdpi/icon_monochrome.png +0 -0
- package/android/src/main/res/mipmap-xhdpi/icon.png +0 -0
- package/android/src/main/res/mipmap-xhdpi/icon_background.png +0 -0
- package/android/src/main/res/mipmap-xhdpi/icon_foreground.png +0 -0
- package/android/src/main/res/mipmap-xhdpi/icon_monochrome.png +0 -0
- package/android/src/main/res/mipmap-xxhdpi/icon.png +0 -0
- package/android/src/main/res/mipmap-xxhdpi/icon_background.png +0 -0
- package/android/src/main/res/mipmap-xxhdpi/icon_foreground.png +0 -0
- package/android/src/main/res/mipmap-xxhdpi/icon_monochrome.png +0 -0
- package/android/src/main/res/mipmap-xxxhdpi/icon.png +0 -0
- package/android/src/main/res/mipmap-xxxhdpi/icon_background.png +0 -0
- package/android/src/main/res/mipmap-xxxhdpi/icon_foreground.png +0 -0
- package/android/src/main/res/mipmap-xxxhdpi/icon_monochrome.png +0 -0
- package/android/src/main/res/values/dimens.xml +9 -0
- package/android/src/main/res/values/strings.xml +61 -0
- package/android/src/main/res/values/styles.xml +25 -0
- package/android/src/main/res/values-fa/strings.xml +14 -0
- package/android/src/main/res/values-ko/strings.xml +54 -0
- package/android/src/main/res/xml/godot_provider_paths.xml +15 -0
- package/android/src/test/java/com/preeternal/reactnativegodot/NativeGodotModulePackageTest.kt +26 -0
- package/app.plugin.js +173 -0
- package/cpp/GodotModule.h +103 -0
- package/cpp/NativeGodotModule.cpp +825 -0
- package/cpp/NativeGodotModule.h +45 -0
- package/cpp/godot-log.h +64 -0
- package/ios/GodotModule.mm +661 -0
- package/ios/NativeGodotModuleProvider.h +35 -0
- package/ios/NativeGodotModuleProvider.mm +42 -0
- package/ios/RTNGodotView.h +42 -0
- package/ios/RTNGodotView.mm +711 -0
- package/ios/log.mm +38 -0
- package/lib/module/NativeNativeGodotModule.js +59 -0
- package/lib/module/NativeNativeGodotModule.js.map +1 -0
- package/lib/module/RTNGodotView.js +6 -0
- package/lib/module/RTNGodotView.js.map +1 -0
- package/lib/module/RTNGodotView.native.js +5 -0
- package/lib/module/RTNGodotView.native.js.map +1 -0
- package/lib/module/RTNGodotViewNativeComponent.ts +37 -0
- package/lib/module/index.js +30 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeNativeGodotModule.d.ts +26 -0
- package/lib/typescript/src/NativeNativeGodotModule.d.ts.map +1 -0
- package/lib/typescript/src/RTNGodotView.d.ts +7 -0
- package/lib/typescript/src/RTNGodotView.d.ts.map +1 -0
- package/lib/typescript/src/RTNGodotView.native.d.ts +3 -0
- package/lib/typescript/src/RTNGodotView.native.d.ts.map +1 -0
- package/lib/typescript/src/RTNGodotViewNativeComponent.d.ts +11 -0
- package/lib/typescript/src/RTNGodotViewNativeComponent.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +6 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +262 -4
- package/prebuilt-utils.rb +36 -0
- package/react-native-godot.podspec +41 -0
- package/react-native.config.js +15 -0
- package/scripts/check-16kb.sh +136 -0
- package/scripts/check-packaged-artifacts.sh +154 -0
- package/scripts/ci/verify-prebuilt-install.cjs +39 -0
- package/scripts/download-prebuilt.js +274 -0
- package/src/NativeNativeGodotModule.ts +90 -0
- package/src/RTNGodotView.native.tsx +2 -0
- package/src/RTNGodotView.tsx +12 -0
- package/src/RTNGodotViewNativeComponent.ts +37 -0
- package/src/index.tsx +28 -0
|
@@ -0,0 +1,718 @@
|
|
|
1
|
+
/**************************************************************************/
|
|
2
|
+
/* GodotModule.cpp */
|
|
3
|
+
/**************************************************************************/
|
|
4
|
+
/* Copyright (c) 2024-2025 Slay GmbH */
|
|
5
|
+
/* */
|
|
6
|
+
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
7
|
+
/* a copy of this software and associated documentation files (the */
|
|
8
|
+
/* "Software"), to deal in the Software without restriction, including */
|
|
9
|
+
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
10
|
+
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
11
|
+
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
12
|
+
/* the following conditions: */
|
|
13
|
+
/* */
|
|
14
|
+
/* The above copyright notice and this permission notice shall be */
|
|
15
|
+
/* included in all copies or substantial portions of the Software. */
|
|
16
|
+
/* */
|
|
17
|
+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
18
|
+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
19
|
+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
20
|
+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
21
|
+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
22
|
+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
23
|
+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
24
|
+
/**************************************************************************/
|
|
25
|
+
|
|
26
|
+
#include "GodotModule.h"
|
|
27
|
+
#define LOG_TAG "GodotModule"
|
|
28
|
+
#include "godot-log.h"
|
|
29
|
+
|
|
30
|
+
#include "libgodot_android.h"
|
|
31
|
+
#include "libgodot_jni.h"
|
|
32
|
+
#include <godot_cpp/classes/display_server_embedded.hpp>
|
|
33
|
+
#include <godot_cpp/classes/gd_extension_manager.hpp>
|
|
34
|
+
#include <godot_cpp/classes/rendering_native_surface.hpp>
|
|
35
|
+
#include <godot_cpp/classes/rendering_native_surface_android.hpp>
|
|
36
|
+
#include <godot_cpp/godot.hpp>
|
|
37
|
+
|
|
38
|
+
#include <android/choreographer.h>
|
|
39
|
+
#include <android/looper.h>
|
|
40
|
+
#include <android/native_window.h>
|
|
41
|
+
#include <android/native_window_jni.h>
|
|
42
|
+
#include <dlfcn.h>
|
|
43
|
+
|
|
44
|
+
#include <unistd.h>
|
|
45
|
+
#include <map>
|
|
46
|
+
#include <queue>
|
|
47
|
+
#include <string>
|
|
48
|
+
|
|
49
|
+
typedef GDExtensionObjectPtr (*libgodot_create_godot_instance_android_type)(int p_argc, char *p_argv[], GDExtensionInitializationFunction p_init_func, JNIEnv *env, jobject p_asset_manager, jobject p_net_utils, jobject p_directory_access_handler, jobject p_file_access_handler, jobject p_godot_io_wrapper, jobject p_godot_wrapper, jobject p_class_loader);
|
|
50
|
+
typedef void (*libgodot_destroy_godot_instance_type)(GDExtensionObjectPtr p_godot_instance);
|
|
51
|
+
|
|
52
|
+
const static char CMD_FUNCTION = 1;
|
|
53
|
+
const static char CMD_EXIT = 2;
|
|
54
|
+
|
|
55
|
+
class AndroidThread {
|
|
56
|
+
std::mutex mutex;
|
|
57
|
+
std::condition_variable started;
|
|
58
|
+
std::queue<std::function<void()>> tasks;
|
|
59
|
+
ALooper *looper = nullptr;
|
|
60
|
+
int function_queue_fd = -1;
|
|
61
|
+
bool quit = false;
|
|
62
|
+
// Keep the worker last so run() cannot access fields before their initialization.
|
|
63
|
+
std::thread thread;
|
|
64
|
+
|
|
65
|
+
public:
|
|
66
|
+
AndroidThread() :
|
|
67
|
+
thread(&AndroidThread::run, this) {
|
|
68
|
+
std::unique_lock<std::mutex> lock(mutex);
|
|
69
|
+
started.wait(lock, [this]() {
|
|
70
|
+
return function_queue_fd >= 0;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static int looper_callback(int fd, int events, void *data) {
|
|
75
|
+
char cmd;
|
|
76
|
+
AndroidThread *self = (AndroidThread *)data;
|
|
77
|
+
ssize_t res = read(fd, &cmd, sizeof(cmd));
|
|
78
|
+
if (res < 0) {
|
|
79
|
+
LOGE("Unable to read looper event from pipe: %d", errno);
|
|
80
|
+
return 1;
|
|
81
|
+
}
|
|
82
|
+
if (res == 0) {
|
|
83
|
+
LOGE("End of file event should not happen.");
|
|
84
|
+
return 1;
|
|
85
|
+
}
|
|
86
|
+
if (res != sizeof(cmd)) {
|
|
87
|
+
LOGE("Unable to read command fully from pipe");
|
|
88
|
+
return 1;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
switch (cmd) {
|
|
92
|
+
case CMD_FUNCTION: {
|
|
93
|
+
std::function<void()> task;
|
|
94
|
+
{
|
|
95
|
+
std::lock_guard<std::mutex> lock(self->mutex);
|
|
96
|
+
if (self->tasks.empty()) {
|
|
97
|
+
LOGW("Empty queue when processing CMD_FUNCTION");
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
task = std::move(self->tasks.front());
|
|
101
|
+
self->tasks.pop();
|
|
102
|
+
}
|
|
103
|
+
task();
|
|
104
|
+
} break;
|
|
105
|
+
case CMD_EXIT: {
|
|
106
|
+
self->quit = true;
|
|
107
|
+
} break;
|
|
108
|
+
default: {
|
|
109
|
+
LOGE("Unknown command: %d", cmd);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return 1;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
void enqueue(std::function<void()> f) {
|
|
117
|
+
if (thread.get_id() == std::this_thread::get_id()) {
|
|
118
|
+
f();
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
{
|
|
122
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
123
|
+
tasks.emplace(f);
|
|
124
|
+
}
|
|
125
|
+
ssize_t res = write(function_queue_fd, &CMD_FUNCTION, sizeof(CMD_FUNCTION));
|
|
126
|
+
if (res < 0) {
|
|
127
|
+
LOGE("Unable to write to pipe: %d", errno);
|
|
128
|
+
}
|
|
129
|
+
if (res != sizeof(CMD_FUNCTION)) {
|
|
130
|
+
LOGE("Unable to write command fully to pipe");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
void run() {
|
|
135
|
+
LOGI("AndroidThread Looper thread started.");
|
|
136
|
+
LibGodot::get_jni_env(); // Force attaching to Java VM
|
|
137
|
+
{
|
|
138
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
139
|
+
|
|
140
|
+
// Prepare Looper
|
|
141
|
+
looper = ALooper_prepare(0);
|
|
142
|
+
|
|
143
|
+
// Register callback for function
|
|
144
|
+
int fds[2];
|
|
145
|
+
int res = pipe(fds);
|
|
146
|
+
if (res < 0) {
|
|
147
|
+
LOGE("Unable to create pipe for ALooper: %d", errno);
|
|
148
|
+
return; // TODO: Abort app
|
|
149
|
+
}
|
|
150
|
+
ALooper_addFd(looper, fds[0], ALOOPER_POLL_CALLBACK, ALOOPER_EVENT_INPUT, AndroidThread::looper_callback, this);
|
|
151
|
+
function_queue_fd = fds[1];
|
|
152
|
+
started.notify_all();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
while (!quit) {
|
|
156
|
+
int outFd;
|
|
157
|
+
int outEvents;
|
|
158
|
+
void *outData;
|
|
159
|
+
int res = ALooper_pollOnce(10, &outFd, &outEvents, &outData);
|
|
160
|
+
if (res == ALOOPER_POLL_ERROR) {
|
|
161
|
+
LOGE("ALooper_pollOnce internal error.");
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
ALooper_release(looper);
|
|
166
|
+
LOGI("AndroidThread Looper thread exited.");
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
typedef struct FuncData {
|
|
171
|
+
std::function<void()> func;
|
|
172
|
+
jobject ref;
|
|
173
|
+
FuncData() :
|
|
174
|
+
func(nullptr), ref(nullptr) {}
|
|
175
|
+
FuncData(std::function<void()> p_func, jobject p_ref) :
|
|
176
|
+
func(p_func), ref(p_ref) {}
|
|
177
|
+
} FuncData;
|
|
178
|
+
|
|
179
|
+
typedef struct WindowFuncData {
|
|
180
|
+
std::function<void(bool)> func;
|
|
181
|
+
jobject ref;
|
|
182
|
+
WindowFuncData() :
|
|
183
|
+
func(nullptr), ref(nullptr) {}
|
|
184
|
+
WindowFuncData(std::function<void(bool)> p_func, jobject p_ref) :
|
|
185
|
+
func(p_func), ref(p_ref) {}
|
|
186
|
+
} WindowFuncData;
|
|
187
|
+
|
|
188
|
+
struct AndroidPlatformData : PlatformData {
|
|
189
|
+
ANativeWindow *mainNativeWindow = nullptr;
|
|
190
|
+
godot::Ref<godot::RenderingNativeSurface> mainSurface;
|
|
191
|
+
|
|
192
|
+
void *handle = nullptr;
|
|
193
|
+
libgodot_create_godot_instance_android_type func_libgodot_create_godot_instance_android = nullptr;
|
|
194
|
+
libgodot_destroy_godot_instance_type func_libgodot_destroy_godot_instance = nullptr;
|
|
195
|
+
double contentScaleFactor = 1.0;
|
|
196
|
+
std::map<std::string, WindowFuncData> windowUpdateCallbacks;
|
|
197
|
+
std::map<void *, std::string> handleToWindowName;
|
|
198
|
+
// No frames until React Native reports an active host. This does not start Godot.
|
|
199
|
+
bool in_background = true;
|
|
200
|
+
bool paused = false;
|
|
201
|
+
// Guarded by GodotModule::_mutex; kept pending across pause/resume until consumed.
|
|
202
|
+
bool frameCallbackPending = false;
|
|
203
|
+
AndroidThread thread;
|
|
204
|
+
std::mutex windowUpdateMutex;
|
|
205
|
+
std::mutex createMutex;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
GodotModule *GodotModule::get_singleton() {
|
|
209
|
+
static GodotModule *singleton = new GodotModule(new AndroidPlatformData());
|
|
210
|
+
return singleton;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
extern "C" {
|
|
214
|
+
|
|
215
|
+
static void initialize_default_module(godot::ModuleInitializationLevel p_level) {
|
|
216
|
+
if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
static void uninitialize_default_module(godot::ModuleInitializationLevel p_level) {
|
|
222
|
+
if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
GDExtensionBool GDE_EXPORT gdextension_default_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {
|
|
228
|
+
godot::GDExtensionBinding::InitObject init_object(p_get_proc_address, p_library, r_initialization);
|
|
229
|
+
|
|
230
|
+
init_object.register_initializer(initialize_default_module);
|
|
231
|
+
init_object.register_terminator(uninitialize_default_module);
|
|
232
|
+
init_object.set_minimum_library_initialization_level(godot::MODULE_INITIALIZATION_LEVEL_SCENE);
|
|
233
|
+
|
|
234
|
+
return init_object.init();
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
using PostFrameCallback64 = void (*)(AChoreographer *, AChoreographer_frameCallback64, void *);
|
|
239
|
+
|
|
240
|
+
static void handleFrameCallback(void *data);
|
|
241
|
+
|
|
242
|
+
static void frameCallback(long, void *data) {
|
|
243
|
+
handleFrameCallback(data);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
static void frameCallback64(int64_t, void *data) {
|
|
247
|
+
handleFrameCallback(data);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
static void postFrameCallback(AChoreographer *choreographer, void *data) {
|
|
251
|
+
static auto postFrameCallback64 = reinterpret_cast<PostFrameCallback64>(
|
|
252
|
+
dlsym(RTLD_DEFAULT, "AChoreographer_postFrameCallback64"));
|
|
253
|
+
if (postFrameCallback64 != nullptr) {
|
|
254
|
+
postFrameCallback64(choreographer, frameCallback64, data);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
#pragma clang diagnostic push
|
|
259
|
+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
260
|
+
AChoreographer_postFrameCallback(choreographer, frameCallback, data);
|
|
261
|
+
#pragma clang diagnostic pop
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
static void handleFrameCallback(void *data) {
|
|
265
|
+
GodotModule *self = (GodotModule *)data;
|
|
266
|
+
self->iterate();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
godot::GodotInstance *GodotModule::get_or_create_instance(std::vector<std::string> args) {
|
|
270
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
271
|
+
|
|
272
|
+
// Make sure that we only run this method once at a time.
|
|
273
|
+
std::lock_guard createLock(data->createMutex);
|
|
274
|
+
|
|
275
|
+
{
|
|
276
|
+
std::lock_guard lock(_mutex);
|
|
277
|
+
if (_instance) {
|
|
278
|
+
return _instance;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
void *handle = nullptr;
|
|
283
|
+
if (!data->func_libgodot_create_godot_instance_android) {
|
|
284
|
+
libgodot_create_godot_instance_android_type func_libgodot_create_godot_instance_android = nullptr;
|
|
285
|
+
#ifndef LIBGODOT_STATIC
|
|
286
|
+
handle = dlopen("libgodot_android.so", RTLD_LAZY | RTLD_LOCAL);
|
|
287
|
+
|
|
288
|
+
if (handle == nullptr) {
|
|
289
|
+
LOGI("Unable to open libgodot_android.so: %s", dlerror());
|
|
290
|
+
return nullptr;
|
|
291
|
+
}
|
|
292
|
+
func_libgodot_create_godot_instance_android = (libgodot_create_godot_instance_android_type)dlsym(handle, "libgodot_create_godot_instance_android");
|
|
293
|
+
|
|
294
|
+
if (func_libgodot_create_godot_instance_android == nullptr) {
|
|
295
|
+
LOGE("Unable to load libgodot_create_godot_instance symbol: %s", dlerror());
|
|
296
|
+
dlclose(handle);
|
|
297
|
+
handle = nullptr;
|
|
298
|
+
return nullptr;
|
|
299
|
+
}
|
|
300
|
+
#else
|
|
301
|
+
func_libgodot_create_godot_instance_android = libgodot_create_godot_instance_android;
|
|
302
|
+
#endif
|
|
303
|
+
{
|
|
304
|
+
std::lock_guard lock(_mutex);
|
|
305
|
+
data->func_libgodot_create_godot_instance_android = func_libgodot_create_godot_instance_android;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
std::vector<std::string> cmdline{ "apk" };
|
|
310
|
+
for (std::string arg : args) {
|
|
311
|
+
cmdline.push_back(arg);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
std::vector<const char *> cargs{};
|
|
315
|
+
for (const std::string &arg : cmdline) {
|
|
316
|
+
cargs.push_back(arg.c_str());
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
GDExtensionObjectPtr instance_ptr = nullptr;
|
|
320
|
+
{
|
|
321
|
+
std::lock_guard lock(_mutex);
|
|
322
|
+
instance_ptr = data->func_libgodot_create_godot_instance_android(
|
|
323
|
+
cargs.size(),
|
|
324
|
+
(char **)cargs.data(),
|
|
325
|
+
gdextension_default_init,
|
|
326
|
+
LibGodot::get_jni_env(),
|
|
327
|
+
LibGodot::get_asset_manager(),
|
|
328
|
+
LibGodot::get_net_utils(),
|
|
329
|
+
LibGodot::get_dir_access_handler(),
|
|
330
|
+
LibGodot::get_file_access_handler(),
|
|
331
|
+
LibGodot::get_godot_io(),
|
|
332
|
+
LibGodot::get_godot_engine(),
|
|
333
|
+
LibGodot::get_class_loader());
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (instance_ptr == nullptr) {
|
|
337
|
+
// Unable to start Godot
|
|
338
|
+
LOGI("Unable to start Godot");
|
|
339
|
+
if (handle) {
|
|
340
|
+
dlclose(handle);
|
|
341
|
+
handle = nullptr;
|
|
342
|
+
{
|
|
343
|
+
std::lock_guard lock(_mutex);
|
|
344
|
+
data->func_libgodot_create_godot_instance_android = nullptr;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return nullptr;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
godot::GodotInstance *instance = reinterpret_cast<godot::GodotInstance *>(godot::internal::get_object_instance_binding(instance_ptr));
|
|
351
|
+
|
|
352
|
+
// Initialize Android Surface
|
|
353
|
+
ANativeWindow *mainNativeWindow = LibGodot::get_main_surface();
|
|
354
|
+
uint32_t width = LibGodot::get_main_width();
|
|
355
|
+
uint32_t height = LibGodot::get_main_height();
|
|
356
|
+
|
|
357
|
+
// TODO: Set main window surface size and position
|
|
358
|
+
// TODO: Get content scale factor from Android
|
|
359
|
+
// data->contentScaleFactor = [[UIScreen mainScreen] scale];
|
|
360
|
+
double contentScaleFactor = 1.0;
|
|
361
|
+
// TODO: Set content scale factor in the surface
|
|
362
|
+
|
|
363
|
+
godot::Ref<godot::RenderingNativeSurfaceAndroid> androidSurface = godot::RenderingNativeSurfaceAndroid::create((uint64_t)mainNativeWindow, width, height);
|
|
364
|
+
|
|
365
|
+
godot::RenderingNativeSurface *ptr = godot::Object::cast_to<godot::RenderingNativeSurface>(androidSurface.ptr());
|
|
366
|
+
godot::Ref<godot::RenderingNativeSurface> nativeSurface(ptr);
|
|
367
|
+
|
|
368
|
+
godot::DisplayServerEmbedded::set_native_surface(nativeSurface);
|
|
369
|
+
|
|
370
|
+
instance->start();
|
|
371
|
+
bool inBackground;
|
|
372
|
+
|
|
373
|
+
{
|
|
374
|
+
std::lock_guard lock(_mutex);
|
|
375
|
+
|
|
376
|
+
data->mainNativeWindow = mainNativeWindow;
|
|
377
|
+
data->mainSurface = nativeSurface;
|
|
378
|
+
data->contentScaleFactor = contentScaleFactor;
|
|
379
|
+
data->handle = handle;
|
|
380
|
+
|
|
381
|
+
_instance = instance;
|
|
382
|
+
inBackground = data->in_background;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
if (inBackground) {
|
|
386
|
+
instance->focus_out();
|
|
387
|
+
instance->pause();
|
|
388
|
+
}
|
|
389
|
+
updateState();
|
|
390
|
+
updateWindows(true);
|
|
391
|
+
|
|
392
|
+
return instance;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
double GodotModule::get_content_scale_factor() {
|
|
396
|
+
std::lock_guard lock(_mutex);
|
|
397
|
+
if (!_instance) {
|
|
398
|
+
return 1.0;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
402
|
+
|
|
403
|
+
return data->contentScaleFactor;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
void GodotModule::destroy_instance() {
|
|
407
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
408
|
+
|
|
409
|
+
// Make sure that we only run this method once at a time.
|
|
410
|
+
std::lock_guard createLock(data->createMutex);
|
|
411
|
+
|
|
412
|
+
if (!_instance) {
|
|
413
|
+
LOGI("Godot instance is already destroyed.");
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
if (!data->func_libgodot_destroy_godot_instance) {
|
|
418
|
+
libgodot_destroy_godot_instance_type func_libgodot_destroy_godot_instance = nullptr;
|
|
419
|
+
#ifndef LIBGODOT_STATIC
|
|
420
|
+
void *handle = nullptr;
|
|
421
|
+
|
|
422
|
+
{
|
|
423
|
+
std::lock_guard lock(_mutex);
|
|
424
|
+
handle = data->handle;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (handle == nullptr) {
|
|
428
|
+
LOGI("Unable to open libgodot_android.so: %s", dlerror());
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
func_libgodot_destroy_godot_instance = (libgodot_destroy_godot_instance_type)dlsym(handle, "libgodot_destroy_godot_instance");
|
|
432
|
+
|
|
433
|
+
if (func_libgodot_destroy_godot_instance == nullptr) {
|
|
434
|
+
LOGE("Unable to load libgodot_destroy_godot_instance symbol: %s", dlerror());
|
|
435
|
+
dlclose(handle);
|
|
436
|
+
handle = nullptr;
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
#else
|
|
440
|
+
func_libgodot_destroy_godot_instance = libgodot_destroy_godot_instance;
|
|
441
|
+
#endif
|
|
442
|
+
{
|
|
443
|
+
std::lock_guard lock(_mutex);
|
|
444
|
+
data->func_libgodot_destroy_godot_instance = func_libgodot_destroy_godot_instance;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
{
|
|
449
|
+
std::lock_guard lock(_mutex);
|
|
450
|
+
|
|
451
|
+
godot::DisplayServerEmbedded::set_native_surface(godot::Ref<godot::RenderingNativeSurface>(nullptr));
|
|
452
|
+
ANativeWindow_release(data->mainNativeWindow);
|
|
453
|
+
data->mainNativeWindow = nullptr;
|
|
454
|
+
data->mainSurface = godot::Ref<godot::RenderingNativeSurface>(nullptr);
|
|
455
|
+
|
|
456
|
+
data->func_libgodot_destroy_godot_instance(_instance->_owner);
|
|
457
|
+
_instance = nullptr;
|
|
458
|
+
godot::GDExtensionBinding::deinit();
|
|
459
|
+
|
|
460
|
+
dlclose(data->handle);
|
|
461
|
+
data->handle = nullptr;
|
|
462
|
+
|
|
463
|
+
data->paused = false;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
godot::Ref<godot::RenderingNativeSurface> GodotModule::get_main_rendering_surface() {
|
|
468
|
+
std::lock_guard lock(_mutex);
|
|
469
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
470
|
+
return data->mainSurface;
|
|
471
|
+
}
|
|
472
|
+
void GodotModule::focus_out() {
|
|
473
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
474
|
+
{
|
|
475
|
+
std::lock_guard lock(_mutex);
|
|
476
|
+
data->in_background = true;
|
|
477
|
+
}
|
|
478
|
+
data->thread.enqueue([this]() {
|
|
479
|
+
std::lock_guard lock(_mutex);
|
|
480
|
+
if (_instance) {
|
|
481
|
+
_instance->focus_out();
|
|
482
|
+
}
|
|
483
|
+
});
|
|
484
|
+
updateState();
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
void GodotModule::focus_in() {
|
|
488
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
489
|
+
{
|
|
490
|
+
std::lock_guard lock(_mutex);
|
|
491
|
+
data->in_background = false;
|
|
492
|
+
}
|
|
493
|
+
data->thread.enqueue([this]() {
|
|
494
|
+
std::lock_guard lock(_mutex);
|
|
495
|
+
if (_instance) {
|
|
496
|
+
_instance->focus_in();
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
updateState();
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
bool GodotModule::is_paused() {
|
|
503
|
+
std::lock_guard lock(_mutex);
|
|
504
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
505
|
+
return data->paused;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
void GodotModule::appPause() {
|
|
509
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
510
|
+
{
|
|
511
|
+
std::lock_guard lock(_mutex);
|
|
512
|
+
if (data->in_background) {
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
data->in_background = true;
|
|
516
|
+
}
|
|
517
|
+
data->thread.enqueue([this]() {
|
|
518
|
+
godot::GodotInstance *instance = get_instance();
|
|
519
|
+
if (instance) {
|
|
520
|
+
instance->focus_out();
|
|
521
|
+
instance->pause();
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
updateState();
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
void GodotModule::appResume() {
|
|
528
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
529
|
+
{
|
|
530
|
+
std::lock_guard lock(_mutex);
|
|
531
|
+
if (!data->in_background) {
|
|
532
|
+
return;
|
|
533
|
+
}
|
|
534
|
+
data->in_background = false;
|
|
535
|
+
}
|
|
536
|
+
data->thread.enqueue([this]() {
|
|
537
|
+
godot::GodotInstance *instance = get_instance();
|
|
538
|
+
if (instance) {
|
|
539
|
+
instance->resume();
|
|
540
|
+
instance->focus_in();
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
updateState();
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
void GodotModule::pause() {
|
|
547
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
548
|
+
{
|
|
549
|
+
std::lock_guard lock(_mutex);
|
|
550
|
+
data->paused = true;
|
|
551
|
+
}
|
|
552
|
+
updateState();
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
void GodotModule::resume() {
|
|
556
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
557
|
+
{
|
|
558
|
+
std::lock_guard lock(_mutex);
|
|
559
|
+
data->paused = false;
|
|
560
|
+
}
|
|
561
|
+
updateState();
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
void GodotModule::updateState() {
|
|
565
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
566
|
+
data->thread.enqueue([this, data]() {
|
|
567
|
+
std::lock_guard lock(_mutex);
|
|
568
|
+
if (!_instance || !_instance->is_started() || data->in_background ||
|
|
569
|
+
data->paused || data->frameCallbackPending) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
data->frameCallbackPending = true;
|
|
574
|
+
AChoreographer *choreographer = AChoreographer_getInstance();
|
|
575
|
+
postFrameCallback(choreographer, this);
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
class CPPCallable : public godot::CallableCustom {
|
|
580
|
+
// friend bool javascript_callable_compare_equal_func(const godot::CallableCustom *p_a, const godot::CallableCustom *p_b) {
|
|
581
|
+
// if (p_a == p_b) return true;
|
|
582
|
+
// const JavascriptCallable *a = static_cast<const JavascriptCallable *>(p_a);
|
|
583
|
+
// const JavascriptCallable *b = static_cast<const JavascriptCallable *>(p_b);
|
|
584
|
+
// return jsi::Function::strictEquals(a->_rt, a->_func, b->_func);
|
|
585
|
+
// };
|
|
586
|
+
|
|
587
|
+
std::function<void(const godot::Variant **, int, godot::Variant &, GDExtensionCallError &)> _func;
|
|
588
|
+
|
|
589
|
+
public:
|
|
590
|
+
CPPCallable(std::function<void(const godot::Variant **, int, godot::Variant &, GDExtensionCallError &)> f) :
|
|
591
|
+
_func(f) {}
|
|
592
|
+
|
|
593
|
+
uint32_t hash() const override {
|
|
594
|
+
return 0; // Use default hash function
|
|
595
|
+
}
|
|
596
|
+
godot::String get_as_text() const override {
|
|
597
|
+
return godot::String("CPPCallable");
|
|
598
|
+
}
|
|
599
|
+
CompareEqualFunc get_compare_equal_func() const override {
|
|
600
|
+
return nullptr;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
CompareLessFunc get_compare_less_func() const override {
|
|
604
|
+
return nullptr;
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
bool is_valid() const override {
|
|
608
|
+
return true;
|
|
609
|
+
}
|
|
610
|
+
godot::ObjectID get_object() const override {
|
|
611
|
+
return godot::ObjectID();
|
|
612
|
+
}
|
|
613
|
+
void call(const godot::Variant **p_arguments, int p_argcount, godot::Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
|
|
614
|
+
_func(p_arguments, p_argcount, r_return_value, r_call_error);
|
|
615
|
+
r_call_error.error = GDEXTENSION_CALL_OK;
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
godot::Callable GodotModule::create_callable(std::function<void(const godot::Variant **, int, godot::Variant &, GDExtensionCallError &)> f) {
|
|
620
|
+
return godot::Callable(memnew(CPPCallable(f)));
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
void GodotModule::registerWindowUpdateCallback(std::string name, void *handle, std::function<void(bool)> f, void *ref) {
|
|
624
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
625
|
+
std::lock_guard lock(data->windowUpdateMutex);
|
|
626
|
+
LOGD("Registering Window: %p, %s", handle, name.c_str());
|
|
627
|
+
if (data->handleToWindowName.contains(handle)) {
|
|
628
|
+
std::string currentName = data->handleToWindowName[handle];
|
|
629
|
+
if (currentName != name) {
|
|
630
|
+
LOGE("RegisterWindowUpdateCallback: Unable to register a different name for the same handle");
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
} else {
|
|
634
|
+
data->handleToWindowName[handle] = name;
|
|
635
|
+
}
|
|
636
|
+
data->windowUpdateCallbacks[name] = WindowFuncData(f, (jobject)ref);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
void GodotModule::unregisterWindowUpdateCallback(void *handle) {
|
|
640
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
641
|
+
std::lock_guard lock(data->windowUpdateMutex);
|
|
642
|
+
if (data->handleToWindowName.contains(handle)) {
|
|
643
|
+
std::string name = data->handleToWindowName[handle];
|
|
644
|
+
LOGD("Unregistering Window: %p, %s", handle, name.c_str());
|
|
645
|
+
WindowFuncData fd = data->windowUpdateCallbacks[name];
|
|
646
|
+
JNIEnv *env = LibGodot::get_jni_env();
|
|
647
|
+
env->DeleteGlobalRef(fd.ref);
|
|
648
|
+
data->windowUpdateCallbacks.erase(name);
|
|
649
|
+
data->handleToWindowName.erase(handle);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
void GodotModule::updateWindow(std::string name, bool adding) {
|
|
654
|
+
LibGodot::updateWindow(name);
|
|
655
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
656
|
+
std::function<void(bool)> cb;
|
|
657
|
+
{
|
|
658
|
+
std::lock_guard lock(data->windowUpdateMutex);
|
|
659
|
+
if (!data->windowUpdateCallbacks.contains(name)) {
|
|
660
|
+
return;
|
|
661
|
+
}
|
|
662
|
+
cb = data->windowUpdateCallbacks[name].func;
|
|
663
|
+
}
|
|
664
|
+
cb(true);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
void GodotModule::updateWindows(bool adding) {
|
|
668
|
+
LibGodot::updateWindows();
|
|
669
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
670
|
+
std::map<std::string, WindowFuncData> callbacks;
|
|
671
|
+
{
|
|
672
|
+
std::lock_guard lock(data->windowUpdateMutex);
|
|
673
|
+
callbacks = data->windowUpdateCallbacks;
|
|
674
|
+
}
|
|
675
|
+
for (const std::pair<std::string, WindowFuncData> elem : callbacks) {
|
|
676
|
+
LOGD("Updating Window: %s", elem.first.c_str());
|
|
677
|
+
elem.second.func(true);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
void GodotModule::runOnGodotThread(std::function<void()> f, bool wait) {
|
|
682
|
+
if (wait) {
|
|
683
|
+
std::mutex waitMutex;
|
|
684
|
+
std::condition_variable waitVar;
|
|
685
|
+
bool ready = false;
|
|
686
|
+
std::function<void()> runFunc = [&f, &waitMutex, &waitVar, &ready]() {
|
|
687
|
+
f();
|
|
688
|
+
std::unique_lock<std::mutex> lock(waitMutex);
|
|
689
|
+
ready = true;
|
|
690
|
+
lock.unlock();
|
|
691
|
+
waitVar.notify_one();
|
|
692
|
+
};
|
|
693
|
+
std::unique_lock<std::mutex> lock(waitMutex);
|
|
694
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
695
|
+
data->thread.enqueue(runFunc);
|
|
696
|
+
waitVar.wait(lock, [&ready] { return ready; });
|
|
697
|
+
} else {
|
|
698
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
699
|
+
data->thread.enqueue(f);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
void GodotModule::iterate() {
|
|
704
|
+
AndroidPlatformData *data = static_cast<AndroidPlatformData *>(_data);
|
|
705
|
+
godot::GodotInstance *instance = nullptr;
|
|
706
|
+
{
|
|
707
|
+
std::lock_guard lock(_mutex);
|
|
708
|
+
data->frameCallbackPending = false;
|
|
709
|
+
if (data->in_background || data->paused) {
|
|
710
|
+
return;
|
|
711
|
+
}
|
|
712
|
+
instance = _instance;
|
|
713
|
+
}
|
|
714
|
+
if (instance && instance->is_started()) {
|
|
715
|
+
instance->iteration();
|
|
716
|
+
updateState();
|
|
717
|
+
}
|
|
718
|
+
}
|