@preeternal/react-native-godot 0.0.0-stage → 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.
- package/LICENSE +22 -0
- package/Package.swift +106 -0
- package/README.md +590 -2
- package/android/build.gradle +291 -0
- package/android/consumer-rules.pro +20 -0
- package/android/fix-prefab.gradle +54 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/CMakeLists.txt +78 -0
- package/android/src/main/cpp/GodotModule.cpp +764 -0
- package/android/src/main/cpp/OnLoad.cpp +66 -0
- package/android/src/main/cpp/libgodot_android.h +73 -0
- package/android/src/main/cpp/libgodot_jni.cpp +658 -0
- package/android/src/main/cpp/libgodot_jni.h +116 -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/RTNGodotIO.kt +123 -0
- package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotView.kt +92 -0
- package/android/src/main/java/com/preeternal/reactnativegodot/RTNGodotViewManager.kt +34 -0
- package/android/src/main/java/com/preeternal/reactnativegodot/RTNLibGodot.kt +215 -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 +169 -0
- package/cpp/AppleRendererPolicy.h +95 -0
- package/cpp/GodotModule.h +126 -0
- package/cpp/LibGodotABI.h +52 -0
- package/cpp/NativeGodotModule.cpp +825 -0
- package/cpp/NativeGodotModule.h +45 -0
- package/cpp/godot-log.h +64 -0
- package/ios/GodotModule.mm +695 -0
- package/ios/NativeGodotModuleProvider.h +35 -0
- package/ios/NativeGodotModuleProvider.mm +42 -0
- package/ios/RTNGodotView.h +42 -0
- package/ios/RTNGodotView.mm +671 -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 +282 -4
- package/prebuilt-utils.rb +46 -0
- package/react-native-godot.podspec +41 -0
- package/react-native.config.js +16 -0
- package/scripts/check-16kb.sh +136 -0
- package/scripts/check-packaged-artifacts.sh +156 -0
- package/scripts/ci/verify-ios-simulator.py +48 -0
- package/scripts/ci/verify-prebuilt-install.cjs +41 -0
- package/scripts/download-prebuilt.js +283 -0
- package/scripts/prebuilt-config.js +80 -0
- package/scripts/spm-autolinking-plugin.js +87 -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,695 @@
|
|
|
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 <godot_cpp/classes/display_server_embedded.hpp>
|
|
33
|
+
#include <godot_cpp/classes/engine.hpp>
|
|
34
|
+
#include <godot_cpp/classes/gd_extension_manager.hpp>
|
|
35
|
+
#include <godot_cpp/classes/rendering_native_surface.hpp>
|
|
36
|
+
#include <godot_cpp/classes/rendering_native_surface_apple.hpp>
|
|
37
|
+
#include <godot_cpp/godot.hpp>
|
|
38
|
+
|
|
39
|
+
#include <dlfcn.h>
|
|
40
|
+
|
|
41
|
+
#include <algorithm>
|
|
42
|
+
#include <map>
|
|
43
|
+
#include <mutex>
|
|
44
|
+
#include <string>
|
|
45
|
+
#include <vector>
|
|
46
|
+
|
|
47
|
+
#include "../cpp/AppleRendererPolicy.h"
|
|
48
|
+
#include "../cpp/LibGodotABI.h"
|
|
49
|
+
#include <TargetConditionals.h>
|
|
50
|
+
|
|
51
|
+
static constexpr NSInteger kGodotMaxFramesPerSecond = 60;
|
|
52
|
+
|
|
53
|
+
static NSInteger getGodotPreferredFramesPerSecond() {
|
|
54
|
+
godot::Engine *engine = godot::Engine::get_singleton();
|
|
55
|
+
if (!engine) {
|
|
56
|
+
return kGodotMaxFramesPerSecond;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const int32_t configuredMaxFps = engine->get_max_fps();
|
|
60
|
+
if (configuredMaxFps <= 0) {
|
|
61
|
+
return kGodotMaxFramesPerSecond;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const NSInteger requestedFramesPerSecond = static_cast<NSInteger>(configuredMaxFps);
|
|
65
|
+
return std::min(requestedFramesPerSecond, kGodotMaxFramesPerSecond);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static void configureGodotDisplayLink(CADisplayLink *displayLink) {
|
|
69
|
+
const NSInteger preferredFramesPerSecond = getGodotPreferredFramesPerSecond();
|
|
70
|
+
if (@available(iOS 15.0, *)) {
|
|
71
|
+
// Apple gives 30 Hz and 60 Hz special priority for games. Allowing the
|
|
72
|
+
// system to fall back to 30 Hz also handles thermal and power policies
|
|
73
|
+
// more gracefully than demanding an exact 60 Hz cadence.
|
|
74
|
+
const NSInteger minimumFramesPerSecond =
|
|
75
|
+
std::min<NSInteger>(30, preferredFramesPerSecond);
|
|
76
|
+
displayLink.preferredFrameRateRange = CAFrameRateRangeMake(
|
|
77
|
+
minimumFramesPerSecond,
|
|
78
|
+
preferredFramesPerSecond,
|
|
79
|
+
preferredFramesPerSecond);
|
|
80
|
+
} else {
|
|
81
|
+
displayLink.preferredFramesPerSecond = preferredFramesPerSecond;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
@interface GodotThread : NSObject
|
|
86
|
+
|
|
87
|
+
// Method to start the thread and run loop
|
|
88
|
+
- (void)start;
|
|
89
|
+
|
|
90
|
+
// Method to submit blocks to the background thread for execution
|
|
91
|
+
- (void)performBlock:(void (^)(void))block waitUntilDone:(bool)wait;
|
|
92
|
+
|
|
93
|
+
- (void)scheduleBlock:(void (^)(void))block;
|
|
94
|
+
|
|
95
|
+
- (void)step:(CADisplayLink *)sender;
|
|
96
|
+
|
|
97
|
+
@end
|
|
98
|
+
|
|
99
|
+
@interface GodotThread ()
|
|
100
|
+
|
|
101
|
+
// Strong reference to the thread
|
|
102
|
+
@property(nonatomic, strong) NSThread *thread;
|
|
103
|
+
|
|
104
|
+
// Run loop source and port
|
|
105
|
+
@property(nonatomic, strong) NSPort *runLoopPort;
|
|
106
|
+
|
|
107
|
+
@end
|
|
108
|
+
|
|
109
|
+
@implementation GodotThread
|
|
110
|
+
|
|
111
|
+
// Method to start the background thread
|
|
112
|
+
- (void)start {
|
|
113
|
+
self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadEntryPoint) object:nil];
|
|
114
|
+
self.thread.qualityOfService = NSQualityOfServiceUserInteractive;
|
|
115
|
+
[self.thread start];
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Entry point of the background thread
|
|
119
|
+
- (void)threadEntryPoint {
|
|
120
|
+
@autoreleasepool {
|
|
121
|
+
// Set up the run loop
|
|
122
|
+
self.runLoopPort = [NSPort port];
|
|
123
|
+
[[NSRunLoop currentRunLoop] addPort:self.runLoopPort forMode:NSDefaultRunLoopMode];
|
|
124
|
+
|
|
125
|
+
// Keep the run loop running
|
|
126
|
+
[[NSRunLoop currentRunLoop] run];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Method to perform a block on the background thread
|
|
131
|
+
- (void)performBlock:(void (^)(void))block waitUntilDone:(bool)wait {
|
|
132
|
+
if (!block) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Ensure the block is executed on the background thread
|
|
137
|
+
if ([[NSThread currentThread] isEqual:self.thread]) {
|
|
138
|
+
block();
|
|
139
|
+
} else {
|
|
140
|
+
[self performSelector:@selector(executeBlock:) onThread:self.thread withObject:[block copy] waitUntilDone:wait];
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
- (void)scheduleBlock:(void (^)(void))block {
|
|
145
|
+
if (!block) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
[self performSelector:@selector(executeBlock:) onThread:self.thread withObject:[block copy] waitUntilDone:FALSE];
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Helper method to execute the block on the background thread
|
|
153
|
+
- (void)executeBlock:(void (^)(void))block {
|
|
154
|
+
block();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
- (void)step:(CADisplayLink *)sender {
|
|
158
|
+
GodotModule::get_singleton()->iterate();
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
@end
|
|
162
|
+
|
|
163
|
+
struct ApplePlatformData : PlatformData {
|
|
164
|
+
CALayer *mainWindowLayer;
|
|
165
|
+
godot::Ref<godot::RenderingNativeSurface> mainSurface;
|
|
166
|
+
CGFloat contentScaleFactor = 1.0;
|
|
167
|
+
CADisplayLink *displayLink = nullptr;
|
|
168
|
+
bool in_background = true;
|
|
169
|
+
bool paused = false;
|
|
170
|
+
void *handle = nullptr;
|
|
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;
|
|
179
|
+
std::mutex windowUpdateMutex;
|
|
180
|
+
std::mutex createMutex;
|
|
181
|
+
GodotThread *thread = nil;
|
|
182
|
+
|
|
183
|
+
ApplePlatformData() {
|
|
184
|
+
thread = [[GodotThread alloc] init];
|
|
185
|
+
[thread start];
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
GodotModule *GodotModule::get_singleton() {
|
|
190
|
+
static GodotModule *singleton = new GodotModule(new ApplePlatformData());
|
|
191
|
+
return singleton;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// The engine/module is process-wide, so these observers survive JS reloads too.
|
|
195
|
+
static void observeApplicationLifecycle(GodotModule *module) {
|
|
196
|
+
static dispatch_once_t once;
|
|
197
|
+
dispatch_once(&once, ^{
|
|
198
|
+
void (^install)(void) = ^{
|
|
199
|
+
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
|
200
|
+
static NSArray *observers;
|
|
201
|
+
observers = @[
|
|
202
|
+
[center addObserverForName:UIApplicationWillResignActiveNotification
|
|
203
|
+
object:nil
|
|
204
|
+
queue:[NSOperationQueue mainQueue]
|
|
205
|
+
usingBlock:^(NSNotification *notification) {
|
|
206
|
+
module->appPause();
|
|
207
|
+
}],
|
|
208
|
+
[center addObserverForName:UIApplicationDidBecomeActiveNotification
|
|
209
|
+
object:nil
|
|
210
|
+
queue:[NSOperationQueue mainQueue]
|
|
211
|
+
usingBlock:^(NSNotification *notification) {
|
|
212
|
+
module->appResume();
|
|
213
|
+
}]
|
|
214
|
+
];
|
|
215
|
+
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
|
|
216
|
+
module->appResume();
|
|
217
|
+
} else {
|
|
218
|
+
module->appPause();
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
if ([NSThread isMainThread]) {
|
|
222
|
+
install();
|
|
223
|
+
} else {
|
|
224
|
+
dispatch_sync(dispatch_get_main_queue(), install);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
extern "C" {
|
|
230
|
+
|
|
231
|
+
static void initialize_default_module(godot::ModuleInitializationLevel p_level) {
|
|
232
|
+
if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
static void uninitialize_default_module(godot::ModuleInitializationLevel p_level) {
|
|
238
|
+
if (p_level != godot::MODULE_INITIALIZATION_LEVEL_SCENE) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
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
|
+
}
|
|
249
|
+
GodotModule *gm = (GodotModule *)p_data;
|
|
250
|
+
gm->log(p_log_message, p_err);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
GDExtensionBool GDE_EXPORT gdextension_default_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {
|
|
254
|
+
godot::GDExtensionBinding::InitObject init_object(p_get_proc_address, p_library, r_initialization);
|
|
255
|
+
|
|
256
|
+
init_object.register_initializer(initialize_default_module);
|
|
257
|
+
init_object.register_terminator(uninitialize_default_module);
|
|
258
|
+
init_object.set_minimum_library_initialization_level(godot::MODULE_INITIALIZATION_LEVEL_SCENE);
|
|
259
|
+
|
|
260
|
+
return init_object.init();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
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) {
|
|
274
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
275
|
+
|
|
276
|
+
// Make sure that we only run this method once at a time.
|
|
277
|
+
std::lock_guard createLock(data->createMutex);
|
|
278
|
+
|
|
279
|
+
{
|
|
280
|
+
std::lock_guard lock(_mutex);
|
|
281
|
+
if (_instance) {
|
|
282
|
+
return _instance;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
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());
|
|
296
|
+
observeApplicationLifecycle(this);
|
|
297
|
+
|
|
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;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
godot::GodotInstance *instance = nullptr;
|
|
312
|
+
|
|
313
|
+
std::vector<std::string> cmdline{ [[[NSBundle mainBundle] executablePath] UTF8String] };
|
|
314
|
+
for (const std::string &arg : args) {
|
|
315
|
+
cmdline.push_back(arg);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
std::vector<const char *> cargs{};
|
|
319
|
+
for (const std::string &arg : cmdline) {
|
|
320
|
+
cargs.push_back(arg.c_str());
|
|
321
|
+
}
|
|
322
|
+
|
|
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();
|
|
336
|
+
if (handle) {
|
|
337
|
+
dlclose(handle);
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
if (!instance_ptr) {
|
|
341
|
+
NSLog(@"Unable to create GodotInstance");
|
|
342
|
+
cleanupFailedCreate();
|
|
343
|
+
return nullptr;
|
|
344
|
+
}
|
|
345
|
+
|
|
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
|
+
}
|
|
352
|
+
|
|
353
|
+
CGRect screen = [[UIScreen mainScreen] bounds];
|
|
354
|
+
CGFloat contentScaleFactor = [[UIScreen mainScreen] scale];
|
|
355
|
+
|
|
356
|
+
LOGI("Initialize Main Window Layer on the main thread");
|
|
357
|
+
|
|
358
|
+
// Godot automatically creates the correct type of layer based on the selected rendering driver
|
|
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
|
+
}
|
|
365
|
+
CALayer __block *mainWindowLayer = (__bridge CALayer *)(void *)appleSurface->get_layer();
|
|
366
|
+
mainWindowLayer.bounds = CGRectMake(0, 0, screen.size.width, screen.size.height);
|
|
367
|
+
mainWindowLayer.position = CGPointMake(0, 0);
|
|
368
|
+
mainWindowLayer.anchorPoint = CGPointMake(0, 0);
|
|
369
|
+
mainWindowLayer.contentsScale = contentScaleFactor;
|
|
370
|
+
|
|
371
|
+
godot::RenderingNativeSurface *ptr = godot::Object::cast_to<godot::RenderingNativeSurface>(appleSurface.ptr());
|
|
372
|
+
godot::Ref<godot::RenderingNativeSurface> nativeSurface(ptr);
|
|
373
|
+
|
|
374
|
+
godot::DisplayServerEmbedded::set_native_surface(nativeSurface);
|
|
375
|
+
|
|
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
|
+
}
|
|
384
|
+
bool inBackground;
|
|
385
|
+
|
|
386
|
+
{
|
|
387
|
+
std::lock_guard lock(_mutex);
|
|
388
|
+
|
|
389
|
+
data->mainWindowLayer = mainWindowLayer;
|
|
390
|
+
data->mainSurface = nativeSurface;
|
|
391
|
+
data->contentScaleFactor = contentScaleFactor;
|
|
392
|
+
data->handle = handle;
|
|
393
|
+
data->func_libgodot_create_godot_instance = create;
|
|
394
|
+
data->func_libgodot_destroy_godot_instance = destroy;
|
|
395
|
+
|
|
396
|
+
_instance = instance;
|
|
397
|
+
_instanceGeneration.store(++_lastInstanceGeneration);
|
|
398
|
+
inBackground = data->in_background;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
if (inBackground) {
|
|
402
|
+
instance->focus_out();
|
|
403
|
+
instance->pause();
|
|
404
|
+
}
|
|
405
|
+
updateState();
|
|
406
|
+
updateWindows(true);
|
|
407
|
+
|
|
408
|
+
return instance;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
double GodotModule::get_content_scale_factor() {
|
|
412
|
+
std::lock_guard lock(_mutex);
|
|
413
|
+
if (!_instance) {
|
|
414
|
+
return 1.0;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
418
|
+
|
|
419
|
+
return data->contentScaleFactor;
|
|
420
|
+
}
|
|
421
|
+
|
|
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() {
|
|
430
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
431
|
+
|
|
432
|
+
// Make sure that we only run this method once at a time.
|
|
433
|
+
std::lock_guard createLock(data->createMutex);
|
|
434
|
+
|
|
435
|
+
if (!_instance) {
|
|
436
|
+
NSLog(@"Godot instance is already destroyed.");
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (!data->func_libgodot_destroy_godot_instance) {
|
|
441
|
+
NSLog(@"Missing destroy function for the active Godot instance");
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
{
|
|
446
|
+
std::lock_guard lock(_mutex);
|
|
447
|
+
|
|
448
|
+
[data->displayLink removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
|
|
449
|
+
data->displayLink = nil;
|
|
450
|
+
|
|
451
|
+
updateWindows(false);
|
|
452
|
+
_instanceGeneration.store(0);
|
|
453
|
+
|
|
454
|
+
godot::DisplayServerEmbedded::set_native_surface(godot::Ref<godot::RenderingNativeSurface>(nullptr));
|
|
455
|
+
data->mainWindowLayer = nil;
|
|
456
|
+
data->mainSurface = godot::Ref<godot::RenderingNativeSurface>(nullptr);
|
|
457
|
+
|
|
458
|
+
data->func_libgodot_destroy_godot_instance(_instance->_owner);
|
|
459
|
+
_instance = nullptr;
|
|
460
|
+
godot::GDExtensionBinding::deinit();
|
|
461
|
+
|
|
462
|
+
dlclose(data->handle);
|
|
463
|
+
data->handle = nullptr;
|
|
464
|
+
data->func_libgodot_create_godot_instance = nullptr;
|
|
465
|
+
data->func_libgodot_destroy_godot_instance = nullptr;
|
|
466
|
+
data->paused = false;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
godot::Ref<godot::RenderingNativeSurface> GodotModule::get_main_rendering_surface() {
|
|
471
|
+
std::lock_guard lock(_mutex);
|
|
472
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
473
|
+
return data->mainSurface;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
void *GodotModule::get_main_rendering_layer() {
|
|
477
|
+
std::lock_guard lock(_mutex);
|
|
478
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
479
|
+
return (__bridge void *)data->mainWindowLayer;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
void GodotModule::focus_out() {
|
|
483
|
+
std::lock_guard lock(_mutex);
|
|
484
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
485
|
+
data->in_background = true;
|
|
486
|
+
[data->thread scheduleBlock:^{
|
|
487
|
+
std::lock_guard lock(_mutex);
|
|
488
|
+
if (_instance) {
|
|
489
|
+
_instance->focus_out();
|
|
490
|
+
}
|
|
491
|
+
}];
|
|
492
|
+
updateState();
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
void GodotModule::focus_in() {
|
|
496
|
+
std::lock_guard lock(_mutex);
|
|
497
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
498
|
+
data->in_background = false;
|
|
499
|
+
[data->thread scheduleBlock:^{
|
|
500
|
+
std::lock_guard lock(_mutex);
|
|
501
|
+
if (_instance) {
|
|
502
|
+
_instance->focus_in();
|
|
503
|
+
}
|
|
504
|
+
}];
|
|
505
|
+
updateState();
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
bool GodotModule::is_paused() {
|
|
509
|
+
std::lock_guard lock(_mutex);
|
|
510
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
511
|
+
return data->paused;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
void GodotModule::appPause() {
|
|
515
|
+
std::lock_guard lock(_mutex);
|
|
516
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
517
|
+
if (data->in_background) {
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
|
|
521
|
+
|
|
522
|
+
}];
|
|
523
|
+
data->in_background = true;
|
|
524
|
+
[data->thread scheduleBlock:^{
|
|
525
|
+
godot::GodotInstance *instance = get_instance();
|
|
526
|
+
if (instance) {
|
|
527
|
+
instance->focus_out();
|
|
528
|
+
instance->pause();
|
|
529
|
+
}
|
|
530
|
+
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
|
|
531
|
+
}];
|
|
532
|
+
updateState();
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
void GodotModule::appResume() {
|
|
536
|
+
std::lock_guard lock(_mutex);
|
|
537
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
538
|
+
if (!data->in_background) {
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
data->in_background = false;
|
|
542
|
+
[data->thread scheduleBlock:^{
|
|
543
|
+
godot::GodotInstance *instance = get_instance();
|
|
544
|
+
if (instance) {
|
|
545
|
+
instance->resume();
|
|
546
|
+
instance->focus_in();
|
|
547
|
+
}
|
|
548
|
+
}];
|
|
549
|
+
updateState();
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
void GodotModule::pause() {
|
|
553
|
+
std::lock_guard lock(_mutex);
|
|
554
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
555
|
+
data->paused = true;
|
|
556
|
+
updateState();
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
void GodotModule::resume() {
|
|
560
|
+
std::lock_guard lock(_mutex);
|
|
561
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
562
|
+
data->paused = false;
|
|
563
|
+
updateState();
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
void GodotModule::updateState() {
|
|
567
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
568
|
+
[data->thread scheduleBlock:^{
|
|
569
|
+
std::lock_guard lock(_mutex);
|
|
570
|
+
ApplePlatformData *threadData = static_cast<ApplePlatformData *>(_data);
|
|
571
|
+
if (!_instance) {
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
if (threadData->in_background || threadData->paused) {
|
|
575
|
+
if (threadData->displayLink) {
|
|
576
|
+
threadData->displayLink.paused = true;
|
|
577
|
+
[threadData->displayLink invalidate];
|
|
578
|
+
threadData->displayLink = nil;
|
|
579
|
+
}
|
|
580
|
+
} else if (!threadData->displayLink) {
|
|
581
|
+
threadData->displayLink = [CADisplayLink displayLinkWithTarget:threadData->thread
|
|
582
|
+
selector:@selector(step:)];
|
|
583
|
+
configureGodotDisplayLink(threadData->displayLink);
|
|
584
|
+
[threadData->displayLink addToRunLoop:[NSRunLoop currentRunLoop]
|
|
585
|
+
forMode:NSRunLoopCommonModes];
|
|
586
|
+
}
|
|
587
|
+
}];
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
class CPPCallable : public godot::CallableCustom {
|
|
591
|
+
// friend bool javascript_callable_compare_equal_func(const godot::CallableCustom *p_a, const godot::CallableCustom *p_b) {
|
|
592
|
+
// if (p_a == p_b) return true;
|
|
593
|
+
// const JavascriptCallable *a = static_cast<const JavascriptCallable *>(p_a);
|
|
594
|
+
// const JavascriptCallable *b = static_cast<const JavascriptCallable *>(p_b);
|
|
595
|
+
// return jsi::Function::strictEquals(a->_rt, a->_func, b->_func);
|
|
596
|
+
// };
|
|
597
|
+
|
|
598
|
+
std::function<void(const godot::Variant **, int, godot::Variant &, GDExtensionCallError &)> _func;
|
|
599
|
+
|
|
600
|
+
public:
|
|
601
|
+
CPPCallable(std::function<void(const godot::Variant **, int, godot::Variant &, GDExtensionCallError &)> f) :
|
|
602
|
+
_func(f) {}
|
|
603
|
+
|
|
604
|
+
uint32_t hash() const override {
|
|
605
|
+
return 0; // Use default hash function
|
|
606
|
+
}
|
|
607
|
+
godot::String get_as_text() const override {
|
|
608
|
+
return godot::String("CPPCallable");
|
|
609
|
+
}
|
|
610
|
+
CompareEqualFunc get_compare_equal_func() const override {
|
|
611
|
+
return nullptr;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
CompareLessFunc get_compare_less_func() const override {
|
|
615
|
+
return nullptr;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
bool is_valid() const override {
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
godot::ObjectID get_object() const override {
|
|
622
|
+
return godot::ObjectID();
|
|
623
|
+
}
|
|
624
|
+
void call(const godot::Variant **p_arguments, int p_argcount, godot::Variant &r_return_value, GDExtensionCallError &r_call_error) const override {
|
|
625
|
+
_func(p_arguments, p_argcount, r_return_value, r_call_error);
|
|
626
|
+
r_call_error.error = GDEXTENSION_CALL_OK;
|
|
627
|
+
}
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
godot::Callable GodotModule::create_callable(std::function<void(const godot::Variant **, int, godot::Variant &, GDExtensionCallError &)> f) {
|
|
631
|
+
return godot::Callable(memnew(CPPCallable(f)));
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
void GodotModule::registerWindowUpdateCallback(std::string name, void *handle, std::function<void(bool)> f, void *ref) {
|
|
635
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
636
|
+
std::lock_guard lock(data->windowUpdateMutex);
|
|
637
|
+
data->windowUpdateCallbacks[handle] = { std::move(name), std::move(f) };
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
void GodotModule::unregisterWindowUpdateCallback(void *handle) {
|
|
641
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
642
|
+
std::lock_guard lock(data->windowUpdateMutex);
|
|
643
|
+
data->windowUpdateCallbacks.erase(handle);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
void GodotModule::updateWindow(std::string name, bool adding) {
|
|
647
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
648
|
+
std::vector<std::function<void(bool)>> callbacks;
|
|
649
|
+
{
|
|
650
|
+
std::lock_guard lock(data->windowUpdateMutex);
|
|
651
|
+
for (const auto &[handle, entry] : data->windowUpdateCallbacks) {
|
|
652
|
+
if (entry.name == name) {
|
|
653
|
+
callbacks.push_back(entry.callback);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
for (const auto &callback : callbacks) {
|
|
658
|
+
callback(adding);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
void GodotModule::updateWindows(bool adding) {
|
|
663
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
664
|
+
std::vector<std::function<void(bool)>> callbacks;
|
|
665
|
+
{
|
|
666
|
+
std::lock_guard lock(data->windowUpdateMutex);
|
|
667
|
+
for (const auto &[handle, entry] : data->windowUpdateCallbacks) {
|
|
668
|
+
callbacks.push_back(entry.callback);
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
for (const auto &callback : callbacks) {
|
|
672
|
+
callback(adding);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
void GodotModule::runOnGodotThread(std::function<void()> f, bool wait) {
|
|
677
|
+
ApplePlatformData *data = static_cast<ApplePlatformData *>(_data);
|
|
678
|
+
[data->thread
|
|
679
|
+
performBlock:^{
|
|
680
|
+
f();
|
|
681
|
+
}
|
|
682
|
+
waitUntilDone:wait];
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
void GodotModule::iterate(std::optional<uint64_t> frameGeneration) {
|
|
686
|
+
(void)frameGeneration;
|
|
687
|
+
godot::GodotInstance *instance = nullptr;
|
|
688
|
+
{
|
|
689
|
+
std::lock_guard lock(_mutex);
|
|
690
|
+
instance = _instance;
|
|
691
|
+
}
|
|
692
|
+
if (instance && instance->is_started()) {
|
|
693
|
+
instance->iteration();
|
|
694
|
+
}
|
|
695
|
+
}
|