Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Godot reinstantiation on iOS for embedding in a single view #99705

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1587,3 +1587,7 @@ ProjectSettings::~ProjectSettings() {
singleton = nullptr;
}
}

void ProjectSettings::reset() {
singleton = nullptr;
}
1 change: 1 addition & 0 deletions core/config/project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class ProjectSettings : public Object {
#ifdef TOOLS_ENABLED
virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
#endif
static void reset();

ProjectSettings();
ProjectSettings(const String &p_path);
Expand Down
1 change: 1 addition & 0 deletions core/extension/gdextension_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,5 @@ GDExtensionManager::~GDExtensionManager() {
#ifndef DISABLE_DEPRECATED
GDExtensionCompatHashes::finalize();
#endif
singleton = nullptr;
}
2 changes: 2 additions & 0 deletions core/io/file_access_pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ PackedData::~PackedData() {
memdelete(sources[i]);
}
_free_packed_dirs(root);

singleton = nullptr;
}

//////////////////////////////////////////////////////////////////
Expand Down
1 change: 1 addition & 0 deletions core/io/file_access_zip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ ZipArchive::~ZipArchive() {
}

packages.clear();
instance = nullptr;
}

Error FileAccessZip::open_internal(const String &p_path, int p_mode_flags) {
Expand Down
1 change: 1 addition & 0 deletions core/io/ip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,5 @@ IP::~IP() {
resolver->thread.wait_to_finish();

memdelete(resolver);
singleton = nullptr;
}
1 change: 1 addition & 0 deletions core/io/resource_uid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,5 @@ ResourceUID::ResourceUID() {
}
ResourceUID::~ResourceUID() {
memdelete((CryptoCore::RandomGenerator *)crypto);
singleton = nullptr;
}
11 changes: 9 additions & 2 deletions core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include "core/templates/local_vector.h"
#include "core/variant/typed_array.h"

bool Object::initialized = false;

#ifdef DEBUG_ENABLED

struct _ObjectDebugLock {
Expand Down Expand Up @@ -1504,14 +1506,15 @@ Variant Object::_get_indexed_bind(const NodePath &p_name) const {
}

void Object::initialize_class() {
static bool initialized = false;
if (initialized) {
static int local_version = -1;
if (Main::version == local_version) {
return;
}
ClassDB::_add_class<Object>();
_bind_methods();
_bind_compatibility_methods();
initialized = true;
local_version++;
}

String Object::tr(const StringName &p_message, const StringName &p_context) const {
Expand Down Expand Up @@ -2319,7 +2322,11 @@ void ObjectDB::cleanup() {

if (object_slots) {
memfree(object_slots);
object_slots = nullptr;
}

slot_count = 0;
slot_max = 0;
object_slots = nullptr;
spin_lock.unlock();
}
44 changes: 33 additions & 11 deletions core/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include "core/templates/safe_refcount.h"
#include "core/variant/callable_bind.h"
#include "core/variant/variant.h"
#include "main/main.h"
#include <queue>

template <typename T>
class TypedArray;
Expand Down Expand Up @@ -411,11 +413,20 @@ public:
return String(#m_class); \
} \
virtual const StringName *_get_class_namev() const override { \
static StringName _class_name_static; \
if (unlikely(!_class_name_static)) { \
StringName::assign_static_unique_class_name(&_class_name_static, #m_class); \
static std::queue<StringName> _class_name_statics; \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static int local_version = -1; \
if (unlikely(Main::version != local_version)) { \
local_version = Main::version; \
if (_class_name_statics.size() > 0) { \
/* TODO: find a way to pop old refs */ \
/*_class_name_statics.pop();*/ \
} \
_class_name_statics.push(StringName()); \
} \
if (unlikely(!_class_name_statics.back())) { \
StringName::assign_static_unique_class_name(&_class_name_statics.back(), #m_class); \
} \
return &_class_name_static; \
return &_class_name_statics.back(); \
} \
static _FORCE_INLINE_ void *get_class_ptr_static() { \
static int ptr; \
Expand Down Expand Up @@ -457,8 +468,8 @@ protected:
\
public: \
static void initialize_class() { \
static bool initialized = false; \
if (initialized) { \
static int local_version = -1; \
if (Main::version == local_version) { \
return; \
} \
m_inherits::initialize_class(); \
Expand All @@ -469,7 +480,7 @@ public:
if (m_class::_get_bind_compatibility_methods() != m_inherits::_get_bind_compatibility_methods()) { \
_bind_compatibility_methods(); \
} \
initialized = true; \
local_version++; \
} \
\
protected: \
Expand Down Expand Up @@ -601,6 +612,8 @@ class Object {
Connection(const Variant &p_variant);
};

static bool initialized;

private:
#ifdef DEBUG_ENABLED
friend struct _ObjectDebugLock;
Expand Down Expand Up @@ -753,11 +766,20 @@ class Object {
Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);

virtual const StringName *_get_class_namev() const {
static StringName _class_name_static;
if (unlikely(!_class_name_static)) {
StringName::assign_static_unique_class_name(&_class_name_static, "Object");
static std::queue<StringName> _class_name_statics;
static int local_version = -1;
if (unlikely(Main::version != local_version)) {
local_version = Main::version;
if (_class_name_statics.size() > 0) {
/* TODO: pop unused references */
/*_class_name_statics.pop();*/
}
_class_name_statics.push(StringName());
}
if (unlikely(!_class_name_statics.back())) {
StringName::assign_static_unique_class_name(&_class_name_statics.back(), "Object");
}
return &_class_name_static;
return &_class_name_statics.back();
}

TypedArray<StringName> _get_meta_list_bind() const;
Expand Down
11 changes: 10 additions & 1 deletion core/string/string_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ StringName _scs_create(const char *p_chr, bool p_static = false);
* Use in places that can be called hundreds of times per frame (or more) is recommended, but this situation is very rare. If in doubt, do not use.
*/

#define SNAME(m_arg) ([]() -> const StringName & { static StringName sname = _scs_create(m_arg, true); return sname; })()
// TODO: reinstate caching
#define SNAME(m_arg) ([]() -> const StringName & { \
static std::queue<StringName> snames; \
static int local_version = -1; \
if (unlikely(Main::version != local_version)) { \
local_version = Main::version; \
snames.push(_scs_create(m_arg, true)); \
} \
return snames.back(); \
})()

#endif // STRING_NAME_H
4 changes: 4 additions & 0 deletions core/templates/paged_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ class PagedAllocator {
available_pool = nullptr;
pages_allocated = 0;
allocs_available = 0;

page_shift = 0;
page_mask = 0;
page_size = 0;
}
}

Expand Down
8 changes: 8 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#include "servers/physics_server_3d.h"
#include "servers/xr_server.h"
#endif // _3D_DISABLED
#include "servers/rendering/renderer_rd/framebuffer_cache_rd.h"

#ifdef TESTS_ENABLED
#include "tests/test_main.h"
Expand Down Expand Up @@ -259,6 +260,7 @@ static const String NULL_AUDIO_DRIVER("Dummy");
// (excluding the 2-space left and right margins).
// Currently, this is `--export-release <preset> <path>`.
static const int OPTION_COLUMN_LENGTH = 32;
int Main::version = 0;

/* Helper methods */

Expand Down Expand Up @@ -4508,6 +4510,7 @@ void Main::cleanup(bool p_force) {
memdelete(steam_tracker);
}
#endif
ProjectSettings::reset();

unregister_core_driver_types();
unregister_core_extensions();
Expand All @@ -4523,4 +4526,9 @@ void Main::cleanup(bool p_force) {
OS::get_singleton()->benchmark_dump();

OS::get_singleton()->finalize_core();

FramebufferCacheRD::reset();
Object::initialized = false;
Main::version++;
AudioDriverManager::reset();
}
1 change: 1 addition & 0 deletions main/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Main {

public:
static bool is_cmdline_tool();
static int version;
#ifdef TOOLS_ENABLED
enum CLIScope {
CLI_SCOPE_TOOL, // Editor and project manager.
Expand Down
1 change: 1 addition & 0 deletions modules/navigation/2d/nav_mesh_generator_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ NavMeshGenerator2D::NavMeshGenerator2D() {

NavMeshGenerator2D::~NavMeshGenerator2D() {
cleanup();
singleton = nullptr;
}

void NavMeshGenerator2D::sync() {
Expand Down
1 change: 1 addition & 0 deletions modules/navigation/3d/nav_mesh_generator_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ NavMeshGenerator3D::NavMeshGenerator3D() {

NavMeshGenerator3D::~NavMeshGenerator3D() {
cleanup();
singleton = nullptr;
}

void NavMeshGenerator3D::sync() {
Expand Down
1 change: 1 addition & 0 deletions platform/ios/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def generate_bundle(target, source, env):


ios_lib = [
"godot_ios_wrapper.mm",
"godot_ios.mm",
"os_ios.mm",
"main.m",
Expand Down
4 changes: 3 additions & 1 deletion platform/ios/app_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
//#endif

@property(strong, nonatomic) UIWindow *window;
@property(strong, class, readonly, nonatomic) ViewController *viewController;
@property(strong, class, nonatomic) ViewController *viewController;
+ (void)focusIn;
+ (void)focusOut;

@end
12 changes: 12 additions & 0 deletions platform/ios/app_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ + (ViewController *)viewController {
return mainViewController;
}

+ (void)setViewController:(ViewController *)newVC {
mainViewController = newVC;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// TODO: might be required to make an early return, so app wouldn't crash because of timeout.
// TODO: logo screen is not displayed while shaders are compiling
Expand Down Expand Up @@ -183,4 +187,12 @@ - (void)dealloc {
self.window = nil;
}

+ (void)focusIn {
OS_IOS::get_singleton()->on_focus_in();
}

+ (void)focusOut {
OS_IOS::get_singleton()->on_focus_out();
}

@end
10 changes: 10 additions & 0 deletions platform/ios/godot_ios_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// godot_ios_wrapper.h

#import <Foundation/Foundation.h>

@interface GodotIosWrapper : NSObject

+ (void)ios_finish_wrapper;
+ (int)ios_main_wrapper:(int)argc argv:(char **)argv;

@end
18 changes: 18 additions & 0 deletions platform/ios/godot_ios_wrapper.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// godot_ios_wrapper.mm

#import "godot_ios_wrapper.h"

@implementation GodotIosWrapper

extern int ios_main(int, char **);
extern void ios_finish();

+ (void)ios_finish_wrapper {
ios_finish();
}

+ (int)ios_main_wrapper:(int)argc argv:(char **)argv {
return ios_main(argc, argv);
}

@end
4 changes: 3 additions & 1 deletion platform/web/api/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ JavaScriptBridge::JavaScriptBridge() {
singleton = this;
}

JavaScriptBridge::~JavaScriptBridge() {}
JavaScriptBridge::~JavaScriptBridge() {
singleton = nullptr;
}

void JavaScriptBridge::_bind_methods() {
ClassDB::bind_method(D_METHOD("eval", "code", "use_global_execution_context"), &JavaScriptBridge::eval, DEFVAL(false));
Expand Down
4 changes: 3 additions & 1 deletion scene/main/multiplayer_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ class MultiplayerAPI : public RefCounted {
bool is_server() { return get_unique_id() == MultiplayerPeer::TARGET_PEER_SERVER; }

MultiplayerAPI() {}
virtual ~MultiplayerAPI() {}
virtual ~MultiplayerAPI() {
default_interface = StringName();
}
};

VARIANT_ENUM_CAST(MultiplayerAPI::RPCMode);
Expand Down
10 changes: 10 additions & 0 deletions servers/audio_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ int AudioDriverManager::get_driver_count() {
return driver_count;
}

void AudioDriverManager::reset() {
AudioDriverManager::driver_count = 1;

for (int i = 0; i < driver_count; i++) {
drivers[i] = nullptr;
}

drivers[0] = &AudioDriverManager::dummy_driver;
}

void AudioDriverManager::initialize(int p_driver) {
GLOBAL_DEF_RST("audio/driver/enable_input", false);
GLOBAL_DEF_RST(PropertyInfo(Variant::INT, "audio/driver/mix_rate", PROPERTY_HINT_RANGE, "11025,192000,1,or_greater,suffix:Hz"), DEFAULT_MIX_RATE);
Expand Down
1 change: 1 addition & 0 deletions servers/audio_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class AudioDriverManager {
static void initialize(int p_driver);
static int get_driver_count();
static AudioDriver *get_driver(int p_driver);
static void reset();
};

class AudioBusLayout;
Expand Down
1 change: 1 addition & 0 deletions servers/rendering/renderer_compositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ RendererCompositor *(*RendererCompositor::_create_func)() = nullptr;
bool RendererCompositor::low_end = false;

RendererCompositor *RendererCompositor::create() {
RendererCompositor::singleton = nullptr;
return _create_func();
}

Expand Down
1 change: 1 addition & 0 deletions servers/rendering/renderer_rd/framebuffer_cache_rd.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ class FramebufferCacheRD : public Object {
static RID get_cache_multipass_array(const TypedArray<RID> &p_textures, const TypedArray<RDFramebufferPass> &p_passes, uint32_t p_views = 1);

static FramebufferCacheRD *get_singleton() { return singleton; }
static void reset() { singleton = nullptr; }

FramebufferCacheRD();
~FramebufferCacheRD();
Expand Down
Loading