@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.
Files changed (103) hide show
  1. package/LICENSE +22 -0
  2. package/Package.swift +106 -0
  3. package/README.md +631 -2
  4. package/android/build.gradle +289 -0
  5. package/android/consumer-rules.pro +19 -0
  6. package/android/fix-prefab.gradle +54 -0
  7. package/android/src/main/AndroidManifest.xml +2 -0
  8. package/android/src/main/cpp/CMakeLists.txt +74 -0
  9. package/android/src/main/cpp/GodotModule.cpp +718 -0
  10. package/android/src/main/cpp/OnLoad.cpp +66 -0
  11. package/android/src/main/cpp/libgodot_android.h +79 -0
  12. package/android/src/main/cpp/libgodot_jni.cpp +585 -0
  13. package/android/src/main/cpp/libgodot_jni.h +130 -0
  14. package/android/src/main/cpp/native_godot_module_jni.cpp +86 -0
  15. package/android/src/main/cpp/native_godot_module_jni.h +58 -0
  16. package/android/src/main/java/com/preeternal/reactnativegodot/NativeGodotModuleModule.kt +83 -0
  17. package/android/src/main/java/com/preeternal/reactnativegodot/NativeGodotModulePackage.kt +41 -0
  18. package/android/src/main/java/com/preeternal/reactnativegodot/NativeLibraryLoader.kt +5 -0
  19. package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotView.kt +81 -0
  20. package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotViewManager.kt +34 -0
  21. package/android/src/main/java/com/preeternal/reactnativegodot/RTNLibGodot.kt +350 -0
  22. package/android/src/main/res/layout/downloading_expansion.xml +165 -0
  23. package/android/src/main/res/layout/godot_app_layout.xml +5 -0
  24. package/android/src/main/res/layout/snackbar.xml +27 -0
  25. package/android/src/main/res/layout/status_bar_ongoing_event_progress_bar.xml +108 -0
  26. package/android/src/main/res/mipmap/icon.png +0 -0
  27. package/android/src/main/res/mipmap/icon_background.png +0 -0
  28. package/android/src/main/res/mipmap/icon_foreground.png +0 -0
  29. package/android/src/main/res/mipmap/icon_monochrome.png +0 -0
  30. package/android/src/main/res/mipmap-anydpi-v26/icon.xml +8 -0
  31. package/android/src/main/res/mipmap-anydpi-v26/themed_icon.xml +11 -0
  32. package/android/src/main/res/mipmap-hdpi/icon.png +0 -0
  33. package/android/src/main/res/mipmap-hdpi/icon_background.png +0 -0
  34. package/android/src/main/res/mipmap-hdpi/icon_foreground.png +0 -0
  35. package/android/src/main/res/mipmap-hdpi/icon_monochrome.png +0 -0
  36. package/android/src/main/res/mipmap-mdpi/icon.png +0 -0
  37. package/android/src/main/res/mipmap-mdpi/icon_background.png +0 -0
  38. package/android/src/main/res/mipmap-mdpi/icon_foreground.png +0 -0
  39. package/android/src/main/res/mipmap-mdpi/icon_monochrome.png +0 -0
  40. package/android/src/main/res/mipmap-xhdpi/icon.png +0 -0
  41. package/android/src/main/res/mipmap-xhdpi/icon_background.png +0 -0
  42. package/android/src/main/res/mipmap-xhdpi/icon_foreground.png +0 -0
  43. package/android/src/main/res/mipmap-xhdpi/icon_monochrome.png +0 -0
  44. package/android/src/main/res/mipmap-xxhdpi/icon.png +0 -0
  45. package/android/src/main/res/mipmap-xxhdpi/icon_background.png +0 -0
  46. package/android/src/main/res/mipmap-xxhdpi/icon_foreground.png +0 -0
  47. package/android/src/main/res/mipmap-xxhdpi/icon_monochrome.png +0 -0
  48. package/android/src/main/res/mipmap-xxxhdpi/icon.png +0 -0
  49. package/android/src/main/res/mipmap-xxxhdpi/icon_background.png +0 -0
  50. package/android/src/main/res/mipmap-xxxhdpi/icon_foreground.png +0 -0
  51. package/android/src/main/res/mipmap-xxxhdpi/icon_monochrome.png +0 -0
  52. package/android/src/main/res/values/dimens.xml +9 -0
  53. package/android/src/main/res/values/strings.xml +61 -0
  54. package/android/src/main/res/values/styles.xml +25 -0
  55. package/android/src/main/res/values-fa/strings.xml +14 -0
  56. package/android/src/main/res/values-ko/strings.xml +54 -0
  57. package/android/src/main/res/xml/godot_provider_paths.xml +15 -0
  58. package/android/src/test/java/com/preeternal/reactnativegodot/NativeGodotModulePackageTest.kt +26 -0
  59. package/app.plugin.js +173 -0
  60. package/cpp/GodotModule.h +103 -0
  61. package/cpp/NativeGodotModule.cpp +825 -0
  62. package/cpp/NativeGodotModule.h +45 -0
  63. package/cpp/godot-log.h +64 -0
  64. package/ios/GodotModule.mm +661 -0
  65. package/ios/NativeGodotModuleProvider.h +35 -0
  66. package/ios/NativeGodotModuleProvider.mm +42 -0
  67. package/ios/RTNGodotView.h +42 -0
  68. package/ios/RTNGodotView.mm +711 -0
  69. package/ios/log.mm +38 -0
  70. package/lib/module/NativeNativeGodotModule.js +59 -0
  71. package/lib/module/NativeNativeGodotModule.js.map +1 -0
  72. package/lib/module/RTNGodotView.js +6 -0
  73. package/lib/module/RTNGodotView.js.map +1 -0
  74. package/lib/module/RTNGodotView.native.js +5 -0
  75. package/lib/module/RTNGodotView.native.js.map +1 -0
  76. package/lib/module/RTNGodotViewNativeComponent.ts +37 -0
  77. package/lib/module/index.js +30 -0
  78. package/lib/module/index.js.map +1 -0
  79. package/lib/module/package.json +1 -0
  80. package/lib/typescript/package.json +1 -0
  81. package/lib/typescript/src/NativeNativeGodotModule.d.ts +26 -0
  82. package/lib/typescript/src/NativeNativeGodotModule.d.ts.map +1 -0
  83. package/lib/typescript/src/RTNGodotView.d.ts +7 -0
  84. package/lib/typescript/src/RTNGodotView.d.ts.map +1 -0
  85. package/lib/typescript/src/RTNGodotView.native.d.ts +3 -0
  86. package/lib/typescript/src/RTNGodotView.native.d.ts.map +1 -0
  87. package/lib/typescript/src/RTNGodotViewNativeComponent.d.ts +11 -0
  88. package/lib/typescript/src/RTNGodotViewNativeComponent.d.ts.map +1 -0
  89. package/lib/typescript/src/index.d.ts +6 -0
  90. package/lib/typescript/src/index.d.ts.map +1 -0
  91. package/package.json +262 -4
  92. package/prebuilt-utils.rb +36 -0
  93. package/react-native-godot.podspec +41 -0
  94. package/react-native.config.js +15 -0
  95. package/scripts/check-16kb.sh +136 -0
  96. package/scripts/check-packaged-artifacts.sh +154 -0
  97. package/scripts/ci/verify-prebuilt-install.cjs +39 -0
  98. package/scripts/download-prebuilt.js +274 -0
  99. package/src/NativeNativeGodotModule.ts +90 -0
  100. package/src/RTNGodotView.native.tsx +2 -0
  101. package/src/RTNGodotView.tsx +12 -0
  102. package/src/RTNGodotViewNativeComponent.ts +37 -0
  103. package/src/index.tsx +28 -0
