@preeternal/react-native-godot 0.1.0 → 0.2.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.
@@ -0,0 +1,52 @@
1
+ /**************************************************************************/
2
+ /* LibGodotABI.h */
3
+ /**************************************************************************/
4
+ /* LibGodot C ABI used by the native bridge. */
5
+ /* Keep this bridge-local declaration in sync with the selected LibGodot headers. */
6
+ /**************************************************************************/
7
+
8
+ #pragma once
9
+
10
+ #include <gdextension_interface.h>
11
+ #include <godot_cpp/variant/string.hpp>
12
+ #include <godot_cpp/variant/string_name.hpp>
13
+
14
+ #include <stdbool.h>
15
+
16
+ // Generated builtin layouts depend on the target architecture. Mixing ARM32
17
+ // headers with an ARM64 library corrupts memory even when the C ABI matches.
18
+ static_assert(sizeof(godot::StringName) == sizeof(void *),
19
+ "LibGodot godot-cpp StringName headers do not match the target ABI. Use headers generated for this architecture.");
20
+ static_assert(sizeof(godot::String) == sizeof(void *),
21
+ "LibGodot godot-cpp String headers do not match the target ABI. Use headers generated for this architecture.");
22
+
23
+ #if defined(__ANDROID__) || defined(ANDROID)
24
+ #include <jni.h>
25
+ #endif
26
+
27
+ using RNLibGodotLogCallbackData = void *;
28
+ using RNLibGodotLogCallbackFunction = void (*)(RNLibGodotLogCallbackData p_data, const char *p_log_message, bool p_err);
29
+
30
+ using RNLibGodotCreateFunction = GDExtensionObjectPtr (*)(
31
+ int p_argc,
32
+ char *p_argv[],
33
+ GDExtensionInitializationFunction p_init_func,
34
+ RNLibGodotLogCallbackFunction p_log_func,
35
+ RNLibGodotLogCallbackData p_log_data);
36
+
37
+ using RNLibGodotDestroyFunction = void (*)(GDExtensionObjectPtr p_godot_instance);
38
+
39
+ #if defined(__ANDROID__) || defined(ANDROID)
40
+ using RNLibGodotCreateAndroidFunction = GDExtensionObjectPtr (*)(
41
+ int p_argc,
42
+ char *p_argv[],
43
+ GDExtensionInitializationFunction p_init_func,
44
+ RNLibGodotLogCallbackFunction p_log_func,
45
+ RNLibGodotLogCallbackData p_log_data,
46
+ JNIEnv *p_env,
47
+ jobject p_asset_manager,
48
+ jobject p_net_utils,
49
+ jobject p_directory_access_handler,
50
+ jobject p_file_access_handler,
51
+ jobject p_godot_io_wrapper);
52
+ #endif
@@ -719,7 +719,7 @@ jsi::Value createNativeGodotModule(jsi::Runtime &rt, const std::shared_ptr<faceb
719
719
  GodotModule *mod = GodotModule::get_singleton();
720
720
  godot::GodotInstance *instance = mod->get_or_create_instance(godotArgs);
721
721
  if (!instance) {
722
- return jsi::Value::undefined();
722
+ throw jsi::JSError(rt, "Failed to create Godot instance. See the native engine log for the startup error.");
723
723
  }
724
724
  return GodotHostObject::godotVariantToJsiValue(workletContext, rt, godot::Variant(instance));
725
725
  };
@@ -29,7 +29,6 @@
29
29
  #define LOG_TAG "GodotModule"
30
30
  #include "godot-log.h"
31
31
 
32
- #include <libgodot/libgodot.h>
33
32
  #include <godot_cpp/classes/display_server_embedded.hpp>
34
33
  #include <godot_cpp/classes/engine.hpp>
35
34
  #include <godot_cpp/classes/gd_extension_manager.hpp>
@@ -43,6 +42,11 @@
43
42
  #include <map>
44
43
  #include <mutex>
45
44
  #include <string>
45
+ #include <vector>
46
+
47
+ #include "../cpp/AppleRendererPolicy.h"
48
+ #include "../cpp/LibGodotABI.h"
49
+ #include <TargetConditionals.h>
46
50
 
47
51
  static constexpr NSInteger kGodotMaxFramesPerSecond = 60;
48
52
 
