Merge pull request #15395 from karroffel/gdnative-reloading

[GDNative] added reload property
This commit is contained in:
Rémi Verschelde 2018-01-06 12:50:11 +01:00 committed by GitHub
commit 9884597bb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 12 deletions

View File

@ -38,9 +38,12 @@
#include "scene/main/scene_tree.h"
const String init_symbol = "gdnative_init";
const String terminate_symbol = "gdnative_terminate";
const String default_symbol_prefix = "godot_";
static const String init_symbol = "gdnative_init";
static const String terminate_symbol = "gdnative_terminate";
static const String default_symbol_prefix = "godot_";
static const bool default_singleton = false;
static const bool default_load_once = true;
static const bool default_reloadable = true;
// Defined in gdnative_api_struct.gen.cpp
extern const godot_gdnative_core_api_struct api_struct;
@ -51,6 +54,9 @@ GDNativeLibrary::GDNativeLibrary() {
config_file.instance();
symbol_prefix = default_symbol_prefix;
load_once = default_load_once;
singleton = default_singleton;
reloadable = default_reloadable;
if (GDNativeLibrary::loaded_libraries == NULL) {
GDNativeLibrary::loaded_libraries = memnew((Map<String, Vector<Ref<GDNative> > >));
@ -69,14 +75,17 @@ void GDNativeLibrary::_bind_methods() {
ClassDB::bind_method(D_METHOD("should_load_once"), &GDNativeLibrary::should_load_once);
ClassDB::bind_method(D_METHOD("is_singleton"), &GDNativeLibrary::is_singleton);
ClassDB::bind_method(D_METHOD("get_symbol_prefix"), &GDNativeLibrary::get_symbol_prefix);
ClassDB::bind_method(D_METHOD("is_reloadable"), &GDNativeLibrary::is_reloadable);
ClassDB::bind_method(D_METHOD("set_load_once", "load_once"), &GDNativeLibrary::set_load_once);
ClassDB::bind_method(D_METHOD("set_singleton", "singleton"), &GDNativeLibrary::set_singleton);
ClassDB::bind_method(D_METHOD("set_symbol_prefix", "symbol_prefix"), &GDNativeLibrary::set_symbol_prefix);
ClassDB::bind_method(D_METHOD("set_reloadable", "reloadable"), &GDNativeLibrary::set_reloadable);
ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "load_once"), "set_load_once", "should_load_once");
ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "singleton"), "set_singleton", "is_singleton");
ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "symbol_prefix"), "set_symbol_prefix", "get_symbol_prefix");
ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "reloadable"), "set_reloadable", "is_reloadable");
}
GDNative::GDNative() {
@ -318,9 +327,10 @@ RES GDNativeLibraryResourceLoader::load(const String &p_path, const String &p_or
*r_error = err;
}
lib->set_singleton(config->get_value("general", "singleton", false));
lib->set_load_once(config->get_value("general", "load_once", true));
lib->set_singleton(config->get_value("general", "singleton", default_singleton));
lib->set_load_once(config->get_value("general", "load_once", default_load_once));
lib->set_symbol_prefix(config->get_value("general", "symbol_prefix", default_symbol_prefix));
lib->set_reloadable(config->get_value("general", "reloadable", default_reloadable));
String entry_lib_path;
{
@ -389,6 +399,7 @@ RES GDNativeLibraryResourceLoader::load(const String &p_path, const String &p_or
}
void GDNativeLibraryResourceLoader::get_recognized_extensions(List<String> *p_extensions) const {
p_extensions->clear();
p_extensions->push_back("gdnlib");
}
@ -416,6 +427,7 @@ Error GDNativeLibraryResourceSaver::save(const String &p_path, const RES &p_reso
config->set_value("general", "singleton", lib->is_singleton());
config->set_value("general", "load_once", lib->should_load_once());
config->set_value("general", "symbol_prefix", lib->get_symbol_prefix());
config->set_value("general", "reloadable", lib->is_reloadable());
return config->save(p_path);
}
@ -426,6 +438,7 @@ bool GDNativeLibraryResourceSaver::recognize(const RES &p_resource) const {
void GDNativeLibraryResourceSaver::get_recognized_extensions(const RES &p_resource, List<String> *p_extensions) const {
if (Object::cast_to<GDNativeLibrary>(*p_resource) != NULL) {
p_extensions->clear();
p_extensions->push_back("gdnlib");
}
}

View File

@ -60,6 +60,7 @@ class GDNativeLibrary : public Resource {
bool singleton;
bool load_once;
String symbol_prefix;
bool reloadable;
public:
GDNativeLibrary();
@ -87,6 +88,10 @@ public:
return symbol_prefix;
}
_FORCE_INLINE_ bool is_reloadable() const {
return reloadable;
}
_FORCE_INLINE_ void set_load_once(bool p_load_once) {
load_once = p_load_once;
}
@ -97,6 +102,10 @@ public:
symbol_prefix = p_symbol_prefix;
}
_FORCE_INLINE_ void set_reloadable(bool p_reloadable) {
reloadable = p_reloadable;
}
static void _bind_methods();
};

View File

@ -1111,7 +1111,13 @@ void NativeReloadNode::_notification(int p_what) {
NSL->_unload_stuff();
for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
L->get()->terminate();
Ref<GDNative> gdn = L->get();
if (!gdn->get_library()->is_reloadable()) {
continue;
}
gdn->terminate();
NSL->library_classes.erase(L->key());
}
@ -1129,21 +1135,23 @@ void NativeReloadNode::_notification(int p_what) {
Set<StringName> libs_to_remove;
for (Map<String, Ref<GDNative> >::Element *L = NSL->library_gdnatives.front(); L; L = L->next()) {
if (!L->get()->initialize()) {
Ref<GDNative> gdn = L->get();
if (!gdn->get_library()->is_reloadable()) {
continue;
}
if (!gdn->initialize()) {
libs_to_remove.insert(L->key());
continue;
}
NSL->library_classes.insert(L->key(), Map<StringName, NativeScriptDesc>());
void *args[1] = {
(void *)&L->key()
};
// here the library registers all the classes and stuff.
void *proc_ptr;
Error err = L->get()->get_symbol(L->get()->get_library()->get_symbol_prefix() + "nativescript_init", proc_ptr);
Error err = gdn->get_symbol(gdn->get_library()->get_symbol_prefix() + "nativescript_init", proc_ptr);
if (err != OK) {
ERR_PRINT(String("No godot_nativescript_init in \"" + L->key() + "\" found").utf8().get_data());
} else {