@@ -0,0 +1,661 @@
1
+ /**************************************************************************/
2
+ /* GodotModule.mm */
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
+ #import "GodotModule.h"
27
+ #import <UIKit/UIKit.h>
28
+
29
+ #define LOG_TAG "GodotModule"
30
+ #include "godot-log.h"
31
+
32
+ #include <libgodot/libgodot.h>
33
+ #include <godot_cpp/classes/display_server_embedded.hpp>
34
+ #include <godot_cpp/classes/engine.hpp>
35
+ #include <godot_cpp/classes/gd_extension_manager.hpp>
36
+ #include <godot_cpp/classes/rendering_native_surface.hpp>
37
+ #include <godot_cpp/classes/rendering_native_surface_apple.hpp>
38
+ #include <godot_cpp/godot.hpp>
39
+
40
+ #include <dlfcn.h>
41
+
42
+ #include <algorithm>
43
+ #include <map>
44
+ #include <mutex>
45
+ #include <string>
46
+
47
+ static constexpr NSInteger kGodotMaxFramesPerSecond = 60;
48
+
49
+ static NSInteger getGodotPreferredFramesPerSecond() {
50
+ godot::Engine *engine = godot::Engine::get_singleton();
51
+ if (!engine) {
52
+ return kGodotMaxFramesPerSecond;
53
+ }
54
+
55
+ const int32_t configuredMaxFps = engine->get_max_fps();
56
+ if (configuredMaxFps <= 0) {
57
+ return kGodotMaxFramesPerSecond;
58
+ }
59
+
60
+ const NSInteger requestedFramesPerSecond = static_cast<NSInteger>(configuredMaxFps);
61
+ return std::min(requestedFramesPerSecond, kGodotMaxFramesPerSecond);
62
+ }
63
+
64
+ static void configureGodotDisplayLink(CADisplayLink *displayLink) {
65
+ const NSInteger preferredFramesPerSecond = getGodotPreferredFramesPerSecond();
66
+ if (@available(iOS 15.0, *)) {
67
+ // Apple gives 30 Hz and 60 Hz special priority for games. Allowing the
68
+ // system to fall back to 30 Hz also handles thermal and power policies
69
+ // more gracefully than demanding an exact 60 Hz cadence.
70
+ const NSInteger minimumFramesPerSecond =
71
+ std::min<NSInteger>(30, preferredFramesPerSecond);
72
+ displayLink.preferredFrameRateRange = CAFrameRateRangeMake(
73
+ minimumFramesPerSecond,
74
+ preferredFramesPerSecond,
75
+ preferredFramesPerSecond);
76
+ } else {
77
+ displayLink.preferredFramesPerSecond = preferredFramesPerSecond;
78
+ }
79
+ }
80
+
81
+ @interface GodotThread : NSObject
82
+
83
+ // Method to start the thread and run loop
84
+ - (void)start;
85
+
86
+ // Method to submit blocks to the background thread for execution
87
+ - (void)performBlock:(void (^)(void))block waitUntilDone:(bool)wait;
88
+
89
+ - (void)scheduleBlock:(void (^)(void))block;
90
+
91
+ - (void)step:(CADisplayLink *)sender;
92
+
93
+ @end
94
+
95
+ @interface GodotThread ()
96
+
97
+ // Strong reference to the thread
98
+ @property(nonatomic, strong) NSThread *thread;
99
+
100
+ // Run loop source and port
101
+ @property(nonatomic, strong) NSPort *runLoopPort;
102
+
103
+ @end
104
+
105
+ @implementation GodotThread
106
+
107
+ // Method to start the background thread
108
+ - (void)start {
109
+ self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadEntryPoint) object:nil];
110
+ self.thread.qualityOfService = NSQualityOfServiceUserInteractive;
111
+ [self.thread start];
112
+ }
113
+
114
+ // Entry point of the background thread
115
+ - (void)threadEntryPoint {
116
+ @autoreleasepool {
117
+ // Set up the run loop
118
+ self.runLoopPort = [NSPort port];
119
+ [[NSRunLoop currentRunLoop] addPort:self.runLoopPort forMode:NSDefaultRunLoopMode];
120
+
121
+ // Keep the run loop running
122
+ [[NSRunLoop currentRunLoop] run];
123
+ }
124
+ }
125
+
126
+ // Method to perform a block on the background thread
127
+ - (void)performBlock:(void (^)(void))block waitUntilDone:(bool)wait {
128
+ if (!block) {
129
+ return;
130
+ }
131
+
132
+ // Ensure the block is executed on the background thread
133
+ if ([[NSThread currentThread] isEqual:self.thread]) {
134
+ block();
135
+ } else {
136
+ [self performSelector:@selector(executeBlock:) onThread:self.thread withObject:[block copy] waitUntilDone:wait];
137
+ }
138
+ }
139
+
140
+ - (void)scheduleBlock:(void (^)(void))block {
141
+ if (!block) {
142
+ return;
143
+ }
144
+
145
+ [self performSelector:@selector(executeBlock:) onThread:self.thread withObject:[block copy] waitUntilDone:FALSE];
146
+ }
147
+
148
+ // Helper method to execute the block on the background thread
149
+ - (void)executeBlock:(void (^)(void))block {
150
+ block();
151
+ }
152
+
153
+ - (void)step:(CADisplayLink *)sender {
154
+ GodotModule::get_singleton()->iterate();
155
+ }
156
+
157
+ @end
158
+
159
+ typedef GDExtensionObjectPtr (*libgodot_create_godot_instance_type)(int, char *[], GDExtensionInitializationFunction, InvokeCallbackFunction, ExecutorData, InvokeCallbackFunction, ExecutorData, LogCallbackFunction, LogCallbackData);
160
+ typedef void (*libgodot_destroy_godot_instance_type)(GDExtensionObjectPtr p_godot_instance);
161
+
162
+ struct ApplePlatformData : PlatformData {
163
+ CALayer *mainWindowLayer;
164
+ godot::Ref<godot::RenderingNativeSurface> mainSurface;
165
+ CGFloat contentScaleFactor = 1.0;
166
+ CADisplayLink *displayLink = nullptr;
167
+ bool in_background = true;
168
+ bool paused = false;
169
+ void *handle = nullptr;
170
+ libgodot_create_godot_instance_type func_libgodot_create_godot_instance = nullptr;
171
+ libgodot_destroy_godot_instance_type func_libgodot_destroy_godot_instance = nullptr;
172
+
173
+ std::map<std::string, std::function<void(bool)>> windowUpdateCallbacks;
174
+ std::map<void *, std::string> handleToWindowName;
175
+ std::mutex windowUpdateMutex;
176
+ std::mutex createMutex;
177
+ GodotThread *thread = nil;
178
+
179
+ ApplePlatformData() {
180
+ thread = [[GodotThread alloc] init];
181
+ [thread start];
182
+ }
183
+ };
184
+
185
+ GodotModule *GodotModule::get_singleton() {
186
+ static GodotModule *singleton = new GodotModule(new ApplePlatformData());
187
+ return singleton;
188
+ }
189
+
190
+ // The engine/module is process-wide, so these observers survive JS reloads too.
191
+ static void observeApplicationLifecycle(GodotModule *module) {
192
+ static dispatch_once_t once;
193
+ dispatch_once(&once, ^{
194
+ void (^install)(void) = ^{
195
+ NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
196
+ static NSArray *observers;
197
+ observers = @[
198
+ [center addObserverForName:UIApplicationWillResignActiveNotification
199
+ object:nil queue:[NSOperationQueue mainQueue]
200
+ usingBlock:^(NSNotification *notification) { module->appPause(); }],
201
+ [center addObserverForName:UIApplicationDidBecomeActiveNotification
202
+ object:nil queue:[NSOperationQueue mainQueue]
203
+ usingBlock:^(NSNotification *notification) { module->appResume(); }]
204
+ ];
205
+ if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
206
+ module->appResume();
207
+ } else {
208
+ module->appPause();
209
+ }
210
+ };
211
+ if ([NSThread isMainThread]) {
212
+ install();
213
+ } else {
214
+ dispatch_sync(dispatch_get_main_queue(), install);
215
+ }
216
+ });
217
+ }
218
+
219
+ extern "C" {
220
+
221
+ static void initialize_default_module(godot::ModuleInitializationLevel p_level) {
222
+ if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) {
223
+ return;
224
+ }
225
+ }
226
+
227
+ static void uninitialize_default_module(godot::ModuleInitializationLevel p_level) {
228
+ if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) {
229
+ return;
230
+ }
231
+ }
232
+
233
+ static void logCallBack(LogCallbackData p_data, const char *p_log_message, bool p_err) {
234
+ GodotModule *gm = (GodotModule *)p_data;
235
+ gm->log(p_log_message, p_err);
236
+ }
237
+
238
+ GDExtensionBool GDE_EXPORT gdextension_default_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {
239
+ godot::GDExtensionBinding::InitObject init_object(p_get_proc_address, p_library, r_initialization);
240
+
241
+ init_object.register_initializer(initialize_default_module);
242
+ init_object.register_terminator(uninitialize_default_module);
243
+ init_object.set_minimum_library_initialization_level(godot::MODULE_INITIALIZATION_LEVEL_SCENE);
244
+
245
+ return init_object.init();
246
+ }
247
+ }
248
+
249
+ godot::GodotInstance *GodotModule::get_or_create_instance(std::vector<std::string> args) {
250
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
251
+
252
+ // Make sure that we only run this method once at a time.
253
+ std::lock_guard createLock(data->createMutex);
254
+
255
+ {
256
+ std::lock_guard lock(_mutex);
257
+ if (_instance) {
258
+ return _instance;
259
+ }
260
+ }
261
+
262
+ observeApplicationLifecycle(this);
263
+
264
+ void *handle = nullptr;
265
+ if (data->func_libgodot_create_godot_instance == nullptr) {
266
+ handle = dlopen("libgodot.framework/libgodot", RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST);
267
+
268
+ if (handle == nullptr) {
269
+ NSLog(@"Unable to open libgodot.framework: %s", dlerror());
270
+ return nullptr;
271
+ }
272
+ libgodot_create_godot_instance_type func_libgodot_create_godot_instance = (libgodot_create_godot_instance_type)dlsym(handle, "libgodot_create_godot_instance");
273
+
274
+ if (func_libgodot_create_godot_instance == nullptr) {
275
+ NSLog(@"Unable to load libgodot_create_godot_instance symbol: %s", dlerror());
276
+ dlclose(handle);
277
+ return nullptr;
278
+ }
279
+
280
+ {
281
+ std::lock_guard lock(_mutex);
282
+ data->func_libgodot_create_godot_instance = func_libgodot_create_godot_instance;
283
+ }
284
+ }
285
+
286
+ godot::GodotInstance *instance = nullptr;
287
+
288
+ std::vector<std::string> cmdline{ [[[NSBundle mainBundle] executablePath] UTF8String] };
289
+ for (const std::string &arg : args) {
290
+ cmdline.push_back(arg);
291
+ }
292
+
293
+ std::vector<const char *> cargs{};
294
+ for (const std::string &arg : cmdline) {
295
+ cargs.push_back(arg.c_str());
296
+ }
297
+
298
+ GDExtensionObjectPtr instance_ptr = data->func_libgodot_create_godot_instance(cargs.size(), (char **)cargs.data(), gdextension_default_init, nullptr, nullptr, nullptr, nullptr, logCallBack, this); // TODO: Do we want to use the extra 4 params?
299
+ if (instance_ptr == nullptr) {
300
+ // Unable to start Godot
301
+ NSLog(@"Unable to start Godot");
302
+ if (handle) {
303
+ dlclose(handle);
304
+ handle = nullptr;
305
+ {
306
+ std::lock_guard lock(_mutex);
307
+ data->func_libgodot_create_godot_instance = nullptr;
308
+ }
309
+ }
310
+ return nullptr;
311
+ }
312
+
313
+ instance = reinterpret_cast<godot::GodotInstance *>(godot::internal::get_object_instance_binding(instance_ptr));
314
+
315
+ CGRect screen = [[UIScreen mainScreen] bounds];
316
+ CGFloat contentScaleFactor = [[UIScreen mainScreen] scale];
317
+
318
+ LOGI("Initialize Main Window Layer on the main thread");
319
+
320
+ // Godot automatically creates the correct type of layer based on the selected rendering driver
321
+ godot::Ref<godot::RenderingNativeSurfaceApple> appleSurface = godot::RenderingNativeSurfaceApple::create(0);
322
+ CALayer __block *mainWindowLayer = (__bridge CALayer *)(void *)appleSurface->get_layer();
323
+ mainWindowLayer.bounds = CGRectMake(0, 0, screen.size.width, screen.size.height);
324
+ mainWindowLayer.position = CGPointMake(0, 0);
325
+ mainWindowLayer.anchorPoint = CGPointMake(0, 0);
326
+ mainWindowLayer.contentsScale = contentScaleFactor;
327
+
328
+ godot::RenderingNativeSurface *ptr = godot::Object::cast_to<godot::RenderingNativeSurface>(appleSurface.ptr());
329
+ godot::Ref<godot::RenderingNativeSurface> nativeSurface(ptr);
330
+
331
+ godot::DisplayServerEmbedded::set_native_surface(nativeSurface);
332
+
333
+ instance->start();
334
+ bool inBackground;
335
+
336
+ {
337
+ std::lock_guard lock(_mutex);
338
+
339
+ data->mainWindowLayer = mainWindowLayer;
340
+ data->mainSurface = nativeSurface;
341
+ data->contentScaleFactor = contentScaleFactor;
342
+ data->handle = handle;
343
+
344
+ _instance = instance;
345
+ inBackground = data->in_background;
346
+ }
347
+
348
+ if (inBackground) {
349
+ instance->pause();
350
+ }
351
+ updateState();
352
+ updateWindows(true);
353
+
354
+ return instance;
355
+ }
356
+
357
+ double GodotModule::get_content_scale_factor() {
358
+ std::lock_guard lock(_mutex);
359
+ if (!_instance) {
360
+ return 1.0;
361
+ }
362
+
363
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
364
+
365
+ return data->contentScaleFactor;
366
+ }
367
+
368
+ void GodotModule::destroy_instance() {
369
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
370
+
371
+ // Make sure that we only run this method once at a time.
372
+ std::lock_guard createLock(data->createMutex);
373
+
374
+ if (!_instance) {
375
+ NSLog(@"Godot instance is already destroyed.");
376
+ return;
377
+ }
378
+
379
+ if (!data->func_libgodot_destroy_godot_instance) {
380
+ libgodot_destroy_godot_instance_type func_libgodot_destroy_godot_instance = nullptr;
381
+ void *handle = nullptr;
382
+
383
+ {
384
+ std::lock_guard lock(_mutex);
385
+ handle = data->handle;
386
+ }
387
+
388
+ if (handle == nullptr) {
389
+ NSLog(@"Unable to open libgodot.framework: %s", dlerror());
390
+ return;
391
+ }
392
+ func_libgodot_destroy_godot_instance = (libgodot_destroy_godot_instance_type)dlsym(handle, "libgodot_destroy_godot_instance");
393
+
394
+ if (func_libgodot_destroy_godot_instance == nullptr) {
395
+ NSLog(@"Unable to load libgodot_destroy_godot_instance symbol: %s", dlerror());
396
+ dlclose(handle);
397
+ handle = nullptr;
398
+ return;
399
+ }
400
+
401
+ {
402
+ std::lock_guard lock(_mutex);
403
+ data->func_libgodot_destroy_godot_instance = func_libgodot_destroy_godot_instance;
404
+ }
405
+ }
406
+
407
+ {
408
+ std::lock_guard lock(_mutex);
409
+
410
+ [data->displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
411
+ data->displayLink = nil;
412
+
413
+ updateWindows(false);
414
+
415
+ godot::DisplayServerEmbedded::set_native_surface(godot::Ref<godot::RenderingNativeSurface>(nullptr));
416
+ data->mainWindowLayer = nil;
417
+ data->mainSurface = godot::Ref<godot::RenderingNativeSurface>(nullptr);
418
+
419
+ data->func_libgodot_destroy_godot_instance(_instance->_owner);
420
+ _instance = nullptr;
421
+ godot::GDExtensionBinding::deinit();
422
+
423
+ dlclose(data->handle);
424
+ data->handle = nullptr;
425
+ }
426
+ }
427
+
428
+ godot::Ref<godot::RenderingNativeSurface> GodotModule::get_main_rendering_surface() {
429
+ std::lock_guard lock(_mutex);
430
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
431
+ return data->mainSurface;
432
+ }
433
+
434
+ void *GodotModule::get_main_rendering_layer() {
435
+ std::lock_guard lock(_mutex);
436
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
437
+ return (__bridge void *)data->mainWindowLayer;
438
+ }
439
+
440
+ void GodotModule::focus_out() {
441
+ std::lock_guard lock(_mutex);
442
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
443
+ data->in_background = true;
444
+ [data->thread scheduleBlock:^{
445
+ std::lock_guard lock(_mutex);
446
+ if (_instance) {
447
+ _instance->focus_out();
448
+ }
449
+ }];
450
+ updateState();
451
+ }
452
+
453
+ void GodotModule::focus_in() {
454
+ std::lock_guard lock(_mutex);
455
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
456
+ data->in_background = false;
457
+ [data->thread scheduleBlock:^{
458
+ std::lock_guard lock(_mutex);
459
+ if (_instance) {
460
+ _instance->focus_in();
461
+ }
462
+ }];
463
+ updateState();
464
+ }
465
+
466
+ bool GodotModule::is_paused() {
467
+ std::lock_guard lock(_mutex);
468
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
469
+ return data->paused;
470
+ }
471
+
472
+ void GodotModule::appPause() {
473
+ std::lock_guard lock(_mutex);
474
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
475
+ if (data->in_background) {
476
+ return;
477
+ }
478
+ UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
479
+
480
+ }];
481
+ data->in_background = true;
482
+ [data->thread scheduleBlock:^{
483
+ godot::GodotInstance *instance = get_instance();
484
+ if (instance) {
485
+ instance->pause();
486
+ }
487
+ [[UIApplication sharedApplication] endBackgroundTask:bgTask];
488
+ }];
489
+ updateState();
490
+ }
491
+
492
+ void GodotModule::appResume() {
493
+ std::lock_guard lock(_mutex);
494
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
495
+ if (!data->in_background) {
496
+ return;
497
+ }
498
+ data->in_background = false;
499
+ [data->thread scheduleBlock:^{
500
+ godot::GodotInstance *instance = get_instance();
501
+ if (instance) {
502
+ instance->resume();
503
+ }
504
+ }];
505
+ updateState();
506
+ }
507
+
508
+ void GodotModule::pause() {
509
+ std::lock_guard lock(_mutex);
510
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
511
+ data->paused = true;
512
+ updateState();
513
+ }
514
+
515
+ void GodotModule::resume() {
516
+ std::lock_guard lock(_mutex);
517
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
518
+ data->paused = false;
519
+ updateState();
520
+ }
521
+
522
+ void GodotModule::updateState() {
523
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
524
+ [data->thread scheduleBlock:^{
525
+ std::lock_guard lock(_mutex);
526
+ ApplePlatformData *threadData = static_cast<ApplePlatformData *>(_data);
527
+ if (!_instance) {
528
+ return;
529
+ }
530
+ if (threadData->in_background || threadData->paused) {
531
+ if (threadData->displayLink) {
532
+ threadData->displayLink.paused = true;
533
+ [threadData->displayLink invalidate];
534
+ threadData->displayLink = nil;
535
+ }
536
+ } else if (!threadData->displayLink) {
537
+ threadData->displayLink = [CADisplayLink displayLinkWithTarget:threadData->thread
538
+ selector:@selector(step:)];
539
+ configureGodotDisplayLink(threadData->displayLink);
540
+ [threadData->displayLink addToRunLoop:[NSRunLoop currentRunLoop]
541
+ forMode:NSRunLoopCommonModes];
542
+ }
543
+ }];
544
+ }
545
+
546
+ class CPPCallable : public godot::CallableCustom {
547
+ // friend bool javascript_callable_compare_equal_func(const godot::CallableCustom *p_a, const godot::CallableCustom *p_b) {
548
+ // if (p_a == p_b) return true;
549
+ // const JavascriptCallable *a = static_cast<const JavascriptCallable *>(p_a);
550
+ // const JavascriptCallable *b = static_cast<const JavascriptCallable *>(p_b);
551
+ // return jsi::Function::strictEquals(a->_rt, a->_func, b->_func);
552
+ // };
553
+
554
+ std::function<void(const godot::Variant **, int, godot::Variant &, GDExtensionCallError &)> _func;
555
+
556
+ public:
557
+ CPPCallable(std::function<void(const godot::Variant **, int, godot::Variant &, GDExtensionCallError &)> f) :
558
+ _func(f) {}
559
+
560
+ uint32_t hash() const override {
561
+ return 0; // Use default hash function
562
+ }
563
+ godot::String get_as_text() const override {
564
+ return godot::String("CPPCallable");
565
+ }
566
+ CompareEqualFunc get_compare_equal_func() const override {
567
+ return nullptr;
568
+ }
569
+
570
+ CompareLessFunc get_compare_less_func() const override {
571
+ return nullptr;
572
+ }
573
+
574
+ bool is_valid() const override {
575
+ return true;
576
+ }
577
+ godot::ObjectID get_object() const override {
578
+ return godot::ObjectID();
579
+ }
580
+ void call(const godot::Variant **p_arguments, int p_argcount, godot::Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
581
+ _func(p_arguments, p_argcount, r_return_value, r_call_error);
582
+ r_call_error.error = GDEXTENSION_CALL_OK;
583
+ }
584
+ };
585
+
586
+ godot::Callable GodotModule::create_callable(std::function<void(const godot::Variant **, int, godot::Variant &, GDExtensionCallError &)> f) {
587
+ return godot::Callable(memnew(CPPCallable(f)));
588
+ }
589
+
590
+ void GodotModule::registerWindowUpdateCallback(std::string name, void *handle, std::function<void(bool)> f, void *ref) {
591
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
592
+ std::lock_guard lock(data->windowUpdateMutex);
593
+ NSLog(@"Registering Window: %llx, %s", (uint64_t)handle, name.c_str());
594
+ if (data->handleToWindowName.contains(handle)) {
595
+ std::string currentName = data->handleToWindowName[handle];
596
+ if (currentName != name) {
597
+ NSLog(@"ERROR: registerWindowUpdateCallback: Unable to register a different name for the same handle");
598
+ return;
599
+ }
600
+ } else {
601
+ data->handleToWindowName[handle] = name;
602
+ }
603
+ data->windowUpdateCallbacks[name] = f;
604
+ }
605
+
606
+ void GodotModule::unregisterWindowUpdateCallback(void *handle) {
607
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
608
+ std::lock_guard lock(data->windowUpdateMutex);
609
+ if (data->handleToWindowName.contains(handle)) {
610
+ std::string name = data->handleToWindowName[handle];
611
+ NSLog(@"Unregistering Window: %llx, %s", (uint64_t)handle, name.c_str());
612
+ data->windowUpdateCallbacks.erase(name);
613
+ data->handleToWindowName.erase(handle);
614
+ }
615
+ }
616
+
617
+ void GodotModule::updateWindow(std::string name, bool adding) {
618
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
619
+ std::function<void(bool)> cb;
620
+ {
621
+ std::lock_guard lock(data->windowUpdateMutex);
622
+ if (!data->windowUpdateCallbacks.contains(name)) {
623
+ return;
624
+ }
625
+ cb = data->windowUpdateCallbacks[name];
626
+ }
627
+ cb(adding);
628
+ }
629
+
630
+ void GodotModule::updateWindows(bool adding) {
631
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
632
+ std::map<std::string, std::function<void(bool)>> callbacks;
633
+ {
634
+ std::lock_guard lock(data->windowUpdateMutex);
635
+ callbacks = data->windowUpdateCallbacks;
636
+ }
637
+ for (const std::pair<std::string, std::function<void(bool)>> elem : callbacks) {
638
+ NSLog(@"Updating Window: %s", elem.first.c_str());
639
+ elem.second(adding);
640
+ }
641
+ }
642
+
643
+ void GodotModule::runOnGodotThread(std::function<void()> f, bool wait) {
644
+ ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
645
+ [data->thread
646
+ performBlock:^{
647
+ f();
648
+ }
649
+ waitUntilDone:wait];
650
+ }
651
+
652
+ void GodotModule::iterate() {
653
+ godot::GodotInstance *instance = nullptr;
654
+ {
655
+ std::lock_guard lock(_mutex);
656
+ instance = _instance;
657
+ }
658
+ if (instance && instance->is_started()) {
659
+ instance->iteration();
660
+ }
661
+ }
@@ -0,0 +1,35 @@
1
+ /**************************************************************************/
2
+ /* NativeGodotModuleProvider.h */
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
+ #import <Foundation/Foundation.h>
27
+ #import <ReactCommon/RCTTurboModule.h>
28
+
29
+ NS_ASSUME_NONNULL_BEGIN
30
+
31
+ @interface NativeGodotModuleProvider : NSObject <RCTModuleProvider>
32
+
33
+ @end
34
+
35
+ NS_ASSUME_NONNULL_END