@@ -156,9 +160,6 @@ static void configureGodotDisplayLink(CADisplayLink *displayLink) {
156
160
 
157
161
  @end
158
162
 
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
163
  struct ApplePlatformData : PlatformData {
163
164
  CALayer *mainWindowLayer;
164
165
  godot::Ref<godot::RenderingNativeSurface> mainSurface;
@@ -167,11 +168,14 @@ struct ApplePlatformData : PlatformData {
167
168
  bool in_background = true;
168
169
  bool paused = false;
169
170
  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;
171
+ RNLibGodotCreateFunction func_libgodot_create_godot_instance = nullptr;
172
+ RNLibGodotDestroyFunction func_libgodot_destroy_godot_instance = nullptr;
173
+
174
+ struct WindowCallback {
175
+ std::string name;
176
+ std::function<void(bool)> callback;
177
+ };
178
+ std::map<void *, WindowCallback> windowUpdateCallbacks;
175
179
  std::mutex windowUpdateMutex;
176
180
  std::mutex createMutex;
177
181
  GodotThread *thread = nil;
@@ -196,11 +200,17 @@ static void observeApplicationLifecycle(GodotModule *module) {
196
200
  static NSArray *observers;
197
201
  observers = @[
198
202
  [center addObserverForName:UIApplicationWillResignActiveNotification
199
- object:nil queue:[NSOperationQueue mainQueue]
200
- usingBlock:^(NSNotification *notification) { module->appPause(); }],
203
+ object:nil
204
+ queue:[NSOperationQueue mainQueue]
205
+ usingBlock:^(NSNotification *notification) {
206
+ module->appPause();
207
+ }],
201
208
  [center addObserverForName:UIApplicationDidBecomeActiveNotification
202
- object:nil queue:[NSOperationQueue mainQueue]
203
- usingBlock:^(NSNotification *notification) { module->appResume(); }]
209
+ object:nil
210
+ queue:[NSOperationQueue mainQueue]
211
+ usingBlock:^(NSNotification *notification) {
212
+ module->appResume();
213
+ }]
204
214
  ];
205
215
  if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
206
216
  module->appResume();
@@ -230,7 +240,12 @@ static void uninitialize_default_module(godot::ModuleInitializationLevel p_level
230
240
  }
231
241
  }
232
242
 
233
- static void logCallBack(LogCallbackData p_data, const char *p_log_message, bool p_err) {
243
+ static void logCallBack(RNLibGodotLogCallbackData p_data, const char *p_log_message, bool p_err) {
244
+ if (p_err) {
245
+ LOGE("%s", p_log_message);
246
+ } else {
247
+ LOGI("%s", p_log_message);
248
+ }
234
249
  GodotModule *gm = (GodotModule *)p_data;
235
250
  gm->log(p_log_message, p_err);
236
251
  }
@@ -247,6 +262,15 @@ GDExtensionBool GDE_EXPORT gdextension_default_init(GDExtensionInterfaceGetProcA
247
262
  }
248
263
 
249
264
  godot::GodotInstance *GodotModule::get_or_create_instance(std::vector<std::string> args) {
265
+ godot::GodotInstance *result = nullptr;
266
+ runOnGodotThread([this, &args, &result]() {
267
+ result = get_or_create_instance_on_godot_thread(std::move(args));
268
+ },
269
+ true);
270
+ return result;
271
+ }
272
+
273
+ godot::GodotInstance *GodotModule::get_or_create_instance_on_godot_thread(std::vector<std::string> args) {
250
274
  ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
251
275
 
252
276
  // Make sure that we only run this method once at a time.
@@ -259,28 +283,29 @@ godot::GodotInstance *GodotModule::get_or_create_instance(std::vector<std::strin
259
283
  }
260
284
  }
261
285
 
286
+ const auto renderer = rn_godot::configure_apple_renderer(args, TARGET_OS_SIMULATOR);
287
+ if (!renderer.error.empty()) {
288
+ LOGE("Invalid iOS renderer request: %s", renderer.error.c_str());
289
+ return nullptr;
290
+ }
291
+ LOGI("iOS renderer target=%s requested=%s/%s selected=%s/%s",
292
+ TARGET_OS_SIMULATOR ? "simulator" : "device",
293
+ renderer.requested_driver.empty() ? "default" : renderer.requested_driver.c_str(),
294
+ renderer.requested_method.empty() ? "default" : renderer.requested_method.c_str(),
295
+ renderer.driver.c_str(), renderer.method.c_str());
262
296
  observeApplicationLifecycle(this);
263
297
 
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
- }
298
+ void *handle = dlopen("libgodot.framework/libgodot", RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST);
299
+ if (!handle) {
300
+ NSLog(@"Unable to open libgodot.framework: %s", dlerror());
301
+ return nullptr;
302
+ }
303
+ auto create = reinterpret_cast<RNLibGodotCreateFunction>(dlsym(handle, "libgodot_create_godot_instance"));
304
+ auto destroy = reinterpret_cast<RNLibGodotDestroyFunction>(dlsym(handle, "libgodot_destroy_godot_instance"));
305
+ if (!create || !destroy) {
306
+ NSLog(@"LibGodot requires both create and destroy symbols");
307
+ dlclose(handle);
308
+ return nullptr;
284
309
  }
285
310
 
286
311
  godot::GodotInstance *instance = nullptr;
@@ -295,22 +320,35 @@ godot::GodotInstance *GodotModule::get_or_create_instance(std::vector<std::strin
295
320
  cargs.push_back(arg.c_str());
296
321
  }
297
322
 
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");
323
+ GDExtensionObjectPtr instance_ptr = create(
324
+ static_cast<int>(cargs.size()),
325
+ (char **)cargs.data(),
326
+ gdextension_default_init,
327
+ logCallBack,
328
+ this);
329
+ // Raw teardown must work even if godot-cpp cannot bind the returned object.
330
+ // Call only after releasing every local Godot Ref/Variant/Callable.
331
+ auto cleanupFailedCreate = [&]() {
332
+ if (instance_ptr) {
333
+ destroy(instance_ptr);
334
+ }
335
+ godot::GDExtensionBinding::deinit();
302
336
  if (handle) {
303
337
  dlclose(handle);
304
- handle = nullptr;
305
- {
306
- std::lock_guard lock(_mutex);
307
- data->func_libgodot_create_godot_instance = nullptr;
308
- }
309
338
  }
339
+ };
340
+ if (!instance_ptr) {
341
+ NSLog(@"Unable to create GodotInstance");
342
+ cleanupFailedCreate();
310
343
  return nullptr;
311
344
  }
312
345
 
313
346
  instance = reinterpret_cast<godot::GodotInstance *>(godot::internal::get_object_instance_binding(instance_ptr));
347
+ if (!instance) {
348
+ NSLog(@"Unable to bind GodotInstance returned by LibGodot");
349
+ cleanupFailedCreate();
350
+ return nullptr;
351
+ }
314
352
 
315
353
  CGRect screen = [[UIScreen mainScreen] bounds];
316
354
  CGFloat contentScaleFactor = [[UIScreen mainScreen] scale];
@@ -319,6 +357,11 @@ godot::GodotInstance *GodotModule::get_or_create_instance(std::vector<std::strin
319
357
 
320
358
  // Godot automatically creates the correct type of layer based on the selected rendering driver
321
359
  godot::Ref<godot::RenderingNativeSurfaceApple> appleSurface = godot::RenderingNativeSurfaceApple::create(0);
360
+ if (appleSurface.is_null()) {
361
+ NSLog(@"Unable to create the main Apple rendering surface");
362
+ cleanupFailedCreate();
363
+ return nullptr;
364
+ }
322
365
  CALayer __block *mainWindowLayer = (__bridge CALayer *)(void *)appleSurface->get_layer();
323
366
  mainWindowLayer.bounds = CGRectMake(0, 0, screen.size.width, screen.size.height);
324
367
  mainWindowLayer.position = CGPointMake(0, 0);
@@ -330,7 +373,14 @@ godot::GodotInstance *GodotModule::get_or_create_instance(std::vector<std::strin
330
373
 
331
374
  godot::DisplayServerEmbedded::set_native_surface(nativeSurface);
332
375
 
333
- instance->start();
376
+ if (!instance->start()) {
377
+ NSLog(@"Unable to start GodotInstance");
378
+ godot::DisplayServerEmbedded::set_native_surface(godot::Ref<godot::RenderingNativeSurface>());
379
+ nativeSurface.unref();
380
+ appleSurface.unref();
381
+ cleanupFailedCreate();
382
+ return nullptr;
383
+ }
334
384
  bool inBackground;
335
385
 
336
386
  {
@@ -340,12 +390,16 @@ godot::GodotInstance *GodotModule::get_or_create_instance(std::vector<std::strin
340
390
  data->mainSurface = nativeSurface;
341
391
  data->contentScaleFactor = contentScaleFactor;
342
392
  data->handle = handle;
393
+ data->func_libgodot_create_godot_instance = create;
394
+ data->func_libgodot_destroy_godot_instance = destroy;
343
395
 
344
396
  _instance = instance;
397
+ _instanceGeneration.store(++_lastInstanceGeneration);
345
398
  inBackground = data->in_background;
346
399
  }
347
400
 
348
401
  if (inBackground) {
402
+ instance->focus_out();
349
403
  instance->pause();
350
404
  }
351
405
  updateState();
@@ -366,6 +420,13 @@ double GodotModule::get_content_scale_factor() {
366
420
  }
367
421
 
368
422
  void GodotModule::destroy_instance() {
423
+ runOnGodotThread([this]() {
424
+ destroy_instance_on_godot_thread();
425
+ },
426
+ true);
427
+ }
428
+
429
+ void GodotModule::destroy_instance_on_godot_thread() {
369
430
  ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
370
431
 
371
432
  // Make sure that we only run this method once at a time.
@@ -377,31 +438,8 @@ void GodotModule::destroy_instance() {
377
438
  }
378
439
 
379
440
  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
- }
441
+ NSLog(@"Missing destroy function for the active Godot instance");
442
+ return;
405
443
  }
406
444
 
407
445
  {
@@ -411,6 +449,7 @@ void GodotModule::destroy_instance() {
411
449
  data->displayLink = nil;
412
450
 
413
451
  updateWindows(false);
452
+ _instanceGeneration.store(0);
414
453
 
415
454
  godot::DisplayServerEmbedded::set_native_surface(godot::Ref<godot::RenderingNativeSurface>(nullptr));
416
455
  data->mainWindowLayer = nil;
@@ -422,6 +461,9 @@ void GodotModule::destroy_instance() {
422
461
 
423
462
  dlclose(data->handle);
424
463
  data->handle = nullptr;
464
+ data->func_libgodot_create_godot_instance = nullptr;
465
+ data->func_libgodot_destroy_godot_instance = nullptr;
466
+ data->paused = false;
425
467
  }
426
468
  }
427
469
 
@@ -482,6 +524,7 @@ void GodotModule::appPause() {
482
524
  [data->thread scheduleBlock:^{
483
525
  godot::GodotInstance *instance = get_instance();
484
526
  if (instance) {
527
+ instance->focus_out();
485
528
  instance->pause();
486
529
  }
487
530
  [[UIApplication sharedApplication] endBackgroundTask:bgTask];
@@ -500,6 +543,7 @@ void GodotModule::appResume() {
500
543
  godot::GodotInstance *instance = get_instance();
501
544
  if (instance) {
502
545
  instance->resume();
546
+ instance->focus_in();
503
547
  }
504
548
  }];
505
549
  updateState();
@@ -535,10 +579,10 @@ void GodotModule::updateState() {
535
579
  }
536
580
  } else if (!threadData->displayLink) {
537
581
  threadData->displayLink = [CADisplayLink displayLinkWithTarget:threadData->thread
538
- selector:@selector(step:)];
582
+ selector:@selector(step:)];
539
583
  configureGodotDisplayLink(threadData->displayLink);
540
584
  [threadData->displayLink addToRunLoop:[NSRunLoop currentRunLoop]
541
- forMode:NSRunLoopCommonModes];
585
+ forMode:NSRunLoopCommonModes];
542
586
  }
543
587
  }];
544
588
  }
@@ -590,53 +634,42 @@ godot::Callable GodotModule::create_callable(std::function<void(const godot::Var
590
634
  void GodotModule::registerWindowUpdateCallback(std::string name, void *handle, std::function<void(bool)> f, void *ref) {
591
635
  ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
592
636
  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;
637
+ data->windowUpdateCallbacks[handle] = { std::move(name), std::move(f) };
604
638
  }
605
639
 
606
640
  void GodotModule::unregisterWindowUpdateCallback(void *handle) {
607
641
  ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
608
642
  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
- }
643
+ data->windowUpdateCallbacks.erase(handle);
615
644
  }
616
645
 
617
646
  void GodotModule::updateWindow(std::string name, bool adding) {
618
647
  ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
619
- std::function<void(bool)> cb;
648
+ std::vector<std::function<void(bool)>> callbacks;
620
649
  {
621
650
  std::lock_guard lock(data->windowUpdateMutex);
622
- if (!data->windowUpdateCallbacks.contains(name)) {
623
- return;
651
+ for (const auto &[handle, entry] : data->windowUpdateCallbacks) {
652
+ if (entry.name == name) {
653
+ callbacks.push_back(entry.callback);
654
+ }
624
655
  }
625
- cb = data->windowUpdateCallbacks[name];
626
656
  }
627
- cb(adding);
657
+ for (const auto &callback : callbacks) {
658
+ callback(adding);
659
+ }
628
660
  }
629
661
 
630
662
  void GodotModule::updateWindows(bool adding) {
631
663
  ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
632
- std::map<std::string, std::function<void(bool)>> callbacks;
664
+ std::vector<std::function<void(bool)>> callbacks;
633
665
  {
634
666
  std::lock_guard lock(data->windowUpdateMutex);
635
- callbacks = data->windowUpdateCallbacks;
667
+ for (const auto &[handle, entry] : data->windowUpdateCallbacks) {
668
+ callbacks.push_back(entry.callback);
669
+ }
636
670
  }
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);
671
+ for (const auto &callback : callbacks) {
672
+ callback(adding);
640
673
  }
641
674
  }
642
675
 
@@ -649,7 +682,8 @@ void GodotModule::runOnGodotThread(std::function<void()> f, bool wait) {
649
682
  waitUntilDone:wait];
650
683
  }
651
684
 
652
- void GodotModule::iterate() {
685
+ void GodotModule::iterate(std::optional<uint64_t> frameGeneration) {
686
+ (void)frameGeneration;
653
687
  godot::GodotInstance *instance = nullptr;
654
688
  {
655
689
  std::lock_guard lock(_mutex);